Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions llvm/include/llvm/MC/MCDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef LLVM_MC_MCDECODER_H
#define LLVM_MC_MCDECODER_H

#include "llvm/ADT/APInt.h"
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
#include "llvm/Support/MathExtras.h"
#include <bitset>
Expand Down Expand Up @@ -58,6 +59,24 @@ uint64_t fieldFromInstruction(const std::bitset<N> &Insn, unsigned StartBit,
return ((Insn >> StartBit) & Mask).to_ullong();
}

template <unsigned StartBit, unsigned NumBits, typename T>
inline std::enable_if_t<std::is_unsigned_v<T>, T> extractBits(T Val) {
static_assert(StartBit + NumBits <= std::numeric_limits<T>::digits);
return (Val >> StartBit) & maskTrailingOnes<T>(NumBits);
}

template <unsigned StartBit, unsigned NumBits, size_t N>
uint64_t extractBits(const std::bitset<N> &Val) {
static_assert(StartBit + NumBits <= N);
std::bitset<N> Mask = maskTrailingOnes<uint64_t>(NumBits);
return ((Val >> StartBit) & Mask).to_ullong();
}

template <unsigned StartBit, unsigned NumBits>
uint64_t extractBits(const APInt &Val) {
return Val.extractBitsAsZExtValue(NumBits, StartBit);
}

} // namespace llvm::MCD

#endif // LLVM_MC_MCDECODER_H
7 changes: 3 additions & 4 deletions llvm/utils/TableGen/DecoderEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,8 +1031,7 @@ static void emitBinaryParser(raw_ostream &OS, indent Indent,
// One variable part and no/zero constant part. Initialize `tmp` with the
// variable part.
auto [Base, Width, Offset] = OpInfo.fields().front();
OS << Indent << "tmp = fieldFromInstruction(insn, " << Base << ", " << Width
<< ')';
OS << Indent << "tmp = extractBits<" << Base << ", " << Width << ">(insn)";
if (Offset)
OS << " << " << Offset;
OS << ";\n";
Expand All @@ -1042,8 +1041,8 @@ static void emitBinaryParser(raw_ostream &OS, indent Indent,
OS << Indent << "tmp = " << format_hex(OpInfo.InitValue.value_or(0), 0)
<< ";\n";
for (auto [Base, Width, Offset] : OpInfo.fields()) {
OS << Indent << "tmp |= fieldFromInstruction(insn, " << Base << ", "
<< Width << ')';
OS << Indent << "tmp |= extractBits<" << Base << ", " << Width
<< ">(insn)";
if (Offset)
OS << " << " << Offset;
OS << ";\n";
Expand Down
Loading