blob: e7b1446a5c5cc0ece630cff9df361aef836ab58c [file] [log] [blame]
Johnny Chen108d5aa2011-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 Chenf5588602011-02-11 23:29:14 +000017// Bits32 - Return the bit field(s) from the most significant bit (msbit) to the
18// least significant bit (lsbit) of a 32-bit unsigned value.
19//
Johnny Chen108d5aa2011-01-26 01:00:55 +000020static inline uint32_t
Johnny Chenc3a38ea2011-02-10 21:49:16 +000021Bits32 (const uint32_t bits, const uint32_t msbit, const uint32_t lsbit)
Johnny Chen108d5aa2011-01-26 01:00:55 +000022{
23 assert(msbit < 32 && lsbit <= msbit);
Johnny Chenc3a38ea2011-02-10 21:49:16 +000024 return (bits >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1);
Johnny Chen108d5aa2011-01-26 01:00:55 +000025}
26
Johnny Chenf5588602011-02-11 23:29:14 +000027// Bit32 - Return the bit from the 'bit' position of a 32-bit unsigned value.
28//
Johnny Chen338bf542011-02-10 19:29:03 +000029static inline uint32_t
Johnny Chenc3a38ea2011-02-10 21:49:16 +000030Bit32 (const uint32_t bits, const uint32_t bit)
Johnny Chen338bf542011-02-10 19:29:03 +000031{
Johnny Chenc3a38ea2011-02-10 21:49:16 +000032 return Bits32(bits, bit, bit);
Johnny Chen338bf542011-02-10 19:29:03 +000033}
34
Johnny Chenf5588602011-02-11 23:29:14 +000035// SetBits32 - Set the bit field(s) from the most significant bit (msbit) to the
36// least significant bit (lsbit) of a 32-bit unsigned value as 'val'.
37//
Johnny Chen93070472011-02-04 23:02:47 +000038static inline void
Johnny Chenc3a38ea2011-02-10 21:49:16 +000039SetBits32(uint32_t &bits, const uint32_t msbit, const uint32_t lsbit, const uint32_t val)
Johnny Chen93070472011-02-04 23:02:47 +000040{
41 assert(msbit < 32 && lsbit < 32 && msbit >= lsbit);
Johnny Chenc3a38ea2011-02-10 21:49:16 +000042 uint32_t mask = ((1u << (msbit - lsbit + 1)) - 1);
Johnny Chen93070472011-02-04 23:02:47 +000043 bits &= ~(mask << lsbit);
44 bits |= (val & mask) << lsbit;
45}
46
Johnny Chenf5588602011-02-11 23:29:14 +000047// SetBit32 - Set the 'bit' position of a 32-bit unsigned value as 'val'.
48//
Johnny Chen338bf542011-02-10 19:29:03 +000049static inline void
Johnny Chenc3a38ea2011-02-10 21:49:16 +000050SetBit32(uint32_t &bits, const uint32_t bit, const uint32_t val)
Johnny Chen338bf542011-02-10 19:29:03 +000051{
Johnny Chenbd599902011-02-10 21:39:01 +000052 SetBits32(bits, bit, bit, val);
Johnny Chen338bf542011-02-10 19:29:03 +000053}
54
Johnny Chenf5588602011-02-11 23:29:14 +000055// Rotr32 - Rotate a 32-bit unsigned value right by the specified amount.
56//
57static inline uint32_t
58Rotr32 (uint32_t bits, uint32_t amt)
59{
60 assert(amt < 32 && "Invalid rotate amount");
61 return (bits >> amt) | (bits << ((32-amt)&31));
62}
63
64// Rotl32 - Rotate a 32-bit unsigned value left by the specified amount.
65//
66static inline uint32_t
67Rotl32 (uint32_t bits, uint32_t amt)
68{
69 assert(amt < 32 && "Invalid rotate amount");
70 return (bits << amt) | (bits >> ((32-amt)&31));
71}
72
Johnny Chen108d5aa2011-01-26 01:00:55 +000073// Create a mask that starts at bit zero and includes "bit"
74static inline uint64_t
75MaskUpToBit (const uint64_t bit)
76{
77 return (1ull << (bit + 1ull)) - 1ull;
78}
79
80// Returns an integer result equal to the number of bits of x that are ones.
81static inline uint32_t
82BitCount (uint64_t x)
83{
84 // c accumulates the total bits set in x
85 uint32_t c;
86 for (c = 0; x; ++c)
87 {
88 x &= x - 1; // clear the least significant bit set
89 }
90 return c;
91}
92
93static inline bool
94BitIsSet (const uint64_t value, const uint64_t bit)
95{
96 return (value & (1ull << bit)) != 0;
97}
98
99static inline bool
100BitIsClear (const uint64_t value, const uint64_t bit)
101{
102 return (value & (1ull << bit)) == 0;
103}
104
105static inline uint64_t
106UnsignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
107{
108 uint64_t result = value >> lsbit;
109 result &= MaskUpToBit (msbit - lsbit);
110 return result;
111}
112
113static inline int64_t
114SignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
115{
116 uint64_t result = UnsignedBits (value, msbit, lsbit);
117 if (BitIsSet(value, msbit))
118 {
119 // Sign extend
120 result |= ~MaskUpToBit (msbit - lsbit);
121 }
122 return result;
123}
124
125} // namespace lldb_private
126
127#endif // lldb_InstructionUtils_h_