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 | |
| 17 | static inline uint32_t |
| 18 | Bits32 (const uint32_t value, const uint32_t msbit, const uint32_t lsbit) |
| 19 | { |
| 20 | assert(msbit < 32 && lsbit <= msbit); |
| 21 | return (value >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1); |
| 22 | } |
| 23 | |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 24 | static inline uint32_t |
| 25 | Bits32 (const uint32_t value, const uint32_t bit) |
| 26 | { |
| 27 | return Bits32(value, bit, bit); |
| 28 | } |
| 29 | |
Johnny Chen | 9307047 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 30 | static inline void |
| 31 | SetBits32(uint32_t &bits, unsigned msbit, unsigned lsbit, unsigned val) |
| 32 | { |
| 33 | assert(msbit < 32 && lsbit < 32 && msbit >= lsbit); |
| 34 | uint32_t mask = ((1 << (msbit - lsbit + 1)) - 1); |
| 35 | bits &= ~(mask << lsbit); |
| 36 | bits |= (val & mask) << lsbit; |
| 37 | } |
| 38 | |
Johnny Chen | 338bf54 | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 39 | static inline void |
| 40 | SetBits32(uint32_t &bits, unsigned bit, unsigned val) |
| 41 | { |
| 42 | SetBits32(bits, bit, val); |
| 43 | } |
| 44 | |
Johnny Chen | 108d5aa | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 45 | // Create a mask that starts at bit zero and includes "bit" |
| 46 | static inline uint64_t |
| 47 | MaskUpToBit (const uint64_t bit) |
| 48 | { |
| 49 | return (1ull << (bit + 1ull)) - 1ull; |
| 50 | } |
| 51 | |
| 52 | // Returns an integer result equal to the number of bits of x that are ones. |
| 53 | static inline uint32_t |
| 54 | BitCount (uint64_t x) |
| 55 | { |
| 56 | // c accumulates the total bits set in x |
| 57 | uint32_t c; |
| 58 | for (c = 0; x; ++c) |
| 59 | { |
| 60 | x &= x - 1; // clear the least significant bit set |
| 61 | } |
| 62 | return c; |
| 63 | } |
| 64 | |
| 65 | static inline bool |
| 66 | BitIsSet (const uint64_t value, const uint64_t bit) |
| 67 | { |
| 68 | return (value & (1ull << bit)) != 0; |
| 69 | } |
| 70 | |
| 71 | static inline bool |
| 72 | BitIsClear (const uint64_t value, const uint64_t bit) |
| 73 | { |
| 74 | return (value & (1ull << bit)) == 0; |
| 75 | } |
| 76 | |
| 77 | static inline uint64_t |
| 78 | UnsignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit) |
| 79 | { |
| 80 | uint64_t result = value >> lsbit; |
| 81 | result &= MaskUpToBit (msbit - lsbit); |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | static inline int64_t |
| 86 | SignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit) |
| 87 | { |
| 88 | uint64_t result = UnsignedBits (value, msbit, lsbit); |
| 89 | if (BitIsSet(value, msbit)) |
| 90 | { |
| 91 | // Sign extend |
| 92 | result |= ~MaskUpToBit (msbit - lsbit); |
| 93 | } |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | } // namespace lldb_private |
| 98 | |
| 99 | #endif // lldb_InstructionUtils_h_ |