Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Process/Utility/InstructionUtils.h
39642 views
//===-- InstructionUtils.h --------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_INSTRUCTIONUTILS_H9#define LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_INSTRUCTIONUTILS_H1011#include <cassert>12#include <cstdint>1314// Common utilities for manipulating instruction bit fields.1516namespace lldb_private {1718// Return the bit field(s) from the most significant bit (msbit) to the19// least significant bit (lsbit) of a 64-bit unsigned value.20static inline uint64_t Bits64(const uint64_t bits, const uint32_t msbit,21const uint32_t lsbit) {22assert(msbit < 64 && lsbit <= msbit);23return (bits >> lsbit) & ((1ull << (msbit - lsbit + 1)) - 1);24}2526// Return the bit field(s) from the most significant bit (msbit) to the27// least significant bit (lsbit) of a 32-bit unsigned value.28static inline uint32_t Bits32(const uint32_t bits, const uint32_t msbit,29const uint32_t lsbit) {30assert(msbit < 32 && lsbit <= msbit);31return (bits >> lsbit) & ((1u << (msbit - lsbit + 1)) - 1);32}3334// Return the bit value from the 'bit' position of a 32-bit unsigned value.35static inline uint32_t Bit32(const uint32_t bits, const uint32_t bit) {36return (bits >> bit) & 1u;37}3839static inline uint64_t Bit64(const uint64_t bits, const uint32_t bit) {40return (bits >> bit) & 1ull;41}4243// Set the bit field(s) from the most significant bit (msbit) to the44// least significant bit (lsbit) of a 32-bit unsigned value to 'val'.45static inline void SetBits32(uint32_t &bits, const uint32_t msbit,46const uint32_t lsbit, const uint32_t val) {47assert(msbit < 32 && lsbit < 32 && msbit >= lsbit);48uint32_t mask = ((1u << (msbit - lsbit + 1)) - 1);49bits &= ~(mask << lsbit);50bits |= (val & mask) << lsbit;51}5253// Set the 'bit' position of a 32-bit unsigned value to 'val'.54static inline void SetBit32(uint32_t &bits, const uint32_t bit,55const uint32_t val) {56SetBits32(bits, bit, bit, val);57}5859// Rotate a 32-bit unsigned value right by the specified amount.60static inline uint32_t Rotr32(uint32_t bits, uint32_t amt) {61assert(amt < 32 && "Invalid rotate amount");62return (bits >> amt) | (bits << ((32 - amt) & 31));63}6465// Rotate a 32-bit unsigned value left by the specified amount.66static inline uint32_t Rotl32(uint32_t bits, uint32_t amt) {67assert(amt < 32 && "Invalid rotate amount");68return (bits << amt) | (bits >> ((32 - amt) & 31));69}7071// Create a mask that starts at bit zero and includes "bit"72static inline uint64_t MaskUpToBit(const uint64_t bit) {73if (bit >= 63)74return -1ll;75return (1ull << (bit + 1ull)) - 1ull;76}7778// Return an integer result equal to the number of bits of x that are ones.79static inline uint32_t BitCount(uint64_t x) {80// c accumulates the total bits set in x81uint32_t c;82for (c = 0; x; ++c) {83x &= x - 1; // clear the least significant bit set84}85return c;86}8788static inline bool BitIsSet(const uint64_t value, const uint64_t bit) {89return (value & (1ull << bit)) != 0;90}9192static inline bool BitIsClear(const uint64_t value, const uint64_t bit) {93return (value & (1ull << bit)) == 0;94}9596static inline uint64_t UnsignedBits(const uint64_t value, const uint64_t msbit,97const uint64_t lsbit) {98uint64_t result = value >> lsbit;99result &= MaskUpToBit(msbit - lsbit);100return result;101}102103static inline int64_t SignedBits(const uint64_t value, const uint64_t msbit,104const uint64_t lsbit) {105uint64_t result = UnsignedBits(value, msbit, lsbit);106if (BitIsSet(value, msbit)) {107// Sign extend108result |= ~MaskUpToBit(msbit - lsbit);109}110return result;111}112113} // namespace lldb_private114115#endif // LLDB_SOURCE_PLUGINS_PROCESS_UTILITY_INSTRUCTIONUTILS_H116117118