Skip to content

Commit 22f8693

Browse files
authored
[NFC][MC][Decoder] Extract fixed pieces of decoder code into new header file (#154802)
Extract fixed functions generated by decoder emitter into a new MCDecoder.h header.
1 parent 628280b commit 22f8693

File tree

23 files changed

+112
-85
lines changed

23 files changed

+112
-85
lines changed

llvm/include/llvm/MC/MCDecoder.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

llvm/include/llvm/MC/MCDecoderOps.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
//===------------ llvm/MC/MCDecoderOps.h - Decoder driver -------*- C++ -*-===//
1+
//===----------------------------------------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
// Disassembler decoder state machine driver.
8+
// Disassembler decoder state machine ops.
99
//===----------------------------------------------------------------------===//
1010
#ifndef LLVM_MC_MCDECODEROPS_H
1111
#define LLVM_MC_MCDECODEROPS_H

llvm/lib/Target/AArch64/Disassembler/AArch64Disassembler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "MCTargetDesc/AArch64MCTargetDesc.h"
1616
#include "TargetInfo/AArch64TargetInfo.h"
1717
#include "Utils/AArch64BaseInfo.h"
18+
#include "llvm/MC/MCDecoder.h"
1819
#include "llvm/MC/MCDecoderOps.h"
1920
#include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
2021
#include "llvm/MC/MCInst.h"
@@ -27,6 +28,7 @@
2728
#include <memory>
2829

2930
using namespace llvm;
31+
using namespace llvm::MCD;
3032

3133
#define DEBUG_TYPE "aarch64-disassembler"
3234

llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/BinaryFormat/ELF.h"
2828
#include "llvm/MC/MCAsmInfo.h"
2929
#include "llvm/MC/MCContext.h"
30+
#include "llvm/MC/MCDecoder.h"
3031
#include "llvm/MC/MCDecoderOps.h"
3132
#include "llvm/MC/MCExpr.h"
3233
#include "llvm/MC/MCInstrDesc.h"
@@ -605,7 +606,7 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
605606
Bytes = Bytes_.slice(0, MaxInstBytesNum);
606607
}
607608

608-
if (isGFX11Plus() && Bytes.size() >= 12 ) {
609+
if (isGFX11Plus() && Bytes.size() >= 12) {
609610
DecoderUInt128 DecW = eat12Bytes(Bytes);
610611

611612
if (isGFX11() &&

llvm/lib/Target/ARC/Disassembler/ARCDisassembler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "MCTargetDesc/ARCMCTargetDesc.h"
1717
#include "TargetInfo/ARCTargetInfo.h"
1818
#include "llvm/MC/MCContext.h"
19+
#include "llvm/MC/MCDecoder.h"
1920
#include "llvm/MC/MCDecoderOps.h"
2021
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
2122
#include "llvm/MC/MCInst.h"
@@ -24,6 +25,7 @@
2425
#include "llvm/MC/TargetRegistry.h"
2526

2627
using namespace llvm;
28+
using namespace llvm::MCD;
2729

2830
#define DEBUG_TYPE "arc-disassembler"
2931

llvm/lib/Target/ARM/Disassembler/ARMDisassembler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "TargetInfo/ARMTargetInfo.h"
1414
#include "Utils/ARMBaseInfo.h"
1515
#include "llvm/MC/MCContext.h"
16+
#include "llvm/MC/MCDecoder.h"
1617
#include "llvm/MC/MCDecoderOps.h"
1718
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
1819
#include "llvm/MC/MCInst.h"
@@ -31,6 +32,7 @@
3132
#include <vector>
3233

3334
using namespace llvm;
35+
using namespace llvm::MCD;
3436

3537
#define DEBUG_TYPE "arm-disassembler"
3638

llvm/lib/Target/AVR/Disassembler/AVRDisassembler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "llvm/MC/MCAsmInfo.h"
2020
#include "llvm/MC/MCContext.h"
21+
#include "llvm/MC/MCDecoder.h"
2122
#include "llvm/MC/MCDecoderOps.h"
2223
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
2324
#include "llvm/MC/MCInst.h"
@@ -26,6 +27,7 @@
2627
#include "llvm/Support/Compiler.h"
2728

2829
using namespace llvm;
30+
using namespace llvm::MCD;
2931

3032
#define DEBUG_TYPE "avr-disassembler"
3133

llvm/lib/Target/BPF/Disassembler/BPFDisassembler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ADT/ArrayRef.h"
1616
#include "llvm/MC/MCAsmInfo.h"
1717
#include "llvm/MC/MCContext.h"
18+
#include "llvm/MC/MCDecoder.h"
1819
#include "llvm/MC/MCDecoderOps.h"
1920
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
2021
#include "llvm/MC/MCInst.h"

llvm/lib/Target/CSKY/Disassembler/CSKYDisassembler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "TargetInfo/CSKYTargetInfo.h"
1616
#include "llvm/ADT/DenseMap.h"
1717
#include "llvm/MC/MCContext.h"
18+
#include "llvm/MC/MCDecoder.h"
1819
#include "llvm/MC/MCDecoderOps.h"
1920
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
2021
#include "llvm/MC/MCInst.h"

llvm/lib/Target/Hexagon/Disassembler/HexagonDisassembler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "TargetInfo/HexagonTargetInfo.h"
1414
#include "llvm/ADT/ArrayRef.h"
1515
#include "llvm/MC/MCContext.h"
16+
#include "llvm/MC/MCDecoder.h"
1617
#include "llvm/MC/MCDecoderOps.h"
1718
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
1819
#include "llvm/MC/MCExpr.h"

0 commit comments

Comments
 (0)