|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// Disassembler decoder helper functions. |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | +#ifndef LLVM_MC_MCDECODER_H |
| 11 | +#define LLVM_MC_MCDECODER_H |
| 12 | + |
| 13 | +#include "llvm/MC/MCDisassembler/MCDisassembler.h" |
| 14 | +#include "llvm/Support/MathExtras.h" |
| 15 | +#include <cassert> |
| 16 | + |
| 17 | +namespace llvm::MCD { |
| 18 | + |
| 19 | +// Helper to propagate SoftFail status. Returns false if the status is Fail; |
| 20 | +// callers are expected to early-exit in that condition. (Note, the '&' operator |
| 21 | +// is correct to propagate the values of this enum; see comment on 'enum |
| 22 | +// DecodeStatus'.) |
| 23 | +inline bool Check(MCDisassembler::DecodeStatus &Out, |
| 24 | + MCDisassembler::DecodeStatus In) { |
| 25 | + Out = static_cast<MCDisassembler::DecodeStatus>(Out & In); |
| 26 | + return Out != MCDisassembler::Fail; |
| 27 | +} |
| 28 | + |
| 29 | +// Extracts a given span of bits from the instruction bits and return it as an |
| 30 | +// integer. |
| 31 | +template <typename IntType> |
| 32 | +#if defined(_MSC_VER) && !defined(__clang__) |
| 33 | +__declspec(noinline) |
| 34 | +#endif |
| 35 | +inline std::enable_if_t<std::is_integral_v<IntType>, IntType> |
| 36 | +fieldFromInstruction(const IntType &Insn, unsigned StartBit, unsigned NumBits) { |
| 37 | + assert(StartBit + NumBits <= 64 && "Cannot support >64-bit extractions!"); |
| 38 | + assert(StartBit + NumBits <= (sizeof(IntType) * 8) && |
| 39 | + "Instruction field out of bounds!"); |
| 40 | + const IntType Mask = maskTrailingOnes<IntType>(NumBits); |
| 41 | + return (Insn >> StartBit) & Mask; |
| 42 | +} |
| 43 | + |
| 44 | +template <typename InsnType> |
| 45 | +inline std::enable_if_t<!std::is_integral_v<InsnType>, uint64_t> |
| 46 | +fieldFromInstruction(const InsnType &Insn, unsigned StartBit, |
| 47 | + unsigned NumBits) { |
| 48 | + return Insn.extractBitsAsZExtValue(NumBits, StartBit); |
| 49 | +} |
| 50 | + |
| 51 | +// Helper function for inserting bits extracted from an encoded instruction into |
| 52 | +// an integer-typed field. |
| 53 | +template <typename IntType> |
| 54 | +static std::enable_if_t<std::is_integral_v<IntType>, void> |
| 55 | +insertBits(IntType &field, IntType bits, unsigned startBit, unsigned numBits) { |
| 56 | + // Check that no bit beyond numBits is set, so that a simple bitwise | |
| 57 | + // is sufficient. |
| 58 | + assert((~(((IntType)1 << numBits) - 1) & bits) == 0 && |
| 59 | + "bits has more than numBits bits set"); |
| 60 | + assert(startBit + numBits <= sizeof(IntType) * 8); |
| 61 | + (void)numBits; |
| 62 | + field |= bits << startBit; |
| 63 | +} |
| 64 | + |
| 65 | +} // namespace llvm::MCD |
| 66 | + |
| 67 | +#endif // LLVM_MC_MCDECODER_H |
0 commit comments