blob: 4bb644e6efe65d097646c52630a7af36866dc171 [file] [log] [blame]
Greg Clayton061b79d2011-05-09 20:18:18 +00001//===-- InstructionUtils.h --------------------------------------*- C++ -*-===//
Johnny Chen108d5aa2011-01-26 01:00:55 +00002//
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
Caroline Ticebf5a66b2011-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 Chenf5588602011-02-11 23:29:14 +000027// least significant bit (lsbit) of a 32-bit unsigned value.
Johnny Chen108d5aa2011-01-26 01:00:55 +000028static inline uint32_t
Johnny Chenc3a38ea2011-02-10 21:49:16 +000029Bits32 (const uint32_t bits, const uint32_t msbit, const uint32_t lsbit)
Johnny Chen108d5aa2011-01-26 01:00:55 +000030{
31 assert(msbit < 32 && lsbit <= msbit);
Johnny Chenc3a38ea2011-02-10 21:49:16 +000032 return (bits >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1);
Johnny Chen108d5aa2011-01-26 01:00:55 +000033}
34
Johnny Chen7da805c2011-02-12 01:01:40 +000035// Return the bit value from the 'bit' position of a 32-bit unsigned value.
Johnny Chen338bf542011-02-10 19:29:03 +000036static inline uint32_t
Johnny Chenc3a38ea2011-02-10 21:49:16 +000037Bit32 (const uint32_t bits, const uint32_t bit)
Johnny Chen338bf542011-02-10 19:29:03 +000038{
Greg Clayton061b79d2011-05-09 20:18:18 +000039 return (bits >> bit) & 1u;
40}
41
42static inline uint64_t
43Bit64 (const uint64_t bits, const uint32_t bit)
44{
45 return (bits >> bit) & 1ull;
Johnny Chen338bf542011-02-10 19:29:03 +000046}
47
Johnny Chen7da805c2011-02-12 01:01:40 +000048// Set the bit field(s) from the most significant bit (msbit) to the
49// least significant bit (lsbit) of a 32-bit unsigned value to 'val'.
Johnny Chen93070472011-02-04 23:02:47 +000050static inline void
Johnny Chenc3a38ea2011-02-10 21:49:16 +000051SetBits32(uint32_t &bits, const uint32_t msbit, const uint32_t lsbit, const uint32_t val)
Johnny Chen93070472011-02-04 23:02:47 +000052{
53 assert(msbit < 32 && lsbit < 32 && msbit >= lsbit);
Johnny Chenc3a38ea2011-02-10 21:49:16 +000054 uint32_t mask = ((1u << (msbit - lsbit + 1)) - 1);
Johnny Chen93070472011-02-04 23:02:47 +000055 bits &= ~(mask << lsbit);
56 bits |= (val & mask) << lsbit;
57}
58
Johnny Chen7da805c2011-02-12 01:01:40 +000059// Set the 'bit' position of a 32-bit unsigned value to 'val'.
Johnny Chen338bf542011-02-10 19:29:03 +000060static inline void
Johnny Chenc3a38ea2011-02-10 21:49:16 +000061SetBit32(uint32_t &bits, const uint32_t bit, const uint32_t val)
Johnny Chen338bf542011-02-10 19:29:03 +000062{
Johnny Chenbd599902011-02-10 21:39:01 +000063 SetBits32(bits, bit, bit, val);
Johnny Chen338bf542011-02-10 19:29:03 +000064}
65
Johnny Chen7da805c2011-02-12 01:01:40 +000066// Rotate a 32-bit unsigned value right by the specified amount.
Johnny Chenf5588602011-02-11 23:29:14 +000067static inline uint32_t
68Rotr32 (uint32_t bits, uint32_t amt)
69{
70 assert(amt < 32 && "Invalid rotate amount");
71 return (bits >> amt) | (bits << ((32-amt)&31));
72}
73
Johnny Chen7da805c2011-02-12 01:01:40 +000074// Rotate a 32-bit unsigned value left by the specified amount.
Johnny Chenf5588602011-02-11 23:29:14 +000075static inline uint32_t
76Rotl32 (uint32_t bits, uint32_t amt)
77{
78 assert(amt < 32 && "Invalid rotate amount");
79 return (bits << amt) | (bits >> ((32-amt)&31));
80}
81
Johnny Chen108d5aa2011-01-26 01:00:55 +000082// Create a mask that starts at bit zero and includes "bit"
83static inline uint64_t
84MaskUpToBit (const uint64_t bit)
85{
86 return (1ull << (bit + 1ull)) - 1ull;
87}
88
Johnny Chen7da805c2011-02-12 01:01:40 +000089// Return an integer result equal to the number of bits of x that are ones.
Johnny Chen108d5aa2011-01-26 01:00:55 +000090static inline uint32_t
91BitCount (uint64_t x)
92{
93 // c accumulates the total bits set in x
94 uint32_t c;
95 for (c = 0; x; ++c)
96 {
97 x &= x - 1; // clear the least significant bit set
98 }
99 return c;
100}
101
102static inline bool
103BitIsSet (const uint64_t value, const uint64_t bit)
104{
105 return (value & (1ull << bit)) != 0;
106}
107
108static inline bool
109BitIsClear (const uint64_t value, const uint64_t bit)
110{
111 return (value & (1ull << bit)) == 0;
112}
113
114static inline uint64_t
115UnsignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
116{
117 uint64_t result = value >> lsbit;
118 result &= MaskUpToBit (msbit - lsbit);
119 return result;
120}
121
122static inline int64_t
123SignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
124{
125 uint64_t result = UnsignedBits (value, msbit, lsbit);
126 if (BitIsSet(value, msbit))
127 {
128 // Sign extend
129 result |= ~MaskUpToBit (msbit - lsbit);
130 }
131 return result;
132}
133
134} // namespace lldb_private
135
136#endif // lldb_InstructionUtils_h_