blob: c03b63e76447d66be2e7af0b0c1687b004a571fb [file] [log] [blame]
Johnny Chen74889b22011-01-26 01:00:55 +00001//===-- 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
15namespace lldb_private {
16
Johnny Chen61938f72011-02-12 01:01:40 +000017// Return the bit field(s) from the most significant bit (msbit) to the
Caroline Ticeb5c6a3e2011-03-31 03:26:23 +000018// least significant bit (lsbit) of a 64-bit unsigned value.
19static inline uint64_t
20Bits64 (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 Chen722d4e42011-02-11 23:29:14 +000027// least significant bit (lsbit) of a 32-bit unsigned value.
Johnny Chen74889b22011-01-26 01:00:55 +000028static inline uint32_t
Johnny Chen95241102011-02-10 21:49:16 +000029Bits32 (const uint32_t bits, const uint32_t msbit, const uint32_t lsbit)
Johnny Chen74889b22011-01-26 01:00:55 +000030{
31 assert(msbit < 32 && lsbit <= msbit);
Johnny Chen95241102011-02-10 21:49:16 +000032 return (bits >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1);
Johnny Chen74889b22011-01-26 01:00:55 +000033}
34
Johnny Chen61938f72011-02-12 01:01:40 +000035// Return the bit value from the 'bit' position of a 32-bit unsigned value.
Johnny Chen0cfda5b2011-02-10 19:29:03 +000036static inline uint32_t
Johnny Chen95241102011-02-10 21:49:16 +000037Bit32 (const uint32_t bits, const uint32_t bit)
Johnny Chen0cfda5b2011-02-10 19:29:03 +000038{
Johnny Chen95241102011-02-10 21:49:16 +000039 return Bits32(bits, bit, bit);
Johnny Chen0cfda5b2011-02-10 19:29:03 +000040}
41
Johnny Chen61938f72011-02-12 01:01:40 +000042// 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 Chenea745e82011-02-04 23:02:47 +000044static inline void
Johnny Chen95241102011-02-10 21:49:16 +000045SetBits32(uint32_t &bits, const uint32_t msbit, const uint32_t lsbit, const uint32_t val)
Johnny Chenea745e82011-02-04 23:02:47 +000046{
47 assert(msbit < 32 && lsbit < 32 && msbit >= lsbit);
Johnny Chen95241102011-02-10 21:49:16 +000048 uint32_t mask = ((1u << (msbit - lsbit + 1)) - 1);
Johnny Chenea745e82011-02-04 23:02:47 +000049 bits &= ~(mask << lsbit);
50 bits |= (val & mask) << lsbit;
51}
52
Johnny Chen61938f72011-02-12 01:01:40 +000053// Set the 'bit' position of a 32-bit unsigned value to 'val'.
Johnny Chen0cfda5b2011-02-10 19:29:03 +000054static inline void
Johnny Chen95241102011-02-10 21:49:16 +000055SetBit32(uint32_t &bits, const uint32_t bit, const uint32_t val)
Johnny Chen0cfda5b2011-02-10 19:29:03 +000056{
Johnny Chenc843a782011-02-10 21:39:01 +000057 SetBits32(bits, bit, bit, val);
Johnny Chen0cfda5b2011-02-10 19:29:03 +000058}
59
Johnny Chen61938f72011-02-12 01:01:40 +000060// Rotate a 32-bit unsigned value right by the specified amount.
Johnny Chen722d4e42011-02-11 23:29:14 +000061static inline uint32_t
62Rotr32 (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 Chen61938f72011-02-12 01:01:40 +000068// Rotate a 32-bit unsigned value left by the specified amount.
Johnny Chen722d4e42011-02-11 23:29:14 +000069static inline uint32_t
70Rotl32 (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 Chen74889b22011-01-26 01:00:55 +000076// Create a mask that starts at bit zero and includes "bit"
77static inline uint64_t
78MaskUpToBit (const uint64_t bit)
79{
80 return (1ull << (bit + 1ull)) - 1ull;
81}
82
Johnny Chen61938f72011-02-12 01:01:40 +000083// Return an integer result equal to the number of bits of x that are ones.
Johnny Chen74889b22011-01-26 01:00:55 +000084static inline uint32_t
85BitCount (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
96static inline bool
97BitIsSet (const uint64_t value, const uint64_t bit)
98{
99 return (value & (1ull << bit)) != 0;
100}
101
102static inline bool
103BitIsClear (const uint64_t value, const uint64_t bit)
104{
105 return (value & (1ull << bit)) == 0;
106}
107
108static inline uint64_t
109UnsignedBits (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
116static inline int64_t
117SignedBits (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_