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