]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/Process/Utility/InstructionUtils.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / Process / Utility / InstructionUtils.h
1 //===-- 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 #include <cassert>
14 #include <cstdint>
15
16 // Common utilities for manipulating instruction bit fields.
17
18 namespace lldb_private {
19
20 // Return the bit field(s) from the most significant bit (msbit) to the
21 // least significant bit (lsbit) of a 64-bit unsigned value.
22 static inline uint64_t Bits64(const uint64_t bits, const uint32_t msbit,
23                               const uint32_t lsbit) {
24   assert(msbit < 64 && lsbit <= msbit);
25   return (bits >> lsbit) & ((1ull << (msbit - lsbit + 1)) - 1);
26 }
27
28 // Return the bit field(s) from the most significant bit (msbit) to the
29 // least significant bit (lsbit) of a 32-bit unsigned value.
30 static inline uint32_t Bits32(const uint32_t bits, const uint32_t msbit,
31                               const uint32_t lsbit) {
32   assert(msbit < 32 && lsbit <= msbit);
33   return (bits >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1);
34 }
35
36 // Return the bit value from the 'bit' position of a 32-bit unsigned value.
37 static inline uint32_t Bit32(const uint32_t bits, const uint32_t bit) {
38   return (bits >> bit) & 1u;
39 }
40
41 static inline uint64_t Bit64(const uint64_t bits, const uint32_t bit) {
42   return (bits >> bit) & 1ull;
43 }
44
45 // Set the bit field(s) from the most significant bit (msbit) to the
46 // least significant bit (lsbit) of a 32-bit unsigned value to 'val'.
47 static inline void SetBits32(uint32_t &bits, const uint32_t msbit,
48                              const uint32_t lsbit, const uint32_t val) {
49   assert(msbit < 32 && lsbit < 32 && msbit >= lsbit);
50   uint32_t mask = ((1u << (msbit - lsbit + 1)) - 1);
51   bits &= ~(mask << lsbit);
52   bits |= (val & mask) << lsbit;
53 }
54
55 // Set the 'bit' position of a 32-bit unsigned value to 'val'.
56 static inline void SetBit32(uint32_t &bits, const uint32_t bit,
57                             const uint32_t val) {
58   SetBits32(bits, bit, bit, val);
59 }
60
61 // Rotate a 32-bit unsigned value right by the specified amount.
62 static inline uint32_t Rotr32(uint32_t bits, uint32_t amt) {
63   assert(amt < 32 && "Invalid rotate amount");
64   return (bits >> amt) | (bits << ((32 - amt) & 31));
65 }
66
67 // Rotate a 32-bit unsigned value left by the specified amount.
68 static inline uint32_t Rotl32(uint32_t bits, uint32_t amt) {
69   assert(amt < 32 && "Invalid rotate amount");
70   return (bits << amt) | (bits >> ((32 - amt) & 31));
71 }
72
73 // Create a mask that starts at bit zero and includes "bit"
74 static inline uint64_t MaskUpToBit(const uint64_t bit) {
75   if (bit >= 63)
76     return -1ll;
77   return (1ull << (bit + 1ull)) - 1ull;
78 }
79
80 // Return an integer result equal to the number of bits of x that are ones.
81 static inline uint32_t BitCount(uint64_t x) {
82   // c accumulates the total bits set in x
83   uint32_t c;
84   for (c = 0; x; ++c) {
85     x &= x - 1; // clear the least significant bit set
86   }
87   return c;
88 }
89
90 static inline bool BitIsSet(const uint64_t value, const uint64_t bit) {
91   return (value & (1ull << bit)) != 0;
92 }
93
94 static inline bool BitIsClear(const uint64_t value, const uint64_t bit) {
95   return (value & (1ull << bit)) == 0;
96 }
97
98 static inline uint64_t UnsignedBits(const uint64_t value, const uint64_t msbit,
99                                     const uint64_t lsbit) {
100   uint64_t result = value >> lsbit;
101   result &= MaskUpToBit(msbit - lsbit);
102   return result;
103 }
104
105 static inline int64_t SignedBits(const uint64_t value, const uint64_t msbit,
106                                  const uint64_t lsbit) {
107   uint64_t result = UnsignedBits(value, msbit, lsbit);
108   if (BitIsSet(value, msbit)) {
109     // Sign extend
110     result |= ~MaskUpToBit(msbit - lsbit);
111   }
112   return result;
113 }
114
115 } // namespace lldb_private
116
117 #endif // lldb_InstructionUtils_h_