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 <bitset>
16#include <cassert>
17
18namespace llvm::MCD {
19
20// Helper to propagate SoftFail status. Returns false if the status is Fail;
21// callers are expected to early-exit in that condition. (Note, the '&' operator
22// is correct to propagate the values of this enum; see comment on 'enum
23// DecodeStatus'.)
26 Out = static_cast<MCDisassembler::DecodeStatus>(Out & In);
27 return Out != MCDisassembler::Fail;
28}
29
30// Extracts a given span of bits from the instruction bits and return it as an
31// integer.
32template <typename IntType>
33#if defined(_MSC_VER) && !defined(__clang__)
34__declspec(noinline)
35#endif
36inline std::enable_if_t<std::is_integral_v<IntType>, IntType>
37fieldFromInstruction(const IntType &Insn, unsigned StartBit, unsigned NumBits) {
38 assert(StartBit + NumBits <= 64 && "Cannot support >64-bit extractions!");
39 assert(StartBit + NumBits <= (sizeof(IntType) * 8) &&
40 "Instruction field out of bounds!");
41 const IntType Mask = maskTrailingOnes<IntType>(NumBits);
42 return (Insn >> StartBit) & Mask;
43}
44
45template <typename InsnType>
46inline std::enable_if_t<!std::is_integral_v<InsnType>, uint64_t>
47fieldFromInstruction(const InsnType &Insn, unsigned StartBit,
48 unsigned NumBits) {
49 return Insn.extractBitsAsZExtValue(NumBits, StartBit);
50}
51
52template <size_t N>
53uint64_t fieldFromInstruction(const std::bitset<N> &Insn, unsigned StartBit,
54 unsigned NumBits) {
55 assert(StartBit + NumBits <= N && "Instruction field out of bounds!");
56 assert(NumBits <= 64 && "Cannot support >64-bit extractions!");
57 const std::bitset<N> Mask(maskTrailingOnes<uint64_t>(NumBits));
58 return ((Insn >> StartBit) & Mask).to_ullong();
59}
60
61// Helper function for inserting bits extracted from an encoded instruction into
62// an integer-typed field.
63template <typename IntType>
64static std::enable_if_t<std::is_integral_v<IntType>, void>
65insertBits(IntType &field, IntType bits, unsigned startBit, unsigned numBits) {
66 // Check that no bit beyond numBits is set, so that a simple bitwise |
67 // is sufficient.
68 assert((~(((IntType)1 << numBits) - 1) & bits) == 0 &&
69 "bits has more than numBits bits set");
70 assert(startBit + numBits <= sizeof(IntType) * 8);
71 (void)numBits;
72 field |= bits << startBit;
73}
74
75} // namespace llvm::MCD
76
77#endif // LLVM_MC_MCDECODER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
DecodeStatus
Ternary decode status.
bool Check(MCDisassembler::DecodeStatus &Out, MCDisassembler::DecodeStatus In)
Definition MCDecoder.h:24
std::enable_if_t< std::is_integral_v< IntType >, IntType > fieldFromInstruction(const IntType &Insn, unsigned StartBit, unsigned NumBits)
Definition MCDecoder.h:37