blob: 4f59a52e3e9e6ba88785becba6f4be43b849f4e0 [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
17static inline uint32_t
Johnny Chenc3a38ea2011-02-10 21:49:16 +000018Bits32 (const uint32_t bits, const uint32_t msbit, const uint32_t lsbit)
Johnny Chen108d5aa2011-01-26 01:00:55 +000019{
20 assert(msbit < 32 && lsbit <= msbit);
Johnny Chenc3a38ea2011-02-10 21:49:16 +000021 return (bits >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1);
Johnny Chen108d5aa2011-01-26 01:00:55 +000022}
23
Johnny Chen338bf542011-02-10 19:29:03 +000024static inline uint32_t
Johnny Chenc3a38ea2011-02-10 21:49:16 +000025Bit32 (const uint32_t bits, const uint32_t bit)
Johnny Chen338bf542011-02-10 19:29:03 +000026{
Johnny Chenc3a38ea2011-02-10 21:49:16 +000027 return Bits32(bits, bit, bit);
Johnny Chen338bf542011-02-10 19:29:03 +000028}
29
Johnny Chen93070472011-02-04 23:02:47 +000030static inline void
Johnny Chenc3a38ea2011-02-10 21:49:16 +000031SetBits32(uint32_t &bits, const uint32_t msbit, const uint32_t lsbit, const uint32_t val)
Johnny Chen93070472011-02-04 23:02:47 +000032{
33 assert(msbit < 32 && lsbit < 32 && msbit >= lsbit);
Johnny Chenc3a38ea2011-02-10 21:49:16 +000034 uint32_t mask = ((1u << (msbit - lsbit + 1)) - 1);
Johnny Chen93070472011-02-04 23:02:47 +000035 bits &= ~(mask << lsbit);
36 bits |= (val & mask) << lsbit;
37}
38
Johnny Chen338bf542011-02-10 19:29:03 +000039static inline void
Johnny Chenc3a38ea2011-02-10 21:49:16 +000040SetBit32(uint32_t &bits, const uint32_t bit, const uint32_t val)
Johnny Chen338bf542011-02-10 19:29:03 +000041{
Johnny Chenbd599902011-02-10 21:39:01 +000042 SetBits32(bits, bit, bit, val);
Johnny Chen338bf542011-02-10 19:29:03 +000043}
44
Johnny Chen108d5aa2011-01-26 01:00:55 +000045// Create a mask that starts at bit zero and includes "bit"
46static inline uint64_t
47MaskUpToBit (const uint64_t bit)
48{
49 return (1ull << (bit + 1ull)) - 1ull;
50}
51
52// Returns an integer result equal to the number of bits of x that are ones.
53static inline uint32_t
54BitCount (uint64_t x)
55{
56 // c accumulates the total bits set in x
57 uint32_t c;
58 for (c = 0; x; ++c)
59 {
60 x &= x - 1; // clear the least significant bit set
61 }
62 return c;
63}
64
65static inline bool
66BitIsSet (const uint64_t value, const uint64_t bit)
67{
68 return (value & (1ull << bit)) != 0;
69}
70
71static inline bool
72BitIsClear (const uint64_t value, const uint64_t bit)
73{
74 return (value & (1ull << bit)) == 0;
75}
76
77static inline uint64_t
78UnsignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
79{
80 uint64_t result = value >> lsbit;
81 result &= MaskUpToBit (msbit - lsbit);
82 return result;
83}
84
85static inline int64_t
86SignedBits (const uint64_t value, const uint64_t msbit, const uint64_t lsbit)
87{
88 uint64_t result = UnsignedBits (value, msbit, lsbit);
89 if (BitIsSet(value, msbit))
90 {
91 // Sign extend
92 result |= ~MaskUpToBit (msbit - lsbit);
93 }
94 return result;
95}
96
97} // namespace lldb_private
98
99#endif // lldb_InstructionUtils_h_