Greg Clayton | 7349bd9 | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 1 | //===-- InstructionUtils.h --------------------------------------*- C++ -*-===// |
Johnny Chen | 74889b2 | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Johnny Chen | 74889b2 | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef lldb_InstructionUtils_h_ |
| 10 | #define lldb_InstructionUtils_h_ |
| 11 | |
Raphael Isemann | 02d4ff4 | 2018-05-26 14:59:14 +0000 | [diff] [blame] | 12 | #include <cassert> |
| 13 | #include <cstdint> |
| 14 | |
Johnny Chen | 74889b2 | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 15 | // Common utilities for manipulating instruction bit fields. |
| 16 | |
| 17 | namespace lldb_private { |
| 18 | |
Johnny Chen | 61938f7 | 2011-02-12 01:01:40 +0000 | [diff] [blame] | 19 | // 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] | 20 | // least significant bit (lsbit) of a 64-bit unsigned value. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 21 | static inline uint64_t Bits64(const uint64_t bits, const uint32_t msbit, |
| 22 | const uint32_t lsbit) { |
| 23 | assert(msbit < 64 && lsbit <= msbit); |
| 24 | return (bits >> lsbit) & ((1ull << (msbit - lsbit + 1)) - 1); |
Caroline Tice | b5c6a3e | 2011-03-31 03:26:23 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | // 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] | 28 | // least significant bit (lsbit) of a 32-bit unsigned value. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | static inline uint32_t Bits32(const uint32_t bits, const uint32_t msbit, |
| 30 | const uint32_t lsbit) { |
| 31 | assert(msbit < 32 && lsbit <= msbit); |
| 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. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | static inline uint32_t Bit32(const uint32_t bits, const uint32_t bit) { |
| 37 | return (bits >> bit) & 1u; |
Greg Clayton | 7349bd9 | 2011-05-09 20:18:18 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | static inline uint64_t Bit64(const uint64_t bits, const uint32_t bit) { |
| 41 | return (bits >> bit) & 1ull; |
Johnny Chen | 0cfda5b | 2011-02-10 19:29:03 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Johnny Chen | 61938f7 | 2011-02-12 01:01:40 +0000 | [diff] [blame] | 44 | // Set the bit field(s) from the most significant bit (msbit) to the |
| 45 | // least significant bit (lsbit) of a 32-bit unsigned value to 'val'. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 46 | static inline void SetBits32(uint32_t &bits, const uint32_t msbit, |
| 47 | const uint32_t lsbit, const uint32_t val) { |
| 48 | assert(msbit < 32 && lsbit < 32 && msbit >= lsbit); |
| 49 | uint32_t mask = ((1u << (msbit - lsbit + 1)) - 1); |
| 50 | bits &= ~(mask << lsbit); |
| 51 | bits |= (val & mask) << lsbit; |
Johnny Chen | ea745e8 | 2011-02-04 23:02:47 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Johnny Chen | 61938f7 | 2011-02-12 01:01:40 +0000 | [diff] [blame] | 54 | // Set the 'bit' position of a 32-bit unsigned value to 'val'. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | static inline void SetBit32(uint32_t &bits, const uint32_t bit, |
| 56 | const uint32_t val) { |
| 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. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | static inline uint32_t Rotr32(uint32_t bits, uint32_t amt) { |
| 62 | assert(amt < 32 && "Invalid rotate amount"); |
| 63 | return (bits >> amt) | (bits << ((32 - amt) & 31)); |
Johnny Chen | 722d4e4 | 2011-02-11 23:29:14 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Johnny Chen | 61938f7 | 2011-02-12 01:01:40 +0000 | [diff] [blame] | 66 | // Rotate a 32-bit unsigned value left by the specified amount. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | static inline uint32_t Rotl32(uint32_t bits, uint32_t amt) { |
| 68 | assert(amt < 32 && "Invalid rotate amount"); |
| 69 | return (bits << amt) | (bits >> ((32 - amt) & 31)); |
Johnny Chen | 722d4e4 | 2011-02-11 23:29:14 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Johnny Chen | 74889b2 | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 72 | // Create a mask that starts at bit zero and includes "bit" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | static inline uint64_t MaskUpToBit(const uint64_t bit) { |
| 74 | if (bit >= 63) |
| 75 | return -1ll; |
| 76 | return (1ull << (bit + 1ull)) - 1ull; |
Johnny Chen | 74889b2 | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Johnny Chen | 61938f7 | 2011-02-12 01:01:40 +0000 | [diff] [blame] | 79 | // Return an integer result equal to the number of bits of x that are ones. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | static inline uint32_t BitCount(uint64_t x) { |
| 81 | // c accumulates the total bits set in x |
| 82 | uint32_t c; |
| 83 | for (c = 0; x; ++c) { |
| 84 | x &= x - 1; // clear the least significant bit set |
| 85 | } |
| 86 | return c; |
Johnny Chen | 74889b2 | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | static inline bool BitIsSet(const uint64_t value, const uint64_t bit) { |
| 90 | return (value & (1ull << bit)) != 0; |
Johnny Chen | 74889b2 | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | static inline bool BitIsClear(const uint64_t value, const uint64_t bit) { |
| 94 | return (value & (1ull << bit)) == 0; |
Johnny Chen | 74889b2 | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 97 | static inline uint64_t UnsignedBits(const uint64_t value, const uint64_t msbit, |
| 98 | const uint64_t lsbit) { |
| 99 | uint64_t result = value >> lsbit; |
| 100 | result &= MaskUpToBit(msbit - lsbit); |
| 101 | return result; |
Johnny Chen | 74889b2 | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | static inline int64_t SignedBits(const uint64_t value, const uint64_t msbit, |
| 105 | const uint64_t lsbit) { |
| 106 | uint64_t result = UnsignedBits(value, msbit, lsbit); |
| 107 | if (BitIsSet(value, msbit)) { |
| 108 | // Sign extend |
| 109 | result |= ~MaskUpToBit(msbit - lsbit); |
| 110 | } |
| 111 | return result; |
Johnny Chen | 74889b2 | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | } // namespace lldb_private |
Johnny Chen | 74889b2 | 2011-01-26 01:00:55 +0000 | [diff] [blame] | 115 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | #endif // lldb_InstructionUtils_h_ |