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) |
| 132 | #define ARMV6T2_ABOVE (ARMv6T2|ARMv7|ARMv8) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 133 | |
Johnny Chen | 0e00af2 | 2011-02-10 19:40:42 +0000 | [diff] [blame] | 134 | //---------------------------------------------------------------------- |
| 135 | // |
| 136 | // EmulateInstructionARM implementation |
| 137 | // |
| 138 | //---------------------------------------------------------------------- |
| 139 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 140 | void |
| 141 | EmulateInstructionARM::Initialize () |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 142 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 143 | } |
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 | void |
| 146 | EmulateInstructionARM::Terminate () |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 147 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 150 | // Write "bits (32) UNKNOWN" to memory address "address". Helper function for many ARM instructions. |
| 151 | bool |
| 152 | EmulateInstructionARM::WriteBits32UnknownToMemory (addr_t address) |
| 153 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 154 | EmulateInstruction::Context context; |
| 155 | context.type = EmulateInstruction::eContextWriteMemoryRandomBits; |
| 156 | context.SetNoArgs (); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 157 | |
| 158 | uint32_t random_data = rand (); |
| 159 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 160 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 161 | if (!MemAWrite (context, address, random_data, addr_byte_size)) |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 162 | return false; |
| 163 | |
| 164 | return true; |
| 165 | } |
| 166 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 167 | // Write "bits (32) UNKNOWN" to register n. Helper function for many ARM instructions. |
| 168 | bool |
| 169 | EmulateInstructionARM::WriteBits32Unknown (int n) |
| 170 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 171 | EmulateInstruction::Context context; |
| 172 | context.type = EmulateInstruction::eContextWriteRegisterRandomBits; |
| 173 | context.SetNoArgs (); |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 174 | |
Johnny Chen | 62ff6f5 | 2011-02-11 18:11:22 +0000 | [diff] [blame] | 175 | bool success; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 176 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 177 | |
| 178 | if (!success) |
| 179 | return false; |
| 180 | |
| 181 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 182 | return false; |
| 183 | |
| 184 | return true; |
| 185 | } |
| 186 | |
Johnny Chen | 08c25e8 | 2011-01-31 18:02:28 +0000 | [diff] [blame] | 187 | // Push Multiple Registers stores multiple registers to the stack, storing to |
| 188 | // consecutive memory locations ending just below the address in SP, and updates |
| 189 | // SP to point to the start of the stored data. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 190 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 191 | EmulateInstructionARM::EmulatePUSH (ARMEncoding encoding) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 192 | { |
| 193 | #if 0 |
| 194 | // ARM pseudo code... |
| 195 | if (ConditionPassed()) |
| 196 | { |
| 197 | EncodingSpecificOperations(); |
| 198 | NullCheckIfThumbEE(13); |
| 199 | address = SP - 4*BitCount(registers); |
| 200 | |
| 201 | for (i = 0 to 14) |
| 202 | { |
| 203 | if (registers<i> == ’1’) |
| 204 | { |
| 205 | if i == 13 && i != LowestSetBit(registers) // Only possible for encoding A1 |
| 206 | MemA[address,4] = bits(32) UNKNOWN; |
| 207 | else |
| 208 | MemA[address,4] = R[i]; |
| 209 | address = address + 4; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (registers<15> == ’1’) // Only possible for encoding A1 or A2 |
| 214 | MemA[address,4] = PCStoreValue(); |
| 215 | |
| 216 | SP = SP - 4*BitCount(registers); |
| 217 | } |
| 218 | #endif |
| 219 | |
| 220 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 221 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 222 | if (!success) |
| 223 | return false; |
| 224 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 225 | if (ConditionPassed()) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 226 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 227 | const uint32_t addr_byte_size = GetAddressByteSize(); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 228 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 229 | if (!success) |
| 230 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 231 | uint32_t registers = 0; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 232 | uint32_t Rt; // the source register |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 233 | switch (encoding) { |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 234 | case eEncodingT1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 235 | registers = Bits32(opcode, 7, 0); |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 236 | // The M bit represents LR. |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 237 | if (Bit32(opcode, 8)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 238 | registers |= (1u << 14); |
Johnny Chen | aedde1c | 2011-01-24 20:38:45 +0000 | [diff] [blame] | 239 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 240 | if (BitCount(registers) < 1) |
| 241 | return false; |
| 242 | break; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 243 | case eEncodingT2: |
| 244 | // Ignore bits 15 & 13. |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 245 | registers = Bits32(opcode, 15, 0) & ~0xa000; |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 246 | // if BitCount(registers) < 2 then UNPREDICTABLE; |
| 247 | if (BitCount(registers) < 2) |
| 248 | return false; |
| 249 | break; |
| 250 | case eEncodingT3: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 251 | Rt = Bits32(opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 252 | // if BadReg(t) then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 253 | if (BadReg(Rt)) |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 254 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 255 | registers = (1u << Rt); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 256 | break; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 257 | case eEncodingA1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 258 | registers = Bits32(opcode, 15, 0); |
Johnny Chen | a33d484 | 2011-01-24 22:25:48 +0000 | [diff] [blame] | 259 | // Instead of return false, let's handle the following case as well, |
| 260 | // which amounts to pushing one reg onto the full descending stacks. |
| 261 | // if BitCount(register_list) < 2 then SEE STMDB / STMFD; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 262 | break; |
| 263 | case eEncodingA2: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 264 | Rt = Bits32(opcode, 15, 12); |
Johnny Chen | 7dc60e1 | 2011-01-24 19:46:32 +0000 | [diff] [blame] | 265 | // if t == 13 then UNPREDICTABLE; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 266 | if (Rt == dwarf_sp) |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 267 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 268 | registers = (1u << Rt); |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 269 | break; |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 270 | default: |
| 271 | return false; |
Johnny Chen | 3c75c76 | 2011-01-22 00:47:08 +0000 | [diff] [blame] | 272 | } |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 273 | addr_t sp_offset = addr_byte_size * BitCount (registers); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 274 | addr_t addr = sp - sp_offset; |
| 275 | uint32_t i; |
| 276 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 277 | EmulateInstruction::Context context; |
| 278 | context.type = EmulateInstruction::eContextPushRegisterOnStack; |
| 279 | Register dwarf_reg; |
| 280 | dwarf_reg.SetRegister (eRegisterKindDWARF, 0); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 281 | for (i=0; i<15; ++i) |
| 282 | { |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 283 | if (BitIsSet (registers, i)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 284 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 285 | dwarf_reg.num = dwarf_r0 + i; |
| 286 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 287 | uint32_t reg_value = ReadCoreReg(i, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 288 | if (!success) |
| 289 | return false; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 290 | if (!MemAWrite (context, addr, reg_value, addr_byte_size)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 291 | return false; |
| 292 | addr += addr_byte_size; |
| 293 | } |
| 294 | } |
| 295 | |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 296 | if (BitIsSet (registers, 15)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 297 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 298 | dwarf_reg.num = dwarf_pc; |
| 299 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 300 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 301 | if (!success) |
| 302 | return false; |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 303 | if (!MemAWrite (context, addr, pc, addr_byte_size)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 304 | return false; |
| 305 | } |
| 306 | |
| 307 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 308 | context.SetImmediateSigned (-sp_offset); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 309 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 310 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 311 | return false; |
| 312 | } |
| 313 | return true; |
| 314 | } |
| 315 | |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 316 | // Pop Multiple Registers loads multiple registers from the stack, loading from |
| 317 | // consecutive memory locations staring at the address in SP, and updates |
| 318 | // SP to point just above the loaded data. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 319 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 320 | EmulateInstructionARM::EmulatePOP (ARMEncoding encoding) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 321 | { |
| 322 | #if 0 |
| 323 | // ARM pseudo code... |
| 324 | if (ConditionPassed()) |
| 325 | { |
| 326 | EncodingSpecificOperations(); NullCheckIfThumbEE(13); |
| 327 | address = SP; |
| 328 | for i = 0 to 14 |
| 329 | if registers<i> == ‘1’ then |
| 330 | R[i} = if UnalignedAllowed then MemU[address,4] else MemA[address,4]; address = address + 4; |
| 331 | if registers<15> == ‘1’ then |
| 332 | if UnalignedAllowed then |
| 333 | LoadWritePC(MemU[address,4]); |
| 334 | else |
| 335 | LoadWritePC(MemA[address,4]); |
| 336 | if registers<13> == ‘0’ then SP = SP + 4*BitCount(registers); |
| 337 | if registers<13> == ‘1’ then SP = bits(32) UNKNOWN; |
| 338 | } |
| 339 | #endif |
| 340 | |
| 341 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 342 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 343 | if (!success) |
| 344 | return false; |
| 345 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 346 | if (ConditionPassed()) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 347 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 348 | const uint32_t addr_byte_size = GetAddressByteSize(); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 349 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 350 | if (!success) |
| 351 | return false; |
| 352 | uint32_t registers = 0; |
| 353 | uint32_t Rt; // the destination register |
| 354 | switch (encoding) { |
| 355 | case eEncodingT1: |
| 356 | registers = Bits32(opcode, 7, 0); |
| 357 | // The P bit represents PC. |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 358 | if (Bit32(opcode, 8)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 359 | registers |= (1u << 15); |
| 360 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 361 | if (BitCount(registers) < 1) |
| 362 | return false; |
| 363 | break; |
| 364 | case eEncodingT2: |
| 365 | // Ignore bit 13. |
| 366 | registers = Bits32(opcode, 15, 0) & ~0x2000; |
| 367 | // if BitCount(registers) < 2 || (P == '1' && M == '1') then UNPREDICTABLE; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 368 | if (BitCount(registers) < 2 || (Bit32(opcode, 15) && Bit32(opcode, 14))) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 369 | return false; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 370 | // if registers<15> == '1' && InITBlock() && !LastInITBlock() then UNPREDICTABLE; |
| 371 | if (BitIsSet(registers, 15) && InITBlock() && !LastInITBlock()) |
| 372 | return false; |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 373 | break; |
| 374 | case eEncodingT3: |
| 375 | Rt = Bits32(opcode, 15, 12); |
| 376 | // if t == 13 || (t == 15 && InITBlock() && !LastInITBlock()) then UNPREDICTABLE; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 377 | if (Rt == 13) |
| 378 | return false; |
| 379 | if (Rt == 15 && InITBlock() && !LastInITBlock()) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 380 | return false; |
| 381 | registers = (1u << Rt); |
| 382 | break; |
| 383 | case eEncodingA1: |
| 384 | registers = Bits32(opcode, 15, 0); |
| 385 | // Instead of return false, let's handle the following case as well, |
| 386 | // which amounts to popping one reg from the full descending stacks. |
| 387 | // if BitCount(register_list) < 2 then SEE LDM / LDMIA / LDMFD; |
| 388 | |
| 389 | // if registers<13> == ‘1’ && ArchVersion() >= 7 then UNPREDICTABLE; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 390 | if (BitIsSet(opcode, 13) && ArchVersion() >= ARMv7) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 391 | return false; |
| 392 | break; |
| 393 | case eEncodingA2: |
| 394 | Rt = Bits32(opcode, 15, 12); |
| 395 | // if t == 13 then UNPREDICTABLE; |
| 396 | if (Rt == dwarf_sp) |
| 397 | return false; |
| 398 | registers = (1u << Rt); |
| 399 | break; |
| 400 | default: |
| 401 | return false; |
| 402 | } |
| 403 | addr_t sp_offset = addr_byte_size * BitCount (registers); |
| 404 | addr_t addr = sp; |
| 405 | uint32_t i, data; |
| 406 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 407 | EmulateInstruction::Context context; |
| 408 | context.type = EmulateInstruction::eContextPopRegisterOffStack; |
| 409 | Register dwarf_reg; |
| 410 | dwarf_reg.SetRegister (eRegisterKindDWARF, 0); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 411 | for (i=0; i<15; ++i) |
| 412 | { |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 413 | if (BitIsSet (registers, i)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 414 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 415 | dwarf_reg.num = dwarf_r0 + i; |
| 416 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 417 | data = MemARead(context, addr, 4, 0, &success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 418 | if (!success) |
| 419 | return false; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 420 | if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_reg.num, data)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 421 | return false; |
| 422 | addr += addr_byte_size; |
| 423 | } |
| 424 | } |
| 425 | |
Johnny Chen | 7c1bf92 | 2011-02-08 23:49:37 +0000 | [diff] [blame] | 426 | if (BitIsSet (registers, 15)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 427 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 428 | dwarf_reg.num = dwarf_pc; |
| 429 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 430 | data = MemARead(context, addr, 4, 0, &success); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 431 | if (!success) |
| 432 | return false; |
Johnny Chen | f3eaacf | 2011-02-09 19:30:49 +0000 | [diff] [blame] | 433 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 434 | if (!LoadWritePC(context, data)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 435 | return false; |
| 436 | addr += addr_byte_size; |
| 437 | } |
| 438 | |
| 439 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 440 | context.SetImmediateSigned (sp_offset); |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 441 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 442 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset)) |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 443 | return false; |
| 444 | } |
| 445 | return true; |
| 446 | } |
| 447 | |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 448 | // 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] | 449 | // ADD (SP plus immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 450 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 451 | EmulateInstructionARM::EmulateADDRdSPImm (ARMEncoding encoding) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 452 | { |
| 453 | #if 0 |
| 454 | // ARM pseudo code... |
| 455 | if (ConditionPassed()) |
| 456 | { |
| 457 | EncodingSpecificOperations(); |
| 458 | (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’); |
| 459 | if d == 15 then |
| 460 | ALUWritePC(result); // setflags is always FALSE here |
| 461 | else |
| 462 | R[d] = result; |
| 463 | if setflags then |
| 464 | APSR.N = result<31>; |
| 465 | APSR.Z = IsZeroBit(result); |
| 466 | APSR.C = carry; |
| 467 | APSR.V = overflow; |
| 468 | } |
| 469 | #endif |
| 470 | |
| 471 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 472 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 473 | if (!success) |
| 474 | return false; |
| 475 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 476 | if (ConditionPassed()) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 477 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 478 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 479 | if (!success) |
| 480 | return false; |
| 481 | uint32_t Rd; // the destination register |
| 482 | uint32_t imm32; |
| 483 | switch (encoding) { |
| 484 | case eEncodingT1: |
| 485 | Rd = 7; |
| 486 | imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32) |
| 487 | break; |
| 488 | case eEncodingA1: |
| 489 | Rd = Bits32(opcode, 15, 12); |
| 490 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 491 | break; |
| 492 | default: |
| 493 | return false; |
| 494 | } |
| 495 | addr_t sp_offset = imm32; |
| 496 | addr_t addr = sp + sp_offset; // a pointer to the stack area |
| 497 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 498 | EmulateInstruction::Context context; |
| 499 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 500 | Register sp_reg; |
| 501 | sp_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP); |
| 502 | context.SetRegisterPlusOffset (sp_reg, sp_offset); |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 503 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 504 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, addr)) |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 505 | return false; |
| 506 | } |
| 507 | return true; |
| 508 | } |
| 509 | |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 510 | // Set r7 or ip to the current stack pointer. |
| 511 | // MOV (register) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 512 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 513 | EmulateInstructionARM::EmulateMOVRdSP (ARMEncoding encoding) |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 514 | { |
| 515 | #if 0 |
| 516 | // ARM pseudo code... |
| 517 | if (ConditionPassed()) |
| 518 | { |
| 519 | EncodingSpecificOperations(); |
| 520 | result = R[m]; |
| 521 | if d == 15 then |
| 522 | ALUWritePC(result); // setflags is always FALSE here |
| 523 | else |
| 524 | R[d] = result; |
| 525 | if setflags then |
| 526 | APSR.N = result<31>; |
| 527 | APSR.Z = IsZeroBit(result); |
| 528 | // APSR.C unchanged |
| 529 | // APSR.V unchanged |
| 530 | } |
| 531 | #endif |
| 532 | |
| 533 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 534 | //const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 535 | //if (!success) |
| 536 | // return false; |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 537 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 538 | if (ConditionPassed()) |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 539 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 540 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 541 | if (!success) |
| 542 | return false; |
| 543 | uint32_t Rd; // the destination register |
| 544 | switch (encoding) { |
| 545 | case eEncodingT1: |
| 546 | Rd = 7; |
| 547 | break; |
| 548 | case eEncodingA1: |
| 549 | Rd = 12; |
| 550 | break; |
| 551 | default: |
| 552 | return false; |
| 553 | } |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 554 | |
| 555 | EmulateInstruction::Context context; |
| 556 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 557 | Register sp_reg; |
| 558 | sp_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP); |
| 559 | context.SetRegisterPlusOffset (sp_reg, 0); |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 560 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 561 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, sp)) |
Johnny Chen | 2ccad83 | 2011-01-28 19:57:25 +0000 | [diff] [blame] | 562 | return false; |
| 563 | } |
| 564 | return true; |
| 565 | } |
| 566 | |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 567 | // Move from high register (r8-r15) to low register (r0-r7). |
| 568 | // MOV (register) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 569 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 570 | EmulateInstructionARM::EmulateMOVLowHigh (ARMEncoding encoding) |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 571 | { |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 572 | return EmulateMOVRdRm (encoding); |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | // Move from register to register. |
| 576 | // MOV (register) |
| 577 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 578 | EmulateInstructionARM::EmulateMOVRdRm (ARMEncoding encoding) |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 579 | { |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 580 | #if 0 |
| 581 | // ARM pseudo code... |
| 582 | if (ConditionPassed()) |
| 583 | { |
| 584 | EncodingSpecificOperations(); |
| 585 | result = R[m]; |
| 586 | if d == 15 then |
| 587 | ALUWritePC(result); // setflags is always FALSE here |
| 588 | else |
| 589 | R[d] = result; |
| 590 | if setflags then |
| 591 | APSR.N = result<31>; |
| 592 | APSR.Z = IsZeroBit(result); |
| 593 | // APSR.C unchanged |
| 594 | // APSR.V unchanged |
| 595 | } |
| 596 | #endif |
| 597 | |
| 598 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 599 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 600 | if (!success) |
| 601 | return false; |
| 602 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 603 | if (ConditionPassed()) |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 604 | { |
| 605 | uint32_t Rm; // the source register |
| 606 | uint32_t Rd; // the destination register |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 607 | bool setflags; |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 608 | switch (encoding) { |
| 609 | case eEncodingT1: |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 610 | Rd = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 0); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 611 | Rm = Bits32(opcode, 6, 3); |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 612 | setflags = false; |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 613 | if (Rd == 15 && InITBlock() && !LastInITBlock()) |
| 614 | return false; |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 615 | break; |
| 616 | case eEncodingT2: |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 617 | Rd = Bits32(opcode, 2, 0); |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 618 | Rm = Bits32(opcode, 5, 3); |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 619 | setflags = true; |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 620 | if (InITBlock()) |
| 621 | return false; |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 622 | break; |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 623 | case eEncodingT3: |
| 624 | Rd = Bits32(opcode, 11, 8); |
| 625 | Rm = Bits32(opcode, 3, 0); |
| 626 | setflags = BitIsSet(opcode, 20); |
| 627 | // if setflags && (BadReg(d) || BadReg(m)) then UNPREDICTABLE; |
| 628 | if (setflags && (BadReg(Rd) || BadReg(Rm))) |
| 629 | return false; |
| 630 | // if !setflags && (d == 15 || m == 15 || (d == 13 && m == 13)) then UNPREDICTABLE; |
| 631 | if (!setflags && (Rd == 15 || Rm == 15 || (Rd == 13 && Rm == 13))) |
| 632 | return false; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 633 | break; |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 634 | default: |
| 635 | return false; |
| 636 | } |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 637 | uint32_t result = ReadCoreReg(Rm, &success); |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 638 | if (!success) |
| 639 | return false; |
| 640 | |
| 641 | // The context specifies that Rm is to be moved into Rd. |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 642 | EmulateInstruction::Context context; |
| 643 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 644 | Register dwarf_reg; |
| 645 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm); |
| 646 | context.SetRegisterPlusOffset (dwarf_reg, 0); |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 647 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 648 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 649 | return false; |
Johnny Chen | 1c13b62 | 2011-01-29 00:11:15 +0000 | [diff] [blame] | 650 | } |
| 651 | return true; |
| 652 | } |
| 653 | |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 654 | // Move (immediate) writes an immediate value to the destination register. It |
| 655 | // can optionally update the condition flags based on the value. |
| 656 | // MOV (immediate) |
| 657 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 658 | EmulateInstructionARM::EmulateMOVRdImm (ARMEncoding encoding) |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 659 | { |
| 660 | #if 0 |
| 661 | // ARM pseudo code... |
| 662 | if (ConditionPassed()) |
| 663 | { |
| 664 | EncodingSpecificOperations(); |
| 665 | result = imm32; |
| 666 | if d == 15 then // Can only occur for ARM encoding |
| 667 | ALUWritePC(result); // setflags is always FALSE here |
| 668 | else |
| 669 | R[d] = result; |
| 670 | if setflags then |
| 671 | APSR.N = result<31>; |
| 672 | APSR.Z = IsZeroBit(result); |
| 673 | APSR.C = carry; |
| 674 | // APSR.V unchanged |
| 675 | } |
| 676 | #endif |
| 677 | bool success = false; |
| 678 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 679 | if (!success) |
| 680 | return false; |
| 681 | |
| 682 | if (ConditionPassed()) |
| 683 | { |
| 684 | uint32_t Rd; // the destination register |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 685 | uint32_t imm32; // the immediate value to be written to Rd |
| 686 | uint32_t carry; // the carry bit after ThumbExpandImm_C or ARMExpandImm_C. |
| 687 | bool setflags; |
| 688 | switch (encoding) { |
| 689 | case eEncodingT1: |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 690 | Rd = Bits32(opcode, 10, 8); |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 691 | setflags = !InITBlock(); |
| 692 | imm32 = Bits32(opcode, 7, 0); // imm32 = ZeroExtend(imm8, 32) |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 693 | carry = APSR_C; |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 694 | break; |
| 695 | case eEncodingT2: |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 696 | Rd = Bits32(opcode, 11, 8); |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 697 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 698 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); |
Johnny Chen | 9798cfc | 2011-02-14 23:33:58 +0000 | [diff] [blame] | 699 | if (BadReg(Rd)) |
| 700 | return false; |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 701 | break; |
| 702 | default: |
| 703 | return false; |
| 704 | } |
| 705 | uint32_t result = imm32; |
| 706 | |
| 707 | // The context specifies that an immediate is to be moved into Rd. |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 708 | EmulateInstruction::Context context; |
| 709 | context.type = EmulateInstruction::eContextImmediate; |
| 710 | context.SetNoArgs (); |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 711 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 712 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 713 | return false; |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 714 | } |
| 715 | return true; |
| 716 | } |
| 717 | |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 718 | // Bitwise NOT (immediate) writes the bitwise inverse of an immediate value to the destination register. |
| 719 | // It can optionally update the condition flags based on the value. |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 720 | bool |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 721 | EmulateInstructionARM::EmulateMVNImm (ARMEncoding encoding) |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 722 | { |
| 723 | #if 0 |
| 724 | // ARM pseudo code... |
| 725 | if (ConditionPassed()) |
| 726 | { |
| 727 | EncodingSpecificOperations(); |
| 728 | result = NOT(imm32); |
| 729 | if d == 15 then // Can only occur for ARM encoding |
| 730 | ALUWritePC(result); // setflags is always FALSE here |
| 731 | else |
| 732 | R[d] = result; |
| 733 | if setflags then |
| 734 | APSR.N = result<31>; |
| 735 | APSR.Z = IsZeroBit(result); |
| 736 | APSR.C = carry; |
| 737 | // APSR.V unchanged |
| 738 | } |
| 739 | #endif |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 740 | bool success = false; |
| 741 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 742 | if (!success) |
| 743 | return false; |
| 744 | |
| 745 | if (ConditionPassed()) |
| 746 | { |
| 747 | uint32_t Rd; // the destination register |
Johnny Chen | 357c30f | 2011-02-14 22:04:25 +0000 | [diff] [blame] | 748 | uint32_t imm32; // the output after ThumbExpandImm_C or ARMExpandImm_C |
| 749 | uint32_t carry; // the carry bit after ThumbExpandImm_C or ARMExpandImm_C |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 750 | bool setflags; |
| 751 | switch (encoding) { |
| 752 | case eEncodingT1: |
| 753 | Rd = Bits32(opcode, 11, 8); |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 754 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 755 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 756 | break; |
| 757 | case eEncodingA1: |
| 758 | Rd = Bits32(opcode, 15, 12); |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 759 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 760 | imm32 = ARMExpandImm_C(opcode, APSR_C, carry); |
| 761 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 762 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 763 | if (Rd == 15 && setflags) |
| 764 | return false; |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 765 | break; |
| 766 | default: |
| 767 | return false; |
| 768 | } |
| 769 | uint32_t result = ~imm32; |
| 770 | |
| 771 | // The context specifies that an immediate is to be moved into Rd. |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 772 | EmulateInstruction::Context context; |
| 773 | context.type = EmulateInstruction::eContextImmediate; |
| 774 | context.SetNoArgs (); |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 775 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 776 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 777 | return false; |
Johnny Chen | 33bf6ab | 2011-02-14 20:39:01 +0000 | [diff] [blame] | 778 | } |
| 779 | return true; |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 782 | // Bitwise NOT (register) writes the bitwise inverse of a register value to the destination register. |
| 783 | // It can optionally update the condition flags based on the result. |
| 784 | bool |
| 785 | EmulateInstructionARM::EmulateMVNReg (ARMEncoding encoding) |
| 786 | { |
| 787 | #if 0 |
| 788 | // ARM pseudo code... |
| 789 | if (ConditionPassed()) |
| 790 | { |
| 791 | EncodingSpecificOperations(); |
| 792 | (shifted, carry) = Shift_C(R[m], shift_t, shift_n, APSR.C); |
| 793 | result = NOT(shifted); |
| 794 | if d == 15 then // Can only occur for ARM encoding |
| 795 | ALUWritePC(result); // setflags is always FALSE here |
| 796 | else |
| 797 | R[d] = result; |
| 798 | if setflags then |
| 799 | APSR.N = result<31>; |
| 800 | APSR.Z = IsZeroBit(result); |
| 801 | APSR.C = carry; |
| 802 | // APSR.V unchanged |
| 803 | } |
| 804 | #endif |
| 805 | |
| 806 | bool success = false; |
| 807 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 808 | if (!success) |
| 809 | return false; |
| 810 | |
| 811 | if (ConditionPassed()) |
| 812 | { |
| 813 | uint32_t Rm; // the source register |
| 814 | uint32_t Rd; // the destination register |
| 815 | ARM_ShifterType shift_t; |
| 816 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 817 | bool setflags; |
| 818 | uint32_t carry; // the carry bit after the shift operation |
| 819 | switch (encoding) { |
| 820 | case eEncodingT1: |
| 821 | Rd = Bits32(opcode, 2, 0); |
| 822 | Rm = Bits32(opcode, 5, 3); |
| 823 | setflags = !InITBlock(); |
| 824 | shift_t = SRType_LSL; |
| 825 | shift_n = 0; |
| 826 | if (InITBlock()) |
| 827 | return false; |
| 828 | break; |
| 829 | case eEncodingT2: |
| 830 | Rd = Bits32(opcode, 11, 8); |
| 831 | Rm = Bits32(opcode, 3, 0); |
| 832 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 833 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 834 | // if (BadReg(d) || BadReg(m)) then UNPREDICTABLE; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 835 | if (BadReg(Rd) || BadReg(Rm)) |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 836 | return false; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 837 | break; |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 838 | case eEncodingA1: |
| 839 | Rd = Bits32(opcode, 15, 12); |
| 840 | Rm = Bits32(opcode, 3, 0); |
| 841 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 842 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 843 | break; |
| 844 | default: |
| 845 | return false; |
| 846 | } |
| 847 | uint32_t value = ReadCoreReg(Rm, &success); |
| 848 | if (!success) |
| 849 | return false; |
| 850 | |
| 851 | uint32_t shifted = Shift_C(value, shift_t, shift_n, APSR_C, carry); |
| 852 | uint32_t result = ~shifted; |
| 853 | |
| 854 | // The context specifies that an immediate is to be moved into Rd. |
| 855 | EmulateInstruction::Context context; |
| 856 | context.type = EmulateInstruction::eContextImmediate; |
| 857 | context.SetNoArgs (); |
| 858 | |
| 859 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 860 | return false; |
| 861 | } |
| 862 | return true; |
| 863 | } |
| 864 | |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 865 | // PC relative immediate load into register, possibly followed by ADD (SP plus register). |
| 866 | // LDR (literal) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 867 | bool |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 868 | EmulateInstructionARM::EmulateLDRRtPCRelative (ARMEncoding encoding) |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 869 | { |
| 870 | #if 0 |
| 871 | // ARM pseudo code... |
| 872 | if (ConditionPassed()) |
| 873 | { |
| 874 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 875 | base = Align(PC,4); |
| 876 | address = if add then (base + imm32) else (base - imm32); |
| 877 | data = MemU[address,4]; |
| 878 | if t == 15 then |
| 879 | if address<1:0> == ‘00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 880 | elsif UnalignedSupport() || address<1:0> = ‘00’ then |
| 881 | R[t] = data; |
| 882 | else // Can only apply before ARMv7 |
| 883 | if CurrentInstrSet() == InstrSet_ARM then |
| 884 | R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 885 | else |
| 886 | R[t] = bits(32) UNKNOWN; |
| 887 | } |
| 888 | #endif |
| 889 | |
| 890 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 891 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 892 | if (!success) |
| 893 | return false; |
| 894 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 895 | if (ConditionPassed()) |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 896 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 897 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 898 | if (!success) |
| 899 | return false; |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 900 | |
| 901 | // PC relative immediate load context |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 902 | EmulateInstruction::Context context; |
| 903 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 904 | Register pc_reg; |
| 905 | pc_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); |
| 906 | context.SetRegisterPlusOffset (pc_reg, 0); |
| 907 | |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 908 | uint32_t Rt; // the destination register |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 909 | uint32_t imm32; // immediate offset from the PC |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 910 | bool add; // +imm32 or -imm32? |
| 911 | addr_t base; // the base address |
| 912 | addr_t address; // the PC relative address |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 913 | uint32_t data; // the literal data value from the PC relative load |
| 914 | switch (encoding) { |
| 915 | case eEncodingT1: |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 916 | Rt = Bits32(opcode, 10, 8); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 917 | imm32 = Bits32(opcode, 7, 0) << 2; // imm32 = ZeroExtend(imm8:'00', 32); |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 918 | add = true; |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 919 | break; |
| 920 | case eEncodingT2: |
| 921 | Rt = Bits32(opcode, 15, 12); |
| 922 | imm32 = Bits32(opcode, 11, 0) << 2; // imm32 = ZeroExtend(imm12, 32); |
| 923 | add = BitIsSet(opcode, 23); |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 924 | if (Rt == 15 && InITBlock() && !LastInITBlock()) |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 925 | return false; |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 926 | break; |
| 927 | default: |
| 928 | return false; |
| 929 | } |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 930 | |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 931 | base = Align(pc, 4); |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 932 | if (add) |
| 933 | address = base + imm32; |
| 934 | else |
| 935 | address = base - imm32; |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 936 | |
| 937 | context.SetRegisterPlusOffset(pc_reg, address - base); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 938 | data = MemURead(context, address, 4, 0, &success); |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 939 | if (!success) |
Johnny Chen | 809742e | 2011-01-28 00:32:27 +0000 | [diff] [blame] | 940 | return false; |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 941 | |
| 942 | if (Rt == 15) |
| 943 | { |
| 944 | if (Bits32(address, 1, 0) == 0) |
| 945 | { |
| 946 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 947 | if (!LoadWritePC(context, data)) |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 948 | return false; |
| 949 | } |
| 950 | else |
| 951 | return false; |
| 952 | } |
| 953 | else if (UnalignedSupport() || Bits32(address, 1, 0) == 0) |
| 954 | { |
| 955 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data)) |
| 956 | return false; |
| 957 | } |
| 958 | else // We don't handle ARM for now. |
| 959 | return false; |
| 960 | |
| 961 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data)) |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 962 | return false; |
| 963 | } |
| 964 | return true; |
| 965 | } |
| 966 | |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 967 | // An add operation to adjust the SP. |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 968 | // ADD (SP plus immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 969 | bool |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 970 | EmulateInstructionARM::EmulateADDSPImm (ARMEncoding encoding) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 971 | { |
| 972 | #if 0 |
| 973 | // ARM pseudo code... |
| 974 | if (ConditionPassed()) |
| 975 | { |
| 976 | EncodingSpecificOperations(); |
| 977 | (result, carry, overflow) = AddWithCarry(SP, imm32, ‘0’); |
| 978 | if d == 15 then // Can only occur for ARM encoding |
| 979 | ALUWritePC(result); // setflags is always FALSE here |
| 980 | else |
| 981 | R[d] = result; |
| 982 | if setflags then |
| 983 | APSR.N = result<31>; |
| 984 | APSR.Z = IsZeroBit(result); |
| 985 | APSR.C = carry; |
| 986 | APSR.V = overflow; |
| 987 | } |
| 988 | #endif |
| 989 | |
| 990 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 991 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 992 | if (!success) |
| 993 | return false; |
| 994 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 995 | if (ConditionPassed()) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 996 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 997 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 998 | if (!success) |
| 999 | return false; |
| 1000 | uint32_t imm32; // the immediate operand |
| 1001 | switch (encoding) { |
| 1002 | case eEncodingT2: |
| 1003 | imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32) |
| 1004 | break; |
| 1005 | default: |
| 1006 | return false; |
| 1007 | } |
| 1008 | addr_t sp_offset = imm32; |
| 1009 | addr_t addr = sp + sp_offset; // the adjusted stack pointer value |
| 1010 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1011 | EmulateInstruction::Context context; |
| 1012 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 1013 | context.SetImmediateSigned (sp_offset); |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 1014 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1015 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 1016 | return false; |
| 1017 | } |
| 1018 | return true; |
| 1019 | } |
| 1020 | |
| 1021 | // An add operation to adjust the SP. |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1022 | // ADD (SP plus register) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1023 | bool |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 1024 | EmulateInstructionARM::EmulateADDSPRm (ARMEncoding encoding) |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1025 | { |
| 1026 | #if 0 |
| 1027 | // ARM pseudo code... |
| 1028 | if (ConditionPassed()) |
| 1029 | { |
| 1030 | EncodingSpecificOperations(); |
| 1031 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 1032 | (result, carry, overflow) = AddWithCarry(SP, shifted, ‘0’); |
| 1033 | if d == 15 then |
| 1034 | ALUWritePC(result); // setflags is always FALSE here |
| 1035 | else |
| 1036 | R[d] = result; |
| 1037 | if setflags then |
| 1038 | APSR.N = result<31>; |
| 1039 | APSR.Z = IsZeroBit(result); |
| 1040 | APSR.C = carry; |
| 1041 | APSR.V = overflow; |
| 1042 | } |
| 1043 | #endif |
| 1044 | |
| 1045 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1046 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1047 | if (!success) |
| 1048 | return false; |
| 1049 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1050 | if (ConditionPassed()) |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1051 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1052 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1053 | if (!success) |
| 1054 | return false; |
| 1055 | uint32_t Rm; // the second operand |
| 1056 | switch (encoding) { |
| 1057 | case eEncodingT2: |
| 1058 | Rm = Bits32(opcode, 6, 3); |
| 1059 | break; |
| 1060 | default: |
| 1061 | return false; |
| 1062 | } |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1063 | int32_t reg_value = ReadCoreReg(Rm, &success); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1064 | if (!success) |
| 1065 | return false; |
| 1066 | |
| 1067 | addr_t addr = (int32_t)sp + reg_value; // the adjusted stack pointer value |
| 1068 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1069 | EmulateInstruction::Context context; |
| 1070 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 1071 | context.SetImmediateSigned (reg_value); |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1072 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1073 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
Johnny Chen | 5b442b7 | 2011-01-27 19:34:30 +0000 | [diff] [blame] | 1074 | return false; |
| 1075 | } |
| 1076 | return true; |
| 1077 | } |
| 1078 | |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1079 | // Branch with Link and Exchange Instruction Sets (immediate) calls a subroutine |
| 1080 | // at a PC-relative address, and changes instruction set from ARM to Thumb, or |
| 1081 | // from Thumb to ARM. |
| 1082 | // BLX (immediate) |
| 1083 | bool |
| 1084 | EmulateInstructionARM::EmulateBLXImmediate (ARMEncoding encoding) |
| 1085 | { |
| 1086 | #if 0 |
| 1087 | // ARM pseudo code... |
| 1088 | if (ConditionPassed()) |
| 1089 | { |
| 1090 | EncodingSpecificOperations(); |
| 1091 | if CurrentInstrSet() == InstrSet_ARM then |
| 1092 | LR = PC - 4; |
| 1093 | else |
| 1094 | LR = PC<31:1> : '1'; |
| 1095 | if targetInstrSet == InstrSet_ARM then |
| 1096 | targetAddress = Align(PC,4) + imm32; |
| 1097 | else |
| 1098 | targetAddress = PC + imm32; |
| 1099 | SelectInstrSet(targetInstrSet); |
| 1100 | BranchWritePC(targetAddress); |
| 1101 | } |
| 1102 | #endif |
| 1103 | |
| 1104 | bool success = false; |
| 1105 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1106 | if (!success) |
| 1107 | return false; |
| 1108 | |
| 1109 | if (ConditionPassed()) |
| 1110 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1111 | EmulateInstruction::Context context; |
| 1112 | context.type = EmulateInstruction::eContextRelativeBranchImmediate; |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1113 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1114 | if (!success) |
| 1115 | return false; |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1116 | addr_t lr; // next instruction address |
| 1117 | addr_t target; // target address |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1118 | int32_t imm32; // PC-relative offset |
| 1119 | switch (encoding) { |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 1120 | case eEncodingT1: |
| 1121 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1122 | lr = pc | 1u; // return address |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1123 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 1124 | uint32_t imm10 = Bits32(opcode, 25, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1125 | uint32_t J1 = Bit32(opcode, 13); |
| 1126 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 1127 | uint32_t imm11 = Bits32(opcode, 10, 0); |
| 1128 | uint32_t I1 = !(J1 ^ S); |
| 1129 | uint32_t I2 = !(J2 ^ S); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1130 | 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] | 1131 | imm32 = llvm::SignExtend32<25>(imm25); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1132 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1133 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 1134 | if (InITBlock() && !LastInITBlock()) |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1135 | return false; |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 1136 | break; |
| 1137 | } |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1138 | case eEncodingT2: |
| 1139 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1140 | lr = pc | 1u; // return address |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1141 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1142 | uint32_t imm10H = Bits32(opcode, 25, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1143 | uint32_t J1 = Bit32(opcode, 13); |
| 1144 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1145 | uint32_t imm10L = Bits32(opcode, 10, 1); |
| 1146 | uint32_t I1 = !(J1 ^ S); |
| 1147 | uint32_t I2 = !(J2 ^ S); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1148 | 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] | 1149 | imm32 = llvm::SignExtend32<25>(imm25); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1150 | target = Align(pc, 4) + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1151 | context.SetModeAndImmediateSigned (eModeARM, 4 + imm32); |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 1152 | if (InITBlock() && !LastInITBlock()) |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1153 | return false; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1154 | break; |
| 1155 | } |
Johnny Chen | c47d0ca | 2011-02-08 18:58:31 +0000 | [diff] [blame] | 1156 | case eEncodingA1: |
| 1157 | lr = pc + 4; // return address |
| 1158 | imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1159 | target = Align(pc, 4) + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1160 | context.SetModeAndImmediateSigned (eModeARM, 8 + imm32); |
Johnny Chen | c47d0ca | 2011-02-08 18:58:31 +0000 | [diff] [blame] | 1161 | break; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1162 | case eEncodingA2: |
| 1163 | lr = pc + 4; // return address |
| 1164 | 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] | 1165 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1166 | context.SetModeAndImmediateSigned (eModeThumb, 8 + imm32); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1167 | break; |
| 1168 | default: |
| 1169 | return false; |
| 1170 | } |
| 1171 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr)) |
| 1172 | return false; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1173 | if (!BranchWritePC(context, target)) |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1174 | return false; |
| 1175 | } |
| 1176 | return true; |
| 1177 | } |
| 1178 | |
| 1179 | // Branch with Link and Exchange (register) calls a subroutine at an address and |
| 1180 | // instruction set specified by a register. |
| 1181 | // BLX (register) |
| 1182 | bool |
| 1183 | EmulateInstructionARM::EmulateBLXRm (ARMEncoding encoding) |
| 1184 | { |
| 1185 | #if 0 |
| 1186 | // ARM pseudo code... |
| 1187 | if (ConditionPassed()) |
| 1188 | { |
| 1189 | EncodingSpecificOperations(); |
| 1190 | target = R[m]; |
| 1191 | if CurrentInstrSet() == InstrSet_ARM then |
| 1192 | next_instr_addr = PC - 4; |
| 1193 | LR = next_instr_addr; |
| 1194 | else |
| 1195 | next_instr_addr = PC - 2; |
| 1196 | LR = next_instr_addr<31:1> : ‘1’; |
| 1197 | BXWritePC(target); |
| 1198 | } |
| 1199 | #endif |
| 1200 | |
| 1201 | bool success = false; |
| 1202 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1203 | if (!success) |
| 1204 | return false; |
| 1205 | |
| 1206 | if (ConditionPassed()) |
| 1207 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1208 | EmulateInstruction::Context context; |
| 1209 | context.type = EmulateInstruction::eContextAbsoluteBranchRegister; |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1210 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1211 | addr_t lr; // next instruction address |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1212 | if (!success) |
| 1213 | return false; |
| 1214 | uint32_t Rm; // the register with the target address |
| 1215 | switch (encoding) { |
| 1216 | case eEncodingT1: |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1217 | lr = (pc - 2) | 1u; // return address |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1218 | Rm = Bits32(opcode, 6, 3); |
| 1219 | // if m == 15 then UNPREDICTABLE; |
| 1220 | if (Rm == 15) |
| 1221 | return false; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 1222 | if (InITBlock() && !LastInITBlock()) |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1223 | return false; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1224 | break; |
| 1225 | case eEncodingA1: |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1226 | lr = pc - 4; // return address |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1227 | Rm = Bits32(opcode, 3, 0); |
| 1228 | // if m == 15 then UNPREDICTABLE; |
| 1229 | if (Rm == 15) |
| 1230 | return false; |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 1231 | break; |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1232 | default: |
| 1233 | return false; |
| 1234 | } |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1235 | addr_t target = ReadCoreReg (Rm, &success); |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1236 | if (!success) |
| 1237 | return false; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1238 | Register dwarf_reg; |
| 1239 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm); |
| 1240 | context.SetRegister (dwarf_reg); |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1241 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr)) |
| 1242 | return false; |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 1243 | if (!BXWritePC(context, target)) |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 1244 | return false; |
| 1245 | } |
| 1246 | return true; |
| 1247 | } |
| 1248 | |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1249 | // Branch and Exchange causes a branch to an address and instruction set specified by a register. |
| 1250 | // BX |
| 1251 | bool |
| 1252 | EmulateInstructionARM::EmulateBXRm (ARMEncoding encoding) |
| 1253 | { |
| 1254 | #if 0 |
| 1255 | // ARM pseudo code... |
| 1256 | if (ConditionPassed()) |
| 1257 | { |
| 1258 | EncodingSpecificOperations(); |
| 1259 | BXWritePC(R[m]); |
| 1260 | } |
| 1261 | #endif |
| 1262 | |
| 1263 | bool success = false; |
| 1264 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1265 | if (!success) |
| 1266 | return false; |
| 1267 | |
| 1268 | if (ConditionPassed()) |
| 1269 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1270 | EmulateInstruction::Context context; |
| 1271 | context.type = EmulateInstruction::eContextAbsoluteBranchRegister; |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1272 | uint32_t Rm; // the register with the target address |
| 1273 | switch (encoding) { |
| 1274 | case eEncodingT1: |
| 1275 | Rm = Bits32(opcode, 6, 3); |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 1276 | if (InITBlock() && !LastInITBlock()) |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1277 | return false; |
| 1278 | break; |
| 1279 | case eEncodingA1: |
| 1280 | Rm = Bits32(opcode, 3, 0); |
| 1281 | break; |
| 1282 | default: |
| 1283 | return false; |
| 1284 | } |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1285 | addr_t target = ReadCoreReg (Rm, &success); |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1286 | if (!success) |
| 1287 | return false; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1288 | |
| 1289 | Register dwarf_reg; |
| 1290 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rm); |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 1291 | context.SetRegister (dwarf_reg); |
| 1292 | if (!BXWritePC(context, target)) |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 1293 | return false; |
| 1294 | } |
| 1295 | return true; |
| 1296 | } |
| 1297 | |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1298 | // Set r7 to point to some ip offset. |
| 1299 | // SUB (immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1300 | bool |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 1301 | EmulateInstructionARM::EmulateSUBR7IPImm (ARMEncoding encoding) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1302 | { |
| 1303 | #if 0 |
| 1304 | // ARM pseudo code... |
| 1305 | if (ConditionPassed()) |
| 1306 | { |
| 1307 | EncodingSpecificOperations(); |
| 1308 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 1309 | if d == 15 then // Can only occur for ARM encoding |
| 1310 | ALUWritePC(result); // setflags is always FALSE here |
| 1311 | else |
| 1312 | R[d] = result; |
| 1313 | if setflags then |
| 1314 | APSR.N = result<31>; |
| 1315 | APSR.Z = IsZeroBit(result); |
| 1316 | APSR.C = carry; |
| 1317 | APSR.V = overflow; |
| 1318 | } |
| 1319 | #endif |
| 1320 | |
| 1321 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1322 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1323 | if (!success) |
| 1324 | return false; |
| 1325 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1326 | if (ConditionPassed()) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1327 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1328 | const addr_t ip = ReadCoreReg (12, &success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1329 | if (!success) |
| 1330 | return false; |
| 1331 | uint32_t imm32; |
| 1332 | switch (encoding) { |
| 1333 | case eEncodingA1: |
| 1334 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 1335 | break; |
| 1336 | default: |
| 1337 | return false; |
| 1338 | } |
| 1339 | addr_t ip_offset = imm32; |
| 1340 | addr_t addr = ip - ip_offset; // the adjusted ip value |
| 1341 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1342 | EmulateInstruction::Context context; |
| 1343 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 1344 | Register dwarf_reg; |
| 1345 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r12); |
| 1346 | context.SetRegisterPlusOffset (dwarf_reg, -ip_offset); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1347 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1348 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r7, addr)) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1349 | return false; |
| 1350 | } |
| 1351 | return true; |
| 1352 | } |
| 1353 | |
| 1354 | // Set ip to point to some stack offset. |
| 1355 | // SUB (SP minus immediate) |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1356 | bool |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 1357 | EmulateInstructionARM::EmulateSUBIPSPImm (ARMEncoding encoding) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1358 | { |
| 1359 | #if 0 |
| 1360 | // ARM pseudo code... |
| 1361 | if (ConditionPassed()) |
| 1362 | { |
| 1363 | EncodingSpecificOperations(); |
| 1364 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 1365 | if d == 15 then // Can only occur for ARM encoding |
| 1366 | ALUWritePC(result); // setflags is always FALSE here |
| 1367 | else |
| 1368 | R[d] = result; |
| 1369 | if setflags then |
| 1370 | APSR.N = result<31>; |
| 1371 | APSR.Z = IsZeroBit(result); |
| 1372 | APSR.C = carry; |
| 1373 | APSR.V = overflow; |
| 1374 | } |
| 1375 | #endif |
| 1376 | |
| 1377 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1378 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1379 | if (!success) |
| 1380 | return false; |
| 1381 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1382 | if (ConditionPassed()) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1383 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1384 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1385 | if (!success) |
| 1386 | return false; |
| 1387 | uint32_t imm32; |
| 1388 | switch (encoding) { |
| 1389 | case eEncodingA1: |
| 1390 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 1391 | break; |
| 1392 | default: |
| 1393 | return false; |
| 1394 | } |
| 1395 | addr_t sp_offset = imm32; |
| 1396 | addr_t addr = sp - sp_offset; // the adjusted stack pointer value |
| 1397 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1398 | EmulateInstruction::Context context; |
| 1399 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 1400 | Register dwarf_reg; |
| 1401 | dwarf_reg.SetRegister (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP); |
| 1402 | context.SetRegisterPlusOffset (dwarf_reg, -sp_offset); |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1403 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1404 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r12, addr)) |
Johnny Chen | 0d0148e | 2011-01-28 02:26:08 +0000 | [diff] [blame] | 1405 | return false; |
| 1406 | } |
| 1407 | return true; |
| 1408 | } |
| 1409 | |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1410 | // A sub operation to adjust the SP -- allocate space for local storage. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1411 | bool |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 1412 | EmulateInstructionARM::EmulateSUBSPImm (ARMEncoding encoding) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1413 | { |
| 1414 | #if 0 |
| 1415 | // ARM pseudo code... |
| 1416 | if (ConditionPassed()) |
| 1417 | { |
| 1418 | EncodingSpecificOperations(); |
| 1419 | (result, carry, overflow) = AddWithCarry(SP, NOT(imm32), ‘1’); |
| 1420 | if d == 15 then // Can only occur for ARM encoding |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1421 | ALUWritePC(result); // setflags is always FALSE here |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1422 | else |
| 1423 | R[d] = result; |
| 1424 | if setflags then |
| 1425 | APSR.N = result<31>; |
| 1426 | APSR.Z = IsZeroBit(result); |
| 1427 | APSR.C = carry; |
| 1428 | APSR.V = overflow; |
| 1429 | } |
| 1430 | #endif |
| 1431 | |
| 1432 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1433 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1434 | if (!success) |
| 1435 | return false; |
| 1436 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1437 | if (ConditionPassed()) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1438 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1439 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1440 | if (!success) |
| 1441 | return false; |
| 1442 | uint32_t imm32; |
| 1443 | switch (encoding) { |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 1444 | case eEncodingT1: |
| 1445 | imm32 = ThumbImmScaled(opcode); // imm32 = ZeroExtend(imm7:'00', 32) |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 1446 | break; |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 1447 | case eEncodingT2: |
| 1448 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 1449 | break; |
| 1450 | case eEncodingT3: |
| 1451 | imm32 = ThumbImm12(opcode); // imm32 = ZeroExtend(i:imm3:imm8, 32) |
| 1452 | break; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1453 | case eEncodingA1: |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 1454 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1455 | break; |
| 1456 | default: |
| 1457 | return false; |
| 1458 | } |
| 1459 | addr_t sp_offset = imm32; |
| 1460 | addr_t addr = sp - sp_offset; // the adjusted stack pointer value |
| 1461 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1462 | EmulateInstruction::Context context; |
| 1463 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
| 1464 | context.SetImmediateSigned (-sp_offset); |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1465 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1466 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, addr)) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 1467 | return false; |
| 1468 | } |
| 1469 | return true; |
| 1470 | } |
| 1471 | |
Johnny Chen | 08c25e8 | 2011-01-31 18:02:28 +0000 | [diff] [blame] | 1472 | // A store operation to the stack that also updates the SP. |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1473 | bool |
| 1474 | EmulateInstructionARM::EmulateSTRRtSP (ARMEncoding encoding) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1475 | { |
| 1476 | #if 0 |
| 1477 | // ARM pseudo code... |
| 1478 | if (ConditionPassed()) |
| 1479 | { |
| 1480 | EncodingSpecificOperations(); |
| 1481 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 1482 | address = if index then offset_addr else R[n]; |
| 1483 | MemU[address,4] = if t == 15 then PCStoreValue() else R[t]; |
| 1484 | if wback then R[n] = offset_addr; |
| 1485 | } |
| 1486 | #endif |
| 1487 | |
| 1488 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1489 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1490 | if (!success) |
| 1491 | return false; |
| 1492 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1493 | if (ConditionPassed()) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1494 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1495 | const uint32_t addr_byte_size = GetAddressByteSize(); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1496 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1497 | if (!success) |
| 1498 | return false; |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 1499 | uint32_t Rt; // the source register |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1500 | uint32_t imm12; |
| 1501 | switch (encoding) { |
| 1502 | case eEncodingA1: |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 1503 | Rt = Bits32(opcode, 15, 12); |
| 1504 | imm12 = Bits32(opcode, 11, 0); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1505 | break; |
| 1506 | default: |
| 1507 | return false; |
| 1508 | } |
| 1509 | addr_t sp_offset = imm12; |
| 1510 | addr_t addr = sp - sp_offset; |
| 1511 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1512 | EmulateInstruction::Context context; |
| 1513 | context.type = EmulateInstruction::eContextPushRegisterOnStack; |
| 1514 | Register dwarf_reg; |
| 1515 | dwarf_reg.SetRegister (eRegisterKindDWARF, 0); |
Johnny Chen | 91d9986 | 2011-01-25 19:07:04 +0000 | [diff] [blame] | 1516 | if (Rt != 15) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1517 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1518 | dwarf_reg.num = dwarf_r0 + Rt; |
| 1519 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1520 | uint32_t reg_value = ReadCoreReg(Rt, &success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1521 | if (!success) |
| 1522 | return false; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 1523 | if (!MemUWrite (context, addr, reg_value, addr_byte_size)) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1524 | return false; |
| 1525 | } |
| 1526 | else |
| 1527 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1528 | dwarf_reg.num = dwarf_pc; |
| 1529 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1530 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1531 | if (!success) |
| 1532 | return false; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 1533 | if (!MemUWrite (context, addr, pc + 8, addr_byte_size)) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1534 | return false; |
| 1535 | } |
| 1536 | |
| 1537 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1538 | context.SetImmediateSigned (-sp_offset); |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1539 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1540 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 1541 | return false; |
| 1542 | } |
| 1543 | return true; |
| 1544 | } |
| 1545 | |
Johnny Chen | 08c25e8 | 2011-01-31 18:02:28 +0000 | [diff] [blame] | 1546 | // Vector Push stores multiple extension registers to the stack. |
| 1547 | // 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] | 1548 | bool |
| 1549 | EmulateInstructionARM::EmulateVPUSH (ARMEncoding encoding) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1550 | { |
| 1551 | #if 0 |
| 1552 | // ARM pseudo code... |
| 1553 | if (ConditionPassed()) |
| 1554 | { |
| 1555 | EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13); |
| 1556 | address = SP - imm32; |
| 1557 | SP = SP - imm32; |
| 1558 | if single_regs then |
| 1559 | for r = 0 to regs-1 |
| 1560 | MemA[address,4] = S[d+r]; address = address+4; |
| 1561 | else |
| 1562 | for r = 0 to regs-1 |
| 1563 | // Store as two word-aligned words in the correct order for current endianness. |
| 1564 | MemA[address,4] = if BigEndian() then D[d+r]<63:32> else D[d+r]<31:0>; |
| 1565 | MemA[address+4,4] = if BigEndian() then D[d+r]<31:0> else D[d+r]<63:32>; |
| 1566 | address = address+8; |
| 1567 | } |
| 1568 | #endif |
| 1569 | |
| 1570 | bool success = false; |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1571 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1572 | if (!success) |
| 1573 | return false; |
| 1574 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1575 | if (ConditionPassed()) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1576 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1577 | const uint32_t addr_byte_size = GetAddressByteSize(); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1578 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1579 | if (!success) |
| 1580 | return false; |
| 1581 | bool single_regs; |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1582 | uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1583 | uint32_t imm32; // stack offset |
| 1584 | uint32_t regs; // number of registers |
| 1585 | switch (encoding) { |
| 1586 | case eEncodingT1: |
| 1587 | case eEncodingA1: |
| 1588 | single_regs = false; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1589 | d = Bit32(opcode, 22) << 4 | Bits32(opcode, 15, 12); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1590 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1591 | // If UInt(imm8) is odd, see "FSTMX". |
| 1592 | regs = Bits32(opcode, 7, 0) / 2; |
| 1593 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1594 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1595 | return false; |
| 1596 | break; |
| 1597 | case eEncodingT2: |
| 1598 | case eEncodingA2: |
| 1599 | single_regs = true; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1600 | d = Bits32(opcode, 15, 12) << 1 | Bit32(opcode, 22); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1601 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1602 | regs = Bits32(opcode, 7, 0); |
| 1603 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1604 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1605 | return false; |
| 1606 | break; |
| 1607 | default: |
| 1608 | return false; |
| 1609 | } |
| 1610 | uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0; |
| 1611 | uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2; |
| 1612 | addr_t sp_offset = imm32; |
| 1613 | addr_t addr = sp - sp_offset; |
| 1614 | uint32_t i; |
| 1615 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1616 | EmulateInstruction::Context context; |
| 1617 | context.type = EmulateInstruction::eContextPushRegisterOnStack; |
| 1618 | Register dwarf_reg; |
| 1619 | dwarf_reg.SetRegister (eRegisterKindDWARF, 0); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1620 | for (i=d; i<regs; ++i) |
| 1621 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1622 | dwarf_reg.num = start_reg + i; |
| 1623 | context.SetRegisterPlusOffset ( dwarf_reg, addr - sp); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1624 | // uint64_t to accommodate 64-bit registers. |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1625 | uint64_t reg_value = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_reg.num, 0, &success); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1626 | if (!success) |
| 1627 | return false; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 1628 | if (!MemAWrite (context, addr, reg_value, reg_byte_size)) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1629 | return false; |
| 1630 | addr += reg_byte_size; |
| 1631 | } |
| 1632 | |
| 1633 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1634 | context.SetImmediateSigned (-sp_offset); |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1635 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 1636 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp - sp_offset)) |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 1637 | return false; |
| 1638 | } |
| 1639 | return true; |
| 1640 | } |
| 1641 | |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1642 | // Vector Pop loads multiple extension registers from the stack. |
| 1643 | // It also updates SP to point just above the loaded data. |
| 1644 | bool |
| 1645 | EmulateInstructionARM::EmulateVPOP (ARMEncoding encoding) |
| 1646 | { |
| 1647 | #if 0 |
| 1648 | // ARM pseudo code... |
| 1649 | if (ConditionPassed()) |
| 1650 | { |
| 1651 | EncodingSpecificOperations(); CheckVFPEnabled(TRUE); NullCheckIfThumbEE(13); |
| 1652 | address = SP; |
| 1653 | SP = SP + imm32; |
| 1654 | if single_regs then |
| 1655 | for r = 0 to regs-1 |
| 1656 | S[d+r] = MemA[address,4]; address = address+4; |
| 1657 | else |
| 1658 | for r = 0 to regs-1 |
| 1659 | word1 = MemA[address,4]; word2 = MemA[address+4,4]; address = address+8; |
| 1660 | // Combine the word-aligned words in the correct order for current endianness. |
| 1661 | D[d+r] = if BigEndian() then word1:word2 else word2:word1; |
| 1662 | } |
| 1663 | #endif |
| 1664 | |
| 1665 | bool success = false; |
| 1666 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1667 | if (!success) |
| 1668 | return false; |
| 1669 | |
| 1670 | if (ConditionPassed()) |
| 1671 | { |
| 1672 | const uint32_t addr_byte_size = GetAddressByteSize(); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1673 | const addr_t sp = ReadCoreReg (SP_REG, &success); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1674 | if (!success) |
| 1675 | return false; |
| 1676 | bool single_regs; |
| 1677 | uint32_t d; // UInt(D:Vd) or UInt(Vd:D) starting register |
| 1678 | uint32_t imm32; // stack offset |
| 1679 | uint32_t regs; // number of registers |
| 1680 | switch (encoding) { |
| 1681 | case eEncodingT1: |
| 1682 | case eEncodingA1: |
| 1683 | single_regs = false; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1684 | d = Bit32(opcode, 22) << 4 | Bits32(opcode, 15, 12); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1685 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1686 | // If UInt(imm8) is odd, see "FLDMX". |
| 1687 | regs = Bits32(opcode, 7, 0) / 2; |
| 1688 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1689 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1690 | return false; |
| 1691 | break; |
| 1692 | case eEncodingT2: |
| 1693 | case eEncodingA2: |
| 1694 | single_regs = true; |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1695 | d = Bits32(opcode, 15, 12) << 1 | Bit32(opcode, 22); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1696 | imm32 = Bits32(opcode, 7, 0) * addr_byte_size; |
| 1697 | regs = Bits32(opcode, 7, 0); |
| 1698 | // if regs == 0 || regs > 16 || (d+regs) > 32 then UNPREDICTABLE; |
| 1699 | if (regs == 0 || regs > 16 || (d + regs) > 32) |
| 1700 | return false; |
| 1701 | break; |
| 1702 | default: |
| 1703 | return false; |
| 1704 | } |
| 1705 | uint32_t start_reg = single_regs ? dwarf_s0 : dwarf_d0; |
| 1706 | uint32_t reg_byte_size = single_regs ? addr_byte_size : addr_byte_size * 2; |
| 1707 | addr_t sp_offset = imm32; |
| 1708 | addr_t addr = sp; |
| 1709 | uint32_t i; |
| 1710 | uint64_t data; // uint64_t to accomodate 64-bit registers. |
| 1711 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1712 | EmulateInstruction::Context context; |
| 1713 | context.type = EmulateInstruction::eContextPopRegisterOffStack; |
| 1714 | Register dwarf_reg; |
| 1715 | dwarf_reg.SetRegister (eRegisterKindDWARF, 0); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1716 | for (i=d; i<regs; ++i) |
| 1717 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1718 | dwarf_reg.num = start_reg + i; |
| 1719 | context.SetRegisterPlusOffset (dwarf_reg, addr - sp); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 1720 | data = MemARead(context, addr, reg_byte_size, 0, &success); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1721 | if (!success) |
| 1722 | return false; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1723 | if (!WriteRegisterUnsigned(context, eRegisterKindDWARF, dwarf_reg.num, data)) |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1724 | return false; |
| 1725 | addr += reg_byte_size; |
| 1726 | } |
| 1727 | |
| 1728 | context.type = EmulateInstruction::eContextAdjustStackPointer; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1729 | context.SetImmediateSigned (sp_offset); |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 1730 | |
| 1731 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, sp + sp_offset)) |
| 1732 | return false; |
| 1733 | } |
| 1734 | return true; |
| 1735 | } |
| 1736 | |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 1737 | // SVC (previously SWI) |
| 1738 | bool |
| 1739 | EmulateInstructionARM::EmulateSVC (ARMEncoding encoding) |
| 1740 | { |
| 1741 | #if 0 |
| 1742 | // ARM pseudo code... |
| 1743 | if (ConditionPassed()) |
| 1744 | { |
| 1745 | EncodingSpecificOperations(); |
| 1746 | CallSupervisor(); |
| 1747 | } |
| 1748 | #endif |
| 1749 | |
| 1750 | bool success = false; |
| 1751 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1752 | if (!success) |
| 1753 | return false; |
| 1754 | |
| 1755 | if (ConditionPassed()) |
| 1756 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1757 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 1758 | addr_t lr; // next instruction address |
| 1759 | if (!success) |
| 1760 | return false; |
| 1761 | uint32_t imm32; // the immediate constant |
| 1762 | uint32_t mode; // ARM or Thumb mode |
| 1763 | switch (encoding) { |
| 1764 | case eEncodingT1: |
| 1765 | lr = (pc + 2) | 1u; // return address |
| 1766 | imm32 = Bits32(opcode, 7, 0); |
| 1767 | mode = eModeThumb; |
| 1768 | break; |
| 1769 | case eEncodingA1: |
| 1770 | lr = pc + 4; // return address |
| 1771 | imm32 = Bits32(opcode, 23, 0); |
| 1772 | mode = eModeARM; |
| 1773 | break; |
| 1774 | default: |
| 1775 | return false; |
| 1776 | } |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1777 | |
| 1778 | EmulateInstruction::Context context; |
| 1779 | context.type = EmulateInstruction::eContextSupervisorCall; |
| 1780 | context.SetModeAndImmediate (mode, imm32); |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 1781 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, lr)) |
| 1782 | return false; |
| 1783 | } |
| 1784 | return true; |
| 1785 | } |
| 1786 | |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 1787 | // If Then makes up to four following instructions (the IT block) conditional. |
| 1788 | bool |
| 1789 | EmulateInstructionARM::EmulateIT (ARMEncoding encoding) |
| 1790 | { |
| 1791 | #if 0 |
| 1792 | // ARM pseudo code... |
| 1793 | EncodingSpecificOperations(); |
| 1794 | ITSTATE.IT<7:0> = firstcond:mask; |
| 1795 | #endif |
| 1796 | |
| 1797 | bool success = false; |
| 1798 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1799 | if (!success) |
| 1800 | return false; |
| 1801 | |
| 1802 | m_it_session.InitIT(Bits32(opcode, 7, 0)); |
| 1803 | return true; |
| 1804 | } |
| 1805 | |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 1806 | // Branch causes a branch to a target address. |
| 1807 | bool |
| 1808 | EmulateInstructionARM::EmulateB (ARMEncoding encoding) |
| 1809 | { |
| 1810 | #if 0 |
| 1811 | // ARM pseudo code... |
| 1812 | if (ConditionPassed()) |
| 1813 | { |
| 1814 | EncodingSpecificOperations(); |
| 1815 | BranchWritePC(PC + imm32); |
| 1816 | } |
| 1817 | #endif |
| 1818 | |
| 1819 | bool success = false; |
| 1820 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1821 | if (!success) |
| 1822 | return false; |
| 1823 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1824 | if (ConditionPassed()) |
| 1825 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1826 | EmulateInstruction::Context context; |
| 1827 | context.type = EmulateInstruction::eContextRelativeBranchImmediate; |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1828 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1829 | if (!success) |
| 1830 | return false; |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1831 | addr_t target; // target address |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1832 | int32_t imm32; // PC-relative offset |
| 1833 | switch (encoding) { |
| 1834 | case eEncodingT1: |
| 1835 | // The 'cond' field is handled in EmulateInstructionARM::CurrentCond(). |
| 1836 | imm32 = llvm::SignExtend32<9>(Bits32(opcode, 7, 0) << 1); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1837 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1838 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1839 | break; |
| 1840 | case eEncodingT2: |
| 1841 | imm32 = llvm::SignExtend32<12>(Bits32(opcode, 10, 0)); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1842 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1843 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1844 | break; |
| 1845 | case eEncodingT3: |
| 1846 | // The 'cond' field is handled in EmulateInstructionARM::CurrentCond(). |
| 1847 | { |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1848 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1849 | uint32_t imm6 = Bits32(opcode, 21, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1850 | uint32_t J1 = Bit32(opcode, 13); |
| 1851 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1852 | uint32_t imm11 = Bits32(opcode, 10, 0); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1853 | 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] | 1854 | imm32 = llvm::SignExtend32<21>(imm21); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1855 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1856 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1857 | break; |
| 1858 | } |
| 1859 | case eEncodingT4: |
| 1860 | { |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1861 | uint32_t S = Bit32(opcode, 26); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1862 | uint32_t imm10 = Bits32(opcode, 25, 16); |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1863 | uint32_t J1 = Bit32(opcode, 13); |
| 1864 | uint32_t J2 = Bit32(opcode, 11); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1865 | uint32_t imm11 = Bits32(opcode, 10, 0); |
| 1866 | uint32_t I1 = !(J1 ^ S); |
| 1867 | uint32_t I2 = !(J2 ^ S); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1868 | 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] | 1869 | imm32 = llvm::SignExtend32<25>(imm25); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1870 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1871 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1872 | break; |
| 1873 | } |
| 1874 | case eEncodingA1: |
| 1875 | imm32 = llvm::SignExtend32<26>(Bits32(opcode, 23, 0) << 2); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1876 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1877 | context.SetModeAndImmediateSigned (eModeARM, 8 + imm32); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 1878 | break; |
| 1879 | default: |
| 1880 | return false; |
| 1881 | } |
| 1882 | if (!BranchWritePC(context, target)) |
| 1883 | return false; |
| 1884 | } |
| 1885 | return true; |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1888 | // Compare and Branch on Nonzero and Compare and Branch on Zero compare the value in a register with |
| 1889 | // zero and conditionally branch forward a constant value. They do not affect the condition flags. |
| 1890 | // CBNZ, CBZ |
| 1891 | bool |
| 1892 | EmulateInstructionARM::EmulateCB (ARMEncoding encoding) |
| 1893 | { |
| 1894 | #if 0 |
| 1895 | // ARM pseudo code... |
| 1896 | EncodingSpecificOperations(); |
| 1897 | if nonzero ^ IsZero(R[n]) then |
| 1898 | BranchWritePC(PC + imm32); |
| 1899 | #endif |
| 1900 | |
| 1901 | bool success = false; |
| 1902 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1903 | if (!success) |
| 1904 | return false; |
| 1905 | |
| 1906 | // Read the register value from the operand register Rn. |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1907 | uint32_t reg_val = ReadCoreReg(Bits32(opcode, 2, 0), &success); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1908 | if (!success) |
| 1909 | return false; |
| 1910 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1911 | EmulateInstruction::Context context; |
| 1912 | context.type = EmulateInstruction::eContextRelativeBranchImmediate; |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1913 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1914 | if (!success) |
| 1915 | return false; |
| 1916 | |
| 1917 | addr_t target; // target address |
| 1918 | uint32_t imm32; // PC-relative offset to branch forward |
| 1919 | bool nonzero; |
| 1920 | switch (encoding) { |
| 1921 | case eEncodingT1: |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 1922 | imm32 = Bit32(opcode, 9) << 6 | Bits32(opcode, 7, 3) << 1; |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1923 | nonzero = BitIsSet(opcode, 11); |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1924 | target = pc + imm32; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 1925 | context.SetModeAndImmediateSigned (eModeThumb, 4 + imm32); |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 1926 | break; |
| 1927 | default: |
| 1928 | return false; |
| 1929 | } |
| 1930 | if (nonzero ^ (reg_val == 0)) |
| 1931 | if (!BranchWritePC(context, target)) |
| 1932 | return false; |
| 1933 | |
| 1934 | return true; |
| 1935 | } |
| 1936 | |
Johnny Chen | 60299ec | 2011-02-17 19:34:27 +0000 | [diff] [blame] | 1937 | // Table Branch Byte causes a PC-relative forward branch using a table of single byte offsets. |
| 1938 | // A base register provides a pointer to the table, and a second register supplies an index into the table. |
| 1939 | // The branch length is twice the value of the byte returned from the table. |
| 1940 | // |
| 1941 | // Table Branch Halfword causes a PC-relative forward branch using a table of single halfword offsets. |
| 1942 | // A base register provides a pointer to the table, and a second register supplies an index into the table. |
| 1943 | // The branch length is twice the value of the halfword returned from the table. |
| 1944 | // TBB, TBH |
| 1945 | bool |
| 1946 | EmulateInstructionARM::EmulateTB (ARMEncoding encoding) |
| 1947 | { |
| 1948 | #if 0 |
| 1949 | // ARM pseudo code... |
| 1950 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 1951 | if is_tbh then |
| 1952 | halfwords = UInt(MemU[R[n]+LSL(R[m],1), 2]); |
| 1953 | else |
| 1954 | halfwords = UInt(MemU[R[n]+R[m], 1]); |
| 1955 | BranchWritePC(PC + 2*halfwords); |
| 1956 | #endif |
| 1957 | |
| 1958 | bool success = false; |
| 1959 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 1960 | if (!success) |
| 1961 | return false; |
| 1962 | |
| 1963 | uint32_t Rn; // the base register which contains the address of the table of branch lengths |
| 1964 | uint32_t Rm; // the index register which contains an integer pointing to a byte/halfword in the table |
| 1965 | bool is_tbh; // true if table branch halfword |
| 1966 | switch (encoding) { |
| 1967 | case eEncodingT1: |
| 1968 | Rn = Bits32(opcode, 19, 16); |
| 1969 | Rm = Bits32(opcode, 3, 0); |
| 1970 | is_tbh = BitIsSet(opcode, 4); |
| 1971 | if (Rn == 13 || BadReg(Rm)) |
| 1972 | return false; |
| 1973 | if (InITBlock() && !LastInITBlock()) |
| 1974 | return false; |
| 1975 | break; |
| 1976 | default: |
| 1977 | return false; |
| 1978 | } |
| 1979 | |
| 1980 | // Read the address of the table from the operand register Rn. |
| 1981 | // 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] | 1982 | uint32_t base = ReadCoreReg(Rm, &success); |
Johnny Chen | 60299ec | 2011-02-17 19:34:27 +0000 | [diff] [blame] | 1983 | if (!success) |
| 1984 | return false; |
| 1985 | |
| 1986 | // the table index |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 1987 | uint32_t index = ReadCoreReg(Rm, &success); |
Johnny Chen | 60299ec | 2011-02-17 19:34:27 +0000 | [diff] [blame] | 1988 | if (!success) |
| 1989 | return false; |
| 1990 | |
| 1991 | // the offsetted table address |
| 1992 | addr_t addr = base + (is_tbh ? index*2 : index); |
| 1993 | |
| 1994 | // PC-relative offset to branch forward |
| 1995 | EmulateInstruction::Context context; |
| 1996 | context.type = EmulateInstruction::eContextTableBranchReadMemory; |
Johnny Chen | 104c8b6 | 2011-02-17 23:27:44 +0000 | [diff] [blame] | 1997 | 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] | 1998 | if (!success) |
| 1999 | return false; |
| 2000 | |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2001 | const uint32_t pc = ReadCoreReg(PC_REG, &success); |
Johnny Chen | 60299ec | 2011-02-17 19:34:27 +0000 | [diff] [blame] | 2002 | if (!success) |
| 2003 | return false; |
| 2004 | |
| 2005 | // target address |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2006 | addr_t target = pc + offset; |
Johnny Chen | 60299ec | 2011-02-17 19:34:27 +0000 | [diff] [blame] | 2007 | context.type = EmulateInstruction::eContextRelativeBranchImmediate; |
| 2008 | context.SetModeAndImmediateSigned (eModeThumb, 4 + offset); |
| 2009 | |
| 2010 | if (!BranchWritePC(context, target)) |
| 2011 | return false; |
| 2012 | |
| 2013 | return true; |
| 2014 | } |
| 2015 | |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 2016 | // This instruction adds an immediate value to a register value, and writes the result to the destination |
| 2017 | // register. It can optionally update the condition flags based on the result. |
| 2018 | bool |
| 2019 | EmulateInstructionARM::EmulateADDImmARM (ARMEncoding encoding) |
| 2020 | { |
| 2021 | #if 0 |
| 2022 | // ARM pseudo code... |
| 2023 | if ConditionPassed() then |
| 2024 | EncodingSpecificOperations(); |
| 2025 | (result, carry, overflow) = AddWithCarry(R[n], imm32, '0'); |
| 2026 | if d == 15 then |
| 2027 | ALUWritePC(result); // setflags is always FALSE here |
| 2028 | else |
| 2029 | R[d] = result; |
| 2030 | if setflags then |
| 2031 | APSR.N = result<31>; |
| 2032 | APSR.Z = IsZeroBit(result); |
| 2033 | APSR.C = carry; |
| 2034 | APSR.V = overflow; |
| 2035 | #endif |
| 2036 | |
| 2037 | bool success = false; |
| 2038 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2039 | if (!success) |
| 2040 | return false; |
| 2041 | |
| 2042 | if (ConditionPassed()) |
| 2043 | { |
| 2044 | uint32_t Rd, Rn; |
| 2045 | uint32_t imm32; // the immediate value to be added to the value obtained from Rn |
| 2046 | bool setflags; |
| 2047 | switch (encoding) |
| 2048 | { |
| 2049 | case eEncodingA1: |
| 2050 | Rd = Bits32(opcode, 15, 12); |
| 2051 | Rn = Bits32(opcode, 19, 16); |
| 2052 | setflags = BitIsSet(opcode, 20); |
| 2053 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 2054 | break; |
| 2055 | default: |
| 2056 | return false; |
| 2057 | } |
| 2058 | |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 2059 | // Read the first operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 2060 | uint32_t val1 = ReadCoreReg(Rn, &success); |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 2061 | if (!success) |
| 2062 | return false; |
| 2063 | |
| 2064 | AddWithCarryResult res = AddWithCarry(val1, imm32, 0); |
| 2065 | |
| 2066 | EmulateInstruction::Context context; |
| 2067 | context.type = EmulateInstruction::eContextImmediate; |
| 2068 | context.SetNoArgs (); |
| 2069 | |
| 2070 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 2071 | return false; |
| 2072 | } |
| 2073 | return true; |
| 2074 | } |
| 2075 | |
Johnny Chen | d761dcf | 2011-02-17 22:03:29 +0000 | [diff] [blame] | 2076 | // This instruction adds a register value and an optionally-shifted register value, and writes the result |
| 2077 | // 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] | 2078 | bool |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 2079 | EmulateInstructionARM::EmulateADDReg (ARMEncoding encoding) |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2080 | { |
| 2081 | #if 0 |
| 2082 | // ARM pseudo code... |
| 2083 | if ConditionPassed() then |
| 2084 | EncodingSpecificOperations(); |
| 2085 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 2086 | (result, carry, overflow) = AddWithCarry(R[n], shifted, '0'); |
| 2087 | if d == 15 then |
| 2088 | ALUWritePC(result); // setflags is always FALSE here |
| 2089 | else |
| 2090 | R[d] = result; |
| 2091 | if setflags then |
| 2092 | APSR.N = result<31>; |
| 2093 | APSR.Z = IsZeroBit(result); |
| 2094 | APSR.C = carry; |
| 2095 | APSR.V = overflow; |
| 2096 | #endif |
| 2097 | |
| 2098 | bool success = false; |
| 2099 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2100 | if (!success) |
| 2101 | return false; |
| 2102 | |
| 2103 | if (ConditionPassed()) |
| 2104 | { |
| 2105 | uint32_t Rd, Rn, Rm; |
Johnny Chen | d761dcf | 2011-02-17 22:03:29 +0000 | [diff] [blame] | 2106 | ARM_ShifterType shift_t; |
| 2107 | 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] | 2108 | bool setflags; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2109 | switch (encoding) |
| 2110 | { |
Johnny Chen | d761dcf | 2011-02-17 22:03:29 +0000 | [diff] [blame] | 2111 | case eEncodingT1: |
| 2112 | Rd = Bits32(opcode, 2, 0); |
| 2113 | Rn = Bits32(opcode, 5, 3); |
| 2114 | Rm = Bits32(opcode, 8, 6); |
| 2115 | setflags = !InITBlock(); |
| 2116 | shift_t = SRType_LSL; |
| 2117 | shift_n = 0; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 2118 | break; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2119 | case eEncodingT2: |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 2120 | Rd = Rn = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 0); |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2121 | Rm = Bits32(opcode, 6, 3); |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 2122 | setflags = false; |
Johnny Chen | d761dcf | 2011-02-17 22:03:29 +0000 | [diff] [blame] | 2123 | shift_t = SRType_LSL; |
| 2124 | shift_n = 0; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2125 | if (Rn == 15 && Rm == 15) |
| 2126 | return false; |
Johnny Chen | d761dcf | 2011-02-17 22:03:29 +0000 | [diff] [blame] | 2127 | if (Rd == 15 && InITBlock() && !LastInITBlock()) |
| 2128 | return false; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2129 | break; |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 2130 | case eEncodingA1: |
| 2131 | Rd = Bits32(opcode, 15, 12); |
| 2132 | Rn = Bits32(opcode, 19, 16); |
| 2133 | Rm = Bits32(opcode, 3, 0); |
| 2134 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 2135 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 2136 | break; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2137 | default: |
| 2138 | return false; |
| 2139 | } |
| 2140 | |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2141 | // Read the first operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 2142 | uint32_t val1 = ReadCoreReg(Rn, &success); |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2143 | if (!success) |
| 2144 | return false; |
| 2145 | |
| 2146 | // Read the second operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 2147 | uint32_t val2 = ReadCoreReg(Rm, &success); |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2148 | if (!success) |
| 2149 | return false; |
| 2150 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 2151 | uint32_t shifted = Shift(val2, shift_t, shift_n, APSR_C); |
Johnny Chen | 8fa2059 | 2011-02-18 01:22:22 +0000 | [diff] [blame] | 2152 | AddWithCarryResult res = AddWithCarry(val1, shifted, 0); |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2153 | |
| 2154 | EmulateInstruction::Context context; |
| 2155 | context.type = EmulateInstruction::eContextImmediate; |
| 2156 | context.SetNoArgs (); |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 2157 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2158 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 2159 | return false; |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 2160 | } |
| 2161 | return true; |
| 2162 | } |
| 2163 | |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2164 | // Compare Negative (immediate) adds a register value and an immediate value. |
| 2165 | // 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] | 2166 | bool |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2167 | EmulateInstructionARM::EmulateCMNImm (ARMEncoding encoding) |
| 2168 | { |
| 2169 | #if 0 |
| 2170 | // ARM pseudo code... |
| 2171 | if ConditionPassed() then |
| 2172 | EncodingSpecificOperations(); |
| 2173 | (result, carry, overflow) = AddWithCarry(R[n], imm32, '0'); |
| 2174 | APSR.N = result<31>; |
| 2175 | APSR.Z = IsZeroBit(result); |
| 2176 | APSR.C = carry; |
| 2177 | APSR.V = overflow; |
| 2178 | #endif |
| 2179 | |
| 2180 | bool success = false; |
| 2181 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2182 | if (!success) |
| 2183 | return false; |
| 2184 | |
| 2185 | uint32_t Rn; // the first operand |
| 2186 | uint32_t imm32; // the immediate value to be compared with |
| 2187 | switch (encoding) { |
| 2188 | case eEncodingT1: |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 2189 | Rn = Bits32(opcode, 19, 16); |
| 2190 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 2191 | if (Rn == 15) |
| 2192 | return false; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 2193 | break; |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2194 | case eEncodingA1: |
| 2195 | Rn = Bits32(opcode, 19, 16); |
| 2196 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 2197 | break; |
| 2198 | default: |
| 2199 | return false; |
| 2200 | } |
| 2201 | // Read the register value from the operand register Rn. |
| 2202 | uint32_t reg_val = ReadCoreReg(Rn, &success); |
| 2203 | if (!success) |
| 2204 | return false; |
| 2205 | |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 2206 | AddWithCarryResult res = AddWithCarry(reg_val, imm32, 0); |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2207 | |
| 2208 | EmulateInstruction::Context context; |
| 2209 | context.type = EmulateInstruction::eContextImmediate; |
| 2210 | context.SetNoArgs (); |
| 2211 | if (!WriteFlags(context, res.result, res.carry_out, res.overflow)) |
| 2212 | return false; |
| 2213 | |
| 2214 | return true; |
| 2215 | } |
| 2216 | |
| 2217 | // Compare Negative (register) adds a register value and an optionally-shifted register value. |
| 2218 | // It updates the condition flags based on the result, and discards the result. |
| 2219 | bool |
| 2220 | EmulateInstructionARM::EmulateCMNReg (ARMEncoding encoding) |
| 2221 | { |
| 2222 | #if 0 |
| 2223 | // ARM pseudo code... |
| 2224 | if ConditionPassed() then |
| 2225 | EncodingSpecificOperations(); |
| 2226 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 2227 | (result, carry, overflow) = AddWithCarry(R[n], shifted, '0'); |
| 2228 | APSR.N = result<31>; |
| 2229 | APSR.Z = IsZeroBit(result); |
| 2230 | APSR.C = carry; |
| 2231 | APSR.V = overflow; |
| 2232 | #endif |
| 2233 | |
| 2234 | bool success = false; |
| 2235 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2236 | if (!success) |
| 2237 | return false; |
| 2238 | |
| 2239 | uint32_t Rn; // the first operand |
| 2240 | uint32_t Rm; // the second operand |
| 2241 | ARM_ShifterType shift_t; |
| 2242 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 2243 | switch (encoding) { |
| 2244 | case eEncodingT1: |
| 2245 | Rn = Bits32(opcode, 2, 0); |
| 2246 | Rm = Bits32(opcode, 5, 3); |
| 2247 | shift_t = SRType_LSL; |
| 2248 | shift_n = 0; |
| 2249 | break; |
| 2250 | case eEncodingT2: |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 2251 | Rn = Bits32(opcode, 19, 16); |
| 2252 | Rm = Bits32(opcode, 3, 0); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 2253 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 2254 | // if n == 15 || BadReg(m) then UNPREDICTABLE; |
| 2255 | if (Rn == 15 || BadReg(Rm)) |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2256 | return false; |
| 2257 | break; |
| 2258 | case eEncodingA1: |
| 2259 | Rn = Bits32(opcode, 19, 16); |
| 2260 | Rm = Bits32(opcode, 3, 0); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 2261 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 2262 | break; |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2263 | default: |
| 2264 | return false; |
| 2265 | } |
| 2266 | // Read the register value from register Rn. |
| 2267 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 2268 | if (!success) |
| 2269 | return false; |
| 2270 | |
| 2271 | // Read the register value from register Rm. |
| 2272 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 2273 | if (!success) |
| 2274 | return false; |
| 2275 | |
| 2276 | uint32_t shifted = Shift(val2, shift_t, shift_n, APSR_C); |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 2277 | AddWithCarryResult res = AddWithCarry(val1, shifted, 0); |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2278 | |
| 2279 | EmulateInstruction::Context context; |
| 2280 | context.type = EmulateInstruction::eContextImmediate; |
| 2281 | context.SetNoArgs(); |
| 2282 | if (!WriteFlags(context, res.result, res.carry_out, res.overflow)) |
| 2283 | return false; |
| 2284 | |
| 2285 | return true; |
| 2286 | } |
| 2287 | |
| 2288 | // Compare (immediate) subtracts an immediate value from a register value. |
| 2289 | // It updates the condition flags based on the result, and discards the result. |
| 2290 | bool |
| 2291 | EmulateInstructionARM::EmulateCMPImm (ARMEncoding encoding) |
Johnny Chen | d4dc444 | 2011-02-11 02:02:56 +0000 | [diff] [blame] | 2292 | { |
| 2293 | #if 0 |
| 2294 | // ARM pseudo code... |
| 2295 | if ConditionPassed() then |
| 2296 | EncodingSpecificOperations(); |
| 2297 | (result, carry, overflow) = AddWithCarry(R[n], NOT(imm32), '1'); |
| 2298 | APSR.N = result<31>; |
| 2299 | APSR.Z = IsZeroBit(result); |
| 2300 | APSR.C = carry; |
| 2301 | APSR.V = overflow; |
| 2302 | #endif |
| 2303 | |
| 2304 | bool success = false; |
| 2305 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2306 | if (!success) |
| 2307 | return false; |
| 2308 | |
| 2309 | uint32_t Rn; // the first operand |
| 2310 | uint32_t imm32; // the immediate value to be compared with |
| 2311 | switch (encoding) { |
| 2312 | case eEncodingT1: |
| 2313 | Rn = Bits32(opcode, 10, 8); |
| 2314 | imm32 = Bits32(opcode, 7, 0); |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 2315 | break; |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 2316 | case eEncodingT2: |
| 2317 | Rn = Bits32(opcode, 19, 16); |
| 2318 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 2319 | if (Rn == 15) |
| 2320 | return false; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 2321 | break; |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2322 | case eEncodingA1: |
| 2323 | Rn = Bits32(opcode, 19, 16); |
| 2324 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
Johnny Chen | d4dc444 | 2011-02-11 02:02:56 +0000 | [diff] [blame] | 2325 | break; |
| 2326 | default: |
| 2327 | return false; |
| 2328 | } |
| 2329 | // Read the register value from the operand register Rn. |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2330 | uint32_t reg_val = ReadCoreReg(Rn, &success); |
Johnny Chen | d4dc444 | 2011-02-11 02:02:56 +0000 | [diff] [blame] | 2331 | if (!success) |
| 2332 | return false; |
| 2333 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2334 | AddWithCarryResult res = AddWithCarry(reg_val, ~imm32, 1); |
| 2335 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2336 | EmulateInstruction::Context context; |
| 2337 | context.type = EmulateInstruction::eContextImmediate; |
| 2338 | context.SetNoArgs (); |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2339 | if (!WriteFlags(context, res.result, res.carry_out, res.overflow)) |
| 2340 | return false; |
| 2341 | |
Johnny Chen | d4dc444 | 2011-02-11 02:02:56 +0000 | [diff] [blame] | 2342 | return true; |
| 2343 | } |
| 2344 | |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2345 | // Compare (register) subtracts an optionally-shifted register value from a register value. |
| 2346 | // 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] | 2347 | bool |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2348 | EmulateInstructionARM::EmulateCMPReg (ARMEncoding encoding) |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2349 | { |
| 2350 | #if 0 |
| 2351 | // ARM pseudo code... |
| 2352 | if ConditionPassed() then |
| 2353 | EncodingSpecificOperations(); |
| 2354 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 2355 | (result, carry, overflow) = AddWithCarry(R[n], NOT(shifted), '1'); |
| 2356 | APSR.N = result<31>; |
| 2357 | APSR.Z = IsZeroBit(result); |
| 2358 | APSR.C = carry; |
| 2359 | APSR.V = overflow; |
| 2360 | #endif |
| 2361 | |
| 2362 | bool success = false; |
| 2363 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2364 | if (!success) |
| 2365 | return false; |
| 2366 | |
| 2367 | uint32_t Rn; // the first operand |
| 2368 | uint32_t Rm; // the second operand |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2369 | ARM_ShifterType shift_t; |
| 2370 | 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] | 2371 | switch (encoding) { |
| 2372 | case eEncodingT1: |
| 2373 | Rn = Bits32(opcode, 2, 0); |
| 2374 | Rm = Bits32(opcode, 5, 3); |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2375 | shift_t = SRType_LSL; |
| 2376 | shift_n = 0; |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2377 | break; |
| 2378 | case eEncodingT2: |
| 2379 | Rn = Bit32(opcode, 7) << 3 | Bits32(opcode, 2, 0); |
| 2380 | Rm = Bits32(opcode, 6, 3); |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2381 | shift_t = SRType_LSL; |
| 2382 | shift_n = 0; |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2383 | if (Rn < 8 && Rm < 8) |
| 2384 | return false; |
| 2385 | if (Rn == 15 || Rm == 15) |
| 2386 | return false; |
| 2387 | break; |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2388 | case eEncodingA1: |
| 2389 | Rn = Bits32(opcode, 19, 16); |
| 2390 | Rm = Bits32(opcode, 3, 0); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 2391 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 2392 | break; |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2393 | default: |
| 2394 | return false; |
| 2395 | } |
| 2396 | // Read the register value from register Rn. |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2397 | uint32_t val1 = ReadCoreReg(Rn, &success); |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2398 | if (!success) |
| 2399 | return false; |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2400 | |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2401 | // Read the register value from register Rm. |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2402 | uint32_t val2 = ReadCoreReg(Rm, &success); |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2403 | if (!success) |
| 2404 | return false; |
| 2405 | |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 2406 | uint32_t shifted = Shift(val2, shift_t, shift_n, APSR_C); |
| 2407 | AddWithCarryResult res = AddWithCarry(val1, ~shifted, 1); |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2408 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2409 | EmulateInstruction::Context context; |
| 2410 | context.type = EmulateInstruction::eContextImmediate; |
| 2411 | context.SetNoArgs(); |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2412 | if (!WriteFlags(context, res.result, res.carry_out, res.overflow)) |
| 2413 | return false; |
| 2414 | |
Johnny Chen | e4a4d30 | 2011-02-11 21:53:58 +0000 | [diff] [blame] | 2415 | return true; |
| 2416 | } |
| 2417 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2418 | // Arithmetic Shift Right (immediate) shifts a register value right by an immediate number of bits, |
| 2419 | // shifting in copies of its sign bit, and writes the result to the destination register. It can |
| 2420 | // optionally update the condition flags based on the result. |
| 2421 | bool |
| 2422 | EmulateInstructionARM::EmulateASRImm (ARMEncoding encoding) |
| 2423 | { |
| 2424 | #if 0 |
| 2425 | // ARM pseudo code... |
| 2426 | if ConditionPassed() then |
| 2427 | EncodingSpecificOperations(); |
| 2428 | (result, carry) = Shift_C(R[m], SRType_ASR, shift_n, APSR.C); |
| 2429 | if d == 15 then // Can only occur for ARM encoding |
| 2430 | ALUWritePC(result); // setflags is always FALSE here |
| 2431 | else |
| 2432 | R[d] = result; |
| 2433 | if setflags then |
| 2434 | APSR.N = result<31>; |
| 2435 | APSR.Z = IsZeroBit(result); |
| 2436 | APSR.C = carry; |
| 2437 | // APSR.V unchanged |
| 2438 | #endif |
| 2439 | |
Johnny Chen | 41a0a15 | 2011-02-16 01:27:54 +0000 | [diff] [blame] | 2440 | return EmulateShiftImm(encoding, SRType_ASR); |
| 2441 | } |
| 2442 | |
| 2443 | // Arithmetic Shift Right (register) shifts a register value right by a variable number of bits, |
| 2444 | // shifting in copies of its sign bit, and writes the result to the destination register. |
| 2445 | // The variable number of bits is read from the bottom byte of a register. It can optionally update |
| 2446 | // the condition flags based on the result. |
| 2447 | bool |
| 2448 | EmulateInstructionARM::EmulateASRReg (ARMEncoding encoding) |
| 2449 | { |
| 2450 | #if 0 |
| 2451 | // ARM pseudo code... |
| 2452 | if ConditionPassed() then |
| 2453 | EncodingSpecificOperations(); |
| 2454 | shift_n = UInt(R[m]<7:0>); |
| 2455 | (result, carry) = Shift_C(R[m], SRType_ASR, shift_n, APSR.C); |
| 2456 | R[d] = result; |
| 2457 | if setflags then |
| 2458 | APSR.N = result<31>; |
| 2459 | APSR.Z = IsZeroBit(result); |
| 2460 | APSR.C = carry; |
| 2461 | // APSR.V unchanged |
| 2462 | #endif |
| 2463 | |
| 2464 | return EmulateShiftReg(encoding, SRType_ASR); |
| 2465 | } |
| 2466 | |
| 2467 | // Logical Shift Left (immediate) shifts a register value left by an immediate number of bits, |
| 2468 | // shifting in zeros, and writes the result to the destination register. It can optionally |
| 2469 | // update the condition flags based on the result. |
| 2470 | bool |
| 2471 | EmulateInstructionARM::EmulateLSLImm (ARMEncoding encoding) |
| 2472 | { |
| 2473 | #if 0 |
| 2474 | // ARM pseudo code... |
| 2475 | if ConditionPassed() then |
| 2476 | EncodingSpecificOperations(); |
| 2477 | (result, carry) = Shift_C(R[m], SRType_LSL, shift_n, APSR.C); |
| 2478 | if d == 15 then // Can only occur for ARM encoding |
| 2479 | ALUWritePC(result); // setflags is always FALSE here |
| 2480 | else |
| 2481 | R[d] = result; |
| 2482 | if setflags then |
| 2483 | APSR.N = result<31>; |
| 2484 | APSR.Z = IsZeroBit(result); |
| 2485 | APSR.C = carry; |
| 2486 | // APSR.V unchanged |
| 2487 | #endif |
| 2488 | |
| 2489 | return EmulateShiftImm(encoding, SRType_LSL); |
| 2490 | } |
| 2491 | |
| 2492 | // Logical Shift Left (register) shifts a register value left by a variable number of bits, |
| 2493 | // shifting in zeros, and writes the result to the destination register. The variable number |
| 2494 | // of bits is read from the bottom byte of a register. It can optionally update the condition |
| 2495 | // flags based on the result. |
| 2496 | bool |
| 2497 | EmulateInstructionARM::EmulateLSLReg (ARMEncoding encoding) |
| 2498 | { |
| 2499 | #if 0 |
| 2500 | // ARM pseudo code... |
| 2501 | if ConditionPassed() then |
| 2502 | EncodingSpecificOperations(); |
| 2503 | shift_n = UInt(R[m]<7:0>); |
| 2504 | (result, carry) = Shift_C(R[m], SRType_LSL, shift_n, APSR.C); |
| 2505 | R[d] = result; |
| 2506 | if setflags then |
| 2507 | APSR.N = result<31>; |
| 2508 | APSR.Z = IsZeroBit(result); |
| 2509 | APSR.C = carry; |
| 2510 | // APSR.V unchanged |
| 2511 | #endif |
| 2512 | |
| 2513 | return EmulateShiftReg(encoding, SRType_LSL); |
| 2514 | } |
| 2515 | |
| 2516 | // Logical Shift Right (immediate) shifts a register value right by an immediate number of bits, |
| 2517 | // shifting in zeros, and writes the result to the destination register. It can optionally |
| 2518 | // update the condition flags based on the result. |
| 2519 | bool |
| 2520 | EmulateInstructionARM::EmulateLSRImm (ARMEncoding encoding) |
| 2521 | { |
| 2522 | #if 0 |
| 2523 | // ARM pseudo code... |
| 2524 | if ConditionPassed() then |
| 2525 | EncodingSpecificOperations(); |
| 2526 | (result, carry) = Shift_C(R[m], SRType_LSR, shift_n, APSR.C); |
| 2527 | if d == 15 then // Can only occur for ARM encoding |
| 2528 | ALUWritePC(result); // setflags is always FALSE here |
| 2529 | else |
| 2530 | R[d] = result; |
| 2531 | if setflags then |
| 2532 | APSR.N = result<31>; |
| 2533 | APSR.Z = IsZeroBit(result); |
| 2534 | APSR.C = carry; |
| 2535 | // APSR.V unchanged |
| 2536 | #endif |
| 2537 | |
| 2538 | return EmulateShiftImm(encoding, SRType_LSR); |
| 2539 | } |
| 2540 | |
| 2541 | // Logical Shift Right (register) shifts a register value right by a variable number of bits, |
| 2542 | // shifting in zeros, and writes the result to the destination register. The variable number |
| 2543 | // of bits is read from the bottom byte of a register. It can optionally update the condition |
| 2544 | // flags based on the result. |
| 2545 | bool |
| 2546 | EmulateInstructionARM::EmulateLSRReg (ARMEncoding encoding) |
| 2547 | { |
| 2548 | #if 0 |
| 2549 | // ARM pseudo code... |
| 2550 | if ConditionPassed() then |
| 2551 | EncodingSpecificOperations(); |
| 2552 | shift_n = UInt(R[m]<7:0>); |
| 2553 | (result, carry) = Shift_C(R[m], SRType_LSR, shift_n, APSR.C); |
| 2554 | R[d] = result; |
| 2555 | if setflags then |
| 2556 | APSR.N = result<31>; |
| 2557 | APSR.Z = IsZeroBit(result); |
| 2558 | APSR.C = carry; |
| 2559 | // APSR.V unchanged |
| 2560 | #endif |
| 2561 | |
| 2562 | return EmulateShiftReg(encoding, SRType_LSR); |
| 2563 | } |
| 2564 | |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 2565 | // Rotate Right (immediate) provides the value of the contents of a register rotated by a constant value. |
| 2566 | // The bits that are rotated off the right end are inserted into the vacated bit positions on the left. |
| 2567 | // It can optionally update the condition flags based on the result. |
| 2568 | bool |
| 2569 | EmulateInstructionARM::EmulateRORImm (ARMEncoding encoding) |
| 2570 | { |
| 2571 | #if 0 |
| 2572 | // ARM pseudo code... |
| 2573 | if ConditionPassed() then |
| 2574 | EncodingSpecificOperations(); |
| 2575 | (result, carry) = Shift_C(R[m], SRType_ROR, shift_n, APSR.C); |
| 2576 | if d == 15 then // Can only occur for ARM encoding |
| 2577 | ALUWritePC(result); // setflags is always FALSE here |
| 2578 | else |
| 2579 | R[d] = result; |
| 2580 | if setflags then |
| 2581 | APSR.N = result<31>; |
| 2582 | APSR.Z = IsZeroBit(result); |
| 2583 | APSR.C = carry; |
| 2584 | // APSR.V unchanged |
| 2585 | #endif |
| 2586 | |
| 2587 | return EmulateShiftImm(encoding, SRType_ROR); |
| 2588 | } |
| 2589 | |
| 2590 | // Rotate Right (register) provides the value of the contents of a register rotated by a variable number of bits. |
| 2591 | // The bits that are rotated off the right end are inserted into the vacated bit positions on the left. |
| 2592 | // The variable number of bits is read from the bottom byte of a register. It can optionally update the condition |
| 2593 | // flags based on the result. |
| 2594 | bool |
| 2595 | EmulateInstructionARM::EmulateRORReg (ARMEncoding encoding) |
| 2596 | { |
| 2597 | #if 0 |
| 2598 | // ARM pseudo code... |
| 2599 | if ConditionPassed() then |
| 2600 | EncodingSpecificOperations(); |
| 2601 | shift_n = UInt(R[m]<7:0>); |
| 2602 | (result, carry) = Shift_C(R[m], SRType_ROR, shift_n, APSR.C); |
| 2603 | R[d] = result; |
| 2604 | if setflags then |
| 2605 | APSR.N = result<31>; |
| 2606 | APSR.Z = IsZeroBit(result); |
| 2607 | APSR.C = carry; |
| 2608 | // APSR.V unchanged |
| 2609 | #endif |
| 2610 | |
| 2611 | return EmulateShiftReg(encoding, SRType_ROR); |
| 2612 | } |
| 2613 | |
| 2614 | // Rotate Right with Extend provides the value of the contents of a register shifted right by one place, |
| 2615 | // with the carry flag shifted into bit [31]. |
| 2616 | // |
| 2617 | // RRX can optionally update the condition flags based on the result. |
| 2618 | // In that case, bit [0] is shifted into the carry flag. |
| 2619 | bool |
| 2620 | EmulateInstructionARM::EmulateRRX (ARMEncoding encoding) |
| 2621 | { |
| 2622 | #if 0 |
| 2623 | // ARM pseudo code... |
| 2624 | if ConditionPassed() then |
| 2625 | EncodingSpecificOperations(); |
| 2626 | (result, carry) = Shift_C(R[m], SRType_RRX, 1, APSR.C); |
| 2627 | if d == 15 then // Can only occur for ARM encoding |
| 2628 | ALUWritePC(result); // setflags is always FALSE here |
| 2629 | else |
| 2630 | R[d] = result; |
| 2631 | if setflags then |
| 2632 | APSR.N = result<31>; |
| 2633 | APSR.Z = IsZeroBit(result); |
| 2634 | APSR.C = carry; |
| 2635 | // APSR.V unchanged |
| 2636 | #endif |
| 2637 | |
| 2638 | return EmulateShiftImm(encoding, SRType_RRX); |
| 2639 | } |
| 2640 | |
Johnny Chen | 41a0a15 | 2011-02-16 01:27:54 +0000 | [diff] [blame] | 2641 | bool |
| 2642 | EmulateInstructionARM::EmulateShiftImm (ARMEncoding encoding, ARM_ShifterType shift_type) |
| 2643 | { |
| 2644 | assert(shift_type == SRType_ASR || shift_type == SRType_LSL || shift_type == SRType_LSR); |
| 2645 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2646 | bool success = false; |
| 2647 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2648 | if (!success) |
| 2649 | return false; |
| 2650 | |
| 2651 | if (ConditionPassed()) |
| 2652 | { |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 2653 | uint32_t Rd; // the destination register |
| 2654 | uint32_t Rm; // the first operand register |
| 2655 | uint32_t imm5; // encoding for the shift amount |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2656 | uint32_t carry; // the carry bit after the shift operation |
| 2657 | bool setflags; |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 2658 | |
| 2659 | // Special case handling! |
| 2660 | // A8.6.139 ROR (immediate) -- Encoding T1 |
| 2661 | if (shift_type == SRType_ROR && encoding == eEncodingT1) |
| 2662 | { |
| 2663 | // Morph the T1 encoding from the ARM Architecture Manual into T2 encoding to |
| 2664 | // have the same decoding of bit fields as the other Thumb2 shift operations. |
| 2665 | encoding = eEncodingT2; |
| 2666 | } |
| 2667 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2668 | switch (encoding) { |
| 2669 | case eEncodingT1: |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 2670 | // Due to the above special case handling! |
| 2671 | assert(shift_type != SRType_ROR); |
| 2672 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2673 | Rd = Bits32(opcode, 2, 0); |
| 2674 | Rm = Bits32(opcode, 5, 3); |
| 2675 | setflags = !InITBlock(); |
| 2676 | imm5 = Bits32(opcode, 10, 6); |
| 2677 | break; |
| 2678 | case eEncodingT2: |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 2679 | // A8.6.141 RRX |
| 2680 | assert(shift_type != SRType_RRX); |
| 2681 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2682 | Rd = Bits32(opcode, 11, 8); |
| 2683 | Rm = Bits32(opcode, 3, 0); |
| 2684 | setflags = BitIsSet(opcode, 20); |
| 2685 | imm5 = Bits32(opcode, 14, 12) << 2 | Bits32(opcode, 7, 6); |
| 2686 | if (BadReg(Rd) || BadReg(Rm)) |
| 2687 | return false; |
| 2688 | break; |
| 2689 | case eEncodingA1: |
| 2690 | Rd = Bits32(opcode, 15, 12); |
| 2691 | Rm = Bits32(opcode, 3, 0); |
| 2692 | setflags = BitIsSet(opcode, 20); |
| 2693 | imm5 = Bits32(opcode, 11, 7); |
| 2694 | break; |
| 2695 | default: |
| 2696 | return false; |
| 2697 | } |
| 2698 | |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 2699 | // A8.6.139 ROR (immediate) |
| 2700 | if (shift_type == SRType_ROR && imm5 == 0) |
| 2701 | shift_type = SRType_RRX; |
| 2702 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2703 | // Get the first operand. |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2704 | uint32_t value = ReadCoreReg (Rm, &success); |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2705 | if (!success) |
| 2706 | return false; |
| 2707 | |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 2708 | // Decode the shift amount if not RRX. |
| 2709 | uint32_t amt = (shift_type == SRType_RRX ? 1 : DecodeImmShift(shift_type, imm5)); |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2710 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 2711 | uint32_t result = Shift_C(value, shift_type, amt, APSR_C, carry); |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2712 | |
| 2713 | // The context specifies that an immediate is to be moved into Rd. |
| 2714 | EmulateInstruction::Context context; |
| 2715 | context.type = EmulateInstruction::eContextImmediate; |
| 2716 | context.SetNoArgs (); |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2717 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2718 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 2719 | return false; |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 2720 | } |
| 2721 | return true; |
| 2722 | } |
| 2723 | |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 2724 | bool |
Johnny Chen | 41a0a15 | 2011-02-16 01:27:54 +0000 | [diff] [blame] | 2725 | EmulateInstructionARM::EmulateShiftReg (ARMEncoding encoding, ARM_ShifterType shift_type) |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 2726 | { |
Johnny Chen | 41a0a15 | 2011-02-16 01:27:54 +0000 | [diff] [blame] | 2727 | 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] | 2728 | |
| 2729 | bool success = false; |
| 2730 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2731 | if (!success) |
| 2732 | return false; |
| 2733 | |
| 2734 | if (ConditionPassed()) |
| 2735 | { |
| 2736 | uint32_t Rd; // the destination register |
| 2737 | uint32_t Rn; // the first operand register |
| 2738 | uint32_t Rm; // the register whose bottom byte contains the amount to shift by |
| 2739 | uint32_t carry; // the carry bit after the shift operation |
| 2740 | bool setflags; |
| 2741 | switch (encoding) { |
| 2742 | case eEncodingT1: |
| 2743 | Rd = Bits32(opcode, 2, 0); |
| 2744 | Rn = Rd; |
| 2745 | Rm = Bits32(opcode, 5, 3); |
| 2746 | setflags = !InITBlock(); |
| 2747 | break; |
| 2748 | case eEncodingT2: |
| 2749 | Rd = Bits32(opcode, 11, 8); |
| 2750 | Rn = Bits32(opcode, 19, 16); |
| 2751 | Rm = Bits32(opcode, 3, 0); |
| 2752 | setflags = BitIsSet(opcode, 20); |
| 2753 | if (BadReg(Rd) || BadReg(Rn) || BadReg(Rm)) |
| 2754 | return false; |
| 2755 | break; |
| 2756 | case eEncodingA1: |
| 2757 | Rd = Bits32(opcode, 15, 12); |
| 2758 | Rn = Bits32(opcode, 3, 0); |
| 2759 | Rm = Bits32(opcode, 11, 8); |
| 2760 | setflags = BitIsSet(opcode, 20); |
| 2761 | if (Rd == 15 || Rn == 15 || Rm == 15) |
| 2762 | return false; |
| 2763 | break; |
| 2764 | default: |
| 2765 | return false; |
| 2766 | } |
| 2767 | |
| 2768 | // Get the first operand. |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2769 | uint32_t value = ReadCoreReg (Rn, &success); |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 2770 | if (!success) |
| 2771 | return false; |
| 2772 | // Get the Rm register content. |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 2773 | uint32_t val = ReadCoreReg (Rm, &success); |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 2774 | if (!success) |
| 2775 | return false; |
| 2776 | |
| 2777 | // Get the shift amount. |
| 2778 | uint32_t amt = Bits32(val, 7, 0); |
| 2779 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 2780 | uint32_t result = Shift_C(value, shift_type, amt, APSR_C, carry); |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 2781 | |
| 2782 | // The context specifies that an immediate is to be moved into Rd. |
| 2783 | EmulateInstruction::Context context; |
| 2784 | context.type = EmulateInstruction::eContextImmediate; |
| 2785 | context.SetNoArgs (); |
| 2786 | |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 2787 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 2788 | return false; |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 2789 | } |
| 2790 | return true; |
| 2791 | } |
| 2792 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2793 | // LDM loads multiple registers from consecutive memory locations, using an |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2794 | // 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] | 2795 | // can be written back to the base register. |
| 2796 | bool |
| 2797 | EmulateInstructionARM::EmulateLDM (ARMEncoding encoding) |
| 2798 | { |
| 2799 | #if 0 |
| 2800 | // ARM pseudo code... |
| 2801 | if ConditionPassed() |
| 2802 | EncodingSpecificOperations(); NullCheckIfThumbEE (n); |
| 2803 | address = R[n]; |
| 2804 | |
| 2805 | for i = 0 to 14 |
| 2806 | if registers<i> == '1' then |
| 2807 | R[i] = MemA[address, 4]; address = address + 4; |
| 2808 | if registers<15> == '1' then |
| 2809 | LoadWritePC (MemA[address, 4]); |
| 2810 | |
| 2811 | if wback && registers<n> == '0' then R[n] = R[n] + 4 * BitCount (registers); |
| 2812 | if wback && registers<n> == '1' then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 2813 | |
| 2814 | #endif |
| 2815 | |
| 2816 | bool success = false; |
| 2817 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2818 | if (!success) |
| 2819 | return false; |
| 2820 | |
| 2821 | if (ConditionPassed()) |
| 2822 | { |
| 2823 | uint32_t n; |
| 2824 | uint32_t registers = 0; |
| 2825 | bool wback; |
| 2826 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2827 | switch (encoding) |
| 2828 | { |
| 2829 | case eEncodingT1: |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2830 | // n = UInt(Rn); registers = ’00000000’:register_list; wback = (registers<n> == ’0’); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2831 | n = Bits32 (opcode, 10, 8); |
| 2832 | registers = Bits32 (opcode, 7, 0); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2833 | registers = registers & 0x00ff; // Make sure the top 8 bits are zeros. |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2834 | wback = BitIsClear (registers, n); |
| 2835 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 2836 | if (BitCount(registers) < 1) |
| 2837 | return false; |
| 2838 | break; |
| 2839 | case eEncodingT2: |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2840 | // if W == ’1’ && Rn == ’1101’ then SEE POP; |
| 2841 | // n = UInt(Rn); registers = P:M:’0’:register_list; wback = (W == ’1’); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2842 | n = Bits32 (opcode, 19, 16); |
| 2843 | registers = Bits32 (opcode, 15, 0); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2844 | registers = registers & 0xdfff; // Make sure bit 13 is zero. |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2845 | wback = BitIsSet (opcode, 21); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2846 | |
| 2847 | // if n == 15 || BitCount(registers) < 2 || (P == ’1’ && M == ’1’) then UNPREDICTABLE; |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2848 | if ((n == 15) |
| 2849 | || (BitCount (registers) < 2) |
| 2850 | || (BitIsSet (opcode, 14) && BitIsSet (opcode, 15))) |
| 2851 | return false; |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2852 | |
| 2853 | // if registers<15> == ’1’ && InITBlock() && !LastInITBlock() then UNPREDICTABLE; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 2854 | if (BitIsSet (registers, 15) && InITBlock() && !LastInITBlock()) |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2855 | return false; |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2856 | |
| 2857 | // if wback && registers<n> == ’1’ then UNPREDICTABLE; |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2858 | if (wback |
| 2859 | && BitIsSet (registers, n)) |
| 2860 | return false; |
| 2861 | break; |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 2862 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2863 | case eEncodingA1: |
| 2864 | n = Bits32 (opcode, 19, 16); |
| 2865 | registers = Bits32 (opcode, 15, 0); |
| 2866 | wback = BitIsSet (opcode, 21); |
| 2867 | if ((n == 15) |
| 2868 | || (BitCount (registers) < 1)) |
| 2869 | return false; |
| 2870 | break; |
| 2871 | default: |
| 2872 | return false; |
| 2873 | } |
| 2874 | |
| 2875 | int32_t offset = 0; |
| 2876 | const addr_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 2877 | if (!success) |
| 2878 | return false; |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2879 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2880 | EmulateInstruction::Context context; |
| 2881 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 2882 | Register dwarf_reg; |
| 2883 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 2884 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2885 | |
| 2886 | for (int i = 0; i < 14; ++i) |
| 2887 | { |
| 2888 | if (BitIsSet (registers, i)) |
| 2889 | { |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2890 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2891 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2892 | if (wback && (n == 13)) // Pop Instruction |
| 2893 | context.type = EmulateInstruction::eContextPopRegisterOffStack; |
| 2894 | |
| 2895 | // R[i] = MemA [address, 4]; address = address + 4; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 2896 | 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] | 2897 | if (!success) |
| 2898 | return false; |
| 2899 | |
| 2900 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 2901 | return false; |
| 2902 | |
| 2903 | offset += addr_byte_size; |
| 2904 | } |
| 2905 | } |
| 2906 | |
| 2907 | if (BitIsSet (registers, 15)) |
| 2908 | { |
| 2909 | //LoadWritePC (MemA [address, 4]); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 2910 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2911 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 2912 | 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] | 2913 | if (!success) |
| 2914 | return false; |
Johnny Chen | e62b50d | 2011-02-09 22:02:17 +0000 | [diff] [blame] | 2915 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 2916 | if (!LoadWritePC(context, data)) |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2917 | return false; |
| 2918 | } |
| 2919 | |
| 2920 | if (wback && BitIsClear (registers, n)) |
| 2921 | { |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 2922 | // R[n] = R[n] + 4 * BitCount (registers) |
| 2923 | int32_t offset = addr_byte_size * BitCount (registers); |
| 2924 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 2925 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2926 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2927 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, base_address + offset)) |
| 2928 | return false; |
| 2929 | } |
| 2930 | if (wback && BitIsSet (registers, n)) |
| 2931 | // R[n] bits(32) UNKNOWN; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2932 | return WriteBits32Unknown (n); |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2933 | } |
| 2934 | return true; |
| 2935 | } |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2936 | |
| 2937 | // LDMDA loads multiple registers from consecutive memory locations using an address from a base registers. |
| 2938 | // The consecutive memorty locations end at this address and the address just below the lowest of those locations |
| 2939 | // can optionally be written back tot he base registers. |
| 2940 | bool |
| 2941 | EmulateInstructionARM::EmulateLDMDA (ARMEncoding encoding) |
| 2942 | { |
| 2943 | #if 0 |
| 2944 | // ARM pseudo code... |
| 2945 | if ConditionPassed() then |
| 2946 | EncodingSpecificOperations(); |
| 2947 | address = R[n] - 4*BitCount(registers) + 4; |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 2948 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 2949 | for i = 0 to 14 |
| 2950 | if registers<i> == ’1’ then |
| 2951 | R[i] = MemA[address,4]; address = address + 4; |
| 2952 | |
| 2953 | if registers<15> == ’1’ then |
| 2954 | LoadWritePC(MemA[address,4]); |
| 2955 | |
| 2956 | if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 2957 | if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; |
| 2958 | #endif |
| 2959 | |
| 2960 | bool success = false; |
| 2961 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 2962 | if (!success) |
| 2963 | return false; |
| 2964 | |
| 2965 | if (ConditionPassed()) |
| 2966 | { |
| 2967 | uint32_t n; |
| 2968 | uint32_t registers = 0; |
| 2969 | bool wback; |
| 2970 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2971 | |
| 2972 | // EncodingSpecificOperations(); |
| 2973 | switch (encoding) |
| 2974 | { |
| 2975 | case eEncodingA1: |
| 2976 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 2977 | n = Bits32 (opcode, 19, 16); |
| 2978 | registers = Bits32 (opcode, 15, 0); |
| 2979 | wback = BitIsSet (opcode, 21); |
| 2980 | |
| 2981 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 2982 | if ((n == 15) || (BitCount (registers) < 1)) |
| 2983 | return false; |
| 2984 | |
| 2985 | break; |
| 2986 | |
| 2987 | default: |
| 2988 | return false; |
| 2989 | } |
| 2990 | // address = R[n] - 4*BitCount(registers) + 4; |
| 2991 | |
| 2992 | int32_t offset = 0; |
| 2993 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 2994 | |
| 2995 | if (!success) |
| 2996 | return false; |
| 2997 | |
| 2998 | address = address - (addr_byte_size * BitCount (registers)) + addr_byte_size; |
| 2999 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3000 | EmulateInstruction::Context context; |
| 3001 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 3002 | Register dwarf_reg; |
| 3003 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3004 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3005 | |
| 3006 | // for i = 0 to 14 |
| 3007 | for (int i = 0; i < 14; ++i) |
| 3008 | { |
| 3009 | // if registers<i> == ’1’ then |
| 3010 | if (BitIsSet (registers, i)) |
| 3011 | { |
| 3012 | // R[i] = MemA[address,4]; address = address + 4; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3013 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3014 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3015 | if (!success) |
| 3016 | return false; |
| 3017 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 3018 | return false; |
| 3019 | offset += addr_byte_size; |
| 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | // if registers<15> == ’1’ then |
| 3024 | // LoadWritePC(MemA[address,4]); |
| 3025 | if (BitIsSet (registers, 15)) |
| 3026 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3027 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3028 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3029 | if (!success) |
| 3030 | return false; |
Johnny Chen | 44c10f0 | 2011-02-11 19:37:03 +0000 | [diff] [blame] | 3031 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 3032 | if (!LoadWritePC(context, data)) |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3033 | return false; |
| 3034 | } |
| 3035 | |
| 3036 | // if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 3037 | if (wback && BitIsClear (registers, n)) |
| 3038 | { |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3039 | addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3040 | if (!success) |
| 3041 | return false; |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3042 | |
| 3043 | offset = (addr_byte_size * BitCount (registers)) * -1; |
| 3044 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3045 | context.SetImmediateSigned (offset); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3046 | addr = addr + offset; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3047 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr)) |
| 3048 | return false; |
| 3049 | } |
| 3050 | |
| 3051 | // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; |
| 3052 | if (wback && BitIsSet (registers, n)) |
| 3053 | return WriteBits32Unknown (n); |
| 3054 | } |
| 3055 | return true; |
| 3056 | } |
| 3057 | |
| 3058 | // LDMDB loads multiple registers from consecutive memory locations using an address from a base register. The |
| 3059 | // consecutive memory lcoations end just below this address, and the address of the lowest of those locations can |
| 3060 | // be optionally written back to the base register. |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3061 | bool |
| 3062 | EmulateInstructionARM::EmulateLDMDB (ARMEncoding encoding) |
| 3063 | { |
| 3064 | #if 0 |
| 3065 | // ARM pseudo code... |
| 3066 | if ConditionPassed() then |
| 3067 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 3068 | address = R[n] - 4*BitCount(registers); |
| 3069 | |
| 3070 | for i = 0 to 14 |
| 3071 | if registers<i> == ’1’ then |
| 3072 | R[i] = MemA[address,4]; address = address + 4; |
| 3073 | if registers<15> == ’1’ then |
| 3074 | LoadWritePC(MemA[address,4]); |
| 3075 | |
| 3076 | if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 3077 | if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 3078 | #endif |
| 3079 | |
| 3080 | bool success = false; |
| 3081 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3082 | if (!success) |
| 3083 | return false; |
| 3084 | |
| 3085 | if (ConditionPassed()) |
| 3086 | { |
| 3087 | uint32_t n; |
| 3088 | uint32_t registers = 0; |
| 3089 | bool wback; |
| 3090 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3091 | switch (encoding) |
| 3092 | { |
| 3093 | case eEncodingT1: |
| 3094 | // n = UInt(Rn); registers = P:M:’0’:register_list; wback = (W == ’1’); |
| 3095 | n = Bits32 (opcode, 19, 16); |
| 3096 | registers = Bits32 (opcode, 15, 0); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3097 | registers = registers & 0xdfff; // Make sure bit 13 is a zero. |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3098 | wback = BitIsSet (opcode, 21); |
| 3099 | |
| 3100 | // if n == 15 || BitCount(registers) < 2 || (P == ’1’ && M == ’1’) then UNPREDICTABLE; |
| 3101 | if ((n == 15) |
| 3102 | || (BitCount (registers) < 2) |
| 3103 | || (BitIsSet (opcode, 14) && BitIsSet (opcode, 15))) |
| 3104 | return false; |
| 3105 | |
| 3106 | // if registers<15> == ’1’ && InITBlock() && !LastInITBlock() then UNPREDICTABLE; |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 3107 | if (BitIsSet (registers, 15) && InITBlock() && !LastInITBlock()) |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3108 | return false; |
| 3109 | |
| 3110 | // if wback && registers<n> == ’1’ then UNPREDICTABLE; |
| 3111 | if (wback && BitIsSet (registers, n)) |
| 3112 | return false; |
| 3113 | |
| 3114 | break; |
| 3115 | |
| 3116 | case eEncodingA1: |
| 3117 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3118 | n = Bits32 (opcode, 19, 16); |
| 3119 | registers = Bits32 (opcode, 15, 0); |
| 3120 | wback = BitIsSet (opcode, 21); |
| 3121 | |
| 3122 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3123 | if ((n == 15) || (BitCount (registers) < 1)) |
| 3124 | return false; |
| 3125 | |
| 3126 | break; |
| 3127 | |
| 3128 | default: |
| 3129 | return false; |
| 3130 | } |
| 3131 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3132 | // address = R[n] - 4*BitCount(registers); |
| 3133 | |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3134 | int32_t offset = 0; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3135 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3136 | |
| 3137 | if (!success) |
| 3138 | return false; |
| 3139 | |
| 3140 | address = address - (addr_byte_size * BitCount (registers)); |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3141 | EmulateInstruction::Context context; |
| 3142 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 3143 | Register dwarf_reg; |
| 3144 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3145 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3146 | |
| 3147 | for (int i = 0; i < 14; ++i) |
| 3148 | { |
| 3149 | if (BitIsSet (registers, i)) |
| 3150 | { |
| 3151 | // R[i] = MemA[address,4]; address = address + 4; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3152 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3153 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3154 | if (!success) |
| 3155 | return false; |
| 3156 | |
| 3157 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 3158 | return false; |
| 3159 | |
| 3160 | offset += addr_byte_size; |
| 3161 | } |
| 3162 | } |
| 3163 | |
| 3164 | // if registers<15> == ’1’ then |
| 3165 | // LoadWritePC(MemA[address,4]); |
| 3166 | if (BitIsSet (registers, 15)) |
| 3167 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3168 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3169 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3170 | if (!success) |
| 3171 | return false; |
Johnny Chen | e62b50d | 2011-02-09 22:02:17 +0000 | [diff] [blame] | 3172 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 3173 | if (!LoadWritePC(context, data)) |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3174 | return false; |
| 3175 | } |
| 3176 | |
| 3177 | // if wback && registers<n> == ’0’ then R[n] = R[n] - 4*BitCount(registers); |
| 3178 | if (wback && BitIsClear (registers, n)) |
| 3179 | { |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3180 | addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3181 | if (!success) |
| 3182 | return false; |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3183 | |
| 3184 | offset = (addr_byte_size * BitCount (registers)) * -1; |
| 3185 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3186 | context.SetImmediateSigned (offset); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3187 | addr = addr + offset; |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3188 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr)) |
| 3189 | return false; |
| 3190 | } |
| 3191 | |
| 3192 | // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 3193 | if (wback && BitIsSet (registers, n)) |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3194 | return WriteBits32Unknown (n); |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3195 | } |
| 3196 | return true; |
| 3197 | } |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3198 | |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3199 | // LDMIB loads multiple registers from consecutive memory locations using an address from a base register. The |
| 3200 | // consecutive memory locations start just above this address, and thea ddress of the last of those locations can |
| 3201 | // optinoally be written back to the base register. |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3202 | bool |
| 3203 | EmulateInstructionARM::EmulateLDMIB (ARMEncoding encoding) |
| 3204 | { |
| 3205 | #if 0 |
| 3206 | if ConditionPassed() then |
| 3207 | EncodingSpecificOperations(); |
| 3208 | address = R[n] + 4; |
| 3209 | |
| 3210 | for i = 0 to 14 |
| 3211 | if registers<i> == ’1’ then |
| 3212 | R[i] = MemA[address,4]; address = address + 4; |
| 3213 | if registers<15> == ’1’ then |
| 3214 | LoadWritePC(MemA[address,4]); |
| 3215 | |
| 3216 | if wback && registers<n> == ’0’ then R[n] = R[n] + 4*BitCount(registers); |
| 3217 | if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; |
| 3218 | #endif |
| 3219 | |
| 3220 | bool success = false; |
| 3221 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3222 | if (!success) |
| 3223 | return false; |
| 3224 | |
| 3225 | if (ConditionPassed()) |
| 3226 | { |
| 3227 | uint32_t n; |
| 3228 | uint32_t registers = 0; |
| 3229 | bool wback; |
| 3230 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3231 | switch (encoding) |
| 3232 | { |
| 3233 | case eEncodingA1: |
| 3234 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3235 | n = Bits32 (opcode, 19, 16); |
| 3236 | registers = Bits32 (opcode, 15, 0); |
| 3237 | wback = BitIsSet (opcode, 21); |
| 3238 | |
| 3239 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3240 | if ((n == 15) || (BitCount (registers) < 1)) |
| 3241 | return false; |
| 3242 | |
| 3243 | break; |
| 3244 | default: |
| 3245 | return false; |
| 3246 | } |
| 3247 | // address = R[n] + 4; |
| 3248 | |
| 3249 | int32_t offset = 0; |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3250 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3251 | |
| 3252 | if (!success) |
| 3253 | return false; |
| 3254 | |
| 3255 | address = address + addr_byte_size; |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3256 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3257 | EmulateInstruction::Context context; |
| 3258 | context.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 3259 | Register dwarf_reg; |
| 3260 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3261 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3262 | |
| 3263 | for (int i = 0; i < 14; ++i) |
| 3264 | { |
| 3265 | if (BitIsSet (registers, i)) |
| 3266 | { |
| 3267 | // R[i] = MemA[address,4]; address = address + 4; |
| 3268 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3269 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3270 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3271 | if (!success) |
| 3272 | return false; |
| 3273 | |
| 3274 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + i, data)) |
| 3275 | return false; |
| 3276 | |
| 3277 | offset += addr_byte_size; |
| 3278 | } |
| 3279 | } |
| 3280 | |
| 3281 | // if registers<15> == ’1’ then |
| 3282 | // LoadWritePC(MemA[address,4]); |
| 3283 | if (BitIsSet (registers, 15)) |
| 3284 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3285 | context.SetRegisterPlusOffset (dwarf_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3286 | uint32_t data = MemARead (context, address + offset, addr_byte_size, 0, &success); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3287 | if (!success) |
| 3288 | return false; |
Johnny Chen | e62b50d | 2011-02-09 22:02:17 +0000 | [diff] [blame] | 3289 | // In ARMv5T and above, this is an interworking branch. |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 3290 | if (!LoadWritePC(context, data)) |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3291 | return false; |
| 3292 | } |
| 3293 | |
| 3294 | // if wback && registers<n> == ’0’ then R[n] = R[n] + 4*BitCount(registers); |
| 3295 | if (wback && BitIsClear (registers, n)) |
| 3296 | { |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3297 | addr_t addr = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3298 | if (!success) |
| 3299 | return false; |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3300 | |
| 3301 | offset = addr_byte_size * BitCount (registers); |
| 3302 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3303 | context.SetImmediateSigned (offset); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3304 | addr = addr + offset; |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3305 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, addr)) |
| 3306 | return false; |
| 3307 | } |
| 3308 | |
| 3309 | // if wback && registers<n> == ’1’ then R[n] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 3310 | if (wback && BitIsSet (registers, n)) |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 3311 | return WriteBits32Unknown (n); |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 3312 | } |
| 3313 | return true; |
| 3314 | } |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 3315 | |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3316 | // Load Register (immediate) calculates an address from a base register value and |
| 3317 | // an immediate offset, loads a word from memory, and writes to a register. |
| 3318 | // LDR (immediate, Thumb) |
| 3319 | bool |
| 3320 | EmulateInstructionARM::EmulateLDRRtRnImm (ARMEncoding encoding) |
| 3321 | { |
| 3322 | #if 0 |
| 3323 | // ARM pseudo code... |
| 3324 | if (ConditionPassed()) |
| 3325 | { |
| 3326 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 3327 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 3328 | address = if index then offset_addr else R[n]; |
| 3329 | data = MemU[address,4]; |
| 3330 | if wback then R[n] = offset_addr; |
| 3331 | if t == 15 then |
| 3332 | if address<1:0> == '00' then LoadWritePC(data); else UNPREDICTABLE; |
| 3333 | elsif UnalignedSupport() || address<1:0> = '00' then |
| 3334 | R[t] = data; |
| 3335 | else R[t] = bits(32) UNKNOWN; // Can only apply before ARMv7 |
| 3336 | } |
| 3337 | #endif |
| 3338 | |
| 3339 | bool success = false; |
| 3340 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3341 | if (!success) |
| 3342 | return false; |
| 3343 | |
| 3344 | if (ConditionPassed()) |
| 3345 | { |
| 3346 | uint32_t Rt; // the destination register |
| 3347 | uint32_t Rn; // the base register |
| 3348 | uint32_t imm32; // the immediate offset used to form the address |
| 3349 | addr_t offset_addr; // the offset address |
| 3350 | addr_t address; // the calculated address |
| 3351 | uint32_t data; // the literal data value from memory load |
| 3352 | bool add, index, wback; |
| 3353 | switch (encoding) { |
| 3354 | case eEncodingT1: |
| 3355 | Rt = Bits32(opcode, 5, 3); |
| 3356 | Rn = Bits32(opcode, 2, 0); |
| 3357 | imm32 = Bits32(opcode, 10, 6) << 2; // imm32 = ZeroExtend(imm5:'00', 32); |
| 3358 | // index = TRUE; add = TRUE; wback = FALSE |
| 3359 | add = true; |
| 3360 | index = true; |
| 3361 | wback = false; |
| 3362 | break; |
| 3363 | default: |
| 3364 | return false; |
| 3365 | } |
| 3366 | uint32_t base = ReadRegisterUnsigned(eRegisterKindDWARF, dwarf_r0 + Rn, 0, &success); |
| 3367 | if (!success) |
| 3368 | return false; |
| 3369 | if (add) |
| 3370 | offset_addr = base + imm32; |
| 3371 | else |
| 3372 | offset_addr = base - imm32; |
| 3373 | |
| 3374 | address = (index ? offset_addr : base); |
| 3375 | |
| 3376 | if (wback) |
| 3377 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3378 | EmulateInstruction::Context ctx; |
| 3379 | ctx.type = EmulateInstruction::eContextRegisterPlusOffset; |
| 3380 | Register dwarf_reg; |
| 3381 | dwarf_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + Rn); |
| 3382 | ctx.SetRegisterPlusOffset (dwarf_reg, (int32_t) (offset_addr - base)); |
| 3383 | |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3384 | if (!WriteRegisterUnsigned (ctx, eRegisterKindDWARF, dwarf_r0 + Rn, offset_addr)) |
| 3385 | return false; |
| 3386 | } |
| 3387 | |
| 3388 | // Prepare to write to the Rt register. |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3389 | EmulateInstruction::Context context; |
| 3390 | context.type = EmulateInstruction::eContextImmediate; |
| 3391 | context.SetNoArgs (); |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3392 | |
| 3393 | // Read memory from the address. |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3394 | data = MemURead(context, address, 4, 0, &success); |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3395 | if (!success) |
| 3396 | return false; |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3397 | |
| 3398 | if (Rt == 15) |
| 3399 | { |
| 3400 | if (Bits32(address, 1, 0) == 0) |
| 3401 | { |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 3402 | if (!LoadWritePC(context, data)) |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 3403 | return false; |
| 3404 | } |
| 3405 | else |
| 3406 | return false; |
| 3407 | } |
| 3408 | else if (UnalignedSupport() || Bits32(address, 1, 0) == 0) |
| 3409 | { |
| 3410 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rt, data)) |
| 3411 | return false; |
| 3412 | } |
| 3413 | else |
| 3414 | return false; |
| 3415 | } |
| 3416 | return true; |
| 3417 | } |
| 3418 | |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3419 | // STM (Store Multiple Increment After) stores multiple registers to consecutive memory locations using an address |
| 3420 | // from a base register. The consecutive memory locations start at this address, and teh address just above the last |
| 3421 | // of those locations can optionally be written back to the base register. |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3422 | bool |
| 3423 | EmulateInstructionARM::EmulateSTM (ARMEncoding encoding) |
| 3424 | { |
| 3425 | #if 0 |
| 3426 | if ConditionPassed() then |
| 3427 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 3428 | address = R[n]; |
| 3429 | |
| 3430 | for i = 0 to 14 |
| 3431 | if registers<i> == ’1’ then |
| 3432 | if i == n && wback && i != LowestSetBit(registers) then |
| 3433 | MemA[address,4] = bits(32) UNKNOWN; // Only possible for encodings T1 and A1 |
| 3434 | else |
| 3435 | MemA[address,4] = R[i]; |
| 3436 | address = address + 4; |
| 3437 | |
| 3438 | if registers<15> == ’1’ then // Only possible for encoding A1 |
| 3439 | MemA[address,4] = PCStoreValue(); |
| 3440 | if wback then R[n] = R[n] + 4*BitCount(registers); |
| 3441 | #endif |
| 3442 | |
| 3443 | bool success = false; |
| 3444 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3445 | if (!success) |
| 3446 | return false; |
| 3447 | |
| 3448 | if (ConditionPassed ()) |
| 3449 | { |
| 3450 | uint32_t n; |
| 3451 | uint32_t registers = 0; |
| 3452 | bool wback; |
| 3453 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3454 | |
| 3455 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 3456 | switch (encoding) |
| 3457 | { |
| 3458 | case eEncodingT1: |
| 3459 | // n = UInt(Rn); registers = ’00000000’:register_list; wback = TRUE; |
| 3460 | n = Bits32 (opcode, 10, 8); |
| 3461 | registers = Bits32 (opcode, 7, 0); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3462 | registers = registers & 0x00ff; // Make sure the top 8 bits are zeros. |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3463 | wback = true; |
| 3464 | |
| 3465 | // if BitCount(registers) < 1 then UNPREDICTABLE; |
| 3466 | if (BitCount (registers) < 1) |
| 3467 | return false; |
| 3468 | |
| 3469 | break; |
| 3470 | |
| 3471 | case eEncodingT2: |
| 3472 | // n = UInt(Rn); registers = ’0’:M:’0’:register_list; wback = (W == ’1’); |
| 3473 | n = Bits32 (opcode, 19, 16); |
| 3474 | registers = Bits32 (opcode, 15, 0); |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3475 | registers = registers & 0x5fff; // Make sure bits 15 & 13 are zeros. |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3476 | wback = BitIsSet (opcode, 21); |
| 3477 | |
| 3478 | // if n == 15 || BitCount(registers) < 2 then UNPREDICTABLE; |
| 3479 | if ((n == 15) || (BitCount (registers) < 2)) |
| 3480 | return false; |
| 3481 | |
| 3482 | // if wback && registers<n> == ’1’ then UNPREDICTABLE; |
| 3483 | if (wback && BitIsSet (registers, n)) |
| 3484 | return false; |
| 3485 | |
| 3486 | break; |
| 3487 | |
| 3488 | case eEncodingA1: |
| 3489 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3490 | n = Bits32 (opcode, 19, 16); |
| 3491 | registers = Bits32 (opcode, 15, 0); |
| 3492 | wback = BitIsSet (opcode, 21); |
| 3493 | |
| 3494 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3495 | if ((n == 15) || (BitCount (registers) < 1)) |
| 3496 | return false; |
| 3497 | |
| 3498 | break; |
| 3499 | |
| 3500 | default: |
| 3501 | return false; |
| 3502 | } |
| 3503 | |
| 3504 | // address = R[n]; |
| 3505 | int32_t offset = 0; |
| 3506 | const addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3507 | if (!success) |
| 3508 | return false; |
| 3509 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3510 | EmulateInstruction::Context context; |
| 3511 | context.type = EmulateInstruction::eContextRegisterStore; |
| 3512 | Register base_reg; |
| 3513 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3514 | |
| 3515 | // for i = 0 to 14 |
| 3516 | for (int i = 0; i < 14; ++i) |
| 3517 | { |
| 3518 | int lowest_set_bit = 14; |
| 3519 | // if registers<i> == ’1’ then |
| 3520 | if (BitIsSet (registers, i)) |
| 3521 | { |
| 3522 | if (i < lowest_set_bit) |
| 3523 | lowest_set_bit = i; |
| 3524 | // if i == n && wback && i != LowestSetBit(registers) then |
| 3525 | if ((i == n) && wback && (i != lowest_set_bit)) |
| 3526 | // MemA[address,4] = bits(32) UNKNOWN; // Only possible for encodings T1 and A1 |
| 3527 | WriteBits32UnknownToMemory (address + offset); |
| 3528 | else |
| 3529 | { |
| 3530 | // MemA[address,4] = R[i]; |
| 3531 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success); |
| 3532 | if (!success) |
| 3533 | return false; |
| 3534 | |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3535 | Register data_reg; |
| 3536 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i); |
| 3537 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3538 | if (!MemAWrite (context, address + offset, data, addr_byte_size)) |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3539 | return false; |
| 3540 | } |
| 3541 | |
| 3542 | // address = address + 4; |
| 3543 | offset += addr_byte_size; |
| 3544 | } |
| 3545 | } |
| 3546 | |
| 3547 | // if registers<15> == ’1’ then // Only possible for encoding A1 |
| 3548 | // MemA[address,4] = PCStoreValue(); |
| 3549 | if (BitIsSet (registers, 15)) |
| 3550 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3551 | Register pc_reg; |
| 3552 | pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc); |
| 3553 | context.SetRegisterPlusOffset (pc_reg, 8); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3554 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 3555 | if (!success) |
| 3556 | return false; |
| 3557 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3558 | if (!MemAWrite (context, address + offset, pc + 8, addr_byte_size)) |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3559 | return false; |
| 3560 | } |
| 3561 | |
| 3562 | // if wback then R[n] = R[n] + 4*BitCount(registers); |
| 3563 | if (wback) |
| 3564 | { |
| 3565 | offset = addr_byte_size * BitCount (registers); |
| 3566 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 3567 | context.SetImmediateSigned (offset); |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 3568 | addr_t data = address + offset; |
| 3569 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 3570 | return false; |
| 3571 | } |
| 3572 | } |
| 3573 | return true; |
| 3574 | } |
| 3575 | |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3576 | // STMDA (Store Multiple Decrement After) stores multiple registers to consecutive memory locations using an address |
| 3577 | // from a base register. The consecutive memory locations end at this address, and the address just below the lowest |
| 3578 | // of those locations can optionally be written back to the base register. |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 3579 | bool |
| 3580 | EmulateInstructionARM::EmulateSTMDA (ARMEncoding encoding) |
| 3581 | { |
| 3582 | #if 0 |
| 3583 | if ConditionPassed() then |
| 3584 | EncodingSpecificOperations(); |
| 3585 | address = R[n] - 4*BitCount(registers) + 4; |
| 3586 | |
| 3587 | for i = 0 to 14 |
| 3588 | if registers<i> == ’1’ then |
| 3589 | if i == n && wback && i != LowestSetBit(registers) then |
| 3590 | MemA[address,4] = bits(32) UNKNOWN; |
| 3591 | else |
| 3592 | MemA[address,4] = R[i]; |
| 3593 | address = address + 4; |
| 3594 | |
| 3595 | if registers<15> == ’1’ then |
| 3596 | MemA[address,4] = PCStoreValue(); |
| 3597 | |
| 3598 | if wback then R[n] = R[n] - 4*BitCount(registers); |
| 3599 | #endif |
| 3600 | |
| 3601 | bool success = false; |
| 3602 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3603 | if (!success) |
| 3604 | return false; |
| 3605 | |
| 3606 | if (ConditionPassed ()) |
| 3607 | { |
| 3608 | uint32_t n; |
| 3609 | uint32_t registers = 0; |
| 3610 | bool wback; |
| 3611 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3612 | |
| 3613 | // EncodingSpecificOperations(); |
| 3614 | switch (encoding) |
| 3615 | { |
| 3616 | case eEncodingA1: |
| 3617 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3618 | n = Bits32 (opcode, 19, 16); |
| 3619 | registers = Bits32 (opcode, 15, 0); |
| 3620 | wback = BitIsSet (opcode, 21); |
| 3621 | |
| 3622 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3623 | if ((n == 15) || (BitCount (registers) < 1)) |
| 3624 | return false; |
| 3625 | break; |
| 3626 | default: |
| 3627 | return false; |
| 3628 | } |
| 3629 | |
| 3630 | // address = R[n] - 4*BitCount(registers) + 4; |
| 3631 | int32_t offset = 0; |
| 3632 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3633 | if (!success) |
| 3634 | return false; |
| 3635 | |
| 3636 | address = address - (addr_byte_size * BitCount (registers)) + 4; |
| 3637 | |
| 3638 | EmulateInstruction::Context context; |
| 3639 | context.type = EmulateInstruction::eContextRegisterStore; |
| 3640 | Register base_reg; |
| 3641 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3642 | |
| 3643 | // for i = 0 to 14 |
| 3644 | for (int i = 0; i < 14; ++i) |
| 3645 | { |
| 3646 | int lowest_bit_set = 14; |
| 3647 | // if registers<i> == ’1’ then |
| 3648 | if (BitIsSet (registers, i)) |
| 3649 | { |
| 3650 | if (i < lowest_bit_set) |
| 3651 | lowest_bit_set = i; |
| 3652 | //if i == n && wback && i != LowestSetBit(registers) then |
| 3653 | if ((i == n) && wback && (i != lowest_bit_set)) |
| 3654 | // MemA[address,4] = bits(32) UNKNOWN; |
| 3655 | WriteBits32UnknownToMemory (address + offset); |
| 3656 | else |
| 3657 | { |
| 3658 | // MemA[address,4] = R[i]; |
| 3659 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success); |
| 3660 | if (!success) |
| 3661 | return false; |
| 3662 | |
| 3663 | Register data_reg; |
| 3664 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i); |
| 3665 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3666 | if (!MemAWrite (context, address + offset, data, addr_byte_size)) |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 3667 | return false; |
| 3668 | } |
| 3669 | |
| 3670 | // address = address + 4; |
| 3671 | offset += addr_byte_size; |
| 3672 | } |
| 3673 | } |
| 3674 | |
| 3675 | // if registers<15> == ’1’ then |
| 3676 | // MemA[address,4] = PCStoreValue(); |
| 3677 | if (BitIsSet (registers, 15)) |
| 3678 | { |
| 3679 | Register pc_reg; |
| 3680 | pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc); |
| 3681 | context.SetRegisterPlusOffset (pc_reg, 8); |
| 3682 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 3683 | if (!success) |
| 3684 | return false; |
| 3685 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3686 | if (!MemAWrite (context, address + offset, pc + 8, addr_byte_size)) |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 3687 | return false; |
| 3688 | } |
| 3689 | |
| 3690 | // if wback then R[n] = R[n] - 4*BitCount(registers); |
| 3691 | if (wback) |
| 3692 | { |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3693 | offset = (addr_byte_size * BitCount (registers)) * -1; |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 3694 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
| 3695 | context.SetImmediateSigned (offset); |
| 3696 | addr_t data = address + offset; |
| 3697 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 3698 | return false; |
| 3699 | } |
| 3700 | } |
| 3701 | return true; |
| 3702 | } |
| 3703 | |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3704 | // STMDB (Store Multiple Decrement Before) stores multiple registers to consecutive memory locations using an address |
| 3705 | // from a base register. The consecutive memory locations end just below this address, and the address of the first of |
| 3706 | // those locations can optionally be written back to the base register. |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3707 | bool |
| 3708 | EmulateInstructionARM::EmulateSTMDB (ARMEncoding encoding) |
| 3709 | { |
| 3710 | #if 0 |
| 3711 | if ConditionPassed() then |
| 3712 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 3713 | address = R[n] - 4*BitCount(registers); |
| 3714 | |
| 3715 | for i = 0 to 14 |
| 3716 | if registers<i> == ’1’ then |
| 3717 | if i == n && wback && i != LowestSetBit(registers) then |
| 3718 | MemA[address,4] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 3719 | else |
| 3720 | MemA[address,4] = R[i]; |
| 3721 | address = address + 4; |
| 3722 | |
| 3723 | if registers<15> == ’1’ then // Only possible for encoding A1 |
| 3724 | MemA[address,4] = PCStoreValue(); |
| 3725 | |
| 3726 | if wback then R[n] = R[n] - 4*BitCount(registers); |
| 3727 | #endif |
| 3728 | |
| 3729 | |
| 3730 | bool success = false; |
| 3731 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3732 | if (!success) |
| 3733 | return false; |
| 3734 | |
| 3735 | if (ConditionPassed ()) |
| 3736 | { |
| 3737 | uint32_t n; |
| 3738 | uint32_t registers = 0; |
| 3739 | bool wback; |
| 3740 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3741 | |
| 3742 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 3743 | switch (encoding) |
| 3744 | { |
| 3745 | case eEncodingT1: |
| 3746 | // if W == ’1’ && Rn == ’1101’ then SEE PUSH; |
| 3747 | if ((BitIsSet (opcode, 21)) && (Bits32 (opcode, 19, 16) == 13)) |
| 3748 | { |
| 3749 | // See PUSH |
| 3750 | } |
| 3751 | // n = UInt(Rn); registers = ’0’:M:’0’:register_list; wback = (W == ’1’); |
| 3752 | n = Bits32 (opcode, 19, 16); |
| 3753 | registers = Bits32 (opcode, 15, 0); |
| 3754 | registers = registers & 0x5fff; // Make sure bits 15 & 13 are zeros. |
| 3755 | wback = BitIsSet (opcode, 21); |
| 3756 | // if n == 15 || BitCount(registers) < 2 then UNPREDICTABLE; |
| 3757 | if ((n == 15) || BitCount (registers) < 2) |
| 3758 | return false; |
| 3759 | // if wback && registers<n> == ’1’ then UNPREDICTABLE; |
| 3760 | if (wback && BitIsSet (registers, n)) |
| 3761 | return false; |
| 3762 | break; |
| 3763 | |
| 3764 | case eEncodingA1: |
| 3765 | // if W == ’1’ && Rn == ’1101’ && BitCount(register_list) >= 2 then SEE PUSH; |
| 3766 | if (BitIsSet (opcode, 21) && (Bits32 (opcode, 19, 16) == 13) && BitCount (Bits32 (opcode, 15, 0)) >= 2) |
| 3767 | { |
| 3768 | // See Push |
| 3769 | } |
| 3770 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3771 | n = Bits32 (opcode, 19, 16); |
| 3772 | registers = Bits32 (opcode, 15, 0); |
| 3773 | wback = BitIsSet (opcode, 21); |
| 3774 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3775 | if ((n == 15) || BitCount (registers) < 1) |
| 3776 | return false; |
| 3777 | break; |
| 3778 | |
| 3779 | default: |
| 3780 | return false; |
| 3781 | } |
| 3782 | |
| 3783 | // address = R[n] - 4*BitCount(registers); |
| 3784 | |
| 3785 | int32_t offset = 0; |
| 3786 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3787 | if (!success) |
| 3788 | return false; |
| 3789 | |
| 3790 | address = address - (addr_byte_size * BitCount (registers)); |
| 3791 | |
| 3792 | EmulateInstruction::Context context; |
| 3793 | context.type = EmulateInstruction::eContextRegisterStore; |
| 3794 | Register base_reg; |
| 3795 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3796 | |
| 3797 | // for i = 0 to 14 |
| 3798 | for (int i = 0; i < 14; ++i) |
| 3799 | { |
| 3800 | uint32_t lowest_set_bit = 14; |
| 3801 | // if registers<i> == ’1’ then |
| 3802 | if (BitIsSet (registers, i)) |
| 3803 | { |
| 3804 | if (i < lowest_set_bit) |
| 3805 | lowest_set_bit = i; |
| 3806 | // if i == n && wback && i != LowestSetBit(registers) then |
| 3807 | if ((i == n) && wback && (i != lowest_set_bit)) |
| 3808 | // MemA[address,4] = bits(32) UNKNOWN; // Only possible for encoding A1 |
| 3809 | WriteBits32UnknownToMemory (address + offset); |
| 3810 | else |
| 3811 | { |
| 3812 | // MemA[address,4] = R[i]; |
| 3813 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success); |
| 3814 | if (!success) |
| 3815 | return false; |
| 3816 | |
| 3817 | Register data_reg; |
| 3818 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i); |
| 3819 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3820 | if (!MemAWrite (context, address + offset, data, addr_byte_size)) |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3821 | return false; |
| 3822 | } |
| 3823 | |
| 3824 | // address = address + 4; |
| 3825 | offset += addr_byte_size; |
| 3826 | } |
| 3827 | } |
| 3828 | |
| 3829 | // if registers<15> == ’1’ then // Only possible for encoding A1 |
| 3830 | // MemA[address,4] = PCStoreValue(); |
| 3831 | if (BitIsSet (registers, 15)) |
| 3832 | { |
| 3833 | Register pc_reg; |
| 3834 | pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc); |
| 3835 | context.SetRegisterPlusOffset (pc_reg, 8); |
| 3836 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 3837 | if (!success) |
| 3838 | return false; |
| 3839 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3840 | if (!MemAWrite (context, address + offset, pc + 8, addr_byte_size)) |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3841 | return false; |
| 3842 | } |
| 3843 | |
| 3844 | // if wback then R[n] = R[n] - 4*BitCount(registers); |
| 3845 | if (wback) |
| 3846 | { |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3847 | offset = (addr_byte_size * BitCount (registers)) * -1; |
| 3848 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
| 3849 | context.SetImmediateSigned (offset); |
| 3850 | addr_t data = address + offset; |
| 3851 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 3852 | return false; |
| 3853 | } |
| 3854 | } |
| 3855 | return true; |
| 3856 | } |
| 3857 | |
| 3858 | // STMIB (Store Multiple Increment Before) stores multiple registers to consecutive memory locations using an address |
| 3859 | // from a base register. The consecutive memory locations start just above this address, and the address of the last |
| 3860 | // of those locations can optionally be written back to the base register. |
| 3861 | bool |
| 3862 | EmulateInstructionARM::EmulateSTMIB (ARMEncoding encoding) |
| 3863 | { |
| 3864 | #if 0 |
| 3865 | if ConditionPassed() then |
| 3866 | EncodingSpecificOperations(); |
| 3867 | address = R[n] + 4; |
| 3868 | |
| 3869 | for i = 0 to 14 |
| 3870 | if registers<i> == ’1’ then |
| 3871 | if i == n && wback && i != LowestSetBit(registers) then |
| 3872 | MemA[address,4] = bits(32) UNKNOWN; |
| 3873 | else |
| 3874 | MemA[address,4] = R[i]; |
| 3875 | address = address + 4; |
| 3876 | |
| 3877 | if registers<15> == ’1’ then |
| 3878 | MemA[address,4] = PCStoreValue(); |
| 3879 | |
| 3880 | if wback then R[n] = R[n] + 4*BitCount(registers); |
| 3881 | #endif |
| 3882 | |
| 3883 | bool success = false; |
| 3884 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 3885 | if (!success) |
| 3886 | return false; |
| 3887 | |
| 3888 | if (ConditionPassed()) |
| 3889 | { |
| 3890 | uint32_t n; |
| 3891 | uint32_t registers = 0; |
| 3892 | bool wback; |
| 3893 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 3894 | |
| 3895 | // EncodingSpecificOperations(); |
| 3896 | switch (encoding) |
| 3897 | { |
| 3898 | case eEncodingA1: |
| 3899 | // n = UInt(Rn); registers = register_list; wback = (W == ’1’); |
| 3900 | n = Bits32 (opcode, 19, 16); |
| 3901 | registers = Bits32 (opcode, 15, 0); |
| 3902 | wback = BitIsSet (opcode, 21); |
| 3903 | |
| 3904 | // if n == 15 || BitCount(registers) < 1 then UNPREDICTABLE; |
| 3905 | if ((n == 15) && (BitCount (registers) < 1)) |
| 3906 | return false; |
| 3907 | break; |
| 3908 | default: |
| 3909 | return false; |
| 3910 | } |
| 3911 | // address = R[n] + 4; |
| 3912 | |
| 3913 | int32_t offset = 0; |
| 3914 | addr_t address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 3915 | if (!success) |
| 3916 | return false; |
| 3917 | |
| 3918 | address = address + addr_byte_size; |
| 3919 | |
| 3920 | EmulateInstruction::Context context; |
| 3921 | context.type = EmulateInstruction::eContextRegisterStore; |
| 3922 | Register base_reg; |
| 3923 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 3924 | |
| 3925 | uint32_t lowest_set_bit = 14; |
| 3926 | // for i = 0 to 14 |
| 3927 | for (int i = 0; i < 14; ++i) |
| 3928 | { |
| 3929 | // if registers<i> == ’1’ then |
| 3930 | if (BitIsSet (registers, i)) |
| 3931 | { |
| 3932 | if (i < lowest_set_bit) |
| 3933 | lowest_set_bit = i; |
| 3934 | // if i == n && wback && i != LowestSetBit(registers) then |
| 3935 | if ((i == n) && wback && (i != lowest_set_bit)) |
| 3936 | // MemA[address,4] = bits(32) UNKNOWN; |
| 3937 | WriteBits32UnknownToMemory (address + offset); |
| 3938 | // else |
| 3939 | else |
| 3940 | { |
| 3941 | // MemA[address,4] = R[i]; |
| 3942 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + i, 0, &success); |
| 3943 | if (!success) |
| 3944 | return false; |
| 3945 | |
| 3946 | Register data_reg; |
| 3947 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + i); |
| 3948 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3949 | if (!MemAWrite (context, address + offset, data, addr_byte_size)) |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3950 | return false; |
| 3951 | } |
| 3952 | |
| 3953 | // address = address + 4; |
| 3954 | offset += addr_byte_size; |
| 3955 | } |
| 3956 | } |
| 3957 | |
| 3958 | // if registers<15> == ’1’ then |
| 3959 | // MemA[address,4] = PCStoreValue(); |
| 3960 | if (BitIsSet (registers, 15)) |
| 3961 | { |
| 3962 | Register pc_reg; |
| 3963 | pc_reg.SetRegister (eRegisterKindDWARF, dwarf_pc); |
| 3964 | context.SetRegisterPlusOffset (pc_reg, 8); |
| 3965 | const uint32_t pc = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 3966 | if (!success) |
| 3967 | return false; |
| 3968 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 3969 | if (!MemAWrite (context, address + offset, pc + 8, addr_byte_size)) |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 3970 | return false; |
| 3971 | } |
| 3972 | |
| 3973 | // if wback then R[n] = R[n] + 4*BitCount(registers); |
| 3974 | if (wback) |
| 3975 | { |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 3976 | offset = addr_byte_size * BitCount (registers); |
| 3977 | context.type = EmulateInstruction::eContextAdjustBaseRegister; |
| 3978 | context.SetImmediateSigned (offset); |
| 3979 | addr_t data = address + offset; |
| 3980 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, data)) |
| 3981 | return false; |
| 3982 | } |
| 3983 | } |
| 3984 | return true; |
| 3985 | } |
Caroline Tice | 7fac857 | 2011-02-15 22:53:54 +0000 | [diff] [blame] | 3986 | |
| 3987 | // STR (store immediate) calcualtes an address from a base register value and an immediate offset, and stores a word |
| 3988 | // from a register to memory. It can use offset, post-indexed, or pre-indexed addressing. |
| 3989 | bool |
| 3990 | EmulateInstructionARM::EmulateSTRThumb (ARMEncoding encoding) |
| 3991 | { |
| 3992 | #if 0 |
| 3993 | if ConditionPassed() then |
| 3994 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 3995 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 3996 | address = if index then offset_addr else R[n]; |
| 3997 | if UnalignedSupport() || address<1:0> == ’00’ then |
| 3998 | MemU[address,4] = R[t]; |
| 3999 | else // Can only occur before ARMv7 |
| 4000 | MemU[address,4] = bits(32) UNKNOWN; |
| 4001 | if wback then R[n] = offset_addr; |
| 4002 | #endif |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 4003 | |
Caroline Tice | 7fac857 | 2011-02-15 22:53:54 +0000 | [diff] [blame] | 4004 | bool success = false; |
| 4005 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4006 | if (!success) |
| 4007 | return false; |
| 4008 | |
| 4009 | if (ConditionPassed()) |
| 4010 | { |
| 4011 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 4012 | |
| 4013 | uint32_t t; |
| 4014 | uint32_t n; |
| 4015 | uint32_t imm32; |
| 4016 | bool index; |
| 4017 | bool add; |
| 4018 | bool wback; |
| 4019 | // EncodingSpecificOperations (); NullCheckIfThumbEE(n); |
| 4020 | switch (encoding) |
| 4021 | { |
| 4022 | case eEncodingT1: |
| 4023 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm5:’00’, 32); |
| 4024 | t = Bits32 (opcode, 2, 0); |
| 4025 | n = Bits32 (opcode, 5, 3); |
| 4026 | imm32 = Bits32 (opcode, 10, 6) << 2; |
| 4027 | |
| 4028 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4029 | index = true; |
| 4030 | add = false; |
| 4031 | wback = false; |
| 4032 | break; |
| 4033 | |
| 4034 | case eEncodingT2: |
| 4035 | // t = UInt(Rt); n = 13; imm32 = ZeroExtend(imm8:’00’, 32); |
| 4036 | t = Bits32 (opcode, 10, 8); |
| 4037 | n = 13; |
| 4038 | imm32 = Bits32 (opcode, 7, 0) << 2; |
| 4039 | |
| 4040 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4041 | index = true; |
| 4042 | add = true; |
| 4043 | wback = false; |
| 4044 | break; |
| 4045 | |
| 4046 | case eEncodingT3: |
| 4047 | // if Rn == ’1111’ then UNDEFINED; |
| 4048 | if (Bits32 (opcode, 19, 16) == 15) |
| 4049 | return false; |
| 4050 | |
| 4051 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32); |
| 4052 | t = Bits32 (opcode, 15, 12); |
| 4053 | n = Bits32 (opcode, 19, 16); |
| 4054 | imm32 = Bits32 (opcode, 11, 0); |
| 4055 | |
| 4056 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4057 | index = true; |
| 4058 | add = true; |
| 4059 | wback = false; |
| 4060 | |
| 4061 | // if t == 15 then UNPREDICTABLE; |
| 4062 | if (t == 15) |
| 4063 | return false; |
| 4064 | break; |
| 4065 | |
| 4066 | case eEncodingT4: |
| 4067 | // if P == ’1’ && U == ’1’ && W == ’0’ then SEE STRT; |
| 4068 | // if Rn == ’1101’ && P == ’1’ && U == ’0’ && W == ’1’ && imm8 == ’00000100’ then SEE PUSH; |
| 4069 | // if Rn == ’1111’ || (P == ’0’ && W == ’0’) then UNDEFINED; |
| 4070 | if ((Bits32 (opcode, 19, 16) == 15) |
| 4071 | || (BitIsClear (opcode, 10) && BitIsClear (opcode, 8))) |
| 4072 | return false; |
| 4073 | |
| 4074 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm8, 32); |
| 4075 | t = Bits32 (opcode, 15, 12); |
| 4076 | n = Bits32 (opcode, 19, 16); |
| 4077 | imm32 = Bits32 (opcode, 7, 0); |
| 4078 | |
| 4079 | // index = (P == ’1’); add = (U == ’1’); wback = (W == ’1’); |
| 4080 | index = BitIsSet (opcode, 10); |
| 4081 | add = BitIsSet (opcode, 9); |
| 4082 | wback = BitIsSet (opcode, 8); |
| 4083 | |
| 4084 | // if t == 15 || (wback && n == t) then UNPREDICTABLE; |
| 4085 | if ((t == 15) || (wback && (n == t))) |
| 4086 | return false; |
| 4087 | break; |
| 4088 | |
| 4089 | default: |
| 4090 | return false; |
| 4091 | } |
| 4092 | |
| 4093 | addr_t offset_addr; |
| 4094 | addr_t address; |
| 4095 | |
| 4096 | // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 4097 | uint32_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 4098 | if (!success) |
| 4099 | return false; |
| 4100 | |
| 4101 | if (add) |
| 4102 | offset_addr = base_address + imm32; |
| 4103 | else |
| 4104 | offset_addr = base_address - imm32; |
| 4105 | |
| 4106 | // address = if index then offset_addr else R[n]; |
| 4107 | if (index) |
| 4108 | address = offset_addr; |
| 4109 | else |
| 4110 | address = base_address; |
| 4111 | |
| 4112 | EmulateInstruction::Context context; |
| 4113 | context.type = eContextRegisterStore; |
| 4114 | Register base_reg; |
| 4115 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 4116 | |
| 4117 | // if UnalignedSupport() || address<1:0> == ’00’ then |
| 4118 | if (UnalignedSupport () || (BitIsClear (address, 1) && BitIsClear (address, 0))) |
| 4119 | { |
| 4120 | // MemU[address,4] = R[t]; |
| 4121 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + t, 0, &success); |
| 4122 | if (!success) |
| 4123 | return false; |
| 4124 | |
| 4125 | Register data_reg; |
| 4126 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + t); |
| 4127 | int32_t offset = address - base_address; |
| 4128 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, offset); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4129 | if (!MemUWrite (context, address, data, addr_byte_size)) |
Caroline Tice | 7fac857 | 2011-02-15 22:53:54 +0000 | [diff] [blame] | 4130 | return false; |
| 4131 | } |
| 4132 | else |
| 4133 | { |
| 4134 | // MemU[address,4] = bits(32) UNKNOWN; |
| 4135 | WriteBits32UnknownToMemory (address); |
| 4136 | } |
| 4137 | |
| 4138 | // if wback then R[n] = offset_addr; |
| 4139 | if (wback) |
| 4140 | { |
| 4141 | context.type = eContextRegisterLoad; |
| 4142 | context.SetAddress (offset_addr); |
| 4143 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 4144 | return false; |
| 4145 | } |
| 4146 | } |
| 4147 | return true; |
| 4148 | } |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 4149 | |
Caroline Tice | 3fd63e9 | 2011-02-16 00:33:43 +0000 | [diff] [blame] | 4150 | // STR (Store Register) calculates an address from a base register value and an offset register value, stores a |
| 4151 | // word from a register to memory. The offset register value can optionally be shifted. |
| 4152 | bool |
| 4153 | EmulateInstructionARM::EmulateSTRRegister (ARMEncoding encoding) |
| 4154 | { |
| 4155 | #if 0 |
| 4156 | if ConditionPassed() then |
| 4157 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 4158 | offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 4159 | offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 4160 | address = if index then offset_addr else R[n]; |
| 4161 | if t == 15 then // Only possible for encoding A1 |
| 4162 | data = PCStoreValue(); |
| 4163 | else |
| 4164 | data = R[t]; |
| 4165 | if UnalignedSupport() || address<1:0> == ’00’ || CurrentInstrSet() == InstrSet_ARM then |
| 4166 | MemU[address,4] = data; |
| 4167 | else // Can only occur before ARMv7 |
| 4168 | MemU[address,4] = bits(32) UNKNOWN; |
| 4169 | if wback then R[n] = offset_addr; |
| 4170 | #endif |
| 4171 | |
| 4172 | bool success = false; |
| 4173 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4174 | if (!success) |
| 4175 | return false; |
| 4176 | |
| 4177 | if (ConditionPassed()) |
| 4178 | { |
| 4179 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 4180 | |
| 4181 | uint32_t t; |
| 4182 | uint32_t n; |
| 4183 | uint32_t m; |
| 4184 | ARM_ShifterType shift_t; |
| 4185 | uint32_t shift_n; |
| 4186 | bool index; |
| 4187 | bool add; |
| 4188 | bool wback; |
| 4189 | |
| 4190 | // EncodingSpecificOperations (); NullCheckIfThumbEE(n); |
| 4191 | switch (encoding) |
| 4192 | { |
| 4193 | case eEncodingT1: |
| 4194 | // if CurrentInstrSet() == InstrSet_ThumbEE then SEE "Modified operation in ThumbEE"; |
| 4195 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 4196 | t = Bits32 (opcode, 2, 0); |
| 4197 | n = Bits32 (opcode, 5, 3); |
| 4198 | m = Bits32 (opcode, 8, 6); |
| 4199 | |
| 4200 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4201 | index = true; |
| 4202 | add = true; |
| 4203 | wback = false; |
| 4204 | |
| 4205 | // (shift_t, shift_n) = (SRType_LSL, 0); |
| 4206 | shift_t = SRType_LSL; |
| 4207 | shift_n = 0; |
| 4208 | break; |
| 4209 | |
| 4210 | case eEncodingT2: |
| 4211 | // if Rn == ’1111’ then UNDEFINED; |
| 4212 | if (Bits32 (opcode, 19, 16) == 15) |
| 4213 | return false; |
| 4214 | |
| 4215 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 4216 | t = Bits32 (opcode, 15, 12); |
| 4217 | n = Bits32 (opcode, 19, 16); |
| 4218 | m = Bits32 (opcode, 3, 0); |
| 4219 | |
| 4220 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4221 | index = true; |
| 4222 | add = true; |
| 4223 | wback = false; |
| 4224 | |
| 4225 | // (shift_t, shift_n) = (SRType_LSL, UInt(imm2)); |
| 4226 | shift_t = SRType_LSL; |
| 4227 | shift_n = Bits32 (opcode, 5, 4); |
| 4228 | |
| 4229 | // if t == 15 || BadReg(m) then UNPREDICTABLE; |
| 4230 | if ((t == 15) || (BadReg (m))) |
| 4231 | return false; |
| 4232 | break; |
| 4233 | |
| 4234 | case eEncodingA1: |
| 4235 | { |
| 4236 | // if P == ’0’ && W == ’1’ then SEE STRT; |
| 4237 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 4238 | t = Bits32 (opcode, 15, 12); |
| 4239 | n = Bits32 (opcode, 19, 16); |
| 4240 | m = Bits32 (opcode, 3, 0); |
| 4241 | |
| 4242 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 4243 | index = BitIsSet (opcode, 24); |
| 4244 | add = BitIsSet (opcode, 23); |
| 4245 | wback = (BitIsClear (opcode, 24) || BitIsSet (opcode, 21)); |
| 4246 | |
| 4247 | // (shift_t, shift_n) = DecodeImmShift(type, imm5); |
| 4248 | uint32_t typ = Bits32 (opcode, 6, 5); |
| 4249 | uint32_t imm5 = Bits32 (opcode, 11, 7); |
| 4250 | shift_n = DecodeImmShift(typ, imm5, shift_t); |
| 4251 | |
| 4252 | // if m == 15 then UNPREDICTABLE; |
| 4253 | if (m == 15) |
| 4254 | return false; |
| 4255 | |
| 4256 | // if wback && (n == 15 || n == t) then UNPREDICTABLE; |
| 4257 | if (wback && ((n == 15) || (n == t))) |
| 4258 | return false; |
| 4259 | |
| 4260 | break; |
| 4261 | } |
| 4262 | default: |
| 4263 | return false; |
| 4264 | } |
| 4265 | |
| 4266 | addr_t offset_addr; |
| 4267 | addr_t address; |
| 4268 | int32_t offset = 0; |
| 4269 | |
| 4270 | addr_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 4271 | if (!success) |
| 4272 | return false; |
| 4273 | |
| 4274 | uint32_t Rm_data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 4275 | if (!success) |
| 4276 | return false; |
| 4277 | |
| 4278 | // offset = Shift(R[m], shift_t, shift_n, APSR.C); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4279 | offset = Shift (Rm_data, shift_t, shift_n, APSR_C); |
Caroline Tice | 3fd63e9 | 2011-02-16 00:33:43 +0000 | [diff] [blame] | 4280 | |
| 4281 | // offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 4282 | if (add) |
| 4283 | offset_addr = base_address + offset; |
| 4284 | else |
| 4285 | offset_addr = base_address - offset; |
| 4286 | |
| 4287 | // address = if index then offset_addr else R[n]; |
| 4288 | if (index) |
| 4289 | address = offset_addr; |
| 4290 | else |
| 4291 | address = base_address; |
| 4292 | |
| 4293 | uint32_t data; |
| 4294 | // if t == 15 then // Only possible for encoding A1 |
| 4295 | if (t == 15) |
| 4296 | // data = PCStoreValue(); |
| 4297 | data = ReadRegisterUnsigned(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 4298 | else |
| 4299 | // data = R[t]; |
| 4300 | data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + t, 0, &success); |
| 4301 | |
| 4302 | if (!success) |
| 4303 | return false; |
| 4304 | |
| 4305 | EmulateInstruction::Context context; |
| 4306 | context.type = eContextRegisterStore; |
| 4307 | |
| 4308 | // if UnalignedSupport() || address<1:0> == ’00’ || CurrentInstrSet() == InstrSet_ARM then |
| 4309 | if (UnalignedSupport () |
| 4310 | || (BitIsClear (address, 1) && BitIsClear (address, 0)) |
| 4311 | || CurrentInstrSet() == eModeARM) |
| 4312 | { |
| 4313 | // MemU[address,4] = data; |
| 4314 | |
| 4315 | Register base_reg; |
| 4316 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 4317 | |
| 4318 | Register data_reg; |
| 4319 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + t); |
| 4320 | |
| 4321 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, address - base_address); |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4322 | if (!MemUWrite (context, address, data, addr_byte_size)) |
Caroline Tice | 3fd63e9 | 2011-02-16 00:33:43 +0000 | [diff] [blame] | 4323 | return false; |
| 4324 | |
| 4325 | } |
| 4326 | else |
| 4327 | // MemU[address,4] = bits(32) UNKNOWN; |
| 4328 | WriteBits32UnknownToMemory (address); |
| 4329 | |
| 4330 | // if wback then R[n] = offset_addr; |
| 4331 | if (wback) |
| 4332 | { |
| 4333 | context.type = eContextRegisterLoad; |
| 4334 | context.SetAddress (offset_addr); |
| 4335 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 4336 | return false; |
| 4337 | } |
| 4338 | |
| 4339 | } |
| 4340 | return true; |
| 4341 | } |
Caroline Tice | 73a29de | 2011-02-16 20:22:22 +0000 | [diff] [blame] | 4342 | |
| 4343 | bool |
| 4344 | EmulateInstructionARM::EmulateSTRBThumb (ARMEncoding encoding) |
| 4345 | { |
| 4346 | #if 0 |
| 4347 | if ConditionPassed() then |
| 4348 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 4349 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 4350 | address = if index then offset_addr else R[n]; |
| 4351 | MemU[address,1] = R[t]<7:0>; |
| 4352 | if wback then R[n] = offset_addr; |
| 4353 | #endif |
| 4354 | |
| 4355 | |
| 4356 | bool success = false; |
| 4357 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4358 | if (!success) |
| 4359 | return false; |
| 4360 | |
| 4361 | if (ConditionPassed ()) |
| 4362 | { |
| 4363 | uint32_t t; |
| 4364 | uint32_t n; |
| 4365 | uint32_t imm32; |
| 4366 | bool index; |
| 4367 | bool add; |
| 4368 | bool wback; |
| 4369 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 4370 | switch (encoding) |
| 4371 | { |
| 4372 | case eEncodingT1: |
| 4373 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm5, 32); |
| 4374 | t = Bits32 (opcode, 2, 0); |
| 4375 | n = Bits32 (opcode, 5, 3); |
| 4376 | imm32 = Bits32 (opcode, 10, 6); |
| 4377 | |
| 4378 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4379 | index = true; |
| 4380 | add = true; |
| 4381 | wback = false; |
| 4382 | break; |
| 4383 | |
| 4384 | case eEncodingT2: |
| 4385 | // if Rn == ’1111’ then UNDEFINED; |
| 4386 | if (Bits32 (opcode, 19, 16) == 15) |
| 4387 | return false; |
| 4388 | |
| 4389 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32); |
| 4390 | t = Bits32 (opcode, 15, 12); |
| 4391 | n = Bits32 (opcode, 19, 16); |
| 4392 | imm32 = Bits32 (opcode, 11, 0); |
| 4393 | |
| 4394 | // index = TRUE; add = TRUE; wback = FALSE; |
| 4395 | index = true; |
| 4396 | add = true; |
| 4397 | wback = false; |
| 4398 | |
| 4399 | // if BadReg(t) then UNPREDICTABLE; |
| 4400 | if (BadReg (t)) |
| 4401 | return false; |
| 4402 | break; |
| 4403 | |
| 4404 | case eEncodingT3: |
| 4405 | // if P == ’1’ && U == ’1’ && W == ’0’ then SEE STRBT; |
| 4406 | // if Rn == ’1111’ || (P == ’0’ && W == ’0’) then UNDEFINED; |
| 4407 | if (Bits32 (opcode, 19, 16) == 15) |
| 4408 | return false; |
| 4409 | |
| 4410 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm8, 32); |
| 4411 | t = Bits32 (opcode, 15, 12); |
| 4412 | n = Bits32 (opcode, 19, 16); |
| 4413 | imm32 = Bits32 (opcode, 7, 0); |
| 4414 | |
| 4415 | // index = (P == ’1’); add = (U == ’1’); wback = (W == ’1’); |
| 4416 | index = BitIsSet (opcode, 10); |
| 4417 | add = BitIsSet (opcode, 9); |
| 4418 | wback = BitIsSet (opcode, 8); |
| 4419 | |
| 4420 | // if BadReg(t) || (wback && n == t) then UNPREDICTABLE |
| 4421 | if ((BadReg (t)) || (wback && (n == t))) |
| 4422 | return false; |
| 4423 | break; |
| 4424 | |
| 4425 | default: |
| 4426 | return false; |
| 4427 | } |
| 4428 | |
| 4429 | addr_t offset_addr; |
| 4430 | addr_t address; |
| 4431 | addr_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 4432 | if (!success) |
| 4433 | return false; |
| 4434 | |
| 4435 | // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 4436 | if (add) |
| 4437 | offset_addr = base_address + imm32; |
| 4438 | else |
| 4439 | offset_addr = base_address - imm32; |
| 4440 | |
| 4441 | // address = if index then offset_addr else R[n]; |
| 4442 | if (index) |
| 4443 | address = offset_addr; |
| 4444 | else |
| 4445 | address = base_address; |
| 4446 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4447 | // MemU[address,1] = R[t]<7:0> |
Caroline Tice | 73a29de | 2011-02-16 20:22:22 +0000 | [diff] [blame] | 4448 | Register base_reg; |
| 4449 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 4450 | |
| 4451 | Register data_reg; |
| 4452 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + t); |
| 4453 | |
| 4454 | EmulateInstruction::Context context; |
| 4455 | context.type = eContextRegisterStore; |
| 4456 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, address - base_address); |
| 4457 | |
| 4458 | uint32_t data = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + t, 0, &success); |
| 4459 | if (!success) |
| 4460 | return false; |
| 4461 | |
| 4462 | data = Bits32 (data, 7, 0); |
| 4463 | |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 4464 | if (!MemUWrite (context, address, data, 1)) |
Caroline Tice | 73a29de | 2011-02-16 20:22:22 +0000 | [diff] [blame] | 4465 | return false; |
| 4466 | |
| 4467 | // if wback then R[n] = offset_addr; |
| 4468 | if (wback) |
| 4469 | { |
| 4470 | context.type = eContextRegisterLoad; |
| 4471 | context.SetAddress (offset_addr); |
| 4472 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 4473 | return false; |
| 4474 | } |
| 4475 | |
| 4476 | } |
| 4477 | |
| 4478 | return true; |
| 4479 | } |
Caroline Tice | 3fd63e9 | 2011-02-16 00:33:43 +0000 | [diff] [blame] | 4480 | |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4481 | // Add with Carry (immediate) adds an immediate value and the carry flag value to a register value, |
| 4482 | // and writes the result to the destination register. It can optionally update the condition flags |
| 4483 | // based on the result. |
| 4484 | bool |
| 4485 | EmulateInstructionARM::EmulateADCImm (ARMEncoding encoding) |
| 4486 | { |
| 4487 | #if 0 |
| 4488 | // ARM pseudo code... |
| 4489 | if ConditionPassed() then |
| 4490 | EncodingSpecificOperations(); |
| 4491 | (result, carry, overflow) = AddWithCarry(R[n], imm32, APSR.C); |
| 4492 | if d == 15 then // Can only occur for ARM encoding |
| 4493 | ALUWritePC(result); // setflags is always FALSE here |
| 4494 | else |
| 4495 | R[d] = result; |
| 4496 | if setflags then |
| 4497 | APSR.N = result<31>; |
| 4498 | APSR.Z = IsZeroBit(result); |
| 4499 | APSR.C = carry; |
| 4500 | APSR.V = overflow; |
| 4501 | #endif |
| 4502 | |
| 4503 | bool success = false; |
| 4504 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4505 | if (!success) |
| 4506 | return false; |
| 4507 | |
| 4508 | if (ConditionPassed()) |
| 4509 | { |
| 4510 | uint32_t Rd, Rn; |
| 4511 | uint32_t imm32; // the immediate value to be added to the value obtained from Rn |
| 4512 | bool setflags; |
| 4513 | switch (encoding) |
| 4514 | { |
| 4515 | case eEncodingT1: |
| 4516 | Rd = Bits32(opcode, 11, 8); |
| 4517 | Rn = Bits32(opcode, 19, 16); |
| 4518 | setflags = BitIsSet(opcode, 20); |
| 4519 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 4520 | if (BadReg(Rd) || BadReg(Rn)) |
| 4521 | return false; |
| 4522 | break; |
| 4523 | case eEncodingA1: |
| 4524 | Rd = Bits32(opcode, 15, 12); |
| 4525 | Rn = Bits32(opcode, 19, 16); |
| 4526 | setflags = BitIsSet(opcode, 20); |
| 4527 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 4528 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 4529 | if (Rd == 15 && setflags) |
| 4530 | return false; |
| 4531 | break; |
| 4532 | default: |
| 4533 | return false; |
| 4534 | } |
| 4535 | |
| 4536 | // Read the first operand. |
| 4537 | int32_t val1 = ReadCoreReg(Rn, &success); |
| 4538 | if (!success) |
| 4539 | return false; |
| 4540 | |
| 4541 | AddWithCarryResult res = AddWithCarry(val1, imm32, APSR_C); |
| 4542 | |
| 4543 | EmulateInstruction::Context context; |
| 4544 | context.type = EmulateInstruction::eContextImmediate; |
| 4545 | context.SetNoArgs (); |
| 4546 | |
| 4547 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 4548 | return false; |
| 4549 | } |
| 4550 | return true; |
| 4551 | } |
| 4552 | |
| 4553 | // Add with Carry (register) adds a register value, the carry flag value, and an optionally-shifted |
| 4554 | // register value, and writes the result to the destination register. It can optionally update the |
| 4555 | // condition flags based on the result. |
| 4556 | bool |
| 4557 | EmulateInstructionARM::EmulateADCReg (ARMEncoding encoding) |
| 4558 | { |
| 4559 | #if 0 |
| 4560 | // ARM pseudo code... |
| 4561 | if ConditionPassed() then |
| 4562 | EncodingSpecificOperations(); |
| 4563 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 4564 | (result, carry, overflow) = AddWithCarry(R[n], shifted, APSR.C); |
| 4565 | if d == 15 then // Can only occur for ARM encoding |
| 4566 | ALUWritePC(result); // setflags is always FALSE here |
| 4567 | else |
| 4568 | R[d] = result; |
| 4569 | if setflags then |
| 4570 | APSR.N = result<31>; |
| 4571 | APSR.Z = IsZeroBit(result); |
| 4572 | APSR.C = carry; |
| 4573 | APSR.V = overflow; |
| 4574 | #endif |
| 4575 | |
| 4576 | bool success = false; |
| 4577 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4578 | if (!success) |
| 4579 | return false; |
| 4580 | |
| 4581 | if (ConditionPassed()) |
| 4582 | { |
| 4583 | uint32_t Rd, Rn, Rm; |
| 4584 | ARM_ShifterType shift_t; |
| 4585 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 4586 | bool setflags; |
| 4587 | switch (encoding) |
| 4588 | { |
| 4589 | case eEncodingT1: |
| 4590 | Rd = Rn = Bits32(opcode, 2, 0); |
| 4591 | Rm = Bits32(opcode, 5, 3); |
| 4592 | setflags = !InITBlock(); |
| 4593 | shift_t = SRType_LSL; |
| 4594 | shift_n = 0; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 4595 | break; |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4596 | case eEncodingT2: |
| 4597 | Rd = Bits32(opcode, 11, 8); |
| 4598 | Rn = Bits32(opcode, 19, 16); |
| 4599 | Rm = Bits32(opcode, 3, 0); |
| 4600 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 4601 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4602 | if (BadReg(Rd) || BadReg(Rn) || BadReg(Rm)) |
| 4603 | return false; |
| 4604 | break; |
| 4605 | case eEncodingA1: |
| 4606 | Rd = Bits32(opcode, 15, 12); |
| 4607 | Rn = Bits32(opcode, 19, 16); |
| 4608 | Rm = Bits32(opcode, 3, 0); |
| 4609 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 4610 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4611 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 4612 | if (Rd == 15 && setflags) |
| 4613 | return false; |
| 4614 | break; |
| 4615 | default: |
| 4616 | return false; |
| 4617 | } |
| 4618 | |
| 4619 | // Read the first operand. |
| 4620 | int32_t val1 = ReadCoreReg(Rn, &success); |
| 4621 | if (!success) |
| 4622 | return false; |
| 4623 | |
| 4624 | // Read the second operand. |
| 4625 | int32_t val2 = ReadCoreReg(Rm, &success); |
| 4626 | if (!success) |
| 4627 | return false; |
| 4628 | |
| 4629 | uint32_t shifted = Shift(val2, shift_t, shift_n, APSR_C); |
| 4630 | AddWithCarryResult res = AddWithCarry(val1, shifted, APSR_C); |
| 4631 | |
| 4632 | EmulateInstruction::Context context; |
| 4633 | context.type = EmulateInstruction::eContextImmediate; |
| 4634 | context.SetNoArgs (); |
| 4635 | |
| 4636 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 4637 | return false; |
| 4638 | } |
| 4639 | return true; |
| 4640 | } |
| 4641 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4642 | // This instruction performs a bitwise AND of a register value and an immediate value, and writes the result |
| 4643 | // to the destination register. It can optionally update the condition flags based on the result. |
| 4644 | bool |
| 4645 | EmulateInstructionARM::EmulateANDImm (ARMEncoding encoding) |
| 4646 | { |
| 4647 | #if 0 |
| 4648 | // ARM pseudo code... |
| 4649 | if ConditionPassed() then |
| 4650 | EncodingSpecificOperations(); |
| 4651 | result = R[n] AND imm32; |
| 4652 | if d == 15 then // Can only occur for ARM encoding |
| 4653 | ALUWritePC(result); // setflags is always FALSE here |
| 4654 | else |
| 4655 | R[d] = result; |
| 4656 | if setflags then |
| 4657 | APSR.N = result<31>; |
| 4658 | APSR.Z = IsZeroBit(result); |
| 4659 | APSR.C = carry; |
| 4660 | // APSR.V unchanged |
| 4661 | #endif |
| 4662 | |
| 4663 | bool success = false; |
| 4664 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4665 | if (!success) |
| 4666 | return false; |
| 4667 | |
| 4668 | if (ConditionPassed()) |
| 4669 | { |
| 4670 | uint32_t Rd, Rn; |
| 4671 | uint32_t imm32; // the immediate value to be ANDed to the value obtained from Rn |
| 4672 | bool setflags; |
| 4673 | uint32_t carry; // the carry bit after ARM/Thumb Expand operation |
| 4674 | switch (encoding) |
| 4675 | { |
| 4676 | case eEncodingT1: |
| 4677 | Rd = Bits32(opcode, 11, 8); |
| 4678 | Rn = Bits32(opcode, 19, 16); |
| 4679 | setflags = BitIsSet(opcode, 20); |
| 4680 | 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] | 4681 | // if Rd == '1111' && S == '1' then SEE TST (immediate); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4682 | if (Rd == 15 && setflags) |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 4683 | return EmulateTSTImm(eEncodingT1); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4684 | if (Rd == 13 || (Rd == 15 && !setflags) || BadReg(Rn)) |
| 4685 | return false; |
| 4686 | break; |
| 4687 | case eEncodingA1: |
| 4688 | Rd = Bits32(opcode, 15, 12); |
| 4689 | Rn = Bits32(opcode, 19, 16); |
| 4690 | setflags = BitIsSet(opcode, 20); |
| 4691 | imm32 = ARMExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ARMExpandImm(imm12, APSR.C) |
| 4692 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 4693 | if (Rd == 15 && setflags) |
| 4694 | return false; |
| 4695 | break; |
| 4696 | default: |
| 4697 | return false; |
| 4698 | } |
| 4699 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4700 | // Read the first operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4701 | uint32_t val1 = ReadCoreReg(Rn, &success); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4702 | if (!success) |
| 4703 | return false; |
| 4704 | |
| 4705 | uint32_t result = val1 & imm32; |
| 4706 | |
| 4707 | EmulateInstruction::Context context; |
| 4708 | context.type = EmulateInstruction::eContextImmediate; |
| 4709 | context.SetNoArgs (); |
| 4710 | |
| 4711 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 4712 | return false; |
| 4713 | } |
| 4714 | return true; |
| 4715 | } |
| 4716 | |
| 4717 | // This instruction performs a bitwise AND of a register value and an optionally-shifted register value, |
| 4718 | // and writes the result to the destination register. It can optionally update the condition flags |
| 4719 | // based on the result. |
| 4720 | bool |
| 4721 | EmulateInstructionARM::EmulateANDReg (ARMEncoding encoding) |
| 4722 | { |
| 4723 | #if 0 |
| 4724 | // ARM pseudo code... |
| 4725 | if ConditionPassed() then |
| 4726 | EncodingSpecificOperations(); |
| 4727 | (shifted, carry) = Shift_C(R[m], shift_t, shift_n, APSR.C); |
| 4728 | result = R[n] AND shifted; |
| 4729 | if d == 15 then // Can only occur for ARM encoding |
| 4730 | ALUWritePC(result); // setflags is always FALSE here |
| 4731 | else |
| 4732 | R[d] = result; |
| 4733 | if setflags then |
| 4734 | APSR.N = result<31>; |
| 4735 | APSR.Z = IsZeroBit(result); |
| 4736 | APSR.C = carry; |
| 4737 | // APSR.V unchanged |
| 4738 | #endif |
| 4739 | |
| 4740 | bool success = false; |
| 4741 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4742 | if (!success) |
| 4743 | return false; |
| 4744 | |
| 4745 | if (ConditionPassed()) |
| 4746 | { |
| 4747 | uint32_t Rd, Rn, Rm; |
| 4748 | ARM_ShifterType shift_t; |
| 4749 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 4750 | bool setflags; |
| 4751 | uint32_t carry; |
| 4752 | switch (encoding) |
| 4753 | { |
| 4754 | case eEncodingT1: |
| 4755 | Rd = Rn = Bits32(opcode, 2, 0); |
| 4756 | Rm = Bits32(opcode, 5, 3); |
| 4757 | setflags = !InITBlock(); |
| 4758 | shift_t = SRType_LSL; |
| 4759 | shift_n = 0; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 4760 | break; |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4761 | case eEncodingT2: |
| 4762 | Rd = Bits32(opcode, 11, 8); |
| 4763 | Rn = Bits32(opcode, 19, 16); |
| 4764 | Rm = Bits32(opcode, 3, 0); |
| 4765 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 4766 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 4767 | // if Rd == '1111' && S == '1' then SEE TST (register); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4768 | if (Rd == 15 && setflags) |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 4769 | return EmulateTSTReg(eEncodingT2); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4770 | if (Rd == 13 || (Rd == 15 && !setflags) || BadReg(Rn) || BadReg(Rm)) |
| 4771 | return false; |
| 4772 | break; |
| 4773 | case eEncodingA1: |
| 4774 | Rd = Bits32(opcode, 15, 12); |
| 4775 | Rn = Bits32(opcode, 19, 16); |
| 4776 | Rm = Bits32(opcode, 3, 0); |
| 4777 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 4778 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4779 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 4780 | if (Rd == 15 && setflags) |
| 4781 | return false; |
| 4782 | break; |
| 4783 | default: |
| 4784 | return false; |
| 4785 | } |
| 4786 | |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4787 | // Read the first operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4788 | uint32_t val1 = ReadCoreReg(Rn, &success); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4789 | if (!success) |
| 4790 | return false; |
| 4791 | |
| 4792 | // Read the second operand. |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 4793 | uint32_t val2 = ReadCoreReg(Rm, &success); |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 4794 | if (!success) |
| 4795 | return false; |
| 4796 | |
| 4797 | uint32_t shifted = Shift_C(val2, shift_t, shift_n, APSR_C, carry); |
| 4798 | uint32_t result = val1 & shifted; |
| 4799 | |
| 4800 | EmulateInstruction::Context context; |
| 4801 | context.type = EmulateInstruction::eContextImmediate; |
| 4802 | context.SetNoArgs (); |
| 4803 | |
| 4804 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 4805 | return false; |
| 4806 | } |
| 4807 | return true; |
| 4808 | } |
| 4809 | |
Caroline Tice | 4d729c5 | 2011-02-18 00:55:53 +0000 | [diff] [blame] | 4810 | // 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] | 4811 | // 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] | 4812 | bool |
| 4813 | EmulateInstructionARM::EmulateLDRImmediateARM (ARMEncoding encoding) |
| 4814 | { |
| 4815 | #if 0 |
| 4816 | if ConditionPassed() then |
| 4817 | EncodingSpecificOperations(); |
| 4818 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 4819 | address = if index then offset_addr else R[n]; |
| 4820 | data = MemU[address,4]; |
| 4821 | if wback then R[n] = offset_addr; |
| 4822 | if t == 15 then |
| 4823 | if address<1:0> == ’00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 4824 | elsif UnalignedSupport() || address<1:0> = ’00’ then |
| 4825 | R[t] = data; |
| 4826 | else // Can only apply before ARMv7 |
| 4827 | R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 4828 | #endif |
| 4829 | |
| 4830 | bool success = false; |
| 4831 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4832 | if (!success) |
| 4833 | return false; |
| 4834 | |
| 4835 | if (ConditionPassed ()) |
| 4836 | { |
| 4837 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 4838 | |
| 4839 | uint32_t t; |
| 4840 | uint32_t n; |
| 4841 | uint32_t imm32; |
| 4842 | bool index; |
| 4843 | bool add; |
| 4844 | bool wback; |
| 4845 | |
| 4846 | switch (encoding) |
| 4847 | { |
| 4848 | case eEncodingA1: |
| 4849 | // if Rn == ’1111’ then SEE LDR (literal); |
| 4850 | // if P == ’0’ && W == ’1’ then SEE LDRT; |
| 4851 | // if Rn == ’1101’ && P == ’0’ && U == ’1’ && W == ’0’ && imm12 == ’000000000100’ then SEE POP; |
| 4852 | // t == UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32); |
| 4853 | t = Bits32 (opcode, 15, 12); |
| 4854 | n = Bits32 (opcode, 19, 16); |
| 4855 | imm32 = Bits32 (opcode, 11, 0); |
| 4856 | |
| 4857 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 4858 | index = BitIsSet (opcode, 24); |
| 4859 | add = BitIsSet (opcode, 23); |
| 4860 | wback = (BitIsClear (opcode, 24) || BitIsSet (opcode, 21)); |
| 4861 | |
| 4862 | // if wback && n == t then UNPREDICTABLE; |
| 4863 | if (wback && (n == t)) |
| 4864 | return false; |
| 4865 | |
| 4866 | break; |
| 4867 | |
| 4868 | default: |
| 4869 | return false; |
| 4870 | } |
| 4871 | |
| 4872 | addr_t address; |
| 4873 | addr_t offset_addr; |
| 4874 | addr_t base_address = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 4875 | if (!success) |
| 4876 | return false; |
| 4877 | |
| 4878 | // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 4879 | if (add) |
| 4880 | offset_addr = base_address + imm32; |
| 4881 | else |
| 4882 | offset_addr = base_address - imm32; |
| 4883 | |
| 4884 | // address = if index then offset_addr else R[n]; |
| 4885 | if (index) |
| 4886 | address = offset_addr; |
| 4887 | else |
| 4888 | address = base_address; |
| 4889 | |
| 4890 | // data = MemU[address,4]; |
| 4891 | |
| 4892 | Register base_reg; |
| 4893 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 4894 | |
| 4895 | EmulateInstruction::Context context; |
| 4896 | context.type = eContextRegisterLoad; |
| 4897 | context.SetRegisterPlusOffset (base_reg, address - base_address); |
| 4898 | |
| 4899 | uint64_t data = MemURead (context, address, addr_byte_size, 0, &success); |
| 4900 | if (!success) |
| 4901 | return false; |
| 4902 | |
| 4903 | // if wback then R[n] = offset_addr; |
| 4904 | if (wback) |
| 4905 | { |
| 4906 | context.type = eContextAdjustBaseRegister; |
| 4907 | context.SetAddress (offset_addr); |
| 4908 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 4909 | return false; |
| 4910 | } |
| 4911 | |
| 4912 | // if t == 15 then |
| 4913 | if (t == 15) |
| 4914 | { |
| 4915 | // if address<1:0> == ’00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 4916 | if (BitIsClear (address, 1) && BitIsClear (address, 0)) |
| 4917 | { |
| 4918 | // LoadWritePC (data); |
| 4919 | context.type = eContextRegisterLoad; |
| 4920 | context.SetRegisterPlusOffset (base_reg, address - base_address); |
| 4921 | LoadWritePC (context, data); |
| 4922 | } |
| 4923 | else |
| 4924 | return false; |
| 4925 | } |
| 4926 | // elsif UnalignedSupport() || address<1:0> = ’00’ then |
| 4927 | else if (UnalignedSupport() || (BitIsClear (address, 1) && BitIsClear (address, 0))) |
| 4928 | { |
| 4929 | // R[t] = data; |
| 4930 | context.type = eContextRegisterLoad; |
| 4931 | context.SetRegisterPlusOffset (base_reg, address - base_address); |
| 4932 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 4933 | return false; |
| 4934 | } |
| 4935 | // else // Can only apply before ARMv7 |
| 4936 | else |
| 4937 | { |
| 4938 | // R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 4939 | data = ROR (data, Bits32 (address, 1, 0)); |
| 4940 | context.type = eContextRegisterLoad; |
| 4941 | context.SetImmediate (data); |
| 4942 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 4943 | return false; |
| 4944 | } |
| 4945 | |
| 4946 | } |
| 4947 | return true; |
| 4948 | } |
| 4949 | |
Caroline Tice | fe47911 | 2011-02-18 18:52:37 +0000 | [diff] [blame] | 4950 | // LDR (register) calculates an address from a base register value and an offset register value, loads a word |
| 4951 | // from memory, and writes it to a resgister. The offset register value can optionally be shifted. |
| 4952 | bool |
| 4953 | EmulateInstructionARM::EmulateLDRRegister (ARMEncoding encoding) |
| 4954 | { |
| 4955 | #if 0 |
| 4956 | if ConditionPassed() then |
| 4957 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 4958 | offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 4959 | offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 4960 | address = if index then offset_addr else R[n]; |
| 4961 | data = MemU[address,4]; |
| 4962 | if wback then R[n] = offset_addr; |
| 4963 | if t == 15 then |
| 4964 | if address<1:0> == ’00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 4965 | elsif UnalignedSupport() || address<1:0> = ’00’ then |
| 4966 | R[t] = data; |
| 4967 | else // Can only apply before ARMv7 |
| 4968 | if CurrentInstrSet() == InstrSet_ARM then |
| 4969 | R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 4970 | else |
| 4971 | R[t] = bits(32) UNKNOWN; |
| 4972 | #endif |
| 4973 | |
| 4974 | bool success = false; |
| 4975 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 4976 | if (!success) |
| 4977 | return false; |
| 4978 | |
| 4979 | if (ConditionPassed ()) |
| 4980 | { |
| 4981 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 4982 | |
| 4983 | uint32_t t; |
| 4984 | uint32_t n; |
| 4985 | uint32_t m; |
| 4986 | bool index; |
| 4987 | bool add; |
| 4988 | bool wback; |
| 4989 | ARM_ShifterType shift_t; |
| 4990 | uint32_t shift_n; |
| 4991 | |
| 4992 | switch (encoding) |
| 4993 | { |
| 4994 | case eEncodingT1: |
| 4995 | // if CurrentInstrSet() == InstrSet_ThumbEE then SEE "Modified operation in ThumbEE"; |
| 4996 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 4997 | t = Bits32 (opcode, 2, 0); |
| 4998 | n = Bits32 (opcode, 5, 3); |
| 4999 | m = Bits32 (opcode, 8, 6); |
| 5000 | |
| 5001 | // index = TRUE; add = TRUE; wback = FALSE; |
| 5002 | index = true; |
| 5003 | add = true; |
| 5004 | wback = false; |
| 5005 | |
| 5006 | // (shift_t, shift_n) = (SRType_LSL, 0); |
| 5007 | shift_t = SRType_LSL; |
| 5008 | shift_n = 0; |
| 5009 | |
| 5010 | break; |
| 5011 | |
| 5012 | case eEncodingT2: |
| 5013 | // if Rn == ’1111’ then SEE LDR (literal); |
| 5014 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 5015 | t = Bits32 (opcode, 15, 12); |
| 5016 | n = Bits32 (opcode, 19, 16); |
| 5017 | m = Bits32 (opcode, 3, 0); |
| 5018 | |
| 5019 | // index = TRUE; add = TRUE; wback = FALSE; |
| 5020 | index = true; |
| 5021 | add = true; |
| 5022 | wback = false; |
| 5023 | |
| 5024 | // (shift_t, shift_n) = (SRType_LSL, UInt(imm2)); |
| 5025 | shift_t = SRType_LSL; |
| 5026 | shift_n = Bits32 (opcode, 5, 4); |
| 5027 | |
| 5028 | // if BadReg(m) then UNPREDICTABLE; |
| 5029 | if (BadReg (m)) |
| 5030 | return false; |
| 5031 | |
| 5032 | // if t == 15 && InITBlock() && !LastInITBlock() then UNPREDICTABLE; |
| 5033 | if ((t == 15) && InITBlock() && !LastInITBlock()) |
| 5034 | return false; |
| 5035 | |
| 5036 | break; |
| 5037 | |
| 5038 | case eEncodingA1: |
| 5039 | { |
| 5040 | // if P == ’0’ && W == ’1’ then SEE LDRT; |
| 5041 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 5042 | t = Bits32 (opcode, 15, 12); |
| 5043 | n = Bits32 (opcode, 19, 16); |
| 5044 | m = Bits32 (opcode, 3, 0); |
| 5045 | |
| 5046 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 5047 | index = BitIsSet (opcode, 24); |
| 5048 | add = BitIsSet (opcode, 23); |
| 5049 | wback = (BitIsClear (opcode, 24) || BitIsSet (opcode, 21)); |
| 5050 | |
| 5051 | // (shift_t, shift_n) = DecodeImmShift(type, imm5); |
| 5052 | uint32_t type = Bits32 (opcode, 6, 5); |
| 5053 | uint32_t imm5 = Bits32 (opcode, 11, 7); |
| 5054 | shift_n = DecodeImmShift (type, imm5, shift_t); |
| 5055 | |
| 5056 | // if m == 15 then UNPREDICTABLE; |
| 5057 | if (m == 15) |
| 5058 | return false; |
| 5059 | |
| 5060 | // if wback && (n == 15 || n == t) then UNPREDICTABLE; |
| 5061 | if (wback && ((n == 15) || (n == t))) |
| 5062 | return false; |
| 5063 | } |
| 5064 | break; |
| 5065 | |
| 5066 | |
| 5067 | default: |
| 5068 | return false; |
| 5069 | } |
| 5070 | |
| 5071 | uint32_t Rm = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 5072 | if (!success) |
| 5073 | return false; |
| 5074 | |
| 5075 | uint32_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 5076 | if (!success) |
| 5077 | return false; |
| 5078 | |
| 5079 | addr_t offset_addr; |
| 5080 | addr_t address; |
| 5081 | |
| 5082 | // 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] | 5083 | 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] | 5084 | |
| 5085 | // offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 5086 | if (add) |
| 5087 | offset_addr = Rn + offset; |
| 5088 | else |
| 5089 | offset_addr = Rn - offset; |
| 5090 | |
| 5091 | // address = if index then offset_addr else R[n]; |
| 5092 | if (index) |
| 5093 | address = offset_addr; |
| 5094 | else |
| 5095 | address = Rn; |
| 5096 | |
| 5097 | // data = MemU[address,4]; |
| 5098 | Register base_reg; |
| 5099 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 5100 | |
| 5101 | EmulateInstruction::Context context; |
| 5102 | context.type = eContextRegisterLoad; |
| 5103 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 5104 | |
| 5105 | uint64_t data = MemURead (context, address, addr_byte_size, 0, &success); |
| 5106 | if (!success) |
| 5107 | return false; |
| 5108 | |
| 5109 | // if wback then R[n] = offset_addr; |
| 5110 | if (wback) |
| 5111 | { |
| 5112 | context.type = eContextAdjustBaseRegister; |
| 5113 | context.SetAddress (offset_addr); |
| 5114 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 5115 | return false; |
| 5116 | } |
| 5117 | |
| 5118 | // if t == 15 then |
| 5119 | if (t == 15) |
| 5120 | { |
| 5121 | // if address<1:0> == ’00’ then LoadWritePC(data); else UNPREDICTABLE; |
| 5122 | if (BitIsClear (address, 1) && BitIsClear (address, 0)) |
| 5123 | { |
| 5124 | context.type = eContextRegisterLoad; |
| 5125 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 5126 | LoadWritePC (context, data); |
| 5127 | } |
| 5128 | else |
| 5129 | return false; |
| 5130 | } |
| 5131 | // elsif UnalignedSupport() || address<1:0> = ’00’ then |
| 5132 | else if (UnalignedSupport () || (BitIsClear (address, 1) && BitIsClear (address, 0))) |
| 5133 | { |
| 5134 | // R[t] = data; |
| 5135 | context.type = eContextRegisterLoad; |
| 5136 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 5137 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 5138 | return false; |
| 5139 | } |
| 5140 | else // Can only apply before ARMv7 |
| 5141 | { |
| 5142 | // if CurrentInstrSet() == InstrSet_ARM then |
| 5143 | if (CurrentInstrSet () == eModeARM) |
| 5144 | { |
| 5145 | // R[t] = ROR(data, 8*UInt(address<1:0>)); |
| 5146 | data = ROR (data, Bits32 (address, 1, 0)); |
| 5147 | context.type = eContextRegisterLoad; |
| 5148 | context.SetImmediate (data); |
| 5149 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 5150 | return false; |
| 5151 | } |
| 5152 | else |
| 5153 | { |
| 5154 | // R[t] = bits(32) UNKNOWN; |
| 5155 | WriteBits32Unknown (t); |
| 5156 | } |
| 5157 | } |
| 5158 | } |
| 5159 | return true; |
| 5160 | } |
Caroline Tice | 21b604b | 2011-02-18 21:06:04 +0000 | [diff] [blame] | 5161 | |
| 5162 | // LDRB (immediate, Thumb) |
| 5163 | bool |
| 5164 | EmulateInstructionARM::EmulateLDRBImmediate (ARMEncoding encoding) |
| 5165 | { |
| 5166 | #if 0 |
| 5167 | if ConditionPassed() then |
| 5168 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 5169 | offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 5170 | address = if index then offset_addr else R[n]; |
| 5171 | R[t] = ZeroExtend(MemU[address,1], 32); |
| 5172 | if wback then R[n] = offset_addr; |
| 5173 | #endif |
| 5174 | |
| 5175 | bool success = false; |
| 5176 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5177 | if (!success) |
| 5178 | return false; |
| 5179 | |
| 5180 | if (ConditionPassed ()) |
| 5181 | { |
| 5182 | uint32_t t; |
| 5183 | uint32_t n; |
| 5184 | uint32_t imm32; |
| 5185 | bool index; |
| 5186 | bool add; |
| 5187 | bool wback; |
| 5188 | |
| 5189 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 5190 | switch (encoding) |
| 5191 | { |
| 5192 | case eEncodingT1: |
| 5193 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm5, 32); |
| 5194 | t = Bits32 (opcode, 2, 0); |
| 5195 | n = Bits32 (opcode, 5, 3); |
| 5196 | imm32 = Bits32 (opcode, 10, 6); |
| 5197 | |
| 5198 | // index = TRUE; add = TRUE; wback = FALSE; |
| 5199 | index = true; |
| 5200 | add = true; |
| 5201 | wback= false; |
| 5202 | |
| 5203 | break; |
| 5204 | |
| 5205 | case eEncodingT2: |
| 5206 | // if Rt == ’1111’ then SEE PLD; |
| 5207 | // if Rn == ’1111’ then SEE LDRB (literal); |
| 5208 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm12, 32); |
| 5209 | t = Bits32 (opcode, 15, 12); |
| 5210 | n = Bits32 (opcode, 19, 16); |
| 5211 | imm32 = Bits32 (opcode, 11, 0); |
| 5212 | |
| 5213 | // index = TRUE; add = TRUE; wback = FALSE; |
| 5214 | index = true; |
| 5215 | add = true; |
| 5216 | wback = false; |
| 5217 | |
| 5218 | // if t == 13 then UNPREDICTABLE; |
| 5219 | if (t == 13) |
| 5220 | return false; |
| 5221 | |
| 5222 | break; |
| 5223 | |
| 5224 | case eEncodingT3: |
| 5225 | // if Rt == ’1111’ && P == ’1’ && U == ’0’ && W == ’0’ then SEE PLD; |
| 5226 | // if Rn == ’1111’ then SEE LDRB (literal); |
| 5227 | // if P == ’1’ && U == ’1’ && W == ’0’ then SEE LDRBT; |
| 5228 | // if P == ’0’ && W == ’0’ then UNDEFINED; |
| 5229 | if (BitIsClear (opcode, 10) && BitIsClear (opcode, 8)) |
| 5230 | return false; |
| 5231 | |
| 5232 | // t = UInt(Rt); n = UInt(Rn); imm32 = ZeroExtend(imm8, 32); |
| 5233 | t = Bits32 (opcode, 15, 12); |
| 5234 | n = Bits32 (opcode, 19, 16); |
| 5235 | imm32 = Bits32 (opcode, 7, 0); |
| 5236 | |
| 5237 | // index = (P == ’1’); add = (U == ’1’); wback = (W == ’1’); |
| 5238 | index = BitIsSet (opcode, 10); |
| 5239 | add = BitIsSet (opcode, 9); |
| 5240 | wback = BitIsSet (opcode, 8); |
| 5241 | |
| 5242 | // if BadReg(t) || (wback && n == t) then UNPREDICTABLE; |
| 5243 | if (BadReg (t) || (wback && (n == t))) |
| 5244 | return false; |
| 5245 | |
| 5246 | break; |
| 5247 | |
| 5248 | default: |
| 5249 | return false; |
| 5250 | } |
| 5251 | |
| 5252 | uint32_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 5253 | if (!success) |
| 5254 | return false; |
| 5255 | |
| 5256 | addr_t address; |
| 5257 | addr_t offset_addr; |
| 5258 | |
| 5259 | // offset_addr = if add then (R[n] + imm32) else (R[n] - imm32); |
| 5260 | if (add) |
| 5261 | offset_addr = Rn + imm32; |
| 5262 | else |
| 5263 | offset_addr = Rn - imm32; |
| 5264 | |
| 5265 | // address = if index then offset_addr else R[n]; |
| 5266 | if (index) |
| 5267 | address = offset_addr; |
| 5268 | else |
| 5269 | address = Rn; |
| 5270 | |
| 5271 | // R[t] = ZeroExtend(MemU[address,1], 32); |
| 5272 | Register base_reg; |
| 5273 | Register data_reg; |
| 5274 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 5275 | data_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + t); |
| 5276 | |
| 5277 | EmulateInstruction::Context context; |
| 5278 | context.type = eContextRegisterLoad; |
| 5279 | context.SetRegisterToRegisterPlusOffset (data_reg, base_reg, address - Rn); |
| 5280 | |
| 5281 | uint64_t data = MemURead (context, address, 1, 0, &success); |
| 5282 | if (!success) |
| 5283 | return false; |
| 5284 | |
| 5285 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 5286 | return false; |
| 5287 | |
| 5288 | // if wback then R[n] = offset_addr; |
| 5289 | if (wback) |
| 5290 | { |
| 5291 | context.type = eContextAdjustBaseRegister; |
| 5292 | context.SetAddress (offset_addr); |
| 5293 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 5294 | return false; |
| 5295 | } |
| 5296 | } |
| 5297 | return true; |
| 5298 | } |
Caroline Tice | f55261f | 2011-02-18 22:24:22 +0000 | [diff] [blame] | 5299 | |
| 5300 | // LDRB (literal) calculates an address from the PC value and an immediate offset, loads a byte from memory, |
| 5301 | // zero-extends it to form a 32-bit word and writes it to a register. |
| 5302 | bool |
| 5303 | EmulateInstructionARM::EmulateLDRBLiteral (ARMEncoding encoding) |
| 5304 | { |
| 5305 | #if 0 |
| 5306 | if ConditionPassed() then |
| 5307 | EncodingSpecificOperations(); NullCheckIfThumbEE(15); |
| 5308 | base = Align(PC,4); |
| 5309 | address = if add then (base + imm32) else (base - imm32); |
| 5310 | R[t] = ZeroExtend(MemU[address,1], 32); |
| 5311 | #endif |
| 5312 | |
| 5313 | bool success = false; |
| 5314 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5315 | if (!success) |
| 5316 | return false; |
| 5317 | |
| 5318 | if (ConditionPassed ()) |
| 5319 | { |
| 5320 | uint32_t t; |
| 5321 | uint32_t imm32; |
| 5322 | bool add; |
| 5323 | switch (encoding) |
| 5324 | { |
| 5325 | case eEncodingT1: |
| 5326 | // if Rt == ’1111’ then SEE PLD; |
| 5327 | // t = UInt(Rt); imm32 = ZeroExtend(imm12, 32); add = (U == ’1’); |
| 5328 | t = Bits32 (opcode, 15, 12); |
| 5329 | imm32 = Bits32 (opcode, 11, 0); |
| 5330 | add = BitIsSet (opcode, 23); |
| 5331 | |
| 5332 | // if t == 13 then UNPREDICTABLE; |
| 5333 | if (t == 13) |
| 5334 | return false; |
| 5335 | |
| 5336 | break; |
| 5337 | |
| 5338 | case eEncodingA1: |
| 5339 | // t == UInt(Rt); imm32 = ZeroExtend(imm12, 32); add = (U == ’1’); |
| 5340 | t = Bits32 (opcode, 15, 12); |
| 5341 | imm32 = Bits32 (opcode, 11, 0); |
| 5342 | add = BitIsSet (opcode, 23); |
| 5343 | |
| 5344 | // if t == 15 then UNPREDICTABLE; |
| 5345 | if (t == 15) |
| 5346 | return false; |
| 5347 | break; |
| 5348 | |
| 5349 | default: |
| 5350 | return false; |
| 5351 | } |
| 5352 | |
| 5353 | // base = Align(PC,4); |
| 5354 | uint32_t pc_val = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, 0, &success); |
| 5355 | if (!success) |
| 5356 | return false; |
| 5357 | |
| 5358 | uint32_t base = AlignPC (pc_val); |
| 5359 | |
| 5360 | addr_t address; |
| 5361 | // address = if add then (base + imm32) else (base - imm32); |
| 5362 | if (add) |
| 5363 | address = base + imm32; |
| 5364 | else |
| 5365 | address = base - imm32; |
| 5366 | |
| 5367 | // R[t] = ZeroExtend(MemU[address,1], 32); |
| 5368 | EmulateInstruction::Context context; |
| 5369 | context.type = eContextRelativeBranchImmediate; |
| 5370 | context.SetImmediate (address - base); |
| 5371 | |
| 5372 | uint64_t data = MemURead (context, address, 1, 0, &success); |
| 5373 | if (!success) |
| 5374 | return false; |
| 5375 | |
| 5376 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 5377 | return false; |
| 5378 | } |
| 5379 | return true; |
| 5380 | } |
Caroline Tice | 30fec12 | 2011-02-18 23:52:21 +0000 | [diff] [blame] | 5381 | |
| 5382 | // LDRB (register) calculates an address from a base register value and an offset rigister value, loads a byte from |
| 5383 | // memory, zero-extends it to form a 32-bit word, and writes it to a register. The offset register value can |
| 5384 | // optionally be shifted. |
| 5385 | bool |
| 5386 | EmulateInstructionARM::EmulateLDRBRegister (ARMEncoding encoding) |
| 5387 | { |
| 5388 | #if 0 |
| 5389 | if ConditionPassed() then |
| 5390 | EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 5391 | offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 5392 | offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 5393 | address = if index then offset_addr else R[n]; |
| 5394 | R[t] = ZeroExtend(MemU[address,1],32); |
| 5395 | if wback then R[n] = offset_addr; |
| 5396 | #endif |
| 5397 | |
| 5398 | bool success = false; |
| 5399 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5400 | if (!success) |
| 5401 | return false; |
| 5402 | |
| 5403 | if (ConditionPassed ()) |
| 5404 | { |
| 5405 | uint32_t t; |
| 5406 | uint32_t n; |
| 5407 | uint32_t m; |
| 5408 | bool index; |
| 5409 | bool add; |
| 5410 | bool wback; |
| 5411 | ARM_ShifterType shift_t; |
| 5412 | uint32_t shift_n; |
| 5413 | |
| 5414 | // EncodingSpecificOperations(); NullCheckIfThumbEE(n); |
| 5415 | switch (encoding) |
| 5416 | { |
| 5417 | case eEncodingT1: |
| 5418 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 5419 | t = Bits32 (opcode, 2, 0); |
| 5420 | n = Bits32 (opcode, 5, 3); |
| 5421 | m = Bits32 (opcode, 8, 6); |
| 5422 | |
| 5423 | // index = TRUE; add = TRUE; wback = FALSE; |
| 5424 | index = true; |
| 5425 | add = true; |
| 5426 | wback = false; |
| 5427 | |
| 5428 | // (shift_t, shift_n) = (SRType_LSL, 0); |
| 5429 | shift_t = SRType_LSL; |
| 5430 | shift_n = 0; |
| 5431 | break; |
| 5432 | |
| 5433 | case eEncodingT2: |
| 5434 | // if Rt == ’1111’ then SEE PLD; |
| 5435 | // if Rn == ’1111’ then SEE LDRB (literal); |
| 5436 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 5437 | t = Bits32 (opcode, 15, 12); |
| 5438 | n = Bits32 (opcode, 19, 16); |
| 5439 | m = Bits32 (opcode, 3, 0); |
| 5440 | |
| 5441 | // index = TRUE; add = TRUE; wback = FALSE; |
| 5442 | index = true; |
| 5443 | add = true; |
| 5444 | wback = false; |
| 5445 | |
| 5446 | // (shift_t, shift_n) = (SRType_LSL, UInt(imm2)); |
| 5447 | shift_t = SRType_LSL; |
| 5448 | shift_n = Bits32 (opcode, 5, 4); |
| 5449 | |
| 5450 | // if t == 13 || BadReg(m) then UNPREDICTABLE; |
| 5451 | if ((t == 13) || BadReg (m)) |
| 5452 | return false; |
| 5453 | break; |
| 5454 | |
| 5455 | case eEncodingA1: |
| 5456 | { |
| 5457 | // if P == ’0’ && W == ’1’ then SEE LDRBT; |
| 5458 | // t = UInt(Rt); n = UInt(Rn); m = UInt(Rm); |
| 5459 | t = Bits32 (opcode, 15, 12); |
| 5460 | n = Bits32 (opcode, 19, 16); |
| 5461 | m = Bits32 (opcode, 3, 0); |
| 5462 | |
| 5463 | // index = (P == ’1’); add = (U == ’1’); wback = (P == ’0’) || (W == ’1’); |
| 5464 | index = BitIsSet (opcode, 24); |
| 5465 | add = BitIsSet (opcode, 23); |
| 5466 | wback = (BitIsClear (opcode, 24) || BitIsSet (opcode, 21)); |
| 5467 | |
| 5468 | // (shift_t, shift_n) = DecodeImmShift(type, imm5); |
| 5469 | uint32_t type = Bits32 (opcode, 6, 5); |
| 5470 | uint32_t imm5 = Bits32 (opcode, 11, 7); |
| 5471 | shift_n = DecodeImmShift (type, imm5, shift_t); |
| 5472 | |
| 5473 | // if t == 15 || m == 15 then UNPREDICTABLE; |
| 5474 | if ((t == 15) || (m == 15)) |
| 5475 | return false; |
| 5476 | |
| 5477 | // if wback && (n == 15 || n == t) then UNPREDICTABLE; |
| 5478 | if (wback && ((n == 15) || (n == t))) |
| 5479 | return false; |
| 5480 | } |
| 5481 | break; |
| 5482 | |
| 5483 | default: |
| 5484 | return false; |
| 5485 | } |
| 5486 | |
| 5487 | addr_t offset_addr; |
| 5488 | addr_t address; |
| 5489 | |
| 5490 | // offset = Shift(R[m], shift_t, shift_n, APSR.C); |
| 5491 | uint32_t Rm = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + m, 0, &success); |
| 5492 | if (!success) |
| 5493 | return false; |
| 5494 | |
| 5495 | addr_t offset = Shift (Rm, shift_t, shift_n, APSR_C); |
| 5496 | |
| 5497 | // offset_addr = if add then (R[n] + offset) else (R[n] - offset); |
| 5498 | uint32_t Rn = ReadRegisterUnsigned (eRegisterKindDWARF, dwarf_r0 + n, 0, &success); |
| 5499 | if (!success) |
| 5500 | return false; |
| 5501 | |
| 5502 | if (add) |
| 5503 | offset_addr = Rn + offset; |
| 5504 | else |
| 5505 | offset_addr = Rn - offset; |
| 5506 | |
| 5507 | // address = if index then offset_addr else R[n]; |
| 5508 | if (index) |
| 5509 | address = offset_addr; |
| 5510 | else |
| 5511 | address = Rn; |
| 5512 | |
| 5513 | // R[t] = ZeroExtend(MemU[address,1],32); |
| 5514 | Register base_reg; |
| 5515 | base_reg.SetRegister (eRegisterKindDWARF, dwarf_r0 + n); |
| 5516 | |
| 5517 | EmulateInstruction::Context context; |
| 5518 | context.type = eContextRegisterLoad; |
| 5519 | context.SetRegisterPlusOffset (base_reg, address - Rn); |
| 5520 | |
| 5521 | uint64_t data = MemURead (context, address, 1, 0, &success); |
| 5522 | if (!success) |
| 5523 | return false; |
| 5524 | |
| 5525 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + t, data)) |
| 5526 | return false; |
| 5527 | |
| 5528 | // if wback then R[n] = offset_addr; |
| 5529 | if (wback) |
| 5530 | { |
| 5531 | context.type = eContextAdjustBaseRegister; |
| 5532 | context.SetAddress (offset_addr); |
| 5533 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + n, offset_addr)) |
| 5534 | return false; |
| 5535 | } |
| 5536 | } |
| 5537 | return true; |
| 5538 | } |
Caroline Tice | fe47911 | 2011-02-18 18:52:37 +0000 | [diff] [blame] | 5539 | |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 5540 | // Bitwise Exclusive OR (immediate) performs a bitwise exclusive OR of a register value and an immediate value, |
| 5541 | // and writes the result to the destination register. It can optionally update the condition flags based on |
| 5542 | // the result. |
| 5543 | bool |
| 5544 | EmulateInstructionARM::EmulateEORImm (ARMEncoding encoding) |
| 5545 | { |
| 5546 | #if 0 |
| 5547 | // ARM pseudo code... |
| 5548 | if ConditionPassed() then |
| 5549 | EncodingSpecificOperations(); |
| 5550 | result = R[n] EOR imm32; |
| 5551 | if d == 15 then // Can only occur for ARM encoding |
| 5552 | ALUWritePC(result); // setflags is always FALSE here |
| 5553 | else |
| 5554 | R[d] = result; |
| 5555 | if setflags then |
| 5556 | APSR.N = result<31>; |
| 5557 | APSR.Z = IsZeroBit(result); |
| 5558 | APSR.C = carry; |
| 5559 | // APSR.V unchanged |
| 5560 | #endif |
| 5561 | |
| 5562 | bool success = false; |
| 5563 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5564 | if (!success) |
| 5565 | return false; |
| 5566 | |
| 5567 | if (ConditionPassed()) |
| 5568 | { |
| 5569 | uint32_t Rd, Rn; |
| 5570 | uint32_t imm32; // the immediate value to be ORed to the value obtained from Rn |
| 5571 | bool setflags; |
| 5572 | uint32_t carry; // the carry bit after ARM/Thumb Expand operation |
| 5573 | switch (encoding) |
| 5574 | { |
| 5575 | case eEncodingT1: |
| 5576 | Rd = Bits32(opcode, 11, 8); |
| 5577 | Rn = Bits32(opcode, 19, 16); |
| 5578 | setflags = BitIsSet(opcode, 20); |
| 5579 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ThumbExpandImm(i:imm3:imm8, APSR.C) |
| 5580 | // if Rd == '1111' && S == '1' then SEE TEQ (immediate); |
| 5581 | if (Rd == 15 && setflags) |
| 5582 | return EmulateTEQImm(eEncodingT1); |
| 5583 | if (Rd == 13 || (Rd == 15 && !setflags) || BadReg(Rn)) |
| 5584 | return false; |
| 5585 | break; |
| 5586 | case eEncodingA1: |
| 5587 | Rd = Bits32(opcode, 15, 12); |
| 5588 | Rn = Bits32(opcode, 19, 16); |
| 5589 | setflags = BitIsSet(opcode, 20); |
| 5590 | imm32 = ARMExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ARMExpandImm(imm12, APSR.C) |
| 5591 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 5592 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 5593 | if (Rd == 15 && setflags) |
| 5594 | return false; |
| 5595 | break; |
| 5596 | default: |
| 5597 | return false; |
| 5598 | } |
| 5599 | |
| 5600 | // Read the first operand. |
| 5601 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 5602 | if (!success) |
| 5603 | return false; |
| 5604 | |
| 5605 | uint32_t result = val1 ^ imm32; |
| 5606 | |
| 5607 | EmulateInstruction::Context context; |
| 5608 | context.type = EmulateInstruction::eContextImmediate; |
| 5609 | context.SetNoArgs (); |
| 5610 | |
| 5611 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 5612 | return false; |
| 5613 | } |
| 5614 | return true; |
| 5615 | } |
| 5616 | |
| 5617 | // Bitwise Exclusive OR (register) performs a bitwise exclusive OR of a register value and an |
| 5618 | // optionally-shifted register value, and writes the result to the destination register. |
| 5619 | // It can optionally update the condition flags based on the result. |
| 5620 | bool |
| 5621 | EmulateInstructionARM::EmulateEORReg (ARMEncoding encoding) |
| 5622 | { |
| 5623 | #if 0 |
| 5624 | // ARM pseudo code... |
| 5625 | if ConditionPassed() then |
| 5626 | EncodingSpecificOperations(); |
| 5627 | (shifted, carry) = Shift_C(R[m], shift_t, shift_n, APSR.C); |
| 5628 | result = R[n] EOR shifted; |
| 5629 | if d == 15 then // Can only occur for ARM encoding |
| 5630 | ALUWritePC(result); // setflags is always FALSE here |
| 5631 | else |
| 5632 | R[d] = result; |
| 5633 | if setflags then |
| 5634 | APSR.N = result<31>; |
| 5635 | APSR.Z = IsZeroBit(result); |
| 5636 | APSR.C = carry; |
| 5637 | // APSR.V unchanged |
| 5638 | #endif |
| 5639 | |
| 5640 | bool success = false; |
| 5641 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5642 | if (!success) |
| 5643 | return false; |
| 5644 | |
| 5645 | if (ConditionPassed()) |
| 5646 | { |
| 5647 | uint32_t Rd, Rn, Rm; |
| 5648 | ARM_ShifterType shift_t; |
| 5649 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 5650 | bool setflags; |
| 5651 | uint32_t carry; |
| 5652 | switch (encoding) |
| 5653 | { |
| 5654 | case eEncodingT1: |
| 5655 | Rd = Rn = Bits32(opcode, 2, 0); |
| 5656 | Rm = Bits32(opcode, 5, 3); |
| 5657 | setflags = !InITBlock(); |
| 5658 | shift_t = SRType_LSL; |
| 5659 | shift_n = 0; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 5660 | break; |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 5661 | case eEncodingT2: |
| 5662 | Rd = Bits32(opcode, 11, 8); |
| 5663 | Rn = Bits32(opcode, 19, 16); |
| 5664 | Rm = Bits32(opcode, 3, 0); |
| 5665 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 5666 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
| 5667 | // if Rd == '1111' && S == '1' then SEE TEQ (register); |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 5668 | if (Rd == 15 && setflags) |
| 5669 | return EmulateTEQReg(eEncodingT1); |
| 5670 | if (Rd == 13 || (Rd == 15 && !setflags) || BadReg(Rn) || BadReg(Rm)) |
| 5671 | return false; |
| 5672 | break; |
| 5673 | case eEncodingA1: |
| 5674 | Rd = Bits32(opcode, 15, 12); |
| 5675 | Rn = Bits32(opcode, 19, 16); |
| 5676 | Rm = Bits32(opcode, 3, 0); |
| 5677 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 5678 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 5679 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 5680 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 5681 | if (Rd == 15 && setflags) |
| 5682 | return false; |
| 5683 | break; |
| 5684 | default: |
| 5685 | return false; |
| 5686 | } |
| 5687 | |
| 5688 | // Read the first operand. |
| 5689 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 5690 | if (!success) |
| 5691 | return false; |
| 5692 | |
| 5693 | // Read the second operand. |
| 5694 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 5695 | if (!success) |
| 5696 | return false; |
| 5697 | |
| 5698 | uint32_t shifted = Shift_C(val2, shift_t, shift_n, APSR_C, carry); |
| 5699 | uint32_t result = val1 ^ shifted; |
| 5700 | |
| 5701 | EmulateInstruction::Context context; |
| 5702 | context.type = EmulateInstruction::eContextImmediate; |
| 5703 | context.SetNoArgs (); |
| 5704 | |
| 5705 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 5706 | return false; |
| 5707 | } |
| 5708 | return true; |
| 5709 | } |
| 5710 | |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 5711 | // Bitwise OR (immediate) performs a bitwise (inclusive) OR of a register value and an immediate value, and |
| 5712 | // writes the result to the destination register. It can optionally update the condition flags based |
| 5713 | // on the result. |
| 5714 | bool |
| 5715 | EmulateInstructionARM::EmulateORRImm (ARMEncoding encoding) |
| 5716 | { |
| 5717 | #if 0 |
| 5718 | // ARM pseudo code... |
| 5719 | if ConditionPassed() then |
| 5720 | EncodingSpecificOperations(); |
| 5721 | result = R[n] OR imm32; |
| 5722 | if d == 15 then // Can only occur for ARM encoding |
| 5723 | ALUWritePC(result); // setflags is always FALSE here |
| 5724 | else |
| 5725 | R[d] = result; |
| 5726 | if setflags then |
| 5727 | APSR.N = result<31>; |
| 5728 | APSR.Z = IsZeroBit(result); |
| 5729 | APSR.C = carry; |
| 5730 | // APSR.V unchanged |
| 5731 | #endif |
| 5732 | |
| 5733 | bool success = false; |
| 5734 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5735 | if (!success) |
| 5736 | return false; |
| 5737 | |
| 5738 | if (ConditionPassed()) |
| 5739 | { |
| 5740 | uint32_t Rd, Rn; |
| 5741 | uint32_t imm32; // the immediate value to be ORed to the value obtained from Rn |
| 5742 | bool setflags; |
| 5743 | uint32_t carry; // the carry bit after ARM/Thumb Expand operation |
| 5744 | switch (encoding) |
| 5745 | { |
| 5746 | case eEncodingT1: |
| 5747 | Rd = Bits32(opcode, 11, 8); |
| 5748 | Rn = Bits32(opcode, 19, 16); |
| 5749 | setflags = BitIsSet(opcode, 20); |
| 5750 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ThumbExpandImm(i:imm3:imm8, APSR.C) |
| 5751 | // if Rn == ‘1111’ then SEE MOV (immediate); |
| 5752 | if (Rn == 15) |
| 5753 | return EmulateMOVRdImm(eEncodingT2); |
| 5754 | if (BadReg(Rd) || Rn == 13) |
| 5755 | return false; |
| 5756 | break; |
| 5757 | case eEncodingA1: |
| 5758 | Rd = Bits32(opcode, 15, 12); |
| 5759 | Rn = Bits32(opcode, 19, 16); |
| 5760 | setflags = BitIsSet(opcode, 20); |
| 5761 | imm32 = ARMExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ARMExpandImm(imm12, APSR.C) |
| 5762 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 5763 | if (Rd == 15 && setflags) |
| 5764 | return false; |
| 5765 | break; |
| 5766 | default: |
| 5767 | return false; |
| 5768 | } |
| 5769 | |
| 5770 | // Read the first operand. |
| 5771 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 5772 | if (!success) |
| 5773 | return false; |
| 5774 | |
| 5775 | uint32_t result = val1 | imm32; |
| 5776 | |
| 5777 | EmulateInstruction::Context context; |
| 5778 | context.type = EmulateInstruction::eContextImmediate; |
| 5779 | context.SetNoArgs (); |
| 5780 | |
| 5781 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 5782 | return false; |
| 5783 | } |
| 5784 | return true; |
| 5785 | } |
| 5786 | |
| 5787 | // Bitwise OR (register) performs a bitwise (inclusive) OR of a register value and an optionally-shifted register |
| 5788 | // value, and writes the result to the destination register. It can optionally update the condition flags based |
| 5789 | // on the result. |
| 5790 | bool |
| 5791 | EmulateInstructionARM::EmulateORRReg (ARMEncoding encoding) |
| 5792 | { |
| 5793 | #if 0 |
| 5794 | // ARM pseudo code... |
| 5795 | if ConditionPassed() then |
| 5796 | EncodingSpecificOperations(); |
| 5797 | (shifted, carry) = Shift_C(R[m], shift_t, shift_n, APSR.C); |
| 5798 | result = R[n] OR shifted; |
| 5799 | if d == 15 then // Can only occur for ARM encoding |
| 5800 | ALUWritePC(result); // setflags is always FALSE here |
| 5801 | else |
| 5802 | R[d] = result; |
| 5803 | if setflags then |
| 5804 | APSR.N = result<31>; |
| 5805 | APSR.Z = IsZeroBit(result); |
| 5806 | APSR.C = carry; |
| 5807 | // APSR.V unchanged |
| 5808 | #endif |
| 5809 | |
| 5810 | bool success = false; |
| 5811 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5812 | if (!success) |
| 5813 | return false; |
| 5814 | |
| 5815 | if (ConditionPassed()) |
| 5816 | { |
| 5817 | uint32_t Rd, Rn, Rm; |
| 5818 | ARM_ShifterType shift_t; |
| 5819 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 5820 | bool setflags; |
| 5821 | uint32_t carry; |
| 5822 | switch (encoding) |
| 5823 | { |
| 5824 | case eEncodingT1: |
| 5825 | Rd = Rn = Bits32(opcode, 2, 0); |
| 5826 | Rm = Bits32(opcode, 5, 3); |
| 5827 | setflags = !InITBlock(); |
| 5828 | shift_t = SRType_LSL; |
| 5829 | shift_n = 0; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 5830 | break; |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 5831 | case eEncodingT2: |
| 5832 | Rd = Bits32(opcode, 11, 8); |
| 5833 | Rn = Bits32(opcode, 19, 16); |
| 5834 | Rm = Bits32(opcode, 3, 0); |
| 5835 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 5836 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
| 5837 | // if Rn == '1111' then SEE MOV (register); |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 5838 | if (Rn == 15) |
| 5839 | return EmulateMOVRdRm(eEncodingT3); |
| 5840 | if (BadReg(Rd) || Rn == 13 || BadReg(Rm)) |
| 5841 | return false; |
| 5842 | break; |
| 5843 | case eEncodingA1: |
| 5844 | Rd = Bits32(opcode, 15, 12); |
| 5845 | Rn = Bits32(opcode, 19, 16); |
| 5846 | Rm = Bits32(opcode, 3, 0); |
| 5847 | setflags = BitIsSet(opcode, 20); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 5848 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 5849 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 5850 | if (Rd == 15 && setflags) |
| 5851 | return false; |
| 5852 | break; |
| 5853 | default: |
| 5854 | return false; |
| 5855 | } |
| 5856 | |
| 5857 | // Read the first operand. |
| 5858 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 5859 | if (!success) |
| 5860 | return false; |
| 5861 | |
| 5862 | // Read the second operand. |
| 5863 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 5864 | if (!success) |
| 5865 | return false; |
| 5866 | |
| 5867 | 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] | 5868 | uint32_t result = val1 | shifted; |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 5869 | |
| 5870 | EmulateInstruction::Context context; |
| 5871 | context.type = EmulateInstruction::eContextImmediate; |
| 5872 | context.SetNoArgs (); |
| 5873 | |
| 5874 | if (!WriteCoreRegOptionalFlags(context, result, Rd, setflags, carry)) |
| 5875 | return false; |
| 5876 | } |
| 5877 | return true; |
| 5878 | } |
| 5879 | |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 5880 | // Reverse Subtract (immediate) subtracts a register value from an immediate value, and writes the result to |
| 5881 | // the destination register. It can optionally update the condition flags based on the result. |
| 5882 | bool |
| 5883 | EmulateInstructionARM::EmulateRSBImm (ARMEncoding encoding) |
| 5884 | { |
| 5885 | #if 0 |
| 5886 | // ARM pseudo code... |
| 5887 | if ConditionPassed() then |
| 5888 | EncodingSpecificOperations(); |
| 5889 | (result, carry, overflow) = AddWithCarry(NOT(R[n]), imm32, '1'); |
| 5890 | if d == 15 then // Can only occur for ARM encoding |
| 5891 | ALUWritePC(result); // setflags is always FALSE here |
| 5892 | else |
| 5893 | R[d] = result; |
| 5894 | if setflags then |
| 5895 | APSR.N = result<31>; |
| 5896 | APSR.Z = IsZeroBit(result); |
| 5897 | APSR.C = carry; |
| 5898 | APSR.V = overflow; |
| 5899 | #endif |
| 5900 | |
| 5901 | bool success = false; |
| 5902 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5903 | if (!success) |
| 5904 | return false; |
| 5905 | |
| 5906 | uint32_t Rd; // the destination register |
| 5907 | uint32_t Rn; // the first operand |
| 5908 | bool setflags; |
| 5909 | uint32_t imm32; // the immediate value to be added to the value obtained from Rn |
| 5910 | switch (encoding) { |
| 5911 | case eEncodingT1: |
| 5912 | Rd = Bits32(opcode, 2, 0); |
| 5913 | Rn = Bits32(opcode, 5, 3); |
| 5914 | setflags = !InITBlock(); |
| 5915 | imm32 = 0; |
| 5916 | break; |
| 5917 | case eEncodingT2: |
| 5918 | Rd = Bits32(opcode, 11, 8); |
| 5919 | Rn = Bits32(opcode, 19, 16); |
| 5920 | setflags = BitIsSet(opcode, 20); |
| 5921 | imm32 = ThumbExpandImm(opcode); // imm32 = ThumbExpandImm(i:imm3:imm8) |
| 5922 | if (BadReg(Rd) || BadReg(Rn)) |
| 5923 | return false; |
| 5924 | break; |
| 5925 | case eEncodingA1: |
| 5926 | Rd = Bits32(opcode, 15, 12); |
| 5927 | Rn = Bits32(opcode, 19, 16); |
| 5928 | setflags = BitIsSet(opcode, 20); |
| 5929 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 5930 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 5931 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 5932 | if (Rd == 15 && setflags) |
| 5933 | return false; |
| 5934 | break; |
| 5935 | default: |
| 5936 | return false; |
| 5937 | } |
| 5938 | // Read the register value from the operand register Rn. |
| 5939 | uint32_t reg_val = ReadCoreReg(Rn, &success); |
| 5940 | if (!success) |
| 5941 | return false; |
| 5942 | |
| 5943 | AddWithCarryResult res = AddWithCarry(~reg_val, imm32, 1); |
| 5944 | |
| 5945 | EmulateInstruction::Context context; |
| 5946 | context.type = EmulateInstruction::eContextImmediate; |
| 5947 | context.SetNoArgs (); |
| 5948 | |
| 5949 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 5950 | return false; |
| 5951 | |
| 5952 | return true; |
| 5953 | } |
| 5954 | |
| 5955 | // Reverse Subtract (register) subtracts a register value from an optionally-shifted register value, and writes the |
| 5956 | // result to the destination register. It can optionally update the condition flags based on the result. |
| 5957 | bool |
| 5958 | EmulateInstructionARM::EmulateRSBReg (ARMEncoding encoding) |
| 5959 | { |
| 5960 | #if 0 |
| 5961 | // ARM pseudo code... |
| 5962 | if ConditionPassed() then |
| 5963 | EncodingSpecificOperations(); |
| 5964 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 5965 | (result, carry, overflow) = AddWithCarry(NOT(R[n]), shifted, '1'); |
| 5966 | if d == 15 then // Can only occur for ARM encoding |
| 5967 | ALUWritePC(result); // setflags is always FALSE here |
| 5968 | else |
| 5969 | R[d] = result; |
| 5970 | if setflags then |
| 5971 | APSR.N = result<31>; |
| 5972 | APSR.Z = IsZeroBit(result); |
| 5973 | APSR.C = carry; |
| 5974 | APSR.V = overflow; |
| 5975 | #endif |
| 5976 | |
| 5977 | bool success = false; |
| 5978 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 5979 | if (!success) |
| 5980 | return false; |
| 5981 | |
| 5982 | uint32_t Rd; // the destination register |
| 5983 | uint32_t Rn; // the first operand |
| 5984 | uint32_t Rm; // the second operand |
| 5985 | bool setflags; |
| 5986 | ARM_ShifterType shift_t; |
| 5987 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 5988 | switch (encoding) { |
| 5989 | case eEncodingT1: |
| 5990 | Rd = Bits32(opcode, 11, 8); |
| 5991 | Rn = Bits32(opcode, 19, 16); |
| 5992 | Rm = Bits32(opcode, 3, 0); |
| 5993 | setflags = BitIsSet(opcode, 20); |
| 5994 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
| 5995 | // if (BadReg(d) || BadReg(m)) then UNPREDICTABLE; |
| 5996 | if (BadReg(Rd) || BadReg(Rn) || BadReg(Rm)) |
| 5997 | return false; |
| 5998 | break; |
| 5999 | case eEncodingA1: |
| 6000 | Rd = Bits32(opcode, 15, 12); |
| 6001 | Rn = Bits32(opcode, 19, 16); |
| 6002 | Rm = Bits32(opcode, 3, 0); |
| 6003 | setflags = BitIsSet(opcode, 20); |
| 6004 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
| 6005 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 6006 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 6007 | if (Rd == 15 && setflags) |
| 6008 | return false; |
| 6009 | break; |
| 6010 | default: |
| 6011 | return false; |
| 6012 | } |
| 6013 | // Read the register value from register Rn. |
| 6014 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 6015 | if (!success) |
| 6016 | return false; |
| 6017 | |
| 6018 | // Read the register value from register Rm. |
| 6019 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 6020 | if (!success) |
| 6021 | return false; |
| 6022 | |
| 6023 | uint32_t shifted = Shift(val2, shift_t, shift_n, APSR_C); |
| 6024 | AddWithCarryResult res = AddWithCarry(~val1, shifted, 1); |
| 6025 | |
| 6026 | EmulateInstruction::Context context; |
| 6027 | context.type = EmulateInstruction::eContextImmediate; |
| 6028 | context.SetNoArgs(); |
| 6029 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 6030 | return false; |
| 6031 | |
| 6032 | return true; |
| 6033 | } |
| 6034 | |
Johnny Chen | 90e607b | 2011-02-23 00:07:09 +0000 | [diff] [blame] | 6035 | // Reverse Subtract with Carry (immediate) subtracts a register value and the value of NOT (Carry flag) from |
| 6036 | // an immediate value, and writes the result to the destination register. It can optionally update the condition |
| 6037 | // flags based on the result. |
| 6038 | bool |
| 6039 | EmulateInstructionARM::EmulateRSCImm (ARMEncoding encoding) |
| 6040 | { |
| 6041 | #if 0 |
| 6042 | // ARM pseudo code... |
| 6043 | if ConditionPassed() then |
| 6044 | EncodingSpecificOperations(); |
| 6045 | (result, carry, overflow) = AddWithCarry(NOT(R[n]), imm32, APSR.C); |
| 6046 | if d == 15 then |
| 6047 | ALUWritePC(result); // setflags is always FALSE here |
| 6048 | else |
| 6049 | R[d] = result; |
| 6050 | if setflags then |
| 6051 | APSR.N = result<31>; |
| 6052 | APSR.Z = IsZeroBit(result); |
| 6053 | APSR.C = carry; |
| 6054 | APSR.V = overflow; |
| 6055 | #endif |
| 6056 | |
| 6057 | bool success = false; |
| 6058 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 6059 | if (!success) |
| 6060 | return false; |
| 6061 | |
| 6062 | uint32_t Rd; // the destination register |
| 6063 | uint32_t Rn; // the first operand |
| 6064 | bool setflags; |
| 6065 | uint32_t imm32; // the immediate value to be added to the value obtained from Rn |
| 6066 | switch (encoding) { |
| 6067 | case eEncodingA1: |
| 6068 | Rd = Bits32(opcode, 15, 12); |
| 6069 | Rn = Bits32(opcode, 19, 16); |
| 6070 | setflags = BitIsSet(opcode, 20); |
| 6071 | imm32 = ARMExpandImm(opcode); // imm32 = ARMExpandImm(imm12) |
| 6072 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 6073 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 6074 | if (Rd == 15 && setflags) |
| 6075 | return false; |
| 6076 | break; |
| 6077 | default: |
| 6078 | return false; |
| 6079 | } |
| 6080 | // Read the register value from the operand register Rn. |
| 6081 | uint32_t reg_val = ReadCoreReg(Rn, &success); |
| 6082 | if (!success) |
| 6083 | return false; |
| 6084 | |
| 6085 | AddWithCarryResult res = AddWithCarry(~reg_val, imm32, APSR_C); |
| 6086 | |
| 6087 | EmulateInstruction::Context context; |
| 6088 | context.type = EmulateInstruction::eContextImmediate; |
| 6089 | context.SetNoArgs (); |
| 6090 | |
| 6091 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 6092 | return false; |
| 6093 | |
| 6094 | return true; |
| 6095 | } |
| 6096 | |
| 6097 | // Reverse Subtract with Carry (register) subtracts a register value and the value of NOT (Carry flag) from an |
| 6098 | // optionally-shifted register value, and writes the result to the destination register. It can optionally update the |
| 6099 | // condition flags based on the result. |
| 6100 | bool |
| 6101 | EmulateInstructionARM::EmulateRSCReg (ARMEncoding encoding) |
| 6102 | { |
| 6103 | #if 0 |
| 6104 | // ARM pseudo code... |
| 6105 | if ConditionPassed() then |
| 6106 | EncodingSpecificOperations(); |
| 6107 | shifted = Shift(R[m], shift_t, shift_n, APSR.C); |
| 6108 | (result, carry, overflow) = AddWithCarry(NOT(R[n]), shifted, APSR.C); |
| 6109 | if d == 15 then |
| 6110 | ALUWritePC(result); // setflags is always FALSE here |
| 6111 | else |
| 6112 | R[d] = result; |
| 6113 | if setflags then |
| 6114 | APSR.N = result<31>; |
| 6115 | APSR.Z = IsZeroBit(result); |
| 6116 | APSR.C = carry; |
| 6117 | APSR.V = overflow; |
| 6118 | #endif |
| 6119 | |
| 6120 | bool success = false; |
| 6121 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 6122 | if (!success) |
| 6123 | return false; |
| 6124 | |
| 6125 | uint32_t Rd; // the destination register |
| 6126 | uint32_t Rn; // the first operand |
| 6127 | uint32_t Rm; // the second operand |
| 6128 | bool setflags; |
| 6129 | ARM_ShifterType shift_t; |
| 6130 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 6131 | switch (encoding) { |
| 6132 | case eEncodingA1: |
| 6133 | Rd = Bits32(opcode, 15, 12); |
| 6134 | Rn = Bits32(opcode, 19, 16); |
| 6135 | Rm = Bits32(opcode, 3, 0); |
| 6136 | setflags = BitIsSet(opcode, 20); |
| 6137 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
| 6138 | // if Rd == '1111' && S == '1' then SEE SUBS PC, LR and related instructions; |
| 6139 | // TODO: Emulate SUBS PC, LR and related instructions. |
| 6140 | if (Rd == 15 && setflags) |
| 6141 | return false; |
| 6142 | break; |
| 6143 | default: |
| 6144 | return false; |
| 6145 | } |
| 6146 | // Read the register value from register Rn. |
| 6147 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 6148 | if (!success) |
| 6149 | return false; |
| 6150 | |
| 6151 | // Read the register value from register Rm. |
| 6152 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 6153 | if (!success) |
| 6154 | return false; |
| 6155 | |
| 6156 | uint32_t shifted = Shift(val2, shift_t, shift_n, APSR_C); |
| 6157 | AddWithCarryResult res = AddWithCarry(~val1, shifted, APSR_C); |
| 6158 | |
| 6159 | EmulateInstruction::Context context; |
| 6160 | context.type = EmulateInstruction::eContextImmediate; |
| 6161 | context.SetNoArgs(); |
| 6162 | if (!WriteCoreRegOptionalFlags(context, res.result, Rd, setflags, res.carry_out, res.overflow)) |
| 6163 | return false; |
| 6164 | |
| 6165 | return true; |
| 6166 | } |
| 6167 | |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 6168 | // Test Equivalence (immediate) performs a bitwise exclusive OR operation on a register value and an |
| 6169 | // immediate value. It updates the condition flags based on the result, and discards the result. |
| 6170 | bool |
| 6171 | EmulateInstructionARM::EmulateTEQImm (ARMEncoding encoding) |
| 6172 | { |
| 6173 | #if 0 |
| 6174 | // ARM pseudo code... |
| 6175 | if ConditionPassed() then |
| 6176 | EncodingSpecificOperations(); |
| 6177 | result = R[n] EOR imm32; |
| 6178 | APSR.N = result<31>; |
| 6179 | APSR.Z = IsZeroBit(result); |
| 6180 | APSR.C = carry; |
| 6181 | // APSR.V unchanged |
| 6182 | #endif |
| 6183 | |
| 6184 | bool success = false; |
| 6185 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 6186 | if (!success) |
| 6187 | return false; |
| 6188 | |
| 6189 | if (ConditionPassed()) |
| 6190 | { |
| 6191 | uint32_t Rn; |
| 6192 | uint32_t imm32; // the immediate value to be ANDed to the value obtained from Rn |
| 6193 | uint32_t carry; // the carry bit after ARM/Thumb Expand operation |
| 6194 | switch (encoding) |
| 6195 | { |
| 6196 | case eEncodingT1: |
| 6197 | Rn = Bits32(opcode, 19, 16); |
| 6198 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ThumbExpandImm(i:imm3:imm8, APSR.C) |
| 6199 | if (BadReg(Rn)) |
| 6200 | return false; |
| 6201 | break; |
| 6202 | case eEncodingA1: |
| 6203 | Rn = Bits32(opcode, 19, 16); |
| 6204 | imm32 = ARMExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ARMExpandImm(imm12, APSR.C) |
| 6205 | break; |
| 6206 | default: |
| 6207 | return false; |
| 6208 | } |
| 6209 | |
| 6210 | // Read the first operand. |
| 6211 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 6212 | if (!success) |
| 6213 | return false; |
| 6214 | |
| 6215 | uint32_t result = val1 ^ imm32; |
| 6216 | |
| 6217 | EmulateInstruction::Context context; |
| 6218 | context.type = EmulateInstruction::eContextImmediate; |
| 6219 | context.SetNoArgs (); |
| 6220 | |
| 6221 | if (!WriteFlags(context, result, carry)) |
| 6222 | return false; |
| 6223 | } |
| 6224 | return true; |
| 6225 | } |
| 6226 | |
| 6227 | // Test Equivalence (register) performs a bitwise exclusive OR operation on a register value and an |
| 6228 | // optionally-shifted register value. It updates the condition flags based on the result, and discards |
| 6229 | // the result. |
| 6230 | bool |
| 6231 | EmulateInstructionARM::EmulateTEQReg (ARMEncoding encoding) |
| 6232 | { |
| 6233 | #if 0 |
| 6234 | // ARM pseudo code... |
| 6235 | if ConditionPassed() then |
| 6236 | EncodingSpecificOperations(); |
| 6237 | (shifted, carry) = Shift_C(R[m], shift_t, shift_n, APSR.C); |
| 6238 | result = R[n] EOR shifted; |
| 6239 | APSR.N = result<31>; |
| 6240 | APSR.Z = IsZeroBit(result); |
| 6241 | APSR.C = carry; |
| 6242 | // APSR.V unchanged |
| 6243 | #endif |
| 6244 | |
| 6245 | bool success = false; |
| 6246 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 6247 | if (!success) |
| 6248 | return false; |
| 6249 | |
| 6250 | if (ConditionPassed()) |
| 6251 | { |
| 6252 | uint32_t Rn, Rm; |
| 6253 | ARM_ShifterType shift_t; |
| 6254 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 6255 | uint32_t carry; |
| 6256 | switch (encoding) |
| 6257 | { |
| 6258 | case eEncodingT1: |
| 6259 | Rn = Bits32(opcode, 19, 16); |
| 6260 | Rm = Bits32(opcode, 3, 0); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 6261 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 6262 | if (BadReg(Rn) || BadReg(Rm)) |
| 6263 | return false; |
| 6264 | break; |
| 6265 | case eEncodingA1: |
| 6266 | Rn = Bits32(opcode, 19, 16); |
| 6267 | Rm = Bits32(opcode, 3, 0); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 6268 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 6269 | break; |
| 6270 | default: |
| 6271 | return false; |
| 6272 | } |
| 6273 | |
| 6274 | // Read the first operand. |
| 6275 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 6276 | if (!success) |
| 6277 | return false; |
| 6278 | |
| 6279 | // Read the second operand. |
| 6280 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 6281 | if (!success) |
| 6282 | return false; |
| 6283 | |
| 6284 | uint32_t shifted = Shift_C(val2, shift_t, shift_n, APSR_C, carry); |
| 6285 | uint32_t result = val1 ^ shifted; |
| 6286 | |
| 6287 | EmulateInstruction::Context context; |
| 6288 | context.type = EmulateInstruction::eContextImmediate; |
| 6289 | context.SetNoArgs (); |
| 6290 | |
| 6291 | if (!WriteFlags(context, result, carry)) |
| 6292 | return false; |
| 6293 | } |
| 6294 | return true; |
| 6295 | } |
| 6296 | |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 6297 | // Test (immediate) performs a bitwise AND operation on a register value and an immediate value. |
| 6298 | // It updates the condition flags based on the result, and discards the result. |
| 6299 | bool |
| 6300 | EmulateInstructionARM::EmulateTSTImm (ARMEncoding encoding) |
| 6301 | { |
| 6302 | #if 0 |
| 6303 | // ARM pseudo code... |
| 6304 | if ConditionPassed() then |
| 6305 | EncodingSpecificOperations(); |
| 6306 | result = R[n] AND imm32; |
| 6307 | APSR.N = result<31>; |
| 6308 | APSR.Z = IsZeroBit(result); |
| 6309 | APSR.C = carry; |
| 6310 | // APSR.V unchanged |
| 6311 | #endif |
| 6312 | |
| 6313 | bool success = false; |
| 6314 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 6315 | if (!success) |
| 6316 | return false; |
| 6317 | |
| 6318 | if (ConditionPassed()) |
| 6319 | { |
| 6320 | uint32_t Rn; |
| 6321 | uint32_t imm32; // the immediate value to be ANDed to the value obtained from Rn |
| 6322 | uint32_t carry; // the carry bit after ARM/Thumb Expand operation |
| 6323 | switch (encoding) |
| 6324 | { |
| 6325 | case eEncodingT1: |
| 6326 | Rn = Bits32(opcode, 19, 16); |
| 6327 | imm32 = ThumbExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ThumbExpandImm(i:imm3:imm8, APSR.C) |
| 6328 | if (BadReg(Rn)) |
| 6329 | return false; |
| 6330 | break; |
| 6331 | case eEncodingA1: |
| 6332 | Rn = Bits32(opcode, 19, 16); |
| 6333 | imm32 = ARMExpandImm_C(opcode, APSR_C, carry); // (imm32, carry) = ARMExpandImm(imm12, APSR.C) |
| 6334 | break; |
| 6335 | default: |
| 6336 | return false; |
| 6337 | } |
| 6338 | |
| 6339 | // Read the first operand. |
| 6340 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 6341 | if (!success) |
| 6342 | return false; |
| 6343 | |
| 6344 | uint32_t result = val1 & imm32; |
| 6345 | |
| 6346 | EmulateInstruction::Context context; |
| 6347 | context.type = EmulateInstruction::eContextImmediate; |
| 6348 | context.SetNoArgs (); |
| 6349 | |
| 6350 | if (!WriteFlags(context, result, carry)) |
| 6351 | return false; |
| 6352 | } |
| 6353 | return true; |
| 6354 | } |
| 6355 | |
| 6356 | // Test (register) performs a bitwise AND operation on a register value and an optionally-shifted register value. |
| 6357 | // It updates the condition flags based on the result, and discards the result. |
| 6358 | bool |
| 6359 | EmulateInstructionARM::EmulateTSTReg (ARMEncoding encoding) |
| 6360 | { |
| 6361 | #if 0 |
| 6362 | // ARM pseudo code... |
| 6363 | if ConditionPassed() then |
| 6364 | EncodingSpecificOperations(); |
| 6365 | (shifted, carry) = Shift_C(R[m], shift_t, shift_n, APSR.C); |
| 6366 | result = R[n] AND shifted; |
| 6367 | APSR.N = result<31>; |
| 6368 | APSR.Z = IsZeroBit(result); |
| 6369 | APSR.C = carry; |
| 6370 | // APSR.V unchanged |
| 6371 | #endif |
| 6372 | |
| 6373 | bool success = false; |
| 6374 | const uint32_t opcode = OpcodeAsUnsigned (&success); |
| 6375 | if (!success) |
| 6376 | return false; |
| 6377 | |
| 6378 | if (ConditionPassed()) |
| 6379 | { |
| 6380 | uint32_t Rn, Rm; |
| 6381 | ARM_ShifterType shift_t; |
| 6382 | uint32_t shift_n; // the shift applied to the value read from Rm |
| 6383 | uint32_t carry; |
| 6384 | switch (encoding) |
| 6385 | { |
| 6386 | case eEncodingT1: |
| 6387 | Rn = Bits32(opcode, 2, 0); |
| 6388 | Rm = Bits32(opcode, 5, 3); |
| 6389 | shift_t = SRType_LSL; |
| 6390 | shift_n = 0; |
Johnny Chen | ed32e7c | 2011-02-22 23:42:58 +0000 | [diff] [blame] | 6391 | break; |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 6392 | case eEncodingT2: |
| 6393 | Rn = Bits32(opcode, 19, 16); |
| 6394 | Rm = Bits32(opcode, 3, 0); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 6395 | shift_n = DecodeImmShiftThumb(opcode, shift_t); |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 6396 | if (BadReg(Rn) || BadReg(Rm)) |
| 6397 | return false; |
| 6398 | break; |
| 6399 | case eEncodingA1: |
| 6400 | Rn = Bits32(opcode, 19, 16); |
| 6401 | Rm = Bits32(opcode, 3, 0); |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 6402 | shift_n = DecodeImmShiftARM(opcode, shift_t); |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 6403 | break; |
| 6404 | default: |
| 6405 | return false; |
| 6406 | } |
| 6407 | |
| 6408 | // Read the first operand. |
| 6409 | uint32_t val1 = ReadCoreReg(Rn, &success); |
| 6410 | if (!success) |
| 6411 | return false; |
| 6412 | |
| 6413 | // Read the second operand. |
| 6414 | uint32_t val2 = ReadCoreReg(Rm, &success); |
| 6415 | if (!success) |
| 6416 | return false; |
| 6417 | |
| 6418 | uint32_t shifted = Shift_C(val2, shift_t, shift_n, APSR_C, carry); |
| 6419 | uint32_t result = val1 & shifted; |
| 6420 | |
| 6421 | EmulateInstruction::Context context; |
| 6422 | context.type = EmulateInstruction::eContextImmediate; |
| 6423 | context.SetNoArgs (); |
| 6424 | |
| 6425 | if (!WriteFlags(context, result, carry)) |
| 6426 | return false; |
| 6427 | } |
| 6428 | return true; |
| 6429 | } |
| 6430 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6431 | EmulateInstructionARM::ARMOpcode* |
| 6432 | EmulateInstructionARM::GetARMOpcodeForInstruction (const uint32_t opcode) |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 6433 | { |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6434 | static ARMOpcode |
| 6435 | g_arm_opcodes[] = |
| 6436 | { |
| 6437 | //---------------------------------------------------------------------- |
| 6438 | // Prologue instructions |
| 6439 | //---------------------------------------------------------------------- |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 6440 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6441 | // push register(s) |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 6442 | { 0x0fff0000, 0x092d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePUSH, "push <registers>" }, |
| 6443 | { 0x0fff0fff, 0x052d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePUSH, "push <register>" }, |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 6444 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6445 | // set r7 to point to a stack offset |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 6446 | { 0x0ffff000, 0x028d7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateADDRdSPImm, "add r7, sp, #<const>" }, |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 6447 | { 0x0ffff000, 0x024c7000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSUBR7IPImm, "sub r7, ip, #<const>"}, |
Johnny Chen | e7cf420 | 2011-02-10 18:13:23 +0000 | [diff] [blame] | 6448 | // copy the stack pointer to ip |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 6449 | { 0x0fffffff, 0x01a0c00d, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMOVRdSP, "mov ip, sp" }, |
| 6450 | { 0x0ffff000, 0x028dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateADDRdSPImm, "add ip, sp, #<const>" }, |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 6451 | { 0x0ffff000, 0x024dc000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSUBIPSPImm, "sub ip, sp, #<const>"}, |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 6452 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6453 | // adjust the stack pointer |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 6454 | { 0x0ffff000, 0x024dd000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSUBSPImm, "sub sp, sp, #<const>"}, |
Johnny Chen | ce1ca77 | 2011-01-25 01:13:00 +0000 | [diff] [blame] | 6455 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6456 | // push one register |
| 6457 | // if Rn == '1101' && imm12 == '000000000100' then SEE PUSH; |
Johnny Chen | c28a76d | 2011-02-01 18:51:48 +0000 | [diff] [blame] | 6458 | { 0x0fff0000, 0x052d0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTRRtSP, "str Rt, [sp, #-imm12]!" }, |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 6459 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6460 | // vector push consecutive extension register(s) |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 6461 | { 0x0fbf0f00, 0x0d2d0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"}, |
| 6462 | { 0x0fbf0f00, 0x0d2d0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"}, |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 6463 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6464 | //---------------------------------------------------------------------- |
Johnny Chen | 587a0a4 | 2011-02-01 18:35:28 +0000 | [diff] [blame] | 6465 | // Epilogue instructions |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6466 | //---------------------------------------------------------------------- |
Johnny Chen | ef85e91 | 2011-01-31 23:07:40 +0000 | [diff] [blame] | 6467 | |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 6468 | { 0x0fff0000, 0x08bd0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulatePOP, "pop <registers>"}, |
| 6469 | { 0x0fff0fff, 0x049d0004, ARMvAll, eEncodingA2, eSize32, &EmulateInstructionARM::EmulatePOP, "pop <register>"}, |
Johnny Chen | 9b8d783 | 2011-02-02 01:13:56 +0000 | [diff] [blame] | 6470 | { 0x0fbf0f00, 0x0cbd0b00, ARMV6T2_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"}, |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 6471 | { 0x0fbf0f00, 0x0cbd0a00, ARMV6T2_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"}, |
| 6472 | |
| 6473 | //---------------------------------------------------------------------- |
| 6474 | // Supervisor Call (previously Software Interrupt) |
| 6475 | //---------------------------------------------------------------------- |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 6476 | { 0x0f000000, 0x0f000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSVC, "svc #imm24"}, |
| 6477 | |
| 6478 | //---------------------------------------------------------------------- |
| 6479 | // Branch instructions |
| 6480 | //---------------------------------------------------------------------- |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 6481 | { 0x0f000000, 0x0a000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSVC, "b #imm24"}, |
Johnny Chen | 383d629 | 2011-02-11 21:23:32 +0000 | [diff] [blame] | 6482 | // To resolve ambiguity, "blx <label>" should come before "bl <label>". |
| 6483 | { 0xfe000000, 0xfa000000, ARMV5_ABOVE, eEncodingA2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"}, |
| 6484 | { 0x0f000000, 0x0b000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "bl <label>"}, |
| 6485 | { 0x0ffffff0, 0x012fff30, ARMV5_ABOVE, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"}, |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 6486 | // for example, "bx lr" |
| 6487 | { 0x0ffffff0, 0x012fff10, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBXRm, "bx <Rm>"}, |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 6488 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 6489 | //---------------------------------------------------------------------- |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 6490 | // Data-processing instructions |
| 6491 | //---------------------------------------------------------------------- |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 6492 | // adc (immediate) |
| 6493 | { 0x0fe00000, 0x02a00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateADCImm, "adc{s}<c> <Rd>, <Rn>, #const"}, |
| 6494 | // adc (register) |
| 6495 | { 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] | 6496 | // add (immediate) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 6497 | { 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] | 6498 | // add (register) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 6499 | { 0x0fe00010, 0x00800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateADDReg, "add{s}<c> <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 6500 | // and (immediate) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 6501 | { 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] | 6502 | // and (register) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 6503 | { 0x0fe00010, 0x00000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateANDReg, "and{s}<c> <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 6504 | // eor (immediate) |
| 6505 | { 0x0fe00000, 0x02200000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateEORImm, "eor{s}<c> <Rd>, <Rn>, #const"}, |
| 6506 | // eor (register) |
| 6507 | { 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] | 6508 | // orr (immediate) |
| 6509 | { 0x0fe00000, 0x03800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateORRImm, "orr{s}<c> <Rd>, <Rn>, #const"}, |
| 6510 | // orr (register) |
| 6511 | { 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] | 6512 | // rsb (immediate) |
| 6513 | { 0x0fe00000, 0x02600000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRSBImm, "rsb{s}<c> <Rd>, <Rn>, #<const>"}, |
| 6514 | // rsb (register) |
| 6515 | { 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] | 6516 | // rsc (immediate) |
| 6517 | { 0x0fe00000, 0x02e00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRSCImm, "rsc{s}<c> <Rd>, <Rn>, #<const>"}, |
| 6518 | // rsc (register) |
| 6519 | { 0x0fe00010, 0x00e00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRSCReg, "rsc{s}<c> <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 6520 | // teq (immediate) |
| 6521 | { 0x0ff0f000, 0x03300000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateTEQImm, "teq<c> <Rn>, #const"}, |
| 6522 | // teq (register) |
| 6523 | { 0x0ff0f010, 0x01300000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateTEQReg, "teq<c> <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 6524 | // tst (immediate) |
| 6525 | { 0x0ff0f000, 0x03100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateTSTImm, "tst<c> <Rn>, #const"}, |
| 6526 | // tst (register) |
| 6527 | { 0x0ff0f010, 0x01100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateTSTReg, "tst<c> <Rn>, <Rm> {,<shift>}"}, |
| 6528 | |
| 6529 | |
Johnny Chen | d642a6a | 2011-02-22 01:01:03 +0000 | [diff] [blame] | 6530 | // mvn (immediate) |
| 6531 | { 0x0fef0000, 0x03e00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateMVNImm, "mvn{s}<c> <Rd>, #<const>"}, |
| 6532 | // mvn (register) |
| 6533 | { 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] | 6534 | // cmn (immediate) |
| 6535 | { 0x0ff0f000, 0x03700000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateCMNImm, "cmn<c> <Rn>, #<const>"}, |
| 6536 | // cmn (register) |
| 6537 | { 0x0ff0f010, 0x01700000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateCMNReg, "cmn<c> <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 6538 | // cmp (immediate) |
| 6539 | { 0x0ff0f000, 0x03500000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateCMPImm, "cmp<c> <Rn>, #<const>"}, |
| 6540 | // cmp (register) |
| 6541 | { 0x0ff0f010, 0x01500000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateCMPReg, "cmp<c> <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 6542 | // asr (immediate) |
| 6543 | { 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] | 6544 | // asr (register) |
Johnny Chen | e7f8953 | 2011-02-15 23:22:46 +0000 | [diff] [blame] | 6545 | { 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] | 6546 | // lsl (immediate) |
| 6547 | { 0x0fef0070, 0x01a00000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLSLImm, "lsl{s}<c> <Rd>, <Rm>, #imm"}, |
| 6548 | // lsl (register) |
| 6549 | { 0x0fef00f0, 0x01a00010, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLSLReg, "lsl{s}<c> <Rd>, <Rn>, <Rm>"}, |
| 6550 | // lsr (immediate) |
| 6551 | { 0x0fef0070, 0x01a00020, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLSRImm, "lsr{s}<c> <Rd>, <Rm>, #imm"}, |
| 6552 | // lsr (register) |
| 6553 | { 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] | 6554 | // rrx is a special case encoding of ror (immediate) |
| 6555 | { 0x0fef0ff0, 0x01a00060, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRRX, "rrx{s}<c> <Rd>, <Rm>"}, |
| 6556 | // ror (immediate) |
| 6557 | { 0x0fef0070, 0x01a00060, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRORImm, "ror{s}<c> <Rd>, <Rm>, #imm"}, |
| 6558 | // ror (register) |
| 6559 | { 0x0fef00f0, 0x01a00070, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateRORReg, "ror{s}<c> <Rd>, <Rn>, <Rm>"}, |
Johnny Chen | 28070c3 | 2011-02-12 01:27:26 +0000 | [diff] [blame] | 6560 | |
| 6561 | //---------------------------------------------------------------------- |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 6562 | // Load instructions |
| 6563 | //---------------------------------------------------------------------- |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 6564 | { 0x0fd00000, 0x08900000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDM, "ldm<c> <Rn>{!} <registers>" }, |
Caroline Tice | 713c266 | 2011-02-11 17:59:55 +0000 | [diff] [blame] | 6565 | { 0x0fd00000, 0x08100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMDA, "ldmda<c> <Rn>{!} <registers>" }, |
Caroline Tice | 85aab33 | 2011-02-08 23:56:10 +0000 | [diff] [blame] | 6566 | { 0x0fd00000, 0x09100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMDB, "ldmdb<c> <Rn>{!} <registers>" }, |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 6567 | { 0x0fd00000, 0x09900000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDMIB, "ldmib<c> <Rn<{!} <registers>" }, |
Caroline Tice | 4d729c5 | 2011-02-18 00:55:53 +0000 | [diff] [blame] | 6568 | { 0x0e500000, 0x04100000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRImmediateARM, "ldr<c> <Rt> [<Rn> {#+/-<imm12>}]" }, |
Caroline Tice | fe47911 | 2011-02-18 18:52:37 +0000 | [diff] [blame] | 6569 | { 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] | 6570 | { 0x0e5f0000, 0x045f0000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRBLiteral, "ldrb<c> <Rt>, [...]"}, |
| 6571 | { 0xfe500010, 0x06500000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateLDRBRegister, "ldrb<c> <Rt>, [<Rn>,+/-<Rm>{, <shift>}]{!}" }, |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 6572 | |
| 6573 | //---------------------------------------------------------------------- |
| 6574 | // Store instructions |
| 6575 | //---------------------------------------------------------------------- |
Caroline Tice | 1511f50 | 2011-02-15 00:19:42 +0000 | [diff] [blame] | 6576 | { 0x0fd00000, 0x08800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTM, "stm<c> <Rn>{!} <registers>" }, |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 6577 | { 0x0fd00000, 0x08000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTMDA, "stmda<c> <Rn>{!} <registers>" }, |
Caroline Tice | af55656 | 2011-02-15 18:42:15 +0000 | [diff] [blame] | 6578 | { 0x0fd00000, 0x09000000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTMDB, "stmdb<c> <Rn>{!} <registers>" }, |
Caroline Tice | 3fd63e9 | 2011-02-16 00:33:43 +0000 | [diff] [blame] | 6579 | { 0x0fd00000, 0x09800000, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateSTMIB, "stmib<c> <Rn>{!} <registers>" }, |
| 6580 | { 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] | 6581 | |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 6582 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6583 | }; |
| 6584 | static const size_t k_num_arm_opcodes = sizeof(g_arm_opcodes)/sizeof(ARMOpcode); |
| 6585 | |
| 6586 | for (size_t i=0; i<k_num_arm_opcodes; ++i) |
| 6587 | { |
| 6588 | if ((g_arm_opcodes[i].mask & opcode) == g_arm_opcodes[i].value) |
| 6589 | return &g_arm_opcodes[i]; |
| 6590 | } |
| 6591 | return NULL; |
| 6592 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 6593 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6594 | |
| 6595 | EmulateInstructionARM::ARMOpcode* |
| 6596 | EmulateInstructionARM::GetThumbOpcodeForInstruction (const uint32_t opcode) |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 6597 | { |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 6598 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6599 | static ARMOpcode |
| 6600 | g_thumb_opcodes[] = |
| 6601 | { |
| 6602 | //---------------------------------------------------------------------- |
| 6603 | // Prologue instructions |
| 6604 | //---------------------------------------------------------------------- |
Johnny Chen | bcec3af | 2011-01-27 01:26:19 +0000 | [diff] [blame] | 6605 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6606 | // push register(s) |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 6607 | { 0xfffffe00, 0x0000b400, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePUSH, "push <registers>" }, |
| 6608 | { 0xffff0000, 0xe92d0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePUSH, "push.w <registers>" }, |
| 6609 | { 0xffff0fff, 0xf84d0d04, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePUSH, "push.w <register>" }, |
Johnny Chen | 788e055 | 2011-01-27 22:52:23 +0000 | [diff] [blame] | 6610 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6611 | // set r7 to point to a stack offset |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 6612 | { 0xffffff00, 0x0000af00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateADDRdSPImm, "add r7, sp, #imm" }, |
Johnny Chen | e7cf420 | 2011-02-10 18:13:23 +0000 | [diff] [blame] | 6613 | // copy the stack pointer to r7 |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 6614 | { 0xffffffff, 0x0000466f, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMOVRdSP, "mov r7, sp" }, |
Johnny Chen | e7cf420 | 2011-02-10 18:13:23 +0000 | [diff] [blame] | 6615 | // 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] | 6616 | { 0xffffffc0, 0x00004640, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMOVLowHigh, "mov r0-r7, r8-r15" }, |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 6617 | |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 6618 | // PC-relative load into register (see also EmulateADDSPRm) |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 6619 | { 0xfffff800, 0x00004800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRtPCRelative, "ldr <Rt>, [PC, #imm]"}, |
Johnny Chen | 799dfd0 | 2011-01-26 23:14:33 +0000 | [diff] [blame] | 6620 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6621 | // adjust the stack pointer |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 6622 | { 0xffffff87, 0x00004485, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateADDSPRm, "add sp, <Rm>"}, |
| 6623 | { 0xffffff80, 0x0000b080, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSUBSPImm, "add sp, sp, #imm"}, |
| 6624 | { 0xfbef8f00, 0xf1ad0d00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSUBSPImm, "sub.w sp, sp, #<const>"}, |
| 6625 | { 0xfbff8f00, 0xf2ad0d00, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSUBSPImm, "subw sp, sp, #imm12"}, |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 6626 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6627 | // vector push consecutive extension register(s) |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 6628 | { 0xffbf0f00, 0xed2d0b00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.64 <list>"}, |
| 6629 | { 0xffbf0f00, 0xed2d0a00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPUSH, "vpush.32 <list>"}, |
Johnny Chen | fdd179e | 2011-01-31 20:09:28 +0000 | [diff] [blame] | 6630 | |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6631 | //---------------------------------------------------------------------- |
| 6632 | // Epilogue instructions |
| 6633 | //---------------------------------------------------------------------- |
Johnny Chen | 347320d | 2011-01-24 23:40:59 +0000 | [diff] [blame] | 6634 | |
Johnny Chen | 864a8e8 | 2011-02-18 00:07:39 +0000 | [diff] [blame] | 6635 | { 0xffffff80, 0x0000b000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateADDSPImm, "add sp, #imm"}, |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 6636 | { 0xfffffe00, 0x0000bc00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulatePOP, "pop <registers>"}, |
| 6637 | { 0xffff0000, 0xe8bd0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulatePOP, "pop.w <registers>" }, |
| 6638 | { 0xffff0fff, 0xf85d0d04, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulatePOP, "pop.w <register>" }, |
Johnny Chen | d6c13f0 | 2011-02-08 20:36:34 +0000 | [diff] [blame] | 6639 | { 0xffbf0f00, 0xecbd0b00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.64 <list>"}, |
| 6640 | { 0xffbf0f00, 0xecbd0a00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateVPOP, "vpop.32 <list>"}, |
Johnny Chen | b77be41 | 2011-02-04 00:40:18 +0000 | [diff] [blame] | 6641 | |
| 6642 | //---------------------------------------------------------------------- |
| 6643 | // Supervisor Call (previously Software Interrupt) |
| 6644 | //---------------------------------------------------------------------- |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 6645 | { 0xffffff00, 0x0000df00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSVC, "svc #imm8"}, |
| 6646 | |
| 6647 | //---------------------------------------------------------------------- |
| 6648 | // If Then makes up to four following instructions conditional. |
| 6649 | //---------------------------------------------------------------------- |
Johnny Chen | 3b620b3 | 2011-02-07 20:11:47 +0000 | [diff] [blame] | 6650 | { 0xffffff00, 0x0000bf00, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateIT, "it{<x>{<y>{<z>}}} <firstcond>"}, |
| 6651 | |
| 6652 | //---------------------------------------------------------------------- |
| 6653 | // Branch instructions |
| 6654 | //---------------------------------------------------------------------- |
| 6655 | // To resolve ambiguity, "b<c> #imm8" should come after "svc #imm8". |
| 6656 | { 0xfffff000, 0x0000d000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateB, "b<c> #imm8 (outside IT)"}, |
| 6657 | { 0xffff8000, 0x0000e000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateB, "b #imm11 (outside or last in IT)"}, |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 6658 | { 0xf800d000, 0xf0008000, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateB, "b<c>.w #imm8 (outside IT)"}, |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 6659 | { 0xf800d000, 0xf0009000, ARMV6T2_ABOVE, eEncodingT4, eSize32, &EmulateInstructionARM::EmulateB, "b.w #imm8 (outside or last in IT)"}, |
Johnny Chen | 383d629 | 2011-02-11 21:23:32 +0000 | [diff] [blame] | 6660 | // J1 == J2 == 1 |
| 6661 | { 0xf800f800, 0xf000f800, ARMV4T_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "bl <label>"}, |
| 6662 | // J1 == J2 == 1 |
| 6663 | { 0xf800e800, 0xf000e800, ARMV5_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateBLXImmediate, "blx <label>"}, |
| 6664 | { 0xffffff87, 0x00004780, ARMV5_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateBLXRm, "blx <Rm>"}, |
Johnny Chen | ab3b351 | 2011-02-12 00:10:51 +0000 | [diff] [blame] | 6665 | // for example, "bx lr" |
| 6666 | { 0xffffff87, 0x00004700, ARMvAll, eEncodingA1, eSize32, &EmulateInstructionARM::EmulateBXRm, "bx <Rm>"}, |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 6667 | // compare and branch |
| 6668 | { 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] | 6669 | // table branch byte |
| 6670 | { 0xfff0fff0, 0xe8d0f000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateTB, "tbb<c> <Rn>, <Rm>"}, |
| 6671 | // table branch halfword |
| 6672 | { 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] | 6673 | |
| 6674 | //---------------------------------------------------------------------- |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 6675 | // Data-processing instructions |
| 6676 | //---------------------------------------------------------------------- |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 6677 | // adc (immediate) |
| 6678 | { 0xfbe08000, 0xf1400000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateADCImm, "adc{s}<c> <Rd>, <Rn>, #<const>"}, |
| 6679 | // adc (register) |
| 6680 | { 0xffffffc0, 0x00004140, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateADCReg, "adcs|adc<c> <Rdn>, <Rm>"}, |
| 6681 | { 0xffe08000, 0xeb400000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateADCReg, "adc{s}<c>.w <Rd>, <Rn>, <Rm> {,<shift>}"}, |
| 6682 | // add (register) |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 6683 | { 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] | 6684 | // 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] | 6685 | { 0xffffff00, 0x00004400, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateADDReg, "add<c> <Rdn>, <Rm>"}, |
Johnny Chen | e97c0d5 | 2011-02-18 19:32:20 +0000 | [diff] [blame] | 6686 | // and (immediate) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 6687 | { 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] | 6688 | // and (register) |
| 6689 | { 0xffffffc0, 0x00004000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateANDReg, "ands|and<c> <Rdn>, <Rm>"}, |
| 6690 | { 0xffe08000, 0xea000000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateANDReg, "and{s}<c>.w <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 6691 | // eor (immediate) |
| 6692 | { 0xfbe08000, 0xf0800000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateEORImm, "eor{s}<c> <Rd>, <Rn>, #<const>"}, |
| 6693 | // eor (register) |
| 6694 | { 0xffffffc0, 0x00004040, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateEORReg, "eors|eor<c> <Rdn>, <Rm>"}, |
| 6695 | { 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] | 6696 | // orr (immediate) |
| 6697 | { 0xfbe08000, 0xf0400000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateORRImm, "orr{s}<c> <Rd>, <Rn>, #<const>"}, |
| 6698 | // orr (register) |
| 6699 | { 0xffffffc0, 0x00004300, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateORRReg, "orrs|orr<c> <Rdn>, <Rm>"}, |
| 6700 | { 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] | 6701 | // rsb (immediate) |
| 6702 | { 0xffffffc0, 0x00004240, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateRSBImm, "rsbs|rsb<c> <Rd>, <Rn>, #0"}, |
| 6703 | { 0xfbe08000, 0xf1c00000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateRSBImm, "rsb{s}<c>.w <Rd>, <Rn>, #<const>"}, |
| 6704 | // rsb (register) |
| 6705 | { 0xffe08000, 0xea400000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateRSBReg, "rsb{s}<c>.w <Rd>, <Rn>, <Rm> {,<shift>}"}, |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 6706 | // teq (immediate) |
| 6707 | { 0xfbf08f00, 0xf0900f00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateTEQImm, "teq<c> <Rn>, #<const>"}, |
| 6708 | // teq (register) |
| 6709 | { 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] | 6710 | // tst (immediate) |
Johnny Chen | 2115b41 | 2011-02-21 23:42:44 +0000 | [diff] [blame] | 6711 | { 0xfbf08f00, 0xf0100f00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateTSTImm, "tst<c> <Rn>, #<const>"}, |
Johnny Chen | de3cce3 | 2011-02-21 21:24:49 +0000 | [diff] [blame] | 6712 | // tst (register) |
| 6713 | { 0xffffffc0, 0x00004200, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateTSTReg, "tst<c> <Rdn>, <Rm>"}, |
| 6714 | { 0xfff08f00, 0xea100f00, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateTSTReg, "tst<c>.w <Rn>, <Rm> {,<shift>}"}, |
| 6715 | |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 6716 | |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 6717 | // move from high register to high register |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 6718 | { 0xffffff00, 0x00004600, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMOVRdRm, "mov<c> <Rd>, <Rm>"}, |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 6719 | // move from low register to low register |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 6720 | { 0xffffffc0, 0x00000000, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateMOVRdRm, "movs <Rd>, <Rm>"}, |
Johnny Chen | 7c5234d | 2011-02-18 23:41:11 +0000 | [diff] [blame] | 6721 | // mov{s}<c>.w <Rd>, <Rm> |
| 6722 | { 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] | 6723 | // move immediate |
Johnny Chen | 9f68772 | 2011-02-18 00:02:28 +0000 | [diff] [blame] | 6724 | { 0xfffff800, 0x00002000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMOVRdImm, "movs|mov<c> <Rd>, #imm8"}, |
| 6725 | { 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] | 6726 | // mvn (immediate) |
| 6727 | { 0xfbef8000, 0xf06f0000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateMVNImm, "mvn{s} <Rd>, #<const>"}, |
| 6728 | // mvn (register) |
| 6729 | { 0xffffffc0, 0x000043c0, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateMVNReg, "mvns|mvn<c> <Rd>, <Rm>"}, |
| 6730 | { 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] | 6731 | // cmn (immediate) |
Johnny Chen | 688926f | 2011-02-22 19:01:11 +0000 | [diff] [blame] | 6732 | { 0xfbf08f00, 0xf1100f00, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateCMNImm, "cmn<c> <Rn>, #<const>"}, |
Johnny Chen | 34075cb | 2011-02-22 01:56:31 +0000 | [diff] [blame] | 6733 | // cmn (register) |
| 6734 | { 0xffffffc0, 0x000042c0, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCMNReg, "cmn<c> <Rn>, <Rm>"}, |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 6735 | { 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] | 6736 | // cmp (immediate) |
| 6737 | { 0xfffff800, 0x00002800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCMPImm, "cmp<c> <Rn>, #imm8"}, |
Johnny Chen | 078fbc6 | 2011-02-22 19:48:22 +0000 | [diff] [blame] | 6738 | { 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] | 6739 | // cmp (register) (Rn and Rm both from r0-r7) |
| 6740 | { 0xffffffc0, 0x00004280, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateCMPReg, "cmp<c> <Rn>, <Rm>"}, |
| 6741 | // cmp (register) (Rn and Rm not both from r0-r7) |
| 6742 | { 0xffffff00, 0x00004500, ARMvAll, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateCMPReg, "cmp<c> <Rn>, <Rm>"}, |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 6743 | // asr (immediate) |
| 6744 | { 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] | 6745 | { 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] | 6746 | // asr (register) |
| 6747 | { 0xffffffc0, 0x00004100, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateASRReg, "asrs|asr<c> <Rdn>, <Rm>"}, |
| 6748 | { 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] | 6749 | // lsl (immediate) |
| 6750 | { 0xfffff800, 0x00000000, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLSLImm, "lsls|lsl<c> <Rd>, <Rm>, #imm"}, |
| 6751 | { 0xffef8030, 0xea4f0000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLSLImm, "lsl{s}<c>.w <Rd>, <Rm>, #imm"}, |
| 6752 | // lsl (register) |
| 6753 | { 0xffffffc0, 0x00004080, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLSLReg, "lsls|lsl<c> <Rdn>, <Rm>"}, |
| 6754 | { 0xffe0f0f0, 0xfa00f000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLSLReg, "lsl{s}<c>.w <Rd>, <Rn>, <Rm>"}, |
| 6755 | // lsr (immediate) |
| 6756 | { 0xfffff800, 0x00000800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLSRImm, "lsrs|lsr<c> <Rd>, <Rm>, #imm"}, |
| 6757 | { 0xffef8030, 0xea4f0010, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLSRImm, "lsr{s}<c>.w <Rd>, <Rm>, #imm"}, |
| 6758 | // lsr (register) |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 6759 | { 0xffffffc0, 0x000040c0, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLSRReg, "lsrs|lsr<c> <Rdn>, <Rm>"}, |
Johnny Chen | 2ee35bc | 2011-02-16 19:27:43 +0000 | [diff] [blame] | 6760 | { 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] | 6761 | // rrx is a special case encoding of ror (immediate) |
| 6762 | { 0xffeff0f0, 0xea4f0030, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateRRX, "rrx{s}<c>.w <Rd>, <Rm>"}, |
| 6763 | // ror (immediate) |
| 6764 | { 0xffef8030, 0xea4f0030, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateRORImm, "ror{s}<c>.w <Rd>, <Rm>, #imm"}, |
| 6765 | // ror (register) |
| 6766 | { 0xffffffc0, 0x000041c0, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateRORReg, "rors|ror<c> <Rdn>, <Rm>"}, |
| 6767 | { 0xffe0f0f0, 0xfa60f000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateRORReg, "ror{s}<c>.w <Rd>, <Rn>, <Rm>"}, |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 6768 | |
| 6769 | //---------------------------------------------------------------------- |
Caroline Tice | b9f76c3 | 2011-02-08 22:24:38 +0000 | [diff] [blame] | 6770 | // Load instructions |
| 6771 | //---------------------------------------------------------------------- |
| 6772 | { 0xfffff800, 0x0000c800, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDM, "ldm<c> <Rn>{!} <registers>" }, |
Caroline Tice | 0b29e24 | 2011-02-08 23:16:02 +0000 | [diff] [blame] | 6773 | { 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] | 6774 | { 0xffd00000, 0xe9100000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateLDMDB, "ldmdb<c> <Rn>{!} <registers>" }, |
Johnny Chen | c9de910 | 2011-02-11 19:12:30 +0000 | [diff] [blame] | 6775 | { 0xfffff800, 0x00006800, ARMvAll, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRtRnImm, "ldr<c> <Rt>, [<Rn>{,#imm}]"}, |
| 6776 | // Thumb2 PC-relative load into register |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 6777 | { 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] | 6778 | { 0xfffffe00, 0x00005800, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRRegister, "ldr<c> <Rt>, [<Rn>, <Rm>]" }, |
| 6779 | { 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] | 6780 | { 0xfffff800, 0x00007800, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRBImmediate, "ldrb<c> <Rt>,[<Rn>{,#<imm5>}]" }, |
| 6781 | { 0xfff00000, 0xf8900000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateLDRBImmediate, "ldrb<c>.w <Rt>,[<Rn>{,#<imm12>}]" }, |
| 6782 | { 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] | 6783 | { 0xff7f0000, 0xf81f0000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateLDRBLiteral, "ldrb<c> <Rt>,[...]" }, |
Caroline Tice | 30fec12 | 2011-02-18 23:52:21 +0000 | [diff] [blame] | 6784 | { 0xfffffe00, 0x00005c00, ARMV6T2_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateLDRBRegister, "ldrb<c> <Rt>,[<Rn>,<Rm>]" }, |
| 6785 | { 0xfff00fc0, 0xf8100000, ARMV6T2_ABOVE, eEncodingT2, eSize32,&EmulateInstructionARM::EmulateLDRBRegister, "ldrb<c>.w <Rt>,[<Rn>,<Rm>{,LSL #imm2>}]" }, |
Caroline Tice | fa17220 | 2011-02-11 22:49:54 +0000 | [diff] [blame] | 6786 | |
| 6787 | //---------------------------------------------------------------------- |
| 6788 | // Store instructions |
| 6789 | //---------------------------------------------------------------------- |
| 6790 | { 0xfffff800, 0x0000c000, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSTM, "stm<c> <Rn>{!} <registers>" }, |
Caroline Tice | b6f8d7e | 2011-02-15 18:10:01 +0000 | [diff] [blame] | 6791 | { 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] | 6792 | { 0xffd00000, 0xe9000000, ARMV6T2_ABOVE, eEncodingT1, eSize32, &EmulateInstructionARM::EmulateSTMDB, "stmdb<c> <Rn>{!} <registers>" }, |
Caroline Tice | fe47911 | 2011-02-18 18:52:37 +0000 | [diff] [blame] | 6793 | { 0xfffff800, 0x00006000, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSTRThumb, "str<c> <Rt>, [<Rn>{,#<imm>}]" }, |
| 6794 | { 0xfffff800, 0x00009000, ARMV4T_ABOVE, eEncodingT2, eSize16, &EmulateInstructionARM::EmulateSTRThumb, "str<c> <Rt>, [SP,#<imm>]" }, |
| 6795 | { 0xfff00000, 0xf8c00000, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSTRThumb, "str<c>.w <Rt>, [<Rn>,#<imm12>]" }, |
| 6796 | { 0xfff00800, 0xf8400800, ARMV6T2_ABOVE, eEncodingT4, eSize32, &EmulateInstructionARM::EmulateSTRThumb, "str<c> <Rt>, [<Rn>,#+/-<imm8>]" }, |
| 6797 | { 0xfffffe00, 0x00005000, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSTRRegister, "str<c> <Rt> ,{<Rn>, <Rm>]" }, |
| 6798 | { 0xfff00fc0, 0xf8400000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSTRRegister, "str<c>.w <Rt>, [<Rn>, <Rm> {lsl #imm2>}]" }, |
| 6799 | { 0xfffff800, 0x00007000, ARMV4T_ABOVE, eEncodingT1, eSize16, &EmulateInstructionARM::EmulateSTRBThumb, "strb<c> <Rt>, [<Rn>, #<imm5>]" }, |
| 6800 | { 0xfff00000, 0xf8800000, ARMV6T2_ABOVE, eEncodingT2, eSize32, &EmulateInstructionARM::EmulateSTRBThumb, "strb<c>.w <Rt>, [<Rn>, #<imm12>]" }, |
| 6801 | { 0xfff00800, 0xf8000800, ARMV6T2_ABOVE, eEncodingT3, eSize32, &EmulateInstructionARM::EmulateSTRBThumb, "strb<c> <Rt> ,[<Rn>, #+/-<imm8>]{!}" } |
Greg Clayton | 2b8e8b0 | 2011-02-01 00:49:32 +0000 | [diff] [blame] | 6802 | }; |
| 6803 | |
| 6804 | const size_t k_num_thumb_opcodes = sizeof(g_thumb_opcodes)/sizeof(ARMOpcode); |
| 6805 | for (size_t i=0; i<k_num_thumb_opcodes; ++i) |
| 6806 | { |
| 6807 | if ((g_thumb_opcodes[i].mask & opcode) == g_thumb_opcodes[i].value) |
| 6808 | return &g_thumb_opcodes[i]; |
| 6809 | } |
| 6810 | return NULL; |
| 6811 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 6812 | |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 6813 | bool |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 6814 | EmulateInstructionARM::SetArchitecture (const ArchSpec &arch) |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 6815 | { |
| 6816 | m_arm_isa = 0; |
Greg Clayton | 940b103 | 2011-02-23 00:35:02 +0000 | [diff] [blame^] | 6817 | const char *arch_cstr = arch.GetArchitectureName (); |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 6818 | if (arch_cstr) |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 6819 | { |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 6820 | if (0 == ::strcasecmp(arch_cstr, "armv4t")) m_arm_isa = ARMv4T; |
| 6821 | else if (0 == ::strcasecmp(arch_cstr, "armv4")) m_arm_isa = ARMv4; |
| 6822 | else if (0 == ::strcasecmp(arch_cstr, "armv5tej")) m_arm_isa = ARMv5TEJ; |
| 6823 | else if (0 == ::strcasecmp(arch_cstr, "armv5te")) m_arm_isa = ARMv5TE; |
| 6824 | else if (0 == ::strcasecmp(arch_cstr, "armv5t")) m_arm_isa = ARMv5T; |
| 6825 | else if (0 == ::strcasecmp(arch_cstr, "armv6k")) m_arm_isa = ARMv6K; |
| 6826 | else if (0 == ::strcasecmp(arch_cstr, "armv6")) m_arm_isa = ARMv6; |
| 6827 | else if (0 == ::strcasecmp(arch_cstr, "armv6t2")) m_arm_isa = ARMv6T2; |
| 6828 | else if (0 == ::strcasecmp(arch_cstr, "armv7")) m_arm_isa = ARMv7; |
| 6829 | else if (0 == ::strcasecmp(arch_cstr, "armv8")) m_arm_isa = ARMv8; |
Greg Clayton | 31e2a38 | 2011-01-30 20:03:56 +0000 | [diff] [blame] | 6830 | } |
| 6831 | return m_arm_isa != 0; |
| 6832 | } |
| 6833 | |
| 6834 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 6835 | bool |
| 6836 | EmulateInstructionARM::ReadInstruction () |
| 6837 | { |
| 6838 | bool success = false; |
| 6839 | m_inst_cpsr = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, 0, &success); |
| 6840 | if (success) |
| 6841 | { |
| 6842 | addr_t pc = ReadRegisterUnsigned (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_ADDRESS, &success); |
| 6843 | if (success) |
| 6844 | { |
Caroline Tice | 9bfe7f2 | 2011-02-14 23:03:21 +0000 | [diff] [blame] | 6845 | Context read_inst_context; |
| 6846 | read_inst_context.type = eContextReadOpcode; |
| 6847 | read_inst_context.SetNoArgs (); |
| 6848 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 6849 | if (m_inst_cpsr & MASK_CPSR_T) |
| 6850 | { |
| 6851 | m_inst_mode = eModeThumb; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 6852 | uint32_t thumb_opcode = MemARead(read_inst_context, pc, 2, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 6853 | |
| 6854 | if (success) |
| 6855 | { |
| 6856 | if ((m_inst.opcode.inst16 & 0xe000) != 0xe000 || ((m_inst.opcode.inst16 & 0x1800u) == 0)) |
| 6857 | { |
| 6858 | m_inst.opcode_type = eOpcode16; |
| 6859 | m_inst.opcode.inst16 = thumb_opcode; |
| 6860 | } |
| 6861 | else |
| 6862 | { |
| 6863 | m_inst.opcode_type = eOpcode32; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 6864 | 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] | 6865 | } |
| 6866 | } |
| 6867 | } |
| 6868 | else |
| 6869 | { |
| 6870 | m_inst_mode = eModeARM; |
| 6871 | m_inst.opcode_type = eOpcode32; |
Caroline Tice | cc96eb5 | 2011-02-17 19:20:40 +0000 | [diff] [blame] | 6872 | m_inst.opcode.inst32 = MemARead(read_inst_context, pc, 4, 0, &success); |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 6873 | } |
| 6874 | } |
| 6875 | } |
| 6876 | if (!success) |
| 6877 | { |
| 6878 | m_inst_mode = eModeInvalid; |
| 6879 | m_inst_pc = LLDB_INVALID_ADDRESS; |
| 6880 | } |
| 6881 | return success; |
| 6882 | } |
| 6883 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 6884 | uint32_t |
| 6885 | EmulateInstructionARM::ArchVersion () |
| 6886 | { |
| 6887 | return m_arm_isa; |
| 6888 | } |
| 6889 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 6890 | bool |
| 6891 | EmulateInstructionARM::ConditionPassed () |
| 6892 | { |
| 6893 | if (m_inst_cpsr == 0) |
| 6894 | return false; |
| 6895 | |
| 6896 | const uint32_t cond = CurrentCond (); |
| 6897 | |
| 6898 | if (cond == UINT32_MAX) |
| 6899 | return false; |
| 6900 | |
| 6901 | bool result = false; |
| 6902 | switch (UnsignedBits(cond, 3, 1)) |
| 6903 | { |
| 6904 | case 0: result = (m_inst_cpsr & MASK_CPSR_Z) != 0; break; |
| 6905 | case 1: result = (m_inst_cpsr & MASK_CPSR_C) != 0; break; |
| 6906 | case 2: result = (m_inst_cpsr & MASK_CPSR_N) != 0; break; |
| 6907 | case 3: result = (m_inst_cpsr & MASK_CPSR_V) != 0; break; |
| 6908 | case 4: result = ((m_inst_cpsr & MASK_CPSR_C) != 0) && ((m_inst_cpsr & MASK_CPSR_Z) == 0); break; |
| 6909 | case 5: |
| 6910 | { |
| 6911 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 6912 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 6913 | result = n == v; |
| 6914 | } |
| 6915 | break; |
| 6916 | case 6: |
| 6917 | { |
| 6918 | bool n = (m_inst_cpsr & MASK_CPSR_N); |
| 6919 | bool v = (m_inst_cpsr & MASK_CPSR_V); |
| 6920 | result = n == v && ((m_inst_cpsr & MASK_CPSR_Z) == 0); |
| 6921 | } |
| 6922 | break; |
| 6923 | case 7: |
| 6924 | result = true; |
| 6925 | break; |
| 6926 | } |
| 6927 | |
| 6928 | if (cond & 1) |
| 6929 | result = !result; |
| 6930 | return result; |
| 6931 | } |
| 6932 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 6933 | uint32_t |
| 6934 | EmulateInstructionARM::CurrentCond () |
| 6935 | { |
| 6936 | switch (m_inst_mode) |
| 6937 | { |
| 6938 | default: |
| 6939 | case eModeInvalid: |
| 6940 | break; |
| 6941 | |
| 6942 | case eModeARM: |
| 6943 | return UnsignedBits(m_inst.opcode.inst32, 31, 28); |
| 6944 | |
| 6945 | case eModeThumb: |
| 6946 | // For T1 and T3 encodings of the Branch instruction, it returns the 4-bit |
| 6947 | // 'cond' field of the encoding. |
| 6948 | if (m_inst.opcode_type == eOpcode16 && |
| 6949 | Bits32(m_inst.opcode.inst16, 15, 12) == 0x0d && |
| 6950 | Bits32(m_inst.opcode.inst16, 11, 7) != 0x0f) |
| 6951 | { |
| 6952 | return Bits32(m_inst.opcode.inst16, 11, 7); |
| 6953 | } |
| 6954 | else if (m_inst.opcode_type == eOpcode32 && |
| 6955 | Bits32(m_inst.opcode.inst32, 31, 27) == 0x1e && |
| 6956 | Bits32(m_inst.opcode.inst32, 15, 14) == 0x02 && |
| 6957 | Bits32(m_inst.opcode.inst32, 12, 12) == 0x00 && |
| 6958 | Bits32(m_inst.opcode.inst32, 25, 22) <= 0x0d) |
| 6959 | { |
| 6960 | return Bits32(m_inst.opcode.inst32, 25, 22); |
| 6961 | } |
| 6962 | |
| 6963 | return m_it_session.GetCond(); |
| 6964 | } |
| 6965 | return UINT32_MAX; // Return invalid value |
| 6966 | } |
| 6967 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 6968 | bool |
Johnny Chen | 098ae2d | 2011-02-12 00:50:05 +0000 | [diff] [blame] | 6969 | EmulateInstructionARM::InITBlock() |
| 6970 | { |
| 6971 | return CurrentInstrSet() == eModeThumb && m_it_session.InITBlock(); |
| 6972 | } |
| 6973 | |
| 6974 | bool |
| 6975 | EmulateInstructionARM::LastInITBlock() |
| 6976 | { |
| 6977 | return CurrentInstrSet() == eModeThumb && m_it_session.LastInITBlock(); |
| 6978 | } |
| 6979 | |
| 6980 | bool |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 6981 | EmulateInstructionARM::BranchWritePC (const Context &context, uint32_t addr) |
| 6982 | { |
| 6983 | addr_t target; |
| 6984 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 6985 | // Check the current instruction set. |
| 6986 | if (CurrentInstrSet() == eModeARM) |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 6987 | target = addr & 0xfffffffc; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 6988 | else |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 6989 | target = addr & 0xfffffffe; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 6990 | |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 6991 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target)) |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 6992 | return false; |
| 6993 | |
| 6994 | return true; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 6995 | } |
| 6996 | |
| 6997 | // As a side effect, BXWritePC sets context.arg2 to eModeARM or eModeThumb by inspecting addr. |
| 6998 | bool |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 6999 | EmulateInstructionARM::BXWritePC (Context &context, uint32_t addr) |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 7000 | { |
| 7001 | addr_t target; |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 7002 | // If the CPSR is changed due to switching between ARM and Thumb ISETSTATE, |
| 7003 | // we want to record it and issue a WriteRegister callback so the clients |
| 7004 | // can track the mode changes accordingly. |
| 7005 | bool cpsr_changed = false; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 7006 | |
| 7007 | if (BitIsSet(addr, 0)) |
| 7008 | { |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 7009 | if (CurrentInstrSet() != eModeThumb) |
| 7010 | { |
| 7011 | SelectInstrSet(eModeThumb); |
| 7012 | cpsr_changed = true; |
| 7013 | } |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 7014 | target = addr & 0xfffffffe; |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 7015 | context.SetMode (eModeThumb); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 7016 | } |
| 7017 | else if (BitIsClear(addr, 1)) |
| 7018 | { |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 7019 | if (CurrentInstrSet() != eModeARM) |
| 7020 | { |
| 7021 | SelectInstrSet(eModeARM); |
| 7022 | cpsr_changed = true; |
| 7023 | } |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 7024 | target = addr & 0xfffffffc; |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 7025 | context.SetMode (eModeARM); |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 7026 | } |
| 7027 | else |
| 7028 | return false; // address<1:0> == '10' => UNPREDICTABLE |
| 7029 | |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 7030 | if (cpsr_changed) |
| 7031 | { |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 7032 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr)) |
Johnny Chen | 0f309db | 2011-02-09 19:11:32 +0000 | [diff] [blame] | 7033 | return false; |
| 7034 | } |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 7035 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, target)) |
Johnny Chen | 53ebab7 | 2011-02-08 23:21:57 +0000 | [diff] [blame] | 7036 | return false; |
| 7037 | |
| 7038 | return true; |
Johnny Chen | 9ee056b | 2011-02-08 00:06:35 +0000 | [diff] [blame] | 7039 | } |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 7040 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 7041 | // Dispatches to either BXWritePC or BranchWritePC based on architecture versions. |
| 7042 | bool |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 7043 | EmulateInstructionARM::LoadWritePC (Context &context, uint32_t addr) |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 7044 | { |
| 7045 | if (ArchVersion() >= ARMv5T) |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 7046 | return BXWritePC(context, addr); |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 7047 | else |
| 7048 | return BranchWritePC((const Context)context, addr); |
| 7049 | } |
| 7050 | |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 7051 | // Dispatches to either BXWritePC or BranchWritePC based on architecture versions and current instruction set. |
| 7052 | bool |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 7053 | EmulateInstructionARM::ALUWritePC (Context &context, uint32_t addr) |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 7054 | { |
| 7055 | if (ArchVersion() >= ARMv7 && CurrentInstrSet() == eModeARM) |
Johnny Chen | 668b451 | 2011-02-15 21:08:58 +0000 | [diff] [blame] | 7056 | return BXWritePC(context, addr); |
Johnny Chen | 26863dc | 2011-02-09 23:43:29 +0000 | [diff] [blame] | 7057 | else |
| 7058 | return BranchWritePC((const Context)context, addr); |
| 7059 | } |
| 7060 | |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 7061 | EmulateInstructionARM::Mode |
| 7062 | EmulateInstructionARM::CurrentInstrSet () |
| 7063 | { |
| 7064 | return m_inst_mode; |
| 7065 | } |
| 7066 | |
| 7067 | // 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] | 7068 | // ReadInstruction() is performed. This function has a side effect of updating |
| 7069 | // the m_new_inst_cpsr member variable if necessary. |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 7070 | bool |
| 7071 | EmulateInstructionARM::SelectInstrSet (Mode arm_or_thumb) |
| 7072 | { |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 7073 | m_new_inst_cpsr = m_inst_cpsr; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 7074 | switch (arm_or_thumb) |
| 7075 | { |
| 7076 | default: |
| 7077 | return false; |
| 7078 | eModeARM: |
| 7079 | // Clear the T bit. |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 7080 | m_new_inst_cpsr &= ~MASK_CPSR_T; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 7081 | break; |
| 7082 | eModeThumb: |
| 7083 | // Set the T bit. |
Johnny Chen | 558133b | 2011-02-09 23:59:17 +0000 | [diff] [blame] | 7084 | m_new_inst_cpsr |= MASK_CPSR_T; |
Johnny Chen | ee9b1f7 | 2011-02-09 01:00:31 +0000 | [diff] [blame] | 7085 | break; |
| 7086 | } |
| 7087 | return true; |
| 7088 | } |
| 7089 | |
Johnny Chen | ef21b59 | 2011-02-10 01:52:38 +0000 | [diff] [blame] | 7090 | // This function returns TRUE if the processor currently provides support for |
| 7091 | // unaligned memory accesses, or FALSE otherwise. This is always TRUE in ARMv7, |
| 7092 | // controllable by the SCTLR.U bit in ARMv6, and always FALSE before ARMv6. |
| 7093 | bool |
| 7094 | EmulateInstructionARM::UnalignedSupport() |
| 7095 | { |
| 7096 | return (ArchVersion() >= ARMv7); |
| 7097 | } |
| 7098 | |
Johnny Chen | bf6ad17 | 2011-02-11 01:29:53 +0000 | [diff] [blame] | 7099 | // The main addition and subtraction instructions can produce status information |
| 7100 | // about both unsigned carry and signed overflow conditions. This status |
| 7101 | // information can be used to synthesize multi-word additions and subtractions. |
| 7102 | EmulateInstructionARM::AddWithCarryResult |
| 7103 | EmulateInstructionARM::AddWithCarry (uint32_t x, uint32_t y, uint8_t carry_in) |
| 7104 | { |
| 7105 | uint32_t result; |
| 7106 | uint8_t carry_out; |
| 7107 | uint8_t overflow; |
| 7108 | |
| 7109 | uint64_t unsigned_sum = x + y + carry_in; |
| 7110 | int64_t signed_sum = (int32_t)x + (int32_t)y + (int32_t)carry_in; |
| 7111 | |
| 7112 | result = UnsignedBits(unsigned_sum, 31, 0); |
| 7113 | carry_out = (result == unsigned_sum ? 0 : 1); |
| 7114 | overflow = ((int32_t)result == signed_sum ? 0 : 1); |
| 7115 | |
| 7116 | AddWithCarryResult res = { result, carry_out, overflow }; |
| 7117 | return res; |
| 7118 | } |
| 7119 | |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 7120 | uint32_t |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 7121 | EmulateInstructionARM::ReadCoreReg(uint32_t num, bool *success) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 7122 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 7123 | uint32_t reg_kind, reg_num; |
| 7124 | switch (num) |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 7125 | { |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 7126 | case SP_REG: |
| 7127 | reg_kind = eRegisterKindGeneric; |
| 7128 | reg_num = LLDB_REGNUM_GENERIC_SP; |
| 7129 | break; |
| 7130 | case LR_REG: |
| 7131 | reg_kind = eRegisterKindGeneric; |
| 7132 | reg_num = LLDB_REGNUM_GENERIC_RA; |
| 7133 | break; |
| 7134 | case PC_REG: |
| 7135 | reg_kind = eRegisterKindGeneric; |
| 7136 | reg_num = LLDB_REGNUM_GENERIC_PC; |
| 7137 | break; |
| 7138 | default: |
| 7139 | if (0 <= num && num < SP_REG) |
| 7140 | { |
| 7141 | reg_kind = eRegisterKindDWARF; |
| 7142 | reg_num = dwarf_r0 + num; |
| 7143 | } |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 7144 | else |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 7145 | { |
| 7146 | assert(0 && "Invalid register number"); |
| 7147 | *success = false; |
| 7148 | return ~0u; |
| 7149 | } |
| 7150 | break; |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 7151 | } |
Johnny Chen | e39f22d | 2011-02-19 01:36:13 +0000 | [diff] [blame] | 7152 | |
| 7153 | // Read our register. |
| 7154 | uint32_t val = ReadRegisterUnsigned (reg_kind, reg_num, 0, success); |
| 7155 | |
| 7156 | // When executing an ARM instruction , PC reads as the address of the current |
| 7157 | // instruction plus 8. |
| 7158 | // When executing a Thumb instruction , PC reads as the address of the current |
| 7159 | // instruction plus 4. |
| 7160 | if (num == 15) |
| 7161 | { |
| 7162 | if (CurrentInstrSet() == eModeARM) |
| 7163 | val += 8; |
| 7164 | else |
| 7165 | val += 4; |
| 7166 | } |
Johnny Chen | 157b959 | 2011-02-18 21:13:05 +0000 | [diff] [blame] | 7167 | |
| 7168 | return val; |
| 7169 | } |
| 7170 | |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 7171 | // Write the result to the ARM core register Rd, and optionally update the |
| 7172 | // condition flags based on the result. |
| 7173 | // |
| 7174 | // This helper method tries to encapsulate the following pseudocode from the |
| 7175 | // ARM Architecture Reference Manual: |
| 7176 | // |
| 7177 | // if d == 15 then // Can only occur for encoding A1 |
| 7178 | // ALUWritePC(result); // setflags is always FALSE here |
| 7179 | // else |
| 7180 | // R[d] = result; |
| 7181 | // if setflags then |
| 7182 | // APSR.N = result<31>; |
| 7183 | // APSR.Z = IsZeroBit(result); |
| 7184 | // APSR.C = carry; |
| 7185 | // // APSR.V unchanged |
| 7186 | // |
| 7187 | // In the above case, the API client does not pass in the overflow arg, which |
| 7188 | // defaults to ~0u. |
| 7189 | bool |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 7190 | EmulateInstructionARM::WriteCoreRegOptionalFlags (Context &context, |
| 7191 | const uint32_t result, |
| 7192 | const uint32_t Rd, |
| 7193 | bool setflags, |
| 7194 | const uint32_t carry, |
| 7195 | const uint32_t overflow) |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 7196 | { |
| 7197 | if (Rd == 15) |
| 7198 | { |
| 7199 | if (!ALUWritePC (context, result)) |
| 7200 | return false; |
| 7201 | } |
| 7202 | else |
| 7203 | { |
| 7204 | if (!WriteRegisterUnsigned (context, eRegisterKindDWARF, dwarf_r0 + Rd, result)) |
| 7205 | return false; |
| 7206 | if (setflags) |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 7207 | return WriteFlags (context, result, carry, overflow); |
| 7208 | } |
| 7209 | return true; |
| 7210 | } |
| 7211 | |
| 7212 | // This helper method tries to encapsulate the following pseudocode from the |
| 7213 | // ARM Architecture Reference Manual: |
| 7214 | // |
| 7215 | // APSR.N = result<31>; |
| 7216 | // APSR.Z = IsZeroBit(result); |
| 7217 | // APSR.C = carry; |
| 7218 | // APSR.V = overflow |
| 7219 | // |
| 7220 | // Default arguments can be specified for carry and overflow parameters, which means |
| 7221 | // not to update the respective flags. |
| 7222 | bool |
| 7223 | EmulateInstructionARM::WriteFlags (Context &context, |
| 7224 | const uint32_t result, |
| 7225 | const uint32_t carry, |
| 7226 | const uint32_t overflow) |
| 7227 | { |
| 7228 | m_new_inst_cpsr = m_inst_cpsr; |
Johnny Chen | 2434884 | 2011-02-23 00:15:56 +0000 | [diff] [blame] | 7229 | SetBit32(m_new_inst_cpsr, CPSR_N_POS, Bit32(result, CPSR_N_POS)); |
| 7230 | SetBit32(m_new_inst_cpsr, CPSR_Z_POS, result == 0 ? 1 : 0); |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 7231 | if (carry != ~0u) |
Johnny Chen | 2434884 | 2011-02-23 00:15:56 +0000 | [diff] [blame] | 7232 | SetBit32(m_new_inst_cpsr, CPSR_C_POS, carry); |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 7233 | if (overflow != ~0u) |
Johnny Chen | 2434884 | 2011-02-23 00:15:56 +0000 | [diff] [blame] | 7234 | SetBit32(m_new_inst_cpsr, CPSR_V_POS, overflow); |
Johnny Chen | 10530c2 | 2011-02-17 22:37:12 +0000 | [diff] [blame] | 7235 | if (m_new_inst_cpsr != m_inst_cpsr) |
| 7236 | { |
| 7237 | if (!WriteRegisterUnsigned (context, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS, m_new_inst_cpsr)) |
| 7238 | return false; |
Johnny Chen | ca67d1c | 2011-02-17 01:35:27 +0000 | [diff] [blame] | 7239 | } |
| 7240 | return true; |
| 7241 | } |
| 7242 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 7243 | bool |
| 7244 | EmulateInstructionARM::EvaluateInstruction () |
| 7245 | { |
Johnny Chen | c315f86 | 2011-02-05 00:46:10 +0000 | [diff] [blame] | 7246 | // Advance the ITSTATE bits to their values for the next instruction. |
| 7247 | if (m_inst_mode == eModeThumb && m_it_session.InITBlock()) |
| 7248 | m_it_session.ITAdvance(); |
| 7249 | |
Greg Clayton | 64c8443 | 2011-01-21 22:02:52 +0000 | [diff] [blame] | 7250 | return false; |
| 7251 | } |