Skip to content

Commit 83c2ede

Browse files
lenaryjoaosaffran
authored andcommitted
[AsmParser][NFCI] Restructure DiagnosticPredicate (llvm#126653)
This restructures the DiagnosticPredicate type to more closely match how neatly ParseStatus is defined. The DiagnosticPredicateTy enum is moved inside the class, and the boolean conversions are made explicit. The main user of this code is AArch64, which I have also updated to use the new structure of the code.
1 parent 0cb3629 commit 83c2ede

File tree

2 files changed

+84
-83
lines changed

2 files changed

+84
-83
lines changed

llvm/include/llvm/MC/MCParser/MCTargetAsmParser.h

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,6 @@ class ParseStatus {
152152
constexpr bool isNoMatch() const { return Status == StatusTy::NoMatch; }
153153
};
154154

155-
enum class DiagnosticPredicateTy {
156-
Match,
157-
NearMatch,
158-
NoMatch,
159-
};
160-
161155
// When an operand is parsed, the assembler will try to iterate through a set of
162156
// possible operand classes that the operand might match and call the
163157
// corresponding PredicateMethod to determine that.
@@ -183,21 +177,24 @@ enum class DiagnosticPredicateTy {
183177
// This is a light-weight alternative to the 'NearMissInfo' approach
184178
// below which collects *all* possible diagnostics. This alternative
185179
// is optional and fully backward compatible with existing
186-
// PredicateMethods that return a 'bool' (match or no match).
180+
// PredicateMethods that return a 'bool' (match or near match).
187181
struct DiagnosticPredicate {
188-
DiagnosticPredicateTy Type;
189-
190-
explicit DiagnosticPredicate(bool Match)
191-
: Type(Match ? DiagnosticPredicateTy::Match
192-
: DiagnosticPredicateTy::NearMatch) {}
193-
DiagnosticPredicate(DiagnosticPredicateTy T) : Type(T) {}
194-
DiagnosticPredicate(const DiagnosticPredicate &) = default;
195-
DiagnosticPredicate& operator=(const DiagnosticPredicate &) = default;
196-
197-
operator bool() const { return Type == DiagnosticPredicateTy::Match; }
198-
bool isMatch() const { return Type == DiagnosticPredicateTy::Match; }
199-
bool isNearMatch() const { return Type == DiagnosticPredicateTy::NearMatch; }
200-
bool isNoMatch() const { return Type == DiagnosticPredicateTy::NoMatch; }
182+
enum PredicateTy {
183+
Match, // Matches
184+
NearMatch, // Close Match: use Specific Diagnostic
185+
NoMatch, // No Match: use `InvalidOperand`
186+
} Predicate;
187+
188+
constexpr DiagnosticPredicate(PredicateTy T) : Predicate(T) {}
189+
190+
explicit constexpr DiagnosticPredicate(bool Matches)
191+
: Predicate(Matches ? Match : NearMatch) {}
192+
193+
explicit operator bool() const { return Predicate == Match; }
194+
195+
constexpr bool isMatch() const { return Predicate == Match; }
196+
constexpr bool isNearMatch() const { return Predicate == NearMatch; }
197+
constexpr bool isNoMatch() const { return Predicate == NoMatch; }
201198
};
202199

203200
// When matching of an assembly instruction fails, there may be multiple

llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp

Lines changed: 67 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,9 @@ class AArch64Operand : public MCParsedAsmOperand {
814814
return (Val >= 0 && Val < 64);
815815
}
816816

817-
template <int Width> bool isSImm() const { return isSImmScaled<Width, 1>(); }
817+
template <int Width> bool isSImm() const {
818+
return bool(isSImmScaled<Width, 1>());
819+
}
818820

819821
template <int Bits, int Scale> DiagnosticPredicate isSImmScaled() const {
820822
return isImmScaled<Bits, Scale>(true);
@@ -824,7 +826,7 @@ class AArch64Operand : public MCParsedAsmOperand {
824826
DiagnosticPredicate isUImmScaled() const {
825827
if (IsRange && isImmRange() &&
826828
(getLastImmVal() != getFirstImmVal() + Offset))
827-
return DiagnosticPredicateTy::NoMatch;
829+
return DiagnosticPredicate::NoMatch;
828830

829831
return isImmScaled<Bits, Scale, IsRange>(false);
830832
}
@@ -833,15 +835,15 @@ class AArch64Operand : public MCParsedAsmOperand {
833835
DiagnosticPredicate isImmScaled(bool Signed) const {
834836
if ((!isImm() && !isImmRange()) || (isImm() && IsRange) ||
835837
(isImmRange() && !IsRange))
836-
return DiagnosticPredicateTy::NoMatch;
838+
return DiagnosticPredicate::NoMatch;
837839

838840
int64_t Val;
839841
if (isImmRange())
840842
Val = getFirstImmVal();
841843
else {
842844
const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
843845
if (!MCE)
844-
return DiagnosticPredicateTy::NoMatch;
846+
return DiagnosticPredicate::NoMatch;
845847
Val = MCE->getValue();
846848
}
847849

@@ -856,33 +858,33 @@ class AArch64Operand : public MCParsedAsmOperand {
856858
}
857859

858860
if (Val >= MinVal && Val <= MaxVal && (Val % Scale) == 0)
859-
return DiagnosticPredicateTy::Match;
861+
return DiagnosticPredicate::Match;
860862

861-
return DiagnosticPredicateTy::NearMatch;
863+
return DiagnosticPredicate::NearMatch;
862864
}
863865

864866
DiagnosticPredicate isSVEPattern() const {
865867
if (!isImm())
866-
return DiagnosticPredicateTy::NoMatch;
868+
return DiagnosticPredicate::NoMatch;
867869
auto *MCE = dyn_cast<MCConstantExpr>(getImm());
868870
if (!MCE)
869-
return DiagnosticPredicateTy::NoMatch;
871+
return DiagnosticPredicate::NoMatch;
870872
int64_t Val = MCE->getValue();
871873
if (Val >= 0 && Val < 32)
872-
return DiagnosticPredicateTy::Match;
873-
return DiagnosticPredicateTy::NearMatch;
874+
return DiagnosticPredicate::Match;
875+
return DiagnosticPredicate::NearMatch;
874876
}
875877

876878
DiagnosticPredicate isSVEVecLenSpecifier() const {
877879
if (!isImm())
878-
return DiagnosticPredicateTy::NoMatch;
880+
return DiagnosticPredicate::NoMatch;
879881
auto *MCE = dyn_cast<MCConstantExpr>(getImm());
880882
if (!MCE)
881-
return DiagnosticPredicateTy::NoMatch;
883+
return DiagnosticPredicate::NoMatch;
882884
int64_t Val = MCE->getValue();
883885
if (Val >= 0 && Val <= 1)
884-
return DiagnosticPredicateTy::Match;
885-
return DiagnosticPredicateTy::NearMatch;
886+
return DiagnosticPredicate::Match;
887+
return DiagnosticPredicate::NearMatch;
886888
}
887889

888890
bool isSymbolicUImm12Offset(const MCExpr *Expr) const {
@@ -1057,41 +1059,41 @@ class AArch64Operand : public MCParsedAsmOperand {
10571059
template <typename T>
10581060
DiagnosticPredicate isSVECpyImm() const {
10591061
if (!isShiftedImm() && (!isImm() || !isa<MCConstantExpr>(getImm())))
1060-
return DiagnosticPredicateTy::NoMatch;
1062+
return DiagnosticPredicate::NoMatch;
10611063

10621064
bool IsByte = std::is_same<int8_t, std::make_signed_t<T>>::value ||
10631065
std::is_same<int8_t, T>::value;
10641066
if (auto ShiftedImm = getShiftedVal<8>())
10651067
if (!(IsByte && ShiftedImm->second) &&
10661068
AArch64_AM::isSVECpyImm<T>(uint64_t(ShiftedImm->first)
10671069
<< ShiftedImm->second))
1068-
return DiagnosticPredicateTy::Match;
1070+
return DiagnosticPredicate::Match;
10691071

1070-
return DiagnosticPredicateTy::NearMatch;
1072+
return DiagnosticPredicate::NearMatch;
10711073
}
10721074

10731075
// Unsigned value in the range 0 to 255. For element widths of
10741076
// 16 bits or higher it may also be a signed multiple of 256 in the
10751077
// range 0 to 65280.
10761078
template <typename T> DiagnosticPredicate isSVEAddSubImm() const {
10771079
if (!isShiftedImm() && (!isImm() || !isa<MCConstantExpr>(getImm())))
1078-
return DiagnosticPredicateTy::NoMatch;
1080+
return DiagnosticPredicate::NoMatch;
10791081

10801082
bool IsByte = std::is_same<int8_t, std::make_signed_t<T>>::value ||
10811083
std::is_same<int8_t, T>::value;
10821084
if (auto ShiftedImm = getShiftedVal<8>())
10831085
if (!(IsByte && ShiftedImm->second) &&
10841086
AArch64_AM::isSVEAddSubImm<T>(ShiftedImm->first
10851087
<< ShiftedImm->second))
1086-
return DiagnosticPredicateTy::Match;
1088+
return DiagnosticPredicate::Match;
10871089

1088-
return DiagnosticPredicateTy::NearMatch;
1090+
return DiagnosticPredicate::NearMatch;
10891091
}
10901092

10911093
template <typename T> DiagnosticPredicate isSVEPreferredLogicalImm() const {
10921094
if (isLogicalImm<T>() && !isSVECpyImm<T>())
1093-
return DiagnosticPredicateTy::Match;
1094-
return DiagnosticPredicateTy::NoMatch;
1095+
return DiagnosticPredicate::Match;
1096+
return DiagnosticPredicate::NoMatch;
10951097
}
10961098

10971099
bool isCondCode() const { return Kind == k_CondCode; }
@@ -1319,48 +1321,48 @@ class AArch64Operand : public MCParsedAsmOperand {
13191321
template <int ElementWidth, unsigned Class>
13201322
DiagnosticPredicate isSVEPredicateVectorRegOfWidth() const {
13211323
if (Kind != k_Register || Reg.Kind != RegKind::SVEPredicateVector)
1322-
return DiagnosticPredicateTy::NoMatch;
1324+
return DiagnosticPredicate::NoMatch;
13231325

13241326
if (isSVEVectorReg<Class>() && (Reg.ElementWidth == ElementWidth))
1325-
return DiagnosticPredicateTy::Match;
1327+
return DiagnosticPredicate::Match;
13261328

1327-
return DiagnosticPredicateTy::NearMatch;
1329+
return DiagnosticPredicate::NearMatch;
13281330
}
13291331

13301332
template <int ElementWidth, unsigned Class>
13311333
DiagnosticPredicate isSVEPredicateOrPredicateAsCounterRegOfWidth() const {
13321334
if (Kind != k_Register || (Reg.Kind != RegKind::SVEPredicateAsCounter &&
13331335
Reg.Kind != RegKind::SVEPredicateVector))
1334-
return DiagnosticPredicateTy::NoMatch;
1336+
return DiagnosticPredicate::NoMatch;
13351337

13361338
if ((isSVEPredicateAsCounterReg<Class>() ||
13371339
isSVEPredicateVectorRegOfWidth<ElementWidth, Class>()) &&
13381340
Reg.ElementWidth == ElementWidth)
1339-
return DiagnosticPredicateTy::Match;
1341+
return DiagnosticPredicate::Match;
13401342

1341-
return DiagnosticPredicateTy::NearMatch;
1343+
return DiagnosticPredicate::NearMatch;
13421344
}
13431345

13441346
template <int ElementWidth, unsigned Class>
13451347
DiagnosticPredicate isSVEPredicateAsCounterRegOfWidth() const {
13461348
if (Kind != k_Register || Reg.Kind != RegKind::SVEPredicateAsCounter)
1347-
return DiagnosticPredicateTy::NoMatch;
1349+
return DiagnosticPredicate::NoMatch;
13481350

13491351
if (isSVEPredicateAsCounterReg<Class>() && (Reg.ElementWidth == ElementWidth))
1350-
return DiagnosticPredicateTy::Match;
1352+
return DiagnosticPredicate::Match;
13511353

1352-
return DiagnosticPredicateTy::NearMatch;
1354+
return DiagnosticPredicate::NearMatch;
13531355
}
13541356

13551357
template <int ElementWidth, unsigned Class>
13561358
DiagnosticPredicate isSVEDataVectorRegOfWidth() const {
13571359
if (Kind != k_Register || Reg.Kind != RegKind::SVEDataVector)
1358-
return DiagnosticPredicateTy::NoMatch;
1360+
return DiagnosticPredicate::NoMatch;
13591361

13601362
if (isSVEVectorReg<Class>() && Reg.ElementWidth == ElementWidth)
1361-
return DiagnosticPredicateTy::Match;
1363+
return DiagnosticPredicate::Match;
13621364

1363-
return DiagnosticPredicateTy::NearMatch;
1365+
return DiagnosticPredicate::NearMatch;
13641366
}
13651367

13661368
template <int ElementWidth, unsigned Class,
@@ -1369,7 +1371,7 @@ class AArch64Operand : public MCParsedAsmOperand {
13691371
DiagnosticPredicate isSVEDataVectorRegWithShiftExtend() const {
13701372
auto VectorMatch = isSVEDataVectorRegOfWidth<ElementWidth, Class>();
13711373
if (!VectorMatch.isMatch())
1372-
return DiagnosticPredicateTy::NoMatch;
1374+
return DiagnosticPredicate::NoMatch;
13731375

13741376
// Give a more specific diagnostic when the user has explicitly typed in
13751377
// a shift-amount that does not match what is expected, but for which
@@ -1378,12 +1380,12 @@ class AArch64Operand : public MCParsedAsmOperand {
13781380
if (!MatchShift && (ShiftExtendTy == AArch64_AM::UXTW ||
13791381
ShiftExtendTy == AArch64_AM::SXTW) &&
13801382
!ShiftWidthAlwaysSame && hasShiftExtendAmount() && ShiftWidth == 8)
1381-
return DiagnosticPredicateTy::NoMatch;
1383+
return DiagnosticPredicate::NoMatch;
13821384

13831385
if (MatchShift && ShiftExtendTy == getShiftExtendType())
1384-
return DiagnosticPredicateTy::Match;
1386+
return DiagnosticPredicate::Match;
13851387

1386-
return DiagnosticPredicateTy::NearMatch;
1388+
return DiagnosticPredicate::NearMatch;
13871389
}
13881390

13891391
bool isGPR32as64() const {
@@ -1420,15 +1422,17 @@ class AArch64Operand : public MCParsedAsmOperand {
14201422

14211423
template<int64_t Angle, int64_t Remainder>
14221424
DiagnosticPredicate isComplexRotation() const {
1423-
if (!isImm()) return DiagnosticPredicateTy::NoMatch;
1425+
if (!isImm())
1426+
return DiagnosticPredicate::NoMatch;
14241427

14251428
const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1426-
if (!CE) return DiagnosticPredicateTy::NoMatch;
1429+
if (!CE)
1430+
return DiagnosticPredicate::NoMatch;
14271431
uint64_t Value = CE->getValue();
14281432

14291433
if (Value % Angle == Remainder && Value <= 270)
1430-
return DiagnosticPredicateTy::Match;
1431-
return DiagnosticPredicateTy::NearMatch;
1434+
return DiagnosticPredicate::Match;
1435+
return DiagnosticPredicate::NearMatch;
14321436
}
14331437

14341438
template <unsigned RegClassID> bool isGPR64() const {
@@ -1439,12 +1443,12 @@ class AArch64Operand : public MCParsedAsmOperand {
14391443
template <unsigned RegClassID, int ExtWidth>
14401444
DiagnosticPredicate isGPR64WithShiftExtend() const {
14411445
if (Kind != k_Register || Reg.Kind != RegKind::Scalar)
1442-
return DiagnosticPredicateTy::NoMatch;
1446+
return DiagnosticPredicate::NoMatch;
14431447

14441448
if (isGPR64<RegClassID>() && getShiftExtendType() == AArch64_AM::LSL &&
14451449
getShiftExtendAmount() == Log2_32(ExtWidth / 8))
1446-
return DiagnosticPredicateTy::Match;
1447-
return DiagnosticPredicateTy::NearMatch;
1450+
return DiagnosticPredicate::Match;
1451+
return DiagnosticPredicate::NearMatch;
14481452
}
14491453

14501454
/// Is this a vector list with the type implicit (presumably attached to the
@@ -1479,10 +1483,10 @@ class AArch64Operand : public MCParsedAsmOperand {
14791483
bool Res =
14801484
isTypedVectorList<VectorKind, NumRegs, NumElements, ElementWidth>();
14811485
if (!Res)
1482-
return DiagnosticPredicateTy::NoMatch;
1486+
return DiagnosticPredicate::NoMatch;
14831487
if (!AArch64MCRegisterClasses[RegClass].contains(VectorList.RegNum))
1484-
return DiagnosticPredicateTy::NearMatch;
1485-
return DiagnosticPredicateTy::Match;
1488+
return DiagnosticPredicate::NearMatch;
1489+
return DiagnosticPredicate::Match;
14861490
}
14871491

14881492
template <RegKind VectorKind, unsigned NumRegs, unsigned Stride,
@@ -1491,21 +1495,21 @@ class AArch64Operand : public MCParsedAsmOperand {
14911495
bool Res = isTypedVectorList<VectorKind, NumRegs, /*NumElements*/ 0,
14921496
ElementWidth, Stride>();
14931497
if (!Res)
1494-
return DiagnosticPredicateTy::NoMatch;
1498+
return DiagnosticPredicate::NoMatch;
14951499
if ((VectorList.RegNum < (AArch64::Z0 + Stride)) ||
14961500
((VectorList.RegNum >= AArch64::Z16) &&
14971501
(VectorList.RegNum < (AArch64::Z16 + Stride))))
1498-
return DiagnosticPredicateTy::Match;
1499-
return DiagnosticPredicateTy::NoMatch;
1502+
return DiagnosticPredicate::Match;
1503+
return DiagnosticPredicate::NoMatch;
15001504
}
15011505

15021506
template <int Min, int Max>
15031507
DiagnosticPredicate isVectorIndex() const {
15041508
if (Kind != k_VectorIndex)
1505-
return DiagnosticPredicateTy::NoMatch;
1509+
return DiagnosticPredicate::NoMatch;
15061510
if (VectorIndex.Val >= Min && VectorIndex.Val <= Max)
1507-
return DiagnosticPredicateTy::Match;
1508-
return DiagnosticPredicateTy::NearMatch;
1511+
return DiagnosticPredicate::Match;
1512+
return DiagnosticPredicate::NearMatch;
15091513
}
15101514

15111515
bool isToken() const override { return Kind == k_Token; }
@@ -1531,7 +1535,7 @@ class AArch64Operand : public MCParsedAsmOperand {
15311535

15321536
template <unsigned ImmEnum> DiagnosticPredicate isExactFPImm() const {
15331537
if (Kind != k_FPImm)
1534-
return DiagnosticPredicateTy::NoMatch;
1538+
return DiagnosticPredicate::NoMatch;
15351539

15361540
if (getFPImmIsExact()) {
15371541
// Lookup the immediate from table of supported immediates.
@@ -1546,19 +1550,19 @@ class AArch64Operand : public MCParsedAsmOperand {
15461550
llvm_unreachable("FP immediate is not exact");
15471551

15481552
if (getFPImm().bitwiseIsEqual(RealVal))
1549-
return DiagnosticPredicateTy::Match;
1553+
return DiagnosticPredicate::Match;
15501554
}
15511555

1552-
return DiagnosticPredicateTy::NearMatch;
1556+
return DiagnosticPredicate::NearMatch;
15531557
}
15541558

15551559
template <unsigned ImmA, unsigned ImmB>
15561560
DiagnosticPredicate isExactFPImm() const {
1557-
DiagnosticPredicate Res = DiagnosticPredicateTy::NoMatch;
1561+
DiagnosticPredicate Res = DiagnosticPredicate::NoMatch;
15581562
if ((Res = isExactFPImm<ImmA>()))
1559-
return DiagnosticPredicateTy::Match;
1563+
return DiagnosticPredicate::Match;
15601564
if ((Res = isExactFPImm<ImmB>()))
1561-
return DiagnosticPredicateTy::Match;
1565+
return DiagnosticPredicate::Match;
15621566
return Res;
15631567
}
15641568

@@ -1741,12 +1745,12 @@ class AArch64Operand : public MCParsedAsmOperand {
17411745
template <MatrixKind Kind, unsigned EltSize, unsigned RegClass>
17421746
DiagnosticPredicate isMatrixRegOperand() const {
17431747
if (!isMatrix())
1744-
return DiagnosticPredicateTy::NoMatch;
1748+
return DiagnosticPredicate::NoMatch;
17451749
if (getMatrixKind() != Kind ||
17461750
!AArch64MCRegisterClasses[RegClass].contains(getMatrixReg()) ||
17471751
EltSize != getMatrixElementWidth())
1748-
return DiagnosticPredicateTy::NearMatch;
1749-
return DiagnosticPredicateTy::Match;
1752+
return DiagnosticPredicate::NearMatch;
1753+
return DiagnosticPredicate::Match;
17501754
}
17511755

17521756
bool isPAuthPCRelLabel16Operand() const {

0 commit comments

Comments
 (0)