Greg Clayton | 061b79d | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 1 | //===-- InstructionUtils.h --------------------------------------*- C++ -*-===// |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +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_InstructionUtils_h_ |
| 11 | #define lldb_InstructionUtils_h_ |
| 12 | |
| 13 | // Common utilities for manipulating instruction bit fields. |
| 14 | |
| 15 | namespace lldb_private { |
| 16 | |
Johnny Chen | 7da805c | 2011-02-12 01:01:40 +0000 | [diff] [blame] | 17 | // Return the bit field(s) from the most significant bit (msbit) to the |
Caroline Tice | bf5a66b | 2011-03-31 03:26:23 +0000 | [diff] [blame] | 18 | // least significant bit (lsbit) of a 64-bit unsigned value. |
| 19 | static inline uint64_t |
| 20 | Bits64 (const uint64_t bits, const uint32_t msbit, const uint32_t lsbit) |
| 21 | { |
| 22 | assert(msbit < 64 && lsbit <= msbit); |
| 23 | return (bits >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1); |
| 24 | } |
| 25 | |
| 26 | // Return the bit field(s) from the most significant bit (msbit) to the |
Johnny Chen | f558860 | 2011-02-11 23:29:14 +0000 | [diff] [blame] | 27 | // least significant bit (lsbit) of a 32-bit unsigned value. |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 28 | static inline uint32_t |
Johnny Chen | c3a38ea | 2011-02-10 21:49:16 +0000 | [diff] [blame] | 29 | Bits32 (const uint32_t bits, const uint32_t msbit, const uint32_t lsbit) |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 30 | { |
| 31 | assert(msbit < 32 && lsbit <= msbit); |
Johnny Chen | c3a38ea | 2011-02-10 21:49:16 +0000 | [diff] [blame] | 32 | return (bits >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1); |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Johnny Chen | 7da805c | 2011-02-12 01:01:40 +0000 | [diff] [blame] | 35 | // Return the bit value from the 'bit' position of a 32-bit unsigned value. |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 36 | static inline uint32_t |
Johnny Chen | c3a38ea | 2011-02-10 21:49:16 +0000 | [diff] [blame] | 37 | Bit32 (const uint32_t bits, const uint32_t bit) |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 38 | { |
Greg Clayton | 061b79d | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 39 | return (bits >> bit) & 1u; |
| 40 | } |
| 41 | |
| 42 | static inline uint64_t |
| 43 | Bit64 (const uint64_t bits, const uint32_t bit) |
| 44 | { |
| 45 | return (bits >> bit) & 1ull; |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Johnny Chen | 7da805c | 2011-02-12 01:01:40 +0000 | [diff] [blame] | 48 | // Set the bit field(s) from the most significant bit (msbit) to the |
| 49 | // least significant bit (lsbit) of a 32-bit unsigned value to 'val'. |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 50 | static inline void |
Johnny Chen | c3a38ea | 2011-02-10 21:49:16 +0000 | [diff] [blame] | 51 | SetBits32(uint32_t &bits, const uint32_t msbit, const uint32_t lsbit, const uint32_t val) |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 52 | { |
| 53 | assert(msbit < 32 && lsbit < 32 && msbit >= lsbit); |
Johnny Chen | c3a38ea | 2011-02-10 21:49:16 +0000 | [diff] [blame] | 54 | uint32_t mask = ((1u << (msbit - lsbit + 1)) - 1); |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 55 | bits &= ~(mask << lsbit); |
| 56 | bits |= (val & mask) << lsbit; |
| 57 | } |
| 58 | |
Johnny Chen | 7da805c | 2011-02-12 01:01:40 +0000 | [diff] [blame] | 59 | // Set the 'bit' position of a 32-bit unsigned value to 'val'. |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 60 | static inline void |
Johnny Chen | c3a38ea | 2011-02-10 21:49:16 +0000 | [diff] [blame] | 61 | SetBit32(uint32_t &bits, const uint32_t bit, const uint32_t val) |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 62 | { |
Johnny Chen | bd59990 | 2011-02-10 21:39:01 +0000 | [diff] [blame] | 63 | SetBits32(bits, bit, bit, val); |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Johnny Chen | 7da805c | 2011-02-12 01:01:40 +0000 | [diff] [blame] | 66 | // Rotate a 32-bit unsigned value right by the specified amount. |
Johnny Chen | f558860 | 2011-02-11 23:29:14 +0000 | [diff] [blame] | 67 | static inline uint32_t |
| 68 | Rotr32 (uint32_t bits, uint32_t amt) |
| 69 | { |
| 70 | assert(amt < 32 && "Invalid rotate amount"); |
| 71 | return (bits >> amt) | (bits << ((32-amt)&31)); |
| 72 | } |
| 73 | |
Johnny Chen | 7da805c | 2011-02-12 01:01:40 +0000 | [diff] [blame] | 74 | // Rotate a 32-bit unsigned value left by the specified amount. |
Johnny Chen | f558860 | 2011-02-11 23:29:14 +0000 | [diff] [blame] | 75 | static inline uint32_t |
| 76 | Rotl32 (uint32_t bits, uint32_t amt) |
| 77 | { |
| 78 | assert(amt < 32 && "Invalid rotate amount"); |
| 79 | return (bits << amt) | (bits >> ((32-amt)&31)); |
| 80 | } |
| 81 | |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 82 | // Create a mask that starts at bit zero and includes "bit" |
| 83 | static inline uint64_t |
| 84 | MaskUpToBit (const uint64_t bit) |
| 85 | { |
| 86 | return (1ull << (bit + 1ull)) - 1ull; |
| 87 | } |
| 88 | |
Johnny Chen | 7da805c | 2011-02-12 01:01:40 +0000 | [diff] [blame] | 89 | // Return an integer result equal to the number of bits of x that are ones. |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 90 | static inline uint32_t |
| 91 | BitCount (uint64_t x) |
| 92 | { |
| 93 | // c accumulates the total bits set in x |
| 94 | uint32_t c; |
| 95 | for (c = 0; x; ++c) |
| 96 | { |
| 97 | x &= x - 1; // clear the least significant bit set |
| 98 | } |
| 99 | return c; |
| 100 | } |
| 101 | |
| 102 | static inline bool |
| 103 | BitIsSet (const uint64_t value, const uint64_t bit) |
| 104 | { |
| 105 | return (value & (1ull << bit)) != 0; |
| 106 | } |
| 107 | |
| 108 | static inline bool |
| 109 | BitIsClear (const uint64_t value, const uint64_t bit) |
| 110 | { |
| 111 | return (value & (1ull << bit)) == 0; |
| 112 | } |
| 113 | |
| 114 | static inline uint64_t |
| 115 | UnsignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit) |
| 116 | { |
| 117 | uint64_t result = value >> lsbit; |
| 118 | result &= MaskUpToBit (msbit - lsbit); |
| 119 | return result; |
| 120 | } |
| 121 | |
| 122 | static inline int64_t |
| 123 | SignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit) |
| 124 | { |
| 125 | uint64_t result = UnsignedBits (value, msbit, lsbit); |
| 126 | if (BitIsSet(value, msbit)) |
| 127 | { |
| 128 | // Sign extend |
| 129 | result |= ~MaskUpToBit (msbit - lsbit); |
| 130 | } |
| 131 | return result; |
| 132 | } |
| 133 | |
| 134 | } // namespace lldb_private |
| 135 | |
| 136 | #endif // lldb_InstructionUtils_h_ |