blob: 1bbfad8a93ec42372338dddddea332521d810d31 [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 Chen7da805c2011-02-12 01:01:40 +000017// Return the bit field(s) from the most significant bit (msbit) to the
Johnny Chenf5588602011-02-11 23:29:14 +000018// least significant bit (lsbit) of a 32-bit unsigned value.
Johnny Chen108d5aa2011-01-26 01:00:55 +000019static inline uint32_t
Johnny Chenc3a38ea2011-02-10 21:49:16 +000020Bits32 (const uint32_t bits, const uint32_t msbit, const uint32_t lsbit)
Johnny Chen108d5aa2011-01-26 01:00:55 +000021{
22 assert(msbit < 32 && lsbit <= msbit);
Johnny Chenc3a38ea2011-02-10 21:49:16 +000023 return (bits >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1);
Johnny Chen108d5aa2011-01-26 01:00:55 +000024}
25
Johnny Chen7da805c2011-02-12 01:01:40 +000026// Return the bit value from the 'bit' position of a 32-bit unsigned value.
Johnny Chen338bf542011-02-10 19:29:03 +000027static inline uint32_t
Johnny Chenc3a38ea2011-02-10 21:49:16 +000028Bit32 (const uint32_t bits, const uint32_t bit)
Johnny Chen338bf542011-02-10 19:29:03 +000029{
Johnny Chenc3a38ea2011-02-10 21:49:16 +000030 return Bits32(bits, bit, bit);
Johnny Chen338bf542011-02-10 19:29:03 +000031}
32
Johnny Chen7da805c2011-02-12 01:01:40 +000033// Set the bit field(s) from the most significant bit (msbit) to the
34// least significant bit (lsbit) of a 32-bit unsigned value to 'val'.
Johnny Chen93070472011-02-04 23:02:47 +000035static inline void
Johnny Chenc3a38ea2011-02-10 21:49:16 +000036SetBits32(uint32_t &bits, const uint32_t msbit, const uint32_t lsbit, const uint32_t val)
Johnny Chen93070472011-02-04 23:02:47 +000037{
38 assert(msbit < 32 && lsbit < 32 && msbit >= lsbit);
Johnny Chenc3a38ea2011-02-10 21:49:16 +000039 uint32_t mask = ((1u << (msbit - lsbit + 1)) - 1);
Johnny Chen93070472011-02-04 23:02:47 +000040 bits &= ~(mask << lsbit);
41 bits |= (val & mask) << lsbit;
42}
43
Johnny Chen7da805c2011-02-12 01:01:40 +000044// Set the 'bit' position of a 32-bit unsigned value to 'val'.
Johnny Chen338bf542011-02-10 19:29:03 +000045static inline void
Johnny Chenc3a38ea2011-02-10 21:49:16 +000046SetBit32(uint32_t &bits, const uint32_t bit, const uint32_t val)
Johnny Chen338bf542011-02-10 19:29:03 +000047{
Johnny Chenbd599902011-02-10 21:39:01 +000048 SetBits32(bits, bit, bit, val);
Johnny Chen338bf542011-02-10 19:29:03 +000049}
50
Johnny Chen7da805c2011-02-12 01:01:40 +000051// Rotate a 32-bit unsigned value right by the specified amount.
Johnny Chenf5588602011-02-11 23:29:14 +000052static inline uint32_t
53Rotr32 (uint32_t bits, uint32_t amt)
54{
55 assert(amt < 32 && "Invalid rotate amount");
56 return (bits >> amt) | (bits << ((32-amt)&31));
57}
58
Johnny Chen7da805c2011-02-12 01:01:40 +000059// Rotate a 32-bit unsigned value left by the specified amount.
Johnny Chenf5588602011-02-11 23:29:14 +000060static inline uint32_t
61Rotl32 (uint32_t bits, uint32_t amt)
62{
63 assert(amt < 32 && "Invalid rotate amount");
64 return (bits << amt) | (bits >> ((32-amt)&31));
65}
66
Johnny Chen108d5aa2011-01-26 01:00:55 +000067// Create a mask that starts at bit zero and includes "bit"
68static inline uint64_t
69MaskUpToBit (const uint64_t bit)
70{
71 return (1ull << (bit + 1ull)) - 1ull;
72}
73
Johnny Chen7da805c2011-02-12 01:01:40 +000074// Return an integer result equal to the number of bits of x that are ones.
Johnny Chen108d5aa2011-01-26 01:00:55 +000075static inline uint32_t
76BitCount (uint64_t x)
77{
78 // c accumulates the total bits set in x
79 uint32_t c;
80 for (c = 0; x; ++c)
81 {
82 x &= x - 1; // clear the least significant bit set
83 }
84 return c;
85}
86
87static inline bool
88BitIsSet (const uint64_t value, const uint64_t bit)
89{
90 return (value & (1ull << bit)) != 0;
91}
92
93static inline bool
94BitIsClear (const uint64_t value, const uint64_t bit)
95{
96 return (value & (1ull << bit)) == 0;
97}
98
99static inline uint64_t
100UnsignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
101{
102 uint64_t result = value >> lsbit;
103 result &= MaskUpToBit (msbit - lsbit);
104 return result;
105}
106
107static inline int64_t
108SignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
109{
110 uint64_t result = UnsignedBits (value, msbit, lsbit);
111 if (BitIsSet(value, msbit))
112 {
113 // Sign extend
114 result |= ~MaskUpToBit (msbit - lsbit);
115 }
116 return result;
117}
118
119} // namespace lldb_private
120
121#endif // lldb_InstructionUtils_h_