Johnny Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame] | 1 | //===-- lldb_ARMUtils.h -----------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef lldb_ARMUtils_h_ |
| 11 | #define lldb_ARMUtils_h_ |
| 12 | |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 13 | #include "InstructionUtils.h" |
| 14 | |
Johnny Chen | c3b5bc3 | 2011-01-24 19:50:30 +0000 | [diff] [blame] | 15 | // Common utilities for the ARM/Thumb Instruction Set Architecture. |
Johnny Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame] | 16 | |
| 17 | namespace lldb_private { |
| 18 | |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 19 | static inline uint32_t bits(const uint32_t val, const uint32_t msbit, const uint32_t lsbit) |
| 20 | { |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 21 | return Bits32(val, msbit, lsbit); |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | static inline uint32_t bit(const uint32_t val, const uint32_t msbit) |
| 25 | { |
| 26 | return bits(val, msbit, msbit); |
| 27 | } |
| 28 | |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 29 | 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] | 30 | { |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 31 | uint32_t m = shift % N; |
| 32 | return (val >> m) | (val << (N - m)); |
| 33 | } |
| 34 | |
Johnny Chen | 07f2f5a | 2011-02-14 19:08:41 +0000 | [diff] [blame] | 35 | // (imm32, carry_out) = ARMExpandImm_C(imm12, carry_in) |
| 36 | static inline uint32_t ARMExpandImm_C(uint32_t val, uint32_t carry_in, uint32_t &carry_out) |
| 37 | { |
| 38 | uint32_t imm32; // the expanded result |
| 39 | uint32_t imm = bits(val, 7, 0); // immediate value |
| 40 | uint32_t amt = 2 * bits(val, 11, 8); // rotate amount |
| 41 | if (amt == 0) |
| 42 | { |
| 43 | imm32 = imm; |
| 44 | carry_out = carry_in; |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | imm32 = ror(imm, 32, amt); |
| 49 | carry_out = Bit32(imm32, 31); |
| 50 | } |
| 51 | return imm32; |
| 52 | } |
| 53 | |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 54 | static inline uint32_t ARMExpandImm(uint32_t val) |
| 55 | { |
Johnny Chen | 07f2f5a | 2011-02-14 19:08:41 +0000 | [diff] [blame] | 56 | // 'carry_in' argument to following function call does not affect the imm32 result. |
| 57 | uint32_t carry_in = 0; |
| 58 | uint32_t carry_out; |
| 59 | return ARMExpandImm_C(val, carry_in, carry_out); |
| 60 | } |
| 61 | |
| 62 | // (imm32, carry_out) = ThumbExpandImm_C(imm12, carry_in) |
| 63 | static inline uint32_t ThumbExpandImm_C(uint32_t val, uint32_t carry_in, uint32_t &carry_out) |
| 64 | { |
Johnny Chen | 3e9a41c | 2011-02-14 19:09:36 +0000 | [diff] [blame] | 65 | uint32_t imm32; // the expaned result |
Johnny Chen | 07f2f5a | 2011-02-14 19:08:41 +0000 | [diff] [blame] | 66 | const uint32_t i = bit(val, 26); |
| 67 | const uint32_t imm3 = bits(val, 14, 12); |
| 68 | const uint32_t abcdefgh = bits(val, 7, 0); |
| 69 | const uint32_t imm12 = i << 11 | imm3 << 8 | abcdefgh; |
| 70 | |
| 71 | if (bits(imm12, 11, 10) == 0) |
| 72 | { |
| 73 | switch (bits(imm12, 8, 9)) { |
| 74 | case 0: |
| 75 | imm32 = abcdefgh; |
| 76 | break; |
| 77 | |
| 78 | case 1: |
| 79 | imm32 = abcdefgh << 16 | abcdefgh; |
| 80 | break; |
| 81 | |
| 82 | case 2: |
| 83 | imm32 = abcdefgh << 24 | abcdefgh << 8; |
| 84 | break; |
| 85 | |
| 86 | case 3: |
| 87 | imm32 = abcdefgh << 24 | abcdefgh << 16 | abcdefgh << 8 | abcdefgh; |
| 88 | break; |
| 89 | } |
| 90 | carry_out = carry_in; |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | const uint32_t unrotated_value = 0x80 | bits(imm12, 6, 0); |
| 95 | imm32 = ror(unrotated_value, 32, bits(imm12, 11, 7)); |
| 96 | carry_out = Bit32(imm32, 31); |
| 97 | } |
| 98 | return imm32; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 101 | static inline uint32_t ThumbExpandImm(uint32_t val) |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 102 | { |
Johnny Chen | 07f2f5a | 2011-02-14 19:08:41 +0000 | [diff] [blame] | 103 | // 'carry_in' argument to following function call does not affect the imm32 result. |
| 104 | uint32_t carry_in = 0; |
| 105 | uint32_t carry_out; |
| 106 | return ThumbExpandImm_C(val, carry_in, carry_out); |
Johnny Chen | 60c0d62 | 2011-01-25 23:49:39 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // imm32 = ZeroExtend(i:imm3:imm8, 32) |
| 110 | static inline uint32_t ThumbImm12(uint32_t val) |
| 111 | { |
| 112 | const uint32_t i = bit(val, 26); |
| 113 | const uint32_t imm3 = bits(val, 14, 12); |
| 114 | const uint32_t imm8 = bits(val, 7, 0); |
| 115 | const uint32_t imm12 = i << 11 | imm3 << 8 | imm8; |
| 116 | return imm12; |
Johnny Chen | 4c0e0bc | 2011-01-25 22:45:28 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Johnny Chen | e445502 | 2011-01-26 00:08:59 +0000 | [diff] [blame] | 119 | // imm32 = ZeroExtend(imm7:'00', 32) |
| 120 | static inline uint32_t ThumbImmScaled(uint32_t val) |
| 121 | { |
| 122 | const uint32_t imm7 = bits(val, 6, 0); |
| 123 | return imm7 * 4; |
| 124 | } |
| 125 | |
Johnny Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame] | 126 | // This function performs the check for the register numbers 13 and 15 that are |
| 127 | // not permitted for many Thumb register specifiers. |
| 128 | static inline bool BadReg(uint32_t n) { return n == 13 || n == 15; } |
| 129 | |
Johnny Chen | 4baf2e3 | 2011-01-24 18:24:53 +0000 | [diff] [blame] | 130 | } // namespace lldb_private |
| 131 | |
| 132 | #endif // lldb_ARMUtils_h_ |