LLVM 22.0.0git
MCDecoder.h
Go to the documentation of this file.
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
15#include <cassert>
16
17namespace 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'.)
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.
31template <typename IntType>
32#if defined(_MSC_VER) && !defined(__clang__)
33__declspec(noinline)
34#endif
35inline std::enable_if_t<std::is_integral_v<IntType>, IntType>
36fieldFromInstruction(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
44template <typename InsnType>
45inline std::enable_if_t<!std::is_integral_v<InsnType>, uint64_t>
46fieldFromInstruction(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.
53template <typename IntType>
54static std::enable_if_t<std::is_integral_v<IntType>, void>
55insertBits(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
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define Check(C,...)
DecodeStatus
Ternary decode status.
std::enable_if_t< std::is_integral_v< IntType >, IntType > fieldFromInstruction(const IntType &Insn, unsigned StartBit, unsigned NumBits)
Definition: MCDecoder.h:36