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. |
Johnny Chen | 2434884 | 2011-02-23 00:15:56 +0000 | [diff] [blame] | 27 | #define APSR_C Bit32(m_inst_cpsr, CPSR_C_POS) |
| 28 | #define APSR_V Bit32(m_inst_cpsr, CPSR_V_POS) |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 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 |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 113 | #define SP_REG 13 |
| 114 | #define LR_REG 14 |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 115 | #define PC_REG 15 |
| 116 | #define PC_REGLIST_BIT 0x8000 |
| 117 | |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 118 | #define ARMv4 (1u << 0) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 119 | #define ARMv4T (1u << 1) |
| 120 | #define ARMv5T (1u << 2) |
| 121 | #define ARMv5TE (1u << 3) |
| 122 | #define ARMv5TEJ (1u << 4) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 123 | #define ARMv6 (1u << 5) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 124 | #define ARMv6K (1u << 6) |
| 125 | #define ARMv6T2 (1u << 7) |
Johnny Chen | 251af6a | 2011-01-21 22:47:25 +0000 | [diff] [blame] | 126 | #define ARMv7 (1u << 8) |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 127 | #define ARMv8 (1u << 9) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 128 | #define ARMvAll (0xffffffffu) |
| 129 | |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 130 | #define ARMV4T_ABOVE (ARMv4T|ARMv5T|ARMv5TE|ARMv5TEJ|ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8) |
| 131 | #define ARMV5_ABOVE (ARMv5T|ARMv5TE|ARMv5TEJ|ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8) |
Johnny Chen | 59e6ab7 | 2011-02-24 21:01:20 +0000 | [diff] [blame] | 132 | #define ARMV5J_ABOVE (ARMv5TEJ|ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8) |
Caroline Tice | 6bf6516 | 2011-03-03 17:42:58 +0000 | [diff] [blame] | 133 | #define ARMV6_ABOVE (ARMv6|ARMv6K|ARMv6T2|ARMv7|ARMv8) |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 134 | #define ARMV6T2_ABOVE (ARMv6T2|ARMv7|ARMv8) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 135 | |
Johnny Chen | 0e00af2 | 2011-02-10 19:40:42 +0000 | [diff] [blame] | 136 | //---------------------------------------------------------------------- |
| 137 | // |
| 138 | // EmulateInstructionARM implementation |
| 139 | // |
| 140 | //---------------------------------------------------------------------- |
| 141 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 142 | void |
| 143 | EmulateInstructionARM::Initialize () |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 144 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 145 | } |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 146 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 147 | void |
| 148 | EmulateInstructionARM::Terminate () |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 149 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 152 | // Write "bits (32) UNKNOWN" to memory address "address". Helper function for many ARM instructions. |
| 153 | bool |
| 154 | EmulateInstructionARM::WriteBits32UnknownToMemory (addr_t address) |
| 155 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 156 | EmulateInstruction::Context context; |
| 157 | context.type = EmulateInstruction::eContextWriteMemoryRandomBits; |
| 158 | context.SetNoArgs (); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 159 | |
| 160 | uint32_t random_data = rand (); |
| 161 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 162 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 163 | if (!MemAWrite (context, address, random_data, addr_byte_size)) |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 164 | return false; |
| 165 | |
| 166 | return true; |
| 167 | } |
| 168 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 169 | // Write "bits (32) UNKNOWN" to register n. Helper function for many ARM instructions. |
| 170 | bool |
| 171 | EmulateInstructionARM::WriteBits32Unknown (int n) |
| 172 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 173 | EmulateInstruction::Context context; |
| 174 | context.type = EmulateInstruction::eContextWriteRegisterRandomBits; |
| 175 | context.SetNoArgs (); |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 176 | |
Johnny Chen | 62ff6f5 | 2011-02-11 18:11:22 +0000 | [diff] [blame] | 177 | bool success; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 178 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 179 | |
| 180 | if (!success) |
| 181 | return false; |
| 182 | |
| 183 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 184 | return false; |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
Johnny Chen | 08c25e8 | 2011-01-31 18:02:28 +0000 | [diff] [blame] | 189 | // Push Multiple Registers stores multiple registers to the stack, storing to |
| 190 | // consecutive memory locations ending just below the address in SP, and updates |
| 191 | // SP to point to the start of the stored data. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 192 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 193 | EmulateInstructionARM::EmulatePUSH (ARMEncoding encoding) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 194 | { |
| 195 | #if 0 |
| 196 | // ARM pseudo code... |
| 197 | if (ConditionPassed()) |
| 198 | { |
| 199 | EncodingSpecificOperations(); |
| 200 | NullCheckIfThumbEE(13); |
| 201 | address = SP - 4*BitCount(registers); |
| 202 | |
| 203 | for (i = 0 to 14) |
| 204 | { |
| 205 | if (registers<i> == ’1’) |
| 206 | { |
| 207 | if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1 |
| 208 | MemA[address,4] = bits(32) UNKNOWN; |
| 209 | else |
| 210 | MemA[address,4] = R[i]; |
| 211 | address = address + 4; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | if (registers<15> == ’1’) // Only possible for encoding A1 or A2 |
| 216 | MemA[address,4] = PCStoreValue(); |
| 217 | |
| 218 | SP = SP - 4*BitCount(registers); |
| 219 | } |
| 220 | #endif |
| 221 | |
| 222 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 223 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 224 | if (!success) |
| 225 | return false; |
| 226 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 227 | if (ConditionPassed()) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 228 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 229 | const uint32_t addr_byte_size = GetAddressByteSize(); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 230 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 231 | if (!success) |
| 232 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 233 | uint32_t registers = 0; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 234 | uint32_t Rt; // the source register |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 235 | switch (encoding) { |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 236 | case eEncodingT1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 237 | registers = Bits32(opcode, 7, 0); |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 238 | // The M bit represents LR. |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 239 | if (Bit32(opcode, 8)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 240 | registers |= (1u << 14); |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 241 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 242 | if (BitCount(registers) < 1) |
| 243 | return false; |
| 244 | break; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 245 | case eEncodingT2: |
| 246 | // Ignore bits 15 & 13. |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 247 | registers = Bits32(opcode, 15, 0) & ~0xa000; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 248 | // if BitCount(registers) < 2 then UNPREDICTABLE; |
| 249 | if (BitCount(registers) < 2) |
| 250 | return false; |
| 251 | break; |
| 252 | case eEncodingT3: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 253 | Rt = Bits32(opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 254 | // if BadReg(t) then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 255 | if (BadReg(Rt)) |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 256 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 257 | registers = (1u << Rt); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 258 | break; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 259 | case eEncodingA1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 260 | registers = Bits32(opcode, 15, 0); |
Johnny Chen | a33d484 | 2011-01-24 22:25:48 +0000 | [diff] [blame] | 261 | // Instead of return false, let's handle the following case as well, |
| 262 | // which amounts to pushing one reg onto the full descending stacks. |
| 263 | // if BitCount(register_list) < 2 then SEE STMDB / STMFD; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 264 | break; |
| 265 | case eEncodingA2: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 266 | Rt = Bits32(opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 267 | // if t == 13 then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 268 | if (Rt == dwarf_sp) |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 269 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 270 | registers = (1u << Rt); |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 271 | break; |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 272 | default: |
| 273 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 274 | } |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 275 | addr_t sp_offset = addr_byte_size * BitCount (registers); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 276 | addr_t addr = sp - sp_offset; |
| 277 | uint32_t i; |
| 278 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 279 | EmulateInstruction::Context context; |
| 280 | context.type = EmulateInstruction::eContextPushRegisterOnStack; |
| 281 | Register dwarf_reg; |
| 282 | dwarf_reg.SetRegister (eRegisterKindDWARF, 0); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 283 | for (i=0; i<15; ++i) |
| 284 | { |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 285 | if (BitIsSet (registers, i)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 286 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 287 | dwarf_reg.num = dwarf_r0 + i; |
| 288 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 289 | uint32_t reg_value = ReadCoreReg(i, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 290 | if (!success) |
| 291 | return false; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 292 | if (!MemAWrite (context, addr, reg_value, addr_byte_size)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 293 | return false; |
| 294 | addr += addr_byte_size; |
| 295 | } |
| 296 | } |
| 297 | |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 298 | if (BitIsSet (registers, 15)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 299 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 300 | dwarf_reg.num = dwarf_pc; |
| 301 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 302 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 303 | if (!success) |
| 304 | return false; |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 305 | if (!MemAWrite (context, addr, pc, addr_byte_size)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 306 | return false; |
| 307 | } |
| 308 | |
| 309 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 310 | context.SetImmediateSigned (-sp_offset); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 311 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 312 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 313 | return false; |
| 314 | } |
| 315 | return true; |
| 316 | } |
| 317 | |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 318 | // Pop Multiple Registers loads multiple registers from the stack, loading from |
| 319 | // consecutive memory locations staring at the address in SP, and updates |
| 320 | // SP to point just above the loaded data. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 321 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 322 | EmulateInstructionARM::EmulatePOP (ARMEncoding encoding) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 323 | { |
| 324 | #if 0 |
| 325 | // ARM pseudo code... |
| 326 | if (ConditionPassed()) |
| 327 | { |
| 328 | EncodingSpecificOperations(); NullCheckIfThumbEE(13); |
| 329 | address = SP; |
| 330 | for i = 0 to 14 |
| 331 | if registers<i> == ‘1’ then |
| 332 | R[i} = if UnalignedAllowed then MemU[address,4] else MemA[address,4]; address = address + 4; |
| 333 | if registers<15> == ‘1’ then |
| 334 | if UnalignedAllowed then |
| 335 | LoadWritePC(MemU[address,4]); |
| 336 | else |
| 337 | LoadWritePC(MemA[address,4]); |
| 338 | if registers<13> == ‘0’ then SP = SP + 4*BitCount(registers); |
| 339 | if registers<13> == ‘1’ then SP = bits(32) UNKNOWN; |
| 340 | } |
| 341 | #endif |
| 342 | |
| 343 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 344 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 345 | if (!success) |
| 346 | return false; |
| 347 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 348 | if (ConditionPassed()) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 349 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 350 | const uint32_t addr_byte_size = GetAddressByteSize(); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 351 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 352 | if (!success) |
| 353 | return false; |
| 354 | uint32_t registers = 0; |
| 355 | uint32_t Rt; // the destination register |
| 356 | switch (encoding) { |
| 357 | case eEncodingT1: |
| 358 | registers = Bits32(opcode, 7, 0); |
| 359 | // The P bit represents PC. |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 360 | if (Bit32(opcode, 8)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 361 | registers |= (1u << 15); |
| 362 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 363 | if (BitCount(registers) < 1) |
| 364 | return false; |
| 365 | break; |
| 366 | case eEncodingT2: |
| 367 | // Ignore bit 13. |
| 368 | registers = Bits32(opcode, 15, 0) & ~0x2000; |
| 369 | // if BitCount(registers) < 2 || (P == '1' && M == '1') then UNPREDICTABLE; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 370 | if (BitCount(registers) < 2 || (Bit32(opcode, 15) && Bit32(opcode, 14))) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 371 | return false; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 372 | // if registers<15> == '1' && InITBlock() && !LastInITBlock() then UNPREDICTABLE; |
| 373 | if (BitIsSet(registers, 15) && InITBlock() && !LastInITBlock()) |
| 374 | return false; |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 375 | break; |
| 376 | case eEncodingT3: |
| 377 | Rt = Bits32(opcode, 15, 12); |
| 378 | // if t == 13 || (t == 15 && InITBlock() && !LastInITBlock()) then UNPREDICTABLE; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 379 | if (Rt == 13) |
| 380 | return false; |
| 381 | if (Rt == 15 && InITBlock() && !LastInITBlock()) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 382 | return false; |
| 383 | registers = (1u << Rt); |
| 384 | break; |
| 385 | case eEncodingA1: |
| 386 | registers = Bits32(opcode, 15, 0); |
| 387 | // Instead of return false, let's handle the following case as well, |
| 388 | // which amounts to popping one reg from the full descending stacks. |
| 389 | // if BitCount(register_list) < 2 then SEE LDM / LDMIA / LDMFD; |
| 390 | |
| 391 | // if registers<13> == ‘1’ && ArchVersion() >= 7 then UNPREDICTABLE; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 392 | if (BitIsSet(opcode, 13) && ArchVersion() >= ARMv7) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 393 | return false; |
| 394 | break; |
| 395 | case eEncodingA2: |
| 396 | Rt = Bits32(opcode, 15, 12); |
| 397 | // if t == 13 then UNPREDICTABLE; |
| 398 | if (Rt == dwarf_sp) |
| 399 | return false; |
| 400 | registers = (1u << Rt); |
| 401 | break; |
| 402 | default: |
| 403 | return false; |
| 404 | } |
| 405 | addr_t sp_offset = addr_byte_size * BitCount (registers); |
| 406 | addr_t addr = sp; |
| 407 | uint32_t i, data; |
| 408 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 409 | EmulateInstruction::Context context; |
| 410 | context.type = EmulateInstruction::eContextPopRegisterOffStack; |
| 411 | Register dwarf_reg; |
| 412 | dwarf_reg.SetRegister (eRegisterKindDWARF, 0); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 413 | for (i=0; i<15; ++i) |
| 414 | { |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 415 | if (BitIsSet (registers, i)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 416 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 417 | dwarf_reg.num = dwarf_r0 + i; |
| 418 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 419 | data = MemARead(context, addr, 4, 0, &success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 420 | if (!success) |
| 421 | return false; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 422 | if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_reg.num, data)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 423 | return false; |
| 424 | addr += addr_byte_size; |
| 425 | } |
| 426 | } |
| 427 | |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 428 | if (BitIsSet (registers, 15)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 429 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 430 | dwarf_reg.num = dwarf_pc; |
| 431 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 432 | data = MemARead(context, addr, 4, 0, &success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 433 | if (!success) |
| 434 | return false; |
Johnny Chen | f3eaacf | 2011-02-09 19:30:49 +0000 | [diff] [blame] | 435 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 436 | if (!LoadWritePC(context, data)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 437 | return false; |
| 438 | addr += addr_byte_size; |
| 439 | } |
| 440 | |
| 441 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 442 | context.SetImmediateSigned (sp_offset); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 443 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 444 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 445 | return false; |
| 446 | } |
| 447 | return true; |
| 448 | } |
| 449 | |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 450 | // 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] | 451 | // ADD (SP plus immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 452 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 453 | EmulateInstructionARM::EmulateADDRdSPImm (ARMEncoding encoding) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 454 | { |
| 455 | #if 0 |
| 456 | // ARM pseudo code... |
| 457 | if (ConditionPassed()) |
| 458 | { |
| 459 | EncodingSpecificOperations(); |
| 460 | (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’); |
| 461 | if d == 15 then |
| 462 | ALUWritePC(result); // setflags is always FALSE here |
| 463 | else |
| 464 | R[d] = result; |
| 465 | if setflags then |
| 466 | APSR.N = result<31>; |
| 467 | APSR.Z = IsZeroBit(result); |
| 468 | APSR.C = carry; |
| 469 | APSR.V = overflow; |
| 470 | } |
| 471 | #endif |
| 472 | |
| 473 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 474 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 475 | if (!success) |
| 476 | return false; |
| 477 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 478 | if (ConditionPassed()) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 479 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 480 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 481 | if (!success) |
| 482 | return false; |
| 483 | uint32_t Rd; // the destination register |
| 484 | uint32_t imm32; |
| 485 | switch (encoding) { |
| 486 | case eEncodingT1: |
| 487 | Rd = 7; |
| 488 | imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32) |
| 489 | break; |
| 490 | case eEncodingA1: |
| 491 | Rd = Bits32(opcode, 15, 12); |
| 492 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 493 | break; |
| 494 | default: |
| 495 | return false; |
| 496 | } |
| 497 | addr_t sp_offset = imm32; |
| 498 | addr_t addr = sp + sp_offset; // a pointer to the stack area |
| 499 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 500 | EmulateInstruction::Context context; |
| 501 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 502 | Register sp_reg; |
| 503 | sp_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP); |
| 504 | context.SetRegisterPlusOffset (sp_reg, sp_offset); |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 505 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 506 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, addr)) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 507 | return false; |
| 508 | } |
| 509 | return true; |
| 510 | } |
| 511 | |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 512 | // Set r7 or ip to the current stack pointer. |
| 513 | // MOV (register) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 514 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 515 | EmulateInstructionARM::EmulateMOVRdSP (ARMEncoding encoding) |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 516 | { |
| 517 | #if 0 |
| 518 | // ARM pseudo code... |
| 519 | if (ConditionPassed()) |
| 520 | { |
| 521 | EncodingSpecificOperations(); |
| 522 | result = R[m]; |
| 523 | if d == 15 then |
| 524 | ALUWritePC(result); // setflags is always FALSE here |
| 525 | else |
| 526 | R[d] = result; |
| 527 | if setflags then |
| 528 | APSR.N = result<31>; |
| 529 | APSR.Z = IsZeroBit(result); |
| 530 | // APSR.C unchanged |
| 531 | // APSR.V unchanged |
| 532 | } |
| 533 | #endif |
| 534 | |
| 535 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 536 | //const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 537 | //if (!success) |
| 538 | // return false; |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 539 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 540 | if (ConditionPassed()) |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 541 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 542 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 543 | if (!success) |
| 544 | return false; |
| 545 | uint32_t Rd; // the destination register |
| 546 | switch (encoding) { |
| 547 | case eEncodingT1: |
| 548 | Rd = 7; |
| 549 | break; |
| 550 | case eEncodingA1: |
| 551 | Rd = 12; |
| 552 | break; |
| 553 | default: |
| 554 | return false; |
| 555 | } |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 556 | |
| 557 | EmulateInstruction::Context context; |
| 558 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 559 | Register sp_reg; |
| 560 | sp_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP); |
| 561 | context.SetRegisterPlusOffset (sp_reg, 0); |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 562 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 563 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, sp)) |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 564 | return false; |
| 565 | } |
| 566 | return true; |
| 567 | } |
| 568 | |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 569 | // Move from high register (r8-r15) to low register (r0-r7). |
| 570 | // MOV (register) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 571 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 572 | EmulateInstructionARM::EmulateMOVLowHigh (ARMEncoding encoding) |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 573 | { |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 574 | return EmulateMOVRdRm (encoding); |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | // Move from register to register. |
| 578 | // MOV (register) |
| 579 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 580 | EmulateInstructionARM::EmulateMOVRdRm (ARMEncoding encoding) |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 581 | { |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 582 | #if 0 |
| 583 | // ARM pseudo code... |
| 584 | if (ConditionPassed()) |
| 585 | { |
| 586 | EncodingSpecificOperations(); |
| 587 | result = R[m]; |
| 588 | if d == 15 then |
| 589 | ALUWritePC(result); // setflags is always FALSE here |
| 590 | else |
| 591 | R[d] = result; |
| 592 | if setflags then |
| 593 | APSR.N = result<31>; |
| 594 | APSR.Z = IsZeroBit(result); |
| 595 | // APSR.C unchanged |
| 596 | // APSR.V unchanged |
| 597 | } |
| 598 | #endif |
| 599 | |
| 600 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 601 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 602 | if (!success) |
| 603 | return false; |
| 604 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 605 | if (ConditionPassed()) |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 606 | { |
| 607 | uint32_t Rm; // the source register |
| 608 | uint32_t Rd; // the destination register |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 609 | bool setflags; |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 610 | switch (encoding) { |
| 611 | case eEncodingT1: |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 612 | Rd = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 0); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 613 | Rm = Bits32(opcode, 6, 3); |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 614 | setflags = false; |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 615 | if (Rd == 15 && InITBlock() && !LastInITBlock()) |
| 616 | return false; |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 617 | break; |
| 618 | case eEncodingT2: |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 619 | Rd = Bits32(opcode, 2, 0); |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 620 | Rm = Bits32(opcode, 5, 3); |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 621 | setflags = true; |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 622 | if (InITBlock()) |
| 623 | return false; |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 624 | break; |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 625 | case eEncodingT3: |
| 626 | Rd = Bits32(opcode, 11, 8); |
| 627 | Rm = Bits32(opcode, 3, 0); |
| 628 | setflags = BitIsSet(opcode, 20); |
| 629 | // if setflags && (BadReg(d) || BadReg(m)) then UNPREDICTABLE; |
| 630 | if (setflags && (BadReg(Rd) || BadReg(Rm))) |
| 631 | return false; |
| 632 | // if !setflags && (d == 15 || m == 15 || (d == 13 && m == 13)) then UNPREDICTABLE; |
| 633 | if (!setflags && (Rd == 15 || Rm == 15 || (Rd == 13 && Rm == 13))) |
| 634 | return false; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 635 | break; |
Johnny Chen | 01d6157 | 2011-02-25 00:23:25 +0000 | [diff] [blame] | 636 | case eEncodingA1: |
| 637 | Rd = Bits32(opcode, 15, 12); |
| 638 | Rm = Bits32(opcode, 3, 0); |
| 639 | setflags = BitIsSet(opcode, 20); |
| 640 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 641 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 642 | if (Rd == 15 && setflags) |
| 643 | return false; |
| 644 | break; |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 645 | default: |
| 646 | return false; |
| 647 | } |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 648 | uint32_t result = ReadCoreReg(Rm, &success); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 649 | if (!success) |
| 650 | return false; |
| 651 | |
| 652 | // The context specifies that Rm is to be moved into Rd. |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 653 | EmulateInstruction::Context context; |
| 654 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 655 | Register dwarf_reg; |
| 656 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm); |
| 657 | context.SetRegisterPlusOffset (dwarf_reg, 0); |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 658 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 659 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 660 | return false; |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 661 | } |
| 662 | return true; |
| 663 | } |
| 664 | |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 665 | // Move (immediate) writes an immediate value to the destination register. It |
| 666 | // can optionally update the condition flags based on the value. |
| 667 | // MOV (immediate) |
| 668 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 669 | EmulateInstructionARM::EmulateMOVRdImm (ARMEncoding encoding) |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 670 | { |
| 671 | #if 0 |
| 672 | // ARM pseudo code... |
| 673 | if (ConditionPassed()) |
| 674 | { |
| 675 | EncodingSpecificOperations(); |
| 676 | result = imm32; |
| 677 | if d == 15 then // Can only occur for ARM encoding |
| 678 | ALUWritePC(result); // setflags is always FALSE here |
| 679 | else |
| 680 | R[d] = result; |
| 681 | if setflags then |
| 682 | APSR.N = result<31>; |
| 683 | APSR.Z = IsZeroBit(result); |
| 684 | APSR.C = carry; |
| 685 | // APSR.V unchanged |
| 686 | } |
| 687 | #endif |
| 688 | bool success = false; |
| 689 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 690 | if (!success) |
| 691 | return false; |
| 692 | |
| 693 | if (ConditionPassed()) |
| 694 | { |
| 695 | uint32_t Rd; // the destination register |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 696 | uint32_t imm32; // the immediate value to be written to Rd |
| 697 | uint32_t carry; // the carry bit after ThumbExpandImm_C or ARMExpandImm_C. |
| 698 | bool setflags; |
| 699 | switch (encoding) { |
| 700 | case eEncodingT1: |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 701 | Rd = Bits32(opcode, 10, 8); |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 702 | setflags = !InITBlock(); |
| 703 | imm32 = Bits32(opcode, 7, 0); // imm32 = ZeroExtend(imm8, 32) |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 704 | carry = APSR_C; |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 705 | break; |
| 706 | case eEncodingT2: |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 707 | Rd = Bits32(opcode, 11, 8); |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 708 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 709 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); |
Johnny Chen | 9798cfc | 2011-02-14 23:33:58 +0000 | [diff] [blame] | 710 | if (BadReg(Rd)) |
| 711 | return false; |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 712 | break; |
| 713 | default: |
| 714 | return false; |
| 715 | } |
| 716 | uint32_t result = imm32; |
| 717 | |
| 718 | // The context specifies that an immediate is to be moved into Rd. |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 719 | EmulateInstruction::Context context; |
| 720 | context.type = EmulateInstruction::eContextImmediate; |
| 721 | context.SetNoArgs (); |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 722 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 723 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 724 | return false; |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 725 | } |
| 726 | return true; |
| 727 | } |
| 728 | |
Caroline Tice | 5c1e2ed | 2011-03-02 22:43:54 +0000 | [diff] [blame] | 729 | // MUL multiplies two register values. The least significant 32 bits of the result are written to the destination |
| 730 | // register. These 32 bits do not depend on whether the source register values are considered to be signed values or |
| 731 | // unsigned values. |
| 732 | // |
| 733 | // Optionally, it can update the condition flags based on the result. In the Thumb instruction set, this option is |
| 734 | // limited to only a few forms of the instruction. |
| 735 | bool |
| 736 | EmulateInstructionARM::EmulateMUL (ARMEncoding encoding) |
| 737 | { |
| 738 | #if 0 |
| 739 | if ConditionPassed() then |
| 740 | EncodingSpecificOperations(); |
| 741 | operand1 = SInt(R[n]); // operand1 = UInt(R[n]) produces the same final results |
| 742 | operand2 = SInt(R[m]); // operand2 = UInt(R[m]) produces the same final results |
| 743 | result = operand1 * operand2; |
| 744 | R[d] = result<31:0>; |
| 745 | if setflags then |
| 746 | APSR.N = result<31>; |
| 747 | APSR.Z = IsZeroBit(result); |
| 748 | if ArchVersion() == 4 then |
| 749 | APSR.C = bit UNKNOWN; |
| 750 | // else APSR.C unchanged |
| 751 | // APSR.V always unchanged |
| 752 | #endif |
| 753 | |
| 754 | bool success = false; |
| 755 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 756 | if (!success) |
| 757 | return false; |
| 758 | |
| 759 | if (ConditionPassed()) |
| 760 | { |
| 761 | uint32_t d; |
| 762 | uint32_t n; |
| 763 | uint32_t m; |
| 764 | bool setflags; |
| 765 | |
| 766 | // EncodingSpecificOperations(); |
| 767 | switch (encoding) |
| 768 | { |
| 769 | case eEncodingT1: |
| 770 | // d = UInt(Rdm); n = UInt(Rn); m = UInt(Rdm); setflags = !InITBlock(); |
| 771 | d = Bits32 (opcode, 2, 0); |
| 772 | n = Bits32 (opcode, 5, 3); |
| 773 | m = Bits32 (opcode, 2, 0); |
| 774 | setflags = !InITBlock(); |
| 775 | |
| 776 | // if ArchVersion() < 6 && d == n then UNPREDICTABLE; |
| 777 | if ((ArchVersion() < ARMv6) && (d == n)) |
| 778 | return false; |
| 779 | |
| 780 | break; |
| 781 | |
| 782 | case eEncodingT2: |
| 783 | // d = UInt(Rd); n = UInt(Rn); m = UInt(Rm); setflags = FALSE; |
| 784 | d = Bits32 (opcode, 11, 8); |
| 785 | n = Bits32 (opcode, 19, 16); |
| 786 | m = Bits32 (opcode, 3, 0); |
| 787 | setflags = false; |
| 788 | |
| 789 | // if BadReg(d) || BadReg(n) || BadReg(m) then UNPREDICTABLE; |
| 790 | if (BadReg (d) || BadReg (n) || BadReg (m)) |
| 791 | return false; |
| 792 | |
| 793 | break; |
| 794 | |
| 795 | case eEncodingA1: |
| 796 | // d = UInt(Rd); n = UInt(Rn); m = UInt(Rm); setflags = (S == ’1’); |
| 797 | d = Bits32 (opcode, 19, 16); |
| 798 | n = Bits32 (opcode, 3, 0); |
| 799 | m = Bits32 (opcode, 11, 8); |
| 800 | setflags = BitIsSet (opcode, 20); |
| 801 | |
| 802 | // if d == 15 || n == 15 || m == 15 then UNPREDICTABLE; |
| 803 | if ((d == 15) || (n == 15) || (m == 15)) |
| 804 | return false; |
| 805 | |
| 806 | // if ArchVersion() < 6 && d == n then UNPREDICTABLE; |
| 807 | if ((ArchVersion() < ARMv6) && (d == n)) |
| 808 | return false; |
| 809 | |
| 810 | break; |
| 811 | |
| 812 | default: |
| 813 | return false; |
| 814 | } |
| 815 | |
| 816 | // operand1 = SInt(R[n]); // operand1 = UInt(R[n]) produces the same final results |
| 817 | uint64_t operand1 = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 818 | if (!success) |
| 819 | return false; |
| 820 | |
| 821 | // operand2 = SInt(R[m]); // operand2 = UInt(R[m]) produces the same final results |
| 822 | uint64_t operand2 = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 823 | if (!success) |
| 824 | return false; |
| 825 | |
| 826 | // result = operand1 * operand2; |
| 827 | uint64_t result = operand1 * operand2; |
| 828 | |
| 829 | // R[d] = result<31:0>; |
| 830 | Register op1_reg; |
| 831 | Register op2_reg; |
| 832 | op1_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 833 | op2_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + m); |
| 834 | |
| 835 | EmulateInstruction::Context context; |
| 836 | context.type = eContextMultiplication; |
| 837 | context.SetRegisterRegisterOperands (op1_reg, op2_reg); |
| 838 | |
| 839 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + d, (0x0000ffff & result))) |
| 840 | return false; |
| 841 | |
| 842 | // if setflags then |
| 843 | if (setflags) |
| 844 | { |
| 845 | // APSR.N = result<31>; |
| 846 | // APSR.Z = IsZeroBit(result); |
| 847 | m_new_inst_cpsr = m_inst_cpsr; |
| 848 | SetBit32 (m_new_inst_cpsr, CPSR_N_POS, Bit32 (result, 31)); |
| 849 | SetBit32 (m_new_inst_cpsr, CPSR_Z_POS, result == 0 ? 1 : 0); |
| 850 | if (m_new_inst_cpsr != m_inst_cpsr) |
| 851 | { |
| 852 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr)) |
| 853 | return false; |
| 854 | } |
| 855 | |
| 856 | // if ArchVersion() == 4 then |
| 857 | // APSR.C = bit UNKNOWN; |
| 858 | } |
| 859 | } |
| 860 | return true; |
| 861 | } |
| 862 | |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 863 | // Bitwise NOT (immediate) writes the bitwise inverse of an immediate value to the destination register. |
| 864 | // It can optionally update the condition flags based on the value. |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 865 | bool |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 866 | EmulateInstructionARM::EmulateMVNImm (ARMEncoding encoding) |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 867 | { |
| 868 | #if 0 |
| 869 | // ARM pseudo code... |
| 870 | if (ConditionPassed()) |
| 871 | { |
| 872 | EncodingSpecificOperations(); |
| 873 | result = NOT(imm32); |
| 874 | if d == 15 then // Can only occur for ARM encoding |
| 875 | ALUWritePC(result); // setflags is always FALSE here |
| 876 | else |
| 877 | R[d] = result; |
| 878 | if setflags then |
| 879 | APSR.N = result<31>; |
| 880 | APSR.Z = IsZeroBit(result); |
| 881 | APSR.C = carry; |
| 882 | // APSR.V unchanged |
| 883 | } |
| 884 | #endif |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 885 | bool success = false; |
| 886 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 887 | if (!success) |
| 888 | return false; |
| 889 | |
| 890 | if (ConditionPassed()) |
| 891 | { |
| 892 | uint32_t Rd; // the destination register |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 893 | uint32_t imm32; // the output after ThumbExpandImm_C or ARMExpandImm_C |
| 894 | uint32_t carry; // the carry bit after ThumbExpandImm_C or ARMExpandImm_C |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 895 | bool setflags; |
| 896 | switch (encoding) { |
| 897 | case eEncodingT1: |
| 898 | Rd = Bits32(opcode, 11, 8); |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 899 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 900 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 901 | break; |
| 902 | case eEncodingA1: |
| 903 | Rd = Bits32(opcode, 15, 12); |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 904 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 905 | imm32 = ARMExpandImm_C(opcode, APSR_C, carry); |
| 906 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 907 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 908 | if (Rd == 15 && setflags) |
| 909 | return false; |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 910 | break; |
| 911 | default: |
| 912 | return false; |
| 913 | } |
| 914 | uint32_t result = ~imm32; |
| 915 | |
| 916 | // The context specifies that an immediate is to be moved into Rd. |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 917 | EmulateInstruction::Context context; |
| 918 | context.type = EmulateInstruction::eContextImmediate; |
| 919 | context.SetNoArgs (); |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 920 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 921 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 922 | return false; |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 923 | } |
| 924 | return true; |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 927 | // Bitwise NOT (register) writes the bitwise inverse of a register value to the destination register. |
| 928 | // It can optionally update the condition flags based on the result. |
| 929 | bool |
| 930 | EmulateInstructionARM::EmulateMVNReg (ARMEncoding encoding) |
| 931 | { |
| 932 | #if 0 |
| 933 | // ARM pseudo code... |
| 934 | if (ConditionPassed()) |
| 935 | { |
| 936 | EncodingSpecificOperations(); |
| 937 | (shifted, carry) = Shift_C(R[m], shift_t, shift_n, APSR.C); |
| 938 | result = NOT(shifted); |
| 939 | if d == 15 then // Can only occur for ARM encoding |
| 940 | ALUWritePC(result); // setflags is always FALSE here |
| 941 | else |
| 942 | R[d] = result; |
| 943 | if setflags then |
| 944 | APSR.N = result<31>; |
| 945 | APSR.Z = IsZeroBit(result); |
| 946 | APSR.C = carry; |
| 947 | // APSR.V unchanged |
| 948 | } |
| 949 | #endif |
| 950 | |
| 951 | bool success = false; |
| 952 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 953 | if (!success) |
| 954 | return false; |
| 955 | |
| 956 | if (ConditionPassed()) |
| 957 | { |
| 958 | uint32_t Rm; // the source register |
| 959 | uint32_t Rd; // the destination register |
| 960 | ARM_ShifterType shift_t; |
| 961 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 962 | bool setflags; |
| 963 | uint32_t carry; // the carry bit after the shift operation |
| 964 | switch (encoding) { |
| 965 | case eEncodingT1: |
| 966 | Rd = Bits32(opcode, 2, 0); |
| 967 | Rm = Bits32(opcode, 5, 3); |
| 968 | setflags = !InITBlock(); |
| 969 | shift_t = SRType_LSL; |
| 970 | shift_n = 0; |
| 971 | if (InITBlock()) |
| 972 | return false; |
| 973 | break; |
| 974 | case eEncodingT2: |
| 975 | Rd = Bits32(opcode, 11, 8); |
| 976 | Rm = Bits32(opcode, 3, 0); |
| 977 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 978 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 979 | // if (BadReg(d) || BadReg(m)) then UNPREDICTABLE; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 980 | if (BadReg(Rd) || BadReg(Rm)) |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 981 | return false; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 982 | break; |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 983 | case eEncodingA1: |
| 984 | Rd = Bits32(opcode, 15, 12); |
| 985 | Rm = Bits32(opcode, 3, 0); |
| 986 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 987 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 988 | break; |
| 989 | default: |
| 990 | return false; |
| 991 | } |
| 992 | uint32_t value = ReadCoreReg(Rm, &success); |
| 993 | if (!success) |
| 994 | return false; |
| 995 | |
| 996 | uint32_t shifted = Shift_C(value, shift_t, shift_n, APSR_C, carry); |
| 997 | uint32_t result = ~shifted; |
| 998 | |
| 999 | // The context specifies that an immediate is to be moved into Rd. |
| 1000 | EmulateInstruction::Context context; |
| 1001 | context.type = EmulateInstruction::eContextImmediate; |
| 1002 | context.SetNoArgs (); |
| 1003 | |
| 1004 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 1005 | return false; |
| 1006 | } |
| 1007 | return true; |
| 1008 | } |
| 1009 | |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 1010 | // PC relative immediate load into register, possibly followed by ADD (SP plus register). |
| 1011 | // LDR (literal) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1012 | bool |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 1013 | EmulateInstructionARM::EmulateLDRRtPCRelative (ARMEncoding encoding) |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 1014 | { |
| 1015 | #if 0 |
| 1016 | // ARM pseudo code... |
| 1017 | if (ConditionPassed()) |
| 1018 | { |
| 1019 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 1020 | base = Align(PC,4); |
| 1021 | address = if add then (base + imm32) else (base - imm32); |
| 1022 | data = MemU[address,4]; |
| 1023 | if t == 15 then |
| 1024 | if address<1:0> == ‘00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 1025 | elsif UnalignedSupport() || address<1:0> = ‘00’ then |
| 1026 | R[t] = data; |
| 1027 | else // Can only apply before ARMv7 |
| 1028 | if CurrentInstrSet() == InstrSet_ARM then |
| 1029 | R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 1030 | else |
| 1031 | R[t] = bits(32) UNKNOWN; |
| 1032 | } |
| 1033 | #endif |
| 1034 | |
| 1035 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1036 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 1037 | if (!success) |
| 1038 | return false; |
| 1039 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1040 | if (ConditionPassed()) |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 1041 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1042 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 1043 | if (!success) |
| 1044 | return false; |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 1045 | |
| 1046 | // PC relative immediate load context |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1047 | EmulateInstruction::Context context; |
| 1048 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 1049 | Register pc_reg; |
| 1050 | pc_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); |
| 1051 | context.SetRegisterPlusOffset (pc_reg, 0); |
| 1052 | |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 1053 | uint32_t Rt; // the destination register |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 1054 | uint32_t imm32; // immediate offset from the PC |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 1055 | bool add; // +imm32 or -imm32? |
| 1056 | addr_t base; // the base address |
| 1057 | addr_t address; // the PC relative address |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 1058 | uint32_t data; // the literal data value from the PC relative load |
| 1059 | switch (encoding) { |
| 1060 | case eEncodingT1: |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 1061 | Rt = Bits32(opcode, 10, 8); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 1062 | imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32); |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 1063 | add = true; |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 1064 | break; |
| 1065 | case eEncodingT2: |
| 1066 | Rt = Bits32(opcode, 15, 12); |
| 1067 | imm32 = Bits32(opcode, 11, 0) << 2; // imm32 = ZeroExtend(imm12, 32); |
| 1068 | add = BitIsSet(opcode, 23); |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 1069 | if (Rt == 15 && InITBlock() && !LastInITBlock()) |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 1070 | return false; |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 1071 | break; |
| 1072 | default: |
| 1073 | return false; |
| 1074 | } |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 1075 | |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1076 | base = Align(pc, 4); |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 1077 | if (add) |
| 1078 | address = base + imm32; |
| 1079 | else |
| 1080 | address = base - imm32; |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1081 | |
| 1082 | context.SetRegisterPlusOffset(pc_reg, address - base); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 1083 | data = MemURead(context, address, 4, 0, &success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 1084 | if (!success) |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 1085 | return false; |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 1086 | |
| 1087 | if (Rt == 15) |
| 1088 | { |
| 1089 | if (Bits32(address, 1, 0) == 0) |
| 1090 | { |
| 1091 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 1092 | if (!LoadWritePC(context, data)) |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 1093 | return false; |
| 1094 | } |
| 1095 | else |
| 1096 | return false; |
| 1097 | } |
| 1098 | else if (UnalignedSupport() || Bits32(address, 1, 0) == 0) |
| 1099 | { |
| 1100 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data)) |
| 1101 | return false; |
| 1102 | } |
| 1103 | else // We don't handle ARM for now. |
| 1104 | return false; |
| 1105 | |
| 1106 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data)) |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 1107 | return false; |
| 1108 | } |
| 1109 | return true; |
| 1110 | } |
| 1111 | |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1112 | // An add operation to adjust the SP. |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 1113 | // ADD (SP plus immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1114 | bool |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 1115 | EmulateInstructionARM::EmulateADDSPImm (ARMEncoding encoding) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 1116 | { |
| 1117 | #if 0 |
| 1118 | // ARM pseudo code... |
| 1119 | if (ConditionPassed()) |
| 1120 | { |
| 1121 | EncodingSpecificOperations(); |
| 1122 | (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’); |
| 1123 | if d == 15 then // Can only occur for ARM encoding |
| 1124 | ALUWritePC(result); // setflags is always FALSE here |
| 1125 | else |
| 1126 | R[d] = result; |
| 1127 | if setflags then |
| 1128 | APSR.N = result<31>; |
| 1129 | APSR.Z = IsZeroBit(result); |
| 1130 | APSR.C = carry; |
| 1131 | APSR.V = overflow; |
| 1132 | } |
| 1133 | #endif |
| 1134 | |
| 1135 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1136 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 1137 | if (!success) |
| 1138 | return false; |
| 1139 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1140 | if (ConditionPassed()) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 1141 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1142 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 1143 | if (!success) |
| 1144 | return false; |
| 1145 | uint32_t imm32; // the immediate operand |
| 1146 | switch (encoding) { |
| 1147 | case eEncodingT2: |
Johnny Chen | a695f95 | 2011-02-23 21:24:25 +0000 | [diff] [blame] | 1148 | imm32 = ThumbImm7Scaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 1149 | break; |
| 1150 | default: |
| 1151 | return false; |
| 1152 | } |
| 1153 | addr_t sp_offset = imm32; |
| 1154 | addr_t addr = sp + sp_offset; // the adjusted stack pointer value |
| 1155 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1156 | EmulateInstruction::Context context; |
| 1157 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 1158 | context.SetImmediateSigned (sp_offset); |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 1159 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1160 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 1161 | return false; |
| 1162 | } |
| 1163 | return true; |
| 1164 | } |
| 1165 | |
| 1166 | // An add operation to adjust the SP. |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1167 | // ADD (SP plus register) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1168 | bool |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 1169 | EmulateInstructionARM::EmulateADDSPRm (ARMEncoding encoding) |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1170 | { |
| 1171 | #if 0 |
| 1172 | // ARM pseudo code... |
| 1173 | if (ConditionPassed()) |
| 1174 | { |
| 1175 | EncodingSpecificOperations(); |
| 1176 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 1177 | (result, carry, overflow) = AddWithCarry(SP, shifted, ‘0’); |
| 1178 | if d == 15 then |
| 1179 | ALUWritePC(result); // setflags is always FALSE here |
| 1180 | else |
| 1181 | R[d] = result; |
| 1182 | if setflags then |
| 1183 | APSR.N = result<31>; |
| 1184 | APSR.Z = IsZeroBit(result); |
| 1185 | APSR.C = carry; |
| 1186 | APSR.V = overflow; |
| 1187 | } |
| 1188 | #endif |
| 1189 | |
| 1190 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1191 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1192 | if (!success) |
| 1193 | return false; |
| 1194 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1195 | if (ConditionPassed()) |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1196 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1197 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1198 | if (!success) |
| 1199 | return false; |
| 1200 | uint32_t Rm; // the second operand |
| 1201 | switch (encoding) { |
| 1202 | case eEncodingT2: |
| 1203 | Rm = Bits32(opcode, 6, 3); |
| 1204 | break; |
| 1205 | default: |
| 1206 | return false; |
| 1207 | } |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1208 | int32_t reg_value = ReadCoreReg(Rm, &success); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1209 | if (!success) |
| 1210 | return false; |
| 1211 | |
| 1212 | addr_t addr = (int32_t)sp + reg_value; // the adjusted stack pointer value |
| 1213 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1214 | EmulateInstruction::Context context; |
| 1215 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 1216 | context.SetImmediateSigned (reg_value); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1217 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1218 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1219 | return false; |
| 1220 | } |
| 1221 | return true; |
| 1222 | } |
| 1223 | |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1224 | // Branch with Link and Exchange Instruction Sets (immediate) calls a subroutine |
| 1225 | // at a PC-relative address, and changes instruction set from ARM to Thumb, or |
| 1226 | // from Thumb to ARM. |
| 1227 | // BLX (immediate) |
| 1228 | bool |
| 1229 | EmulateInstructionARM::EmulateBLXImmediate (ARMEncoding encoding) |
| 1230 | { |
| 1231 | #if 0 |
| 1232 | // ARM pseudo code... |
| 1233 | if (ConditionPassed()) |
| 1234 | { |
| 1235 | EncodingSpecificOperations(); |
| 1236 | if CurrentInstrSet() == InstrSet_ARM then |
| 1237 | LR = PC - 4; |
| 1238 | else |
| 1239 | LR = PC<31:1> : '1'; |
| 1240 | if targetInstrSet == InstrSet_ARM then |
| 1241 | targetAddress = Align(PC,4) + imm32; |
| 1242 | else |
| 1243 | targetAddress = PC + imm32; |
| 1244 | SelectInstrSet(targetInstrSet); |
| 1245 | BranchWritePC(targetAddress); |
| 1246 | } |
| 1247 | #endif |
| 1248 | |
| 1249 | bool success = false; |
| 1250 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1251 | if (!success) |
| 1252 | return false; |
| 1253 | |
| 1254 | if (ConditionPassed()) |
| 1255 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1256 | EmulateInstruction::Context context; |
| 1257 | context.type = EmulateInstruction::eContextRelativeBranchImmediate; |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1258 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1259 | if (!success) |
| 1260 | return false; |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1261 | addr_t lr; // next instruction address |
| 1262 | addr_t target; // target address |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1263 | int32_t imm32; // PC-relative offset |
| 1264 | switch (encoding) { |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 1265 | case eEncodingT1: |
| 1266 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1267 | lr = pc | 1u; // return address |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1268 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 1269 | uint32_t imm10 = Bits32(opcode, 25, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1270 | uint32_t J1 = Bit32(opcode, 13); |
| 1271 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 1272 | uint32_t imm11 = Bits32(opcode, 10, 0); |
| 1273 | uint32_t I1 = !(J1 ^ S); |
| 1274 | uint32_t I2 = !(J2 ^ S); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1275 | 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] | 1276 | imm32 = llvm::SignExtend32<25>(imm25); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1277 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1278 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 1279 | if (InITBlock() && !LastInITBlock()) |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1280 | return false; |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 1281 | break; |
| 1282 | } |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1283 | case eEncodingT2: |
| 1284 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1285 | lr = pc | 1u; // return address |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1286 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1287 | uint32_t imm10H = Bits32(opcode, 25, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1288 | uint32_t J1 = Bit32(opcode, 13); |
| 1289 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1290 | uint32_t imm10L = Bits32(opcode, 10, 1); |
| 1291 | uint32_t I1 = !(J1 ^ S); |
| 1292 | uint32_t I2 = !(J2 ^ S); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1293 | 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] | 1294 | imm32 = llvm::SignExtend32<25>(imm25); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1295 | target = Align(pc, 4) + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1296 | context.SetModeAndImmediateSigned (eModeARM, 4 + imm32); |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 1297 | if (InITBlock() && !LastInITBlock()) |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1298 | return false; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1299 | break; |
| 1300 | } |
Johnny Chen | c47d0ca | 2011-02-08 18:58:31 +0000 | [diff] [blame] | 1301 | case eEncodingA1: |
| 1302 | lr = pc + 4; // return address |
| 1303 | imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1304 | target = Align(pc, 4) + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1305 | context.SetModeAndImmediateSigned (eModeARM, 8 + imm32); |
Johnny Chen | c47d0ca | 2011-02-08 18:58:31 +0000 | [diff] [blame] | 1306 | break; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1307 | case eEncodingA2: |
| 1308 | lr = pc + 4; // return address |
| 1309 | imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2 | Bits32(opcode, 24, 24) << 1); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1310 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1311 | context.SetModeAndImmediateSigned (eModeThumb, 8 + imm32); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1312 | break; |
| 1313 | default: |
| 1314 | return false; |
| 1315 | } |
| 1316 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr)) |
| 1317 | return false; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1318 | if (!BranchWritePC(context, target)) |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1319 | return false; |
| 1320 | } |
| 1321 | return true; |
| 1322 | } |
| 1323 | |
| 1324 | // Branch with Link and Exchange (register) calls a subroutine at an address and |
| 1325 | // instruction set specified by a register. |
| 1326 | // BLX (register) |
| 1327 | bool |
| 1328 | EmulateInstructionARM::EmulateBLXRm (ARMEncoding encoding) |
| 1329 | { |
| 1330 | #if 0 |
| 1331 | // ARM pseudo code... |
| 1332 | if (ConditionPassed()) |
| 1333 | { |
| 1334 | EncodingSpecificOperations(); |
| 1335 | target = R[m]; |
| 1336 | if CurrentInstrSet() == InstrSet_ARM then |
| 1337 | next_instr_addr = PC - 4; |
| 1338 | LR = next_instr_addr; |
| 1339 | else |
| 1340 | next_instr_addr = PC - 2; |
| 1341 | LR = next_instr_addr<31:1> : ‘1’; |
| 1342 | BXWritePC(target); |
| 1343 | } |
| 1344 | #endif |
| 1345 | |
| 1346 | bool success = false; |
| 1347 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1348 | if (!success) |
| 1349 | return false; |
| 1350 | |
| 1351 | if (ConditionPassed()) |
| 1352 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1353 | EmulateInstruction::Context context; |
| 1354 | context.type = EmulateInstruction::eContextAbsoluteBranchRegister; |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1355 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1356 | addr_t lr; // next instruction address |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1357 | if (!success) |
| 1358 | return false; |
| 1359 | uint32_t Rm; // the register with the target address |
| 1360 | switch (encoding) { |
| 1361 | case eEncodingT1: |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1362 | lr = (pc - 2) | 1u; // return address |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1363 | Rm = Bits32(opcode, 6, 3); |
| 1364 | // if m == 15 then UNPREDICTABLE; |
| 1365 | if (Rm == 15) |
| 1366 | return false; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 1367 | if (InITBlock() && !LastInITBlock()) |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1368 | return false; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1369 | break; |
| 1370 | case eEncodingA1: |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1371 | lr = pc - 4; // return address |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1372 | Rm = Bits32(opcode, 3, 0); |
| 1373 | // if m == 15 then UNPREDICTABLE; |
| 1374 | if (Rm == 15) |
| 1375 | return false; |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 1376 | break; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1377 | default: |
| 1378 | return false; |
| 1379 | } |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1380 | addr_t target = ReadCoreReg (Rm, &success); |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1381 | if (!success) |
| 1382 | return false; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1383 | Register dwarf_reg; |
| 1384 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm); |
| 1385 | context.SetRegister (dwarf_reg); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1386 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr)) |
| 1387 | return false; |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 1388 | if (!BXWritePC(context, target)) |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1389 | return false; |
| 1390 | } |
| 1391 | return true; |
| 1392 | } |
| 1393 | |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1394 | // Branch and Exchange causes a branch to an address and instruction set specified by a register. |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1395 | bool |
| 1396 | EmulateInstructionARM::EmulateBXRm (ARMEncoding encoding) |
| 1397 | { |
| 1398 | #if 0 |
| 1399 | // ARM pseudo code... |
| 1400 | if (ConditionPassed()) |
| 1401 | { |
| 1402 | EncodingSpecificOperations(); |
| 1403 | BXWritePC(R[m]); |
| 1404 | } |
| 1405 | #endif |
| 1406 | |
| 1407 | bool success = false; |
| 1408 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1409 | if (!success) |
| 1410 | return false; |
| 1411 | |
| 1412 | if (ConditionPassed()) |
| 1413 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1414 | EmulateInstruction::Context context; |
| 1415 | context.type = EmulateInstruction::eContextAbsoluteBranchRegister; |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1416 | uint32_t Rm; // the register with the target address |
| 1417 | switch (encoding) { |
| 1418 | case eEncodingT1: |
| 1419 | Rm = Bits32(opcode, 6, 3); |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 1420 | if (InITBlock() && !LastInITBlock()) |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1421 | return false; |
| 1422 | break; |
| 1423 | case eEncodingA1: |
| 1424 | Rm = Bits32(opcode, 3, 0); |
| 1425 | break; |
| 1426 | default: |
| 1427 | return false; |
| 1428 | } |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1429 | addr_t target = ReadCoreReg (Rm, &success); |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1430 | if (!success) |
| 1431 | return false; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1432 | |
| 1433 | Register dwarf_reg; |
| 1434 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm); |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 1435 | context.SetRegister (dwarf_reg); |
| 1436 | if (!BXWritePC(context, target)) |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1437 | return false; |
| 1438 | } |
| 1439 | return true; |
| 1440 | } |
| 1441 | |
Johnny Chen | 59e6ab7 | 2011-02-24 21:01:20 +0000 | [diff] [blame] | 1442 | // Branch and Exchange Jazelle attempts to change to Jazelle state. If the attempt fails, it branches to an |
| 1443 | // address and instruction set specified by a register as though it were a BX instruction. |
| 1444 | // |
| 1445 | // TODO: Emulate Jazelle architecture? |
| 1446 | // We currently assume that switching to Jazelle state fails, thus treating BXJ as a BX operation. |
| 1447 | bool |
| 1448 | EmulateInstructionARM::EmulateBXJRm (ARMEncoding encoding) |
| 1449 | { |
| 1450 | #if 0 |
| 1451 | // ARM pseudo code... |
| 1452 | if (ConditionPassed()) |
| 1453 | { |
| 1454 | EncodingSpecificOperations(); |
| 1455 | if JMCR.JE == ‘0’ || CurrentInstrSet() == InstrSet_ThumbEE then |
| 1456 | BXWritePC(R[m]); |
| 1457 | else |
| 1458 | if JazelleAcceptsExecution() then |
| 1459 | SwitchToJazelleExecution(); |
| 1460 | else |
| 1461 | SUBARCHITECTURE_DEFINED handler call; |
| 1462 | } |
| 1463 | #endif |
| 1464 | |
| 1465 | bool success = false; |
| 1466 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1467 | if (!success) |
| 1468 | return false; |
| 1469 | |
| 1470 | if (ConditionPassed()) |
| 1471 | { |
| 1472 | EmulateInstruction::Context context; |
| 1473 | context.type = EmulateInstruction::eContextAbsoluteBranchRegister; |
| 1474 | uint32_t Rm; // the register with the target address |
| 1475 | switch (encoding) { |
| 1476 | case eEncodingT1: |
| 1477 | Rm = Bits32(opcode, 19, 16); |
| 1478 | if (BadReg(Rm)) |
| 1479 | return false; |
| 1480 | if (InITBlock() && !LastInITBlock()) |
| 1481 | return false; |
| 1482 | break; |
| 1483 | case eEncodingA1: |
| 1484 | Rm = Bits32(opcode, 3, 0); |
| 1485 | if (Rm == 15) |
| 1486 | return false; |
| 1487 | break; |
| 1488 | default: |
| 1489 | return false; |
| 1490 | } |
| 1491 | addr_t target = ReadCoreReg (Rm, &success); |
| 1492 | if (!success) |
| 1493 | return false; |
| 1494 | |
| 1495 | Register dwarf_reg; |
| 1496 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm); |
| 1497 | context.SetRegister (dwarf_reg); |
| 1498 | if (!BXWritePC(context, target)) |
| 1499 | return false; |
| 1500 | } |
| 1501 | return true; |
| 1502 | } |
| 1503 | |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1504 | // Set r7 to point to some ip offset. |
| 1505 | // SUB (immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1506 | bool |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 1507 | EmulateInstructionARM::EmulateSUBR7IPImm (ARMEncoding encoding) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1508 | { |
| 1509 | #if 0 |
| 1510 | // ARM pseudo code... |
| 1511 | if (ConditionPassed()) |
| 1512 | { |
| 1513 | EncodingSpecificOperations(); |
| 1514 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 1515 | if d == 15 then // Can only occur for ARM encoding |
| 1516 | ALUWritePC(result); // setflags is always FALSE here |
| 1517 | else |
| 1518 | R[d] = result; |
| 1519 | if setflags then |
| 1520 | APSR.N = result<31>; |
| 1521 | APSR.Z = IsZeroBit(result); |
| 1522 | APSR.C = carry; |
| 1523 | APSR.V = overflow; |
| 1524 | } |
| 1525 | #endif |
| 1526 | |
| 1527 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1528 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1529 | if (!success) |
| 1530 | return false; |
| 1531 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1532 | if (ConditionPassed()) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1533 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1534 | const addr_t ip = ReadCoreReg (12, &success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1535 | if (!success) |
| 1536 | return false; |
| 1537 | uint32_t imm32; |
| 1538 | switch (encoding) { |
| 1539 | case eEncodingA1: |
| 1540 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 1541 | break; |
| 1542 | default: |
| 1543 | return false; |
| 1544 | } |
| 1545 | addr_t ip_offset = imm32; |
| 1546 | addr_t addr = ip - ip_offset; // the adjusted ip value |
| 1547 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1548 | EmulateInstruction::Context context; |
| 1549 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 1550 | Register dwarf_reg; |
| 1551 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r12); |
| 1552 | context.SetRegisterPlusOffset (dwarf_reg, -ip_offset); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1553 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1554 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r7, addr)) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1555 | return false; |
| 1556 | } |
| 1557 | return true; |
| 1558 | } |
| 1559 | |
| 1560 | // Set ip to point to some stack offset. |
| 1561 | // SUB (SP minus immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1562 | bool |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 1563 | EmulateInstructionARM::EmulateSUBIPSPImm (ARMEncoding encoding) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1564 | { |
| 1565 | #if 0 |
| 1566 | // ARM pseudo code... |
| 1567 | if (ConditionPassed()) |
| 1568 | { |
| 1569 | EncodingSpecificOperations(); |
| 1570 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 1571 | if d == 15 then // Can only occur for ARM encoding |
| 1572 | ALUWritePC(result); // setflags is always FALSE here |
| 1573 | else |
| 1574 | R[d] = result; |
| 1575 | if setflags then |
| 1576 | APSR.N = result<31>; |
| 1577 | APSR.Z = IsZeroBit(result); |
| 1578 | APSR.C = carry; |
| 1579 | APSR.V = overflow; |
| 1580 | } |
| 1581 | #endif |
| 1582 | |
| 1583 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1584 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1585 | if (!success) |
| 1586 | return false; |
| 1587 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1588 | if (ConditionPassed()) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1589 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1590 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1591 | if (!success) |
| 1592 | return false; |
| 1593 | uint32_t imm32; |
| 1594 | switch (encoding) { |
| 1595 | case eEncodingA1: |
| 1596 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 1597 | break; |
| 1598 | default: |
| 1599 | return false; |
| 1600 | } |
| 1601 | addr_t sp_offset = imm32; |
| 1602 | addr_t addr = sp - sp_offset; // the adjusted stack pointer value |
| 1603 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1604 | EmulateInstruction::Context context; |
| 1605 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 1606 | Register dwarf_reg; |
| 1607 | dwarf_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP); |
| 1608 | context.SetRegisterPlusOffset (dwarf_reg, -sp_offset); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1609 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1610 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r12, addr)) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1611 | return false; |
| 1612 | } |
| 1613 | return true; |
| 1614 | } |
| 1615 | |
Johnny Chen | c9e747f | 2011-02-23 01:55:07 +0000 | [diff] [blame] | 1616 | // This instruction subtracts an immediate value from the SP value, and writes |
| 1617 | // the result to the destination register. |
| 1618 | // |
| 1619 | // If Rd == 13 => A sub operation to adjust the SP -- allocate space for local storage. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1620 | bool |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 1621 | EmulateInstructionARM::EmulateSUBSPImm (ARMEncoding encoding) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1622 | { |
| 1623 | #if 0 |
| 1624 | // ARM pseudo code... |
| 1625 | if (ConditionPassed()) |
| 1626 | { |
| 1627 | EncodingSpecificOperations(); |
| 1628 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
Johnny Chen | 15a7a6b | 2011-02-23 23:47:56 +0000 | [diff] [blame] | 1629 | if d == 15 then // Can only occur for ARM encoding |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1630 | ALUWritePC(result); // setflags is always FALSE here |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1631 | else |
| 1632 | R[d] = result; |
| 1633 | if setflags then |
| 1634 | APSR.N = result<31>; |
| 1635 | APSR.Z = IsZeroBit(result); |
| 1636 | APSR.C = carry; |
| 1637 | APSR.V = overflow; |
| 1638 | } |
| 1639 | #endif |
| 1640 | |
| 1641 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1642 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1643 | if (!success) |
| 1644 | return false; |
| 1645 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1646 | if (ConditionPassed()) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1647 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1648 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1649 | if (!success) |
| 1650 | return false; |
Johnny Chen | c9e747f | 2011-02-23 01:55:07 +0000 | [diff] [blame] | 1651 | |
| 1652 | uint32_t Rd; |
| 1653 | bool setflags; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1654 | uint32_t imm32; |
| 1655 | switch (encoding) { |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 1656 | case eEncodingT1: |
Johnny Chen | c9e747f | 2011-02-23 01:55:07 +0000 | [diff] [blame] | 1657 | Rd = 13; |
| 1658 | setflags = false; |
Johnny Chen | a695f95 | 2011-02-23 21:24:25 +0000 | [diff] [blame] | 1659 | imm32 = ThumbImm7Scaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32) |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 1660 | break; |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 1661 | case eEncodingT2: |
Johnny Chen | c9e747f | 2011-02-23 01:55:07 +0000 | [diff] [blame] | 1662 | Rd = Bits32(opcode, 11, 8); |
| 1663 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 1664 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
Johnny Chen | c9e747f | 2011-02-23 01:55:07 +0000 | [diff] [blame] | 1665 | if (Rd == 15 && setflags) |
| 1666 | return EmulateCMPImm(eEncodingT2); |
| 1667 | if (Rd == 15 && !setflags) |
| 1668 | return false; |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 1669 | break; |
| 1670 | case eEncodingT3: |
Johnny Chen | c9e747f | 2011-02-23 01:55:07 +0000 | [diff] [blame] | 1671 | Rd = Bits32(opcode, 11, 8); |
| 1672 | setflags = false; |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 1673 | imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32) |
Johnny Chen | 15a7a6b | 2011-02-23 23:47:56 +0000 | [diff] [blame] | 1674 | if (Rd == 15) |
| 1675 | return false; |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 1676 | break; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1677 | case eEncodingA1: |
Johnny Chen | c9e747f | 2011-02-23 01:55:07 +0000 | [diff] [blame] | 1678 | Rd = Bits32(opcode, 15, 12); |
| 1679 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 1680 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
Johnny Chen | 15a7a6b | 2011-02-23 23:47:56 +0000 | [diff] [blame] | 1681 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 1682 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 1683 | if (Rd == 15 && setflags) |
| 1684 | return false; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1685 | break; |
| 1686 | default: |
| 1687 | return false; |
| 1688 | } |
Johnny Chen | c9e747f | 2011-02-23 01:55:07 +0000 | [diff] [blame] | 1689 | AddWithCarryResult res = AddWithCarry(sp, ~imm32, 1); |
| 1690 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1691 | EmulateInstruction::Context context; |
Johnny Chen | c9e747f | 2011-02-23 01:55:07 +0000 | [diff] [blame] | 1692 | if (Rd == 13) |
| 1693 | { |
| 1694 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 1695 | context.SetImmediateSigned (-imm32); // the stack pointer offset |
| 1696 | } |
| 1697 | else |
| 1698 | { |
| 1699 | context.type = EmulateInstruction::eContextImmediate; |
| 1700 | context.SetNoArgs (); |
| 1701 | } |
| 1702 | |
| 1703 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1704 | return false; |
| 1705 | } |
| 1706 | return true; |
| 1707 | } |
| 1708 | |
Johnny Chen | 08c25e8 | 2011-01-31 18:02:28 +0000 | [diff] [blame] | 1709 | // A store operation to the stack that also updates the SP. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1710 | bool |
| 1711 | EmulateInstructionARM::EmulateSTRRtSP (ARMEncoding encoding) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1712 | { |
| 1713 | #if 0 |
| 1714 | // ARM pseudo code... |
| 1715 | if (ConditionPassed()) |
| 1716 | { |
| 1717 | EncodingSpecificOperations(); |
| 1718 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 1719 | address = if index then offset_addr else R[n]; |
| 1720 | MemU[address,4] = if t == 15 then PCStoreValue() else R[t]; |
| 1721 | if wback then R[n] = offset_addr; |
| 1722 | } |
| 1723 | #endif |
| 1724 | |
| 1725 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1726 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1727 | if (!success) |
| 1728 | return false; |
| 1729 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1730 | if (ConditionPassed()) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1731 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1732 | const uint32_t addr_byte_size = GetAddressByteSize(); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1733 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1734 | if (!success) |
| 1735 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 1736 | uint32_t Rt; // the source register |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1737 | uint32_t imm12; |
| 1738 | switch (encoding) { |
| 1739 | case eEncodingA1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 1740 | Rt = Bits32(opcode, 15, 12); |
| 1741 | imm12 = Bits32(opcode, 11, 0); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1742 | break; |
| 1743 | default: |
| 1744 | return false; |
| 1745 | } |
| 1746 | addr_t sp_offset = imm12; |
| 1747 | addr_t addr = sp - sp_offset; |
| 1748 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1749 | EmulateInstruction::Context context; |
| 1750 | context.type = EmulateInstruction::eContextPushRegisterOnStack; |
| 1751 | Register dwarf_reg; |
| 1752 | dwarf_reg.SetRegister (eRegisterKindDWARF, 0); |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 1753 | if (Rt != 15) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1754 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1755 | dwarf_reg.num = dwarf_r0 + Rt; |
| 1756 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1757 | uint32_t reg_value = ReadCoreReg(Rt, &success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1758 | if (!success) |
| 1759 | return false; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 1760 | if (!MemUWrite (context, addr, reg_value, addr_byte_size)) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1761 | return false; |
| 1762 | } |
| 1763 | else |
| 1764 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1765 | dwarf_reg.num = dwarf_pc; |
| 1766 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1767 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1768 | if (!success) |
| 1769 | return false; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 1770 | if (!MemUWrite (context, addr, pc + 8, addr_byte_size)) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1771 | return false; |
| 1772 | } |
| 1773 | |
| 1774 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1775 | context.SetImmediateSigned (-sp_offset); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1776 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1777 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1778 | return false; |
| 1779 | } |
| 1780 | return true; |
| 1781 | } |
| 1782 | |
Johnny Chen | 08c25e8 | 2011-01-31 18:02:28 +0000 | [diff] [blame] | 1783 | // Vector Push stores multiple extension registers to the stack. |
| 1784 | // 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] | 1785 | bool |
| 1786 | EmulateInstructionARM::EmulateVPUSH (ARMEncoding encoding) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1787 | { |
| 1788 | #if 0 |
| 1789 | // ARM pseudo code... |
| 1790 | if (ConditionPassed()) |
| 1791 | { |
| 1792 | EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13); |
| 1793 | address = SP - imm32; |
| 1794 | SP = SP - imm32; |
| 1795 | if single_regs then |
| 1796 | for r = 0 to regs-1 |
| 1797 | MemA[address,4] = S[d+r]; address = address+4; |
| 1798 | else |
| 1799 | for r = 0 to regs-1 |
| 1800 | // Store as two word-aligned words in the correct order for current endianness. |
| 1801 | MemA[address,4] = if BigEndian() then D[d+r]<63:32> else D[d+r]<31:0>; |
| 1802 | MemA[address+4,4] = if BigEndian() then D[d+r]<31:0> else D[d+r]<63:32>; |
| 1803 | address = address+8; |
| 1804 | } |
| 1805 | #endif |
| 1806 | |
| 1807 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1808 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1809 | if (!success) |
| 1810 | return false; |
| 1811 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1812 | if (ConditionPassed()) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1813 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1814 | const uint32_t addr_byte_size = GetAddressByteSize(); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1815 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1816 | if (!success) |
| 1817 | return false; |
| 1818 | bool single_regs; |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1819 | uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1820 | uint32_t imm32; // stack offset |
| 1821 | uint32_t regs; // number of registers |
| 1822 | switch (encoding) { |
| 1823 | case eEncodingT1: |
| 1824 | case eEncodingA1: |
| 1825 | single_regs = false; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1826 | d = Bit32(opcode, 22) << 4 | Bits32(opcode, 15, 12); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1827 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1828 | // If UInt(imm8) is odd, see "FSTMX". |
| 1829 | regs = Bits32(opcode, 7, 0) / 2; |
| 1830 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1831 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1832 | return false; |
| 1833 | break; |
| 1834 | case eEncodingT2: |
| 1835 | case eEncodingA2: |
| 1836 | single_regs = true; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1837 | d = Bits32(opcode, 15, 12) << 1 | Bit32(opcode, 22); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1838 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1839 | regs = Bits32(opcode, 7, 0); |
| 1840 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1841 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1842 | return false; |
| 1843 | break; |
| 1844 | default: |
| 1845 | return false; |
| 1846 | } |
| 1847 | uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0; |
| 1848 | uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2; |
| 1849 | addr_t sp_offset = imm32; |
| 1850 | addr_t addr = sp - sp_offset; |
| 1851 | uint32_t i; |
| 1852 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1853 | EmulateInstruction::Context context; |
| 1854 | context.type = EmulateInstruction::eContextPushRegisterOnStack; |
| 1855 | Register dwarf_reg; |
| 1856 | dwarf_reg.SetRegister (eRegisterKindDWARF, 0); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1857 | for (i=d; i<regs; ++i) |
| 1858 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1859 | dwarf_reg.num = start_reg + i; |
| 1860 | context.SetRegisterPlusOffset ( dwarf_reg, addr - sp); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1861 | // uint64_t to accommodate 64-bit registers. |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1862 | uint64_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_reg.num, 0, &success); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1863 | if (!success) |
| 1864 | return false; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 1865 | if (!MemAWrite (context, addr, reg_value, reg_byte_size)) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1866 | return false; |
| 1867 | addr += reg_byte_size; |
| 1868 | } |
| 1869 | |
| 1870 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1871 | context.SetImmediateSigned (-sp_offset); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1872 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1873 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1874 | return false; |
| 1875 | } |
| 1876 | return true; |
| 1877 | } |
| 1878 | |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1879 | // Vector Pop loads multiple extension registers from the stack. |
| 1880 | // It also updates SP to point just above the loaded data. |
| 1881 | bool |
| 1882 | EmulateInstructionARM::EmulateVPOP (ARMEncoding encoding) |
| 1883 | { |
| 1884 | #if 0 |
| 1885 | // ARM pseudo code... |
| 1886 | if (ConditionPassed()) |
| 1887 | { |
| 1888 | EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13); |
| 1889 | address = SP; |
| 1890 | SP = SP + imm32; |
| 1891 | if single_regs then |
| 1892 | for r = 0 to regs-1 |
| 1893 | S[d+r] = MemA[address,4]; address = address+4; |
| 1894 | else |
| 1895 | for r = 0 to regs-1 |
| 1896 | word1 = MemA[address,4]; word2 = MemA[address+4,4]; address = address+8; |
| 1897 | // Combine the word-aligned words in the correct order for current endianness. |
| 1898 | D[d+r] = if BigEndian() then word1:word2 else word2:word1; |
| 1899 | } |
| 1900 | #endif |
| 1901 | |
| 1902 | bool success = false; |
| 1903 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1904 | if (!success) |
| 1905 | return false; |
| 1906 | |
| 1907 | if (ConditionPassed()) |
| 1908 | { |
| 1909 | const uint32_t addr_byte_size = GetAddressByteSize(); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1910 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1911 | if (!success) |
| 1912 | return false; |
| 1913 | bool single_regs; |
| 1914 | uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register |
| 1915 | uint32_t imm32; // stack offset |
| 1916 | uint32_t regs; // number of registers |
| 1917 | switch (encoding) { |
| 1918 | case eEncodingT1: |
| 1919 | case eEncodingA1: |
| 1920 | single_regs = false; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1921 | d = Bit32(opcode, 22) << 4 | Bits32(opcode, 15, 12); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1922 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1923 | // If UInt(imm8) is odd, see "FLDMX". |
| 1924 | regs = Bits32(opcode, 7, 0) / 2; |
| 1925 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1926 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1927 | return false; |
| 1928 | break; |
| 1929 | case eEncodingT2: |
| 1930 | case eEncodingA2: |
| 1931 | single_regs = true; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1932 | d = Bits32(opcode, 15, 12) << 1 | Bit32(opcode, 22); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1933 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1934 | regs = Bits32(opcode, 7, 0); |
| 1935 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1936 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1937 | return false; |
| 1938 | break; |
| 1939 | default: |
| 1940 | return false; |
| 1941 | } |
| 1942 | uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0; |
| 1943 | uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2; |
| 1944 | addr_t sp_offset = imm32; |
| 1945 | addr_t addr = sp; |
| 1946 | uint32_t i; |
| 1947 | uint64_t data; // uint64_t to accomodate 64-bit registers. |
| 1948 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1949 | EmulateInstruction::Context context; |
| 1950 | context.type = EmulateInstruction::eContextPopRegisterOffStack; |
| 1951 | Register dwarf_reg; |
| 1952 | dwarf_reg.SetRegister (eRegisterKindDWARF, 0); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1953 | for (i=d; i<regs; ++i) |
| 1954 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1955 | dwarf_reg.num = start_reg + i; |
| 1956 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 1957 | data = MemARead(context, addr, reg_byte_size, 0, &success); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1958 | if (!success) |
| 1959 | return false; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1960 | if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_reg.num, data)) |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1961 | return false; |
| 1962 | addr += reg_byte_size; |
| 1963 | } |
| 1964 | |
| 1965 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1966 | context.SetImmediateSigned (sp_offset); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1967 | |
| 1968 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset)) |
| 1969 | return false; |
| 1970 | } |
| 1971 | return true; |
| 1972 | } |
| 1973 | |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 1974 | // SVC (previously SWI) |
| 1975 | bool |
| 1976 | EmulateInstructionARM::EmulateSVC (ARMEncoding encoding) |
| 1977 | { |
| 1978 | #if 0 |
| 1979 | // ARM pseudo code... |
| 1980 | if (ConditionPassed()) |
| 1981 | { |
| 1982 | EncodingSpecificOperations(); |
| 1983 | CallSupervisor(); |
| 1984 | } |
| 1985 | #endif |
| 1986 | |
| 1987 | bool success = false; |
| 1988 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1989 | if (!success) |
| 1990 | return false; |
| 1991 | |
| 1992 | if (ConditionPassed()) |
| 1993 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1994 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 1995 | addr_t lr; // next instruction address |
| 1996 | if (!success) |
| 1997 | return false; |
| 1998 | uint32_t imm32; // the immediate constant |
| 1999 | uint32_t mode; // ARM or Thumb mode |
| 2000 | switch (encoding) { |
| 2001 | case eEncodingT1: |
| 2002 | lr = (pc + 2) | 1u; // return address |
| 2003 | imm32 = Bits32(opcode, 7, 0); |
| 2004 | mode = eModeThumb; |
| 2005 | break; |
| 2006 | case eEncodingA1: |
| 2007 | lr = pc + 4; // return address |
| 2008 | imm32 = Bits32(opcode, 23, 0); |
| 2009 | mode = eModeARM; |
| 2010 | break; |
| 2011 | default: |
| 2012 | return false; |
| 2013 | } |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2014 | |
| 2015 | EmulateInstruction::Context context; |
| 2016 | context.type = EmulateInstruction::eContextSupervisorCall; |
| 2017 | context.SetModeAndImmediate (mode, imm32); |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 2018 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr)) |
| 2019 | return false; |
| 2020 | } |
| 2021 | return true; |
| 2022 | } |
| 2023 | |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 2024 | // If Then makes up to four following instructions (the IT block) conditional. |
| 2025 | bool |
| 2026 | EmulateInstructionARM::EmulateIT (ARMEncoding encoding) |
| 2027 | { |
| 2028 | #if 0 |
| 2029 | // ARM pseudo code... |
| 2030 | EncodingSpecificOperations(); |
| 2031 | ITSTATE.IT<7:0> = firstcond:mask; |
| 2032 | #endif |
| 2033 | |
| 2034 | bool success = false; |
| 2035 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2036 | if (!success) |
| 2037 | return false; |
| 2038 | |
| 2039 | m_it_session.InitIT(Bits32(opcode, 7, 0)); |
| 2040 | return true; |
| 2041 | } |
| 2042 | |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 2043 | // Branch causes a branch to a target address. |
| 2044 | bool |
| 2045 | EmulateInstructionARM::EmulateB (ARMEncoding encoding) |
| 2046 | { |
| 2047 | #if 0 |
| 2048 | // ARM pseudo code... |
| 2049 | if (ConditionPassed()) |
| 2050 | { |
| 2051 | EncodingSpecificOperations(); |
| 2052 | BranchWritePC(PC + imm32); |
| 2053 | } |
| 2054 | #endif |
| 2055 | |
| 2056 | bool success = false; |
| 2057 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2058 | if (!success) |
| 2059 | return false; |
| 2060 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2061 | if (ConditionPassed()) |
| 2062 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2063 | EmulateInstruction::Context context; |
| 2064 | context.type = EmulateInstruction::eContextRelativeBranchImmediate; |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2065 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2066 | if (!success) |
| 2067 | return false; |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 2068 | addr_t target; // target address |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2069 | int32_t imm32; // PC-relative offset |
| 2070 | switch (encoding) { |
| 2071 | case eEncodingT1: |
| 2072 | // The 'cond' field is handled in EmulateInstructionARM::CurrentCond(). |
| 2073 | imm32 = llvm::SignExtend32<9>(Bits32(opcode, 7, 0) << 1); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2074 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2075 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2076 | break; |
| 2077 | case eEncodingT2: |
| 2078 | imm32 = llvm::SignExtend32<12>(Bits32(opcode, 10, 0)); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2079 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2080 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2081 | break; |
| 2082 | case eEncodingT3: |
| 2083 | // The 'cond' field is handled in EmulateInstructionARM::CurrentCond(). |
| 2084 | { |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 2085 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2086 | uint32_t imm6 = Bits32(opcode, 21, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 2087 | uint32_t J1 = Bit32(opcode, 13); |
| 2088 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2089 | uint32_t imm11 = Bits32(opcode, 10, 0); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 2090 | 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] | 2091 | imm32 = llvm::SignExtend32<21>(imm21); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2092 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2093 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2094 | break; |
| 2095 | } |
| 2096 | case eEncodingT4: |
| 2097 | { |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 2098 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2099 | uint32_t imm10 = Bits32(opcode, 25, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 2100 | uint32_t J1 = Bit32(opcode, 13); |
| 2101 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2102 | uint32_t imm11 = Bits32(opcode, 10, 0); |
| 2103 | uint32_t I1 = !(J1 ^ S); |
| 2104 | uint32_t I2 = !(J2 ^ S); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 2105 | 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] | 2106 | imm32 = llvm::SignExtend32<25>(imm25); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2107 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2108 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2109 | break; |
| 2110 | } |
| 2111 | case eEncodingA1: |
| 2112 | imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2113 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2114 | context.SetModeAndImmediateSigned (eModeARM, 8 + imm32); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 2115 | break; |
| 2116 | default: |
| 2117 | return false; |
| 2118 | } |
| 2119 | if (!BranchWritePC(context, target)) |
| 2120 | return false; |
| 2121 | } |
| 2122 | return true; |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 2125 | // Compare and Branch on Nonzero and Compare and Branch on Zero compare the value in a register with |
| 2126 | // zero and conditionally branch forward a constant value. They do not affect the condition flags. |
| 2127 | // CBNZ, CBZ |
| 2128 | bool |
| 2129 | EmulateInstructionARM::EmulateCB (ARMEncoding encoding) |
| 2130 | { |
| 2131 | #if 0 |
| 2132 | // ARM pseudo code... |
| 2133 | EncodingSpecificOperations(); |
| 2134 | if nonzero ^ IsZero(R[n]) then |
| 2135 | BranchWritePC(PC + imm32); |
| 2136 | #endif |
| 2137 | |
| 2138 | bool success = false; |
| 2139 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2140 | if (!success) |
| 2141 | return false; |
| 2142 | |
| 2143 | // Read the register value from the operand register Rn. |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2144 | uint32_t reg_val = ReadCoreReg(Bits32(opcode, 2, 0), &success); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 2145 | if (!success) |
| 2146 | return false; |
| 2147 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2148 | EmulateInstruction::Context context; |
| 2149 | context.type = EmulateInstruction::eContextRelativeBranchImmediate; |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2150 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 2151 | if (!success) |
| 2152 | return false; |
| 2153 | |
| 2154 | addr_t target; // target address |
| 2155 | uint32_t imm32; // PC-relative offset to branch forward |
| 2156 | bool nonzero; |
| 2157 | switch (encoding) { |
| 2158 | case eEncodingT1: |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 2159 | imm32 = Bit32(opcode, 9) << 6 | Bits32(opcode, 7, 3) << 1; |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 2160 | nonzero = BitIsSet(opcode, 11); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2161 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2162 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 2163 | break; |
| 2164 | default: |
| 2165 | return false; |
| 2166 | } |
| 2167 | if (nonzero ^ (reg_val == 0)) |
| 2168 | if (!BranchWritePC(context, target)) |
| 2169 | return false; |
| 2170 | |
| 2171 | return true; |
| 2172 | } |
| 2173 | |
Johnny Chen | 60299ec | 2011-02-17 19:34:27 +0000 | [diff] [blame] | 2174 | // Table Branch Byte causes a PC-relative forward branch using a table of single byte offsets. |
| 2175 | // A base register provides a pointer to the table, and a second register supplies an index into the table. |
| 2176 | // The branch length is twice the value of the byte returned from the table. |
| 2177 | // |
| 2178 | // Table Branch Halfword causes a PC-relative forward branch using a table of single halfword offsets. |
| 2179 | // A base register provides a pointer to the table, and a second register supplies an index into the table. |
| 2180 | // The branch length is twice the value of the halfword returned from the table. |
| 2181 | // TBB, TBH |
| 2182 | bool |
| 2183 | EmulateInstructionARM::EmulateTB (ARMEncoding encoding) |
| 2184 | { |
| 2185 | #if 0 |
| 2186 | // ARM pseudo code... |
| 2187 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 2188 | if is_tbh then |
| 2189 | halfwords = UInt(MemU[R[n]+LSL(R[m],1), 2]); |
| 2190 | else |
| 2191 | halfwords = UInt(MemU[R[n]+R[m], 1]); |
| 2192 | BranchWritePC(PC + 2*halfwords); |
| 2193 | #endif |
| 2194 | |
| 2195 | bool success = false; |
| 2196 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2197 | if (!success) |
| 2198 | return false; |
| 2199 | |
| 2200 | uint32_t Rn; // the base register which contains the address of the table of branch lengths |
| 2201 | uint32_t Rm; // the index register which contains an integer pointing to a byte/halfword in the table |
| 2202 | bool is_tbh; // true if table branch halfword |
| 2203 | switch (encoding) { |
| 2204 | case eEncodingT1: |
| 2205 | Rn = Bits32(opcode, 19, 16); |
| 2206 | Rm = Bits32(opcode, 3, 0); |
| 2207 | is_tbh = BitIsSet(opcode, 4); |
| 2208 | if (Rn == 13 || BadReg(Rm)) |
| 2209 | return false; |
| 2210 | if (InITBlock() && !LastInITBlock()) |
| 2211 | return false; |
| 2212 | break; |
| 2213 | default: |
| 2214 | return false; |
| 2215 | } |
| 2216 | |
| 2217 | // Read the address of the table from the operand register Rn. |
| 2218 | // The PC can be used, in which case the table immediately follows this instruction. |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2219 | uint32_t base = ReadCoreReg(Rm, &success); |
Johnny Chen | 60299ec | 2011-02-17 19:34:27 +0000 | [diff] [blame] | 2220 | if (!success) |
| 2221 | return false; |
| 2222 | |
| 2223 | // the table index |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2224 | uint32_t index = ReadCoreReg(Rm, &success); |
Johnny Chen | 60299ec | 2011-02-17 19:34:27 +0000 | [diff] [blame] | 2225 | if (!success) |
| 2226 | return false; |
| 2227 | |
| 2228 | // the offsetted table address |
| 2229 | addr_t addr = base + (is_tbh ? index*2 : index); |
| 2230 | |
| 2231 | // PC-relative offset to branch forward |
| 2232 | EmulateInstruction::Context context; |
| 2233 | context.type = EmulateInstruction::eContextTableBranchReadMemory; |
Johnny Chen | 104c8b6 | 2011-02-17 23:27:44 +0000 | [diff] [blame] | 2234 | 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] | 2235 | if (!success) |
| 2236 | return false; |
| 2237 | |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2238 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | 60299ec | 2011-02-17 19:34:27 +0000 | [diff] [blame] | 2239 | if (!success) |
| 2240 | return false; |
| 2241 | |
| 2242 | // target address |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2243 | addr_t target = pc + offset; |
Johnny Chen | 60299ec | 2011-02-17 19:34:27 +0000 | [diff] [blame] | 2244 | context.type = EmulateInstruction::eContextRelativeBranchImmediate; |
| 2245 | context.SetModeAndImmediateSigned (eModeThumb, 4 + offset); |
| 2246 | |
| 2247 | if (!BranchWritePC(context, target)) |
| 2248 | return false; |
| 2249 | |
| 2250 | return true; |
| 2251 | } |
| 2252 | |
Caroline Tice | dcc11b3 | 2011-03-02 23:57:02 +0000 | [diff] [blame] | 2253 | // This instruction adds an immediate value to a register value, and writes the result to the destination register. |
| 2254 | // It can optionally update the condition flags based on the result. |
| 2255 | bool |
| 2256 | EmulateInstructionARM::EmulateADDImmThumb (ARMEncoding encoding) |
| 2257 | { |
| 2258 | #if 0 |
| 2259 | if ConditionPassed() then |
| 2260 | EncodingSpecificOperations(); |
| 2261 | (result, carry, overflow) = AddWithCarry(R[n], imm32, ’0’); |
| 2262 | R[d] = result; |
| 2263 | if setflags then |
| 2264 | APSR.N = result<31>; |
| 2265 | APSR.Z = IsZeroBit(result); |
| 2266 | APSR.C = carry; |
| 2267 | APSR.V = overflow; |
| 2268 | #endif |
| 2269 | |
| 2270 | bool success = false; |
| 2271 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2272 | if (!success) |
| 2273 | return false; |
| 2274 | |
| 2275 | if (ConditionPassed()) |
| 2276 | { |
| 2277 | uint32_t d; |
| 2278 | uint32_t n; |
| 2279 | bool setflags; |
| 2280 | uint32_t imm32; |
| 2281 | uint32_t carry_out; |
| 2282 | |
| 2283 | //EncodingSpecificOperations(); |
| 2284 | switch (encoding) |
| 2285 | { |
| 2286 | case eEncodingT1: |
| 2287 | // d = UInt(Rd); n = UInt(Rn); setflags = !InITBlock(); imm32 = ZeroExtend(imm3, 32); |
| 2288 | d = Bits32 (opcode, 2, 0); |
| 2289 | n = Bits32 (opcode, 5, 3); |
| 2290 | setflags = !InITBlock(); |
| 2291 | imm32 = Bits32 (opcode, 8,6); |
| 2292 | |
| 2293 | break; |
| 2294 | |
| 2295 | case eEncodingT2: |
| 2296 | // d = UInt(Rdn); n = UInt(Rdn); setflags = !InITBlock(); imm32 = ZeroExtend(imm8, 32); |
| 2297 | d = Bits32 (opcode, 10, 8); |
| 2298 | n = Bits32 (opcode, 10, 8); |
| 2299 | setflags = !InITBlock(); |
| 2300 | imm32 = Bits32 (opcode, 7, 0); |
| 2301 | |
| 2302 | break; |
| 2303 | |
| 2304 | case eEncodingT3: |
| 2305 | // if Rd == ’1111’ && S == ’1’ then SEE CMN (immediate); |
| 2306 | // if Rn == ’1101’ then SEE ADD (SP plus immediate); |
| 2307 | // d = UInt(Rd); n = UInt(Rn); setflags = (S == ’1’); imm32 = ThumbExpandImm(i:imm3:imm8); |
| 2308 | d = Bits32 (opcode, 11, 8); |
| 2309 | n = Bits32 (opcode, 19, 16); |
| 2310 | setflags = BitIsSet (opcode, 20); |
| 2311 | imm32 = ThumbExpandImm_C (opcode, APSR_C, carry_out); |
| 2312 | |
| 2313 | // if BadReg(d) || n == 15 then UNPREDICTABLE; |
| 2314 | if (BadReg (d) || (n == 15)) |
| 2315 | return false; |
| 2316 | |
| 2317 | break; |
| 2318 | |
| 2319 | case eEncodingT4: |
| 2320 | { |
| 2321 | // if Rn == ’1111’ then SEE ADR; |
| 2322 | // if Rn == ’1101’ then SEE ADD (SP plus immediate); |
| 2323 | // d = UInt(Rd); n = UInt(Rn); setflags = FALSE; imm32 = ZeroExtend(i:imm3:imm8, 32); |
| 2324 | d = Bits32 (opcode, 11, 8); |
| 2325 | n = Bits32 (opcode, 19, 16); |
| 2326 | setflags = false; |
| 2327 | uint32_t i = Bit32 (opcode, 26); |
| 2328 | uint32_t imm3 = Bits32 (opcode, 14, 12); |
| 2329 | uint32_t imm8 = Bits32 (opcode, 7, 0); |
| 2330 | imm32 = (i << 11) | (imm3 << 8) | imm8; |
| 2331 | |
| 2332 | // if BadReg(d) then UNPREDICTABLE; |
| 2333 | if (BadReg (d)) |
| 2334 | return false; |
| 2335 | |
| 2336 | break; |
| 2337 | } |
| 2338 | default: |
| 2339 | return false; |
| 2340 | } |
| 2341 | |
| 2342 | uint64_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 2343 | if (!success) |
| 2344 | return false; |
| 2345 | |
| 2346 | //(result, carry, overflow) = AddWithCarry(R[n], imm32, ’0’); |
| 2347 | AddWithCarryResult res = AddWithCarry (Rn, imm32, 0); |
| 2348 | |
| 2349 | Register reg_n; |
| 2350 | reg_n.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 2351 | |
| 2352 | EmulateInstruction::Context context; |
| 2353 | context.type = eContextAddition; |
| 2354 | context.SetRegisterPlusOffset (reg_n, imm32); |
| 2355 | |
| 2356 | //R[d] = result; |
| 2357 | //if setflags then |
| 2358 | //APSR.N = result<31>; |
| 2359 | //APSR.Z = IsZeroBit(result); |
| 2360 | //APSR.C = carry; |
| 2361 | //APSR.V = overflow; |
| 2362 | if (!WriteCoreRegOptionalFlags (context, res.result, d, setflags, res.carry_out, res.overflow)) |
| 2363 | return false; |
| 2364 | |
| 2365 | } |
| 2366 | return true; |
| 2367 | } |
| 2368 | |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 2369 | // This instruction adds an immediate value to a register value, and writes the result to the destination |
| 2370 | // register. It can optionally update the condition flags based on the result. |
| 2371 | bool |
| 2372 | EmulateInstructionARM::EmulateADDImmARM (ARMEncoding encoding) |
| 2373 | { |
| 2374 | #if 0 |
| 2375 | // ARM pseudo code... |
| 2376 | if ConditionPassed() then |
| 2377 | EncodingSpecificOperations(); |
| 2378 | (result, carry, overflow) = AddWithCarry(R[n], imm32, '0'); |
| 2379 | if d == 15 then |
| 2380 | ALUWritePC(result); // setflags is always FALSE here |
| 2381 | else |
| 2382 | R[d] = result; |
| 2383 | if setflags then |
| 2384 | APSR.N = result<31>; |
| 2385 | APSR.Z = IsZeroBit(result); |
| 2386 | APSR.C = carry; |
| 2387 | APSR.V = overflow; |
| 2388 | #endif |
| 2389 | |
| 2390 | bool success = false; |
| 2391 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2392 | if (!success) |
| 2393 | return false; |
| 2394 | |
| 2395 | if (ConditionPassed()) |
| 2396 | { |
| 2397 | uint32_t Rd, Rn; |
| 2398 | uint32_t imm32; // the immediate value to be added to the value obtained from Rn |
| 2399 | bool setflags; |
| 2400 | switch (encoding) |
| 2401 | { |
| 2402 | case eEncodingA1: |
| 2403 | Rd = Bits32(opcode, 15, 12); |
| 2404 | Rn = Bits32(opcode, 19, 16); |
| 2405 | setflags = BitIsSet(opcode, 20); |
| 2406 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 2407 | break; |
| 2408 | default: |
| 2409 | return false; |
| 2410 | } |
| 2411 | |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 2412 | // Read the first operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 2413 | uint32_t val1 = ReadCoreReg(Rn, &success); |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 2414 | if (!success) |
| 2415 | return false; |
| 2416 | |
| 2417 | AddWithCarryResult res = AddWithCarry(val1, imm32, 0); |
| 2418 | |
| 2419 | EmulateInstruction::Context context; |
| 2420 | context.type = EmulateInstruction::eContextImmediate; |
| 2421 | context.SetNoArgs (); |
| 2422 | |
| 2423 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 2424 | return false; |
| 2425 | } |
| 2426 | return true; |
| 2427 | } |
| 2428 | |
Johnny Chen | d761dcf | 2011-02-17 22:03:29 +0000 | [diff] [blame] | 2429 | // This instruction adds a register value and an optionally-shifted register value, and writes the result |
| 2430 | // 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] | 2431 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 2432 | EmulateInstructionARM::EmulateADDReg (ARMEncoding encoding) |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2433 | { |
| 2434 | #if 0 |
| 2435 | // ARM pseudo code... |
| 2436 | if ConditionPassed() then |
| 2437 | EncodingSpecificOperations(); |
| 2438 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 2439 | (result, carry, overflow) = AddWithCarry(R[n], shifted, '0'); |
| 2440 | if d == 15 then |
| 2441 | ALUWritePC(result); // setflags is always FALSE here |
| 2442 | else |
| 2443 | R[d] = result; |
| 2444 | if setflags then |
| 2445 | APSR.N = result<31>; |
| 2446 | APSR.Z = IsZeroBit(result); |
| 2447 | APSR.C = carry; |
| 2448 | APSR.V = overflow; |
| 2449 | #endif |
| 2450 | |
| 2451 | bool success = false; |
| 2452 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2453 | if (!success) |
| 2454 | return false; |
| 2455 | |
| 2456 | if (ConditionPassed()) |
| 2457 | { |
| 2458 | uint32_t Rd, Rn, Rm; |
Johnny Chen | d761dcf | 2011-02-17 22:03:29 +0000 | [diff] [blame] | 2459 | ARM_ShifterType shift_t; |
| 2460 | 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] | 2461 | bool setflags; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2462 | switch (encoding) |
| 2463 | { |
Johnny Chen | d761dcf | 2011-02-17 22:03:29 +0000 | [diff] [blame] | 2464 | case eEncodingT1: |
| 2465 | Rd = Bits32(opcode, 2, 0); |
| 2466 | Rn = Bits32(opcode, 5, 3); |
| 2467 | Rm = Bits32(opcode, 8, 6); |
| 2468 | setflags = !InITBlock(); |
| 2469 | shift_t = SRType_LSL; |
| 2470 | shift_n = 0; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 2471 | break; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2472 | case eEncodingT2: |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 2473 | Rd = Rn = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 0); |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2474 | Rm = Bits32(opcode, 6, 3); |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 2475 | setflags = false; |
Johnny Chen | d761dcf | 2011-02-17 22:03:29 +0000 | [diff] [blame] | 2476 | shift_t = SRType_LSL; |
| 2477 | shift_n = 0; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2478 | if (Rn == 15 && Rm == 15) |
| 2479 | return false; |
Johnny Chen | d761dcf | 2011-02-17 22:03:29 +0000 | [diff] [blame] | 2480 | if (Rd == 15 && InITBlock() && !LastInITBlock()) |
| 2481 | return false; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2482 | break; |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 2483 | case eEncodingA1: |
| 2484 | Rd = Bits32(opcode, 15, 12); |
| 2485 | Rn = Bits32(opcode, 19, 16); |
| 2486 | Rm = Bits32(opcode, 3, 0); |
| 2487 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 2488 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 2489 | break; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2490 | default: |
| 2491 | return false; |
| 2492 | } |
| 2493 | |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2494 | // Read the first operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 2495 | uint32_t val1 = ReadCoreReg(Rn, &success); |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2496 | if (!success) |
| 2497 | return false; |
| 2498 | |
| 2499 | // Read the second operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 2500 | uint32_t val2 = ReadCoreReg(Rm, &success); |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2501 | if (!success) |
| 2502 | return false; |
| 2503 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 2504 | uint32_t shifted = Shift(val2, shift_t, shift_n, APSR_C); |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 2505 | AddWithCarryResult res = AddWithCarry(val1, shifted, 0); |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2506 | |
| 2507 | EmulateInstruction::Context context; |
| 2508 | context.type = EmulateInstruction::eContextImmediate; |
| 2509 | context.SetNoArgs (); |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 2510 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2511 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 2512 | return false; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2513 | } |
| 2514 | return true; |
| 2515 | } |
| 2516 | |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2517 | // Compare Negative (immediate) adds a register value and an immediate value. |
| 2518 | // It updates the condition flags based on the result, and discards the result. |
Johnny Chen | d4dc444 | 2011-02-11 02:02:56 +0000 | [diff] [blame] | 2519 | bool |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2520 | EmulateInstructionARM::EmulateCMNImm (ARMEncoding encoding) |
| 2521 | { |
| 2522 | #if 0 |
| 2523 | // ARM pseudo code... |
| 2524 | if ConditionPassed() then |
| 2525 | EncodingSpecificOperations(); |
| 2526 | (result, carry, overflow) = AddWithCarry(R[n], imm32, '0'); |
| 2527 | APSR.N = result<31>; |
| 2528 | APSR.Z = IsZeroBit(result); |
| 2529 | APSR.C = carry; |
| 2530 | APSR.V = overflow; |
| 2531 | #endif |
| 2532 | |
| 2533 | bool success = false; |
| 2534 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2535 | if (!success) |
| 2536 | return false; |
| 2537 | |
| 2538 | uint32_t Rn; // the first operand |
| 2539 | uint32_t imm32; // the immediate value to be compared with |
| 2540 | switch (encoding) { |
| 2541 | case eEncodingT1: |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 2542 | Rn = Bits32(opcode, 19, 16); |
| 2543 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 2544 | if (Rn == 15) |
| 2545 | return false; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 2546 | break; |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2547 | case eEncodingA1: |
| 2548 | Rn = Bits32(opcode, 19, 16); |
| 2549 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 2550 | break; |
| 2551 | default: |
| 2552 | return false; |
| 2553 | } |
| 2554 | // Read the register value from the operand register Rn. |
| 2555 | uint32_t reg_val = ReadCoreReg(Rn, &success); |
| 2556 | if (!success) |
| 2557 | return false; |
| 2558 | |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 2559 | AddWithCarryResult res = AddWithCarry(reg_val, imm32, 0); |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2560 | |
| 2561 | EmulateInstruction::Context context; |
| 2562 | context.type = EmulateInstruction::eContextImmediate; |
| 2563 | context.SetNoArgs (); |
| 2564 | if (!WriteFlags(context, res.result, res.carry_out, res.overflow)) |
| 2565 | return false; |
| 2566 | |
| 2567 | return true; |
| 2568 | } |
| 2569 | |
| 2570 | // Compare Negative (register) adds a register value and an optionally-shifted register value. |
| 2571 | // It updates the condition flags based on the result, and discards the result. |
| 2572 | bool |
| 2573 | EmulateInstructionARM::EmulateCMNReg (ARMEncoding encoding) |
| 2574 | { |
| 2575 | #if 0 |
| 2576 | // ARM pseudo code... |
| 2577 | if ConditionPassed() then |
| 2578 | EncodingSpecificOperations(); |
| 2579 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 2580 | (result, carry, overflow) = AddWithCarry(R[n], shifted, '0'); |
| 2581 | APSR.N = result<31>; |
| 2582 | APSR.Z = IsZeroBit(result); |
| 2583 | APSR.C = carry; |
| 2584 | APSR.V = overflow; |
| 2585 | #endif |
| 2586 | |
| 2587 | bool success = false; |
| 2588 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2589 | if (!success) |
| 2590 | return false; |
| 2591 | |
| 2592 | uint32_t Rn; // the first operand |
| 2593 | uint32_t Rm; // the second operand |
| 2594 | ARM_ShifterType shift_t; |
| 2595 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 2596 | switch (encoding) { |
| 2597 | case eEncodingT1: |
| 2598 | Rn = Bits32(opcode, 2, 0); |
| 2599 | Rm = Bits32(opcode, 5, 3); |
| 2600 | shift_t = SRType_LSL; |
| 2601 | shift_n = 0; |
| 2602 | break; |
| 2603 | case eEncodingT2: |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 2604 | Rn = Bits32(opcode, 19, 16); |
| 2605 | Rm = Bits32(opcode, 3, 0); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 2606 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 2607 | // if n == 15 || BadReg(m) then UNPREDICTABLE; |
| 2608 | if (Rn == 15 || BadReg(Rm)) |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2609 | return false; |
| 2610 | break; |
| 2611 | case eEncodingA1: |
| 2612 | Rn = Bits32(opcode, 19, 16); |
| 2613 | Rm = Bits32(opcode, 3, 0); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 2614 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 2615 | break; |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2616 | default: |
| 2617 | return false; |
| 2618 | } |
| 2619 | // Read the register value from register Rn. |
| 2620 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 2621 | if (!success) |
| 2622 | return false; |
| 2623 | |
| 2624 | // Read the register value from register Rm. |
| 2625 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 2626 | if (!success) |
| 2627 | return false; |
| 2628 | |
| 2629 | uint32_t shifted = Shift(val2, shift_t, shift_n, APSR_C); |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 2630 | AddWithCarryResult res = AddWithCarry(val1, shifted, 0); |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2631 | |
| 2632 | EmulateInstruction::Context context; |
| 2633 | context.type = EmulateInstruction::eContextImmediate; |
| 2634 | context.SetNoArgs(); |
| 2635 | if (!WriteFlags(context, res.result, res.carry_out, res.overflow)) |
| 2636 | return false; |
| 2637 | |
| 2638 | return true; |
| 2639 | } |
| 2640 | |
| 2641 | // Compare (immediate) subtracts an immediate value from a register value. |
| 2642 | // It updates the condition flags based on the result, and discards the result. |
| 2643 | bool |
| 2644 | EmulateInstructionARM::EmulateCMPImm (ARMEncoding encoding) |
Johnny Chen | d4dc444 | 2011-02-11 02:02:56 +0000 | [diff] [blame] | 2645 | { |
| 2646 | #if 0 |
| 2647 | // ARM pseudo code... |
| 2648 | if ConditionPassed() then |
| 2649 | EncodingSpecificOperations(); |
| 2650 | (result, carry, overflow) = AddWithCarry(R[n], NOT(imm32), '1'); |
| 2651 | APSR.N = result<31>; |
| 2652 | APSR.Z = IsZeroBit(result); |
| 2653 | APSR.C = carry; |
| 2654 | APSR.V = overflow; |
| 2655 | #endif |
| 2656 | |
| 2657 | bool success = false; |
| 2658 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2659 | if (!success) |
| 2660 | return false; |
| 2661 | |
| 2662 | uint32_t Rn; // the first operand |
| 2663 | uint32_t imm32; // the immediate value to be compared with |
| 2664 | switch (encoding) { |
| 2665 | case eEncodingT1: |
| 2666 | Rn = Bits32(opcode, 10, 8); |
| 2667 | imm32 = Bits32(opcode, 7, 0); |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 2668 | break; |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 2669 | case eEncodingT2: |
| 2670 | Rn = Bits32(opcode, 19, 16); |
| 2671 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 2672 | if (Rn == 15) |
| 2673 | return false; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 2674 | break; |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2675 | case eEncodingA1: |
| 2676 | Rn = Bits32(opcode, 19, 16); |
| 2677 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
Johnny Chen | d4dc444 | 2011-02-11 02:02:56 +0000 | [diff] [blame] | 2678 | break; |
| 2679 | default: |
| 2680 | return false; |
| 2681 | } |
| 2682 | // Read the register value from the operand register Rn. |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2683 | uint32_t reg_val = ReadCoreReg(Rn, &success); |
Johnny Chen | d4dc444 | 2011-02-11 02:02:56 +0000 | [diff] [blame] | 2684 | if (!success) |
| 2685 | return false; |
| 2686 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2687 | AddWithCarryResult res = AddWithCarry(reg_val, ~imm32, 1); |
| 2688 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2689 | EmulateInstruction::Context context; |
| 2690 | context.type = EmulateInstruction::eContextImmediate; |
| 2691 | context.SetNoArgs (); |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2692 | if (!WriteFlags(context, res.result, res.carry_out, res.overflow)) |
| 2693 | return false; |
| 2694 | |
Johnny Chen | d4dc444 | 2011-02-11 02:02:56 +0000 | [diff] [blame] | 2695 | return true; |
| 2696 | } |
| 2697 | |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2698 | // Compare (register) subtracts an optionally-shifted register value from a register value. |
| 2699 | // It updates the condition flags based on the result, and discards the result. |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2700 | bool |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2701 | EmulateInstructionARM::EmulateCMPReg (ARMEncoding encoding) |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2702 | { |
| 2703 | #if 0 |
| 2704 | // ARM pseudo code... |
| 2705 | if ConditionPassed() then |
| 2706 | EncodingSpecificOperations(); |
| 2707 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 2708 | (result, carry, overflow) = AddWithCarry(R[n], NOT(shifted), '1'); |
| 2709 | APSR.N = result<31>; |
| 2710 | APSR.Z = IsZeroBit(result); |
| 2711 | APSR.C = carry; |
| 2712 | APSR.V = overflow; |
| 2713 | #endif |
| 2714 | |
| 2715 | bool success = false; |
| 2716 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2717 | if (!success) |
| 2718 | return false; |
| 2719 | |
| 2720 | uint32_t Rn; // the first operand |
| 2721 | uint32_t Rm; // the second operand |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2722 | ARM_ShifterType shift_t; |
| 2723 | uint32_t shift_n; // the shift applied to the value read from Rm |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2724 | switch (encoding) { |
| 2725 | case eEncodingT1: |
| 2726 | Rn = Bits32(opcode, 2, 0); |
| 2727 | Rm = Bits32(opcode, 5, 3); |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2728 | shift_t = SRType_LSL; |
| 2729 | shift_n = 0; |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2730 | break; |
| 2731 | case eEncodingT2: |
| 2732 | Rn = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 0); |
| 2733 | Rm = Bits32(opcode, 6, 3); |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2734 | shift_t = SRType_LSL; |
| 2735 | shift_n = 0; |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2736 | if (Rn < 8 && Rm < 8) |
| 2737 | return false; |
| 2738 | if (Rn == 15 || Rm == 15) |
| 2739 | return false; |
| 2740 | break; |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2741 | case eEncodingA1: |
| 2742 | Rn = Bits32(opcode, 19, 16); |
| 2743 | Rm = Bits32(opcode, 3, 0); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 2744 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 2745 | break; |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2746 | default: |
| 2747 | return false; |
| 2748 | } |
| 2749 | // Read the register value from register Rn. |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2750 | uint32_t val1 = ReadCoreReg(Rn, &success); |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2751 | if (!success) |
| 2752 | return false; |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2753 | |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2754 | // Read the register value from register Rm. |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2755 | uint32_t val2 = ReadCoreReg(Rm, &success); |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2756 | if (!success) |
| 2757 | return false; |
| 2758 | |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2759 | uint32_t shifted = Shift(val2, shift_t, shift_n, APSR_C); |
| 2760 | AddWithCarryResult res = AddWithCarry(val1, ~shifted, 1); |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2761 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2762 | EmulateInstruction::Context context; |
| 2763 | context.type = EmulateInstruction::eContextImmediate; |
| 2764 | context.SetNoArgs(); |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2765 | if (!WriteFlags(context, res.result, res.carry_out, res.overflow)) |
| 2766 | return false; |
| 2767 | |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2768 | return true; |
| 2769 | } |
| 2770 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2771 | // Arithmetic Shift Right (immediate) shifts a register value right by an immediate number of bits, |
| 2772 | // shifting in copies of its sign bit, and writes the result to the destination register. It can |
| 2773 | // optionally update the condition flags based on the result. |
| 2774 | bool |
| 2775 | EmulateInstructionARM::EmulateASRImm (ARMEncoding encoding) |
| 2776 | { |
| 2777 | #if 0 |
| 2778 | // ARM pseudo code... |
| 2779 | if ConditionPassed() then |
| 2780 | EncodingSpecificOperations(); |
| 2781 | (result, carry) = Shift_C(R[m], SRType_ASR, shift_n, APSR.C); |
| 2782 | if d == 15 then // Can only occur for ARM encoding |
| 2783 | ALUWritePC(result); // setflags is always FALSE here |
| 2784 | else |
| 2785 | R[d] = result; |
| 2786 | if setflags then |
| 2787 | APSR.N = result<31>; |
| 2788 | APSR.Z = IsZeroBit(result); |
| 2789 | APSR.C = carry; |
| 2790 | // APSR.V unchanged |
| 2791 | #endif |
| 2792 | |
Johnny Chen | 41a0a15 | 2011-02-16 01:27:54 +0000 | [diff] [blame] | 2793 | return EmulateShiftImm(encoding, SRType_ASR); |
| 2794 | } |
| 2795 | |
| 2796 | // Arithmetic Shift Right (register) shifts a register value right by a variable number of bits, |
| 2797 | // shifting in copies of its sign bit, and writes the result to the destination register. |
| 2798 | // The variable number of bits is read from the bottom byte of a register. It can optionally update |
| 2799 | // the condition flags based on the result. |
| 2800 | bool |
| 2801 | EmulateInstructionARM::EmulateASRReg (ARMEncoding encoding) |
| 2802 | { |
| 2803 | #if 0 |
| 2804 | // ARM pseudo code... |
| 2805 | if ConditionPassed() then |
| 2806 | EncodingSpecificOperations(); |
| 2807 | shift_n = UInt(R[m]<7:0>); |
| 2808 | (result, carry) = Shift_C(R[m], SRType_ASR, shift_n, APSR.C); |
| 2809 | R[d] = result; |
| 2810 | if setflags then |
| 2811 | APSR.N = result<31>; |
| 2812 | APSR.Z = IsZeroBit(result); |
| 2813 | APSR.C = carry; |
| 2814 | // APSR.V unchanged |
| 2815 | #endif |
| 2816 | |
| 2817 | return EmulateShiftReg(encoding, SRType_ASR); |
| 2818 | } |
| 2819 | |
| 2820 | // Logical Shift Left (immediate) shifts a register value left by an immediate number of bits, |
| 2821 | // shifting in zeros, and writes the result to the destination register. It can optionally |
| 2822 | // update the condition flags based on the result. |
| 2823 | bool |
| 2824 | EmulateInstructionARM::EmulateLSLImm (ARMEncoding encoding) |
| 2825 | { |
| 2826 | #if 0 |
| 2827 | // ARM pseudo code... |
| 2828 | if ConditionPassed() then |
| 2829 | EncodingSpecificOperations(); |
| 2830 | (result, carry) = Shift_C(R[m], SRType_LSL, shift_n, APSR.C); |
| 2831 | if d == 15 then // Can only occur for ARM encoding |
| 2832 | ALUWritePC(result); // setflags is always FALSE here |
| 2833 | else |
| 2834 | R[d] = result; |
| 2835 | if setflags then |
| 2836 | APSR.N = result<31>; |
| 2837 | APSR.Z = IsZeroBit(result); |
| 2838 | APSR.C = carry; |
| 2839 | // APSR.V unchanged |
| 2840 | #endif |
| 2841 | |
| 2842 | return EmulateShiftImm(encoding, SRType_LSL); |
| 2843 | } |
| 2844 | |
| 2845 | // Logical Shift Left (register) shifts a register value left by a variable number of bits, |
| 2846 | // shifting in zeros, and writes the result to the destination register. The variable number |
| 2847 | // of bits is read from the bottom byte of a register. It can optionally update the condition |
| 2848 | // flags based on the result. |
| 2849 | bool |
| 2850 | EmulateInstructionARM::EmulateLSLReg (ARMEncoding encoding) |
| 2851 | { |
| 2852 | #if 0 |
| 2853 | // ARM pseudo code... |
| 2854 | if ConditionPassed() then |
| 2855 | EncodingSpecificOperations(); |
| 2856 | shift_n = UInt(R[m]<7:0>); |
| 2857 | (result, carry) = Shift_C(R[m], SRType_LSL, shift_n, APSR.C); |
| 2858 | R[d] = result; |
| 2859 | if setflags then |
| 2860 | APSR.N = result<31>; |
| 2861 | APSR.Z = IsZeroBit(result); |
| 2862 | APSR.C = carry; |
| 2863 | // APSR.V unchanged |
| 2864 | #endif |
| 2865 | |
| 2866 | return EmulateShiftReg(encoding, SRType_LSL); |
| 2867 | } |
| 2868 | |
| 2869 | // Logical Shift Right (immediate) shifts a register value right by an immediate number of bits, |
| 2870 | // shifting in zeros, and writes the result to the destination register. It can optionally |
| 2871 | // update the condition flags based on the result. |
| 2872 | bool |
| 2873 | EmulateInstructionARM::EmulateLSRImm (ARMEncoding encoding) |
| 2874 | { |
| 2875 | #if 0 |
| 2876 | // ARM pseudo code... |
| 2877 | if ConditionPassed() then |
| 2878 | EncodingSpecificOperations(); |
| 2879 | (result, carry) = Shift_C(R[m], SRType_LSR, shift_n, APSR.C); |
| 2880 | if d == 15 then // Can only occur for ARM encoding |
| 2881 | ALUWritePC(result); // setflags is always FALSE here |
| 2882 | else |
| 2883 | R[d] = result; |
| 2884 | if setflags then |
| 2885 | APSR.N = result<31>; |
| 2886 | APSR.Z = IsZeroBit(result); |
| 2887 | APSR.C = carry; |
| 2888 | // APSR.V unchanged |
| 2889 | #endif |
| 2890 | |
| 2891 | return EmulateShiftImm(encoding, SRType_LSR); |
| 2892 | } |
| 2893 | |
| 2894 | // Logical Shift Right (register) shifts a register value right by a variable number of bits, |
| 2895 | // shifting in zeros, and writes the result to the destination register. The variable number |
| 2896 | // of bits is read from the bottom byte of a register. It can optionally update the condition |
| 2897 | // flags based on the result. |
| 2898 | bool |
| 2899 | EmulateInstructionARM::EmulateLSRReg (ARMEncoding encoding) |
| 2900 | { |
| 2901 | #if 0 |
| 2902 | // ARM pseudo code... |
| 2903 | if ConditionPassed() then |
| 2904 | EncodingSpecificOperations(); |
| 2905 | shift_n = UInt(R[m]<7:0>); |
| 2906 | (result, carry) = Shift_C(R[m], SRType_LSR, shift_n, APSR.C); |
| 2907 | R[d] = result; |
| 2908 | if setflags then |
| 2909 | APSR.N = result<31>; |
| 2910 | APSR.Z = IsZeroBit(result); |
| 2911 | APSR.C = carry; |
| 2912 | // APSR.V unchanged |
| 2913 | #endif |
| 2914 | |
| 2915 | return EmulateShiftReg(encoding, SRType_LSR); |
| 2916 | } |
| 2917 | |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 2918 | // Rotate Right (immediate) provides the value of the contents of a register rotated by a constant value. |
| 2919 | // The bits that are rotated off the right end are inserted into the vacated bit positions on the left. |
| 2920 | // It can optionally update the condition flags based on the result. |
| 2921 | bool |
| 2922 | EmulateInstructionARM::EmulateRORImm (ARMEncoding encoding) |
| 2923 | { |
| 2924 | #if 0 |
| 2925 | // ARM pseudo code... |
| 2926 | if ConditionPassed() then |
| 2927 | EncodingSpecificOperations(); |
| 2928 | (result, carry) = Shift_C(R[m], SRType_ROR, shift_n, APSR.C); |
| 2929 | if d == 15 then // Can only occur for ARM encoding |
| 2930 | ALUWritePC(result); // setflags is always FALSE here |
| 2931 | else |
| 2932 | R[d] = result; |
| 2933 | if setflags then |
| 2934 | APSR.N = result<31>; |
| 2935 | APSR.Z = IsZeroBit(result); |
| 2936 | APSR.C = carry; |
| 2937 | // APSR.V unchanged |
| 2938 | #endif |
| 2939 | |
| 2940 | return EmulateShiftImm(encoding, SRType_ROR); |
| 2941 | } |
| 2942 | |
| 2943 | // Rotate Right (register) provides the value of the contents of a register rotated by a variable number of bits. |
| 2944 | // The bits that are rotated off the right end are inserted into the vacated bit positions on the left. |
| 2945 | // The variable number of bits is read from the bottom byte of a register. It can optionally update the condition |
| 2946 | // flags based on the result. |
| 2947 | bool |
| 2948 | EmulateInstructionARM::EmulateRORReg (ARMEncoding encoding) |
| 2949 | { |
| 2950 | #if 0 |
| 2951 | // ARM pseudo code... |
| 2952 | if ConditionPassed() then |
| 2953 | EncodingSpecificOperations(); |
| 2954 | shift_n = UInt(R[m]<7:0>); |
| 2955 | (result, carry) = Shift_C(R[m], SRType_ROR, shift_n, APSR.C); |
| 2956 | R[d] = result; |
| 2957 | if setflags then |
| 2958 | APSR.N = result<31>; |
| 2959 | APSR.Z = IsZeroBit(result); |
| 2960 | APSR.C = carry; |
| 2961 | // APSR.V unchanged |
| 2962 | #endif |
| 2963 | |
| 2964 | return EmulateShiftReg(encoding, SRType_ROR); |
| 2965 | } |
| 2966 | |
| 2967 | // Rotate Right with Extend provides the value of the contents of a register shifted right by one place, |
| 2968 | // with the carry flag shifted into bit [31]. |
| 2969 | // |
| 2970 | // RRX can optionally update the condition flags based on the result. |
| 2971 | // In that case, bit [0] is shifted into the carry flag. |
| 2972 | bool |
| 2973 | EmulateInstructionARM::EmulateRRX (ARMEncoding encoding) |
| 2974 | { |
| 2975 | #if 0 |
| 2976 | // ARM pseudo code... |
| 2977 | if ConditionPassed() then |
| 2978 | EncodingSpecificOperations(); |
| 2979 | (result, carry) = Shift_C(R[m], SRType_RRX, 1, APSR.C); |
| 2980 | if d == 15 then // Can only occur for ARM encoding |
| 2981 | ALUWritePC(result); // setflags is always FALSE here |
| 2982 | else |
| 2983 | R[d] = result; |
| 2984 | if setflags then |
| 2985 | APSR.N = result<31>; |
| 2986 | APSR.Z = IsZeroBit(result); |
| 2987 | APSR.C = carry; |
| 2988 | // APSR.V unchanged |
| 2989 | #endif |
| 2990 | |
| 2991 | return EmulateShiftImm(encoding, SRType_RRX); |
| 2992 | } |
| 2993 | |
Johnny Chen | 41a0a15 | 2011-02-16 01:27:54 +0000 | [diff] [blame] | 2994 | bool |
| 2995 | EmulateInstructionARM::EmulateShiftImm (ARMEncoding encoding, ARM_ShifterType shift_type) |
| 2996 | { |
| 2997 | assert(shift_type == SRType_ASR || shift_type == SRType_LSL || shift_type == SRType_LSR); |
| 2998 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2999 | bool success = false; |
| 3000 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3001 | if (!success) |
| 3002 | return false; |
| 3003 | |
| 3004 | if (ConditionPassed()) |
| 3005 | { |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 3006 | uint32_t Rd; // the destination register |
| 3007 | uint32_t Rm; // the first operand register |
| 3008 | uint32_t imm5; // encoding for the shift amount |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 3009 | uint32_t carry; // the carry bit after the shift operation |
| 3010 | bool setflags; |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 3011 | |
| 3012 | // Special case handling! |
| 3013 | // A8.6.139 ROR (immediate) -- Encoding T1 |
| 3014 | if (shift_type == SRType_ROR && encoding == eEncodingT1) |
| 3015 | { |
| 3016 | // Morph the T1 encoding from the ARM Architecture Manual into T2 encoding to |
| 3017 | // have the same decoding of bit fields as the other Thumb2 shift operations. |
| 3018 | encoding = eEncodingT2; |
| 3019 | } |
| 3020 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 3021 | switch (encoding) { |
| 3022 | case eEncodingT1: |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 3023 | // Due to the above special case handling! |
| 3024 | assert(shift_type != SRType_ROR); |
| 3025 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 3026 | Rd = Bits32(opcode, 2, 0); |
| 3027 | Rm = Bits32(opcode, 5, 3); |
| 3028 | setflags = !InITBlock(); |
| 3029 | imm5 = Bits32(opcode, 10, 6); |
| 3030 | break; |
| 3031 | case eEncodingT2: |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 3032 | // A8.6.141 RRX |
| 3033 | assert(shift_type != SRType_RRX); |
| 3034 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 3035 | Rd = Bits32(opcode, 11, 8); |
| 3036 | Rm = Bits32(opcode, 3, 0); |
| 3037 | setflags = BitIsSet(opcode, 20); |
| 3038 | imm5 = Bits32(opcode, 14, 12) << 2 | Bits32(opcode, 7, 6); |
| 3039 | if (BadReg(Rd) || BadReg(Rm)) |
| 3040 | return false; |
| 3041 | break; |
| 3042 | case eEncodingA1: |
| 3043 | Rd = Bits32(opcode, 15, 12); |
| 3044 | Rm = Bits32(opcode, 3, 0); |
| 3045 | setflags = BitIsSet(opcode, 20); |
| 3046 | imm5 = Bits32(opcode, 11, 7); |
| 3047 | break; |
| 3048 | default: |
| 3049 | return false; |
| 3050 | } |
| 3051 | |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 3052 | // A8.6.139 ROR (immediate) |
| 3053 | if (shift_type == SRType_ROR && imm5 == 0) |
| 3054 | shift_type = SRType_RRX; |
| 3055 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 3056 | // Get the first operand. |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 3057 | uint32_t value = ReadCoreReg (Rm, &success); |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 3058 | if (!success) |
| 3059 | return false; |
| 3060 | |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 3061 | // Decode the shift amount if not RRX. |
| 3062 | uint32_t amt = (shift_type == SRType_RRX ? 1 : DecodeImmShift(shift_type, imm5)); |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 3063 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 3064 | uint32_t result = Shift_C(value, shift_type, amt, APSR_C, carry); |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 3065 | |
| 3066 | // The context specifies that an immediate is to be moved into Rd. |
| 3067 | EmulateInstruction::Context context; |
| 3068 | context.type = EmulateInstruction::eContextImmediate; |
| 3069 | context.SetNoArgs (); |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 3070 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 3071 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 3072 | return false; |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 3073 | } |
| 3074 | return true; |
| 3075 | } |
| 3076 | |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 3077 | bool |
Johnny Chen | 41a0a15 | 2011-02-16 01:27:54 +0000 | [diff] [blame] | 3078 | EmulateInstructionARM::EmulateShiftReg (ARMEncoding encoding, ARM_ShifterType shift_type) |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 3079 | { |
Johnny Chen | 41a0a15 | 2011-02-16 01:27:54 +0000 | [diff] [blame] | 3080 | 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] | 3081 | |
| 3082 | bool success = false; |
| 3083 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3084 | if (!success) |
| 3085 | return false; |
| 3086 | |
| 3087 | if (ConditionPassed()) |
| 3088 | { |
| 3089 | uint32_t Rd; // the destination register |
| 3090 | uint32_t Rn; // the first operand register |
| 3091 | uint32_t Rm; // the register whose bottom byte contains the amount to shift by |
| 3092 | uint32_t carry; // the carry bit after the shift operation |
| 3093 | bool setflags; |
| 3094 | switch (encoding) { |
| 3095 | case eEncodingT1: |
| 3096 | Rd = Bits32(opcode, 2, 0); |
| 3097 | Rn = Rd; |
| 3098 | Rm = Bits32(opcode, 5, 3); |
| 3099 | setflags = !InITBlock(); |
| 3100 | break; |
| 3101 | case eEncodingT2: |
| 3102 | Rd = Bits32(opcode, 11, 8); |
| 3103 | Rn = Bits32(opcode, 19, 16); |
| 3104 | Rm = Bits32(opcode, 3, 0); |
| 3105 | setflags = BitIsSet(opcode, 20); |
| 3106 | if (BadReg(Rd) || BadReg(Rn) || BadReg(Rm)) |
| 3107 | return false; |
| 3108 | break; |
| 3109 | case eEncodingA1: |
| 3110 | Rd = Bits32(opcode, 15, 12); |
| 3111 | Rn = Bits32(opcode, 3, 0); |
| 3112 | Rm = Bits32(opcode, 11, 8); |
| 3113 | setflags = BitIsSet(opcode, 20); |
| 3114 | if (Rd == 15 || Rn == 15 || Rm == 15) |
| 3115 | return false; |
| 3116 | break; |
| 3117 | default: |
| 3118 | return false; |
| 3119 | } |
| 3120 | |
| 3121 | // Get the first operand. |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 3122 | uint32_t value = ReadCoreReg (Rn, &success); |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 3123 | if (!success) |
| 3124 | return false; |
| 3125 | // Get the Rm register content. |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 3126 | uint32_t val = ReadCoreReg (Rm, &success); |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 3127 | if (!success) |
| 3128 | return false; |
| 3129 | |
| 3130 | // Get the shift amount. |
| 3131 | uint32_t amt = Bits32(val, 7, 0); |
| 3132 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 3133 | uint32_t result = Shift_C(value, shift_type, amt, APSR_C, carry); |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 3134 | |
| 3135 | // The context specifies that an immediate is to be moved into Rd. |
| 3136 | EmulateInstruction::Context context; |
| 3137 | context.type = EmulateInstruction::eContextImmediate; |
| 3138 | context.SetNoArgs (); |
| 3139 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 3140 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 3141 | return false; |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 3142 | } |
| 3143 | return true; |
| 3144 | } |
| 3145 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3146 | // LDM loads multiple registers from consecutive memory locations, using an |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3147 | // 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] | 3148 | // can be written back to the base register. |
| 3149 | bool |
| 3150 | EmulateInstructionARM::EmulateLDM (ARMEncoding encoding) |
| 3151 | { |
| 3152 | #if 0 |
| 3153 | // ARM pseudo code... |
| 3154 | if ConditionPassed() |
| 3155 | EncodingSpecificOperations(); NullCheckIfThumbEE (n); |
| 3156 | address = R[n]; |
| 3157 | |
| 3158 | for i = 0 to 14 |
| 3159 | if registers<i> == '1' then |
| 3160 | R[i] = MemA[address, 4]; address = address + 4; |
| 3161 | if registers<15> == '1' then |
| 3162 | LoadWritePC (MemA[address, 4]); |
| 3163 | |
| 3164 | if wback && registers<n> == '0' then R[n] = R[n] + 4 * BitCount (registers); |
| 3165 | if wback && registers<n> == '1' then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 3166 | |
| 3167 | #endif |
| 3168 | |
| 3169 | bool success = false; |
| 3170 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3171 | if (!success) |
| 3172 | return false; |
| 3173 | |
| 3174 | if (ConditionPassed()) |
| 3175 | { |
| 3176 | uint32_t n; |
| 3177 | uint32_t registers = 0; |
| 3178 | bool wback; |
| 3179 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3180 | switch (encoding) |
| 3181 | { |
| 3182 | case eEncodingT1: |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3183 | // n = UInt(Rn); registers = ’00000000’:register_list; wback = (registers<n> == ’0’); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3184 | n = Bits32 (opcode, 10, 8); |
| 3185 | registers = Bits32 (opcode, 7, 0); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3186 | registers = registers & 0x00ff; // Make sure the top 8 bits are zeros. |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3187 | wback = BitIsClear (registers, n); |
| 3188 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 3189 | if (BitCount(registers) < 1) |
| 3190 | return false; |
| 3191 | break; |
| 3192 | case eEncodingT2: |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3193 | // if W == ’1’ && Rn == ’1101’ then SEE POP; |
| 3194 | // n = UInt(Rn); registers = P:M:’0’:register_list; wback = (W == ’1’); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3195 | n = Bits32 (opcode, 19, 16); |
| 3196 | registers = Bits32 (opcode, 15, 0); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3197 | registers = registers & 0xdfff; // Make sure bit 13 is zero. |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3198 | wback = BitIsSet (opcode, 21); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3199 | |
| 3200 | // if n == 15 || BitCount(registers) < 2 || (P == ’1’ && M == ’1’) then UNPREDICTABLE; |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3201 | if ((n == 15) |
| 3202 | || (BitCount (registers) < 2) |
| 3203 | || (BitIsSet (opcode, 14) && BitIsSet (opcode, 15))) |
| 3204 | return false; |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3205 | |
| 3206 | // if registers<15> == ’1’ && InITBlock() && !LastInITBlock() then UNPREDICTABLE; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 3207 | if (BitIsSet (registers, 15) && InITBlock() && !LastInITBlock()) |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3208 | return false; |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3209 | |
| 3210 | // if wback && registers<n> == ’1’ then UNPREDICTABLE; |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3211 | if (wback |
| 3212 | && BitIsSet (registers, n)) |
| 3213 | return false; |
| 3214 | break; |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3215 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3216 | case eEncodingA1: |
| 3217 | n = Bits32 (opcode, 19, 16); |
| 3218 | registers = Bits32 (opcode, 15, 0); |
| 3219 | wback = BitIsSet (opcode, 21); |
| 3220 | if ((n == 15) |
| 3221 | || (BitCount (registers) < 1)) |
| 3222 | return false; |
| 3223 | break; |
| 3224 | default: |
| 3225 | return false; |
| 3226 | } |
| 3227 | |
| 3228 | int32_t offset = 0; |
| 3229 | const addr_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3230 | if (!success) |
| 3231 | return false; |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3232 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3233 | EmulateInstruction::Context context; |
| 3234 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 3235 | Register dwarf_reg; |
| 3236 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3237 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3238 | |
| 3239 | for (int i = 0; i < 14; ++i) |
| 3240 | { |
| 3241 | if (BitIsSet (registers, i)) |
| 3242 | { |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3243 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3244 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3245 | if (wback && (n == 13)) // Pop Instruction |
| 3246 | context.type = EmulateInstruction::eContextPopRegisterOffStack; |
| 3247 | |
| 3248 | // R[i] = MemA [address, 4]; address = address + 4; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3249 | 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] | 3250 | if (!success) |
| 3251 | return false; |
| 3252 | |
| 3253 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 3254 | return false; |
| 3255 | |
| 3256 | offset += addr_byte_size; |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | if (BitIsSet (registers, 15)) |
| 3261 | { |
| 3262 | //LoadWritePC (MemA [address, 4]); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3263 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3264 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3265 | 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] | 3266 | if (!success) |
| 3267 | return false; |
Johnny Chen | e62b50d | 2011-02-09 22:02:17 +0000 | [diff] [blame] | 3268 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 3269 | if (!LoadWritePC(context, data)) |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3270 | return false; |
| 3271 | } |
| 3272 | |
| 3273 | if (wback && BitIsClear (registers, n)) |
| 3274 | { |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3275 | // R[n] = R[n] + 4 * BitCount (registers) |
| 3276 | int32_t offset = addr_byte_size * BitCount (registers); |
| 3277 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3278 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3279 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3280 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, base_address + offset)) |
| 3281 | return false; |
| 3282 | } |
| 3283 | if (wback && BitIsSet (registers, n)) |
| 3284 | // R[n] bits(32) UNKNOWN; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3285 | return WriteBits32Unknown (n); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3286 | } |
| 3287 | return true; |
| 3288 | } |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3289 | |
| 3290 | // LDMDA loads multiple registers from consecutive memory locations using an address from a base registers. |
| 3291 | // The consecutive memorty locations end at this address and the address just below the lowest of those locations |
| 3292 | // can optionally be written back tot he base registers. |
| 3293 | bool |
| 3294 | EmulateInstructionARM::EmulateLDMDA (ARMEncoding encoding) |
| 3295 | { |
| 3296 | #if 0 |
| 3297 | // ARM pseudo code... |
| 3298 | if ConditionPassed() then |
| 3299 | EncodingSpecificOperations(); |
| 3300 | address = R[n] - 4*BitCount(registers) + 4; |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 3301 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3302 | for i = 0 to 14 |
| 3303 | if registers<i> == ’1’ then |
| 3304 | R[i] = MemA[address,4]; address = address + 4; |
| 3305 | |
| 3306 | if registers<15> == ’1’ then |
| 3307 | LoadWritePC(MemA[address,4]); |
| 3308 | |
| 3309 | if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 3310 | if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; |
| 3311 | #endif |
| 3312 | |
| 3313 | bool success = false; |
| 3314 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3315 | if (!success) |
| 3316 | return false; |
| 3317 | |
| 3318 | if (ConditionPassed()) |
| 3319 | { |
| 3320 | uint32_t n; |
| 3321 | uint32_t registers = 0; |
| 3322 | bool wback; |
| 3323 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3324 | |
| 3325 | // EncodingSpecificOperations(); |
| 3326 | switch (encoding) |
| 3327 | { |
| 3328 | case eEncodingA1: |
| 3329 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3330 | n = Bits32 (opcode, 19, 16); |
| 3331 | registers = Bits32 (opcode, 15, 0); |
| 3332 | wback = BitIsSet (opcode, 21); |
| 3333 | |
| 3334 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3335 | if ((n == 15) || (BitCount (registers) < 1)) |
| 3336 | return false; |
| 3337 | |
| 3338 | break; |
| 3339 | |
| 3340 | default: |
| 3341 | return false; |
| 3342 | } |
| 3343 | // address = R[n] - 4*BitCount(registers) + 4; |
| 3344 | |
| 3345 | int32_t offset = 0; |
| 3346 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3347 | |
| 3348 | if (!success) |
| 3349 | return false; |
| 3350 | |
| 3351 | address = address - (addr_byte_size * BitCount (registers)) + addr_byte_size; |
| 3352 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3353 | EmulateInstruction::Context context; |
| 3354 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 3355 | Register dwarf_reg; |
| 3356 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3357 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3358 | |
| 3359 | // for i = 0 to 14 |
| 3360 | for (int i = 0; i < 14; ++i) |
| 3361 | { |
| 3362 | // if registers<i> == ’1’ then |
| 3363 | if (BitIsSet (registers, i)) |
| 3364 | { |
| 3365 | // R[i] = MemA[address,4]; address = address + 4; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3366 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3367 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3368 | if (!success) |
| 3369 | return false; |
| 3370 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 3371 | return false; |
| 3372 | offset += addr_byte_size; |
| 3373 | } |
| 3374 | } |
| 3375 | |
| 3376 | // if registers<15> == ’1’ then |
| 3377 | // LoadWritePC(MemA[address,4]); |
| 3378 | if (BitIsSet (registers, 15)) |
| 3379 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3380 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3381 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3382 | if (!success) |
| 3383 | return false; |
Johnny Chen | 44c10f0 | 2011-02-11 19:37:03 +0000 | [diff] [blame] | 3384 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 3385 | if (!LoadWritePC(context, data)) |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3386 | return false; |
| 3387 | } |
| 3388 | |
| 3389 | // if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 3390 | if (wback && BitIsClear (registers, n)) |
| 3391 | { |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3392 | addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3393 | if (!success) |
| 3394 | return false; |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3395 | |
| 3396 | offset = (addr_byte_size * BitCount (registers)) * -1; |
| 3397 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3398 | context.SetImmediateSigned (offset); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3399 | addr = addr + offset; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3400 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr)) |
| 3401 | return false; |
| 3402 | } |
| 3403 | |
| 3404 | // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; |
| 3405 | if (wback && BitIsSet (registers, n)) |
| 3406 | return WriteBits32Unknown (n); |
| 3407 | } |
| 3408 | return true; |
| 3409 | } |
| 3410 | |
| 3411 | // LDMDB loads multiple registers from consecutive memory locations using an address from a base register. The |
| 3412 | // consecutive memory lcoations end just below this address, and the address of the lowest of those locations can |
| 3413 | // be optionally written back to the base register. |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3414 | bool |
| 3415 | EmulateInstructionARM::EmulateLDMDB (ARMEncoding encoding) |
| 3416 | { |
| 3417 | #if 0 |
| 3418 | // ARM pseudo code... |
| 3419 | if ConditionPassed() then |
| 3420 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 3421 | address = R[n] - 4*BitCount(registers); |
| 3422 | |
| 3423 | for i = 0 to 14 |
| 3424 | if registers<i> == ’1’ then |
| 3425 | R[i] = MemA[address,4]; address = address + 4; |
| 3426 | if registers<15> == ’1’ then |
| 3427 | LoadWritePC(MemA[address,4]); |
| 3428 | |
| 3429 | if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 3430 | if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 3431 | #endif |
| 3432 | |
| 3433 | bool success = false; |
| 3434 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3435 | if (!success) |
| 3436 | return false; |
| 3437 | |
| 3438 | if (ConditionPassed()) |
| 3439 | { |
| 3440 | uint32_t n; |
| 3441 | uint32_t registers = 0; |
| 3442 | bool wback; |
| 3443 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3444 | switch (encoding) |
| 3445 | { |
| 3446 | case eEncodingT1: |
| 3447 | // n = UInt(Rn); registers = P:M:’0’:register_list; wback = (W == ’1’); |
| 3448 | n = Bits32 (opcode, 19, 16); |
| 3449 | registers = Bits32 (opcode, 15, 0); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3450 | registers = registers & 0xdfff; // Make sure bit 13 is a zero. |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3451 | wback = BitIsSet (opcode, 21); |
| 3452 | |
| 3453 | // if n == 15 || BitCount(registers) < 2 || (P == ’1’ && M == ’1’) then UNPREDICTABLE; |
| 3454 | if ((n == 15) |
| 3455 | || (BitCount (registers) < 2) |
| 3456 | || (BitIsSet (opcode, 14) && BitIsSet (opcode, 15))) |
| 3457 | return false; |
| 3458 | |
| 3459 | // if registers<15> == ’1’ && InITBlock() && !LastInITBlock() then UNPREDICTABLE; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 3460 | if (BitIsSet (registers, 15) && InITBlock() && !LastInITBlock()) |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3461 | return false; |
| 3462 | |
| 3463 | // if wback && registers<n> == ’1’ then UNPREDICTABLE; |
| 3464 | if (wback && BitIsSet (registers, n)) |
| 3465 | return false; |
| 3466 | |
| 3467 | break; |
| 3468 | |
| 3469 | case eEncodingA1: |
| 3470 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3471 | n = Bits32 (opcode, 19, 16); |
| 3472 | registers = Bits32 (opcode, 15, 0); |
| 3473 | wback = BitIsSet (opcode, 21); |
| 3474 | |
| 3475 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3476 | if ((n == 15) || (BitCount (registers) < 1)) |
| 3477 | return false; |
| 3478 | |
| 3479 | break; |
| 3480 | |
| 3481 | default: |
| 3482 | return false; |
| 3483 | } |
| 3484 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3485 | // address = R[n] - 4*BitCount(registers); |
| 3486 | |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3487 | int32_t offset = 0; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3488 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3489 | |
| 3490 | if (!success) |
| 3491 | return false; |
| 3492 | |
| 3493 | address = address - (addr_byte_size * BitCount (registers)); |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3494 | EmulateInstruction::Context context; |
| 3495 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 3496 | Register dwarf_reg; |
| 3497 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3498 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3499 | |
| 3500 | for (int i = 0; i < 14; ++i) |
| 3501 | { |
| 3502 | if (BitIsSet (registers, i)) |
| 3503 | { |
| 3504 | // R[i] = MemA[address,4]; address = address + 4; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3505 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3506 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3507 | if (!success) |
| 3508 | return false; |
| 3509 | |
| 3510 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 3511 | return false; |
| 3512 | |
| 3513 | offset += addr_byte_size; |
| 3514 | } |
| 3515 | } |
| 3516 | |
| 3517 | // if registers<15> == ’1’ then |
| 3518 | // LoadWritePC(MemA[address,4]); |
| 3519 | if (BitIsSet (registers, 15)) |
| 3520 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3521 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3522 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3523 | if (!success) |
| 3524 | return false; |
Johnny Chen | e62b50d | 2011-02-09 22:02:17 +0000 | [diff] [blame] | 3525 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 3526 | if (!LoadWritePC(context, data)) |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3527 | return false; |
| 3528 | } |
| 3529 | |
| 3530 | // if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 3531 | if (wback && BitIsClear (registers, n)) |
| 3532 | { |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3533 | addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3534 | if (!success) |
| 3535 | return false; |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3536 | |
| 3537 | offset = (addr_byte_size * BitCount (registers)) * -1; |
| 3538 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3539 | context.SetImmediateSigned (offset); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3540 | addr = addr + offset; |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3541 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr)) |
| 3542 | return false; |
| 3543 | } |
| 3544 | |
| 3545 | // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 3546 | if (wback && BitIsSet (registers, n)) |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3547 | return WriteBits32Unknown (n); |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3548 | } |
| 3549 | return true; |
| 3550 | } |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3551 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3552 | // LDMIB loads multiple registers from consecutive memory locations using an address from a base register. The |
| 3553 | // consecutive memory locations start just above this address, and thea ddress of the last of those locations can |
| 3554 | // optinoally be written back to the base register. |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3555 | bool |
| 3556 | EmulateInstructionARM::EmulateLDMIB (ARMEncoding encoding) |
| 3557 | { |
| 3558 | #if 0 |
| 3559 | if ConditionPassed() then |
| 3560 | EncodingSpecificOperations(); |
| 3561 | address = R[n] + 4; |
| 3562 | |
| 3563 | for i = 0 to 14 |
| 3564 | if registers<i> == ’1’ then |
| 3565 | R[i] = MemA[address,4]; address = address + 4; |
| 3566 | if registers<15> == ’1’ then |
| 3567 | LoadWritePC(MemA[address,4]); |
| 3568 | |
| 3569 | if wback && registers<n> == ’0’ then R[n] = R[n] + 4*BitCount(registers); |
| 3570 | if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; |
| 3571 | #endif |
| 3572 | |
| 3573 | bool success = false; |
| 3574 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3575 | if (!success) |
| 3576 | return false; |
| 3577 | |
| 3578 | if (ConditionPassed()) |
| 3579 | { |
| 3580 | uint32_t n; |
| 3581 | uint32_t registers = 0; |
| 3582 | bool wback; |
| 3583 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3584 | switch (encoding) |
| 3585 | { |
| 3586 | case eEncodingA1: |
| 3587 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3588 | n = Bits32 (opcode, 19, 16); |
| 3589 | registers = Bits32 (opcode, 15, 0); |
| 3590 | wback = BitIsSet (opcode, 21); |
| 3591 | |
| 3592 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3593 | if ((n == 15) || (BitCount (registers) < 1)) |
| 3594 | return false; |
| 3595 | |
| 3596 | break; |
| 3597 | default: |
| 3598 | return false; |
| 3599 | } |
| 3600 | // address = R[n] + 4; |
| 3601 | |
| 3602 | int32_t offset = 0; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3603 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3604 | |
| 3605 | if (!success) |
| 3606 | return false; |
| 3607 | |
| 3608 | address = address + addr_byte_size; |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3609 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3610 | EmulateInstruction::Context context; |
| 3611 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 3612 | Register dwarf_reg; |
| 3613 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3614 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3615 | |
| 3616 | for (int i = 0; i < 14; ++i) |
| 3617 | { |
| 3618 | if (BitIsSet (registers, i)) |
| 3619 | { |
| 3620 | // R[i] = MemA[address,4]; address = address + 4; |
| 3621 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3622 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3623 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3624 | if (!success) |
| 3625 | return false; |
| 3626 | |
| 3627 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 3628 | return false; |
| 3629 | |
| 3630 | offset += addr_byte_size; |
| 3631 | } |
| 3632 | } |
| 3633 | |
| 3634 | // if registers<15> == ’1’ then |
| 3635 | // LoadWritePC(MemA[address,4]); |
| 3636 | if (BitIsSet (registers, 15)) |
| 3637 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3638 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3639 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3640 | if (!success) |
| 3641 | return false; |
Johnny Chen | e62b50d | 2011-02-09 22:02:17 +0000 | [diff] [blame] | 3642 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 3643 | if (!LoadWritePC(context, data)) |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3644 | return false; |
| 3645 | } |
| 3646 | |
| 3647 | // if wback && registers<n> == ’0’ then R[n] = R[n] + 4*BitCount(registers); |
| 3648 | if (wback && BitIsClear (registers, n)) |
| 3649 | { |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3650 | addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3651 | if (!success) |
| 3652 | return false; |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3653 | |
| 3654 | offset = addr_byte_size * BitCount (registers); |
| 3655 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3656 | context.SetImmediateSigned (offset); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3657 | addr = addr + offset; |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3658 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr)) |
| 3659 | return false; |
| 3660 | } |
| 3661 | |
| 3662 | // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 3663 | if (wback && BitIsSet (registers, n)) |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3664 | return WriteBits32Unknown (n); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3665 | } |
| 3666 | return true; |
| 3667 | } |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3668 | |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3669 | // Load Register (immediate) calculates an address from a base register value and |
| 3670 | // an immediate offset, loads a word from memory, and writes to a register. |
| 3671 | // LDR (immediate, Thumb) |
| 3672 | bool |
| 3673 | EmulateInstructionARM::EmulateLDRRtRnImm (ARMEncoding encoding) |
| 3674 | { |
| 3675 | #if 0 |
| 3676 | // ARM pseudo code... |
| 3677 | if (ConditionPassed()) |
| 3678 | { |
| 3679 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 3680 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 3681 | address = if index then offset_addr else R[n]; |
| 3682 | data = MemU[address,4]; |
| 3683 | if wback then R[n] = offset_addr; |
| 3684 | if t == 15 then |
| 3685 | if address<1:0> == '00' then LoadWritePC(data); else UNPREDICTABLE; |
| 3686 | elsif UnalignedSupport() || address<1:0> = '00' then |
| 3687 | R[t] = data; |
| 3688 | else R[t] = bits(32) UNKNOWN; // Can only apply before ARMv7 |
| 3689 | } |
| 3690 | #endif |
| 3691 | |
| 3692 | bool success = false; |
| 3693 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3694 | if (!success) |
| 3695 | return false; |
| 3696 | |
| 3697 | if (ConditionPassed()) |
| 3698 | { |
| 3699 | uint32_t Rt; // the destination register |
| 3700 | uint32_t Rn; // the base register |
| 3701 | uint32_t imm32; // the immediate offset used to form the address |
| 3702 | addr_t offset_addr; // the offset address |
| 3703 | addr_t address; // the calculated address |
| 3704 | uint32_t data; // the literal data value from memory load |
| 3705 | bool add, index, wback; |
| 3706 | switch (encoding) { |
| 3707 | case eEncodingT1: |
| 3708 | Rt = Bits32(opcode, 5, 3); |
| 3709 | Rn = Bits32(opcode, 2, 0); |
| 3710 | imm32 = Bits32(opcode, 10, 6) << 2; // imm32 = ZeroExtend(imm5:'00', 32); |
| 3711 | // index = TRUE; add = TRUE; wback = FALSE |
| 3712 | add = true; |
| 3713 | index = true; |
| 3714 | wback = false; |
| 3715 | break; |
| 3716 | default: |
| 3717 | return false; |
| 3718 | } |
| 3719 | uint32_t base = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success); |
| 3720 | if (!success) |
| 3721 | return false; |
| 3722 | if (add) |
| 3723 | offset_addr = base + imm32; |
| 3724 | else |
| 3725 | offset_addr = base - imm32; |
| 3726 | |
| 3727 | address = (index ? offset_addr : base); |
| 3728 | |
| 3729 | if (wback) |
| 3730 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3731 | EmulateInstruction::Context ctx; |
| 3732 | ctx.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 3733 | Register dwarf_reg; |
| 3734 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rn); |
| 3735 | ctx.SetRegisterPlusOffset (dwarf_reg, (int32_t) (offset_addr - base)); |
| 3736 | |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3737 | if (!WriteRegisterUnsigned (ctx, eRegisterKindDWARF, dwarf_r0 + Rn, offset_addr)) |
| 3738 | return false; |
| 3739 | } |
| 3740 | |
| 3741 | // Prepare to write to the Rt register. |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3742 | EmulateInstruction::Context context; |
| 3743 | context.type = EmulateInstruction::eContextImmediate; |
| 3744 | context.SetNoArgs (); |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3745 | |
| 3746 | // Read memory from the address. |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3747 | data = MemURead(context, address, 4, 0, &success); |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3748 | if (!success) |
| 3749 | return false; |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3750 | |
| 3751 | if (Rt == 15) |
| 3752 | { |
| 3753 | if (Bits32(address, 1, 0) == 0) |
| 3754 | { |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 3755 | if (!LoadWritePC(context, data)) |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3756 | return false; |
| 3757 | } |
| 3758 | else |
| 3759 | return false; |
| 3760 | } |
| 3761 | else if (UnalignedSupport() || Bits32(address, 1, 0) == 0) |
| 3762 | { |
| 3763 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data)) |
| 3764 | return false; |
| 3765 | } |
| 3766 | else |
| 3767 | return false; |
| 3768 | } |
| 3769 | return true; |
| 3770 | } |
| 3771 | |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3772 | // STM (Store Multiple Increment After) stores multiple registers to consecutive memory locations using an address |
| 3773 | // from a base register. The consecutive memory locations start at this address, and teh address just above the last |
| 3774 | // of those locations can optionally be written back to the base register. |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3775 | bool |
| 3776 | EmulateInstructionARM::EmulateSTM (ARMEncoding encoding) |
| 3777 | { |
| 3778 | #if 0 |
| 3779 | if ConditionPassed() then |
| 3780 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 3781 | address = R[n]; |
| 3782 | |
| 3783 | for i = 0 to 14 |
| 3784 | if registers<i> == ’1’ then |
| 3785 | if i == n && wback && i != LowestSetBit(registers) then |
| 3786 | MemA[address,4] = bits(32) UNKNOWN; // Only possible for encodings T1 and A1 |
| 3787 | else |
| 3788 | MemA[address,4] = R[i]; |
| 3789 | address = address + 4; |
| 3790 | |
| 3791 | if registers<15> == ’1’ then // Only possible for encoding A1 |
| 3792 | MemA[address,4] = PCStoreValue(); |
| 3793 | if wback then R[n] = R[n] + 4*BitCount(registers); |
| 3794 | #endif |
| 3795 | |
| 3796 | bool success = false; |
| 3797 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3798 | if (!success) |
| 3799 | return false; |
| 3800 | |
| 3801 | if (ConditionPassed ()) |
| 3802 | { |
| 3803 | uint32_t n; |
| 3804 | uint32_t registers = 0; |
| 3805 | bool wback; |
| 3806 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3807 | |
| 3808 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 3809 | switch (encoding) |
| 3810 | { |
| 3811 | case eEncodingT1: |
| 3812 | // n = UInt(Rn); registers = ’00000000’:register_list; wback = TRUE; |
| 3813 | n = Bits32 (opcode, 10, 8); |
| 3814 | registers = Bits32 (opcode, 7, 0); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3815 | registers = registers & 0x00ff; // Make sure the top 8 bits are zeros. |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3816 | wback = true; |
| 3817 | |
| 3818 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 3819 | if (BitCount (registers) < 1) |
| 3820 | return false; |
| 3821 | |
| 3822 | break; |
| 3823 | |
| 3824 | case eEncodingT2: |
| 3825 | // n = UInt(Rn); registers = ’0’:M:’0’:register_list; wback = (W == ’1’); |
| 3826 | n = Bits32 (opcode, 19, 16); |
| 3827 | registers = Bits32 (opcode, 15, 0); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3828 | registers = registers & 0x5fff; // Make sure bits 15 & 13 are zeros. |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3829 | wback = BitIsSet (opcode, 21); |
| 3830 | |
| 3831 | // if n == 15 || BitCount(registers) < 2 then UNPREDICTABLE; |
| 3832 | if ((n == 15) || (BitCount (registers) < 2)) |
| 3833 | return false; |
| 3834 | |
| 3835 | // if wback && registers<n> == ’1’ then UNPREDICTABLE; |
| 3836 | if (wback && BitIsSet (registers, n)) |
| 3837 | return false; |
| 3838 | |
| 3839 | break; |
| 3840 | |
| 3841 | case eEncodingA1: |
| 3842 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3843 | n = Bits32 (opcode, 19, 16); |
| 3844 | registers = Bits32 (opcode, 15, 0); |
| 3845 | wback = BitIsSet (opcode, 21); |
| 3846 | |
| 3847 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3848 | if ((n == 15) || (BitCount (registers) < 1)) |
| 3849 | return false; |
| 3850 | |
| 3851 | break; |
| 3852 | |
| 3853 | default: |
| 3854 | return false; |
| 3855 | } |
| 3856 | |
| 3857 | // address = R[n]; |
| 3858 | int32_t offset = 0; |
| 3859 | const addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3860 | if (!success) |
| 3861 | return false; |
| 3862 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3863 | EmulateInstruction::Context context; |
| 3864 | context.type = EmulateInstruction::eContextRegisterStore; |
| 3865 | Register base_reg; |
| 3866 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3867 | |
| 3868 | // for i = 0 to 14 |
| 3869 | for (int i = 0; i < 14; ++i) |
| 3870 | { |
| 3871 | int lowest_set_bit = 14; |
| 3872 | // if registers<i> == ’1’ then |
| 3873 | if (BitIsSet (registers, i)) |
| 3874 | { |
| 3875 | if (i < lowest_set_bit) |
| 3876 | lowest_set_bit = i; |
| 3877 | // if i == n && wback && i != LowestSetBit(registers) then |
| 3878 | if ((i == n) && wback && (i != lowest_set_bit)) |
| 3879 | // MemA[address,4] = bits(32) UNKNOWN; // Only possible for encodings T1 and A1 |
| 3880 | WriteBits32UnknownToMemory (address + offset); |
| 3881 | else |
| 3882 | { |
| 3883 | // MemA[address,4] = R[i]; |
| 3884 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success); |
| 3885 | if (!success) |
| 3886 | return false; |
| 3887 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3888 | Register data_reg; |
| 3889 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i); |
| 3890 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3891 | if (!MemAWrite (context, address + offset, data, addr_byte_size)) |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3892 | return false; |
| 3893 | } |
| 3894 | |
| 3895 | // address = address + 4; |
| 3896 | offset += addr_byte_size; |
| 3897 | } |
| 3898 | } |
| 3899 | |
| 3900 | // if registers<15> == ’1’ then // Only possible for encoding A1 |
| 3901 | // MemA[address,4] = PCStoreValue(); |
| 3902 | if (BitIsSet (registers, 15)) |
| 3903 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3904 | Register pc_reg; |
| 3905 | pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc); |
| 3906 | context.SetRegisterPlusOffset (pc_reg, 8); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3907 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 3908 | if (!success) |
| 3909 | return false; |
| 3910 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3911 | if (!MemAWrite (context, address + offset, pc + 8, addr_byte_size)) |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3912 | return false; |
| 3913 | } |
| 3914 | |
| 3915 | // if wback then R[n] = R[n] + 4*BitCount(registers); |
| 3916 | if (wback) |
| 3917 | { |
| 3918 | offset = addr_byte_size * BitCount (registers); |
| 3919 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3920 | context.SetImmediateSigned (offset); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3921 | addr_t data = address + offset; |
| 3922 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 3923 | return false; |
| 3924 | } |
| 3925 | } |
| 3926 | return true; |
| 3927 | } |
| 3928 | |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3929 | // STMDA (Store Multiple Decrement After) stores multiple registers to consecutive memory locations using an address |
| 3930 | // from a base register. The consecutive memory locations end at this address, and the address just below the lowest |
| 3931 | // of those locations can optionally be written back to the base register. |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 3932 | bool |
| 3933 | EmulateInstructionARM::EmulateSTMDA (ARMEncoding encoding) |
| 3934 | { |
| 3935 | #if 0 |
| 3936 | if ConditionPassed() then |
| 3937 | EncodingSpecificOperations(); |
| 3938 | address = R[n] - 4*BitCount(registers) + 4; |
| 3939 | |
| 3940 | for i = 0 to 14 |
| 3941 | if registers<i> == ’1’ then |
| 3942 | if i == n && wback && i != LowestSetBit(registers) then |
| 3943 | MemA[address,4] = bits(32) UNKNOWN; |
| 3944 | else |
| 3945 | MemA[address,4] = R[i]; |
| 3946 | address = address + 4; |
| 3947 | |
| 3948 | if registers<15> == ’1’ then |
| 3949 | MemA[address,4] = PCStoreValue(); |
| 3950 | |
| 3951 | if wback then R[n] = R[n] - 4*BitCount(registers); |
| 3952 | #endif |
| 3953 | |
| 3954 | bool success = false; |
| 3955 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3956 | if (!success) |
| 3957 | return false; |
| 3958 | |
| 3959 | if (ConditionPassed ()) |
| 3960 | { |
| 3961 | uint32_t n; |
| 3962 | uint32_t registers = 0; |
| 3963 | bool wback; |
| 3964 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3965 | |
| 3966 | // EncodingSpecificOperations(); |
| 3967 | switch (encoding) |
| 3968 | { |
| 3969 | case eEncodingA1: |
| 3970 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3971 | n = Bits32 (opcode, 19, 16); |
| 3972 | registers = Bits32 (opcode, 15, 0); |
| 3973 | wback = BitIsSet (opcode, 21); |
| 3974 | |
| 3975 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3976 | if ((n == 15) || (BitCount (registers) < 1)) |
| 3977 | return false; |
| 3978 | break; |
| 3979 | default: |
| 3980 | return false; |
| 3981 | } |
| 3982 | |
| 3983 | // address = R[n] - 4*BitCount(registers) + 4; |
| 3984 | int32_t offset = 0; |
| 3985 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3986 | if (!success) |
| 3987 | return false; |
| 3988 | |
| 3989 | address = address - (addr_byte_size * BitCount (registers)) + 4; |
| 3990 | |
| 3991 | EmulateInstruction::Context context; |
| 3992 | context.type = EmulateInstruction::eContextRegisterStore; |
| 3993 | Register base_reg; |
| 3994 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3995 | |
| 3996 | // for i = 0 to 14 |
| 3997 | for (int i = 0; i < 14; ++i) |
| 3998 | { |
| 3999 | int lowest_bit_set = 14; |
| 4000 | // if registers<i> == ’1’ then |
| 4001 | if (BitIsSet (registers, i)) |
| 4002 | { |
| 4003 | if (i < lowest_bit_set) |
| 4004 | lowest_bit_set = i; |
| 4005 | //if i == n && wback && i != LowestSetBit(registers) then |
| 4006 | if ((i == n) && wback && (i != lowest_bit_set)) |
| 4007 | // MemA[address,4] = bits(32) UNKNOWN; |
| 4008 | WriteBits32UnknownToMemory (address + offset); |
| 4009 | else |
| 4010 | { |
| 4011 | // MemA[address,4] = R[i]; |
| 4012 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success); |
| 4013 | if (!success) |
| 4014 | return false; |
| 4015 | |
| 4016 | Register data_reg; |
| 4017 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i); |
| 4018 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4019 | if (!MemAWrite (context, address + offset, data, addr_byte_size)) |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 4020 | return false; |
| 4021 | } |
| 4022 | |
| 4023 | // address = address + 4; |
| 4024 | offset += addr_byte_size; |
| 4025 | } |
| 4026 | } |
| 4027 | |
| 4028 | // if registers<15> == ’1’ then |
| 4029 | // MemA[address,4] = PCStoreValue(); |
| 4030 | if (BitIsSet (registers, 15)) |
| 4031 | { |
| 4032 | Register pc_reg; |
| 4033 | pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc); |
| 4034 | context.SetRegisterPlusOffset (pc_reg, 8); |
| 4035 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 4036 | if (!success) |
| 4037 | return false; |
| 4038 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4039 | if (!MemAWrite (context, address + offset, pc + 8, addr_byte_size)) |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 4040 | return false; |
| 4041 | } |
| 4042 | |
| 4043 | // if wback then R[n] = R[n] - 4*BitCount(registers); |
| 4044 | if (wback) |
| 4045 | { |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 4046 | offset = (addr_byte_size * BitCount (registers)) * -1; |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 4047 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
| 4048 | context.SetImmediateSigned (offset); |
| 4049 | addr_t data = address + offset; |
| 4050 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 4051 | return false; |
| 4052 | } |
| 4053 | } |
| 4054 | return true; |
| 4055 | } |
| 4056 | |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 4057 | // STMDB (Store Multiple Decrement Before) stores multiple registers to consecutive memory locations using an address |
| 4058 | // from a base register. The consecutive memory locations end just below this address, and the address of the first of |
| 4059 | // those locations can optionally be written back to the base register. |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 4060 | bool |
| 4061 | EmulateInstructionARM::EmulateSTMDB (ARMEncoding encoding) |
| 4062 | { |
| 4063 | #if 0 |
| 4064 | if ConditionPassed() then |
| 4065 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 4066 | address = R[n] - 4*BitCount(registers); |
| 4067 | |
| 4068 | for i = 0 to 14 |
| 4069 | if registers<i> == ’1’ then |
| 4070 | if i == n && wback && i != LowestSetBit(registers) then |
| 4071 | MemA[address,4] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 4072 | else |
| 4073 | MemA[address,4] = R[i]; |
| 4074 | address = address + 4; |
| 4075 | |
| 4076 | if registers<15> == ’1’ then // Only possible for encoding A1 |
| 4077 | MemA[address,4] = PCStoreValue(); |
| 4078 | |
| 4079 | if wback then R[n] = R[n] - 4*BitCount(registers); |
| 4080 | #endif |
| 4081 | |
| 4082 | |
| 4083 | bool success = false; |
| 4084 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4085 | if (!success) |
| 4086 | return false; |
| 4087 | |
| 4088 | if (ConditionPassed ()) |
| 4089 | { |
| 4090 | uint32_t n; |
| 4091 | uint32_t registers = 0; |
| 4092 | bool wback; |
| 4093 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 4094 | |
| 4095 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 4096 | switch (encoding) |
| 4097 | { |
| 4098 | case eEncodingT1: |
| 4099 | // if W == ’1’ && Rn == ’1101’ then SEE PUSH; |
| 4100 | if ((BitIsSet (opcode, 21)) && (Bits32 (opcode, 19, 16) == 13)) |
| 4101 | { |
| 4102 | // See PUSH |
| 4103 | } |
| 4104 | // n = UInt(Rn); registers = ’0’:M:’0’:register_list; wback = (W == ’1’); |
| 4105 | n = Bits32 (opcode, 19, 16); |
| 4106 | registers = Bits32 (opcode, 15, 0); |
| 4107 | registers = registers & 0x5fff; // Make sure bits 15 & 13 are zeros. |
| 4108 | wback = BitIsSet (opcode, 21); |
| 4109 | // if n == 15 || BitCount(registers) < 2 then UNPREDICTABLE; |
| 4110 | if ((n == 15) || BitCount (registers) < 2) |
| 4111 | return false; |
| 4112 | // if wback && registers<n> == ’1’ then UNPREDICTABLE; |
| 4113 | if (wback && BitIsSet (registers, n)) |
| 4114 | return false; |
| 4115 | break; |
| 4116 | |
| 4117 | case eEncodingA1: |
| 4118 | // if W == ’1’ && Rn == ’1101’ && BitCount(register_list) >= 2 then SEE PUSH; |
| 4119 | if (BitIsSet (opcode, 21) && (Bits32 (opcode, 19, 16) == 13) && BitCount (Bits32 (opcode, 15, 0)) >= 2) |
| 4120 | { |
| 4121 | // See Push |
| 4122 | } |
| 4123 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 4124 | n = Bits32 (opcode, 19, 16); |
| 4125 | registers = Bits32 (opcode, 15, 0); |
| 4126 | wback = BitIsSet (opcode, 21); |
| 4127 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 4128 | if ((n == 15) || BitCount (registers) < 1) |
| 4129 | return false; |
| 4130 | break; |
| 4131 | |
| 4132 | default: |
| 4133 | return false; |
| 4134 | } |
| 4135 | |
| 4136 | // address = R[n] - 4*BitCount(registers); |
| 4137 | |
| 4138 | int32_t offset = 0; |
| 4139 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 4140 | if (!success) |
| 4141 | return false; |
| 4142 | |
| 4143 | address = address - (addr_byte_size * BitCount (registers)); |
| 4144 | |
| 4145 | EmulateInstruction::Context context; |
| 4146 | context.type = EmulateInstruction::eContextRegisterStore; |
| 4147 | Register base_reg; |
| 4148 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 4149 | |
| 4150 | // for i = 0 to 14 |
| 4151 | for (int i = 0; i < 14; ++i) |
| 4152 | { |
| 4153 | uint32_t lowest_set_bit = 14; |
| 4154 | // if registers<i> == ’1’ then |
| 4155 | if (BitIsSet (registers, i)) |
| 4156 | { |
| 4157 | if (i < lowest_set_bit) |
| 4158 | lowest_set_bit = i; |
| 4159 | // if i == n && wback && i != LowestSetBit(registers) then |
| 4160 | if ((i == n) && wback && (i != lowest_set_bit)) |
| 4161 | // MemA[address,4] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 4162 | WriteBits32UnknownToMemory (address + offset); |
| 4163 | else |
| 4164 | { |
| 4165 | // MemA[address,4] = R[i]; |
| 4166 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success); |
| 4167 | if (!success) |
| 4168 | return false; |
| 4169 | |
| 4170 | Register data_reg; |
| 4171 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i); |
| 4172 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4173 | if (!MemAWrite (context, address + offset, data, addr_byte_size)) |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 4174 | return false; |
| 4175 | } |
| 4176 | |
| 4177 | // address = address + 4; |
| 4178 | offset += addr_byte_size; |
| 4179 | } |
| 4180 | } |
| 4181 | |
| 4182 | // if registers<15> == ’1’ then // Only possible for encoding A1 |
| 4183 | // MemA[address,4] = PCStoreValue(); |
| 4184 | if (BitIsSet (registers, 15)) |
| 4185 | { |
| 4186 | Register pc_reg; |
| 4187 | pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc); |
| 4188 | context.SetRegisterPlusOffset (pc_reg, 8); |
| 4189 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 4190 | if (!success) |
| 4191 | return false; |
| 4192 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4193 | if (!MemAWrite (context, address + offset, pc + 8, addr_byte_size)) |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 4194 | return false; |
| 4195 | } |
| 4196 | |
| 4197 | // if wback then R[n] = R[n] - 4*BitCount(registers); |
| 4198 | if (wback) |
| 4199 | { |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 4200 | offset = (addr_byte_size * BitCount (registers)) * -1; |
| 4201 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
| 4202 | context.SetImmediateSigned (offset); |
| 4203 | addr_t data = address + offset; |
| 4204 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 4205 | return false; |
| 4206 | } |
| 4207 | } |
| 4208 | return true; |
| 4209 | } |
| 4210 | |
| 4211 | // STMIB (Store Multiple Increment Before) stores multiple registers to consecutive memory locations using an address |
| 4212 | // from a base register. The consecutive memory locations start just above this address, and the address of the last |
| 4213 | // of those locations can optionally be written back to the base register. |
| 4214 | bool |
| 4215 | EmulateInstructionARM::EmulateSTMIB (ARMEncoding encoding) |
| 4216 | { |
| 4217 | #if 0 |
| 4218 | if ConditionPassed() then |
| 4219 | EncodingSpecificOperations(); |
| 4220 | address = R[n] + 4; |
| 4221 | |
| 4222 | for i = 0 to 14 |
| 4223 | if registers<i> == ’1’ then |
| 4224 | if i == n && wback && i != LowestSetBit(registers) then |
| 4225 | MemA[address,4] = bits(32) UNKNOWN; |
| 4226 | else |
| 4227 | MemA[address,4] = R[i]; |
| 4228 | address = address + 4; |
| 4229 | |
| 4230 | if registers<15> == ’1’ then |
| 4231 | MemA[address,4] = PCStoreValue(); |
| 4232 | |
| 4233 | if wback then R[n] = R[n] + 4*BitCount(registers); |
| 4234 | #endif |
| 4235 | |
| 4236 | bool success = false; |
| 4237 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4238 | if (!success) |
| 4239 | return false; |
| 4240 | |
| 4241 | if (ConditionPassed()) |
| 4242 | { |
| 4243 | uint32_t n; |
| 4244 | uint32_t registers = 0; |
| 4245 | bool wback; |
| 4246 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 4247 | |
| 4248 | // EncodingSpecificOperations(); |
| 4249 | switch (encoding) |
| 4250 | { |
| 4251 | case eEncodingA1: |
| 4252 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 4253 | n = Bits32 (opcode, 19, 16); |
| 4254 | registers = Bits32 (opcode, 15, 0); |
| 4255 | wback = BitIsSet (opcode, 21); |
| 4256 | |
| 4257 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 4258 | if ((n == 15) && (BitCount (registers) < 1)) |
| 4259 | return false; |
| 4260 | break; |
| 4261 | default: |
| 4262 | return false; |
| 4263 | } |
| 4264 | // address = R[n] + 4; |
| 4265 | |
| 4266 | int32_t offset = 0; |
| 4267 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 4268 | if (!success) |
| 4269 | return false; |
| 4270 | |
| 4271 | address = address + addr_byte_size; |
| 4272 | |
| 4273 | EmulateInstruction::Context context; |
| 4274 | context.type = EmulateInstruction::eContextRegisterStore; |
| 4275 | Register base_reg; |
| 4276 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 4277 | |
| 4278 | uint32_t lowest_set_bit = 14; |
| 4279 | // for i = 0 to 14 |
| 4280 | for (int i = 0; i < 14; ++i) |
| 4281 | { |
| 4282 | // if registers<i> == ’1’ then |
| 4283 | if (BitIsSet (registers, i)) |
| 4284 | { |
| 4285 | if (i < lowest_set_bit) |
| 4286 | lowest_set_bit = i; |
| 4287 | // if i == n && wback && i != LowestSetBit(registers) then |
| 4288 | if ((i == n) && wback && (i != lowest_set_bit)) |
| 4289 | // MemA[address,4] = bits(32) UNKNOWN; |
| 4290 | WriteBits32UnknownToMemory (address + offset); |
| 4291 | // else |
| 4292 | else |
| 4293 | { |
| 4294 | // MemA[address,4] = R[i]; |
| 4295 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success); |
| 4296 | if (!success) |
| 4297 | return false; |
| 4298 | |
| 4299 | Register data_reg; |
| 4300 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i); |
| 4301 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4302 | if (!MemAWrite (context, address + offset, data, addr_byte_size)) |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 4303 | return false; |
| 4304 | } |
| 4305 | |
| 4306 | // address = address + 4; |
| 4307 | offset += addr_byte_size; |
| 4308 | } |
| 4309 | } |
| 4310 | |
| 4311 | // if registers<15> == ’1’ then |
| 4312 | // MemA[address,4] = PCStoreValue(); |
| 4313 | if (BitIsSet (registers, 15)) |
| 4314 | { |
| 4315 | Register pc_reg; |
| 4316 | pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc); |
| 4317 | context.SetRegisterPlusOffset (pc_reg, 8); |
| 4318 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 4319 | if (!success) |
| 4320 | return false; |
| 4321 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4322 | if (!MemAWrite (context, address + offset, pc + 8, addr_byte_size)) |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 4323 | return false; |
| 4324 | } |
| 4325 | |
| 4326 | // if wback then R[n] = R[n] + 4*BitCount(registers); |
| 4327 | if (wback) |
| 4328 | { |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 4329 | offset = addr_byte_size * BitCount (registers); |
| 4330 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
| 4331 | context.SetImmediateSigned (offset); |
| 4332 | addr_t data = address + offset; |
| 4333 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 4334 | return false; |
| 4335 | } |
| 4336 | } |
| 4337 | return true; |
| 4338 | } |
Caroline Tice | 7fac857 | 2011-02-15 22:53:54 +0000 | [diff] [blame] | 4339 | |
| 4340 | // STR (store immediate) calcualtes an address from a base register value and an immediate offset, and stores a word |
| 4341 | // from a register to memory. It can use offset, post-indexed, or pre-indexed addressing. |
| 4342 | bool |
| 4343 | EmulateInstructionARM::EmulateSTRThumb (ARMEncoding encoding) |
| 4344 | { |
| 4345 | #if 0 |
| 4346 | if ConditionPassed() then |
| 4347 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 4348 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 4349 | address = if index then offset_addr else R[n]; |
| 4350 | if UnalignedSupport() || address<1:0> == ’00’ then |
| 4351 | MemU[address,4] = R[t]; |
| 4352 | else // Can only occur before ARMv7 |
| 4353 | MemU[address,4] = bits(32) UNKNOWN; |
| 4354 | if wback then R[n] = offset_addr; |
| 4355 | #endif |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 4356 | |
Caroline Tice | 7fac857 | 2011-02-15 22:53:54 +0000 | [diff] [blame] | 4357 | bool success = false; |
| 4358 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4359 | if (!success) |
| 4360 | return false; |
| 4361 | |
| 4362 | if (ConditionPassed()) |
| 4363 | { |
| 4364 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 4365 | |
| 4366 | uint32_t t; |
| 4367 | uint32_t n; |
| 4368 | uint32_t imm32; |
| 4369 | bool index; |
| 4370 | bool add; |
| 4371 | bool wback; |
| 4372 | // EncodingSpecificOperations (); NullCheckIfThumbEE(n); |
| 4373 | switch (encoding) |
| 4374 | { |
| 4375 | case eEncodingT1: |
| 4376 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm5:’00’, 32); |
| 4377 | t = Bits32 (opcode, 2, 0); |
| 4378 | n = Bits32 (opcode, 5, 3); |
| 4379 | imm32 = Bits32 (opcode, 10, 6) << 2; |
| 4380 | |
| 4381 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4382 | index = true; |
| 4383 | add = false; |
| 4384 | wback = false; |
| 4385 | break; |
| 4386 | |
| 4387 | case eEncodingT2: |
| 4388 | // t = UInt(Rt); n = 13; imm32 = ZeroExtend(imm8:’00’, 32); |
| 4389 | t = Bits32 (opcode, 10, 8); |
| 4390 | n = 13; |
| 4391 | imm32 = Bits32 (opcode, 7, 0) << 2; |
| 4392 | |
| 4393 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4394 | index = true; |
| 4395 | add = true; |
| 4396 | wback = false; |
| 4397 | break; |
| 4398 | |
| 4399 | case eEncodingT3: |
| 4400 | // if Rn == ’1111’ then UNDEFINED; |
| 4401 | if (Bits32 (opcode, 19, 16) == 15) |
| 4402 | return false; |
| 4403 | |
| 4404 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32); |
| 4405 | t = Bits32 (opcode, 15, 12); |
| 4406 | n = Bits32 (opcode, 19, 16); |
| 4407 | imm32 = Bits32 (opcode, 11, 0); |
| 4408 | |
| 4409 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4410 | index = true; |
| 4411 | add = true; |
| 4412 | wback = false; |
| 4413 | |
| 4414 | // if t == 15 then UNPREDICTABLE; |
| 4415 | if (t == 15) |
| 4416 | return false; |
| 4417 | break; |
| 4418 | |
| 4419 | case eEncodingT4: |
| 4420 | // if P == ’1’ && U == ’1’ && W == ’0’ then SEE STRT; |
| 4421 | // if Rn == ’1101’ && P == ’1’ && U == ’0’ && W == ’1’ && imm8 == ’00000100’ then SEE PUSH; |
| 4422 | // if Rn == ’1111’ || (P == ’0’ && W == ’0’) then UNDEFINED; |
| 4423 | if ((Bits32 (opcode, 19, 16) == 15) |
| 4424 | || (BitIsClear (opcode, 10) && BitIsClear (opcode, 8))) |
| 4425 | return false; |
| 4426 | |
| 4427 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm8, 32); |
| 4428 | t = Bits32 (opcode, 15, 12); |
| 4429 | n = Bits32 (opcode, 19, 16); |
| 4430 | imm32 = Bits32 (opcode, 7, 0); |
| 4431 | |
| 4432 | // index = (P == ’1’); add = (U == ’1’); wback = (W == ’1’); |
| 4433 | index = BitIsSet (opcode, 10); |
| 4434 | add = BitIsSet (opcode, 9); |
| 4435 | wback = BitIsSet (opcode, 8); |
| 4436 | |
| 4437 | // if t == 15 || (wback && n == t) then UNPREDICTABLE; |
| 4438 | if ((t == 15) || (wback && (n == t))) |
| 4439 | return false; |
| 4440 | break; |
| 4441 | |
| 4442 | default: |
| 4443 | return false; |
| 4444 | } |
| 4445 | |
| 4446 | addr_t offset_addr; |
| 4447 | addr_t address; |
| 4448 | |
| 4449 | // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 4450 | uint32_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 4451 | if (!success) |
| 4452 | return false; |
| 4453 | |
| 4454 | if (add) |
| 4455 | offset_addr = base_address + imm32; |
| 4456 | else |
| 4457 | offset_addr = base_address - imm32; |
| 4458 | |
| 4459 | // address = if index then offset_addr else R[n]; |
| 4460 | if (index) |
| 4461 | address = offset_addr; |
| 4462 | else |
| 4463 | address = base_address; |
| 4464 | |
| 4465 | EmulateInstruction::Context context; |
| 4466 | context.type = eContextRegisterStore; |
| 4467 | Register base_reg; |
| 4468 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 4469 | |
| 4470 | // if UnalignedSupport() || address<1:0> == ’00’ then |
| 4471 | if (UnalignedSupport () || (BitIsClear (address, 1) && BitIsClear (address, 0))) |
| 4472 | { |
| 4473 | // MemU[address,4] = R[t]; |
| 4474 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + t, 0, &success); |
| 4475 | if (!success) |
| 4476 | return false; |
| 4477 | |
| 4478 | Register data_reg; |
| 4479 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + t); |
| 4480 | int32_t offset = address - base_address; |
| 4481 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4482 | if (!MemUWrite (context, address, data, addr_byte_size)) |
Caroline Tice | 7fac857 | 2011-02-15 22:53:54 +0000 | [diff] [blame] | 4483 | return false; |
| 4484 | } |
| 4485 | else |
| 4486 | { |
| 4487 | // MemU[address,4] = bits(32) UNKNOWN; |
| 4488 | WriteBits32UnknownToMemory (address); |
| 4489 | } |
| 4490 | |
| 4491 | // if wback then R[n] = offset_addr; |
| 4492 | if (wback) |
| 4493 | { |
| 4494 | context.type = eContextRegisterLoad; |
| 4495 | context.SetAddress (offset_addr); |
| 4496 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 4497 | return false; |
| 4498 | } |
| 4499 | } |
| 4500 | return true; |
| 4501 | } |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 4502 | |
Caroline Tice | 3fd63e9 | 2011-02-16 00:33:43 +0000 | [diff] [blame] | 4503 | // STR (Store Register) calculates an address from a base register value and an offset register value, stores a |
| 4504 | // word from a register to memory. The offset register value can optionally be shifted. |
| 4505 | bool |
| 4506 | EmulateInstructionARM::EmulateSTRRegister (ARMEncoding encoding) |
| 4507 | { |
| 4508 | #if 0 |
| 4509 | if ConditionPassed() then |
| 4510 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 4511 | offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 4512 | offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 4513 | address = if index then offset_addr else R[n]; |
| 4514 | if t == 15 then // Only possible for encoding A1 |
| 4515 | data = PCStoreValue(); |
| 4516 | else |
| 4517 | data = R[t]; |
| 4518 | if UnalignedSupport() || address<1:0> == ’00’ || CurrentInstrSet() == InstrSet_ARM then |
| 4519 | MemU[address,4] = data; |
| 4520 | else // Can only occur before ARMv7 |
| 4521 | MemU[address,4] = bits(32) UNKNOWN; |
| 4522 | if wback then R[n] = offset_addr; |
| 4523 | #endif |
| 4524 | |
| 4525 | bool success = false; |
| 4526 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4527 | if (!success) |
| 4528 | return false; |
| 4529 | |
| 4530 | if (ConditionPassed()) |
| 4531 | { |
| 4532 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 4533 | |
| 4534 | uint32_t t; |
| 4535 | uint32_t n; |
| 4536 | uint32_t m; |
| 4537 | ARM_ShifterType shift_t; |
| 4538 | uint32_t shift_n; |
| 4539 | bool index; |
| 4540 | bool add; |
| 4541 | bool wback; |
| 4542 | |
| 4543 | // EncodingSpecificOperations (); NullCheckIfThumbEE(n); |
| 4544 | switch (encoding) |
| 4545 | { |
| 4546 | case eEncodingT1: |
| 4547 | // if CurrentInstrSet() == InstrSet_ThumbEE then SEE "Modified operation in ThumbEE"; |
| 4548 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 4549 | t = Bits32 (opcode, 2, 0); |
| 4550 | n = Bits32 (opcode, 5, 3); |
| 4551 | m = Bits32 (opcode, 8, 6); |
| 4552 | |
| 4553 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4554 | index = true; |
| 4555 | add = true; |
| 4556 | wback = false; |
| 4557 | |
| 4558 | // (shift_t, shift_n) = (SRType_LSL, 0); |
| 4559 | shift_t = SRType_LSL; |
| 4560 | shift_n = 0; |
| 4561 | break; |
| 4562 | |
| 4563 | case eEncodingT2: |
| 4564 | // if Rn == ’1111’ then UNDEFINED; |
| 4565 | if (Bits32 (opcode, 19, 16) == 15) |
| 4566 | return false; |
| 4567 | |
| 4568 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 4569 | t = Bits32 (opcode, 15, 12); |
| 4570 | n = Bits32 (opcode, 19, 16); |
| 4571 | m = Bits32 (opcode, 3, 0); |
| 4572 | |
| 4573 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4574 | index = true; |
| 4575 | add = true; |
| 4576 | wback = false; |
| 4577 | |
| 4578 | // (shift_t, shift_n) = (SRType_LSL, UInt(imm2)); |
| 4579 | shift_t = SRType_LSL; |
| 4580 | shift_n = Bits32 (opcode, 5, 4); |
| 4581 | |
| 4582 | // if t == 15 || BadReg(m) then UNPREDICTABLE; |
| 4583 | if ((t == 15) || (BadReg (m))) |
| 4584 | return false; |
| 4585 | break; |
| 4586 | |
| 4587 | case eEncodingA1: |
| 4588 | { |
| 4589 | // if P == ’0’ && W == ’1’ then SEE STRT; |
| 4590 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 4591 | t = Bits32 (opcode, 15, 12); |
| 4592 | n = Bits32 (opcode, 19, 16); |
| 4593 | m = Bits32 (opcode, 3, 0); |
| 4594 | |
| 4595 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 4596 | index = BitIsSet (opcode, 24); |
| 4597 | add = BitIsSet (opcode, 23); |
| 4598 | wback = (BitIsClear (opcode, 24) || BitIsSet (opcode, 21)); |
| 4599 | |
| 4600 | // (shift_t, shift_n) = DecodeImmShift(type, imm5); |
| 4601 | uint32_t typ = Bits32 (opcode, 6, 5); |
| 4602 | uint32_t imm5 = Bits32 (opcode, 11, 7); |
| 4603 | shift_n = DecodeImmShift(typ, imm5, shift_t); |
| 4604 | |
| 4605 | // if m == 15 then UNPREDICTABLE; |
| 4606 | if (m == 15) |
| 4607 | return false; |
| 4608 | |
| 4609 | // if wback && (n == 15 || n == t) then UNPREDICTABLE; |
| 4610 | if (wback && ((n == 15) || (n == t))) |
| 4611 | return false; |
| 4612 | |
| 4613 | break; |
| 4614 | } |
| 4615 | default: |
| 4616 | return false; |
| 4617 | } |
| 4618 | |
| 4619 | addr_t offset_addr; |
| 4620 | addr_t address; |
| 4621 | int32_t offset = 0; |
| 4622 | |
| 4623 | addr_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 4624 | if (!success) |
| 4625 | return false; |
| 4626 | |
| 4627 | uint32_t Rm_data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 4628 | if (!success) |
| 4629 | return false; |
| 4630 | |
| 4631 | // offset = Shift(R[m], shift_t, shift_n, APSR.C); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4632 | offset = Shift (Rm_data, shift_t, shift_n, APSR_C); |
Caroline Tice | 3fd63e9 | 2011-02-16 00:33:43 +0000 | [diff] [blame] | 4633 | |
| 4634 | // offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 4635 | if (add) |
| 4636 | offset_addr = base_address + offset; |
| 4637 | else |
| 4638 | offset_addr = base_address - offset; |
| 4639 | |
| 4640 | // address = if index then offset_addr else R[n]; |
| 4641 | if (index) |
| 4642 | address = offset_addr; |
| 4643 | else |
| 4644 | address = base_address; |
| 4645 | |
| 4646 | uint32_t data; |
| 4647 | // if t == 15 then // Only possible for encoding A1 |
| 4648 | if (t == 15) |
| 4649 | // data = PCStoreValue(); |
| 4650 | data = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 4651 | else |
| 4652 | // data = R[t]; |
| 4653 | data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + t, 0, &success); |
| 4654 | |
| 4655 | if (!success) |
| 4656 | return false; |
| 4657 | |
| 4658 | EmulateInstruction::Context context; |
| 4659 | context.type = eContextRegisterStore; |
| 4660 | |
| 4661 | // if UnalignedSupport() || address<1:0> == ’00’ || CurrentInstrSet() == InstrSet_ARM then |
| 4662 | if (UnalignedSupport () |
| 4663 | || (BitIsClear (address, 1) && BitIsClear (address, 0)) |
| 4664 | || CurrentInstrSet() == eModeARM) |
| 4665 | { |
| 4666 | // MemU[address,4] = data; |
| 4667 | |
| 4668 | Register base_reg; |
| 4669 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 4670 | |
| 4671 | Register data_reg; |
| 4672 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + t); |
| 4673 | |
| 4674 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, address - base_address); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4675 | if (!MemUWrite (context, address, data, addr_byte_size)) |
Caroline Tice | 3fd63e9 | 2011-02-16 00:33:43 +0000 | [diff] [blame] | 4676 | return false; |
| 4677 | |
| 4678 | } |
| 4679 | else |
| 4680 | // MemU[address,4] = bits(32) UNKNOWN; |
| 4681 | WriteBits32UnknownToMemory (address); |
| 4682 | |
| 4683 | // if wback then R[n] = offset_addr; |
| 4684 | if (wback) |
| 4685 | { |
| 4686 | context.type = eContextRegisterLoad; |
| 4687 | context.SetAddress (offset_addr); |
| 4688 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 4689 | return false; |
| 4690 | } |
| 4691 | |
| 4692 | } |
| 4693 | return true; |
| 4694 | } |
Caroline Tice | 73a29de | 2011-02-16 20:22:22 +0000 | [diff] [blame] | 4695 | |
| 4696 | bool |
| 4697 | EmulateInstructionARM::EmulateSTRBThumb (ARMEncoding encoding) |
| 4698 | { |
| 4699 | #if 0 |
| 4700 | if ConditionPassed() then |
| 4701 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 4702 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 4703 | address = if index then offset_addr else R[n]; |
| 4704 | MemU[address,1] = R[t]<7:0>; |
| 4705 | if wback then R[n] = offset_addr; |
| 4706 | #endif |
| 4707 | |
| 4708 | |
| 4709 | bool success = false; |
| 4710 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4711 | if (!success) |
| 4712 | return false; |
| 4713 | |
| 4714 | if (ConditionPassed ()) |
| 4715 | { |
| 4716 | uint32_t t; |
| 4717 | uint32_t n; |
| 4718 | uint32_t imm32; |
| 4719 | bool index; |
| 4720 | bool add; |
| 4721 | bool wback; |
| 4722 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 4723 | switch (encoding) |
| 4724 | { |
| 4725 | case eEncodingT1: |
| 4726 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm5, 32); |
| 4727 | t = Bits32 (opcode, 2, 0); |
| 4728 | n = Bits32 (opcode, 5, 3); |
| 4729 | imm32 = Bits32 (opcode, 10, 6); |
| 4730 | |
| 4731 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4732 | index = true; |
| 4733 | add = true; |
| 4734 | wback = false; |
| 4735 | break; |
| 4736 | |
| 4737 | case eEncodingT2: |
| 4738 | // if Rn == ’1111’ then UNDEFINED; |
| 4739 | if (Bits32 (opcode, 19, 16) == 15) |
| 4740 | return false; |
| 4741 | |
| 4742 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32); |
| 4743 | t = Bits32 (opcode, 15, 12); |
| 4744 | n = Bits32 (opcode, 19, 16); |
| 4745 | imm32 = Bits32 (opcode, 11, 0); |
| 4746 | |
| 4747 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4748 | index = true; |
| 4749 | add = true; |
| 4750 | wback = false; |
| 4751 | |
| 4752 | // if BadReg(t) then UNPREDICTABLE; |
| 4753 | if (BadReg (t)) |
| 4754 | return false; |
| 4755 | break; |
| 4756 | |
| 4757 | case eEncodingT3: |
| 4758 | // if P == ’1’ && U == ’1’ && W == ’0’ then SEE STRBT; |
| 4759 | // if Rn == ’1111’ || (P == ’0’ && W == ’0’) then UNDEFINED; |
| 4760 | if (Bits32 (opcode, 19, 16) == 15) |
| 4761 | return false; |
| 4762 | |
| 4763 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm8, 32); |
| 4764 | t = Bits32 (opcode, 15, 12); |
| 4765 | n = Bits32 (opcode, 19, 16); |
| 4766 | imm32 = Bits32 (opcode, 7, 0); |
| 4767 | |
| 4768 | // index = (P == ’1’); add = (U == ’1’); wback = (W == ’1’); |
| 4769 | index = BitIsSet (opcode, 10); |
| 4770 | add = BitIsSet (opcode, 9); |
| 4771 | wback = BitIsSet (opcode, 8); |
| 4772 | |
| 4773 | // if BadReg(t) || (wback && n == t) then UNPREDICTABLE |
| 4774 | if ((BadReg (t)) || (wback && (n == t))) |
| 4775 | return false; |
| 4776 | break; |
| 4777 | |
| 4778 | default: |
| 4779 | return false; |
| 4780 | } |
| 4781 | |
| 4782 | addr_t offset_addr; |
| 4783 | addr_t address; |
| 4784 | addr_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 4785 | if (!success) |
| 4786 | return false; |
| 4787 | |
| 4788 | // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 4789 | if (add) |
| 4790 | offset_addr = base_address + imm32; |
| 4791 | else |
| 4792 | offset_addr = base_address - imm32; |
| 4793 | |
| 4794 | // address = if index then offset_addr else R[n]; |
| 4795 | if (index) |
| 4796 | address = offset_addr; |
| 4797 | else |
| 4798 | address = base_address; |
| 4799 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4800 | // MemU[address,1] = R[t]<7:0> |
Caroline Tice | 73a29de | 2011-02-16 20:22:22 +0000 | [diff] [blame] | 4801 | Register base_reg; |
| 4802 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 4803 | |
| 4804 | Register data_reg; |
| 4805 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + t); |
| 4806 | |
| 4807 | EmulateInstruction::Context context; |
| 4808 | context.type = eContextRegisterStore; |
| 4809 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, address - base_address); |
| 4810 | |
| 4811 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + t, 0, &success); |
| 4812 | if (!success) |
| 4813 | return false; |
| 4814 | |
| 4815 | data = Bits32 (data, 7, 0); |
| 4816 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4817 | if (!MemUWrite (context, address, data, 1)) |
Caroline Tice | 73a29de | 2011-02-16 20:22:22 +0000 | [diff] [blame] | 4818 | return false; |
| 4819 | |
| 4820 | // if wback then R[n] = offset_addr; |
| 4821 | if (wback) |
| 4822 | { |
| 4823 | context.type = eContextRegisterLoad; |
| 4824 | context.SetAddress (offset_addr); |
| 4825 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 4826 | return false; |
| 4827 | } |
| 4828 | |
| 4829 | } |
| 4830 | |
| 4831 | return true; |
| 4832 | } |
Caroline Tice | 3fd63e9 | 2011-02-16 00:33:43 +0000 | [diff] [blame] | 4833 | |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4834 | // Add with Carry (immediate) adds an immediate value and the carry flag value to a register value, |
| 4835 | // and writes the result to the destination register. It can optionally update the condition flags |
| 4836 | // based on the result. |
| 4837 | bool |
| 4838 | EmulateInstructionARM::EmulateADCImm (ARMEncoding encoding) |
| 4839 | { |
| 4840 | #if 0 |
| 4841 | // ARM pseudo code... |
| 4842 | if ConditionPassed() then |
| 4843 | EncodingSpecificOperations(); |
| 4844 | (result, carry, overflow) = AddWithCarry(R[n], imm32, APSR.C); |
| 4845 | if d == 15 then // Can only occur for ARM encoding |
| 4846 | ALUWritePC(result); // setflags is always FALSE here |
| 4847 | else |
| 4848 | R[d] = result; |
| 4849 | if setflags then |
| 4850 | APSR.N = result<31>; |
| 4851 | APSR.Z = IsZeroBit(result); |
| 4852 | APSR.C = carry; |
| 4853 | APSR.V = overflow; |
| 4854 | #endif |
| 4855 | |
| 4856 | bool success = false; |
| 4857 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4858 | if (!success) |
| 4859 | return false; |
| 4860 | |
| 4861 | if (ConditionPassed()) |
| 4862 | { |
| 4863 | uint32_t Rd, Rn; |
| 4864 | uint32_t imm32; // the immediate value to be added to the value obtained from Rn |
| 4865 | bool setflags; |
| 4866 | switch (encoding) |
| 4867 | { |
| 4868 | case eEncodingT1: |
| 4869 | Rd = Bits32(opcode, 11, 8); |
| 4870 | Rn = Bits32(opcode, 19, 16); |
| 4871 | setflags = BitIsSet(opcode, 20); |
| 4872 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 4873 | if (BadReg(Rd) || BadReg(Rn)) |
| 4874 | return false; |
| 4875 | break; |
| 4876 | case eEncodingA1: |
| 4877 | Rd = Bits32(opcode, 15, 12); |
| 4878 | Rn = Bits32(opcode, 19, 16); |
| 4879 | setflags = BitIsSet(opcode, 20); |
| 4880 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 4881 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 4882 | if (Rd == 15 && setflags) |
| 4883 | return false; |
| 4884 | break; |
| 4885 | default: |
| 4886 | return false; |
| 4887 | } |
| 4888 | |
| 4889 | // Read the first operand. |
| 4890 | int32_t val1 = ReadCoreReg(Rn, &success); |
| 4891 | if (!success) |
| 4892 | return false; |
| 4893 | |
| 4894 | AddWithCarryResult res = AddWithCarry(val1, imm32, APSR_C); |
| 4895 | |
| 4896 | EmulateInstruction::Context context; |
| 4897 | context.type = EmulateInstruction::eContextImmediate; |
| 4898 | context.SetNoArgs (); |
| 4899 | |
| 4900 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 4901 | return false; |
| 4902 | } |
| 4903 | return true; |
| 4904 | } |
| 4905 | |
| 4906 | // Add with Carry (register) adds a register value, the carry flag value, and an optionally-shifted |
| 4907 | // register value, and writes the result to the destination register. It can optionally update the |
| 4908 | // condition flags based on the result. |
| 4909 | bool |
| 4910 | EmulateInstructionARM::EmulateADCReg (ARMEncoding encoding) |
| 4911 | { |
| 4912 | #if 0 |
| 4913 | // ARM pseudo code... |
| 4914 | if ConditionPassed() then |
| 4915 | EncodingSpecificOperations(); |
| 4916 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 4917 | (result, carry, overflow) = AddWithCarry(R[n], shifted, APSR.C); |
| 4918 | if d == 15 then // Can only occur for ARM encoding |
| 4919 | ALUWritePC(result); // setflags is always FALSE here |
| 4920 | else |
| 4921 | R[d] = result; |
| 4922 | if setflags then |
| 4923 | APSR.N = result<31>; |
| 4924 | APSR.Z = IsZeroBit(result); |
| 4925 | APSR.C = carry; |
| 4926 | APSR.V = overflow; |
| 4927 | #endif |
| 4928 | |
| 4929 | bool success = false; |
| 4930 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4931 | if (!success) |
| 4932 | return false; |
| 4933 | |
| 4934 | if (ConditionPassed()) |
| 4935 | { |
| 4936 | uint32_t Rd, Rn, Rm; |
| 4937 | ARM_ShifterType shift_t; |
| 4938 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 4939 | bool setflags; |
| 4940 | switch (encoding) |
| 4941 | { |
| 4942 | case eEncodingT1: |
| 4943 | Rd = Rn = Bits32(opcode, 2, 0); |
| 4944 | Rm = Bits32(opcode, 5, 3); |
| 4945 | setflags = !InITBlock(); |
| 4946 | shift_t = SRType_LSL; |
| 4947 | shift_n = 0; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 4948 | break; |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4949 | case eEncodingT2: |
| 4950 | Rd = Bits32(opcode, 11, 8); |
| 4951 | Rn = Bits32(opcode, 19, 16); |
| 4952 | Rm = Bits32(opcode, 3, 0); |
| 4953 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 4954 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4955 | if (BadReg(Rd) || BadReg(Rn) || BadReg(Rm)) |
| 4956 | return false; |
| 4957 | break; |
| 4958 | case eEncodingA1: |
| 4959 | Rd = Bits32(opcode, 15, 12); |
| 4960 | Rn = Bits32(opcode, 19, 16); |
| 4961 | Rm = Bits32(opcode, 3, 0); |
| 4962 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 4963 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4964 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 4965 | if (Rd == 15 && setflags) |
| 4966 | return false; |
| 4967 | break; |
| 4968 | default: |
| 4969 | return false; |
| 4970 | } |
| 4971 | |
| 4972 | // Read the first operand. |
| 4973 | int32_t val1 = ReadCoreReg(Rn, &success); |
| 4974 | if (!success) |
| 4975 | return false; |
| 4976 | |
| 4977 | // Read the second operand. |
| 4978 | int32_t val2 = ReadCoreReg(Rm, &success); |
| 4979 | if (!success) |
| 4980 | return false; |
| 4981 | |
| 4982 | uint32_t shifted = Shift(val2, shift_t, shift_n, APSR_C); |
| 4983 | AddWithCarryResult res = AddWithCarry(val1, shifted, APSR_C); |
| 4984 | |
| 4985 | EmulateInstruction::Context context; |
| 4986 | context.type = EmulateInstruction::eContextImmediate; |
| 4987 | context.SetNoArgs (); |
| 4988 | |
| 4989 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 4990 | return false; |
| 4991 | } |
| 4992 | return true; |
| 4993 | } |
| 4994 | |
Johnny Chen | a695f95 | 2011-02-23 21:24:25 +0000 | [diff] [blame] | 4995 | // This instruction adds an immediate value to the PC value to form a PC-relative address, |
| 4996 | // and writes the result to the destination register. |
| 4997 | bool |
| 4998 | EmulateInstructionARM::EmulateADR (ARMEncoding encoding) |
| 4999 | { |
| 5000 | #if 0 |
| 5001 | // ARM pseudo code... |
| 5002 | if ConditionPassed() then |
| 5003 | EncodingSpecificOperations(); |
| 5004 | result = if add then (Align(PC,4) + imm32) else (Align(PC,4) - imm32); |
| 5005 | if d == 15 then // Can only occur for ARM encodings |
| 5006 | ALUWritePC(result); |
| 5007 | else |
| 5008 | R[d] = result; |
| 5009 | #endif |
| 5010 | |
| 5011 | bool success = false; |
| 5012 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5013 | if (!success) |
| 5014 | return false; |
| 5015 | |
| 5016 | if (ConditionPassed()) |
| 5017 | { |
| 5018 | uint32_t Rd; |
| 5019 | uint32_t imm32; // the immediate value to be added/subtracted to/from the PC |
| 5020 | bool add; |
| 5021 | switch (encoding) |
| 5022 | { |
| 5023 | case eEncodingT1: |
| 5024 | Rd = Bits32(opcode, 10, 8); |
| 5025 | imm32 = ThumbImm8Scaled(opcode); // imm32 = ZeroExtend(imm8:'00', 32) |
| 5026 | break; |
| 5027 | case eEncodingT2: |
| 5028 | case eEncodingT3: |
| 5029 | Rd = Bits32(opcode, 11, 8); |
| 5030 | imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32) |
| 5031 | add = (Bits32(opcode, 24, 21) == 0); // 0b0000 => ADD; 0b0101 => SUB |
| 5032 | if (BadReg(Rd)) |
| 5033 | return false; |
| 5034 | break; |
| 5035 | case eEncodingA1: |
| 5036 | case eEncodingA2: |
| 5037 | Rd = Bits32(opcode, 15, 12); |
| 5038 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 5039 | add = (Bits32(opcode, 24, 21) == 0x4); // 0b0100 => ADD; 0b0010 => SUB |
| 5040 | break; |
| 5041 | default: |
| 5042 | return false; |
| 5043 | } |
| 5044 | |
| 5045 | // Read the PC value. |
| 5046 | uint32_t pc = ReadCoreReg(PC_REG, &success); |
| 5047 | if (!success) |
| 5048 | return false; |
| 5049 | |
| 5050 | uint32_t result = (add ? Align(pc, 4) + imm32 : Align(pc, 4) - imm32); |
| 5051 | |
| 5052 | EmulateInstruction::Context context; |
| 5053 | context.type = EmulateInstruction::eContextImmediate; |
| 5054 | context.SetNoArgs (); |
| 5055 | |
| 5056 | if (!WriteCoreReg(context, result, Rd)) |
| 5057 | return false; |
| 5058 | } |
| 5059 | return true; |
| 5060 | } |
| 5061 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5062 | // This instruction performs a bitwise AND of a register value and an immediate value, and writes the result |
| 5063 | // to the destination register. It can optionally update the condition flags based on the result. |
| 5064 | bool |
| 5065 | EmulateInstructionARM::EmulateANDImm (ARMEncoding encoding) |
| 5066 | { |
| 5067 | #if 0 |
| 5068 | // ARM pseudo code... |
| 5069 | if ConditionPassed() then |
| 5070 | EncodingSpecificOperations(); |
| 5071 | result = R[n] AND imm32; |
| 5072 | if d == 15 then // Can only occur for ARM encoding |
| 5073 | ALUWritePC(result); // setflags is always FALSE here |
| 5074 | else |
| 5075 | R[d] = result; |
| 5076 | if setflags then |
| 5077 | APSR.N = result<31>; |
| 5078 | APSR.Z = IsZeroBit(result); |
| 5079 | APSR.C = carry; |
| 5080 | // APSR.V unchanged |
| 5081 | #endif |
| 5082 | |
| 5083 | bool success = false; |
| 5084 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5085 | if (!success) |
| 5086 | return false; |
| 5087 | |
| 5088 | if (ConditionPassed()) |
| 5089 | { |
| 5090 | uint32_t Rd, Rn; |
| 5091 | uint32_t imm32; // the immediate value to be ANDed to the value obtained from Rn |
| 5092 | bool setflags; |
| 5093 | uint32_t carry; // the carry bit after ARM/Thumb Expand operation |
| 5094 | switch (encoding) |
| 5095 | { |
| 5096 | case eEncodingT1: |
| 5097 | Rd = Bits32(opcode, 11, 8); |
| 5098 | Rn = Bits32(opcode, 19, 16); |
| 5099 | setflags = BitIsSet(opcode, 20); |
| 5100 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ThumbExpandImm(i:imm3:imm8, APSR.C) |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 5101 | // if Rd == '1111' && S == '1' then SEE TST (immediate); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5102 | if (Rd == 15 && setflags) |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 5103 | return EmulateTSTImm(eEncodingT1); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5104 | if (Rd == 13 || (Rd == 15 && !setflags) || BadReg(Rn)) |
| 5105 | return false; |
| 5106 | break; |
| 5107 | case eEncodingA1: |
| 5108 | Rd = Bits32(opcode, 15, 12); |
| 5109 | Rn = Bits32(opcode, 19, 16); |
| 5110 | setflags = BitIsSet(opcode, 20); |
| 5111 | imm32 = ARMExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ARMExpandImm(imm12, APSR.C) |
| 5112 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 5113 | if (Rd == 15 && setflags) |
| 5114 | return false; |
| 5115 | break; |
| 5116 | default: |
| 5117 | return false; |
| 5118 | } |
| 5119 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5120 | // Read the first operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 5121 | uint32_t val1 = ReadCoreReg(Rn, &success); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5122 | if (!success) |
| 5123 | return false; |
| 5124 | |
| 5125 | uint32_t result = val1 & imm32; |
| 5126 | |
| 5127 | EmulateInstruction::Context context; |
| 5128 | context.type = EmulateInstruction::eContextImmediate; |
| 5129 | context.SetNoArgs (); |
| 5130 | |
| 5131 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 5132 | return false; |
| 5133 | } |
| 5134 | return true; |
| 5135 | } |
| 5136 | |
| 5137 | // This instruction performs a bitwise AND of a register value and an optionally-shifted register value, |
| 5138 | // and writes the result to the destination register. It can optionally update the condition flags |
| 5139 | // based on the result. |
| 5140 | bool |
| 5141 | EmulateInstructionARM::EmulateANDReg (ARMEncoding encoding) |
| 5142 | { |
| 5143 | #if 0 |
| 5144 | // ARM pseudo code... |
| 5145 | if ConditionPassed() then |
| 5146 | EncodingSpecificOperations(); |
| 5147 | (shifted, carry) = Shift_C(R[m], shift_t, shift_n, APSR.C); |
| 5148 | result = R[n] AND shifted; |
| 5149 | if d == 15 then // Can only occur for ARM encoding |
| 5150 | ALUWritePC(result); // setflags is always FALSE here |
| 5151 | else |
| 5152 | R[d] = result; |
| 5153 | if setflags then |
| 5154 | APSR.N = result<31>; |
| 5155 | APSR.Z = IsZeroBit(result); |
| 5156 | APSR.C = carry; |
| 5157 | // APSR.V unchanged |
| 5158 | #endif |
| 5159 | |
| 5160 | bool success = false; |
| 5161 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5162 | if (!success) |
| 5163 | return false; |
| 5164 | |
| 5165 | if (ConditionPassed()) |
| 5166 | { |
| 5167 | uint32_t Rd, Rn, Rm; |
| 5168 | ARM_ShifterType shift_t; |
| 5169 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 5170 | bool setflags; |
| 5171 | uint32_t carry; |
| 5172 | switch (encoding) |
| 5173 | { |
| 5174 | case eEncodingT1: |
| 5175 | Rd = Rn = Bits32(opcode, 2, 0); |
| 5176 | Rm = Bits32(opcode, 5, 3); |
| 5177 | setflags = !InITBlock(); |
| 5178 | shift_t = SRType_LSL; |
| 5179 | shift_n = 0; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 5180 | break; |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5181 | case eEncodingT2: |
| 5182 | Rd = Bits32(opcode, 11, 8); |
| 5183 | Rn = Bits32(opcode, 19, 16); |
| 5184 | Rm = Bits32(opcode, 3, 0); |
| 5185 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 5186 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 5187 | // if Rd == '1111' && S == '1' then SEE TST (register); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5188 | if (Rd == 15 && setflags) |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 5189 | return EmulateTSTReg(eEncodingT2); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5190 | if (Rd == 13 || (Rd == 15 && !setflags) || BadReg(Rn) || BadReg(Rm)) |
| 5191 | return false; |
| 5192 | break; |
| 5193 | case eEncodingA1: |
| 5194 | Rd = Bits32(opcode, 15, 12); |
| 5195 | Rn = Bits32(opcode, 19, 16); |
| 5196 | Rm = Bits32(opcode, 3, 0); |
| 5197 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 5198 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5199 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 5200 | if (Rd == 15 && setflags) |
| 5201 | return false; |
| 5202 | break; |
| 5203 | default: |
| 5204 | return false; |
| 5205 | } |
| 5206 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5207 | // Read the first operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 5208 | uint32_t val1 = ReadCoreReg(Rn, &success); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5209 | if (!success) |
| 5210 | return false; |
| 5211 | |
| 5212 | // Read the second operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 5213 | uint32_t val2 = ReadCoreReg(Rm, &success); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 5214 | if (!success) |
| 5215 | return false; |
| 5216 | |
| 5217 | uint32_t shifted = Shift_C(val2, shift_t, shift_n, APSR_C, carry); |
| 5218 | uint32_t result = val1 & shifted; |
| 5219 | |
| 5220 | EmulateInstruction::Context context; |
| 5221 | context.type = EmulateInstruction::eContextImmediate; |
| 5222 | context.SetNoArgs (); |
| 5223 | |
| 5224 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 5225 | return false; |
| 5226 | } |
| 5227 | return true; |
| 5228 | } |
| 5229 | |
Johnny Chen | b9f02cf | 2011-02-24 01:15:17 +0000 | [diff] [blame] | 5230 | // Bitwise Bit Clear (immediate) performs a bitwise AND of a register value and the complement of an |
| 5231 | // immediate value, and writes the result to the destination register. It can optionally update the |
| 5232 | // condition flags based on the result. |
| 5233 | bool |
| 5234 | EmulateInstructionARM::EmulateBICImm (ARMEncoding encoding) |
| 5235 | { |
| 5236 | #if 0 |
| 5237 | // ARM pseudo code... |
| 5238 | if ConditionPassed() then |
| 5239 | EncodingSpecificOperations(); |
| 5240 | result = R[n] AND NOT(imm32); |
| 5241 | if d == 15 then // Can only occur for ARM encoding |
| 5242 | ALUWritePC(result); // setflags is always FALSE here |
| 5243 | else |
| 5244 | R[d] = result; |
| 5245 | if setflags then |
| 5246 | APSR.N = result<31>; |
| 5247 | APSR.Z = IsZeroBit(result); |
| 5248 | APSR.C = carry; |
| 5249 | // APSR.V unchanged |
| 5250 | #endif |
| 5251 | |
| 5252 | bool success = false; |
| 5253 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5254 | if (!success) |
| 5255 | return false; |
| 5256 | |
| 5257 | if (ConditionPassed()) |
| 5258 | { |
| 5259 | uint32_t Rd, Rn; |
| 5260 | uint32_t imm32; // the immediate value to be bitwise inverted and ANDed to the value obtained from Rn |
| 5261 | bool setflags; |
| 5262 | uint32_t carry; // the carry bit after ARM/Thumb Expand operation |
| 5263 | switch (encoding) |
| 5264 | { |
| 5265 | case eEncodingT1: |
| 5266 | Rd = Bits32(opcode, 11, 8); |
| 5267 | Rn = Bits32(opcode, 19, 16); |
| 5268 | setflags = BitIsSet(opcode, 20); |
| 5269 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ThumbExpandImm(i:imm3:imm8, APSR.C) |
| 5270 | if (BadReg(Rd) || BadReg(Rn)) |
| 5271 | return false; |
| 5272 | break; |
| 5273 | case eEncodingA1: |
| 5274 | Rd = Bits32(opcode, 15, 12); |
| 5275 | Rn = Bits32(opcode, 19, 16); |
| 5276 | setflags = BitIsSet(opcode, 20); |
| 5277 | imm32 = ARMExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ARMExpandImm(imm12, APSR.C) |
| 5278 | // if Rd == ‘1111’ && S == ‘1’ then SEE SUBS PC, LR and related instructions; |
| 5279 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 5280 | if (Rd == 15 && setflags) |
| 5281 | return false; |
| 5282 | break; |
| 5283 | default: |
| 5284 | return false; |
| 5285 | } |
| 5286 | |
| 5287 | // Read the first operand. |
| 5288 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 5289 | if (!success) |
| 5290 | return false; |
| 5291 | |
| 5292 | uint32_t result = val1 & ~imm32; |
| 5293 | |
| 5294 | EmulateInstruction::Context context; |
| 5295 | context.type = EmulateInstruction::eContextImmediate; |
| 5296 | context.SetNoArgs (); |
| 5297 | |
| 5298 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 5299 | return false; |
| 5300 | } |
| 5301 | return true; |
| 5302 | } |
| 5303 | |
| 5304 | // Bitwise Bit Clear (register) performs a bitwise AND of a register value and the complement of an |
| 5305 | // optionally-shifted register value, and writes the result to the destination register. |
| 5306 | // It can optionally update the condition flags based on the result. |
| 5307 | bool |
| 5308 | EmulateInstructionARM::EmulateBICReg (ARMEncoding encoding) |
| 5309 | { |
| 5310 | #if 0 |
| 5311 | // ARM pseudo code... |
| 5312 | if ConditionPassed() then |
| 5313 | EncodingSpecificOperations(); |
| 5314 | (shifted, carry) = Shift_C(R[m], shift_t, shift_n, APSR.C); |
| 5315 | result = R[n] AND NOT(shifted); |
| 5316 | if d == 15 then // Can only occur for ARM encoding |
| 5317 | ALUWritePC(result); // setflags is always FALSE here |
| 5318 | else |
| 5319 | R[d] = result; |
| 5320 | if setflags then |
| 5321 | APSR.N = result<31>; |
| 5322 | APSR.Z = IsZeroBit(result); |
| 5323 | APSR.C = carry; |
| 5324 | // APSR.V unchanged |
| 5325 | #endif |
| 5326 | |
| 5327 | bool success = false; |
| 5328 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5329 | if (!success) |
| 5330 | return false; |
| 5331 | |
| 5332 | if (ConditionPassed()) |
| 5333 | { |
| 5334 | uint32_t Rd, Rn, Rm; |
| 5335 | ARM_ShifterType shift_t; |
| 5336 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 5337 | bool setflags; |
| 5338 | uint32_t carry; |
| 5339 | switch (encoding) |
| 5340 | { |
| 5341 | case eEncodingT1: |
| 5342 | Rd = Rn = Bits32(opcode, 2, 0); |
| 5343 | Rm = Bits32(opcode, 5, 3); |
| 5344 | setflags = !InITBlock(); |
| 5345 | shift_t = SRType_LSL; |
| 5346 | shift_n = 0; |
| 5347 | break; |
| 5348 | case eEncodingT2: |
| 5349 | Rd = Bits32(opcode, 11, 8); |
| 5350 | Rn = Bits32(opcode, 19, 16); |
| 5351 | Rm = Bits32(opcode, 3, 0); |
| 5352 | setflags = BitIsSet(opcode, 20); |
| 5353 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
| 5354 | if (BadReg(Rd) || BadReg(Rn) || BadReg(Rm)) |
| 5355 | return false; |
| 5356 | break; |
| 5357 | case eEncodingA1: |
| 5358 | Rd = Bits32(opcode, 15, 12); |
| 5359 | Rn = Bits32(opcode, 19, 16); |
| 5360 | Rm = Bits32(opcode, 3, 0); |
| 5361 | setflags = BitIsSet(opcode, 20); |
| 5362 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
| 5363 | // if Rd == ‘1111’ && S == ‘1’ then SEE SUBS PC, LR and related instructions; |
| 5364 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 5365 | if (Rd == 15 && setflags) |
| 5366 | return false; |
| 5367 | break; |
| 5368 | default: |
| 5369 | return false; |
| 5370 | } |
| 5371 | |
| 5372 | // Read the first operand. |
| 5373 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 5374 | if (!success) |
| 5375 | return false; |
| 5376 | |
| 5377 | // Read the second operand. |
| 5378 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 5379 | if (!success) |
| 5380 | return false; |
| 5381 | |
| 5382 | uint32_t shifted = Shift_C(val2, shift_t, shift_n, APSR_C, carry); |
| 5383 | uint32_t result = val1 & ~shifted; |
| 5384 | |
| 5385 | EmulateInstruction::Context context; |
| 5386 | context.type = EmulateInstruction::eContextImmediate; |
| 5387 | context.SetNoArgs (); |
| 5388 | |
| 5389 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 5390 | return false; |
| 5391 | } |
| 5392 | return true; |
| 5393 | } |
| 5394 | |
Caroline Tice | 4d729c5 | 2011-02-18 00:55:53 +0000 | [diff] [blame] | 5395 | // 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] | 5396 | // 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] | 5397 | bool |
| 5398 | EmulateInstructionARM::EmulateLDRImmediateARM (ARMEncoding encoding) |
| 5399 | { |
| 5400 | #if 0 |
| 5401 | if ConditionPassed() then |
| 5402 | EncodingSpecificOperations(); |
| 5403 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 5404 | address = if index then offset_addr else R[n]; |
| 5405 | data = MemU[address,4]; |
| 5406 | if wback then R[n] = offset_addr; |
| 5407 | if t == 15 then |
| 5408 | if address<1:0> == ’00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 5409 | elsif UnalignedSupport() || address<1:0> = ’00’ then |
| 5410 | R[t] = data; |
| 5411 | else // Can only apply before ARMv7 |
| 5412 | R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 5413 | #endif |
| 5414 | |
| 5415 | bool success = false; |
| 5416 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5417 | if (!success) |
| 5418 | return false; |
| 5419 | |
| 5420 | if (ConditionPassed ()) |
| 5421 | { |
| 5422 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 5423 | |
| 5424 | uint32_t t; |
| 5425 | uint32_t n; |
| 5426 | uint32_t imm32; |
| 5427 | bool index; |
| 5428 | bool add; |
| 5429 | bool wback; |
| 5430 | |
| 5431 | switch (encoding) |
| 5432 | { |
| 5433 | case eEncodingA1: |
| 5434 | // if Rn == ’1111’ then SEE LDR (literal); |
| 5435 | // if P == ’0’ && W == ’1’ then SEE LDRT; |
| 5436 | // if Rn == ’1101’ && P == ’0’ && U == ’1’ && W == ’0’ && imm12 == ’000000000100’ then SEE POP; |
| 5437 | // t == UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32); |
| 5438 | t = Bits32 (opcode, 15, 12); |
| 5439 | n = Bits32 (opcode, 19, 16); |
| 5440 | imm32 = Bits32 (opcode, 11, 0); |
| 5441 | |
| 5442 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 5443 | index = BitIsSet (opcode, 24); |
| 5444 | add = BitIsSet (opcode, 23); |
| 5445 | wback = (BitIsClear (opcode, 24) || BitIsSet (opcode, 21)); |
| 5446 | |
| 5447 | // if wback && n == t then UNPREDICTABLE; |
| 5448 | if (wback && (n == t)) |
| 5449 | return false; |
| 5450 | |
| 5451 | break; |
| 5452 | |
| 5453 | default: |
| 5454 | return false; |
| 5455 | } |
| 5456 | |
| 5457 | addr_t address; |
| 5458 | addr_t offset_addr; |
| 5459 | addr_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 5460 | if (!success) |
| 5461 | return false; |
| 5462 | |
| 5463 | // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 5464 | if (add) |
| 5465 | offset_addr = base_address + imm32; |
| 5466 | else |
| 5467 | offset_addr = base_address - imm32; |
| 5468 | |
| 5469 | // address = if index then offset_addr else R[n]; |
| 5470 | if (index) |
| 5471 | address = offset_addr; |
| 5472 | else |
| 5473 | address = base_address; |
| 5474 | |
| 5475 | // data = MemU[address,4]; |
| 5476 | |
| 5477 | Register base_reg; |
| 5478 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 5479 | |
| 5480 | EmulateInstruction::Context context; |
| 5481 | context.type = eContextRegisterLoad; |
| 5482 | context.SetRegisterPlusOffset (base_reg, address - base_address); |
| 5483 | |
| 5484 | uint64_t data = MemURead (context, address, addr_byte_size, 0, &success); |
| 5485 | if (!success) |
| 5486 | return false; |
| 5487 | |
| 5488 | // if wback then R[n] = offset_addr; |
| 5489 | if (wback) |
| 5490 | { |
| 5491 | context.type = eContextAdjustBaseRegister; |
| 5492 | context.SetAddress (offset_addr); |
| 5493 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 5494 | return false; |
| 5495 | } |
| 5496 | |
| 5497 | // if t == 15 then |
| 5498 | if (t == 15) |
| 5499 | { |
| 5500 | // if address<1:0> == ’00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 5501 | if (BitIsClear (address, 1) && BitIsClear (address, 0)) |
| 5502 | { |
| 5503 | // LoadWritePC (data); |
| 5504 | context.type = eContextRegisterLoad; |
| 5505 | context.SetRegisterPlusOffset (base_reg, address - base_address); |
| 5506 | LoadWritePC (context, data); |
| 5507 | } |
| 5508 | else |
| 5509 | return false; |
| 5510 | } |
| 5511 | // elsif UnalignedSupport() || address<1:0> = ’00’ then |
| 5512 | else if (UnalignedSupport() || (BitIsClear (address, 1) && BitIsClear (address, 0))) |
| 5513 | { |
| 5514 | // R[t] = data; |
| 5515 | context.type = eContextRegisterLoad; |
| 5516 | context.SetRegisterPlusOffset (base_reg, address - base_address); |
| 5517 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 5518 | return false; |
| 5519 | } |
| 5520 | // else // Can only apply before ARMv7 |
| 5521 | else |
| 5522 | { |
| 5523 | // R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 5524 | data = ROR (data, Bits32 (address, 1, 0)); |
| 5525 | context.type = eContextRegisterLoad; |
| 5526 | context.SetImmediate (data); |
| 5527 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 5528 | return false; |
| 5529 | } |
| 5530 | |
| 5531 | } |
| 5532 | return true; |
| 5533 | } |
| 5534 | |
Caroline Tice | fe47911 | 2011-02-18 18:52:37 +0000 | [diff] [blame] | 5535 | // LDR (register) calculates an address from a base register value and an offset register value, loads a word |
| 5536 | // from memory, and writes it to a resgister. The offset register value can optionally be shifted. |
| 5537 | bool |
| 5538 | EmulateInstructionARM::EmulateLDRRegister (ARMEncoding encoding) |
| 5539 | { |
| 5540 | #if 0 |
| 5541 | if ConditionPassed() then |
| 5542 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 5543 | offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 5544 | offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 5545 | address = if index then offset_addr else R[n]; |
| 5546 | data = MemU[address,4]; |
| 5547 | if wback then R[n] = offset_addr; |
| 5548 | if t == 15 then |
| 5549 | if address<1:0> == ’00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 5550 | elsif UnalignedSupport() || address<1:0> = ’00’ then |
| 5551 | R[t] = data; |
| 5552 | else // Can only apply before ARMv7 |
| 5553 | if CurrentInstrSet() == InstrSet_ARM then |
| 5554 | R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 5555 | else |
| 5556 | R[t] = bits(32) UNKNOWN; |
| 5557 | #endif |
| 5558 | |
| 5559 | bool success = false; |
| 5560 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5561 | if (!success) |
| 5562 | return false; |
| 5563 | |
| 5564 | if (ConditionPassed ()) |
| 5565 | { |
| 5566 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 5567 | |
| 5568 | uint32_t t; |
| 5569 | uint32_t n; |
| 5570 | uint32_t m; |
| 5571 | bool index; |
| 5572 | bool add; |
| 5573 | bool wback; |
| 5574 | ARM_ShifterType shift_t; |
| 5575 | uint32_t shift_n; |
| 5576 | |
| 5577 | switch (encoding) |
| 5578 | { |
| 5579 | case eEncodingT1: |
| 5580 | // if CurrentInstrSet() == InstrSet_ThumbEE then SEE "Modified operation in ThumbEE"; |
| 5581 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 5582 | t = Bits32 (opcode, 2, 0); |
| 5583 | n = Bits32 (opcode, 5, 3); |
| 5584 | m = Bits32 (opcode, 8, 6); |
| 5585 | |
| 5586 | // index = TRUE; add = TRUE; wback = FALSE; |
| 5587 | index = true; |
| 5588 | add = true; |
| 5589 | wback = false; |
| 5590 | |
| 5591 | // (shift_t, shift_n) = (SRType_LSL, 0); |
| 5592 | shift_t = SRType_LSL; |
| 5593 | shift_n = 0; |
| 5594 | |
| 5595 | break; |
| 5596 | |
| 5597 | case eEncodingT2: |
| 5598 | // if Rn == ’1111’ then SEE LDR (literal); |
| 5599 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 5600 | t = Bits32 (opcode, 15, 12); |
| 5601 | n = Bits32 (opcode, 19, 16); |
| 5602 | m = Bits32 (opcode, 3, 0); |
| 5603 | |
| 5604 | // index = TRUE; add = TRUE; wback = FALSE; |
| 5605 | index = true; |
| 5606 | add = true; |
| 5607 | wback = false; |
| 5608 | |
| 5609 | // (shift_t, shift_n) = (SRType_LSL, UInt(imm2)); |
| 5610 | shift_t = SRType_LSL; |
| 5611 | shift_n = Bits32 (opcode, 5, 4); |
| 5612 | |
| 5613 | // if BadReg(m) then UNPREDICTABLE; |
| 5614 | if (BadReg (m)) |
| 5615 | return false; |
| 5616 | |
| 5617 | // if t == 15 && InITBlock() && !LastInITBlock() then UNPREDICTABLE; |
| 5618 | if ((t == 15) && InITBlock() && !LastInITBlock()) |
| 5619 | return false; |
| 5620 | |
| 5621 | break; |
| 5622 | |
| 5623 | case eEncodingA1: |
| 5624 | { |
| 5625 | // if P == ’0’ && W == ’1’ then SEE LDRT; |
| 5626 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 5627 | t = Bits32 (opcode, 15, 12); |
| 5628 | n = Bits32 (opcode, 19, 16); |
| 5629 | m = Bits32 (opcode, 3, 0); |
| 5630 | |
| 5631 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 5632 | index = BitIsSet (opcode, 24); |
| 5633 | add = BitIsSet (opcode, 23); |
| 5634 | wback = (BitIsClear (opcode, 24) || BitIsSet (opcode, 21)); |
| 5635 | |
| 5636 | // (shift_t, shift_n) = DecodeImmShift(type, imm5); |
| 5637 | uint32_t type = Bits32 (opcode, 6, 5); |
| 5638 | uint32_t imm5 = Bits32 (opcode, 11, 7); |
| 5639 | shift_n = DecodeImmShift (type, imm5, shift_t); |
| 5640 | |
| 5641 | // if m == 15 then UNPREDICTABLE; |
| 5642 | if (m == 15) |
| 5643 | return false; |
| 5644 | |
| 5645 | // if wback && (n == 15 || n == t) then UNPREDICTABLE; |
| 5646 | if (wback && ((n == 15) || (n == t))) |
| 5647 | return false; |
| 5648 | } |
| 5649 | break; |
| 5650 | |
| 5651 | |
| 5652 | default: |
| 5653 | return false; |
| 5654 | } |
| 5655 | |
| 5656 | uint32_t Rm = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 5657 | if (!success) |
| 5658 | return false; |
| 5659 | |
| 5660 | uint32_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 5661 | if (!success) |
| 5662 | return false; |
| 5663 | |
| 5664 | addr_t offset_addr; |
| 5665 | addr_t address; |
| 5666 | |
| 5667 | // offset = Shift(R[m], shift_t, shift_n, APSR.C); -- Note "The APSR is an application level alias for the CPSR". |
Johnny Chen | 90e607b | 2011-02-23 00:07:09 +0000 | [diff] [blame] | 5668 | addr_t offset = Shift (Rm, shift_t, shift_n, Bit32 (m_inst_cpsr, APSR_C)); |
Caroline Tice | fe47911 | 2011-02-18 18:52:37 +0000 | [diff] [blame] | 5669 | |
| 5670 | // offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 5671 | if (add) |
| 5672 | offset_addr = Rn + offset; |
| 5673 | else |
| 5674 | offset_addr = Rn - offset; |
| 5675 | |
| 5676 | // address = if index then offset_addr else R[n]; |
| 5677 | if (index) |
| 5678 | address = offset_addr; |
| 5679 | else |
| 5680 | address = Rn; |
| 5681 | |
| 5682 | // data = MemU[address,4]; |
| 5683 | Register base_reg; |
| 5684 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 5685 | |
| 5686 | EmulateInstruction::Context context; |
| 5687 | context.type = eContextRegisterLoad; |
| 5688 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 5689 | |
| 5690 | uint64_t data = MemURead (context, address, addr_byte_size, 0, &success); |
| 5691 | if (!success) |
| 5692 | return false; |
| 5693 | |
| 5694 | // if wback then R[n] = offset_addr; |
| 5695 | if (wback) |
| 5696 | { |
| 5697 | context.type = eContextAdjustBaseRegister; |
| 5698 | context.SetAddress (offset_addr); |
| 5699 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 5700 | return false; |
| 5701 | } |
| 5702 | |
| 5703 | // if t == 15 then |
| 5704 | if (t == 15) |
| 5705 | { |
| 5706 | // if address<1:0> == ’00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 5707 | if (BitIsClear (address, 1) && BitIsClear (address, 0)) |
| 5708 | { |
| 5709 | context.type = eContextRegisterLoad; |
| 5710 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 5711 | LoadWritePC (context, data); |
| 5712 | } |
| 5713 | else |
| 5714 | return false; |
| 5715 | } |
| 5716 | // elsif UnalignedSupport() || address<1:0> = ’00’ then |
| 5717 | else if (UnalignedSupport () || (BitIsClear (address, 1) && BitIsClear (address, 0))) |
| 5718 | { |
| 5719 | // R[t] = data; |
| 5720 | context.type = eContextRegisterLoad; |
| 5721 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 5722 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 5723 | return false; |
| 5724 | } |
| 5725 | else // Can only apply before ARMv7 |
| 5726 | { |
| 5727 | // if CurrentInstrSet() == InstrSet_ARM then |
| 5728 | if (CurrentInstrSet () == eModeARM) |
| 5729 | { |
| 5730 | // R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 5731 | data = ROR (data, Bits32 (address, 1, 0)); |
| 5732 | context.type = eContextRegisterLoad; |
| 5733 | context.SetImmediate (data); |
| 5734 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 5735 | return false; |
| 5736 | } |
| 5737 | else |
| 5738 | { |
| 5739 | // R[t] = bits(32) UNKNOWN; |
| 5740 | WriteBits32Unknown (t); |
| 5741 | } |
| 5742 | } |
| 5743 | } |
| 5744 | return true; |
| 5745 | } |
Caroline Tice | 21b604b | 2011-02-18 21:06:04 +0000 | [diff] [blame] | 5746 | |
| 5747 | // LDRB (immediate, Thumb) |
| 5748 | bool |
| 5749 | EmulateInstructionARM::EmulateLDRBImmediate (ARMEncoding encoding) |
| 5750 | { |
| 5751 | #if 0 |
| 5752 | if ConditionPassed() then |
| 5753 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 5754 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 5755 | address = if index then offset_addr else R[n]; |
| 5756 | R[t] = ZeroExtend(MemU[address,1], 32); |
| 5757 | if wback then R[n] = offset_addr; |
| 5758 | #endif |
| 5759 | |
| 5760 | bool success = false; |
| 5761 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5762 | if (!success) |
| 5763 | return false; |
| 5764 | |
| 5765 | if (ConditionPassed ()) |
| 5766 | { |
| 5767 | uint32_t t; |
| 5768 | uint32_t n; |
| 5769 | uint32_t imm32; |
| 5770 | bool index; |
| 5771 | bool add; |
| 5772 | bool wback; |
| 5773 | |
| 5774 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 5775 | switch (encoding) |
| 5776 | { |
| 5777 | case eEncodingT1: |
| 5778 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm5, 32); |
| 5779 | t = Bits32 (opcode, 2, 0); |
| 5780 | n = Bits32 (opcode, 5, 3); |
| 5781 | imm32 = Bits32 (opcode, 10, 6); |
| 5782 | |
| 5783 | // index = TRUE; add = TRUE; wback = FALSE; |
| 5784 | index = true; |
| 5785 | add = true; |
| 5786 | wback= false; |
| 5787 | |
| 5788 | break; |
| 5789 | |
| 5790 | case eEncodingT2: |
| 5791 | // if Rt == ’1111’ then SEE PLD; |
| 5792 | // if Rn == ’1111’ then SEE LDRB (literal); |
| 5793 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32); |
| 5794 | t = Bits32 (opcode, 15, 12); |
| 5795 | n = Bits32 (opcode, 19, 16); |
| 5796 | imm32 = Bits32 (opcode, 11, 0); |
| 5797 | |
| 5798 | // index = TRUE; add = TRUE; wback = FALSE; |
| 5799 | index = true; |
| 5800 | add = true; |
| 5801 | wback = false; |
| 5802 | |
| 5803 | // if t == 13 then UNPREDICTABLE; |
| 5804 | if (t == 13) |
| 5805 | return false; |
| 5806 | |
| 5807 | break; |
| 5808 | |
| 5809 | case eEncodingT3: |
| 5810 | // if Rt == ’1111’ && P == ’1’ && U == ’0’ && W == ’0’ then SEE PLD; |
| 5811 | // if Rn == ’1111’ then SEE LDRB (literal); |
| 5812 | // if P == ’1’ && U == ’1’ && W == ’0’ then SEE LDRBT; |
| 5813 | // if P == ’0’ && W == ’0’ then UNDEFINED; |
| 5814 | if (BitIsClear (opcode, 10) && BitIsClear (opcode, 8)) |
| 5815 | return false; |
| 5816 | |
| 5817 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm8, 32); |
| 5818 | t = Bits32 (opcode, 15, 12); |
| 5819 | n = Bits32 (opcode, 19, 16); |
| 5820 | imm32 = Bits32 (opcode, 7, 0); |
| 5821 | |
| 5822 | // index = (P == ’1’); add = (U == ’1’); wback = (W == ’1’); |
| 5823 | index = BitIsSet (opcode, 10); |
| 5824 | add = BitIsSet (opcode, 9); |
| 5825 | wback = BitIsSet (opcode, 8); |
| 5826 | |
| 5827 | // if BadReg(t) || (wback && n == t) then UNPREDICTABLE; |
| 5828 | if (BadReg (t) || (wback && (n == t))) |
| 5829 | return false; |
| 5830 | |
| 5831 | break; |
| 5832 | |
| 5833 | default: |
| 5834 | return false; |
| 5835 | } |
| 5836 | |
| 5837 | uint32_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 5838 | if (!success) |
| 5839 | return false; |
| 5840 | |
| 5841 | addr_t address; |
| 5842 | addr_t offset_addr; |
| 5843 | |
| 5844 | // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 5845 | if (add) |
| 5846 | offset_addr = Rn + imm32; |
| 5847 | else |
| 5848 | offset_addr = Rn - imm32; |
| 5849 | |
| 5850 | // address = if index then offset_addr else R[n]; |
| 5851 | if (index) |
| 5852 | address = offset_addr; |
| 5853 | else |
| 5854 | address = Rn; |
| 5855 | |
| 5856 | // R[t] = ZeroExtend(MemU[address,1], 32); |
| 5857 | Register base_reg; |
| 5858 | Register data_reg; |
| 5859 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 5860 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + t); |
| 5861 | |
| 5862 | EmulateInstruction::Context context; |
| 5863 | context.type = eContextRegisterLoad; |
| 5864 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, address - Rn); |
| 5865 | |
| 5866 | uint64_t data = MemURead (context, address, 1, 0, &success); |
| 5867 | if (!success) |
| 5868 | return false; |
| 5869 | |
| 5870 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 5871 | return false; |
| 5872 | |
| 5873 | // if wback then R[n] = offset_addr; |
| 5874 | if (wback) |
| 5875 | { |
| 5876 | context.type = eContextAdjustBaseRegister; |
| 5877 | context.SetAddress (offset_addr); |
| 5878 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 5879 | return false; |
| 5880 | } |
| 5881 | } |
| 5882 | return true; |
| 5883 | } |
Caroline Tice | f55261f | 2011-02-18 22:24:22 +0000 | [diff] [blame] | 5884 | |
| 5885 | // LDRB (literal) calculates an address from the PC value and an immediate offset, loads a byte from memory, |
| 5886 | // zero-extends it to form a 32-bit word and writes it to a register. |
| 5887 | bool |
| 5888 | EmulateInstructionARM::EmulateLDRBLiteral (ARMEncoding encoding) |
| 5889 | { |
| 5890 | #if 0 |
| 5891 | if ConditionPassed() then |
| 5892 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 5893 | base = Align(PC,4); |
| 5894 | address = if add then (base + imm32) else (base - imm32); |
| 5895 | R[t] = ZeroExtend(MemU[address,1], 32); |
| 5896 | #endif |
| 5897 | |
| 5898 | bool success = false; |
| 5899 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5900 | if (!success) |
| 5901 | return false; |
| 5902 | |
| 5903 | if (ConditionPassed ()) |
| 5904 | { |
| 5905 | uint32_t t; |
| 5906 | uint32_t imm32; |
| 5907 | bool add; |
| 5908 | switch (encoding) |
| 5909 | { |
| 5910 | case eEncodingT1: |
| 5911 | // if Rt == ’1111’ then SEE PLD; |
| 5912 | // t = UInt(Rt); imm32 = ZeroExtend(imm12, 32); add = (U == ’1’); |
| 5913 | t = Bits32 (opcode, 15, 12); |
| 5914 | imm32 = Bits32 (opcode, 11, 0); |
| 5915 | add = BitIsSet (opcode, 23); |
| 5916 | |
| 5917 | // if t == 13 then UNPREDICTABLE; |
| 5918 | if (t == 13) |
| 5919 | return false; |
| 5920 | |
| 5921 | break; |
| 5922 | |
| 5923 | case eEncodingA1: |
| 5924 | // t == UInt(Rt); imm32 = ZeroExtend(imm12, 32); add = (U == ’1’); |
| 5925 | t = Bits32 (opcode, 15, 12); |
| 5926 | imm32 = Bits32 (opcode, 11, 0); |
| 5927 | add = BitIsSet (opcode, 23); |
| 5928 | |
| 5929 | // if t == 15 then UNPREDICTABLE; |
| 5930 | if (t == 15) |
| 5931 | return false; |
| 5932 | break; |
| 5933 | |
| 5934 | default: |
| 5935 | return false; |
| 5936 | } |
| 5937 | |
| 5938 | // base = Align(PC,4); |
| 5939 | uint32_t pc_val = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 5940 | if (!success) |
| 5941 | return false; |
| 5942 | |
| 5943 | uint32_t base = AlignPC (pc_val); |
| 5944 | |
| 5945 | addr_t address; |
| 5946 | // address = if add then (base + imm32) else (base - imm32); |
| 5947 | if (add) |
| 5948 | address = base + imm32; |
| 5949 | else |
| 5950 | address = base - imm32; |
| 5951 | |
| 5952 | // R[t] = ZeroExtend(MemU[address,1], 32); |
| 5953 | EmulateInstruction::Context context; |
| 5954 | context.type = eContextRelativeBranchImmediate; |
| 5955 | context.SetImmediate (address - base); |
| 5956 | |
| 5957 | uint64_t data = MemURead (context, address, 1, 0, &success); |
| 5958 | if (!success) |
| 5959 | return false; |
| 5960 | |
| 5961 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 5962 | return false; |
| 5963 | } |
| 5964 | return true; |
| 5965 | } |
Caroline Tice | 30fec12 | 2011-02-18 23:52:21 +0000 | [diff] [blame] | 5966 | |
| 5967 | // LDRB (register) calculates an address from a base register value and an offset rigister value, loads a byte from |
| 5968 | // memory, zero-extends it to form a 32-bit word, and writes it to a register. The offset register value can |
| 5969 | // optionally be shifted. |
| 5970 | bool |
| 5971 | EmulateInstructionARM::EmulateLDRBRegister (ARMEncoding encoding) |
| 5972 | { |
| 5973 | #if 0 |
| 5974 | if ConditionPassed() then |
| 5975 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 5976 | offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 5977 | offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 5978 | address = if index then offset_addr else R[n]; |
| 5979 | R[t] = ZeroExtend(MemU[address,1],32); |
| 5980 | if wback then R[n] = offset_addr; |
| 5981 | #endif |
| 5982 | |
| 5983 | bool success = false; |
| 5984 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5985 | if (!success) |
| 5986 | return false; |
| 5987 | |
| 5988 | if (ConditionPassed ()) |
| 5989 | { |
| 5990 | uint32_t t; |
| 5991 | uint32_t n; |
| 5992 | uint32_t m; |
| 5993 | bool index; |
| 5994 | bool add; |
| 5995 | bool wback; |
| 5996 | ARM_ShifterType shift_t; |
| 5997 | uint32_t shift_n; |
| 5998 | |
| 5999 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 6000 | switch (encoding) |
| 6001 | { |
| 6002 | case eEncodingT1: |
| 6003 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 6004 | t = Bits32 (opcode, 2, 0); |
| 6005 | n = Bits32 (opcode, 5, 3); |
| 6006 | m = Bits32 (opcode, 8, 6); |
| 6007 | |
| 6008 | // index = TRUE; add = TRUE; wback = FALSE; |
| 6009 | index = true; |
| 6010 | add = true; |
| 6011 | wback = false; |
| 6012 | |
| 6013 | // (shift_t, shift_n) = (SRType_LSL, 0); |
| 6014 | shift_t = SRType_LSL; |
| 6015 | shift_n = 0; |
| 6016 | break; |
| 6017 | |
| 6018 | case eEncodingT2: |
| 6019 | // if Rt == ’1111’ then SEE PLD; |
| 6020 | // if Rn == ’1111’ then SEE LDRB (literal); |
| 6021 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 6022 | t = Bits32 (opcode, 15, 12); |
| 6023 | n = Bits32 (opcode, 19, 16); |
| 6024 | m = Bits32 (opcode, 3, 0); |
| 6025 | |
| 6026 | // index = TRUE; add = TRUE; wback = FALSE; |
| 6027 | index = true; |
| 6028 | add = true; |
| 6029 | wback = false; |
| 6030 | |
| 6031 | // (shift_t, shift_n) = (SRType_LSL, UInt(imm2)); |
| 6032 | shift_t = SRType_LSL; |
| 6033 | shift_n = Bits32 (opcode, 5, 4); |
| 6034 | |
| 6035 | // if t == 13 || BadReg(m) then UNPREDICTABLE; |
| 6036 | if ((t == 13) || BadReg (m)) |
| 6037 | return false; |
| 6038 | break; |
| 6039 | |
| 6040 | case eEncodingA1: |
| 6041 | { |
| 6042 | // if P == ’0’ && W == ’1’ then SEE LDRBT; |
| 6043 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 6044 | t = Bits32 (opcode, 15, 12); |
| 6045 | n = Bits32 (opcode, 19, 16); |
| 6046 | m = Bits32 (opcode, 3, 0); |
| 6047 | |
| 6048 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 6049 | index = BitIsSet (opcode, 24); |
| 6050 | add = BitIsSet (opcode, 23); |
| 6051 | wback = (BitIsClear (opcode, 24) || BitIsSet (opcode, 21)); |
| 6052 | |
| 6053 | // (shift_t, shift_n) = DecodeImmShift(type, imm5); |
| 6054 | uint32_t type = Bits32 (opcode, 6, 5); |
| 6055 | uint32_t imm5 = Bits32 (opcode, 11, 7); |
| 6056 | shift_n = DecodeImmShift (type, imm5, shift_t); |
| 6057 | |
| 6058 | // if t == 15 || m == 15 then UNPREDICTABLE; |
| 6059 | if ((t == 15) || (m == 15)) |
| 6060 | return false; |
| 6061 | |
| 6062 | // if wback && (n == 15 || n == t) then UNPREDICTABLE; |
| 6063 | if (wback && ((n == 15) || (n == t))) |
| 6064 | return false; |
| 6065 | } |
| 6066 | break; |
| 6067 | |
| 6068 | default: |
| 6069 | return false; |
| 6070 | } |
| 6071 | |
| 6072 | addr_t offset_addr; |
| 6073 | addr_t address; |
| 6074 | |
| 6075 | // offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 6076 | uint32_t Rm = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 6077 | if (!success) |
| 6078 | return false; |
| 6079 | |
| 6080 | addr_t offset = Shift (Rm, shift_t, shift_n, APSR_C); |
| 6081 | |
| 6082 | // offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 6083 | uint32_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 6084 | if (!success) |
| 6085 | return false; |
| 6086 | |
| 6087 | if (add) |
| 6088 | offset_addr = Rn + offset; |
| 6089 | else |
| 6090 | offset_addr = Rn - offset; |
| 6091 | |
| 6092 | // address = if index then offset_addr else R[n]; |
| 6093 | if (index) |
| 6094 | address = offset_addr; |
| 6095 | else |
| 6096 | address = Rn; |
| 6097 | |
| 6098 | // R[t] = ZeroExtend(MemU[address,1],32); |
| 6099 | Register base_reg; |
| 6100 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 6101 | |
| 6102 | EmulateInstruction::Context context; |
| 6103 | context.type = eContextRegisterLoad; |
| 6104 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 6105 | |
| 6106 | uint64_t data = MemURead (context, address, 1, 0, &success); |
| 6107 | if (!success) |
| 6108 | return false; |
| 6109 | |
| 6110 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 6111 | return false; |
| 6112 | |
| 6113 | // if wback then R[n] = offset_addr; |
| 6114 | if (wback) |
| 6115 | { |
| 6116 | context.type = eContextAdjustBaseRegister; |
| 6117 | context.SetAddress (offset_addr); |
| 6118 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 6119 | return false; |
| 6120 | } |
| 6121 | } |
| 6122 | return true; |
| 6123 | } |
Caroline Tice | 0491b3b | 2011-02-28 22:39:58 +0000 | [diff] [blame] | 6124 | |
| 6125 | // LDRH (immediate, Thumb) calculates an address from a base register value and an immediate offset, loads a |
| 6126 | // halfword from memory, zero-extends it to form a 32-bit word, and writes it to a register. It can use offset, |
| 6127 | // post-indexed, or pre-indexed addressing. |
| 6128 | bool |
| 6129 | EmulateInstructionARM::EmulateLDRHImmediate (ARMEncoding encoding) |
| 6130 | { |
| 6131 | #if 0 |
| 6132 | if ConditionPassed() then |
| 6133 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 6134 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 6135 | address = if index then offset_addr else R[n]; |
| 6136 | data = MemU[address,2]; |
| 6137 | if wback then R[n] = offset_addr; |
| 6138 | if UnalignedSupport() || address<0> = ’0’ then |
| 6139 | R[t] = ZeroExtend(data, 32); |
| 6140 | else // Can only apply before ARMv7 |
| 6141 | R[t] = bits(32) UNKNOWN; |
| 6142 | #endif |
| 6143 | |
| 6144 | |
| 6145 | bool success = false; |
| 6146 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 6147 | if (!success) |
| 6148 | return false; |
| 6149 | |
| 6150 | if (ConditionPassed()) |
| 6151 | { |
| 6152 | uint32_t t; |
| 6153 | uint32_t n; |
| 6154 | uint32_t imm32; |
| 6155 | bool index; |
| 6156 | bool add; |
| 6157 | bool wback; |
| 6158 | |
| 6159 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 6160 | switch (encoding) |
| 6161 | { |
| 6162 | case eEncodingT1: |
| 6163 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm5:’0’, 32); |
| 6164 | t = Bits32 (opcode, 2, 0); |
| 6165 | n = Bits32 (opcode, 5, 3); |
| 6166 | imm32 = Bits32 (opcode, 10, 6) << 1; |
| 6167 | |
| 6168 | // index = TRUE; add = TRUE; wback = FALSE; |
| 6169 | index = true; |
| 6170 | add = true; |
| 6171 | wback = false; |
| 6172 | |
| 6173 | break; |
| 6174 | |
| 6175 | case eEncodingT2: |
| 6176 | // if Rt == ’1111’ then SEE "Unallocated memory hints"; |
| 6177 | // if Rn == ’1111’ then SEE LDRH (literal); |
| 6178 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32); |
| 6179 | t = Bits32 (opcode, 15, 12); |
| 6180 | n = Bits32 (opcode, 19, 16); |
| 6181 | imm32 = Bits32 (opcode, 11, 0); |
| 6182 | |
| 6183 | // index = TRUE; add = TRUE; wback = FALSE; |
| 6184 | index = true; |
| 6185 | add = true; |
| 6186 | wback = false; |
| 6187 | |
| 6188 | // if t == 13 then UNPREDICTABLE; |
| 6189 | if (t == 13) |
| 6190 | return false; |
| 6191 | break; |
| 6192 | |
| 6193 | case eEncodingT3: |
| 6194 | // if Rn == ’1111’ then SEE LDRH (literal); |
| 6195 | // if Rt == ’1111’ && P == ’1’ && U == ’0’ && W == ’0’ then SEE "Unallocated memory hints"; |
| 6196 | // if P == ’1’ && U == ’1’ && W == ’0’ then SEE LDRHT; |
| 6197 | // if P == ’0’ && W == ’0’ then UNDEFINED; |
| 6198 | if (BitIsClear (opcode, 10) && BitIsClear (opcode, 8)) |
| 6199 | return false; |
| 6200 | |
| 6201 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm8, 32); |
| 6202 | t = Bits32 (opcode, 15, 12); |
| 6203 | n = Bits32 (opcode, 19, 16); |
| 6204 | imm32 = Bits32 (opcode, 7, 0); |
| 6205 | |
| 6206 | // index = (P == ’1’); add = (U == ’1’); wback = (W == ’1’); |
| 6207 | index = BitIsSet (opcode, 10); |
| 6208 | add = BitIsSet (opcode, 9); |
| 6209 | wback = BitIsSet (opcode, 8); |
| 6210 | |
| 6211 | // if BadReg(t) || (wback && n == t) then UNPREDICTABLE; |
| 6212 | if (BadReg (t) || (wback && (n == t))) |
| 6213 | return false; |
| 6214 | break; |
| 6215 | |
| 6216 | default: |
| 6217 | return false; |
| 6218 | } |
| 6219 | |
| 6220 | // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 6221 | uint32_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 6222 | if (!success) |
| 6223 | return false; |
| 6224 | |
| 6225 | addr_t offset_addr; |
| 6226 | addr_t address; |
| 6227 | |
| 6228 | if (add) |
| 6229 | offset_addr = Rn + imm32; |
| 6230 | else |
| 6231 | offset_addr = Rn - imm32; |
| 6232 | |
| 6233 | // address = if index then offset_addr else R[n]; |
| 6234 | if (index) |
| 6235 | address = offset_addr; |
| 6236 | else |
| 6237 | address = Rn; |
| 6238 | |
| 6239 | // data = MemU[address,2]; |
| 6240 | Register base_reg; |
| 6241 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 6242 | |
| 6243 | EmulateInstruction::Context context; |
| 6244 | context.type = eContextRegisterLoad; |
| 6245 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 6246 | |
| 6247 | uint64_t data = MemURead (context, address, 2, 0, &success); |
| 6248 | if (!success) |
| 6249 | return false; |
| 6250 | |
| 6251 | // if wback then R[n] = offset_addr; |
| 6252 | if (wback) |
| 6253 | { |
| 6254 | context.type = eContextAdjustBaseRegister; |
| 6255 | context.SetAddress (offset_addr); |
| 6256 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 6257 | return false; |
| 6258 | } |
| 6259 | |
| 6260 | // if UnalignedSupport() || address<0> = ’0’ then |
| 6261 | if (UnalignedSupport () || BitIsClear (address, 0)) |
| 6262 | { |
| 6263 | // R[t] = ZeroExtend(data, 32); |
| 6264 | context.type = eContextRegisterLoad; |
| 6265 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 6266 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 6267 | return false; |
| 6268 | } |
| 6269 | else // Can only apply before ARMv7 |
| 6270 | { |
| 6271 | // R[t] = bits(32) UNKNOWN; |
| 6272 | WriteBits32Unknown (t); |
| 6273 | } |
| 6274 | } |
| 6275 | return true; |
| 6276 | } |
Caroline Tice | fe47911 | 2011-02-18 18:52:37 +0000 | [diff] [blame] | 6277 | |
Caroline Tice | 952b538 | 2011-02-28 23:15:24 +0000 | [diff] [blame] | 6278 | // LDRH (literal) caculates an address from the PC value and an immediate offset, loads a halfword from memory, |
| 6279 | // zero-extends it to form a 32-bit word, and writes it to a register. |
| 6280 | bool |
| 6281 | EmulateInstructionARM::EmulateLDRHLiteral (ARMEncoding encoding) |
| 6282 | { |
| 6283 | #if 0 |
| 6284 | if ConditionPassed() then |
| 6285 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 6286 | base = Align(PC,4); |
| 6287 | address = if add then (base + imm32) else (base - imm32); |
| 6288 | data = MemU[address,2]; |
| 6289 | if UnalignedSupport() || address<0> = ’0’ then |
| 6290 | R[t] = ZeroExtend(data, 32); |
| 6291 | else // Can only apply before ARMv7 |
| 6292 | R[t] = bits(32) UNKNOWN; |
| 6293 | #endif |
Caroline Tice | 0e6bc95 | 2011-03-01 18:00:42 +0000 | [diff] [blame] | 6294 | |
Caroline Tice | 952b538 | 2011-02-28 23:15:24 +0000 | [diff] [blame] | 6295 | bool success = false; |
| 6296 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 6297 | if (!success) |
| 6298 | return false; |
| 6299 | |
| 6300 | if (ConditionPassed()) |
| 6301 | { |
| 6302 | uint32_t t; |
| 6303 | uint32_t imm32; |
| 6304 | bool add; |
| 6305 | |
| 6306 | // EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 6307 | switch (encoding) |
| 6308 | { |
| 6309 | case eEncodingT1: |
| 6310 | // if Rt == ’1111’ then SEE "Unallocated memory hints"; |
| 6311 | // t = UInt(Rt); imm32 = ZeroExtend(imm12, 32); add = (U == ’1’); |
| 6312 | t = Bits32 (opcode, 15, 12); |
| 6313 | imm32 = Bits32 (opcode, 11, 0); |
| 6314 | add = BitIsSet (opcode, 23); |
| 6315 | |
| 6316 | // if t == 13 then UNPREDICTABLE; |
| 6317 | if (t == 13) |
| 6318 | return false; |
| 6319 | |
| 6320 | break; |
| 6321 | |
| 6322 | case eEncodingA1: |
| 6323 | { |
| 6324 | uint32_t imm4H = Bits32 (opcode, 11, 8); |
| 6325 | uint32_t imm4L = Bits32 (opcode, 3, 0); |
| 6326 | |
| 6327 | // t == UInt(Rt); imm32 = ZeroExtend(imm4H:imm4L, 32); add = (U == ’1’); |
| 6328 | t = Bits32 (opcode, 15, 12); |
Caroline Tice | 40b1a6c | 2011-03-03 00:07:02 +0000 | [diff] [blame] | 6329 | imm32 = (imm4H << 4) | imm4L; |
Caroline Tice | 952b538 | 2011-02-28 23:15:24 +0000 | [diff] [blame] | 6330 | add = BitIsSet (opcode, 23); |
| 6331 | |
| 6332 | // if t == 15 then UNPREDICTABLE; |
| 6333 | if (t == 15) |
| 6334 | return false; |
| 6335 | break; |
| 6336 | } |
| 6337 | |
| 6338 | default: |
| 6339 | return false; |
| 6340 | } |
| 6341 | |
| 6342 | // base = Align(PC,4); |
| 6343 | uint64_t pc_value = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 6344 | if (!success) |
| 6345 | return false; |
| 6346 | |
| 6347 | addr_t base = AlignPC (pc_value); |
| 6348 | addr_t address; |
| 6349 | |
| 6350 | // address = if add then (base + imm32) else (base - imm32); |
| 6351 | if (add) |
| 6352 | address = base + imm32; |
| 6353 | else |
| 6354 | address = base - imm32; |
| 6355 | |
| 6356 | // data = MemU[address,2]; |
| 6357 | Register base_reg; |
| 6358 | base_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); |
| 6359 | |
| 6360 | EmulateInstruction::Context context; |
| 6361 | context.type = eContextRegisterLoad; |
| 6362 | context.SetRegisterPlusOffset (base_reg, address - base); |
| 6363 | |
| 6364 | uint64_t data = MemURead (context, address, 2, 0, &success); |
| 6365 | if (!success) |
| 6366 | return false; |
| 6367 | |
| 6368 | |
| 6369 | // if UnalignedSupport() || address<0> = ’0’ then |
| 6370 | if (UnalignedSupport () || BitIsClear (address, 0)) |
| 6371 | { |
| 6372 | // R[t] = ZeroExtend(data, 32); |
| 6373 | context.type = eContextRegisterLoad; |
| 6374 | context.SetRegisterPlusOffset (base_reg, address - base); |
| 6375 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 6376 | return false; |
| 6377 | |
| 6378 | } |
| 6379 | else // Can only apply before ARMv7 |
| 6380 | { |
| 6381 | // R[t] = bits(32) UNKNOWN; |
| 6382 | WriteBits32Unknown (t); |
| 6383 | } |
| 6384 | } |
| 6385 | return true; |
| 6386 | } |
| 6387 | |
Caroline Tice | 0e6bc95 | 2011-03-01 18:00:42 +0000 | [diff] [blame] | 6388 | // LDRH (literal) calculates an address from a base register value and an offset register value, loads a halfword |
| 6389 | // from memory, zero-extends it to form a 32-bit word, and writes it to a register. The offset register value can |
| 6390 | // be shifted left by 0, 1, 2, or 3 bits. |
| 6391 | bool |
| 6392 | EmulateInstructionARM::EmulateLDRHRegister (ARMEncoding encoding) |
| 6393 | { |
| 6394 | #if 0 |
| 6395 | if ConditionPassed() then |
| 6396 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 6397 | offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 6398 | offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 6399 | address = if index then offset_addr else R[n]; |
| 6400 | data = MemU[address,2]; |
| 6401 | if wback then R[n] = offset_addr; |
| 6402 | if UnalignedSupport() || address<0> = ’0’ then |
| 6403 | R[t] = ZeroExtend(data, 32); |
| 6404 | else // Can only apply before ARMv7 |
| 6405 | R[t] = bits(32) UNKNOWN; |
| 6406 | #endif |
| 6407 | |
| 6408 | bool success = false; |
| 6409 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 6410 | if (!success) |
| 6411 | return false; |
| 6412 | |
| 6413 | if (ConditionPassed()) |
| 6414 | { |
| 6415 | uint32_t t; |
| 6416 | uint32_t n; |
| 6417 | uint32_t m; |
| 6418 | bool index; |
| 6419 | bool add; |
| 6420 | bool wback; |
| 6421 | ARM_ShifterType shift_t; |
| 6422 | uint32_t shift_n; |
| 6423 | |
| 6424 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 6425 | switch (encoding) |
| 6426 | { |
| 6427 | case eEncodingT1: |
| 6428 | // if CurrentInstrSet() == InstrSet_ThumbEE then SEE "Modified operation in ThumbEE"; |
| 6429 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 6430 | t = Bits32 (opcode, 2, 0); |
| 6431 | n = Bits32 (opcode, 5, 3); |
| 6432 | m = Bits32 (opcode, 8, 6); |
| 6433 | |
| 6434 | // index = TRUE; add = TRUE; wback = FALSE; |
| 6435 | index = true; |
| 6436 | add = true; |
| 6437 | wback = false; |
| 6438 | |
| 6439 | // (shift_t, shift_n) = (SRType_LSL, 0); |
| 6440 | shift_t = SRType_LSL; |
| 6441 | shift_n = 0; |
| 6442 | |
| 6443 | break; |
| 6444 | |
| 6445 | case eEncodingT2: |
| 6446 | // if Rn == ’1111’ then SEE LDRH (literal); |
| 6447 | // if Rt == ’1111’ then SEE "Unallocated memory hints"; |
| 6448 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 6449 | t = Bits32 (opcode, 15, 12); |
| 6450 | n = Bits32 (opcode, 19, 16); |
| 6451 | m = Bits32 (opcode, 3, 0); |
| 6452 | |
| 6453 | // index = TRUE; add = TRUE; wback = FALSE; |
| 6454 | index = true; |
| 6455 | add = true; |
| 6456 | wback = false; |
| 6457 | |
| 6458 | // (shift_t, shift_n) = (SRType_LSL, UInt(imm2)); |
| 6459 | shift_t = SRType_LSL; |
| 6460 | shift_n = Bits32 (opcode, 5, 4); |
| 6461 | |
| 6462 | // if t == 13 || BadReg(m) then UNPREDICTABLE; |
| 6463 | if ((t == 13) || BadReg (m)) |
| 6464 | return false; |
| 6465 | break; |
| 6466 | |
| 6467 | case eEncodingA1: |
| 6468 | // if P == ’0’ && W == ’1’ then SEE LDRHT; |
| 6469 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 6470 | t = Bits32 (opcode, 15, 12); |
| 6471 | n = Bits32 (opcode, 19, 16); |
| 6472 | m = Bits32 (opcode, 3, 0); |
| 6473 | |
| 6474 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 6475 | index = BitIsSet (opcode, 24); |
| 6476 | add = BitIsSet (opcode, 23); |
| 6477 | wback = (BitIsClear (opcode, 24) || BitIsSet (opcode, 21)); |
| 6478 | |
| 6479 | // (shift_t, shift_n) = (SRType_LSL, 0); |
| 6480 | shift_t = SRType_LSL; |
| 6481 | shift_n = 0; |
| 6482 | |
| 6483 | // if t == 15 || m == 15 then UNPREDICTABLE; |
| 6484 | if ((t == 15) || (m == 15)) |
| 6485 | return false; |
| 6486 | |
| 6487 | // if wback && (n == 15 || n == t) then UNPREDICTABLE; |
| 6488 | if (wback && ((n == 15) || (n == t))) |
| 6489 | return false; |
| 6490 | |
| 6491 | break; |
| 6492 | |
| 6493 | default: |
| 6494 | return false; |
| 6495 | } |
| 6496 | |
| 6497 | // offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 6498 | |
| 6499 | uint64_t Rm = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 6500 | if (!success) |
| 6501 | return false; |
| 6502 | |
| 6503 | addr_t offset = Shift (Rm, shift_t, shift_n, APSR_C); |
| 6504 | |
| 6505 | addr_t offset_addr; |
| 6506 | addr_t address; |
| 6507 | |
| 6508 | // offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 6509 | uint64_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 6510 | if (!success) |
| 6511 | return false; |
| 6512 | |
| 6513 | if (add) |
| 6514 | offset_addr = Rn + offset; |
| 6515 | else |
| 6516 | offset_addr = Rn - offset; |
| 6517 | |
| 6518 | // address = if index then offset_addr else R[n]; |
| 6519 | if (index) |
| 6520 | address = offset_addr; |
| 6521 | else |
| 6522 | address = Rn; |
| 6523 | |
| 6524 | // data = MemU[address,2]; |
| 6525 | Register base_reg; |
| 6526 | Register offset_reg; |
| 6527 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 6528 | offset_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + m); |
| 6529 | |
| 6530 | EmulateInstruction::Context context; |
| 6531 | context.type = eContextRegisterLoad; |
| 6532 | context.SetRegisterPlusIndirectOffset (base_reg, offset_reg); |
| 6533 | uint64_t data = MemURead (context, address, 2, 0, &success); |
| 6534 | if (!success) |
| 6535 | return false; |
| 6536 | |
| 6537 | // if wback then R[n] = offset_addr; |
| 6538 | if (wback) |
| 6539 | { |
| 6540 | context.type = eContextAdjustBaseRegister; |
| 6541 | context.SetAddress (offset_addr); |
| 6542 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 6543 | return false; |
| 6544 | } |
| 6545 | |
| 6546 | // if UnalignedSupport() || address<0> = ’0’ then |
| 6547 | if (UnalignedSupport() || BitIsClear (address, 0)) |
| 6548 | { |
| 6549 | // R[t] = ZeroExtend(data, 32); |
| 6550 | context.type = eContextRegisterLoad; |
| 6551 | context.SetRegisterPlusIndirectOffset (base_reg, offset_reg); |
| 6552 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 6553 | return false; |
| 6554 | } |
| 6555 | else // Can only apply before ARMv7 |
| 6556 | { |
| 6557 | // R[t] = bits(32) UNKNOWN; |
| 6558 | WriteBits32Unknown (t); |
| 6559 | } |
| 6560 | } |
| 6561 | return true; |
| 6562 | } |
| 6563 | |
Caroline Tice | a5e28af | 2011-03-01 21:53:03 +0000 | [diff] [blame] | 6564 | // LDRSB (immediate) calculates an address from a base register value and an immediate offset, loads a byte from |
| 6565 | // memory, sign-extends it to form a 32-bit word, and writes it to a register. It can use offset, post-indexed, |
| 6566 | // or pre-indexed addressing. |
| 6567 | bool |
| 6568 | EmulateInstructionARM::EmulateLDRSBImmediate (ARMEncoding encoding) |
| 6569 | { |
| 6570 | #if 0 |
| 6571 | if ConditionPassed() then |
| 6572 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 6573 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 6574 | address = if index then offset_addr else R[n]; |
| 6575 | R[t] = SignExtend(MemU[address,1], 32); |
| 6576 | if wback then R[n] = offset_addr; |
| 6577 | #endif |
| 6578 | |
| 6579 | bool success = false; |
| 6580 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 6581 | if (!success) |
| 6582 | return false; |
| 6583 | |
| 6584 | if (ConditionPassed ()) |
| 6585 | { |
| 6586 | uint32_t t; |
| 6587 | uint32_t n; |
| 6588 | uint32_t imm32; |
| 6589 | bool index; |
| 6590 | bool add; |
| 6591 | bool wback; |
| 6592 | |
| 6593 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 6594 | switch (encoding) |
| 6595 | { |
| 6596 | case eEncodingT1: |
| 6597 | // if Rt == ’1111’ then SEE PLI; |
| 6598 | // if Rn == ’1111’ then SEE LDRSB (literal); |
| 6599 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32); |
| 6600 | t = Bits32 (opcode, 15, 12); |
| 6601 | n = Bits32 (opcode, 19, 16); |
| 6602 | imm32 = Bits32 (opcode, 11, 0); |
| 6603 | |
| 6604 | // index = TRUE; add = TRUE; wback = FALSE; |
| 6605 | index = true; |
| 6606 | add = true; |
| 6607 | wback = false; |
| 6608 | |
| 6609 | // if t == 13 then UNPREDICTABLE; |
| 6610 | if (t == 13) |
| 6611 | return false; |
| 6612 | |
| 6613 | break; |
| 6614 | |
| 6615 | case eEncodingT2: |
| 6616 | // if Rt == ’1111’ && P == ’1’ && U == ’0’ && W == ’0’ then SEE PLI; |
| 6617 | // if Rn == ’1111’ then SEE LDRSB (literal); |
| 6618 | // if P == ’1’ && U == ’1’ && W == ’0’ then SEE LDRSBT; |
| 6619 | // if P == ’0’ && W == ’0’ then UNDEFINED; |
| 6620 | if (BitIsClear (opcode, 10) && BitIsClear (opcode, 8)) |
| 6621 | return false; |
| 6622 | |
| 6623 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm8, 32); |
| 6624 | t = Bits32 (opcode, 15, 12); |
| 6625 | n = Bits32 (opcode, 19, 16); |
| 6626 | imm32 = Bits32 (opcode, 7, 0); |
| 6627 | |
| 6628 | // index = (P == ’1’); add = (U == ’1’); wback = (W == ’1’); |
| 6629 | index = BitIsSet (opcode, 10); |
| 6630 | add = BitIsSet (opcode, 9); |
| 6631 | wback = BitIsSet (opcode, 8); |
| 6632 | |
| 6633 | // if BadReg(t) || (wback && n == t) then UNPREDICTABLE; |
| 6634 | if (BadReg (t) || (wback && (n == t))) |
| 6635 | return false; |
| 6636 | |
| 6637 | break; |
| 6638 | |
| 6639 | case eEncodingA1: |
| 6640 | { |
| 6641 | // if Rn == ’1111’ then SEE LDRSB (literal); |
| 6642 | // if P == ’0’ && W == ’1’ then SEE LDRSBT; |
| 6643 | // t == UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm4H:imm4L, 32); |
| 6644 | t = Bits32 (opcode, 15, 12); |
| 6645 | n = Bits32 (opcode, 19, 16); |
| 6646 | |
| 6647 | uint32_t imm4H = Bits32 (opcode, 11, 8); |
| 6648 | uint32_t imm4L = Bits32 (opcode, 3, 0); |
Caroline Tice | 40b1a6c | 2011-03-03 00:07:02 +0000 | [diff] [blame] | 6649 | imm32 = (imm4H << 4) | imm4L; |
Caroline Tice | a5e28af | 2011-03-01 21:53:03 +0000 | [diff] [blame] | 6650 | |
| 6651 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 6652 | index = BitIsSet (opcode, 24); |
| 6653 | add = BitIsSet (opcode, 23); |
| 6654 | wback = (BitIsClear (opcode, 24) || BitIsSet (opcode, 21)); |
| 6655 | |
| 6656 | // if t == 15 || (wback && n == t) then UNPREDICTABLE; |
| 6657 | if ((t == 15) || (wback && (n == t))) |
| 6658 | return false; |
| 6659 | |
| 6660 | break; |
| 6661 | } |
| 6662 | |
| 6663 | default: |
| 6664 | return false; |
| 6665 | } |
| 6666 | |
| 6667 | uint64_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 6668 | if (!success) |
| 6669 | return false; |
| 6670 | |
| 6671 | addr_t offset_addr; |
| 6672 | addr_t address; |
| 6673 | |
| 6674 | // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 6675 | if (add) |
| 6676 | offset_addr = Rn + imm32; |
| 6677 | else |
| 6678 | offset_addr = Rn - imm32; |
| 6679 | |
| 6680 | // address = if index then offset_addr else R[n]; |
| 6681 | if (index) |
| 6682 | address = offset_addr; |
| 6683 | else |
| 6684 | address = Rn; |
| 6685 | |
| 6686 | // R[t] = SignExtend(MemU[address,1], 32); |
| 6687 | Register base_reg; |
| 6688 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 6689 | |
| 6690 | EmulateInstruction::Context context; |
| 6691 | context.type = eContextRegisterLoad; |
| 6692 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 6693 | |
| 6694 | uint64_t unsigned_data = MemURead (context, address, 1, 0, &success); |
| 6695 | if (!success) |
| 6696 | return false; |
| 6697 | |
| 6698 | int64_t signed_data = llvm::SignExtend64<8>(unsigned_data); |
| 6699 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, (uint64_t) signed_data)) |
| 6700 | return false; |
| 6701 | |
| 6702 | // if wback then R[n] = offset_addr; |
| 6703 | if (wback) |
| 6704 | { |
| 6705 | context.type = eContextAdjustBaseRegister; |
| 6706 | context.SetAddress (offset_addr); |
| 6707 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 6708 | return false; |
| 6709 | } |
| 6710 | } |
| 6711 | |
| 6712 | return true; |
| 6713 | } |
Caroline Tice | 0e6bc95 | 2011-03-01 18:00:42 +0000 | [diff] [blame] | 6714 | |
Caroline Tice | 5f59391 | 2011-03-01 22:25:17 +0000 | [diff] [blame] | 6715 | // LDRSB (literal) calculates an address from the PC value and an immediate offset, loads a byte from memory, |
| 6716 | // sign-extends it to form a 32-bit word, and writes tit to a register. |
| 6717 | bool |
| 6718 | EmulateInstructionARM::EmulateLDRSBLiteral (ARMEncoding encoding) |
| 6719 | { |
| 6720 | #if 0 |
| 6721 | if ConditionPassed() then |
| 6722 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 6723 | base = Align(PC,4); |
| 6724 | address = if add then (base + imm32) else (base - imm32); |
| 6725 | R[t] = SignExtend(MemU[address,1], 32); |
| 6726 | #endif |
| 6727 | |
| 6728 | bool success = false; |
| 6729 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 6730 | if (!success) |
| 6731 | return false; |
| 6732 | |
| 6733 | if (ConditionPassed ()) |
| 6734 | { |
| 6735 | uint32_t t; |
| 6736 | uint32_t imm32; |
| 6737 | bool add; |
| 6738 | |
| 6739 | // EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 6740 | switch (encoding) |
| 6741 | { |
| 6742 | case eEncodingT1: |
| 6743 | // if Rt == ’1111’ then SEE PLI; |
| 6744 | // t = UInt(Rt); imm32 = ZeroExtend(imm12, 32); add = (U == ’1’); |
| 6745 | t = Bits32 (opcode, 15, 12); |
| 6746 | imm32 = Bits32 (opcode, 11, 0); |
| 6747 | add = BitIsSet (opcode, 23); |
| 6748 | |
| 6749 | // if t == 13 then UNPREDICTABLE; |
| 6750 | if (t == 13) |
| 6751 | return false; |
| 6752 | |
| 6753 | break; |
| 6754 | |
| 6755 | case eEncodingA1: |
| 6756 | { |
| 6757 | // t == UInt(Rt); imm32 = ZeroExtend(imm4H:imm4L, 32); add = (U == ’1’); |
| 6758 | t = Bits32 (opcode, 15, 12); |
| 6759 | uint32_t imm4H = Bits32 (opcode, 11, 8); |
| 6760 | uint32_t imm4L = Bits32 (opcode, 3, 0); |
Caroline Tice | 40b1a6c | 2011-03-03 00:07:02 +0000 | [diff] [blame] | 6761 | imm32 = (imm4H << 4) | imm4L; |
Caroline Tice | 5f59391 | 2011-03-01 22:25:17 +0000 | [diff] [blame] | 6762 | add = BitIsSet (opcode, 23); |
| 6763 | |
| 6764 | // if t == 15 then UNPREDICTABLE; |
| 6765 | if (t == 15) |
| 6766 | return false; |
| 6767 | |
| 6768 | break; |
| 6769 | } |
| 6770 | |
| 6771 | default: |
| 6772 | return false; |
| 6773 | } |
| 6774 | |
| 6775 | // base = Align(PC,4); |
| 6776 | uint64_t pc_value = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, |
| 6777 | &success); |
| 6778 | if (!success) |
| 6779 | return false; |
| 6780 | uint64_t base = AlignPC (pc_value); |
| 6781 | |
| 6782 | // address = if add then (base + imm32) else (base - imm32); |
| 6783 | addr_t address; |
| 6784 | if (add) |
| 6785 | address = base + imm32; |
| 6786 | else |
| 6787 | address = base - imm32; |
| 6788 | |
| 6789 | // R[t] = SignExtend(MemU[address,1], 32); |
| 6790 | Register base_reg; |
| 6791 | base_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); |
| 6792 | |
| 6793 | EmulateInstruction::Context context; |
| 6794 | context.type = eContextRegisterLoad; |
| 6795 | context.SetRegisterPlusOffset (base_reg, address - base); |
| 6796 | |
| 6797 | uint64_t unsigned_data = MemURead (context, address, 1, 0, &success); |
| 6798 | if (!success) |
| 6799 | return false; |
| 6800 | |
| 6801 | int64_t signed_data = llvm::SignExtend64<8>(unsigned_data); |
| 6802 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, (uint64_t) signed_data)) |
| 6803 | return false; |
| 6804 | } |
| 6805 | return true; |
| 6806 | } |
| 6807 | |
Caroline Tice | 672f311 | 2011-03-01 23:55:59 +0000 | [diff] [blame] | 6808 | // LDRSB (register) calculates an address from a base register value and an offset register value, loadsa byte from |
| 6809 | // memory, sign-extends it to form a 32-bit word, and writes it to a register. The offset register value can be |
| 6810 | // shifted left by 0, 1, 2, or 3 bits. |
| 6811 | bool |
| 6812 | EmulateInstructionARM::EmulateLDRSBRegister (ARMEncoding encoding) |
| 6813 | { |
| 6814 | #if 0 |
| 6815 | if ConditionPassed() then |
| 6816 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 6817 | offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 6818 | offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 6819 | address = if index then offset_addr else R[n]; |
| 6820 | R[t] = SignExtend(MemU[address,1], 32); |
| 6821 | if wback then R[n] = offset_addr; |
| 6822 | #endif |
| 6823 | |
| 6824 | bool success = false; |
| 6825 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 6826 | if (!success) |
| 6827 | return false; |
| 6828 | |
| 6829 | if (ConditionPassed ()) |
| 6830 | { |
| 6831 | uint32_t t; |
| 6832 | uint32_t n; |
| 6833 | uint32_t m; |
| 6834 | bool index; |
| 6835 | bool add; |
| 6836 | bool wback; |
| 6837 | ARM_ShifterType shift_t; |
| 6838 | uint32_t shift_n; |
| 6839 | |
| 6840 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 6841 | switch (encoding) |
| 6842 | { |
| 6843 | case eEncodingT1: |
| 6844 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 6845 | t = Bits32 (opcode, 2, 0); |
| 6846 | n = Bits32 (opcode, 5, 3); |
| 6847 | m = Bits32 (opcode, 8, 6); |
| 6848 | |
| 6849 | // index = TRUE; add = TRUE; wback = FALSE; |
| 6850 | index = true; |
| 6851 | add = true; |
| 6852 | wback = false; |
| 6853 | |
| 6854 | // (shift_t, shift_n) = (SRType_LSL, 0); |
| 6855 | shift_t = SRType_LSL; |
| 6856 | shift_n = 0; |
| 6857 | |
| 6858 | break; |
| 6859 | |
| 6860 | case eEncodingT2: |
| 6861 | // if Rt == ’1111’ then SEE PLI; |
| 6862 | // if Rn == ’1111’ then SEE LDRSB (literal); |
| 6863 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 6864 | t = Bits32 (opcode, 15, 12); |
| 6865 | n = Bits32 (opcode, 19, 16); |
| 6866 | m = Bits32 (opcode, 3, 0); |
| 6867 | |
| 6868 | // index = TRUE; add = TRUE; wback = FALSE; |
| 6869 | index = true; |
| 6870 | add = true; |
| 6871 | wback = false; |
| 6872 | |
| 6873 | // (shift_t, shift_n) = (SRType_LSL, UInt(imm2)); |
| 6874 | shift_t = SRType_LSL; |
| 6875 | shift_n = Bits32 (opcode, 5, 4); |
| 6876 | |
| 6877 | // if t == 13 || BadReg(m) then UNPREDICTABLE; |
| 6878 | if ((t == 13) || BadReg (m)) |
| 6879 | return false; |
| 6880 | break; |
| 6881 | |
| 6882 | case eEncodingA1: |
| 6883 | // if P == ’0’ && W == ’1’ then SEE LDRSBT; |
| 6884 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 6885 | t = Bits32 (opcode, 15, 12); |
| 6886 | n = Bits32 (opcode, 19, 16); |
| 6887 | m = Bits32 (opcode, 3, 0); |
| 6888 | |
| 6889 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 6890 | index = BitIsSet (opcode, 24); |
| 6891 | add = BitIsSet (opcode, 23); |
| 6892 | wback = BitIsClear (opcode, 24) || BitIsSet (opcode, 21); |
| 6893 | |
| 6894 | // (shift_t, shift_n) = (SRType_LSL, 0); |
| 6895 | shift_t = SRType_LSL; |
| 6896 | shift_n = 0; |
| 6897 | |
| 6898 | // if t == 15 || m == 15 then UNPREDICTABLE; |
| 6899 | if ((t == 15) || (m == 15)) |
| 6900 | return false; |
| 6901 | |
| 6902 | // if wback && (n == 15 || n == t) then UNPREDICTABLE; |
| 6903 | if (wback && ((n == 15) || (n == t))) |
| 6904 | return false; |
| 6905 | break; |
| 6906 | |
| 6907 | default: |
| 6908 | return false; |
| 6909 | } |
| 6910 | |
| 6911 | uint64_t Rm = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 6912 | if (!success) |
| 6913 | return false; |
| 6914 | |
| 6915 | // offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 6916 | addr_t offset = Shift (Rm, shift_t, shift_n, APSR_C); |
| 6917 | |
| 6918 | addr_t offset_addr; |
| 6919 | addr_t address; |
| 6920 | |
| 6921 | // offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 6922 | uint64_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 6923 | if (!success) |
| 6924 | return false; |
| 6925 | |
| 6926 | if (add) |
| 6927 | offset_addr = Rn + offset; |
| 6928 | else |
| 6929 | offset_addr = Rn - offset; |
| 6930 | |
| 6931 | // address = if index then offset_addr else R[n]; |
| 6932 | if (index) |
| 6933 | address = offset_addr; |
| 6934 | else |
| 6935 | address = Rn; |
| 6936 | |
| 6937 | // R[t] = SignExtend(MemU[address,1], 32); |
| 6938 | Register base_reg; |
| 6939 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 6940 | Register offset_reg; |
| 6941 | offset_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + m); |
| 6942 | |
| 6943 | EmulateInstruction::Context context; |
| 6944 | context.type = eContextRegisterLoad; |
| 6945 | context.SetRegisterPlusIndirectOffset (base_reg, offset_reg); |
| 6946 | |
| 6947 | uint64_t unsigned_data = MemURead (context, address, 1, 0, &success); |
| 6948 | if (!success) |
| 6949 | return false; |
| 6950 | |
| 6951 | int64_t signed_data = llvm::SignExtend64<8>(unsigned_data); |
| 6952 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, (uint64_t) signed_data)) |
| 6953 | return false; |
| 6954 | |
| 6955 | // if wback then R[n] = offset_addr; |
| 6956 | if (wback) |
| 6957 | { |
| 6958 | context.type = eContextAdjustBaseRegister; |
| 6959 | context.SetAddress (offset_addr); |
| 6960 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 6961 | return false; |
| 6962 | } |
| 6963 | } |
| 6964 | return true; |
| 6965 | } |
| 6966 | |
Caroline Tice | 78fb563 | 2011-03-02 00:39:42 +0000 | [diff] [blame] | 6967 | // LDRSH (immediate) calculates an address from a base register value and an immediate offset, loads a halfword from |
| 6968 | // memory, sign-extends it to form a 32-bit word, and writes it to a register. It can use offset, post-indexed, or |
| 6969 | // pre-indexed addressing. |
| 6970 | bool |
| 6971 | EmulateInstructionARM::EmulateLDRSHImmediate (ARMEncoding encoding) |
| 6972 | { |
| 6973 | #if 0 |
| 6974 | if ConditionPassed() then |
| 6975 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 6976 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 6977 | address = if index then offset_addr else R[n]; |
| 6978 | data = MemU[address,2]; |
| 6979 | if wback then R[n] = offset_addr; |
| 6980 | if UnalignedSupport() || address<0> = ’0’ then |
| 6981 | R[t] = SignExtend(data, 32); |
| 6982 | else // Can only apply before ARMv7 |
| 6983 | R[t] = bits(32) UNKNOWN; |
| 6984 | #endif |
| 6985 | |
| 6986 | bool success = false; |
| 6987 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 6988 | if (!success) |
| 6989 | return false; |
| 6990 | |
| 6991 | if (ConditionPassed()) |
| 6992 | { |
| 6993 | uint32_t t; |
| 6994 | uint32_t n; |
| 6995 | uint32_t imm32; |
| 6996 | bool index; |
| 6997 | bool add; |
| 6998 | bool wback; |
| 6999 | |
| 7000 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 7001 | switch (encoding) |
| 7002 | { |
| 7003 | case eEncodingT1: |
| 7004 | // if Rn == ’1111’ then SEE LDRSH (literal); |
| 7005 | // if Rt == ’1111’ then SEE "Unallocated memory hints"; |
| 7006 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32); |
| 7007 | t = Bits32 (opcode, 15, 12); |
| 7008 | n = Bits32 (opcode, 19, 16); |
| 7009 | imm32 = Bits32 (opcode, 11, 0); |
| 7010 | |
| 7011 | // index = TRUE; add = TRUE; wback = FALSE; |
| 7012 | index = true; |
| 7013 | add = true; |
| 7014 | wback = false; |
| 7015 | |
| 7016 | // if t == 13 then UNPREDICTABLE; |
| 7017 | if (t == 13) |
| 7018 | return false; |
| 7019 | |
| 7020 | break; |
| 7021 | |
| 7022 | case eEncodingT2: |
| 7023 | // if Rn == ’1111’ then SEE LDRSH (literal); |
| 7024 | // if Rt == ’1111’ && P == ’1’ && U == ’0’ && W == ’0’ then SEE "Unallocated memory hints"; |
| 7025 | // if P == ’1’ && U == ’1’ && W == ’0’ then SEE LDRSHT; |
| 7026 | // if P == ’0’ && W == ’0’ then UNDEFINED; |
| 7027 | if (BitIsClear (opcode, 10) && BitIsClear (opcode, 8)) |
| 7028 | return false; |
| 7029 | |
| 7030 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm8, 32); |
| 7031 | t = Bits32 (opcode, 15, 12); |
| 7032 | n = Bits32 (opcode, 19, 16); |
| 7033 | imm32 = Bits32 (opcode, 7, 0); |
| 7034 | |
| 7035 | // index = (P == ’1’); add = (U == ’1’); wback = (W == ’1’); |
| 7036 | index = BitIsSet (opcode, 10); |
| 7037 | add = BitIsSet (opcode, 9); |
| 7038 | wback = BitIsSet (opcode, 8); |
| 7039 | |
| 7040 | // if BadReg(t) || (wback && n == t) then UNPREDICTABLE; |
| 7041 | if (BadReg (t) || (wback && (n == t))) |
| 7042 | return false; |
| 7043 | |
| 7044 | break; |
| 7045 | |
| 7046 | case eEncodingA1: |
| 7047 | { |
| 7048 | // if Rn == ’1111’ then SEE LDRSH (literal); |
| 7049 | // if P == ’0’ && W == ’1’ then SEE LDRSHT; |
| 7050 | // t == UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm4H:imm4L, 32); |
| 7051 | t = Bits32 (opcode, 15, 12); |
| 7052 | n = Bits32 (opcode, 19, 16); |
| 7053 | uint32_t imm4H = Bits32 (opcode, 11,8); |
| 7054 | uint32_t imm4L = Bits32 (opcode, 3, 0); |
Caroline Tice | 40b1a6c | 2011-03-03 00:07:02 +0000 | [diff] [blame] | 7055 | imm32 = (imm4H << 4) | imm4L; |
Caroline Tice | 78fb563 | 2011-03-02 00:39:42 +0000 | [diff] [blame] | 7056 | |
| 7057 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 7058 | index = BitIsSet (opcode, 24); |
| 7059 | add = BitIsSet (opcode, 23); |
| 7060 | wback = BitIsClear (opcode, 24) || BitIsSet (opcode, 21); |
| 7061 | |
| 7062 | // if t == 15 || (wback && n == t) then UNPREDICTABLE; |
| 7063 | if ((t == 15) || (wback && (n == t))) |
| 7064 | return false; |
| 7065 | |
| 7066 | break; |
| 7067 | } |
| 7068 | |
| 7069 | default: |
| 7070 | return false; |
| 7071 | } |
| 7072 | |
| 7073 | // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 7074 | uint64_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 7075 | if (!success) |
| 7076 | return false; |
| 7077 | |
| 7078 | addr_t offset_addr; |
| 7079 | if (add) |
| 7080 | offset_addr = Rn + imm32; |
| 7081 | else |
| 7082 | offset_addr = Rn - imm32; |
| 7083 | |
| 7084 | // address = if index then offset_addr else R[n]; |
| 7085 | addr_t address; |
| 7086 | if (index) |
| 7087 | address = offset_addr; |
| 7088 | else |
| 7089 | address = Rn; |
| 7090 | |
| 7091 | // data = MemU[address,2]; |
| 7092 | Register base_reg; |
| 7093 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 7094 | |
| 7095 | EmulateInstruction::Context context; |
| 7096 | context.type = eContextRegisterLoad; |
| 7097 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 7098 | |
| 7099 | uint64_t data = MemURead (context, address, 2, 0, &success); |
| 7100 | if (!success) |
| 7101 | return false; |
| 7102 | |
| 7103 | // if wback then R[n] = offset_addr; |
| 7104 | if (wback) |
| 7105 | { |
| 7106 | context.type = eContextAdjustBaseRegister; |
| 7107 | context.SetAddress (offset_addr); |
| 7108 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 7109 | return false; |
| 7110 | } |
| 7111 | |
| 7112 | // if UnalignedSupport() || address<0> = ’0’ then |
| 7113 | if (UnalignedSupport() || BitIsClear (address, 0)) |
| 7114 | { |
| 7115 | // R[t] = SignExtend(data, 32); |
| 7116 | int64_t signed_data = llvm::SignExtend64<16>(data); |
| 7117 | context.type = eContextRegisterLoad; |
| 7118 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 7119 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, (uint64_t) signed_data)) |
| 7120 | return false; |
| 7121 | } |
| 7122 | else // Can only apply before ARMv7 |
| 7123 | { |
| 7124 | // R[t] = bits(32) UNKNOWN; |
| 7125 | WriteBits32Unknown (t); |
| 7126 | } |
| 7127 | } |
| 7128 | return true; |
| 7129 | } |
| 7130 | |
Caroline Tice | d2fac09 | 2011-03-02 19:45:34 +0000 | [diff] [blame] | 7131 | // LDRSH (literal) calculates an address from the PC value and an immediate offset, loads a halfword from memory, |
| 7132 | // sign-extends it to from a 32-bit word, and writes it to a register. |
| 7133 | bool |
| 7134 | EmulateInstructionARM::EmulateLDRSHLiteral (ARMEncoding encoding) |
| 7135 | { |
| 7136 | #if 0 |
| 7137 | if ConditionPassed() then |
| 7138 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 7139 | base = Align(PC,4); |
| 7140 | address = if add then (base + imm32) else (base - imm32); |
| 7141 | data = MemU[address,2]; |
| 7142 | if UnalignedSupport() || address<0> = ’0’ then |
| 7143 | R[t] = SignExtend(data, 32); |
| 7144 | else // Can only apply before ARMv7 |
| 7145 | R[t] = bits(32) UNKNOWN; |
| 7146 | #endif |
| 7147 | |
| 7148 | bool success = false; |
| 7149 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 7150 | if (!success) |
| 7151 | return false; |
| 7152 | |
| 7153 | if (ConditionPassed()) |
| 7154 | { |
| 7155 | uint32_t t; |
| 7156 | uint32_t imm32; |
| 7157 | bool add; |
| 7158 | |
| 7159 | // EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 7160 | switch (encoding) |
| 7161 | { |
| 7162 | case eEncodingT1: |
| 7163 | // if Rt == ’1111’ then SEE "Unallocated memory hints"; |
| 7164 | // t = UInt(Rt); imm32 = ZeroExtend(imm12, 32); add = (U == ’1’); |
| 7165 | t = Bits32 (opcode, 15, 12); |
| 7166 | imm32 = Bits32 (opcode, 11, 0); |
| 7167 | add = BitIsSet (opcode, 23); |
| 7168 | |
| 7169 | // if t == 13 then UNPREDICTABLE; |
| 7170 | if (t == 13) |
| 7171 | return false; |
| 7172 | |
| 7173 | break; |
| 7174 | |
| 7175 | case eEncodingA1: |
| 7176 | { |
| 7177 | // t == UInt(Rt); imm32 = ZeroExtend(imm4H:imm4L, 32); add = (U == ’1’); |
| 7178 | t = Bits32 (opcode, 15, 12); |
| 7179 | uint32_t imm4H = Bits32 (opcode, 11, 8); |
| 7180 | uint32_t imm4L = Bits32 (opcode, 3, 0); |
Caroline Tice | 40b1a6c | 2011-03-03 00:07:02 +0000 | [diff] [blame] | 7181 | imm32 = (imm4H << 4) | imm4L; |
Caroline Tice | d2fac09 | 2011-03-02 19:45:34 +0000 | [diff] [blame] | 7182 | add = BitIsSet (opcode, 23); |
| 7183 | |
| 7184 | // if t == 15 then UNPREDICTABLE; |
| 7185 | if (t == 15) |
| 7186 | return false; |
| 7187 | |
| 7188 | break; |
| 7189 | } |
| 7190 | default: |
| 7191 | return false; |
| 7192 | } |
| 7193 | |
| 7194 | // base = Align(PC,4); |
| 7195 | uint64_t pc_value = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 7196 | if (!success) |
| 7197 | return false; |
| 7198 | |
| 7199 | uint64_t base = AlignPC (pc_value); |
| 7200 | |
| 7201 | addr_t address; |
| 7202 | // address = if add then (base + imm32) else (base - imm32); |
| 7203 | if (add) |
| 7204 | address = base + imm32; |
| 7205 | else |
| 7206 | address = base - imm32; |
| 7207 | |
| 7208 | // data = MemU[address,2]; |
| 7209 | Register base_reg; |
| 7210 | base_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); |
| 7211 | |
| 7212 | EmulateInstruction::Context context; |
| 7213 | context.type = eContextRegisterLoad; |
| 7214 | context.SetRegisterPlusOffset (base_reg, imm32); |
| 7215 | |
| 7216 | uint64_t data = MemURead (context, address, 2, 0, &success); |
| 7217 | if (!success) |
| 7218 | return false; |
| 7219 | |
| 7220 | // if UnalignedSupport() || address<0> = ’0’ then |
| 7221 | if (UnalignedSupport() || BitIsClear (address, 0)) |
| 7222 | { |
| 7223 | // R[t] = SignExtend(data, 32); |
| 7224 | int64_t signed_data = llvm::SignExtend64<16>(data); |
| 7225 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, (uint64_t) signed_data)) |
| 7226 | return false; |
| 7227 | } |
| 7228 | else // Can only apply before ARMv7 |
| 7229 | { |
| 7230 | // R[t] = bits(32) UNKNOWN; |
| 7231 | WriteBits32Unknown (t); |
| 7232 | } |
| 7233 | } |
| 7234 | return true; |
| 7235 | } |
| 7236 | |
Caroline Tice | 291a3e9 | 2011-03-02 21:13:44 +0000 | [diff] [blame] | 7237 | // LDRSH (register) calculates an address from a base register value and an offset register value, loads a halfword |
| 7238 | // from memory, sign-extends it to form a 32-bit word, and writes it to a register. The offset register value can be |
| 7239 | // shifted left by 0, 1, 2, or 3 bits. |
| 7240 | bool |
| 7241 | EmulateInstructionARM::EmulateLDRSHRegister (ARMEncoding encoding) |
| 7242 | { |
| 7243 | #if 0 |
| 7244 | if ConditionPassed() then |
| 7245 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 7246 | offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 7247 | offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 7248 | address = if index then offset_addr else R[n]; |
| 7249 | data = MemU[address,2]; |
| 7250 | if wback then R[n] = offset_addr; |
| 7251 | if UnalignedSupport() || address<0> = ’0’ then |
| 7252 | R[t] = SignExtend(data, 32); |
| 7253 | else // Can only apply before ARMv7 |
| 7254 | R[t] = bits(32) UNKNOWN; |
| 7255 | #endif |
| 7256 | |
| 7257 | bool success = false; |
| 7258 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 7259 | if (!success) |
| 7260 | return false; |
| 7261 | |
| 7262 | if (ConditionPassed()) |
| 7263 | { |
| 7264 | uint32_t t; |
| 7265 | uint32_t n; |
| 7266 | uint32_t m; |
| 7267 | bool index; |
| 7268 | bool add; |
| 7269 | bool wback; |
| 7270 | ARM_ShifterType shift_t; |
| 7271 | uint32_t shift_n; |
| 7272 | |
| 7273 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 7274 | switch (encoding) |
| 7275 | { |
| 7276 | case eEncodingT1: |
| 7277 | // if CurrentInstrSet() == InstrSet_ThumbEE then SEE "Modified operation in ThumbEE"; |
| 7278 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 7279 | t = Bits32 (opcode, 2, 0); |
| 7280 | n = Bits32 (opcode, 5, 3); |
| 7281 | m = Bits32 (opcode, 8, 6); |
| 7282 | |
| 7283 | // index = TRUE; add = TRUE; wback = FALSE; |
| 7284 | index = true; |
| 7285 | add = true; |
| 7286 | wback = false; |
| 7287 | |
| 7288 | // (shift_t, shift_n) = (SRType_LSL, 0); |
| 7289 | shift_t = SRType_LSL; |
| 7290 | shift_n = 0; |
| 7291 | |
| 7292 | break; |
| 7293 | |
| 7294 | case eEncodingT2: |
| 7295 | // if Rn == ’1111’ then SEE LDRSH (literal); |
| 7296 | // if Rt == ’1111’ then SEE "Unallocated memory hints"; |
| 7297 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 7298 | t = Bits32 (opcode, 15, 12); |
| 7299 | n = Bits32 (opcode, 19, 16); |
| 7300 | m = Bits32 (opcode, 3, 0); |
| 7301 | |
| 7302 | // index = TRUE; add = TRUE; wback = FALSE; |
| 7303 | index = true; |
| 7304 | add = true; |
| 7305 | wback = false; |
| 7306 | |
| 7307 | // (shift_t, shift_n) = (SRType_LSL, UInt(imm2)); |
| 7308 | shift_t = SRType_LSL; |
| 7309 | shift_n = Bits32 (opcode, 5, 4); |
| 7310 | |
| 7311 | // if t == 13 || BadReg(m) then UNPREDICTABLE; |
| 7312 | if ((t == 13) || BadReg (m)) |
| 7313 | return false; |
| 7314 | |
| 7315 | break; |
| 7316 | |
| 7317 | case eEncodingA1: |
| 7318 | // if P == ’0’ && W == ’1’ then SEE LDRSHT; |
| 7319 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 7320 | t = Bits32 (opcode, 15, 12); |
| 7321 | n = Bits32 (opcode, 19, 16); |
| 7322 | m = Bits32 (opcode, 3, 0); |
| 7323 | |
| 7324 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 7325 | index = BitIsSet (opcode, 24); |
| 7326 | add = BitIsSet (opcode, 23); |
| 7327 | wback = BitIsClear (opcode, 24) || BitIsSet (opcode, 21); |
| 7328 | |
| 7329 | // (shift_t, shift_n) = (SRType_LSL, 0); |
| 7330 | shift_t = SRType_LSL; |
| 7331 | shift_n = 0; |
| 7332 | |
| 7333 | // if t == 15 || m == 15 then UNPREDICTABLE; |
| 7334 | if ((t == 15) || (m == 15)) |
| 7335 | return false; |
| 7336 | |
| 7337 | // if wback && (n == 15 || n == t) then UNPREDICTABLE; |
| 7338 | if (wback && ((n == 15) || (n == t))) |
| 7339 | return false; |
| 7340 | |
| 7341 | break; |
| 7342 | |
| 7343 | default: |
| 7344 | break; |
| 7345 | } |
| 7346 | |
| 7347 | uint64_t Rm = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 7348 | if (!success) |
| 7349 | return false; |
| 7350 | |
| 7351 | uint64_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 7352 | if (!success) |
| 7353 | return false; |
| 7354 | |
| 7355 | // offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 7356 | addr_t offset = Shift (Rm, shift_t, shift_n, APSR_C); |
| 7357 | |
| 7358 | addr_t offset_addr; |
| 7359 | addr_t address; |
| 7360 | |
| 7361 | // offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 7362 | if (add) |
| 7363 | offset_addr = Rn + offset; |
| 7364 | else |
| 7365 | offset_addr = Rn - offset; |
| 7366 | |
| 7367 | // address = if index then offset_addr else R[n]; |
| 7368 | if (index) |
| 7369 | address = offset_addr; |
| 7370 | else |
| 7371 | address = Rn; |
| 7372 | |
| 7373 | // data = MemU[address,2]; |
| 7374 | Register base_reg; |
| 7375 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 7376 | |
| 7377 | Register offset_reg; |
| 7378 | offset_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + m); |
| 7379 | |
| 7380 | EmulateInstruction::Context context; |
| 7381 | context.type = eContextRegisterLoad; |
| 7382 | context.SetRegisterPlusIndirectOffset (base_reg, offset_reg); |
| 7383 | |
| 7384 | uint64_t data = MemURead (context, address, 2, 0, &success); |
| 7385 | if (!success) |
| 7386 | return false; |
| 7387 | |
| 7388 | // if wback then R[n] = offset_addr; |
| 7389 | if (wback) |
| 7390 | { |
| 7391 | context.type = eContextAdjustBaseRegister; |
| 7392 | context.SetAddress (offset_addr); |
| 7393 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 7394 | return false; |
| 7395 | } |
| 7396 | |
| 7397 | // if UnalignedSupport() || address<0> = ’0’ then |
| 7398 | if (UnalignedSupport() || BitIsClear (address, 0)) |
| 7399 | { |
| 7400 | // R[t] = SignExtend(data, 32); |
| 7401 | context.type = eContextRegisterLoad; |
| 7402 | context.SetRegisterPlusIndirectOffset (base_reg, offset_reg); |
| 7403 | |
| 7404 | int64_t signed_data = llvm::SignExtend64<16>(data); |
| 7405 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, (uint64_t) signed_data)) |
| 7406 | return false; |
| 7407 | } |
| 7408 | else // Can only apply before ARMv7 |
| 7409 | { |
| 7410 | // R[t] = bits(32) UNKNOWN; |
| 7411 | WriteBits32Unknown (t); |
| 7412 | } |
| 7413 | } |
| 7414 | return true; |
| 7415 | } |
Caroline Tice | 6bf6516 | 2011-03-03 17:42:58 +0000 | [diff] [blame] | 7416 | |
| 7417 | // SXTB extracts an 8-bit value from a register, sign-extends it to 32 bits, and writes the result to the destination |
| 7418 | // register. You can specifiy a rotation by 0, 8, 16, or 24 bits before extracting the 8-bit value. |
| 7419 | bool |
| 7420 | EmulateInstructionARM::EmulateSXTB (ARMEncoding encoding) |
| 7421 | { |
| 7422 | #if 0 |
| 7423 | if ConditionPassed() then |
| 7424 | EncodingSpecificOperations(); |
| 7425 | rotated = ROR(R[m], rotation); |
| 7426 | R[d] = SignExtend(rotated<7:0>, 32); |
| 7427 | #endif |
| 7428 | |
| 7429 | bool success = false; |
| 7430 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 7431 | if (!success) |
| 7432 | return false; |
| 7433 | |
| 7434 | if (ConditionPassed()) |
| 7435 | { |
| 7436 | uint32_t d; |
| 7437 | uint32_t m; |
| 7438 | uint32_t rotation; |
| 7439 | |
| 7440 | // EncodingSpecificOperations(); |
| 7441 | switch (encoding) |
| 7442 | { |
| 7443 | case eEncodingT1: |
| 7444 | // d = UInt(Rd); m = UInt(Rm); rotation = 0; |
| 7445 | d = Bits32 (opcode, 2, 0); |
| 7446 | m = Bits32 (opcode, 5, 3); |
| 7447 | rotation = 0; |
| 7448 | |
| 7449 | break; |
| 7450 | |
| 7451 | case eEncodingT2: |
| 7452 | // d = UInt(Rd); m = UInt(Rm); rotation = UInt(rotate:’000’); |
| 7453 | d = Bits32 (opcode, 11, 8); |
| 7454 | m = Bits32 (opcode, 3, 0); |
| 7455 | rotation = Bits32 (opcode, 5, 4) << 3; |
| 7456 | |
| 7457 | // if BadReg(d) || BadReg(m) then UNPREDICTABLE; |
| 7458 | if (BadReg (d) || BadReg (m)) |
| 7459 | return false; |
| 7460 | |
| 7461 | break; |
| 7462 | |
| 7463 | case eEncodingA1: |
| 7464 | // d = UInt(Rd); m = UInt(Rm); rotation = UInt(rotate:’000’); |
| 7465 | d = Bits32 (opcode, 15, 12); |
| 7466 | m = Bits32 (opcode, 3, 0); |
| 7467 | rotation = Bits32 (opcode, 11, 10) << 3; |
| 7468 | |
| 7469 | // if d == 15 || m == 15 then UNPREDICTABLE; |
| 7470 | if ((d == 15) || (m == 15)) |
| 7471 | return false; |
| 7472 | |
| 7473 | break; |
| 7474 | |
| 7475 | default: |
| 7476 | return false; |
| 7477 | } |
| 7478 | |
Caroline Tice | 868198b | 2011-03-03 18:04:49 +0000 | [diff] [blame] | 7479 | uint64_t Rm = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 7480 | if (!success) |
| 7481 | return false; |
Caroline Tice | 6bf6516 | 2011-03-03 17:42:58 +0000 | [diff] [blame] | 7482 | |
| 7483 | // rotated = ROR(R[m], rotation); |
| 7484 | uint64_t rotated = ROR (Rm, rotation); |
| 7485 | |
| 7486 | // R[d] = SignExtend(rotated<7:0>, 32); |
Caroline Tice | 8ce96d9 | 2011-03-03 18:27:17 +0000 | [diff] [blame] | 7487 | int64_t data = llvm::SignExtend64<8>(rotated); |
Caroline Tice | 6bf6516 | 2011-03-03 17:42:58 +0000 | [diff] [blame] | 7488 | |
| 7489 | Register source_reg; |
| 7490 | source_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + m); |
| 7491 | |
| 7492 | EmulateInstruction::Context context; |
| 7493 | context.type = eContextRegisterLoad; |
| 7494 | context.SetRegister (source_reg); |
| 7495 | |
Caroline Tice | 8ce96d9 | 2011-03-03 18:27:17 +0000 | [diff] [blame] | 7496 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + d, (uint64_t) data)) |
Caroline Tice | 6bf6516 | 2011-03-03 17:42:58 +0000 | [diff] [blame] | 7497 | return false; |
| 7498 | } |
| 7499 | return true; |
| 7500 | } |
Caroline Tice | 291a3e9 | 2011-03-02 21:13:44 +0000 | [diff] [blame] | 7501 | |
Caroline Tice | 868198b | 2011-03-03 18:04:49 +0000 | [diff] [blame] | 7502 | // SXTH extracts a 16-bit value from a register, sign-extends it to 32 bits, and writes the result to the destination |
| 7503 | // register. You can specify a rotation by 0, 8, 16, or 24 bits before extracting the 16-bit value. |
| 7504 | bool |
| 7505 | EmulateInstructionARM::EmulateSXTH (ARMEncoding encoding) |
| 7506 | { |
| 7507 | #if 0 |
| 7508 | if ConditionPassed() then |
| 7509 | EncodingSpecificOperations(); |
| 7510 | rotated = ROR(R[m], rotation); |
| 7511 | R[d] = SignExtend(rotated<15:0>, 32); |
| 7512 | #endif |
| 7513 | |
| 7514 | bool success = false; |
| 7515 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 7516 | if (!success) |
| 7517 | return false; |
| 7518 | |
| 7519 | if (ConditionPassed()) |
| 7520 | { |
| 7521 | uint32_t d; |
| 7522 | uint32_t m; |
| 7523 | uint32_t rotation; |
| 7524 | |
| 7525 | // EncodingSpecificOperations(); |
| 7526 | switch (encoding) |
| 7527 | { |
| 7528 | case eEncodingT1: |
| 7529 | // d = UInt(Rd); m = UInt(Rm); rotation = 0; |
| 7530 | d = Bits32 (opcode, 2, 0); |
| 7531 | m = Bits32 (opcode, 5, 3); |
| 7532 | rotation = 0; |
| 7533 | |
| 7534 | break; |
| 7535 | |
| 7536 | case eEncodingT2: |
| 7537 | // d = UInt(Rd); m = UInt(Rm); rotation = UInt(rotate:’000’); |
| 7538 | d = Bits32 (opcode, 11, 8); |
| 7539 | m = Bits32 (opcode, 3, 0); |
| 7540 | rotation = Bits32 (opcode, 5, 4) << 3; |
| 7541 | |
| 7542 | // if BadReg(d) || BadReg(m) then UNPREDICTABLE; |
| 7543 | if (BadReg (d) || BadReg (m)) |
| 7544 | return false; |
| 7545 | |
| 7546 | break; |
| 7547 | |
| 7548 | case eEncodingA1: |
| 7549 | // d = UInt(Rd); m = UInt(Rm); rotation = UInt(rotate:’000’); |
| 7550 | d = Bits32 (opcode, 15, 12); |
| 7551 | m = Bits32 (opcode, 3, 0); |
| 7552 | rotation = Bits32 (opcode, 11, 10) << 3; |
| 7553 | |
| 7554 | // if d == 15 || m == 15 then UNPREDICTABLE; |
| 7555 | if ((d == 15) || (m == 15)) |
| 7556 | return false; |
| 7557 | |
| 7558 | break; |
| 7559 | |
| 7560 | default: |
| 7561 | return false; |
| 7562 | } |
| 7563 | |
| 7564 | uint64_t Rm = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 7565 | if (!success) |
| 7566 | return false; |
| 7567 | |
| 7568 | // rotated = ROR(R[m], rotation); |
| 7569 | uint64_t rotated = ROR (Rm, rotation); |
| 7570 | |
| 7571 | // R[d] = SignExtend(rotated<15:0>, 32); |
| 7572 | Register source_reg; |
| 7573 | source_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + m); |
| 7574 | |
| 7575 | EmulateInstruction::Context context; |
| 7576 | context.type = eContextRegisterLoad; |
| 7577 | context.SetRegister (source_reg); |
| 7578 | |
Caroline Tice | 8ce96d9 | 2011-03-03 18:27:17 +0000 | [diff] [blame] | 7579 | int64_t data = llvm::SignExtend64<16> (rotated); |
| 7580 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + d, (uint64_t) data)) |
Caroline Tice | 868198b | 2011-03-03 18:04:49 +0000 | [diff] [blame] | 7581 | return false; |
| 7582 | } |
| 7583 | |
| 7584 | return true; |
| 7585 | } |
| 7586 | |
Caroline Tice | 8ce96d9 | 2011-03-03 18:27:17 +0000 | [diff] [blame] | 7587 | // UXTB extracts an 8-bit value from a register, zero-extneds it to 32 bits, and writes the result to the destination |
| 7588 | // register. You can specify a rotation by 0, 8, 16, or 24 bits before extracting the 8-bit value. |
| 7589 | bool |
| 7590 | EmulateInstructionARM::EmulateUXTB (ARMEncoding encoding) |
| 7591 | { |
| 7592 | #if 0 |
| 7593 | if ConditionPassed() then |
| 7594 | EncodingSpecificOperations(); |
| 7595 | rotated = ROR(R[m], rotation); |
| 7596 | R[d] = ZeroExtend(rotated<7:0>, 32); |
| 7597 | #endif |
| 7598 | |
| 7599 | bool success = false; |
| 7600 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 7601 | if (!success) |
| 7602 | return false; |
| 7603 | |
| 7604 | if (ConditionPassed()) |
| 7605 | { |
| 7606 | uint32_t d; |
| 7607 | uint32_t m; |
| 7608 | uint32_t rotation; |
| 7609 | |
| 7610 | // EncodingSpecificOperations(); |
| 7611 | switch (encoding) |
| 7612 | { |
| 7613 | case eEncodingT1: |
| 7614 | // d = UInt(Rd); m = UInt(Rm); rotation = 0; |
| 7615 | d = Bits32 (opcode, 2, 0); |
| 7616 | m = Bits32 (opcode, 5, 3); |
| 7617 | rotation = 0; |
| 7618 | |
| 7619 | break; |
| 7620 | |
| 7621 | case eEncodingT2: |
| 7622 | // d = UInt(Rd); m = UInt(Rm); rotation = UInt(rotate:’000’); |
| 7623 | d = Bits32 (opcode, 11, 8); |
| 7624 | m = Bits32 (opcode, 3, 0); |
| 7625 | rotation = Bits32 (opcode, 5, 4) << 3; |
| 7626 | |
| 7627 | // if BadReg(d) || BadReg(m) then UNPREDICTABLE; |
| 7628 | if (BadReg (d) || BadReg (m)) |
| 7629 | return false; |
| 7630 | |
| 7631 | break; |
| 7632 | |
| 7633 | case eEncodingA1: |
| 7634 | // d = UInt(Rd); m = UInt(Rm); rotation = UInt(rotate:’000’); |
| 7635 | d = Bits32 (opcode, 15, 12); |
| 7636 | m = Bits32 (opcode, 3, 0); |
| 7637 | rotation = Bits32 (opcode, 11, 10) << 3; |
| 7638 | |
| 7639 | // if d == 15 || m == 15 then UNPREDICTABLE; |
| 7640 | if ((d == 15) || (m == 15)) |
| 7641 | return false; |
| 7642 | |
| 7643 | break; |
| 7644 | |
| 7645 | default: |
| 7646 | return false; |
| 7647 | } |
| 7648 | |
| 7649 | uint64_t Rm = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 7650 | if (!success) |
| 7651 | return false; |
| 7652 | |
| 7653 | // rotated = ROR(R[m], rotation); |
| 7654 | uint64_t rotated = ROR (Rm, rotation); |
| 7655 | |
| 7656 | // R[d] = ZeroExtend(rotated<7:0>, 32); |
| 7657 | Register source_reg; |
| 7658 | source_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + m); |
| 7659 | |
| 7660 | EmulateInstruction::Context context; |
| 7661 | context.type = eContextRegisterLoad; |
| 7662 | context.SetRegister (source_reg); |
| 7663 | |
| 7664 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + d, Bits32 (rotated, 7, 0))) |
| 7665 | return false; |
| 7666 | } |
| 7667 | return true; |
| 7668 | } |
| 7669 | |
Caroline Tice | 11555f2 | 2011-03-03 18:48:58 +0000 | [diff] [blame^] | 7670 | // UXTH extracts a 16-bit value from a register, zero-extends it to 32 bits, and writes the result to the destination |
| 7671 | // register. You can specify a rotation by 0, 8, 16, or 24 bits before extracting the 16-bit value. |
| 7672 | bool |
| 7673 | EmulateInstructionARM::EmulateUXTH (ARMEncoding encoding) |
| 7674 | { |
| 7675 | #if 0 |
| 7676 | if ConditionPassed() then |
| 7677 | EncodingSpecificOperations(); |
| 7678 | rotated = ROR(R[m], rotation); |
| 7679 | R[d] = ZeroExtend(rotated<15:0>, 32); |
| 7680 | #endif |
| 7681 | |
| 7682 | bool success = false; |
| 7683 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 7684 | if (!success) |
| 7685 | return false; |
| 7686 | |
| 7687 | if (ConditionPassed ()) |
| 7688 | { |
| 7689 | uint32_t d; |
| 7690 | uint32_t m; |
| 7691 | uint32_t rotation; |
| 7692 | |
| 7693 | switch (encoding) |
| 7694 | { |
| 7695 | case eEncodingT1: |
| 7696 | // d = UInt(Rd); m = UInt(Rm); rotation = 0; |
| 7697 | d = Bits32 (opcode, 2, 0); |
| 7698 | m = Bits32 (opcode, 5, 3); |
| 7699 | rotation = 0; |
| 7700 | |
| 7701 | break; |
| 7702 | |
| 7703 | case eEncodingT2: |
| 7704 | // d = UInt(Rd); m = UInt(Rm); rotation = UInt(rotate:’000’); |
| 7705 | d = Bits32 (opcode, 11, 8); |
| 7706 | m = Bits32 (opcode, 3, 0); |
| 7707 | rotation = Bits32 (opcode, 5, 4) << 3; |
| 7708 | |
| 7709 | // if BadReg(d) || BadReg(m) then UNPREDICTABLE; |
| 7710 | if (BadReg (d) || BadReg (m)) |
| 7711 | return false; |
| 7712 | |
| 7713 | break; |
| 7714 | |
| 7715 | case eEncodingA1: |
| 7716 | // d = UInt(Rd); m = UInt(Rm); rotation = UInt(rotate:’000’); |
| 7717 | d = Bits32 (opcode, 15, 12); |
| 7718 | m = Bits32 (opcode, 3, 0); |
| 7719 | rotation = Bits32 (opcode, 11, 10) << 3; |
| 7720 | |
| 7721 | // if d == 15 || m == 15 then UNPREDICTABLE; |
| 7722 | if ((d == 15) || (m == 15)) |
| 7723 | return false; |
| 7724 | |
| 7725 | break; |
| 7726 | |
| 7727 | default: |
| 7728 | return false; |
| 7729 | } |
| 7730 | |
| 7731 | uint64_t Rm = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 7732 | if (!success) |
| 7733 | return false; |
| 7734 | |
| 7735 | // rotated = ROR(R[m], rotation); |
| 7736 | uint64_t rotated = ROR (Rm, rotation); |
| 7737 | |
| 7738 | // R[d] = ZeroExtend(rotated<15:0>, 32); |
| 7739 | Register source_reg; |
| 7740 | source_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + m); |
| 7741 | |
| 7742 | EmulateInstruction::Context context; |
| 7743 | context.type = eContextRegisterLoad; |
| 7744 | context.SetRegister (source_reg); |
| 7745 | |
| 7746 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + d, Bits32 (rotated, 15, 0))) |
| 7747 | return false; |
| 7748 | } |
| 7749 | return true; |
| 7750 | } |
| 7751 | |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 7752 | // Bitwise Exclusive OR (immediate) performs a bitwise exclusive OR of a register value and an immediate value, |
| 7753 | // and writes the result to the destination register. It can optionally update the condition flags based on |
| 7754 | // the result. |
| 7755 | bool |
| 7756 | EmulateInstructionARM::EmulateEORImm (ARMEncoding encoding) |
| 7757 | { |
| 7758 | #if 0 |
| 7759 | // ARM pseudo code... |
| 7760 | if ConditionPassed() then |
| 7761 | EncodingSpecificOperations(); |
| 7762 | result = R[n] EOR imm32; |
| 7763 | if d == 15 then // Can only occur for ARM encoding |
| 7764 | ALUWritePC(result); // setflags is always FALSE here |
| 7765 | else |
| 7766 | R[d] = result; |
| 7767 | if setflags then |
| 7768 | APSR.N = result<31>; |
| 7769 | APSR.Z = IsZeroBit(result); |
| 7770 | APSR.C = carry; |
| 7771 | // APSR.V unchanged |
| 7772 | #endif |
| 7773 | |
| 7774 | bool success = false; |
| 7775 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 7776 | if (!success) |
| 7777 | return false; |
| 7778 | |
| 7779 | if (ConditionPassed()) |
| 7780 | { |
| 7781 | uint32_t Rd, Rn; |
| 7782 | uint32_t imm32; // the immediate value to be ORed to the value obtained from Rn |
| 7783 | bool setflags; |
| 7784 | uint32_t carry; // the carry bit after ARM/Thumb Expand operation |
| 7785 | switch (encoding) |
| 7786 | { |
| 7787 | case eEncodingT1: |
| 7788 | Rd = Bits32(opcode, 11, 8); |
| 7789 | Rn = Bits32(opcode, 19, 16); |
| 7790 | setflags = BitIsSet(opcode, 20); |
| 7791 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ThumbExpandImm(i:imm3:imm8, APSR.C) |
| 7792 | // if Rd == '1111' && S == '1' then SEE TEQ (immediate); |
| 7793 | if (Rd == 15 && setflags) |
| 7794 | return EmulateTEQImm(eEncodingT1); |
| 7795 | if (Rd == 13 || (Rd == 15 && !setflags) || BadReg(Rn)) |
| 7796 | return false; |
| 7797 | break; |
| 7798 | case eEncodingA1: |
| 7799 | Rd = Bits32(opcode, 15, 12); |
| 7800 | Rn = Bits32(opcode, 19, 16); |
| 7801 | setflags = BitIsSet(opcode, 20); |
| 7802 | imm32 = ARMExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ARMExpandImm(imm12, APSR.C) |
| 7803 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 7804 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 7805 | if (Rd == 15 && setflags) |
| 7806 | return false; |
| 7807 | break; |
| 7808 | default: |
| 7809 | return false; |
| 7810 | } |
| 7811 | |
| 7812 | // Read the first operand. |
| 7813 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 7814 | if (!success) |
| 7815 | return false; |
| 7816 | |
| 7817 | uint32_t result = val1 ^ imm32; |
| 7818 | |
| 7819 | EmulateInstruction::Context context; |
| 7820 | context.type = EmulateInstruction::eContextImmediate; |
| 7821 | context.SetNoArgs (); |
| 7822 | |
| 7823 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 7824 | return false; |
| 7825 | } |
| 7826 | return true; |
| 7827 | } |
| 7828 | |
| 7829 | // Bitwise Exclusive OR (register) performs a bitwise exclusive OR of a register value and an |
| 7830 | // optionally-shifted register value, and writes the result to the destination register. |
| 7831 | // It can optionally update the condition flags based on the result. |
| 7832 | bool |
| 7833 | EmulateInstructionARM::EmulateEORReg (ARMEncoding encoding) |
| 7834 | { |
| 7835 | #if 0 |
| 7836 | // ARM pseudo code... |
| 7837 | if ConditionPassed() then |
| 7838 | EncodingSpecificOperations(); |
| 7839 | (shifted, carry) = Shift_C(R[m], shift_t, shift_n, APSR.C); |
| 7840 | result = R[n] EOR shifted; |
| 7841 | if d == 15 then // Can only occur for ARM encoding |
| 7842 | ALUWritePC(result); // setflags is always FALSE here |
| 7843 | else |
| 7844 | R[d] = result; |
| 7845 | if setflags then |
| 7846 | APSR.N = result<31>; |
| 7847 | APSR.Z = IsZeroBit(result); |
| 7848 | APSR.C = carry; |
| 7849 | // APSR.V unchanged |
| 7850 | #endif |
| 7851 | |
| 7852 | bool success = false; |
| 7853 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 7854 | if (!success) |
| 7855 | return false; |
| 7856 | |
| 7857 | if (ConditionPassed()) |
| 7858 | { |
| 7859 | uint32_t Rd, Rn, Rm; |
| 7860 | ARM_ShifterType shift_t; |
| 7861 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 7862 | bool setflags; |
| 7863 | uint32_t carry; |
| 7864 | switch (encoding) |
| 7865 | { |
| 7866 | case eEncodingT1: |
| 7867 | Rd = Rn = Bits32(opcode, 2, 0); |
| 7868 | Rm = Bits32(opcode, 5, 3); |
| 7869 | setflags = !InITBlock(); |
| 7870 | shift_t = SRType_LSL; |
| 7871 | shift_n = 0; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 7872 | break; |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 7873 | case eEncodingT2: |
| 7874 | Rd = Bits32(opcode, 11, 8); |
| 7875 | Rn = Bits32(opcode, 19, 16); |
| 7876 | Rm = Bits32(opcode, 3, 0); |
| 7877 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 7878 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
| 7879 | // if Rd == '1111' && S == '1' then SEE TEQ (register); |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 7880 | if (Rd == 15 && setflags) |
| 7881 | return EmulateTEQReg(eEncodingT1); |
| 7882 | if (Rd == 13 || (Rd == 15 && !setflags) || BadReg(Rn) || BadReg(Rm)) |
| 7883 | return false; |
| 7884 | break; |
| 7885 | case eEncodingA1: |
| 7886 | Rd = Bits32(opcode, 15, 12); |
| 7887 | Rn = Bits32(opcode, 19, 16); |
| 7888 | Rm = Bits32(opcode, 3, 0); |
| 7889 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 7890 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 7891 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 7892 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 7893 | if (Rd == 15 && setflags) |
| 7894 | return false; |
| 7895 | break; |
| 7896 | default: |
| 7897 | return false; |
| 7898 | } |
| 7899 | |
| 7900 | // Read the first operand. |
| 7901 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 7902 | if (!success) |
| 7903 | return false; |
| 7904 | |
| 7905 | // Read the second operand. |
| 7906 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 7907 | if (!success) |
| 7908 | return false; |
| 7909 | |
| 7910 | uint32_t shifted = Shift_C(val2, shift_t, shift_n, APSR_C, carry); |
| 7911 | uint32_t result = val1 ^ shifted; |
| 7912 | |
| 7913 | EmulateInstruction::Context context; |
| 7914 | context.type = EmulateInstruction::eContextImmediate; |
| 7915 | context.SetNoArgs (); |
| 7916 | |
| 7917 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 7918 | return false; |
| 7919 | } |
| 7920 | return true; |
| 7921 | } |
| 7922 | |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 7923 | // Bitwise OR (immediate) performs a bitwise (inclusive) OR of a register value and an immediate value, and |
| 7924 | // writes the result to the destination register. It can optionally update the condition flags based |
| 7925 | // on the result. |
| 7926 | bool |
| 7927 | EmulateInstructionARM::EmulateORRImm (ARMEncoding encoding) |
| 7928 | { |
| 7929 | #if 0 |
| 7930 | // ARM pseudo code... |
| 7931 | if ConditionPassed() then |
| 7932 | EncodingSpecificOperations(); |
| 7933 | result = R[n] OR imm32; |
| 7934 | if d == 15 then // Can only occur for ARM encoding |
| 7935 | ALUWritePC(result); // setflags is always FALSE here |
| 7936 | else |
| 7937 | R[d] = result; |
| 7938 | if setflags then |
| 7939 | APSR.N = result<31>; |
| 7940 | APSR.Z = IsZeroBit(result); |
| 7941 | APSR.C = carry; |
| 7942 | // APSR.V unchanged |
| 7943 | #endif |
| 7944 | |
| 7945 | bool success = false; |
| 7946 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 7947 | if (!success) |
| 7948 | return false; |
| 7949 | |
| 7950 | if (ConditionPassed()) |
| 7951 | { |
| 7952 | uint32_t Rd, Rn; |
| 7953 | uint32_t imm32; // the immediate value to be ORed to the value obtained from Rn |
| 7954 | bool setflags; |
| 7955 | uint32_t carry; // the carry bit after ARM/Thumb Expand operation |
| 7956 | switch (encoding) |
| 7957 | { |
| 7958 | case eEncodingT1: |
| 7959 | Rd = Bits32(opcode, 11, 8); |
| 7960 | Rn = Bits32(opcode, 19, 16); |
| 7961 | setflags = BitIsSet(opcode, 20); |
| 7962 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ThumbExpandImm(i:imm3:imm8, APSR.C) |
| 7963 | // if Rn == ‘1111’ then SEE MOV (immediate); |
| 7964 | if (Rn == 15) |
| 7965 | return EmulateMOVRdImm(eEncodingT2); |
| 7966 | if (BadReg(Rd) || Rn == 13) |
| 7967 | return false; |
| 7968 | break; |
| 7969 | case eEncodingA1: |
| 7970 | Rd = Bits32(opcode, 15, 12); |
| 7971 | Rn = Bits32(opcode, 19, 16); |
| 7972 | setflags = BitIsSet(opcode, 20); |
| 7973 | imm32 = ARMExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ARMExpandImm(imm12, APSR.C) |
| 7974 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 7975 | if (Rd == 15 && setflags) |
| 7976 | return false; |
| 7977 | break; |
| 7978 | default: |
| 7979 | return false; |
| 7980 | } |
| 7981 | |
| 7982 | // Read the first operand. |
| 7983 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 7984 | if (!success) |
| 7985 | return false; |
| 7986 | |
| 7987 | uint32_t result = val1 | imm32; |
| 7988 | |
| 7989 | EmulateInstruction::Context context; |
| 7990 | context.type = EmulateInstruction::eContextImmediate; |
| 7991 | context.SetNoArgs (); |
| 7992 | |
| 7993 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 7994 | return false; |
| 7995 | } |
| 7996 | return true; |
| 7997 | } |
| 7998 | |
| 7999 | // Bitwise OR (register) performs a bitwise (inclusive) OR of a register value and an optionally-shifted register |
| 8000 | // value, and writes the result to the destination register. It can optionally update the condition flags based |
| 8001 | // on the result. |
| 8002 | bool |
| 8003 | EmulateInstructionARM::EmulateORRReg (ARMEncoding encoding) |
| 8004 | { |
| 8005 | #if 0 |
| 8006 | // ARM pseudo code... |
| 8007 | if ConditionPassed() then |
| 8008 | EncodingSpecificOperations(); |
| 8009 | (shifted, carry) = Shift_C(R[m], shift_t, shift_n, APSR.C); |
| 8010 | result = R[n] OR shifted; |
| 8011 | if d == 15 then // Can only occur for ARM encoding |
| 8012 | ALUWritePC(result); // setflags is always FALSE here |
| 8013 | else |
| 8014 | R[d] = result; |
| 8015 | if setflags then |
| 8016 | APSR.N = result<31>; |
| 8017 | APSR.Z = IsZeroBit(result); |
| 8018 | APSR.C = carry; |
| 8019 | // APSR.V unchanged |
| 8020 | #endif |
| 8021 | |
| 8022 | bool success = false; |
| 8023 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 8024 | if (!success) |
| 8025 | return false; |
| 8026 | |
| 8027 | if (ConditionPassed()) |
| 8028 | { |
| 8029 | uint32_t Rd, Rn, Rm; |
| 8030 | ARM_ShifterType shift_t; |
| 8031 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 8032 | bool setflags; |
| 8033 | uint32_t carry; |
| 8034 | switch (encoding) |
| 8035 | { |
| 8036 | case eEncodingT1: |
| 8037 | Rd = Rn = Bits32(opcode, 2, 0); |
| 8038 | Rm = Bits32(opcode, 5, 3); |
| 8039 | setflags = !InITBlock(); |
| 8040 | shift_t = SRType_LSL; |
| 8041 | shift_n = 0; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 8042 | break; |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 8043 | case eEncodingT2: |
| 8044 | Rd = Bits32(opcode, 11, 8); |
| 8045 | Rn = Bits32(opcode, 19, 16); |
| 8046 | Rm = Bits32(opcode, 3, 0); |
| 8047 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 8048 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
| 8049 | // if Rn == '1111' then SEE MOV (register); |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 8050 | if (Rn == 15) |
| 8051 | return EmulateMOVRdRm(eEncodingT3); |
| 8052 | if (BadReg(Rd) || Rn == 13 || BadReg(Rm)) |
| 8053 | return false; |
| 8054 | break; |
| 8055 | case eEncodingA1: |
| 8056 | Rd = Bits32(opcode, 15, 12); |
| 8057 | Rn = Bits32(opcode, 19, 16); |
| 8058 | Rm = Bits32(opcode, 3, 0); |
| 8059 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 8060 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 8061 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 8062 | if (Rd == 15 && setflags) |
| 8063 | return false; |
| 8064 | break; |
| 8065 | default: |
| 8066 | return false; |
| 8067 | } |
| 8068 | |
| 8069 | // Read the first operand. |
| 8070 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 8071 | if (!success) |
| 8072 | return false; |
| 8073 | |
| 8074 | // Read the second operand. |
| 8075 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 8076 | if (!success) |
| 8077 | return false; |
| 8078 | |
| 8079 | uint32_t shifted = Shift_C(val2, shift_t, shift_n, APSR_C, carry); |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 8080 | uint32_t result = val1 | shifted; |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 8081 | |
| 8082 | EmulateInstruction::Context context; |
| 8083 | context.type = EmulateInstruction::eContextImmediate; |
| 8084 | context.SetNoArgs (); |
| 8085 | |
| 8086 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 8087 | return false; |
| 8088 | } |
| 8089 | return true; |
| 8090 | } |
| 8091 | |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 8092 | // Reverse Subtract (immediate) subtracts a register value from an immediate value, and writes the result to |
| 8093 | // the destination register. It can optionally update the condition flags based on the result. |
| 8094 | bool |
| 8095 | EmulateInstructionARM::EmulateRSBImm (ARMEncoding encoding) |
| 8096 | { |
| 8097 | #if 0 |
| 8098 | // ARM pseudo code... |
| 8099 | if ConditionPassed() then |
| 8100 | EncodingSpecificOperations(); |
| 8101 | (result, carry, overflow) = AddWithCarry(NOT(R[n]), imm32, '1'); |
| 8102 | if d == 15 then // Can only occur for ARM encoding |
| 8103 | ALUWritePC(result); // setflags is always FALSE here |
| 8104 | else |
| 8105 | R[d] = result; |
| 8106 | if setflags then |
| 8107 | APSR.N = result<31>; |
| 8108 | APSR.Z = IsZeroBit(result); |
| 8109 | APSR.C = carry; |
| 8110 | APSR.V = overflow; |
| 8111 | #endif |
| 8112 | |
| 8113 | bool success = false; |
| 8114 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 8115 | if (!success) |
| 8116 | return false; |
| 8117 | |
| 8118 | uint32_t Rd; // the destination register |
| 8119 | uint32_t Rn; // the first operand |
| 8120 | bool setflags; |
| 8121 | uint32_t imm32; // the immediate value to be added to the value obtained from Rn |
| 8122 | switch (encoding) { |
| 8123 | case eEncodingT1: |
| 8124 | Rd = Bits32(opcode, 2, 0); |
| 8125 | Rn = Bits32(opcode, 5, 3); |
| 8126 | setflags = !InITBlock(); |
| 8127 | imm32 = 0; |
| 8128 | break; |
| 8129 | case eEncodingT2: |
| 8130 | Rd = Bits32(opcode, 11, 8); |
| 8131 | Rn = Bits32(opcode, 19, 16); |
| 8132 | setflags = BitIsSet(opcode, 20); |
| 8133 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 8134 | if (BadReg(Rd) || BadReg(Rn)) |
| 8135 | return false; |
| 8136 | break; |
| 8137 | case eEncodingA1: |
| 8138 | Rd = Bits32(opcode, 15, 12); |
| 8139 | Rn = Bits32(opcode, 19, 16); |
| 8140 | setflags = BitIsSet(opcode, 20); |
| 8141 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 8142 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 8143 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 8144 | if (Rd == 15 && setflags) |
| 8145 | return false; |
| 8146 | break; |
| 8147 | default: |
| 8148 | return false; |
| 8149 | } |
| 8150 | // Read the register value from the operand register Rn. |
| 8151 | uint32_t reg_val = ReadCoreReg(Rn, &success); |
| 8152 | if (!success) |
| 8153 | return false; |
| 8154 | |
| 8155 | AddWithCarryResult res = AddWithCarry(~reg_val, imm32, 1); |
| 8156 | |
| 8157 | EmulateInstruction::Context context; |
| 8158 | context.type = EmulateInstruction::eContextImmediate; |
| 8159 | context.SetNoArgs (); |
| 8160 | |
| 8161 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 8162 | return false; |
| 8163 | |
| 8164 | return true; |
| 8165 | } |
| 8166 | |
| 8167 | // Reverse Subtract (register) subtracts a register value from an optionally-shifted register value, and writes the |
| 8168 | // result to the destination register. It can optionally update the condition flags based on the result. |
| 8169 | bool |
| 8170 | EmulateInstructionARM::EmulateRSBReg (ARMEncoding encoding) |
| 8171 | { |
| 8172 | #if 0 |
| 8173 | // ARM pseudo code... |
| 8174 | if ConditionPassed() then |
| 8175 | EncodingSpecificOperations(); |
| 8176 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 8177 | (result, carry, overflow) = AddWithCarry(NOT(R[n]), shifted, '1'); |
| 8178 | if d == 15 then // Can only occur for ARM encoding |
| 8179 | ALUWritePC(result); // setflags is always FALSE here |
| 8180 | else |
| 8181 | R[d] = result; |
| 8182 | if setflags then |
| 8183 | APSR.N = result<31>; |
| 8184 | APSR.Z = IsZeroBit(result); |
| 8185 | APSR.C = carry; |
| 8186 | APSR.V = overflow; |
| 8187 | #endif |
| 8188 | |
| 8189 | bool success = false; |
| 8190 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 8191 | if (!success) |
| 8192 | return false; |
| 8193 | |
| 8194 | uint32_t Rd; // the destination register |
| 8195 | uint32_t Rn; // the first operand |
| 8196 | uint32_t Rm; // the second operand |
| 8197 | bool setflags; |
| 8198 | ARM_ShifterType shift_t; |
| 8199 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 8200 | switch (encoding) { |
| 8201 | case eEncodingT1: |
| 8202 | Rd = Bits32(opcode, 11, 8); |
| 8203 | Rn = Bits32(opcode, 19, 16); |
| 8204 | Rm = Bits32(opcode, 3, 0); |
| 8205 | setflags = BitIsSet(opcode, 20); |
| 8206 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
| 8207 | // if (BadReg(d) || BadReg(m)) then UNPREDICTABLE; |
| 8208 | if (BadReg(Rd) || BadReg(Rn) || BadReg(Rm)) |
| 8209 | return false; |
| 8210 | break; |
| 8211 | case eEncodingA1: |
| 8212 | Rd = Bits32(opcode, 15, 12); |
| 8213 | Rn = Bits32(opcode, 19, 16); |
| 8214 | Rm = Bits32(opcode, 3, 0); |
| 8215 | setflags = BitIsSet(opcode, 20); |
| 8216 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
| 8217 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 8218 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 8219 | if (Rd == 15 && setflags) |
| 8220 | return false; |
| 8221 | break; |
| 8222 | default: |
| 8223 | return false; |
| 8224 | } |
| 8225 | // Read the register value from register Rn. |
| 8226 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 8227 | if (!success) |
| 8228 | return false; |
| 8229 | |
| 8230 | // Read the register value from register Rm. |
| 8231 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 8232 | if (!success) |
| 8233 | return false; |
| 8234 | |
| 8235 | uint32_t shifted = Shift(val2, shift_t, shift_n, APSR_C); |
| 8236 | AddWithCarryResult res = AddWithCarry(~val1, shifted, 1); |
| 8237 | |
| 8238 | EmulateInstruction::Context context; |
| 8239 | context.type = EmulateInstruction::eContextImmediate; |
| 8240 | context.SetNoArgs(); |
| 8241 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 8242 | return false; |
| 8243 | |
| 8244 | return true; |
| 8245 | } |
| 8246 | |
Johnny Chen | 90e607b | 2011-02-23 00:07:09 +0000 | [diff] [blame] | 8247 | // Reverse Subtract with Carry (immediate) subtracts a register value and the value of NOT (Carry flag) from |
| 8248 | // an immediate value, and writes the result to the destination register. It can optionally update the condition |
| 8249 | // flags based on the result. |
| 8250 | bool |
| 8251 | EmulateInstructionARM::EmulateRSCImm (ARMEncoding encoding) |
| 8252 | { |
| 8253 | #if 0 |
| 8254 | // ARM pseudo code... |
| 8255 | if ConditionPassed() then |
| 8256 | EncodingSpecificOperations(); |
| 8257 | (result, carry, overflow) = AddWithCarry(NOT(R[n]), imm32, APSR.C); |
| 8258 | if d == 15 then |
| 8259 | ALUWritePC(result); // setflags is always FALSE here |
| 8260 | else |
| 8261 | R[d] = result; |
| 8262 | if setflags then |
| 8263 | APSR.N = result<31>; |
| 8264 | APSR.Z = IsZeroBit(result); |
| 8265 | APSR.C = carry; |
| 8266 | APSR.V = overflow; |
| 8267 | #endif |
| 8268 | |
| 8269 | bool success = false; |
| 8270 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 8271 | if (!success) |
| 8272 | return false; |
| 8273 | |
| 8274 | uint32_t Rd; // the destination register |
| 8275 | uint32_t Rn; // the first operand |
| 8276 | bool setflags; |
| 8277 | uint32_t imm32; // the immediate value to be added to the value obtained from Rn |
| 8278 | switch (encoding) { |
| 8279 | case eEncodingA1: |
| 8280 | Rd = Bits32(opcode, 15, 12); |
| 8281 | Rn = Bits32(opcode, 19, 16); |
| 8282 | setflags = BitIsSet(opcode, 20); |
| 8283 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 8284 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 8285 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 8286 | if (Rd == 15 && setflags) |
| 8287 | return false; |
| 8288 | break; |
| 8289 | default: |
| 8290 | return false; |
| 8291 | } |
| 8292 | // Read the register value from the operand register Rn. |
| 8293 | uint32_t reg_val = ReadCoreReg(Rn, &success); |
| 8294 | if (!success) |
| 8295 | return false; |
| 8296 | |
| 8297 | AddWithCarryResult res = AddWithCarry(~reg_val, imm32, APSR_C); |
| 8298 | |
| 8299 | EmulateInstruction::Context context; |
| 8300 | context.type = EmulateInstruction::eContextImmediate; |
| 8301 | context.SetNoArgs (); |
| 8302 | |
| 8303 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 8304 | return false; |
| 8305 | |
| 8306 | return true; |
| 8307 | } |
| 8308 | |
| 8309 | // Reverse Subtract with Carry (register) subtracts a register value and the value of NOT (Carry flag) from an |
| 8310 | // optionally-shifted register value, and writes the result to the destination register. It can optionally update the |
| 8311 | // condition flags based on the result. |
| 8312 | bool |
| 8313 | EmulateInstructionARM::EmulateRSCReg (ARMEncoding encoding) |
| 8314 | { |
| 8315 | #if 0 |
| 8316 | // ARM pseudo code... |
| 8317 | if ConditionPassed() then |
| 8318 | EncodingSpecificOperations(); |
| 8319 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 8320 | (result, carry, overflow) = AddWithCarry(NOT(R[n]), shifted, APSR.C); |
| 8321 | if d == 15 then |
| 8322 | ALUWritePC(result); // setflags is always FALSE here |
| 8323 | else |
| 8324 | R[d] = result; |
| 8325 | if setflags then |
| 8326 | APSR.N = result<31>; |
| 8327 | APSR.Z = IsZeroBit(result); |
| 8328 | APSR.C = carry; |
| 8329 | APSR.V = overflow; |
| 8330 | #endif |
| 8331 | |
| 8332 | bool success = false; |
| 8333 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 8334 | if (!success) |
| 8335 | return false; |
| 8336 | |
| 8337 | uint32_t Rd; // the destination register |
| 8338 | uint32_t Rn; // the first operand |
| 8339 | uint32_t Rm; // the second operand |
| 8340 | bool setflags; |
| 8341 | ARM_ShifterType shift_t; |
| 8342 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 8343 | switch (encoding) { |
| 8344 | case eEncodingA1: |
| 8345 | Rd = Bits32(opcode, 15, 12); |
| 8346 | Rn = Bits32(opcode, 19, 16); |
| 8347 | Rm = Bits32(opcode, 3, 0); |
| 8348 | setflags = BitIsSet(opcode, 20); |
| 8349 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
| 8350 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 8351 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 8352 | if (Rd == 15 && setflags) |
| 8353 | return false; |
| 8354 | break; |
| 8355 | default: |
| 8356 | return false; |
| 8357 | } |
| 8358 | // Read the register value from register Rn. |
| 8359 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 8360 | if (!success) |
| 8361 | return false; |
| 8362 | |
| 8363 | // Read the register value from register Rm. |
| 8364 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 8365 | if (!success) |
| 8366 | return false; |
| 8367 | |
| 8368 | uint32_t shifted = Shift(val2, shift_t, shift_n, APSR_C); |
| 8369 | AddWithCarryResult res = AddWithCarry(~val1, shifted, APSR_C); |
| 8370 | |
| 8371 | EmulateInstruction::Context context; |
| 8372 | context.type = EmulateInstruction::eContextImmediate; |
| 8373 | context.SetNoArgs(); |
| 8374 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 8375 | return false; |
| 8376 | |
| 8377 | return true; |
| 8378 | } |
| 8379 | |
Johnny Chen | 9b38177 | 2011-02-23 01:01:21 +0000 | [diff] [blame] | 8380 | // Subtract with Carry (immediate) subtracts an immediate value and the value of |
| 8381 | // NOT (Carry flag) from a register value, and writes the result to the destination register. |
| 8382 | // It can optionally update the condition flags based on the result. |
| 8383 | bool |
| 8384 | EmulateInstructionARM::EmulateSBCImm (ARMEncoding encoding) |
| 8385 | { |
| 8386 | #if 0 |
| 8387 | // ARM pseudo code... |
| 8388 | if ConditionPassed() then |
| 8389 | EncodingSpecificOperations(); |
Johnny Chen | 15a7a6b | 2011-02-23 23:47:56 +0000 | [diff] [blame] | 8390 | (result, carry, overflow) = AddWithCarry(R[n], NOT(imm32), APSR.C); |
Johnny Chen | 9b38177 | 2011-02-23 01:01:21 +0000 | [diff] [blame] | 8391 | if d == 15 then // Can only occur for ARM encoding |
| 8392 | ALUWritePC(result); // setflags is always FALSE here |
| 8393 | else |
| 8394 | R[d] = result; |
| 8395 | if setflags then |
| 8396 | APSR.N = result<31>; |
| 8397 | APSR.Z = IsZeroBit(result); |
| 8398 | APSR.C = carry; |
| 8399 | APSR.V = overflow; |
| 8400 | #endif |
| 8401 | |
| 8402 | bool success = false; |
| 8403 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 8404 | if (!success) |
| 8405 | return false; |
| 8406 | |
| 8407 | uint32_t Rd; // the destination register |
| 8408 | uint32_t Rn; // the first operand |
| 8409 | bool setflags; |
| 8410 | uint32_t imm32; // the immediate value to be added to the value obtained from Rn |
| 8411 | switch (encoding) { |
| 8412 | case eEncodingT1: |
| 8413 | Rd = Bits32(opcode, 11, 8); |
| 8414 | Rn = Bits32(opcode, 19, 16); |
| 8415 | setflags = BitIsSet(opcode, 20); |
| 8416 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 8417 | if (BadReg(Rd) || BadReg(Rn)) |
| 8418 | return false; |
| 8419 | break; |
| 8420 | case eEncodingA1: |
| 8421 | Rd = Bits32(opcode, 15, 12); |
| 8422 | Rn = Bits32(opcode, 19, 16); |
| 8423 | setflags = BitIsSet(opcode, 20); |
| 8424 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 8425 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 8426 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 8427 | if (Rd == 15 && setflags) |
| 8428 | return false; |
| 8429 | break; |
| 8430 | default: |
| 8431 | return false; |
| 8432 | } |
| 8433 | // Read the register value from the operand register Rn. |
| 8434 | uint32_t reg_val = ReadCoreReg(Rn, &success); |
| 8435 | if (!success) |
| 8436 | return false; |
| 8437 | |
| 8438 | AddWithCarryResult res = AddWithCarry(reg_val, ~imm32, APSR_C); |
| 8439 | |
| 8440 | EmulateInstruction::Context context; |
| 8441 | context.type = EmulateInstruction::eContextImmediate; |
| 8442 | context.SetNoArgs (); |
| 8443 | |
| 8444 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 8445 | return false; |
| 8446 | |
| 8447 | return true; |
| 8448 | } |
| 8449 | |
| 8450 | // Subtract with Carry (register) subtracts an optionally-shifted register value and the value of |
| 8451 | // NOT (Carry flag) from a register value, and writes the result to the destination register. |
| 8452 | // It can optionally update the condition flags based on the result. |
| 8453 | bool |
| 8454 | EmulateInstructionARM::EmulateSBCReg (ARMEncoding encoding) |
| 8455 | { |
| 8456 | #if 0 |
| 8457 | // ARM pseudo code... |
| 8458 | if ConditionPassed() then |
| 8459 | EncodingSpecificOperations(); |
| 8460 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 8461 | (result, carry, overflow) = AddWithCarry(R[n], NOT(shifted), APSR.C); |
| 8462 | if d == 15 then // Can only occur for ARM encoding |
| 8463 | ALUWritePC(result); // setflags is always FALSE here |
| 8464 | else |
| 8465 | R[d] = result; |
| 8466 | if setflags then |
| 8467 | APSR.N = result<31>; |
| 8468 | APSR.Z = IsZeroBit(result); |
| 8469 | APSR.C = carry; |
| 8470 | APSR.V = overflow; |
| 8471 | #endif |
| 8472 | |
| 8473 | bool success = false; |
| 8474 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 8475 | if (!success) |
| 8476 | return false; |
| 8477 | |
| 8478 | uint32_t Rd; // the destination register |
| 8479 | uint32_t Rn; // the first operand |
| 8480 | uint32_t Rm; // the second operand |
| 8481 | bool setflags; |
| 8482 | ARM_ShifterType shift_t; |
| 8483 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 8484 | switch (encoding) { |
| 8485 | case eEncodingT1: |
| 8486 | Rd = Rn = Bits32(opcode, 2, 0); |
| 8487 | Rm = Bits32(opcode, 5, 3); |
| 8488 | setflags = !InITBlock(); |
| 8489 | shift_t = SRType_LSL; |
| 8490 | shift_n = 0; |
| 8491 | break; |
| 8492 | case eEncodingT2: |
| 8493 | Rd = Bits32(opcode, 11, 8); |
| 8494 | Rn = Bits32(opcode, 19, 16); |
| 8495 | Rm = Bits32(opcode, 3, 0); |
| 8496 | setflags = BitIsSet(opcode, 20); |
| 8497 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
| 8498 | if (BadReg(Rd) || BadReg(Rn) || BadReg(Rm)) |
| 8499 | return false; |
| 8500 | break; |
| 8501 | case eEncodingA1: |
| 8502 | Rd = Bits32(opcode, 15, 12); |
| 8503 | Rn = Bits32(opcode, 19, 16); |
| 8504 | Rm = Bits32(opcode, 3, 0); |
| 8505 | setflags = BitIsSet(opcode, 20); |
| 8506 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
| 8507 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 8508 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 8509 | if (Rd == 15 && setflags) |
| 8510 | return false; |
| 8511 | break; |
| 8512 | default: |
| 8513 | return false; |
| 8514 | } |
| 8515 | // Read the register value from register Rn. |
| 8516 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 8517 | if (!success) |
| 8518 | return false; |
| 8519 | |
| 8520 | // Read the register value from register Rm. |
| 8521 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 8522 | if (!success) |
| 8523 | return false; |
| 8524 | |
| 8525 | uint32_t shifted = Shift(val2, shift_t, shift_n, APSR_C); |
| 8526 | AddWithCarryResult res = AddWithCarry(val1, ~shifted, APSR_C); |
| 8527 | |
| 8528 | EmulateInstruction::Context context; |
| 8529 | context.type = EmulateInstruction::eContextImmediate; |
| 8530 | context.SetNoArgs(); |
| 8531 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 8532 | return false; |
| 8533 | |
| 8534 | return true; |
| 8535 | } |
| 8536 | |
Johnny Chen | 15a7a6b | 2011-02-23 23:47:56 +0000 | [diff] [blame] | 8537 | // This instruction subtracts an immediate value from a register value, and writes the result |
| 8538 | // to the destination register. It can optionally update the condition flags based on the result. |
| 8539 | bool |
| 8540 | EmulateInstructionARM::EmulateSUBImmThumb (ARMEncoding encoding) |
| 8541 | { |
| 8542 | #if 0 |
| 8543 | // ARM pseudo code... |
| 8544 | if ConditionPassed() then |
| 8545 | EncodingSpecificOperations(); |
| 8546 | (result, carry, overflow) = AddWithCarry(R[n], NOT(imm32), '1'); |
| 8547 | R[d] = result; |
| 8548 | if setflags then |
| 8549 | APSR.N = result<31>; |
| 8550 | APSR.Z = IsZeroBit(result); |
| 8551 | APSR.C = carry; |
| 8552 | APSR.V = overflow; |
| 8553 | #endif |
| 8554 | |
| 8555 | bool success = false; |
| 8556 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 8557 | if (!success) |
| 8558 | return false; |
| 8559 | |
| 8560 | uint32_t Rd; // the destination register |
| 8561 | uint32_t Rn; // the first operand |
| 8562 | bool setflags; |
| 8563 | uint32_t imm32; // the immediate value to be subtracted from the value obtained from Rn |
| 8564 | switch (encoding) { |
| 8565 | case eEncodingT1: |
| 8566 | Rd = Bits32(opcode, 2, 0); |
| 8567 | Rn = Bits32(opcode, 5, 3); |
| 8568 | setflags = !InITBlock(); |
| 8569 | imm32 = Bits32(opcode, 8, 6); // imm32 = ZeroExtend(imm3, 32) |
| 8570 | break; |
| 8571 | case eEncodingT2: |
| 8572 | Rd = Rn = Bits32(opcode, 10, 8); |
| 8573 | setflags = !InITBlock(); |
| 8574 | imm32 = Bits32(opcode, 7, 0); // imm32 = ZeroExtend(imm8, 32) |
| 8575 | break; |
| 8576 | case eEncodingT3: |
| 8577 | Rd = Bits32(opcode, 11, 8); |
| 8578 | Rn = Bits32(opcode, 19, 16); |
| 8579 | setflags = BitIsSet(opcode, 20); |
| 8580 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 8581 | |
| 8582 | // if Rd == '1111' && S == '1' then SEE CMP (immediate); |
| 8583 | if (Rd == 15 && setflags) |
| 8584 | return EmulateCMPImm(eEncodingT2); |
| 8585 | |
| 8586 | // if Rn == ‘1101’ then SEE SUB (SP minus immediate); |
| 8587 | if (Rn == 13) |
| 8588 | return EmulateSUBSPImm(eEncodingT2); |
| 8589 | |
| 8590 | // if d == 13 || (d == 15 && S == '0') || n == 15 then UNPREDICTABLE; |
| 8591 | if (Rd == 13 || (Rd == 15 && !setflags) || Rn == 15) |
| 8592 | return false; |
| 8593 | break; |
| 8594 | case eEncodingT4: |
| 8595 | Rd = Bits32(opcode, 11, 8); |
| 8596 | Rn = Bits32(opcode, 19, 16); |
| 8597 | setflags = BitIsSet(opcode, 20); |
| 8598 | imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32) |
| 8599 | |
| 8600 | // if Rn == '1111' then SEE ADR; |
| 8601 | if (Rn == 15) |
| 8602 | return EmulateADR(eEncodingT2); |
| 8603 | |
| 8604 | // if Rn == '1101' then SEE SUB (SP minus immediate); |
| 8605 | if (Rn == 13) |
| 8606 | return EmulateSUBSPImm(eEncodingT3); |
| 8607 | |
| 8608 | if (BadReg(Rd)) |
| 8609 | return false; |
| 8610 | break; |
| 8611 | default: |
| 8612 | return false; |
| 8613 | } |
| 8614 | // Read the register value from the operand register Rn. |
| 8615 | uint32_t reg_val = ReadCoreReg(Rn, &success); |
| 8616 | if (!success) |
| 8617 | return false; |
| 8618 | |
| 8619 | AddWithCarryResult res = AddWithCarry(reg_val, ~imm32, 1); |
| 8620 | |
| 8621 | EmulateInstruction::Context context; |
| 8622 | context.type = EmulateInstruction::eContextImmediate; |
| 8623 | context.SetNoArgs (); |
| 8624 | |
| 8625 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 8626 | return false; |
| 8627 | |
| 8628 | return true; |
| 8629 | } |
| 8630 | |
| 8631 | // This instruction subtracts an immediate value from a register value, and writes the result |
| 8632 | // to the destination register. It can optionally update the condition flags based on the result. |
| 8633 | bool |
| 8634 | EmulateInstructionARM::EmulateSUBImmARM (ARMEncoding encoding) |
| 8635 | { |
| 8636 | #if 0 |
| 8637 | // ARM pseudo code... |
| 8638 | if ConditionPassed() then |
| 8639 | EncodingSpecificOperations(); |
| 8640 | (result, carry, overflow) = AddWithCarry(R[n], NOT(imm32), '1'); |
| 8641 | if d == 15 then |
| 8642 | ALUWritePC(result); // setflags is always FALSE here |
| 8643 | else |
| 8644 | R[d] = result; |
| 8645 | if setflags then |
| 8646 | APSR.N = result<31>; |
| 8647 | APSR.Z = IsZeroBit(result); |
| 8648 | APSR.C = carry; |
| 8649 | APSR.V = overflow; |
| 8650 | #endif |
| 8651 | |
| 8652 | bool success = false; |
| 8653 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 8654 | if (!success) |
| 8655 | return false; |
| 8656 | |
| 8657 | uint32_t Rd; // the destination register |
| 8658 | uint32_t Rn; // the first operand |
| 8659 | bool setflags; |
| 8660 | uint32_t imm32; // the immediate value to be subtracted from the value obtained from Rn |
| 8661 | switch (encoding) { |
| 8662 | case eEncodingA1: |
| 8663 | Rd = Bits32(opcode, 15, 12); |
| 8664 | Rn = Bits32(opcode, 19, 16); |
| 8665 | setflags = BitIsSet(opcode, 20); |
| 8666 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 8667 | |
| 8668 | // if Rn == ‘1111’ && S == ‘0’ then SEE ADR; |
| 8669 | if (Rn == 15 && !setflags) |
| 8670 | return EmulateADR(eEncodingA2); |
| 8671 | |
| 8672 | // if Rn == ‘1101’ then SEE SUB (SP minus immediate); |
| 8673 | if (Rn == 13) |
| 8674 | return EmulateSUBSPImm(eEncodingA1); |
| 8675 | |
| 8676 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 8677 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 8678 | if (Rd == 15 && setflags) |
| 8679 | return false; |
| 8680 | break; |
| 8681 | default: |
| 8682 | return false; |
| 8683 | } |
| 8684 | // Read the register value from the operand register Rn. |
| 8685 | uint32_t reg_val = ReadCoreReg(Rn, &success); |
| 8686 | if (!success) |
| 8687 | return false; |
| 8688 | |
| 8689 | AddWithCarryResult res = AddWithCarry(reg_val, ~imm32, 1); |
| 8690 | |
| 8691 | EmulateInstruction::Context context; |
| 8692 | context.type = EmulateInstruction::eContextImmediate; |
| 8693 | context.SetNoArgs (); |
| 8694 | |
| 8695 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 8696 | return false; |
| 8697 | |
| 8698 | return true; |
| 8699 | } |
| 8700 | |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 8701 | // Test Equivalence (immediate) performs a bitwise exclusive OR operation on a register value and an |
| 8702 | // immediate value. It updates the condition flags based on the result, and discards the result. |
| 8703 | bool |
| 8704 | EmulateInstructionARM::EmulateTEQImm (ARMEncoding encoding) |
| 8705 | { |
| 8706 | #if 0 |
| 8707 | // ARM pseudo code... |
| 8708 | if ConditionPassed() then |
| 8709 | EncodingSpecificOperations(); |
| 8710 | result = R[n] EOR imm32; |
| 8711 | APSR.N = result<31>; |
| 8712 | APSR.Z = IsZeroBit(result); |
| 8713 | APSR.C = carry; |
| 8714 | // APSR.V unchanged |
| 8715 | #endif |
| 8716 | |
| 8717 | bool success = false; |
| 8718 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 8719 | if (!success) |
| 8720 | return false; |
| 8721 | |
| 8722 | if (ConditionPassed()) |
| 8723 | { |
| 8724 | uint32_t Rn; |
| 8725 | uint32_t imm32; // the immediate value to be ANDed to the value obtained from Rn |
| 8726 | uint32_t carry; // the carry bit after ARM/Thumb Expand operation |
| 8727 | switch (encoding) |
| 8728 | { |
| 8729 | case eEncodingT1: |
| 8730 | Rn = Bits32(opcode, 19, 16); |
| 8731 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ThumbExpandImm(i:imm3:imm8, APSR.C) |
| 8732 | if (BadReg(Rn)) |
| 8733 | return false; |
| 8734 | break; |
| 8735 | case eEncodingA1: |
| 8736 | Rn = Bits32(opcode, 19, 16); |
| 8737 | imm32 = ARMExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ARMExpandImm(imm12, APSR.C) |
| 8738 | break; |
| 8739 | default: |
| 8740 | return false; |
| 8741 | } |
| 8742 | |
| 8743 | // Read the first operand. |
| 8744 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 8745 | if (!success) |
| 8746 | return false; |
| 8747 | |
| 8748 | uint32_t result = val1 ^ imm32; |
| 8749 | |
| 8750 | EmulateInstruction::Context context; |
| 8751 | context.type = EmulateInstruction::eContextImmediate; |
| 8752 | context.SetNoArgs (); |
| 8753 | |
| 8754 | if (!WriteFlags(context, result, carry)) |
| 8755 | return false; |
| 8756 | } |
| 8757 | return true; |
| 8758 | } |
| 8759 | |
| 8760 | // Test Equivalence (register) performs a bitwise exclusive OR operation on a register value and an |
| 8761 | // optionally-shifted register value. It updates the condition flags based on the result, and discards |
| 8762 | // the result. |
| 8763 | bool |
| 8764 | EmulateInstructionARM::EmulateTEQReg (ARMEncoding encoding) |
| 8765 | { |
| 8766 | #if 0 |
| 8767 | // ARM pseudo code... |
| 8768 | if ConditionPassed() then |
| 8769 | EncodingSpecificOperations(); |
| 8770 | (shifted, carry) = Shift_C(R[m], shift_t, shift_n, APSR.C); |
| 8771 | result = R[n] EOR shifted; |
| 8772 | APSR.N = result<31>; |
| 8773 | APSR.Z = IsZeroBit(result); |
| 8774 | APSR.C = carry; |
| 8775 | // APSR.V unchanged |
| 8776 | #endif |
| 8777 | |
| 8778 | bool success = false; |
| 8779 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 8780 | if (!success) |
| 8781 | return false; |
| 8782 | |
| 8783 | if (ConditionPassed()) |
| 8784 | { |
| 8785 | uint32_t Rn, Rm; |
| 8786 | ARM_ShifterType shift_t; |
| 8787 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 8788 | uint32_t carry; |
| 8789 | switch (encoding) |
| 8790 | { |
| 8791 | case eEncodingT1: |
| 8792 | Rn = Bits32(opcode, 19, 16); |
| 8793 | Rm = Bits32(opcode, 3, 0); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 8794 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 8795 | if (BadReg(Rn) || BadReg(Rm)) |
| 8796 | return false; |
| 8797 | break; |
| 8798 | case eEncodingA1: |
| 8799 | Rn = Bits32(opcode, 19, 16); |
| 8800 | Rm = Bits32(opcode, 3, 0); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 8801 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 8802 | break; |
| 8803 | default: |
| 8804 | return false; |
| 8805 | } |
| 8806 | |
| 8807 | // Read the first operand. |
| 8808 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 8809 | if (!success) |
| 8810 | return false; |
| 8811 | |
| 8812 | // Read the second operand. |
| 8813 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 8814 | if (!success) |
| 8815 | return false; |
| 8816 | |
| 8817 | uint32_t shifted = Shift_C(val2, shift_t, shift_n, APSR_C, carry); |
| 8818 | uint32_t result = val1 ^ shifted; |
| 8819 | |
| 8820 | EmulateInstruction::Context context; |
| 8821 | context.type = EmulateInstruction::eContextImmediate; |
| 8822 | context.SetNoArgs (); |
| 8823 | |
| 8824 | if (!WriteFlags(context, result, carry)) |
| 8825 | return false; |
| 8826 | } |
| 8827 | return true; |
| 8828 | } |
| 8829 | |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 8830 | // Test (immediate) performs a bitwise AND operation on a register value and an immediate value. |
| 8831 | // It updates the condition flags based on the result, and discards the result. |
| 8832 | bool |
| 8833 | EmulateInstructionARM::EmulateTSTImm (ARMEncoding encoding) |
| 8834 | { |
| 8835 | #if 0 |
| 8836 | // ARM pseudo code... |
| 8837 | if ConditionPassed() then |
| 8838 | EncodingSpecificOperations(); |
| 8839 | result = R[n] AND imm32; |
| 8840 | APSR.N = result<31>; |
| 8841 | APSR.Z = IsZeroBit(result); |
| 8842 | APSR.C = carry; |
| 8843 | // APSR.V unchanged |
| 8844 | #endif |
| 8845 | |
| 8846 | bool success = false; |
| 8847 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 8848 | if (!success) |
| 8849 | return false; |
| 8850 | |
| 8851 | if (ConditionPassed()) |
| 8852 | { |
| 8853 | uint32_t Rn; |
| 8854 | uint32_t imm32; // the immediate value to be ANDed to the value obtained from Rn |
| 8855 | uint32_t carry; // the carry bit after ARM/Thumb Expand operation |
| 8856 | switch (encoding) |
| 8857 | { |
| 8858 | case eEncodingT1: |
| 8859 | Rn = Bits32(opcode, 19, 16); |
| 8860 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ThumbExpandImm(i:imm3:imm8, APSR.C) |
| 8861 | if (BadReg(Rn)) |
| 8862 | return false; |
| 8863 | break; |
| 8864 | case eEncodingA1: |
| 8865 | Rn = Bits32(opcode, 19, 16); |
| 8866 | imm32 = ARMExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ARMExpandImm(imm12, APSR.C) |
| 8867 | break; |
| 8868 | default: |
| 8869 | return false; |
| 8870 | } |
| 8871 | |
| 8872 | // Read the first operand. |
| 8873 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 8874 | if (!success) |
| 8875 | return false; |
| 8876 | |
| 8877 | uint32_t result = val1 & imm32; |
| 8878 | |
| 8879 | EmulateInstruction::Context context; |
| 8880 | context.type = EmulateInstruction::eContextImmediate; |
| 8881 | context.SetNoArgs (); |
| 8882 | |
| 8883 | if (!WriteFlags(context, result, carry)) |
| 8884 | return false; |
| 8885 | } |
| 8886 | return true; |
| 8887 | } |
| 8888 | |
| 8889 | // Test (register) performs a bitwise AND operation on a register value and an optionally-shifted register value. |
| 8890 | // It updates the condition flags based on the result, and discards the result. |
| 8891 | bool |
| 8892 | EmulateInstructionARM::EmulateTSTReg (ARMEncoding encoding) |
| 8893 | { |
| 8894 | #if 0 |
| 8895 | // ARM pseudo code... |
| 8896 | if ConditionPassed() then |
| 8897 | EncodingSpecificOperations(); |
| 8898 | (shifted, carry) = Shift_C(R[m], shift_t, shift_n, APSR.C); |
| 8899 | result = R[n] AND shifted; |
| 8900 | APSR.N = result<31>; |
| 8901 | APSR.Z = IsZeroBit(result); |
| 8902 | APSR.C = carry; |
| 8903 | // APSR.V unchanged |
| 8904 | #endif |
| 8905 | |
| 8906 | bool success = false; |
| 8907 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 8908 | if (!success) |
| 8909 | return false; |
| 8910 | |
| 8911 | if (ConditionPassed()) |
| 8912 | { |
| 8913 | uint32_t Rn, Rm; |
| 8914 | ARM_ShifterType shift_t; |
| 8915 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 8916 | uint32_t carry; |
| 8917 | switch (encoding) |
| 8918 | { |
| 8919 | case eEncodingT1: |
| 8920 | Rn = Bits32(opcode, 2, 0); |
| 8921 | Rm = Bits32(opcode, 5, 3); |
| 8922 | shift_t = SRType_LSL; |
| 8923 | shift_n = 0; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 8924 | break; |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 8925 | case eEncodingT2: |
| 8926 | Rn = Bits32(opcode, 19, 16); |
| 8927 | Rm = Bits32(opcode, 3, 0); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 8928 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 8929 | if (BadReg(Rn) || BadReg(Rm)) |
| 8930 | return false; |
| 8931 | break; |
| 8932 | case eEncodingA1: |
| 8933 | Rn = Bits32(opcode, 19, 16); |
| 8934 | Rm = Bits32(opcode, 3, 0); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 8935 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 8936 | break; |
| 8937 | default: |
| 8938 | return false; |
| 8939 | } |
| 8940 | |
| 8941 | // Read the first operand. |
| 8942 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 8943 | if (!success) |
| 8944 | return false; |
| 8945 | |
| 8946 | // Read the second operand. |
| 8947 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 8948 | if (!success) |
| 8949 | return false; |
| 8950 | |
| 8951 | uint32_t shifted = Shift_C(val2, shift_t, shift_n, APSR_C, carry); |
| 8952 | uint32_t result = val1 & shifted; |
| 8953 | |
| 8954 | EmulateInstruction::Context context; |
| 8955 | context.type = EmulateInstruction::eContextImmediate; |
| 8956 | context.SetNoArgs (); |
| 8957 | |
| 8958 | if (!WriteFlags(context, result, carry)) |
| 8959 | return false; |
| 8960 | } |
| 8961 | return true; |
| 8962 | } |
| 8963 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 8964 | EmulateInstructionARM::ARMOpcode* |
| 8965 | EmulateInstructionARM::GetARMOpcodeForInstruction (const uint32_t opcode) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 8966 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 8967 | static ARMOpcode |
| 8968 | g_arm_opcodes[] = |
| 8969 | { |
| 8970 | //---------------------------------------------------------------------- |
| 8971 | // Prologue instructions |
| 8972 | //---------------------------------------------------------------------- |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 8973 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 8974 | // push register(s) |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 8975 | { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePUSH, "push <registers>" }, |
| 8976 | { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePUSH, "push <register>" }, |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 8977 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 8978 | // set r7 to point to a stack offset |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 8979 | { 0x0ffff000, 0x028d7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateADDRdSPImm, "add r7, sp, #<const>" }, |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 8980 | { 0x0ffff000, 0x024c7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSUBR7IPImm, "sub r7, ip, #<const>"}, |
Johnny Chen | e7cf420 | 2011-02-10 18:13:23 +0000 | [diff] [blame] | 8981 | // copy the stack pointer to ip |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 8982 | { 0x0fffffff, 0x01a0c00d, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMOVRdSP, "mov ip, sp" }, |
| 8983 | { 0x0ffff000, 0x028dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateADDRdSPImm, "add ip, sp, #<const>" }, |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 8984 | { 0x0ffff000, 0x024dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSUBIPSPImm, "sub ip, sp, #<const>"}, |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 8985 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 8986 | // adjust the stack pointer |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 8987 | { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSUBSPImm, "sub sp, sp, #<const>"}, |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 8988 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 8989 | // push one register |
| 8990 | // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH; |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 8991 | { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTRRtSP, "str Rt, [sp, #-imm12]!" }, |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 8992 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 8993 | // vector push consecutive extension register(s) |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 8994 | { 0x0fbf0f00, 0x0d2d0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"}, |
| 8995 | { 0x0fbf0f00, 0x0d2d0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"}, |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 8996 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 8997 | //---------------------------------------------------------------------- |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 8998 | // Epilogue instructions |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 8999 | //---------------------------------------------------------------------- |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 9000 | |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 9001 | { 0x0fff0000, 0x08bd0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePOP, "pop <registers>"}, |
| 9002 | { 0x0fff0fff, 0x049d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePOP, "pop <register>"}, |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 9003 | { 0x0fbf0f00, 0x0cbd0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"}, |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 9004 | { 0x0fbf0f00, 0x0cbd0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"}, |
| 9005 | |
| 9006 | //---------------------------------------------------------------------- |
| 9007 | // Supervisor Call (previously Software Interrupt) |
| 9008 | //---------------------------------------------------------------------- |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 9009 | { 0x0f000000, 0x0f000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSVC, "svc #imm24"}, |
| 9010 | |
| 9011 | //---------------------------------------------------------------------- |
| 9012 | // Branch instructions |
| 9013 | //---------------------------------------------------------------------- |
Johnny Chen | 696b4ef | 2011-02-24 21:54:22 +0000 | [diff] [blame] | 9014 | { 0x0f000000, 0x0a000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateB, "b #imm24"}, |
Johnny Chen | 383d629 | 2011-02-11 21:23:32 +0000 | [diff] [blame] | 9015 | // To resolve ambiguity, "blx <label>" should come before "bl <label>". |
| 9016 | { 0xfe000000, 0xfa000000, ARMV5_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"}, |
| 9017 | { 0x0f000000, 0x0b000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "bl <label>"}, |
| 9018 | { 0x0ffffff0, 0x012fff30, ARMV5_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"}, |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 9019 | // for example, "bx lr" |
| 9020 | { 0x0ffffff0, 0x012fff10, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBXRm, "bx <Rm>"}, |
Johnny Chen | 59e6ab7 | 2011-02-24 21:01:20 +0000 | [diff] [blame] | 9021 | // bxj |
| 9022 | { 0x0ffffff0, 0x012fff20, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBXJRm, "bxj <Rm>"}, |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 9023 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 9024 | //---------------------------------------------------------------------- |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 9025 | // Data-processing instructions |
| 9026 | //---------------------------------------------------------------------- |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 9027 | // adc (immediate) |
| 9028 | { 0x0fe00000, 0x02a00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateADCImm, "adc{s}<c> <Rd>, <Rn>, #const"}, |
| 9029 | // adc (register) |
| 9030 | { 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] | 9031 | // add (immediate) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 9032 | { 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] | 9033 | // add (register) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 9034 | { 0x0fe00010, 0x00800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateADDReg, "add{s}<c> <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | a695f95 | 2011-02-23 21:24:25 +0000 | [diff] [blame] | 9035 | // adr |
| 9036 | { 0x0fff0000, 0x028f0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateADR, "add<c> <Rd>, PC, #<const>"}, |
| 9037 | { 0x0fff0000, 0x024f0000, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateADR, "sub<c> <Rd>, PC, #<const>"}, |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 9038 | // and (immediate) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 9039 | { 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] | 9040 | // and (register) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 9041 | { 0x0fe00010, 0x00000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateANDReg, "and{s}<c> <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | b9f02cf | 2011-02-24 01:15:17 +0000 | [diff] [blame] | 9042 | // bic (immediate) |
| 9043 | { 0x0fe00000, 0x03c00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBICImm, "bic{s}<c> <Rd>, <Rn>, #const"}, |
| 9044 | // bic (register) |
| 9045 | { 0x0fe00010, 0x01c00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBICReg, "bic{s}<c> <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 9046 | // eor (immediate) |
| 9047 | { 0x0fe00000, 0x02200000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateEORImm, "eor{s}<c> <Rd>, <Rn>, #const"}, |
| 9048 | // eor (register) |
| 9049 | { 0x0fe00010, 0x00200000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateEORReg, "eor{s}<c> <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 9050 | // orr (immediate) |
| 9051 | { 0x0fe00000, 0x03800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateORRImm, "orr{s}<c> <Rd>, <Rn>, #const"}, |
| 9052 | // orr (register) |
| 9053 | { 0x0fe00010, 0x01800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateORRReg, "orr{s}<c> <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 9054 | // rsb (immediate) |
| 9055 | { 0x0fe00000, 0x02600000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRSBImm, "rsb{s}<c> <Rd>, <Rn>, #<const>"}, |
| 9056 | // rsb (register) |
| 9057 | { 0x0fe00010, 0x00600000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRSBReg, "rsb{s}<c> <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 90e607b | 2011-02-23 00:07:09 +0000 | [diff] [blame] | 9058 | // rsc (immediate) |
| 9059 | { 0x0fe00000, 0x02e00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRSCImm, "rsc{s}<c> <Rd>, <Rn>, #<const>"}, |
| 9060 | // rsc (register) |
| 9061 | { 0x0fe00010, 0x00e00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRSCReg, "rsc{s}<c> <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 9b38177 | 2011-02-23 01:01:21 +0000 | [diff] [blame] | 9062 | // sbc (immediate) |
| 9063 | { 0x0fe00000, 0x02c00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSBCImm, "sbc{s}<c> <Rd>, <Rn>, #<const>"}, |
| 9064 | // sbc (register) |
| 9065 | { 0x0fe00010, 0x00c00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSBCReg, "sbc{s}<c> <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 15a7a6b | 2011-02-23 23:47:56 +0000 | [diff] [blame] | 9066 | // sub (immediate, ARM) |
| 9067 | { 0x0fe00000, 0x02400000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSUBImmARM, "sub{s}<c> <Rd>, <Rn>, #<const>"}, |
Johnny Chen | c9e747f | 2011-02-23 01:55:07 +0000 | [diff] [blame] | 9068 | // sub (sp minus immediate) |
| 9069 | { 0x0fef0000, 0x024d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSUBSPImm, "sub{s}<c> <Rd>, sp, #<const>"}, |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 9070 | // teq (immediate) |
| 9071 | { 0x0ff0f000, 0x03300000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateTEQImm, "teq<c> <Rn>, #const"}, |
| 9072 | // teq (register) |
| 9073 | { 0x0ff0f010, 0x01300000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateTEQReg, "teq<c> <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 9074 | // tst (immediate) |
| 9075 | { 0x0ff0f000, 0x03100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateTSTImm, "tst<c> <Rn>, #const"}, |
| 9076 | // tst (register) |
| 9077 | { 0x0ff0f010, 0x01100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateTSTReg, "tst<c> <Rn>, <Rm> {,<shift>}"}, |
| 9078 | |
| 9079 | |
Johnny Chen | 01d6157 | 2011-02-25 00:23:25 +0000 | [diff] [blame] | 9080 | // mov (register) |
| 9081 | { 0x0fef0ff0, 0x01a00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMOVRdRm, "mov{s}<c> <Rd>, <Rm>"}, |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 9082 | // mvn (immediate) |
| 9083 | { 0x0fef0000, 0x03e00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMVNImm, "mvn{s}<c> <Rd>, #<const>"}, |
| 9084 | // mvn (register) |
| 9085 | { 0x0fef0010, 0x01e00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMVNReg, "mvn{s}<c> <Rd>, <Rm> {,<shift>}"}, |
Johnny Chen | 3847dad | 2011-02-22 02:00:12 +0000 | [diff] [blame] | 9086 | // cmn (immediate) |
| 9087 | { 0x0ff0f000, 0x03700000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateCMNImm, "cmn<c> <Rn>, #<const>"}, |
| 9088 | // cmn (register) |
| 9089 | { 0x0ff0f010, 0x01700000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateCMNReg, "cmn<c> <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 9090 | // cmp (immediate) |
| 9091 | { 0x0ff0f000, 0x03500000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateCMPImm, "cmp<c> <Rn>, #<const>"}, |
| 9092 | // cmp (register) |
| 9093 | { 0x0ff0f010, 0x01500000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateCMPReg, "cmp<c> <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 9094 | // asr (immediate) |
| 9095 | { 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] | 9096 | // asr (register) |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 9097 | { 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] | 9098 | // lsl (immediate) |
| 9099 | { 0x0fef0070, 0x01a00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLSLImm, "lsl{s}<c> <Rd>, <Rm>, #imm"}, |
| 9100 | // lsl (register) |
| 9101 | { 0x0fef00f0, 0x01a00010, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLSLReg, "lsl{s}<c> <Rd>, <Rn>, <Rm>"}, |
| 9102 | // lsr (immediate) |
| 9103 | { 0x0fef0070, 0x01a00020, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLSRImm, "lsr{s}<c> <Rd>, <Rm>, #imm"}, |
| 9104 | // lsr (register) |
| 9105 | { 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] | 9106 | // rrx is a special case encoding of ror (immediate) |
| 9107 | { 0x0fef0ff0, 0x01a00060, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRRX, "rrx{s}<c> <Rd>, <Rm>"}, |
| 9108 | // ror (immediate) |
| 9109 | { 0x0fef0070, 0x01a00060, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRORImm, "ror{s}<c> <Rd>, <Rm>, #imm"}, |
| 9110 | // ror (register) |
| 9111 | { 0x0fef00f0, 0x01a00070, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRORReg, "ror{s}<c> <Rd>, <Rn>, <Rm>"}, |
Caroline Tice | 5c1e2ed | 2011-03-02 22:43:54 +0000 | [diff] [blame] | 9112 | // mul |
| 9113 | { 0x0fe000f0, 0x00000090, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMUL, "mul{s}<c> <Rd>,<R>,<Rm>" }, |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 9114 | |
| 9115 | //---------------------------------------------------------------------- |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 9116 | // Load instructions |
| 9117 | //---------------------------------------------------------------------- |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 9118 | { 0x0fd00000, 0x08900000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDM, "ldm<c> <Rn>{!} <registers>" }, |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 9119 | { 0x0fd00000, 0x08100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMDA, "ldmda<c> <Rn>{!} <registers>" }, |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 9120 | { 0x0fd00000, 0x09100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMDB, "ldmdb<c> <Rn>{!} <registers>" }, |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 9121 | { 0x0fd00000, 0x09900000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMIB, "ldmib<c> <Rn<{!} <registers>" }, |
Caroline Tice | 4d729c5 | 2011-02-18 00:55:53 +0000 | [diff] [blame] | 9122 | { 0x0e500000, 0x04100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRImmediateARM, "ldr<c> <Rt> [<Rn> {#+/-<imm12>}]" }, |
Caroline Tice | fe47911 | 2011-02-18 18:52:37 +0000 | [diff] [blame] | 9123 | { 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] | 9124 | { 0x0e5f0000, 0x045f0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRBLiteral, "ldrb<c> <Rt>, [...]"}, |
Caroline Tice | 0491b3b | 2011-02-28 22:39:58 +0000 | [diff] [blame] | 9125 | { 0xfe500010, 0x06500000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRBRegister, "ldrb<c> <Rt>, [<Rn>,+/-<Rm>{, <shift>}]{!}" }, |
Caroline Tice | 952b538 | 2011-02-28 23:15:24 +0000 | [diff] [blame] | 9126 | { 0x0e5f00f0, 0x005f00b0, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRHLiteral, "ldrh<c> <Rt>, <label>" }, |
Caroline Tice | 0e6bc95 | 2011-03-01 18:00:42 +0000 | [diff] [blame] | 9127 | { 0x0e5000f0, 0x001000b0, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRHRegister, "ldrh<c> <Rt>,[<Rn>,+/-<Rm>]{!}" }, |
Caroline Tice | a5e28af | 2011-03-01 21:53:03 +0000 | [diff] [blame] | 9128 | { 0x0e5000f0, 0x005000d0, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRSBImmediate, "ldrsb<c> <Rt>, [<Rn>{,#+/-<imm8>}]" }, |
Caroline Tice | 5f59391 | 2011-03-01 22:25:17 +0000 | [diff] [blame] | 9129 | { 0x0e5f00f0, 0x005f00d0, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRSBLiteral, "ldrsb<c> <Rt> <label>" }, |
Caroline Tice | 672f311 | 2011-03-01 23:55:59 +0000 | [diff] [blame] | 9130 | { 0x0e5000f0, 0x001000d0, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRSBRegister, "ldrsb<c> <Rt>,[<Rn>,+/-<Rm>]{!}" }, |
Caroline Tice | 78fb563 | 2011-03-02 00:39:42 +0000 | [diff] [blame] | 9131 | { 0x0e5000f0, 0x005000f0, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRSHImmediate, "ldrsh<c> <Rt>,[<Rn>{,#+/-<imm8>}]"}, |
Caroline Tice | 291a3e9 | 2011-03-02 21:13:44 +0000 | [diff] [blame] | 9132 | { 0x0e5f00f0, 0x005f00f0, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRSHLiteral, "ldrsh<c> <Rt>,<label>" }, |
| 9133 | { 0x0e5000f0, 0x001000f0, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRSHRegister, "ldrsh<c> <Rt>,[<Rn>,+/-<Rm>]{!}" }, |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 9134 | |
| 9135 | //---------------------------------------------------------------------- |
| 9136 | // Store instructions |
| 9137 | //---------------------------------------------------------------------- |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 9138 | { 0x0fd00000, 0x08800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTM, "stm<c> <Rn>{!} <registers>" }, |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 9139 | { 0x0fd00000, 0x08000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTMDA, "stmda<c> <Rn>{!} <registers>" }, |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 9140 | { 0x0fd00000, 0x09000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTMDB, "stmdb<c> <Rn>{!} <registers>" }, |
Caroline Tice | 3fd63e9 | 2011-02-16 00:33:43 +0000 | [diff] [blame] | 9141 | { 0x0fd00000, 0x09800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTMIB, "stmib<c> <Rn>{!} <registers>" }, |
Caroline Tice | 6bf6516 | 2011-03-03 17:42:58 +0000 | [diff] [blame] | 9142 | { 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] | 9143 | |
Caroline Tice | 6bf6516 | 2011-03-03 17:42:58 +0000 | [diff] [blame] | 9144 | //---------------------------------------------------------------------- |
| 9145 | // Other instructions |
| 9146 | //---------------------------------------------------------------------- |
Caroline Tice | 868198b | 2011-03-03 18:04:49 +0000 | [diff] [blame] | 9147 | { 0x0fff00f0, 0x06af00f0, ARMV6_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSXTB, "sxtb<c> <Rd>,<Rm>{,<rotation>}" }, |
Caroline Tice | 8ce96d9 | 2011-03-03 18:27:17 +0000 | [diff] [blame] | 9148 | { 0x0fff00f0, 0x06bf0070, ARMV6_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSXTH, "sxth<c> <Rd>,<Rm>{,<rotation>}" }, |
Caroline Tice | 11555f2 | 2011-03-03 18:48:58 +0000 | [diff] [blame^] | 9149 | { 0x0fff00f0, 0x06ef0070, ARMV6_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateUXTB, "uxtb<c> <Rd>,<Rm>{,<rotation>}" }, |
| 9150 | { 0x0fff00f0, 0x06ff0070, ARMV6_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateUXTH, "uxth<c> <Rd>,<Rm>{,<rotation>}" } |
Caroline Tice | 6bf6516 | 2011-03-03 17:42:58 +0000 | [diff] [blame] | 9151 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 9152 | }; |
| 9153 | static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode); |
| 9154 | |
| 9155 | for (size_t i=0; i<k_num_arm_opcodes; ++i) |
| 9156 | { |
| 9157 | if ((g_arm_opcodes[i].mask & opcode) == g_arm_opcodes[i].value) |
| 9158 | return &g_arm_opcodes[i]; |
| 9159 | } |
| 9160 | return NULL; |
| 9161 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 9162 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 9163 | |
| 9164 | EmulateInstructionARM::ARMOpcode* |
| 9165 | EmulateInstructionARM::GetThumbOpcodeForInstruction (const uint32_t opcode) |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 9166 | { |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 9167 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 9168 | static ARMOpcode |
| 9169 | g_thumb_opcodes[] = |
| 9170 | { |
| 9171 | //---------------------------------------------------------------------- |
| 9172 | // Prologue instructions |
| 9173 | //---------------------------------------------------------------------- |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 9174 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 9175 | // push register(s) |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 9176 | { 0xfffffe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePUSH, "push <registers>" }, |
| 9177 | { 0xffff0000, 0xe92d0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePUSH, "push.w <registers>" }, |
| 9178 | { 0xffff0fff, 0xf84d0d04, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePUSH, "push.w <register>" }, |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 9179 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 9180 | // set r7 to point to a stack offset |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 9181 | { 0xffffff00, 0x0000af00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateADDRdSPImm, "add r7, sp, #imm" }, |
Johnny Chen | e7cf420 | 2011-02-10 18:13:23 +0000 | [diff] [blame] | 9182 | // copy the stack pointer to r7 |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 9183 | { 0xffffffff, 0x0000466f, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMOVRdSP, "mov r7, sp" }, |
Johnny Chen | e7cf420 | 2011-02-10 18:13:23 +0000 | [diff] [blame] | 9184 | // 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] | 9185 | { 0xffffffc0, 0x00004640, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMOVLowHigh, "mov r0-r7, r8-r15" }, |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 9186 | |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 9187 | // PC-relative load into register (see also EmulateADDSPRm) |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 9188 | { 0xfffff800, 0x00004800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRtPCRelative, "ldr <Rt>, [PC, #imm]"}, |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 9189 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 9190 | // adjust the stack pointer |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 9191 | { 0xffffff87, 0x00004485, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateADDSPRm, "add sp, <Rm>"}, |
Johnny Chen | c9e747f | 2011-02-23 01:55:07 +0000 | [diff] [blame] | 9192 | { 0xffffff80, 0x0000b080, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSUBSPImm, "sub sp, sp, #imm"}, |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 9193 | { 0xfbef8f00, 0xf1ad0d00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSUBSPImm, "sub.w sp, sp, #<const>"}, |
| 9194 | { 0xfbff8f00, 0xf2ad0d00, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSUBSPImm, "subw sp, sp, #imm12"}, |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 9195 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 9196 | // vector push consecutive extension register(s) |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 9197 | { 0xffbf0f00, 0xed2d0b00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"}, |
| 9198 | { 0xffbf0f00, 0xed2d0a00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"}, |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 9199 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 9200 | //---------------------------------------------------------------------- |
| 9201 | // Epilogue instructions |
| 9202 | //---------------------------------------------------------------------- |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 9203 | |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 9204 | { 0xffffff80, 0x0000b000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateADDSPImm, "add sp, #imm"}, |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 9205 | { 0xfffffe00, 0x0000bc00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePOP, "pop <registers>"}, |
| 9206 | { 0xffff0000, 0xe8bd0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePOP, "pop.w <registers>" }, |
| 9207 | { 0xffff0fff, 0xf85d0d04, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePOP, "pop.w <register>" }, |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 9208 | { 0xffbf0f00, 0xecbd0b00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"}, |
| 9209 | { 0xffbf0f00, 0xecbd0a00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"}, |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 9210 | |
| 9211 | //---------------------------------------------------------------------- |
| 9212 | // Supervisor Call (previously Software Interrupt) |
| 9213 | //---------------------------------------------------------------------- |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 9214 | { 0xffffff00, 0x0000df00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSVC, "svc #imm8"}, |
| 9215 | |
| 9216 | //---------------------------------------------------------------------- |
| 9217 | // If Then makes up to four following instructions conditional. |
| 9218 | //---------------------------------------------------------------------- |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 9219 | { 0xffffff00, 0x0000bf00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateIT, "it{<x>{<y>{<z>}}} <firstcond>"}, |
| 9220 | |
| 9221 | //---------------------------------------------------------------------- |
| 9222 | // Branch instructions |
| 9223 | //---------------------------------------------------------------------- |
| 9224 | // To resolve ambiguity, "b<c> #imm8" should come after "svc #imm8". |
| 9225 | { 0xfffff000, 0x0000d000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateB, "b<c> #imm8 (outside IT)"}, |
Johnny Chen | 696b4ef | 2011-02-24 21:54:22 +0000 | [diff] [blame] | 9226 | { 0xffff8000, 0x0000e000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateB, "b<c> #imm11 (outside or last in IT)"}, |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9227 | { 0xf800d000, 0xf0008000, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateB, "b<c>.w #imm8 (outside IT)"}, |
Johnny Chen | 696b4ef | 2011-02-24 21:54:22 +0000 | [diff] [blame] | 9228 | { 0xf800d000, 0xf0009000, ARMV6T2_ABOVE, eEncodingT4, eSize32, &EmulateInstructionARM::EmulateB, "b<c>.w #imm8 (outside or last in IT)"}, |
Johnny Chen | 383d629 | 2011-02-11 21:23:32 +0000 | [diff] [blame] | 9229 | // J1 == J2 == 1 |
| 9230 | { 0xf800f800, 0xf000f800, ARMV4T_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "bl <label>"}, |
| 9231 | // J1 == J2 == 1 |
| 9232 | { 0xf800e800, 0xf000e800, ARMV5_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"}, |
| 9233 | { 0xffffff87, 0x00004780, ARMV5_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"}, |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 9234 | // for example, "bx lr" |
| 9235 | { 0xffffff87, 0x00004700, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBXRm, "bx <Rm>"}, |
Johnny Chen | 59e6ab7 | 2011-02-24 21:01:20 +0000 | [diff] [blame] | 9236 | // bxj |
| 9237 | { 0xfff0ffff, 0xf3c08f00, ARMV5J_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateBXJRm, "bxj <Rm>"}, |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 9238 | // compare and branch |
| 9239 | { 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] | 9240 | // table branch byte |
| 9241 | { 0xfff0fff0, 0xe8d0f000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateTB, "tbb<c> <Rn>, <Rm>"}, |
| 9242 | // table branch halfword |
| 9243 | { 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] | 9244 | |
| 9245 | //---------------------------------------------------------------------- |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 9246 | // Data-processing instructions |
| 9247 | //---------------------------------------------------------------------- |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 9248 | // adc (immediate) |
| 9249 | { 0xfbe08000, 0xf1400000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateADCImm, "adc{s}<c> <Rd>, <Rn>, #<const>"}, |
| 9250 | // adc (register) |
| 9251 | { 0xffffffc0, 0x00004140, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateADCReg, "adcs|adc<c> <Rdn>, <Rm>"}, |
| 9252 | { 0xffe08000, 0xeb400000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateADCReg, "adc{s}<c>.w <Rd>, <Rn>, <Rm> {,<shift>}"}, |
| 9253 | // add (register) |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 9254 | { 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] | 9255 | // 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] | 9256 | { 0xffffff00, 0x00004400, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateADDReg, "add<c> <Rdn>, <Rm>"}, |
Johnny Chen | a695f95 | 2011-02-23 21:24:25 +0000 | [diff] [blame] | 9257 | // adr |
| 9258 | { 0xfffff800, 0x0000a000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateADR, "add<c> <Rd>, PC, #<const>"}, |
| 9259 | { 0xfbff8000, 0xf2af0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateADR, "sub<c> <Rd>, PC, #<const>"}, |
| 9260 | { 0xfbff8000, 0xf20f0000, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateADR, "add<c> <Rd>, PC, #<const>"}, |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 9261 | // and (immediate) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 9262 | { 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] | 9263 | // and (register) |
| 9264 | { 0xffffffc0, 0x00004000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateANDReg, "ands|and<c> <Rdn>, <Rm>"}, |
| 9265 | { 0xffe08000, 0xea000000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateANDReg, "and{s}<c>.w <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | b9f02cf | 2011-02-24 01:15:17 +0000 | [diff] [blame] | 9266 | // bic (immediate) |
| 9267 | { 0xfbe08000, 0xf0200000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateBICImm, "bic{s}<c> <Rd>, <Rn>, #<const>"}, |
| 9268 | // bic (register) |
| 9269 | { 0xffffffc0, 0x00004380, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateBICReg, "bics|bic<c> <Rdn>, <Rm>"}, |
| 9270 | { 0xffe08000, 0xea200000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateBICReg, "bic{s}<c>.w <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 9271 | // eor (immediate) |
| 9272 | { 0xfbe08000, 0xf0800000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateEORImm, "eor{s}<c> <Rd>, <Rn>, #<const>"}, |
| 9273 | // eor (register) |
| 9274 | { 0xffffffc0, 0x00004040, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateEORReg, "eors|eor<c> <Rdn>, <Rm>"}, |
| 9275 | { 0xffe08000, 0xea800000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateEORReg, "eor{s}<c>.w <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 9276 | // orr (immediate) |
| 9277 | { 0xfbe08000, 0xf0400000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateORRImm, "orr{s}<c> <Rd>, <Rn>, #<const>"}, |
| 9278 | // orr (register) |
| 9279 | { 0xffffffc0, 0x00004300, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateORRReg, "orrs|orr<c> <Rdn>, <Rm>"}, |
| 9280 | { 0xffe08000, 0xea400000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateORRReg, "orr{s}<c>.w <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 9281 | // rsb (immediate) |
| 9282 | { 0xffffffc0, 0x00004240, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateRSBImm, "rsbs|rsb<c> <Rd>, <Rn>, #0"}, |
| 9283 | { 0xfbe08000, 0xf1c00000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateRSBImm, "rsb{s}<c>.w <Rd>, <Rn>, #<const>"}, |
| 9284 | // rsb (register) |
| 9285 | { 0xffe08000, 0xea400000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateRSBReg, "rsb{s}<c>.w <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 9b38177 | 2011-02-23 01:01:21 +0000 | [diff] [blame] | 9286 | // sbc (immediate) |
| 9287 | { 0xfbe08000, 0xf1600000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateSBCImm, "sbc{s}<c> <Rd>, <Rn>, #<const>"}, |
| 9288 | // sbc (register) |
| 9289 | { 0xffffffc0, 0x00004180, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSBCReg, "sbcs|sbc<c> <Rdn>, <Rm>"}, |
| 9290 | { 0xffe08000, 0xeb600000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSBCReg, "sbc{s}<c>.w <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Caroline Tice | dcc11b3 | 2011-03-02 23:57:02 +0000 | [diff] [blame] | 9291 | // add (immediate, Thumb) |
| 9292 | { 0xfffffe00, 0x00001c00, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateADDImmThumb, "adds|add<c> <Rd>,<Rn>,#<imm3>" }, |
| 9293 | { 0xfffff800, 0x00003000, ARMV4T_ABOVE, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateADDImmThumb, "adds|add<c> <Rdn>,#<imm8>" }, |
| 9294 | { 0xfbe08000, 0xf1000000, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateADDImmThumb, "add{s}<c>.w <Rd>,<Rn>,#<const>" }, |
| 9295 | { 0xfbf08000, 0xf2000000, ARMV6T2_ABOVE, eEncodingT4, eSize32, &EmulateInstructionARM::EmulateADDImmThumb, "addw<c> <Rd>,<Rn>,#<imm12>" }, |
Johnny Chen | 15a7a6b | 2011-02-23 23:47:56 +0000 | [diff] [blame] | 9296 | // sub (immediate, Thumb) |
| 9297 | { 0xfffffe00, 0x00001e00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSUBImmThumb, "subs|sub<c> <Rd>, <Rn> #imm3"}, |
| 9298 | { 0xfffff800, 0x00003800, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateSUBImmThumb, "subs|sub<c> <Rdn>, #imm8"}, |
| 9299 | { 0xfbe08000, 0xf1a00000, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSUBImmThumb, "sub{s}<c>.w <Rd>, <Rn>, #<const>"}, |
| 9300 | { 0xfbf08000, 0xf2a00000, ARMV6T2_ABOVE, eEncodingT4, eSize32, &EmulateInstructionARM::EmulateSUBImmThumb, "subw<c> <Rd>, <Rn>, #imm12"}, |
Johnny Chen | c9e747f | 2011-02-23 01:55:07 +0000 | [diff] [blame] | 9301 | // sub (sp minus immediate) |
| 9302 | { 0xfbef8000, 0xf1ad0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSUBSPImm, "sub{s}.w <Rd>, sp, #<const>"}, |
| 9303 | { 0xfbff8000, 0xf2ad0000, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSUBSPImm, "subw<c> <Rd>, sp, #imm12"}, |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 9304 | // teq (immediate) |
| 9305 | { 0xfbf08f00, 0xf0900f00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateTEQImm, "teq<c> <Rn>, #<const>"}, |
| 9306 | // teq (register) |
| 9307 | { 0xfff08f00, 0xea900f00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateTEQReg, "teq<c> <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 9308 | // tst (immediate) |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 9309 | { 0xfbf08f00, 0xf0100f00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateTSTImm, "tst<c> <Rn>, #<const>"}, |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 9310 | // tst (register) |
| 9311 | { 0xffffffc0, 0x00004200, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateTSTReg, "tst<c> <Rdn>, <Rm>"}, |
| 9312 | { 0xfff08f00, 0xea100f00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateTSTReg, "tst<c>.w <Rn>, <Rm> {,<shift>}"}, |
| 9313 | |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 9314 | |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 9315 | // move from high register to high register |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 9316 | { 0xffffff00, 0x00004600, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMOVRdRm, "mov<c> <Rd>, <Rm>"}, |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 9317 | // move from low register to low register |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 9318 | { 0xffffffc0, 0x00000000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateMOVRdRm, "movs <Rd>, <Rm>"}, |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 9319 | // mov{s}<c>.w <Rd>, <Rm> |
| 9320 | { 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] | 9321 | // move immediate |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 9322 | { 0xfffff800, 0x00002000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMOVRdImm, "movs|mov<c> <Rd>, #imm8"}, |
| 9323 | { 0xfbef8000, 0xf04f0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateMOVRdImm, "mov{s}<c>.w <Rd>, #<const>"}, |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 9324 | // mvn (immediate) |
| 9325 | { 0xfbef8000, 0xf06f0000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateMVNImm, "mvn{s} <Rd>, #<const>"}, |
| 9326 | // mvn (register) |
| 9327 | { 0xffffffc0, 0x000043c0, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMVNReg, "mvns|mvn<c> <Rd>, <Rm>"}, |
| 9328 | { 0xffef8000, 0xea6f0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateMVNReg, "mvn{s}<c>.w <Rd>, <Rm> {,<shift>}"}, |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 9329 | // cmn (immediate) |
Johnny Chen | 688926f | 2011-02-22 19:01:11 +0000 | [diff] [blame] | 9330 | { 0xfbf08f00, 0xf1100f00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateCMNImm, "cmn<c> <Rn>, #<const>"}, |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 9331 | // cmn (register) |
| 9332 | { 0xffffffc0, 0x000042c0, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCMNReg, "cmn<c> <Rn>, <Rm>"}, |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 9333 | { 0xfff08f00, 0xeb100f00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateCMNReg, "cmn<c> <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 9334 | // cmp (immediate) |
| 9335 | { 0xfffff800, 0x00002800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCMPImm, "cmp<c> <Rn>, #imm8"}, |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 9336 | { 0xfbf08f00, 0xf1b00f00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateCMPImm, "cmp<c>.w <Rn>, #<const>"}, |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 9337 | // cmp (register) (Rn and Rm both from r0-r7) |
| 9338 | { 0xffffffc0, 0x00004280, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCMPReg, "cmp<c> <Rn>, <Rm>"}, |
| 9339 | // cmp (register) (Rn and Rm not both from r0-r7) |
| 9340 | { 0xffffff00, 0x00004500, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateCMPReg, "cmp<c> <Rn>, <Rm>"}, |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 9341 | // asr (immediate) |
| 9342 | { 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] | 9343 | { 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] | 9344 | // asr (register) |
| 9345 | { 0xffffffc0, 0x00004100, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateASRReg, "asrs|asr<c> <Rdn>, <Rm>"}, |
| 9346 | { 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] | 9347 | // lsl (immediate) |
| 9348 | { 0xfffff800, 0x00000000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLSLImm, "lsls|lsl<c> <Rd>, <Rm>, #imm"}, |
| 9349 | { 0xffef8030, 0xea4f0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLSLImm, "lsl{s}<c>.w <Rd>, <Rm>, #imm"}, |
| 9350 | // lsl (register) |
| 9351 | { 0xffffffc0, 0x00004080, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLSLReg, "lsls|lsl<c> <Rdn>, <Rm>"}, |
| 9352 | { 0xffe0f0f0, 0xfa00f000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLSLReg, "lsl{s}<c>.w <Rd>, <Rn>, <Rm>"}, |
| 9353 | // lsr (immediate) |
| 9354 | { 0xfffff800, 0x00000800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLSRImm, "lsrs|lsr<c> <Rd>, <Rm>, #imm"}, |
| 9355 | { 0xffef8030, 0xea4f0010, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLSRImm, "lsr{s}<c>.w <Rd>, <Rm>, #imm"}, |
| 9356 | // lsr (register) |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 9357 | { 0xffffffc0, 0x000040c0, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLSRReg, "lsrs|lsr<c> <Rdn>, <Rm>"}, |
Johnny Chen | 2ee35bc | 2011-02-16 19:27:43 +0000 | [diff] [blame] | 9358 | { 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] | 9359 | // rrx is a special case encoding of ror (immediate) |
| 9360 | { 0xffeff0f0, 0xea4f0030, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateRRX, "rrx{s}<c>.w <Rd>, <Rm>"}, |
| 9361 | // ror (immediate) |
| 9362 | { 0xffef8030, 0xea4f0030, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateRORImm, "ror{s}<c>.w <Rd>, <Rm>, #imm"}, |
| 9363 | // ror (register) |
| 9364 | { 0xffffffc0, 0x000041c0, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateRORReg, "rors|ror<c> <Rdn>, <Rm>"}, |
| 9365 | { 0xffe0f0f0, 0xfa60f000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateRORReg, "ror{s}<c>.w <Rd>, <Rn>, <Rm>"}, |
Caroline Tice | 5c1e2ed | 2011-03-02 22:43:54 +0000 | [diff] [blame] | 9366 | // mul |
| 9367 | { 0xffffffc0, 0x00004340, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMUL, "muls <Rdm>,<Rn>,<Rdm>" }, |
| 9368 | // mul |
| 9369 | { 0xfff0f0f0, 0xfb00f000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateMUL, "mul<c> <Rd>,<Rn>,<Rm>" }, |
Caroline Tice | 6bf6516 | 2011-03-03 17:42:58 +0000 | [diff] [blame] | 9370 | |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 9371 | //---------------------------------------------------------------------- |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 9372 | // Load instructions |
| 9373 | //---------------------------------------------------------------------- |
| 9374 | { 0xfffff800, 0x0000c800, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDM, "ldm<c> <Rn>{!} <registers>" }, |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 9375 | { 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] | 9376 | { 0xffd00000, 0xe9100000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateLDMDB, "ldmdb<c> <Rn>{!} <registers>" }, |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 9377 | { 0xfffff800, 0x00006800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRtRnImm, "ldr<c> <Rt>, [<Rn>{,#imm}]"}, |
| 9378 | // Thumb2 PC-relative load into register |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 9379 | { 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] | 9380 | { 0xfffffe00, 0x00005800, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRegister, "ldr<c> <Rt>, [<Rn>, <Rm>]" }, |
| 9381 | { 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] | 9382 | { 0xfffff800, 0x00007800, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRBImmediate, "ldrb<c> <Rt>,[<Rn>{,#<imm5>}]" }, |
| 9383 | { 0xfff00000, 0xf8900000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDRBImmediate, "ldrb<c>.w <Rt>,[<Rn>{,#<imm12>}]" }, |
| 9384 | { 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] | 9385 | { 0xff7f0000, 0xf81f0000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateLDRBLiteral, "ldrb<c> <Rt>,[...]" }, |
Caroline Tice | 30fec12 | 2011-02-18 23:52:21 +0000 | [diff] [blame] | 9386 | { 0xfffffe00, 0x00005c00, ARMV6T2_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRBRegister, "ldrb<c> <Rt>,[<Rn>,<Rm>]" }, |
| 9387 | { 0xfff00fc0, 0xf8100000, ARMV6T2_ABOVE, eEncodingT2, eSize32,&EmulateInstructionARM::EmulateLDRBRegister, "ldrb<c>.w <Rt>,[<Rn>,<Rm>{,LSL #imm2>}]" }, |
Caroline Tice | 0491b3b | 2011-02-28 22:39:58 +0000 | [diff] [blame] | 9388 | { 0xfffff800, 0x00008800, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRHImmediate, "ldrh<c> <Rt>, [<Rn>{,#<imm>}]" }, |
| 9389 | { 0xfff00000, 0xf8b00000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDRHImmediate, "ldrh<c>.w <Rt>,[<Rn>{,#<imm12>}]" }, |
| 9390 | { 0xfff00800, 0xf8300800, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateLDRHImmediate, "ldrh<c> <Rt>,[<Rn>,#+/-<imm8>]{!}" }, |
Caroline Tice | 952b538 | 2011-02-28 23:15:24 +0000 | [diff] [blame] | 9391 | { 0xff7f0000, 0xf83f0000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateLDRHLiteral, "ldrh<c> <Rt>, <label>" }, |
Caroline Tice | 0e6bc95 | 2011-03-01 18:00:42 +0000 | [diff] [blame] | 9392 | { 0xfffffe00, 0x00005a00, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRHRegister, "ldrh<c> <Rt>, [<Rn>,<Rm>]" }, |
| 9393 | { 0xfff00fc0, 0xf8300000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDRHRegister, "ldrh<c>.w <Rt>,[<Rn>,<Rm>{,LSL #<imm2>}]" }, |
Caroline Tice | a5e28af | 2011-03-01 21:53:03 +0000 | [diff] [blame] | 9394 | { 0xfff00000, 0xf9900000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateLDRSBImmediate, "ldrsb<c> <Rt>,[<Rn>,#<imm12>]" }, |
| 9395 | { 0xfff00800, 0xf9100800, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDRSBImmediate, "ldrsb<c> <Rt>,[<Rn>,#+/-<imm8>]" }, |
Caroline Tice | 5f59391 | 2011-03-01 22:25:17 +0000 | [diff] [blame] | 9396 | { 0xff7f0000, 0xf91f0000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateLDRSBLiteral, "ldrsb<c> <Rt>, <label>" }, |
Caroline Tice | 672f311 | 2011-03-01 23:55:59 +0000 | [diff] [blame] | 9397 | { 0xfffffe00, 0x00005600, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRSBRegister, "ldrsb<c> <Rt>,[<Rn>,<Rm>]" }, |
| 9398 | { 0xfff00fc0, 0xf9100000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDRSBRegister, "ldrsb<c>.w <Rt>,[<Rn>,<Rm>{,LSL #imm2>}]" }, |
Caroline Tice | 78fb563 | 2011-03-02 00:39:42 +0000 | [diff] [blame] | 9399 | { 0xfff00000, 0xf9b00000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateLDRSHImmediate, "ldrsh<c> <Rt>,[<Rn>,#<imm12>]" }, |
| 9400 | { 0xfff00800, 0xf9300800, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDRSHImmediate, "ldrsh<c> <Rt>,[<Rn>,#+/-<imm8>]" }, |
Caroline Tice | d2fac09 | 2011-03-02 19:45:34 +0000 | [diff] [blame] | 9401 | { 0xff7f0000, 0xf93f0000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateLDRSHLiteral, "ldrsh<c> <Rt>,<label>" }, |
Caroline Tice | 291a3e9 | 2011-03-02 21:13:44 +0000 | [diff] [blame] | 9402 | { 0xfffffe00, 0x00005e00, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRSHRegister, "ldrsh<c> <Rt>,[<Rn>,<Rm>]" }, |
| 9403 | { 0xfff00fc0, 0xf9300000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDRSHRegister, "ldrsh<c>.w <Rt>,[<Rn>,<Rm>{,LSL #<imm2>}]" }, |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 9404 | |
| 9405 | //---------------------------------------------------------------------- |
| 9406 | // Store instructions |
| 9407 | //---------------------------------------------------------------------- |
| 9408 | { 0xfffff800, 0x0000c000, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSTM, "stm<c> <Rn>{!} <registers>" }, |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 9409 | { 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] | 9410 | { 0xffd00000, 0xe9000000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateSTMDB, "stmdb<c> <Rn>{!} <registers>" }, |
Caroline Tice | fe47911 | 2011-02-18 18:52:37 +0000 | [diff] [blame] | 9411 | { 0xfffff800, 0x00006000, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSTRThumb, "str<c> <Rt>, [<Rn>{,#<imm>}]" }, |
| 9412 | { 0xfffff800, 0x00009000, ARMV4T_ABOVE, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateSTRThumb, "str<c> <Rt>, [SP,#<imm>]" }, |
| 9413 | { 0xfff00000, 0xf8c00000, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSTRThumb, "str<c>.w <Rt>, [<Rn>,#<imm12>]" }, |
| 9414 | { 0xfff00800, 0xf8400800, ARMV6T2_ABOVE, eEncodingT4, eSize32, &EmulateInstructionARM::EmulateSTRThumb, "str<c> <Rt>, [<Rn>,#+/-<imm8>]" }, |
| 9415 | { 0xfffffe00, 0x00005000, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSTRRegister, "str<c> <Rt> ,{<Rn>, <Rm>]" }, |
| 9416 | { 0xfff00fc0, 0xf8400000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSTRRegister, "str<c>.w <Rt>, [<Rn>, <Rm> {lsl #imm2>}]" }, |
| 9417 | { 0xfffff800, 0x00007000, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSTRBThumb, "strb<c> <Rt>, [<Rn>, #<imm5>]" }, |
| 9418 | { 0xfff00000, 0xf8800000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSTRBThumb, "strb<c>.w <Rt>, [<Rn>, #<imm12>]" }, |
Caroline Tice | 6bf6516 | 2011-03-03 17:42:58 +0000 | [diff] [blame] | 9419 | { 0xfff00800, 0xf8000800, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSTRBThumb, "strb<c> <Rt> ,[<Rn>, #+/-<imm8>]{!}" }, |
| 9420 | |
| 9421 | //---------------------------------------------------------------------- |
| 9422 | // Other instructions |
| 9423 | //---------------------------------------------------------------------- |
| 9424 | { 0xffffffc0, 0x0000b240, ARMV6_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSXTB, "sxtb<c> <Rd>,<Rm>" }, |
Caroline Tice | 868198b | 2011-03-03 18:04:49 +0000 | [diff] [blame] | 9425 | { 0xfffff080, 0xfa4ff080, ARMV6_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSXTB, "sxtb<c>.w <Rd>,<Rm>{,<rotation>}" }, |
| 9426 | { 0xffffffc0, 0x0000b200, ARMV6_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSXTH, "sxth<c> <Rd>,<Rm>" }, |
Caroline Tice | 8ce96d9 | 2011-03-03 18:27:17 +0000 | [diff] [blame] | 9427 | { 0xfffff080, 0xfa0ff080, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSXTH, "sxth<c>.w <Rd>,<Rm>{,<rotation>}" }, |
| 9428 | { 0xffffffc0, 0x0000b2c0, ARMV6_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateUXTB, "uxtb<c> <Rd>,<Rm>" }, |
Caroline Tice | 11555f2 | 2011-03-03 18:48:58 +0000 | [diff] [blame^] | 9429 | { 0xfffff080, 0xfa5ff080, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateUXTB, "uxtb<c>.w <Rd>,<Rm>{,<rotation>}" }, |
| 9430 | { 0xffffffc0, 0x0000b280, ARMV6_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateUXTH, "uxth<c> <Rd>,<Rm>" }, |
| 9431 | { 0xfffff080, 0xfa1ff080, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateUXTH, "uxth<c>.w <Rd>,<Rm>{,<rotation>}" } |
Caroline Tice | 6bf6516 | 2011-03-03 17:42:58 +0000 | [diff] [blame] | 9432 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 9433 | }; |
| 9434 | |
| 9435 | const size_t k_num_thumb_opcodes = sizeof(g_thumb_opcodes)/sizeof(ARMOpcode); |
| 9436 | for (size_t i=0; i<k_num_thumb_opcodes; ++i) |
| 9437 | { |
| 9438 | if ((g_thumb_opcodes[i].mask & opcode) == g_thumb_opcodes[i].value) |
| 9439 | return &g_thumb_opcodes[i]; |
| 9440 | } |
| 9441 | return NULL; |
| 9442 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 9443 | |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 9444 | bool |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 9445 | EmulateInstructionARM::SetArchitecture (const ArchSpec &arch) |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 9446 | { |
| 9447 | m_arm_isa = 0; |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 9448 | const char *arch_cstr = arch.GetArchitectureName (); |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 9449 | if (arch_cstr) |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 9450 | { |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 9451 | if (0 == ::strcasecmp(arch_cstr, "armv4t")) m_arm_isa = ARMv4T; |
| 9452 | else if (0 == ::strcasecmp(arch_cstr, "armv4")) m_arm_isa = ARMv4; |
| 9453 | else if (0 == ::strcasecmp(arch_cstr, "armv5tej")) m_arm_isa = ARMv5TEJ; |
| 9454 | else if (0 == ::strcasecmp(arch_cstr, "armv5te")) m_arm_isa = ARMv5TE; |
| 9455 | else if (0 == ::strcasecmp(arch_cstr, "armv5t")) m_arm_isa = ARMv5T; |
| 9456 | else if (0 == ::strcasecmp(arch_cstr, "armv6k")) m_arm_isa = ARMv6K; |
| 9457 | else if (0 == ::strcasecmp(arch_cstr, "armv6")) m_arm_isa = ARMv6; |
| 9458 | else if (0 == ::strcasecmp(arch_cstr, "armv6t2")) m_arm_isa = ARMv6T2; |
| 9459 | else if (0 == ::strcasecmp(arch_cstr, "armv7")) m_arm_isa = ARMv7; |
| 9460 | else if (0 == ::strcasecmp(arch_cstr, "armv8")) m_arm_isa = ARMv8; |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 9461 | } |
| 9462 | return m_arm_isa != 0; |
| 9463 | } |
| 9464 | |
| 9465 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 9466 | bool |
| 9467 | EmulateInstructionARM::ReadInstruction () |
| 9468 | { |
| 9469 | bool success = false; |
| 9470 | m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success); |
| 9471 | if (success) |
| 9472 | { |
| 9473 | addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success); |
| 9474 | if (success) |
| 9475 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 9476 | Context read_inst_context; |
| 9477 | read_inst_context.type = eContextReadOpcode; |
| 9478 | read_inst_context.SetNoArgs (); |
| 9479 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 9480 | if (m_inst_cpsr & MASK_CPSR_T) |
| 9481 | { |
| 9482 | m_inst_mode = eModeThumb; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 9483 | uint32_t thumb_opcode = MemARead(read_inst_context, pc, 2, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 9484 | |
| 9485 | if (success) |
| 9486 | { |
| 9487 | if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0)) |
| 9488 | { |
| 9489 | m_inst.opcode_type = eOpcode16; |
| 9490 | m_inst.opcode.inst16 = thumb_opcode; |
| 9491 | } |
| 9492 | else |
| 9493 | { |
| 9494 | m_inst.opcode_type = eOpcode32; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 9495 | 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] | 9496 | } |
| 9497 | } |
| 9498 | } |
| 9499 | else |
| 9500 | { |
| 9501 | m_inst_mode = eModeARM; |
| 9502 | m_inst.opcode_type = eOpcode32; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 9503 | m_inst.opcode.inst32 = MemARead(read_inst_context, pc, 4, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 9504 | } |
| 9505 | } |
| 9506 | } |
| 9507 | if (!success) |
| 9508 | { |
| 9509 | m_inst_mode = eModeInvalid; |
| 9510 | m_inst_pc = LLDB_INVALID_ADDRESS; |
| 9511 | } |
| 9512 | return success; |
| 9513 | } |
| 9514 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 9515 | uint32_t |
| 9516 | EmulateInstructionARM::ArchVersion () |
| 9517 | { |
| 9518 | return m_arm_isa; |
| 9519 | } |
| 9520 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 9521 | bool |
| 9522 | EmulateInstructionARM::ConditionPassed () |
| 9523 | { |
| 9524 | if (m_inst_cpsr == 0) |
| 9525 | return false; |
| 9526 | |
| 9527 | const uint32_t cond = CurrentCond (); |
| 9528 | |
| 9529 | if (cond == UINT32_MAX) |
| 9530 | return false; |
| 9531 | |
| 9532 | bool result = false; |
| 9533 | switch (UnsignedBits(cond, 3, 1)) |
| 9534 | { |
| 9535 | case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break; |
| 9536 | case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break; |
| 9537 | case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break; |
| 9538 | case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break; |
| 9539 | case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break; |
| 9540 | case 5: |
| 9541 | { |
| 9542 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 9543 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 9544 | result = n == v; |
| 9545 | } |
| 9546 | break; |
| 9547 | case 6: |
| 9548 | { |
| 9549 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 9550 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 9551 | result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0); |
| 9552 | } |
| 9553 | break; |
| 9554 | case 7: |
| 9555 | result = true; |
| 9556 | break; |
| 9557 | } |
| 9558 | |
| 9559 | if (cond & 1) |
| 9560 | result = !result; |
| 9561 | return result; |
| 9562 | } |
| 9563 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9564 | uint32_t |
| 9565 | EmulateInstructionARM::CurrentCond () |
| 9566 | { |
| 9567 | switch (m_inst_mode) |
| 9568 | { |
| 9569 | default: |
| 9570 | case eModeInvalid: |
| 9571 | break; |
| 9572 | |
| 9573 | case eModeARM: |
| 9574 | return UnsignedBits(m_inst.opcode.inst32, 31, 28); |
| 9575 | |
| 9576 | case eModeThumb: |
| 9577 | // For T1 and T3 encodings of the Branch instruction, it returns the 4-bit |
| 9578 | // 'cond' field of the encoding. |
| 9579 | if (m_inst.opcode_type == eOpcode16 && |
| 9580 | Bits32(m_inst.opcode.inst16, 15, 12) == 0x0d && |
| 9581 | Bits32(m_inst.opcode.inst16, 11, 7) != 0x0f) |
| 9582 | { |
| 9583 | return Bits32(m_inst.opcode.inst16, 11, 7); |
| 9584 | } |
| 9585 | else if (m_inst.opcode_type == eOpcode32 && |
| 9586 | Bits32(m_inst.opcode.inst32, 31, 27) == 0x1e && |
| 9587 | Bits32(m_inst.opcode.inst32, 15, 14) == 0x02 && |
| 9588 | Bits32(m_inst.opcode.inst32, 12, 12) == 0x00 && |
| 9589 | Bits32(m_inst.opcode.inst32, 25, 22) <= 0x0d) |
| 9590 | { |
| 9591 | return Bits32(m_inst.opcode.inst32, 25, 22); |
| 9592 | } |
| 9593 | |
| 9594 | return m_it_session.GetCond(); |
| 9595 | } |
| 9596 | return UINT32_MAX; // Return invalid value |
| 9597 | } |
| 9598 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9599 | bool |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 9600 | EmulateInstructionARM::InITBlock() |
| 9601 | { |
| 9602 | return CurrentInstrSet() == eModeThumb && m_it_session.InITBlock(); |
| 9603 | } |
| 9604 | |
| 9605 | bool |
| 9606 | EmulateInstructionARM::LastInITBlock() |
| 9607 | { |
| 9608 | return CurrentInstrSet() == eModeThumb && m_it_session.LastInITBlock(); |
| 9609 | } |
| 9610 | |
| 9611 | bool |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9612 | EmulateInstructionARM::BranchWritePC (const Context &context, uint32_t addr) |
| 9613 | { |
| 9614 | addr_t target; |
| 9615 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 9616 | // Check the current instruction set. |
| 9617 | if (CurrentInstrSet() == eModeARM) |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9618 | target = addr & 0xfffffffc; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 9619 | else |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9620 | target = addr & 0xfffffffe; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 9621 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9622 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target)) |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 9623 | return false; |
| 9624 | |
| 9625 | return true; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9626 | } |
| 9627 | |
| 9628 | // As a side effect, BXWritePC sets context.arg2 to eModeARM or eModeThumb by inspecting addr. |
| 9629 | bool |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 9630 | EmulateInstructionARM::BXWritePC (Context &context, uint32_t addr) |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9631 | { |
| 9632 | addr_t target; |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 9633 | // If the CPSR is changed due to switching between ARM and Thumb ISETSTATE, |
| 9634 | // we want to record it and issue a WriteRegister callback so the clients |
| 9635 | // can track the mode changes accordingly. |
| 9636 | bool cpsr_changed = false; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9637 | |
| 9638 | if (BitIsSet(addr, 0)) |
| 9639 | { |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 9640 | if (CurrentInstrSet() != eModeThumb) |
| 9641 | { |
| 9642 | SelectInstrSet(eModeThumb); |
| 9643 | cpsr_changed = true; |
| 9644 | } |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9645 | target = addr & 0xfffffffe; |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 9646 | context.SetMode (eModeThumb); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9647 | } |
| 9648 | else if (BitIsClear(addr, 1)) |
| 9649 | { |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 9650 | if (CurrentInstrSet() != eModeARM) |
| 9651 | { |
| 9652 | SelectInstrSet(eModeARM); |
| 9653 | cpsr_changed = true; |
| 9654 | } |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9655 | target = addr & 0xfffffffc; |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 9656 | context.SetMode (eModeARM); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9657 | } |
| 9658 | else |
| 9659 | return false; // address<1:0> == '10' => UNPREDICTABLE |
| 9660 | |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 9661 | if (cpsr_changed) |
| 9662 | { |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 9663 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr)) |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 9664 | return false; |
| 9665 | } |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9666 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target)) |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 9667 | return false; |
| 9668 | |
| 9669 | return true; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 9670 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 9671 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 9672 | // Dispatches to either BXWritePC or BranchWritePC based on architecture versions. |
| 9673 | bool |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 9674 | EmulateInstructionARM::LoadWritePC (Context &context, uint32_t addr) |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 9675 | { |
| 9676 | if (ArchVersion() >= ARMv5T) |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 9677 | return BXWritePC(context, addr); |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 9678 | else |
| 9679 | return BranchWritePC((const Context)context, addr); |
| 9680 | } |
| 9681 | |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 9682 | // Dispatches to either BXWritePC or BranchWritePC based on architecture versions and current instruction set. |
| 9683 | bool |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 9684 | EmulateInstructionARM::ALUWritePC (Context &context, uint32_t addr) |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 9685 | { |
| 9686 | if (ArchVersion() >= ARMv7 && CurrentInstrSet() == eModeARM) |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 9687 | return BXWritePC(context, addr); |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 9688 | else |
| 9689 | return BranchWritePC((const Context)context, addr); |
| 9690 | } |
| 9691 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 9692 | EmulateInstructionARM::Mode |
| 9693 | EmulateInstructionARM::CurrentInstrSet () |
| 9694 | { |
| 9695 | return m_inst_mode; |
| 9696 | } |
| 9697 | |
| 9698 | // 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] | 9699 | // ReadInstruction() is performed. This function has a side effect of updating |
| 9700 | // the m_new_inst_cpsr member variable if necessary. |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 9701 | bool |
| 9702 | EmulateInstructionARM::SelectInstrSet (Mode arm_or_thumb) |
| 9703 | { |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 9704 | m_new_inst_cpsr = m_inst_cpsr; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 9705 | switch (arm_or_thumb) |
| 9706 | { |
| 9707 | default: |
| 9708 | return false; |
| 9709 | eModeARM: |
| 9710 | // Clear the T bit. |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 9711 | m_new_inst_cpsr &= ~MASK_CPSR_T; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 9712 | break; |
| 9713 | eModeThumb: |
| 9714 | // Set the T bit. |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 9715 | m_new_inst_cpsr |= MASK_CPSR_T; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 9716 | break; |
| 9717 | } |
| 9718 | return true; |
| 9719 | } |
| 9720 | |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 9721 | // This function returns TRUE if the processor currently provides support for |
| 9722 | // unaligned memory accesses, or FALSE otherwise. This is always TRUE in ARMv7, |
| 9723 | // controllable by the SCTLR.U bit in ARMv6, and always FALSE before ARMv6. |
| 9724 | bool |
| 9725 | EmulateInstructionARM::UnalignedSupport() |
| 9726 | { |
| 9727 | return (ArchVersion() >= ARMv7); |
| 9728 | } |
| 9729 | |
Johnny Chen | bf6ad17 | 2011-02-11 01:29:53 +0000 | [diff] [blame] | 9730 | // The main addition and subtraction instructions can produce status information |
| 9731 | // about both unsigned carry and signed overflow conditions. This status |
| 9732 | // information can be used to synthesize multi-word additions and subtractions. |
| 9733 | EmulateInstructionARM::AddWithCarryResult |
| 9734 | EmulateInstructionARM::AddWithCarry (uint32_t x, uint32_t y, uint8_t carry_in) |
| 9735 | { |
| 9736 | uint32_t result; |
| 9737 | uint8_t carry_out; |
| 9738 | uint8_t overflow; |
| 9739 | |
| 9740 | uint64_t unsigned_sum = x + y + carry_in; |
| 9741 | int64_t signed_sum = (int32_t)x + (int32_t)y + (int32_t)carry_in; |
| 9742 | |
| 9743 | result = UnsignedBits(unsigned_sum, 31, 0); |
| 9744 | carry_out = (result == unsigned_sum ? 0 : 1); |
| 9745 | overflow = ((int32_t)result == signed_sum ? 0 : 1); |
| 9746 | |
| 9747 | AddWithCarryResult res = { result, carry_out, overflow }; |
| 9748 | return res; |
| 9749 | } |
| 9750 | |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 9751 | uint32_t |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 9752 | EmulateInstructionARM::ReadCoreReg(uint32_t num, bool *success) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 9753 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 9754 | uint32_t reg_kind, reg_num; |
| 9755 | switch (num) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 9756 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 9757 | case SP_REG: |
| 9758 | reg_kind = eRegisterKindGeneric; |
| 9759 | reg_num = LLDB_REGNUM_GENERIC_SP; |
| 9760 | break; |
| 9761 | case LR_REG: |
| 9762 | reg_kind = eRegisterKindGeneric; |
| 9763 | reg_num = LLDB_REGNUM_GENERIC_RA; |
| 9764 | break; |
| 9765 | case PC_REG: |
| 9766 | reg_kind = eRegisterKindGeneric; |
| 9767 | reg_num = LLDB_REGNUM_GENERIC_PC; |
| 9768 | break; |
| 9769 | default: |
| 9770 | if (0 <= num && num < SP_REG) |
| 9771 | { |
| 9772 | reg_kind = eRegisterKindDWARF; |
| 9773 | reg_num = dwarf_r0 + num; |
| 9774 | } |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 9775 | else |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 9776 | { |
| 9777 | assert(0 && "Invalid register number"); |
| 9778 | *success = false; |
| 9779 | return ~0u; |
| 9780 | } |
| 9781 | break; |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 9782 | } |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 9783 | |
| 9784 | // Read our register. |
| 9785 | uint32_t val = ReadRegisterUnsigned (reg_kind, reg_num, 0, success); |
| 9786 | |
| 9787 | // When executing an ARM instruction , PC reads as the address of the current |
| 9788 | // instruction plus 8. |
| 9789 | // When executing a Thumb instruction , PC reads as the address of the current |
| 9790 | // instruction plus 4. |
| 9791 | if (num == 15) |
| 9792 | { |
| 9793 | if (CurrentInstrSet() == eModeARM) |
| 9794 | val += 8; |
| 9795 | else |
| 9796 | val += 4; |
| 9797 | } |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 9798 | |
| 9799 | return val; |
| 9800 | } |
| 9801 | |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 9802 | // Write the result to the ARM core register Rd, and optionally update the |
| 9803 | // condition flags based on the result. |
| 9804 | // |
| 9805 | // This helper method tries to encapsulate the following pseudocode from the |
| 9806 | // ARM Architecture Reference Manual: |
| 9807 | // |
| 9808 | // if d == 15 then // Can only occur for encoding A1 |
| 9809 | // ALUWritePC(result); // setflags is always FALSE here |
| 9810 | // else |
| 9811 | // R[d] = result; |
| 9812 | // if setflags then |
| 9813 | // APSR.N = result<31>; |
| 9814 | // APSR.Z = IsZeroBit(result); |
| 9815 | // APSR.C = carry; |
| 9816 | // // APSR.V unchanged |
| 9817 | // |
| 9818 | // In the above case, the API client does not pass in the overflow arg, which |
| 9819 | // defaults to ~0u. |
| 9820 | bool |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 9821 | EmulateInstructionARM::WriteCoreRegOptionalFlags (Context &context, |
| 9822 | const uint32_t result, |
| 9823 | const uint32_t Rd, |
| 9824 | bool setflags, |
| 9825 | const uint32_t carry, |
| 9826 | const uint32_t overflow) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 9827 | { |
| 9828 | if (Rd == 15) |
| 9829 | { |
| 9830 | if (!ALUWritePC (context, result)) |
| 9831 | return false; |
| 9832 | } |
| 9833 | else |
| 9834 | { |
Johnny Chen | a695f95 | 2011-02-23 21:24:25 +0000 | [diff] [blame] | 9835 | uint32_t reg_kind, reg_num; |
| 9836 | switch (Rd) |
| 9837 | { |
| 9838 | case SP_REG: |
| 9839 | reg_kind = eRegisterKindGeneric; |
| 9840 | reg_num = LLDB_REGNUM_GENERIC_SP; |
| 9841 | break; |
| 9842 | case LR_REG: |
| 9843 | reg_kind = eRegisterKindGeneric; |
| 9844 | reg_num = LLDB_REGNUM_GENERIC_RA; |
| 9845 | break; |
| 9846 | default: |
| 9847 | reg_kind = eRegisterKindDWARF; |
| 9848 | reg_num = dwarf_r0 + Rd; |
| 9849 | } |
| 9850 | if (!WriteRegisterUnsigned (context, reg_kind, reg_num, result)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 9851 | return false; |
| 9852 | if (setflags) |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 9853 | return WriteFlags (context, result, carry, overflow); |
| 9854 | } |
| 9855 | return true; |
| 9856 | } |
| 9857 | |
| 9858 | // This helper method tries to encapsulate the following pseudocode from the |
| 9859 | // ARM Architecture Reference Manual: |
| 9860 | // |
| 9861 | // APSR.N = result<31>; |
| 9862 | // APSR.Z = IsZeroBit(result); |
| 9863 | // APSR.C = carry; |
| 9864 | // APSR.V = overflow |
| 9865 | // |
| 9866 | // Default arguments can be specified for carry and overflow parameters, which means |
| 9867 | // not to update the respective flags. |
| 9868 | bool |
| 9869 | EmulateInstructionARM::WriteFlags (Context &context, |
| 9870 | const uint32_t result, |
| 9871 | const uint32_t carry, |
| 9872 | const uint32_t overflow) |
| 9873 | { |
| 9874 | m_new_inst_cpsr = m_inst_cpsr; |
Johnny Chen | 2434884 | 2011-02-23 00:15:56 +0000 | [diff] [blame] | 9875 | SetBit32(m_new_inst_cpsr, CPSR_N_POS, Bit32(result, CPSR_N_POS)); |
| 9876 | SetBit32(m_new_inst_cpsr, CPSR_Z_POS, result == 0 ? 1 : 0); |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 9877 | if (carry != ~0u) |
Johnny Chen | 2434884 | 2011-02-23 00:15:56 +0000 | [diff] [blame] | 9878 | SetBit32(m_new_inst_cpsr, CPSR_C_POS, carry); |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 9879 | if (overflow != ~0u) |
Johnny Chen | 2434884 | 2011-02-23 00:15:56 +0000 | [diff] [blame] | 9880 | SetBit32(m_new_inst_cpsr, CPSR_V_POS, overflow); |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 9881 | if (m_new_inst_cpsr != m_inst_cpsr) |
| 9882 | { |
| 9883 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr)) |
| 9884 | return false; |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 9885 | } |
| 9886 | return true; |
| 9887 | } |
| 9888 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 9889 | bool |
| 9890 | EmulateInstructionARM::EvaluateInstruction () |
| 9891 | { |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 9892 | // Advance the ITSTATE bits to their values for the next instruction. |
| 9893 | if (m_inst_mode == eModeThumb && m_it_session.InITBlock()) |
| 9894 | m_it_session.ITAdvance(); |
| 9895 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 9896 | return false; |
| 9897 | } |