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