Greg Clayton | 061b79d | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 1 | //===-- ARMUtils.h ----------------------------------------------*- C++ -*-===// |
Johnny Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef lldb_ARMUtils_h_ |
| 11 | #define lldb_ARMUtils_h_ |
| 12 | |
Johnny Chen | 41a0a15 | 2011-02-16 01:27:54 +0000 | [diff] [blame] | 13 | #include "ARMDefines.h" |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 14 | #include "InstructionUtils.h" |
Johnny Chen | 0d771a2 | 2011-02-15 17:52:22 +0000 | [diff] [blame] | 15 | #include "llvm/Support/MathExtras.h" // for SignExtend64 template function |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 16 | |
Johnny Chen | c3b5bc3 | 2011-01-24 19:50:30 +0000 | [diff] [blame] | 17 | // Common utilities for the ARM/Thumb Instruction Set Architecture. |
Johnny Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame] | 18 | |
| 19 | namespace lldb_private { |
| 20 | |
Johnny Chen | 7c677ac | 2011-02-17 17:31:08 +0000 | [diff] [blame] | 21 | static inline uint32_t Align(uint32_t val, uint32_t alignment) |
| 22 | { |
| 23 | return alignment * (val / alignment); |
| 24 | } |
| 25 | |
Johnny Chen | 0d771a2 | 2011-02-15 17:52:22 +0000 | [diff] [blame] | 26 | static inline uint32_t DecodeImmShift(const uint32_t type, const uint32_t imm5, ARM_ShifterType &shift_t) |
| 27 | { |
| 28 | switch (type) { |
| 29 | default: |
Greg Clayton | e1f47bb | 2011-06-02 22:23:35 +0000 | [diff] [blame] | 30 | //assert(0 && "Invalid shift type"); |
| 31 | return UINT32_MAX; |
Johnny Chen | 0d771a2 | 2011-02-15 17:52:22 +0000 | [diff] [blame] | 32 | case 0: |
| 33 | shift_t = SRType_LSL; |
| 34 | return imm5; |
| 35 | case 1: |
| 36 | shift_t = SRType_LSR; |
| 37 | return (imm5 == 0 ? 32 : imm5); |
| 38 | case 2: |
| 39 | shift_t = SRType_ASR; |
| 40 | return (imm5 == 0 ? 32 : imm5); |
| 41 | case 3: |
| 42 | if (imm5 == 0) |
| 43 | { |
| 44 | shift_t = SRType_RRX; |
| 45 | return 1; |
| 46 | } |
| 47 | else |
| 48 | { |
| 49 | shift_t = SRType_ROR; |
| 50 | return imm5; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 55 | // A8.6.35 CMP (register) -- Encoding T3 |
| 56 | // Convenience function. |
| 57 | static inline uint32_t DecodeImmShiftThumb(const uint32_t opcode, ARM_ShifterType &shift_t) |
| 58 | { |
| 59 | return DecodeImmShift(Bits32(opcode, 5, 4), Bits32(opcode, 14, 12)<<2 | Bits32(opcode, 7, 6), shift_t); |
| 60 | } |
| 61 | |
| 62 | // A8.6.35 CMP (register) -- Encoding A1 |
| 63 | // Convenience function. |
| 64 | static inline uint32_t DecodeImmShiftARM(const uint32_t opcode, ARM_ShifterType &shift_t) |
| 65 | { |
| 66 | return DecodeImmShift(Bits32(opcode, 6, 5), Bits32(opcode, 11, 7), shift_t); |
| 67 | } |
| 68 | |
Johnny Chen | 82f16aa | 2011-02-15 20:10:55 +0000 | [diff] [blame] | 69 | static inline uint32_t DecodeImmShift(const ARM_ShifterType shift_t, const uint32_t imm5) |
| 70 | { |
| 71 | ARM_ShifterType dont_care; |
| 72 | return DecodeImmShift(shift_t, imm5, dont_care); |
| 73 | } |
| 74 | |
Johnny Chen | 0d771a2 | 2011-02-15 17:52:22 +0000 | [diff] [blame] | 75 | static inline ARM_ShifterType DecodeRegShift(const uint32_t type) |
| 76 | { |
| 77 | switch (type) { |
| 78 | default: |
Greg Clayton | e1f47bb | 2011-06-02 22:23:35 +0000 | [diff] [blame] | 79 | //assert(0 && "Invalid shift type"); |
| 80 | return SRType_Invalid; |
Johnny Chen | 0d771a2 | 2011-02-15 17:52:22 +0000 | [diff] [blame] | 81 | case 0: |
| 82 | return SRType_LSL; |
| 83 | case 1: |
| 84 | return SRType_LSR; |
| 85 | case 2: |
| 86 | return SRType_ASR; |
| 87 | case 3: |
| 88 | return SRType_ROR; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static inline uint32_t LSL_C(const uint32_t value, const uint32_t amount, uint32_t &carry_out) |
| 93 | { |
Greg Clayton | e1f47bb | 2011-06-02 22:23:35 +0000 | [diff] [blame] | 94 | //assert(amount > 0); |
Johnny Chen | 0fa98ca | 2011-02-15 22:21:33 +0000 | [diff] [blame] | 95 | carry_out = amount <= 32 ? Bit32(value, 32 - amount) : 0; |
Johnny Chen | 0d771a2 | 2011-02-15 17:52:22 +0000 | [diff] [blame] | 96 | return value << amount; |
| 97 | } |
| 98 | |
| 99 | static inline uint32_t LSL(const uint32_t value, const uint32_t amount) |
| 100 | { |
Greg Clayton | e1f47bb | 2011-06-02 22:23:35 +0000 | [diff] [blame] | 101 | //assert(amount >= 0); |
Johnny Chen | 0d771a2 | 2011-02-15 17:52:22 +0000 | [diff] [blame] | 102 | if (amount == 0) |
| 103 | return value; |
| 104 | uint32_t dont_care; |
| 105 | return LSL_C(value, amount, dont_care); |
| 106 | } |
| 107 | |
| 108 | static inline uint32_t LSR_C(const uint32_t value, const uint32_t amount, uint32_t &carry_out) |
| 109 | { |
Greg Clayton | e1f47bb | 2011-06-02 22:23:35 +0000 | [diff] [blame] | 110 | //assert(amount > 0); |
Johnny Chen | 0fa98ca | 2011-02-15 22:21:33 +0000 | [diff] [blame] | 111 | carry_out = amount <= 32 ? Bit32(value, amount - 1) : 0; |
Johnny Chen | 0d771a2 | 2011-02-15 17:52:22 +0000 | [diff] [blame] | 112 | return value >> amount; |
| 113 | } |
| 114 | |
| 115 | static inline uint32_t LSR(const uint32_t value, const uint32_t amount) |
| 116 | { |
Greg Clayton | e1f47bb | 2011-06-02 22:23:35 +0000 | [diff] [blame] | 117 | //assert(amount >= 0); |
Johnny Chen | 0d771a2 | 2011-02-15 17:52:22 +0000 | [diff] [blame] | 118 | if (amount == 0) |
| 119 | return value; |
| 120 | uint32_t dont_care; |
| 121 | return LSR_C(value, amount, dont_care); |
| 122 | } |
| 123 | |
| 124 | static inline uint32_t ASR_C(const uint32_t value, const uint32_t amount, uint32_t &carry_out) |
| 125 | { |
Greg Clayton | e1f47bb | 2011-06-02 22:23:35 +0000 | [diff] [blame] | 126 | //assert(amount > 0 && amount <= 32); |
Johnny Chen | 0fa98ca | 2011-02-15 22:21:33 +0000 | [diff] [blame] | 127 | bool negative = BitIsSet(value, 31); |
| 128 | if (amount <= 32) |
| 129 | { |
| 130 | carry_out = Bit32(value, amount - 1); |
| 131 | int64_t extended = llvm::SignExtend64<32>(value); |
| 132 | return UnsignedBits(extended, amount + 31, amount); |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | carry_out = (negative ? 1 : 0); |
| 137 | return (negative ? 0xffffffff : 0); |
| 138 | } |
Johnny Chen | 0d771a2 | 2011-02-15 17:52:22 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static inline uint32_t ASR(const uint32_t value, const uint32_t amount) |
| 142 | { |
Greg Clayton | e1f47bb | 2011-06-02 22:23:35 +0000 | [diff] [blame] | 143 | //assert(amount >= 0); |
Johnny Chen | 0d771a2 | 2011-02-15 17:52:22 +0000 | [diff] [blame] | 144 | if (amount == 0) |
| 145 | return value; |
| 146 | uint32_t dont_care; |
| 147 | return ASR_C(value, amount, dont_care); |
| 148 | } |
| 149 | |
| 150 | static inline uint32_t ROR_C(const uint32_t value, const uint32_t amount, uint32_t &carry_out) |
| 151 | { |
Greg Clayton | e1f47bb | 2011-06-02 22:23:35 +0000 | [diff] [blame] | 152 | //assert(amount > 0); |
Johnny Chen | 0fa98ca | 2011-02-15 22:21:33 +0000 | [diff] [blame] | 153 | uint32_t amt = amount % 32; |
| 154 | uint32_t result = Rotr32(value, amt); |
Johnny Chen | 0d771a2 | 2011-02-15 17:52:22 +0000 | [diff] [blame] | 155 | carry_out = Bit32(value, 31); |
| 156 | return result; |
| 157 | } |
| 158 | |
| 159 | static inline uint32_t ROR(const uint32_t value, const uint32_t amount) |
| 160 | { |
Greg Clayton | e1f47bb | 2011-06-02 22:23:35 +0000 | [diff] [blame] | 161 | //assert(amount >= 0); |
Johnny Chen | 0d771a2 | 2011-02-15 17:52:22 +0000 | [diff] [blame] | 162 | if (amount == 0) |
| 163 | return value; |
| 164 | uint32_t dont_care; |
| 165 | return ROR_C(value, amount, dont_care); |
| 166 | } |
| 167 | |
| 168 | static inline uint32_t RRX_C(const uint32_t value, const uint32_t carry_in, uint32_t &carry_out) |
| 169 | { |
| 170 | carry_out = Bit32(value, 0); |
| 171 | return Bit32(carry_in, 0) << 31 | Bits32(value, 31, 1); |
| 172 | } |
| 173 | |
| 174 | static inline uint32_t RRX(const uint32_t value, const uint32_t carry_in) |
| 175 | { |
| 176 | uint32_t dont_care; |
| 177 | return RRX_C(value, carry_in, dont_care); |
| 178 | } |
| 179 | |
| 180 | static inline uint32_t Shift_C(const uint32_t value, ARM_ShifterType type, const uint32_t amount, |
| 181 | const uint32_t carry_in, uint32_t &carry_out) |
| 182 | { |
Greg Clayton | e1f47bb | 2011-06-02 22:23:35 +0000 | [diff] [blame] | 183 | //assert(type != SRType_RRX || amount == 1); |
Johnny Chen | 0d771a2 | 2011-02-15 17:52:22 +0000 | [diff] [blame] | 184 | if (amount == 0) |
| 185 | { |
| 186 | carry_out = carry_in; |
| 187 | return value; |
| 188 | } |
| 189 | uint32_t result; |
| 190 | switch (type) { |
| 191 | case SRType_LSL: |
| 192 | result = LSL_C(value, amount, carry_out); |
| 193 | break; |
| 194 | case SRType_LSR: |
| 195 | result = LSR_C(value, amount, carry_out); |
| 196 | break; |
| 197 | case SRType_ASR: |
| 198 | result = ASR_C(value, amount, carry_out); |
| 199 | break; |
| 200 | case SRType_ROR: |
| 201 | result = ROR_C(value, amount, carry_out); |
| 202 | break; |
| 203 | case SRType_RRX: |
Johnny Chen | eeab485 | 2011-02-16 22:14:44 +0000 | [diff] [blame] | 204 | result = RRX_C(value, carry_in, carry_out); |
Johnny Chen | 0d771a2 | 2011-02-15 17:52:22 +0000 | [diff] [blame] | 205 | break; |
| 206 | } |
| 207 | return result; |
| 208 | } |
| 209 | |
| 210 | static inline uint32_t Shift(const uint32_t value, ARM_ShifterType type, const uint32_t amount, |
| 211 | const uint32_t carry_in) |
| 212 | { |
| 213 | // Don't care about carry out in this case. |
| 214 | uint32_t dont_care; |
| 215 | return Shift_C(value, type, amount, carry_in, dont_care); |
| 216 | } |
| 217 | |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 218 | static inline uint32_t bits(const uint32_t val, const uint32_t msbit, const uint32_t lsbit) |
| 219 | { |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 220 | return Bits32(val, msbit, lsbit); |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static inline uint32_t bit(const uint32_t val, const uint32_t msbit) |
| 224 | { |
| 225 | return bits(val, msbit, msbit); |
| 226 | } |
| 227 | |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 228 | static uint32_t ror(uint32_t val, uint32_t N, uint32_t shift) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 229 | { |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 230 | uint32_t m = shift % N; |
| 231 | return (val >> m) | (val << (N - m)); |
| 232 | } |
| 233 | |
Johnny Chen | 07f2f5a | 2011-02-14 19:08:41 +0000 | [diff] [blame] | 234 | // (imm32, carry_out) = ARMExpandImm_C(imm12, carry_in) |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 235 | static inline uint32_t ARMExpandImm_C(uint32_t opcode, uint32_t carry_in, uint32_t &carry_out) |
Johnny Chen | 07f2f5a | 2011-02-14 19:08:41 +0000 | [diff] [blame] | 236 | { |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 237 | uint32_t imm32; // the expanded result |
| 238 | uint32_t imm = bits(opcode, 7, 0); // immediate value |
| 239 | uint32_t amt = 2 * bits(opcode, 11, 8); // rotate amount |
Johnny Chen | 07f2f5a | 2011-02-14 19:08:41 +0000 | [diff] [blame] | 240 | if (amt == 0) |
| 241 | { |
| 242 | imm32 = imm; |
| 243 | carry_out = carry_in; |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | imm32 = ror(imm, 32, amt); |
| 248 | carry_out = Bit32(imm32, 31); |
| 249 | } |
| 250 | return imm32; |
| 251 | } |
| 252 | |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 253 | static inline uint32_t ARMExpandImm(uint32_t opcode) |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 254 | { |
Johnny Chen | 07f2f5a | 2011-02-14 19:08:41 +0000 | [diff] [blame] | 255 | // 'carry_in' argument to following function call does not affect the imm32 result. |
| 256 | uint32_t carry_in = 0; |
| 257 | uint32_t carry_out; |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 258 | return ARMExpandImm_C(opcode, carry_in, carry_out); |
Johnny Chen | 07f2f5a | 2011-02-14 19:08:41 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // (imm32, carry_out) = ThumbExpandImm_C(imm12, carry_in) |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 262 | static inline uint32_t ThumbExpandImm_C(uint32_t opcode, uint32_t carry_in, uint32_t &carry_out) |
Johnny Chen | 07f2f5a | 2011-02-14 19:08:41 +0000 | [diff] [blame] | 263 | { |
Johnny Chen | 3e9a41c | 2011-02-14 19:09:36 +0000 | [diff] [blame] | 264 | uint32_t imm32; // the expaned result |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 265 | const uint32_t i = bit(opcode, 26); |
| 266 | const uint32_t imm3 = bits(opcode, 14, 12); |
| 267 | const uint32_t abcdefgh = bits(opcode, 7, 0); |
Johnny Chen | 07f2f5a | 2011-02-14 19:08:41 +0000 | [diff] [blame] | 268 | const uint32_t imm12 = i << 11 | imm3 << 8 | abcdefgh; |
| 269 | |
| 270 | if (bits(imm12, 11, 10) == 0) |
| 271 | { |
Caroline Tice | 3d79d34 | 2011-03-24 21:11:26 +0000 | [diff] [blame] | 272 | switch (bits(imm12, 9, 8)) { |
Johnny Chen | 07f2f5a | 2011-02-14 19:08:41 +0000 | [diff] [blame] | 273 | case 0: |
| 274 | imm32 = abcdefgh; |
| 275 | break; |
| 276 | |
| 277 | case 1: |
| 278 | imm32 = abcdefgh << 16 | abcdefgh; |
| 279 | break; |
| 280 | |
| 281 | case 2: |
| 282 | imm32 = abcdefgh << 24 | abcdefgh << 8; |
| 283 | break; |
| 284 | |
| 285 | case 3: |
| 286 | imm32 = abcdefgh << 24 | abcdefgh << 16 | abcdefgh << 8 | abcdefgh; |
| 287 | break; |
| 288 | } |
| 289 | carry_out = carry_in; |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | const uint32_t unrotated_value = 0x80 | bits(imm12, 6, 0); |
| 294 | imm32 = ror(unrotated_value, 32, bits(imm12, 11, 7)); |
| 295 | carry_out = Bit32(imm32, 31); |
| 296 | } |
| 297 | return imm32; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 300 | static inline uint32_t ThumbExpandImm(uint32_t opcode) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 301 | { |
Johnny Chen | 07f2f5a | 2011-02-14 19:08:41 +0000 | [diff] [blame] | 302 | // 'carry_in' argument to following function call does not affect the imm32 result. |
| 303 | uint32_t carry_in = 0; |
| 304 | uint32_t carry_out; |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 305 | return ThumbExpandImm_C(opcode, carry_in, carry_out); |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | // imm32 = ZeroExtend(i:imm3:imm8, 32) |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 309 | static inline uint32_t ThumbImm12(uint32_t opcode) |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 310 | { |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 311 | const uint32_t i = bit(opcode, 26); |
| 312 | const uint32_t imm3 = bits(opcode, 14, 12); |
| 313 | const uint32_t imm8 = bits(opcode, 7, 0); |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 314 | const uint32_t imm12 = i << 11 | imm3 << 8 | imm8; |
| 315 | return imm12; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 318 | // imm32 = ZeroExtend(imm7:'00', 32) |
Johnny Chen | a695f95 | 2011-02-23 21:24:25 +0000 | [diff] [blame] | 319 | static inline uint32_t ThumbImm7Scaled(uint32_t opcode) |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 320 | { |
Johnny Chen | 3dd0605 | 2011-02-22 21:17:52 +0000 | [diff] [blame] | 321 | const uint32_t imm7 = bits(opcode, 6, 0); |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 322 | return imm7 * 4; |
| 323 | } |
| 324 | |
Johnny Chen | a695f95 | 2011-02-23 21:24:25 +0000 | [diff] [blame] | 325 | // imm32 = ZeroExtend(imm8:'00', 32) |
| 326 | static inline uint32_t ThumbImm8Scaled(uint32_t opcode) |
| 327 | { |
| 328 | const uint32_t imm8 = bits(opcode, 7, 0); |
| 329 | return imm8 * 4; |
| 330 | } |
| 331 | |
Johnny Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame] | 332 | // This function performs the check for the register numbers 13 and 15 that are |
| 333 | // not permitted for many Thumb register specifiers. |
| 334 | static inline bool BadReg(uint32_t n) { return n == 13 || n == 15; } |
| 335 | |
Johnny Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame] | 336 | } // namespace lldb_private |
| 337 | |
| 338 | #endif // lldb_ARMUtils_h_ |