1 //===- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions -------===//
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 
9 #include "ARMBaseInstrInfo.h"
10 #include "ARMFeatures.h"
11 #include "MCTargetDesc/ARMAddressingModes.h"
12 #include "MCTargetDesc/ARMBaseInfo.h"
13 #include "MCTargetDesc/ARMInstPrinter.h"
14 #include "MCTargetDesc/ARMMCExpr.h"
15 #include "MCTargetDesc/ARMMCTargetDesc.h"
16 #include "TargetInfo/ARMTargetInfo.h"
17 #include "Utils/ARMBaseInfo.h"
18 #include "llvm/ADT/APFloat.h"
19 #include "llvm/ADT/APInt.h"
20 #include "llvm/ADT/None.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/StringSet.h"
27 #include "llvm/ADT/StringSwitch.h"
28 #include "llvm/ADT/Triple.h"
29 #include "llvm/ADT/Twine.h"
30 #include "llvm/MC/MCContext.h"
31 #include "llvm/MC/MCExpr.h"
32 #include "llvm/MC/MCInst.h"
33 #include "llvm/MC/MCInstrDesc.h"
34 #include "llvm/MC/MCInstrInfo.h"
35 #include "llvm/MC/MCParser/MCAsmLexer.h"
36 #include "llvm/MC/MCParser/MCAsmParser.h"
37 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
38 #include "llvm/MC/MCParser/MCAsmParserUtils.h"
39 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
40 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
41 #include "llvm/MC/MCRegisterInfo.h"
42 #include "llvm/MC/MCSection.h"
43 #include "llvm/MC/MCStreamer.h"
44 #include "llvm/MC/MCSubtargetInfo.h"
45 #include "llvm/MC/MCSymbol.h"
46 #include "llvm/MC/SubtargetFeature.h"
47 #include "llvm/MC/TargetRegistry.h"
48 #include "llvm/Support/ARMBuildAttributes.h"
49 #include "llvm/Support/ARMEHABI.h"
50 #include "llvm/Support/Casting.h"
51 #include "llvm/Support/CommandLine.h"
52 #include "llvm/Support/Compiler.h"
53 #include "llvm/Support/ErrorHandling.h"
54 #include "llvm/Support/MathExtras.h"
55 #include "llvm/Support/SMLoc.h"
56 #include "llvm/Support/TargetParser.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include <algorithm>
59 #include <cassert>
60 #include <cstddef>
61 #include <cstdint>
62 #include <iterator>
63 #include <limits>
64 #include <memory>
65 #include <string>
66 #include <utility>
67 #include <vector>
68 
69 #define DEBUG_TYPE "asm-parser"
70 
71 using namespace llvm;
72 
73 namespace llvm {
74 extern const MCInstrDesc ARMInsts[];
75 } // end namespace llvm
76 
77 namespace {
78 
79 enum class ImplicitItModeTy { Always, Never, ARMOnly, ThumbOnly };
80 
81 static cl::opt<ImplicitItModeTy> ImplicitItMode(
82     "arm-implicit-it", cl::init(ImplicitItModeTy::ARMOnly),
83     cl::desc("Allow conditional instructions outdside of an IT block"),
84     cl::values(clEnumValN(ImplicitItModeTy::Always, "always",
85                           "Accept in both ISAs, emit implicit ITs in Thumb"),
86                clEnumValN(ImplicitItModeTy::Never, "never",
87                           "Warn in ARM, reject in Thumb"),
88                clEnumValN(ImplicitItModeTy::ARMOnly, "arm",
89                           "Accept in ARM, reject in Thumb"),
90                clEnumValN(ImplicitItModeTy::ThumbOnly, "thumb",
91                           "Warn in ARM, emit implicit ITs in Thumb")));
92 
93 static cl::opt<bool> AddBuildAttributes("arm-add-build-attributes",
94                                         cl::init(false));
95 
96 enum VectorLaneTy { NoLanes, AllLanes, IndexedLane };
97 
98 static inline unsigned extractITMaskBit(unsigned Mask, unsigned Position) {
99   // Position==0 means we're not in an IT block at all. Position==1
100   // means we want the first state bit, which is always 0 (Then).
101   // Position==2 means we want the second state bit, stored at bit 3
102   // of Mask, and so on downwards. So (5 - Position) will shift the
103   // right bit down to bit 0, including the always-0 bit at bit 4 for
104   // the mandatory initial Then.
105   return (Mask >> (5 - Position) & 1);
106 }
107 
108 class UnwindContext {
109   using Locs = SmallVector<SMLoc, 4>;
110 
111   MCAsmParser &Parser;
112   Locs FnStartLocs;
113   Locs CantUnwindLocs;
114   Locs PersonalityLocs;
115   Locs PersonalityIndexLocs;
116   Locs HandlerDataLocs;
117   int FPReg;
118 
119 public:
120   UnwindContext(MCAsmParser &P) : Parser(P), FPReg(ARM::SP) {}
121 
122   bool hasFnStart() const { return !FnStartLocs.empty(); }
123   bool cantUnwind() const { return !CantUnwindLocs.empty(); }
124   bool hasHandlerData() const { return !HandlerDataLocs.empty(); }
125 
126   bool hasPersonality() const {
127     return !(PersonalityLocs.empty() && PersonalityIndexLocs.empty());
128   }
129 
130   void recordFnStart(SMLoc L) { FnStartLocs.push_back(L); }
131   void recordCantUnwind(SMLoc L) { CantUnwindLocs.push_back(L); }
132   void recordPersonality(SMLoc L) { PersonalityLocs.push_back(L); }
133   void recordHandlerData(SMLoc L) { HandlerDataLocs.push_back(L); }
134   void recordPersonalityIndex(SMLoc L) { PersonalityIndexLocs.push_back(L); }
135 
136   void saveFPReg(int Reg) { FPReg = Reg; }
137   int getFPReg() const { return FPReg; }
138 
139   void emitFnStartLocNotes() const {
140     for (Locs::const_iterator FI = FnStartLocs.begin(), FE = FnStartLocs.end();
141          FI != FE; ++FI)
142       Parser.Note(*FI, ".fnstart was specified here");
143   }
144 
145   void emitCantUnwindLocNotes() const {
146     for (Locs::const_iterator UI = CantUnwindLocs.begin(),
147                               UE = CantUnwindLocs.end(); UI != UE; ++UI)
148       Parser.Note(*UI, ".cantunwind was specified here");
149   }
150 
151   void emitHandlerDataLocNotes() const {
152     for (Locs::const_iterator HI = HandlerDataLocs.begin(),
153                               HE = HandlerDataLocs.end(); HI != HE; ++HI)
154       Parser.Note(*HI, ".handlerdata was specified here");
155   }
156 
157   void emitPersonalityLocNotes() const {
158     for (Locs::const_iterator PI = PersonalityLocs.begin(),
159                               PE = PersonalityLocs.end(),
160                               PII = PersonalityIndexLocs.begin(),
161                               PIE = PersonalityIndexLocs.end();
162          PI != PE || PII != PIE;) {
163       if (PI != PE && (PII == PIE || PI->getPointer() < PII->getPointer()))
164         Parser.Note(*PI++, ".personality was specified here");
165       else if (PII != PIE && (PI == PE || PII->getPointer() < PI->getPointer()))
166         Parser.Note(*PII++, ".personalityindex was specified here");
167       else
168         llvm_unreachable(".personality and .personalityindex cannot be "
169                          "at the same location");
170     }
171   }
172 
173   void reset() {
174     FnStartLocs = Locs();
175     CantUnwindLocs = Locs();
176     PersonalityLocs = Locs();
177     HandlerDataLocs = Locs();
178     PersonalityIndexLocs = Locs();
179     FPReg = ARM::SP;
180   }
181 };
182 
183 // Various sets of ARM instruction mnemonics which are used by the asm parser
184 class ARMMnemonicSets {
185   StringSet<> CDE;
186   StringSet<> CDEWithVPTSuffix;
187 public:
188   ARMMnemonicSets(const MCSubtargetInfo &STI);
189 
190   /// Returns true iff a given mnemonic is a CDE instruction
191   bool isCDEInstr(StringRef Mnemonic) {
192     // Quick check before searching the set
193     if (!Mnemonic.startswith("cx") && !Mnemonic.startswith("vcx"))
194       return false;
195     return CDE.count(Mnemonic);
196   }
197 
198   /// Returns true iff a given mnemonic is a VPT-predicable CDE instruction
199   /// (possibly with a predication suffix "e" or "t")
200   bool isVPTPredicableCDEInstr(StringRef Mnemonic) {
201     if (!Mnemonic.startswith("vcx"))
202       return false;
203     return CDEWithVPTSuffix.count(Mnemonic);
204   }
205 
206   /// Returns true iff a given mnemonic is an IT-predicable CDE instruction
207   /// (possibly with a condition suffix)
208   bool isITPredicableCDEInstr(StringRef Mnemonic) {
209     if (!Mnemonic.startswith("cx"))
210       return false;
211     return Mnemonic.startswith("cx1a") || Mnemonic.startswith("cx1da") ||
212            Mnemonic.startswith("cx2a") || Mnemonic.startswith("cx2da") ||
213            Mnemonic.startswith("cx3a") || Mnemonic.startswith("cx3da");
214   }
215 
216   /// Return true iff a given mnemonic is an integer CDE instruction with
217   /// dual-register destination
218   bool isCDEDualRegInstr(StringRef Mnemonic) {
219     if (!Mnemonic.startswith("cx"))
220       return false;
221     return Mnemonic == "cx1d" || Mnemonic == "cx1da" ||
222            Mnemonic == "cx2d" || Mnemonic == "cx2da" ||
223            Mnemonic == "cx3d" || Mnemonic == "cx3da";
224   }
225 };
226 
227 ARMMnemonicSets::ARMMnemonicSets(const MCSubtargetInfo &STI) {
228   for (StringRef Mnemonic: { "cx1", "cx1a", "cx1d", "cx1da",
229                              "cx2", "cx2a", "cx2d", "cx2da",
230                              "cx3", "cx3a", "cx3d", "cx3da", })
231     CDE.insert(Mnemonic);
232   for (StringRef Mnemonic :
233        {"vcx1", "vcx1a", "vcx2", "vcx2a", "vcx3", "vcx3a"}) {
234     CDE.insert(Mnemonic);
235     CDEWithVPTSuffix.insert(Mnemonic);
236     CDEWithVPTSuffix.insert(std::string(Mnemonic) + "t");
237     CDEWithVPTSuffix.insert(std::string(Mnemonic) + "e");
238   }
239 }
240 
241 class ARMAsmParser : public MCTargetAsmParser {
242   const MCRegisterInfo *MRI;
243   UnwindContext UC;
244   ARMMnemonicSets MS;
245 
246   ARMTargetStreamer &getTargetStreamer() {
247     assert(getParser().getStreamer().getTargetStreamer() &&
248            "do not have a target streamer");
249     MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
250     return static_cast<ARMTargetStreamer &>(TS);
251   }
252 
253   // Map of register aliases registers via the .req directive.
254   StringMap<unsigned> RegisterReqs;
255 
256   bool NextSymbolIsThumb;
257 
258   bool useImplicitITThumb() const {
259     return ImplicitItMode == ImplicitItModeTy::Always ||
260            ImplicitItMode == ImplicitItModeTy::ThumbOnly;
261   }
262 
263   bool useImplicitITARM() const {
264     return ImplicitItMode == ImplicitItModeTy::Always ||
265            ImplicitItMode == ImplicitItModeTy::ARMOnly;
266   }
267 
268   struct {
269     ARMCC::CondCodes Cond;    // Condition for IT block.
270     unsigned Mask:4;          // Condition mask for instructions.
271                               // Starting at first 1 (from lsb).
272                               //   '1'  condition as indicated in IT.
273                               //   '0'  inverse of condition (else).
274                               // Count of instructions in IT block is
275                               // 4 - trailingzeroes(mask)
276                               // Note that this does not have the same encoding
277                               // as in the IT instruction, which also depends
278                               // on the low bit of the condition code.
279 
280     unsigned CurPosition;     // Current position in parsing of IT
281                               // block. In range [0,4], with 0 being the IT
282                               // instruction itself. Initialized according to
283                               // count of instructions in block.  ~0U if no
284                               // active IT block.
285 
286     bool IsExplicit;          // true  - The IT instruction was present in the
287                               //         input, we should not modify it.
288                               // false - The IT instruction was added
289                               //         implicitly, we can extend it if that
290                               //         would be legal.
291   } ITState;
292 
293   SmallVector<MCInst, 4> PendingConditionalInsts;
294 
295   void flushPendingInstructions(MCStreamer &Out) override {
296     if (!inImplicitITBlock()) {
297       assert(PendingConditionalInsts.size() == 0);
298       return;
299     }
300 
301     // Emit the IT instruction
302     MCInst ITInst;
303     ITInst.setOpcode(ARM::t2IT);
304     ITInst.addOperand(MCOperand::createImm(ITState.Cond));
305     ITInst.addOperand(MCOperand::createImm(ITState.Mask));
306     Out.emitInstruction(ITInst, getSTI());
307 
308     // Emit the conditonal instructions
309     assert(PendingConditionalInsts.size() <= 4);
310     for (const MCInst &Inst : PendingConditionalInsts) {
311       Out.emitInstruction(Inst, getSTI());
312     }
313     PendingConditionalInsts.clear();
314 
315     // Clear the IT state
316     ITState.Mask = 0;
317     ITState.CurPosition = ~0U;
318   }
319 
320   bool inITBlock() { return ITState.CurPosition != ~0U; }
321   bool inExplicitITBlock() { return inITBlock() && ITState.IsExplicit; }
322   bool inImplicitITBlock() { return inITBlock() && !ITState.IsExplicit; }
323 
324   bool lastInITBlock() {
325     return ITState.CurPosition == 4 - countTrailingZeros(ITState.Mask);
326   }
327 
328   void forwardITPosition() {
329     if (!inITBlock()) return;
330     // Move to the next instruction in the IT block, if there is one. If not,
331     // mark the block as done, except for implicit IT blocks, which we leave
332     // open until we find an instruction that can't be added to it.
333     unsigned TZ = countTrailingZeros(ITState.Mask);
334     if (++ITState.CurPosition == 5 - TZ && ITState.IsExplicit)
335       ITState.CurPosition = ~0U; // Done with the IT block after this.
336   }
337 
338   // Rewind the state of the current IT block, removing the last slot from it.
339   void rewindImplicitITPosition() {
340     assert(inImplicitITBlock());
341     assert(ITState.CurPosition > 1);
342     ITState.CurPosition--;
343     unsigned TZ = countTrailingZeros(ITState.Mask);
344     unsigned NewMask = 0;
345     NewMask |= ITState.Mask & (0xC << TZ);
346     NewMask |= 0x2 << TZ;
347     ITState.Mask = NewMask;
348   }
349 
350   // Rewind the state of the current IT block, removing the last slot from it.
351   // If we were at the first slot, this closes the IT block.
352   void discardImplicitITBlock() {
353     assert(inImplicitITBlock());
354     assert(ITState.CurPosition == 1);
355     ITState.CurPosition = ~0U;
356   }
357 
358   // Return the low-subreg of a given Q register.
359   unsigned getDRegFromQReg(unsigned QReg) const {
360     return MRI->getSubReg(QReg, ARM::dsub_0);
361   }
362 
363   // Get the condition code corresponding to the current IT block slot.
364   ARMCC::CondCodes currentITCond() {
365     unsigned MaskBit = extractITMaskBit(ITState.Mask, ITState.CurPosition);
366     return MaskBit ? ARMCC::getOppositeCondition(ITState.Cond) : ITState.Cond;
367   }
368 
369   // Invert the condition of the current IT block slot without changing any
370   // other slots in the same block.
371   void invertCurrentITCondition() {
372     if (ITState.CurPosition == 1) {
373       ITState.Cond = ARMCC::getOppositeCondition(ITState.Cond);
374     } else {
375       ITState.Mask ^= 1 << (5 - ITState.CurPosition);
376     }
377   }
378 
379   // Returns true if the current IT block is full (all 4 slots used).
380   bool isITBlockFull() {
381     return inITBlock() && (ITState.Mask & 1);
382   }
383 
384   // Extend the current implicit IT block to have one more slot with the given
385   // condition code.
386   void extendImplicitITBlock(ARMCC::CondCodes Cond) {
387     assert(inImplicitITBlock());
388     assert(!isITBlockFull());
389     assert(Cond == ITState.Cond ||
390            Cond == ARMCC::getOppositeCondition(ITState.Cond));
391     unsigned TZ = countTrailingZeros(ITState.Mask);
392     unsigned NewMask = 0;
393     // Keep any existing condition bits.
394     NewMask |= ITState.Mask & (0xE << TZ);
395     // Insert the new condition bit.
396     NewMask |= (Cond != ITState.Cond) << TZ;
397     // Move the trailing 1 down one bit.
398     NewMask |= 1 << (TZ - 1);
399     ITState.Mask = NewMask;
400   }
401 
402   // Create a new implicit IT block with a dummy condition code.
403   void startImplicitITBlock() {
404     assert(!inITBlock());
405     ITState.Cond = ARMCC::AL;
406     ITState.Mask = 8;
407     ITState.CurPosition = 1;
408     ITState.IsExplicit = false;
409   }
410 
411   // Create a new explicit IT block with the given condition and mask.
412   // The mask should be in the format used in ARMOperand and
413   // MCOperand, with a 1 implying 'e', regardless of the low bit of
414   // the condition.
415   void startExplicitITBlock(ARMCC::CondCodes Cond, unsigned Mask) {
416     assert(!inITBlock());
417     ITState.Cond = Cond;
418     ITState.Mask = Mask;
419     ITState.CurPosition = 0;
420     ITState.IsExplicit = true;
421   }
422 
423   struct {
424     unsigned Mask : 4;
425     unsigned CurPosition;
426   } VPTState;
427   bool inVPTBlock() { return VPTState.CurPosition != ~0U; }
428   void forwardVPTPosition() {
429     if (!inVPTBlock()) return;
430     unsigned TZ = countTrailingZeros(VPTState.Mask);
431     if (++VPTState.CurPosition == 5 - TZ)
432       VPTState.CurPosition = ~0U;
433   }
434 
435   void Note(SMLoc L, const Twine &Msg, SMRange Range = None) {
436     return getParser().Note(L, Msg, Range);
437   }
438 
439   bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) {
440     return getParser().Warning(L, Msg, Range);
441   }
442 
443   bool Error(SMLoc L, const Twine &Msg, SMRange Range = None) {
444     return getParser().Error(L, Msg, Range);
445   }
446 
447   bool validatetLDMRegList(const MCInst &Inst, const OperandVector &Operands,
448                            unsigned ListNo, bool IsARPop = false);
449   bool validatetSTMRegList(const MCInst &Inst, const OperandVector &Operands,
450                            unsigned ListNo);
451 
452   int tryParseRegister();
453   bool tryParseRegisterWithWriteBack(OperandVector &);
454   int tryParseShiftRegister(OperandVector &);
455   bool parseRegisterList(OperandVector &, bool EnforceOrder = true,
456                          bool AllowRAAC = false);
457   bool parseMemory(OperandVector &);
458   bool parseOperand(OperandVector &, StringRef Mnemonic);
459   bool parsePrefix(ARMMCExpr::VariantKind &RefKind);
460   bool parseMemRegOffsetShift(ARM_AM::ShiftOpc &ShiftType,
461                               unsigned &ShiftAmount);
462   bool parseLiteralValues(unsigned Size, SMLoc L);
463   bool parseDirectiveThumb(SMLoc L);
464   bool parseDirectiveARM(SMLoc L);
465   bool parseDirectiveThumbFunc(SMLoc L);
466   bool parseDirectiveCode(SMLoc L);
467   bool parseDirectiveSyntax(SMLoc L);
468   bool parseDirectiveReq(StringRef Name, SMLoc L);
469   bool parseDirectiveUnreq(SMLoc L);
470   bool parseDirectiveArch(SMLoc L);
471   bool parseDirectiveEabiAttr(SMLoc L);
472   bool parseDirectiveCPU(SMLoc L);
473   bool parseDirectiveFPU(SMLoc L);
474   bool parseDirectiveFnStart(SMLoc L);
475   bool parseDirectiveFnEnd(SMLoc L);
476   bool parseDirectiveCantUnwind(SMLoc L);
477   bool parseDirectivePersonality(SMLoc L);
478   bool parseDirectiveHandlerData(SMLoc L);
479   bool parseDirectiveSetFP(SMLoc L);
480   bool parseDirectivePad(SMLoc L);
481   bool parseDirectiveRegSave(SMLoc L, bool IsVector);
482   bool parseDirectiveInst(SMLoc L, char Suffix = '\0');
483   bool parseDirectiveLtorg(SMLoc L);
484   bool parseDirectiveEven(SMLoc L);
485   bool parseDirectivePersonalityIndex(SMLoc L);
486   bool parseDirectiveUnwindRaw(SMLoc L);
487   bool parseDirectiveTLSDescSeq(SMLoc L);
488   bool parseDirectiveMovSP(SMLoc L);
489   bool parseDirectiveObjectArch(SMLoc L);
490   bool parseDirectiveArchExtension(SMLoc L);
491   bool parseDirectiveAlign(SMLoc L);
492   bool parseDirectiveThumbSet(SMLoc L);
493 
494   bool isMnemonicVPTPredicable(StringRef Mnemonic, StringRef ExtraToken);
495   StringRef splitMnemonic(StringRef Mnemonic, StringRef ExtraToken,
496                           unsigned &PredicationCode,
497                           unsigned &VPTPredicationCode, bool &CarrySetting,
498                           unsigned &ProcessorIMod, StringRef &ITMask);
499   void getMnemonicAcceptInfo(StringRef Mnemonic, StringRef ExtraToken,
500                              StringRef FullInst, bool &CanAcceptCarrySet,
501                              bool &CanAcceptPredicationCode,
502                              bool &CanAcceptVPTPredicationCode);
503   bool enableArchExtFeature(StringRef Name, SMLoc &ExtLoc);
504 
505   void tryConvertingToTwoOperandForm(StringRef Mnemonic, bool CarrySetting,
506                                      OperandVector &Operands);
507   bool CDEConvertDualRegOperand(StringRef Mnemonic, OperandVector &Operands);
508 
509   bool isThumb() const {
510     // FIXME: Can tablegen auto-generate this?
511     return getSTI().getFeatureBits()[ARM::ModeThumb];
512   }
513 
514   bool isThumbOne() const {
515     return isThumb() && !getSTI().getFeatureBits()[ARM::FeatureThumb2];
516   }
517 
518   bool isThumbTwo() const {
519     return isThumb() && getSTI().getFeatureBits()[ARM::FeatureThumb2];
520   }
521 
522   bool hasThumb() const {
523     return getSTI().getFeatureBits()[ARM::HasV4TOps];
524   }
525 
526   bool hasThumb2() const {
527     return getSTI().getFeatureBits()[ARM::FeatureThumb2];
528   }
529 
530   bool hasV6Ops() const {
531     return getSTI().getFeatureBits()[ARM::HasV6Ops];
532   }
533 
534   bool hasV6T2Ops() const {
535     return getSTI().getFeatureBits()[ARM::HasV6T2Ops];
536   }
537 
538   bool hasV6MOps() const {
539     return getSTI().getFeatureBits()[ARM::HasV6MOps];
540   }
541 
542   bool hasV7Ops() const {
543     return getSTI().getFeatureBits()[ARM::HasV7Ops];
544   }
545 
546   bool hasV8Ops() const {
547     return getSTI().getFeatureBits()[ARM::HasV8Ops];
548   }
549 
550   bool hasV8MBaseline() const {
551     return getSTI().getFeatureBits()[ARM::HasV8MBaselineOps];
552   }
553 
554   bool hasV8MMainline() const {
555     return getSTI().getFeatureBits()[ARM::HasV8MMainlineOps];
556   }
557   bool hasV8_1MMainline() const {
558     return getSTI().getFeatureBits()[ARM::HasV8_1MMainlineOps];
559   }
560   bool hasMVE() const {
561     return getSTI().getFeatureBits()[ARM::HasMVEIntegerOps];
562   }
563   bool hasMVEFloat() const {
564     return getSTI().getFeatureBits()[ARM::HasMVEFloatOps];
565   }
566   bool hasCDE() const {
567     return getSTI().getFeatureBits()[ARM::HasCDEOps];
568   }
569   bool has8MSecExt() const {
570     return getSTI().getFeatureBits()[ARM::Feature8MSecExt];
571   }
572 
573   bool hasARM() const {
574     return !getSTI().getFeatureBits()[ARM::FeatureNoARM];
575   }
576 
577   bool hasDSP() const {
578     return getSTI().getFeatureBits()[ARM::FeatureDSP];
579   }
580 
581   bool hasD32() const {
582     return getSTI().getFeatureBits()[ARM::FeatureD32];
583   }
584 
585   bool hasV8_1aOps() const {
586     return getSTI().getFeatureBits()[ARM::HasV8_1aOps];
587   }
588 
589   bool hasRAS() const {
590     return getSTI().getFeatureBits()[ARM::FeatureRAS];
591   }
592 
593   void SwitchMode() {
594     MCSubtargetInfo &STI = copySTI();
595     auto FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
596     setAvailableFeatures(FB);
597   }
598 
599   void FixModeAfterArchChange(bool WasThumb, SMLoc Loc);
600 
601   bool isMClass() const {
602     return getSTI().getFeatureBits()[ARM::FeatureMClass];
603   }
604 
605   /// @name Auto-generated Match Functions
606   /// {
607 
608 #define GET_ASSEMBLER_HEADER
609 #include "ARMGenAsmMatcher.inc"
610 
611   /// }
612 
613   OperandMatchResultTy parseITCondCode(OperandVector &);
614   OperandMatchResultTy parseCoprocNumOperand(OperandVector &);
615   OperandMatchResultTy parseCoprocRegOperand(OperandVector &);
616   OperandMatchResultTy parseCoprocOptionOperand(OperandVector &);
617   OperandMatchResultTy parseMemBarrierOptOperand(OperandVector &);
618   OperandMatchResultTy parseTraceSyncBarrierOptOperand(OperandVector &);
619   OperandMatchResultTy parseInstSyncBarrierOptOperand(OperandVector &);
620   OperandMatchResultTy parseProcIFlagsOperand(OperandVector &);
621   OperandMatchResultTy parseMSRMaskOperand(OperandVector &);
622   OperandMatchResultTy parseBankedRegOperand(OperandVector &);
623   OperandMatchResultTy parsePKHImm(OperandVector &O, StringRef Op, int Low,
624                                    int High);
625   OperandMatchResultTy parsePKHLSLImm(OperandVector &O) {
626     return parsePKHImm(O, "lsl", 0, 31);
627   }
628   OperandMatchResultTy parsePKHASRImm(OperandVector &O) {
629     return parsePKHImm(O, "asr", 1, 32);
630   }
631   OperandMatchResultTy parseSetEndImm(OperandVector &);
632   OperandMatchResultTy parseShifterImm(OperandVector &);
633   OperandMatchResultTy parseRotImm(OperandVector &);
634   OperandMatchResultTy parseModImm(OperandVector &);
635   OperandMatchResultTy parseBitfield(OperandVector &);
636   OperandMatchResultTy parsePostIdxReg(OperandVector &);
637   OperandMatchResultTy parseAM3Offset(OperandVector &);
638   OperandMatchResultTy parseFPImm(OperandVector &);
639   OperandMatchResultTy parseVectorList(OperandVector &);
640   OperandMatchResultTy parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index,
641                                        SMLoc &EndLoc);
642 
643   // Asm Match Converter Methods
644   void cvtThumbMultiply(MCInst &Inst, const OperandVector &);
645   void cvtThumbBranches(MCInst &Inst, const OperandVector &);
646   void cvtMVEVMOVQtoDReg(MCInst &Inst, const OperandVector &);
647 
648   bool validateInstruction(MCInst &Inst, const OperandVector &Ops);
649   bool processInstruction(MCInst &Inst, const OperandVector &Ops, MCStreamer &Out);
650   bool shouldOmitCCOutOperand(StringRef Mnemonic, OperandVector &Operands);
651   bool shouldOmitPredicateOperand(StringRef Mnemonic, OperandVector &Operands);
652   bool shouldOmitVectorPredicateOperand(StringRef Mnemonic, OperandVector &Operands);
653   bool isITBlockTerminator(MCInst &Inst) const;
654   void fixupGNULDRDAlias(StringRef Mnemonic, OperandVector &Operands);
655   bool validateLDRDSTRD(MCInst &Inst, const OperandVector &Operands,
656                         bool Load, bool ARMMode, bool Writeback);
657 
658 public:
659   enum ARMMatchResultTy {
660     Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
661     Match_RequiresNotITBlock,
662     Match_RequiresV6,
663     Match_RequiresThumb2,
664     Match_RequiresV8,
665     Match_RequiresFlagSetting,
666 #define GET_OPERAND_DIAGNOSTIC_TYPES
667 #include "ARMGenAsmMatcher.inc"
668 
669   };
670 
671   ARMAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
672                const MCInstrInfo &MII, const MCTargetOptions &Options)
673     : MCTargetAsmParser(Options, STI, MII), UC(Parser), MS(STI) {
674     MCAsmParserExtension::Initialize(Parser);
675 
676     // Cache the MCRegisterInfo.
677     MRI = getContext().getRegisterInfo();
678 
679     // Initialize the set of available features.
680     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
681 
682     // Add build attributes based on the selected target.
683     if (AddBuildAttributes)
684       getTargetStreamer().emitTargetAttributes(STI);
685 
686     // Not in an ITBlock to start with.
687     ITState.CurPosition = ~0U;
688 
689     VPTState.CurPosition = ~0U;
690 
691     NextSymbolIsThumb = false;
692   }
693 
694   // Implementation of the MCTargetAsmParser interface:
695   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
696   OperandMatchResultTy tryParseRegister(unsigned &RegNo, SMLoc &StartLoc,
697                                         SMLoc &EndLoc) override;
698   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
699                         SMLoc NameLoc, OperandVector &Operands) override;
700   bool ParseDirective(AsmToken DirectiveID) override;
701 
702   unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
703                                       unsigned Kind) override;
704   unsigned checkTargetMatchPredicate(MCInst &Inst) override;
705 
706   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
707                                OperandVector &Operands, MCStreamer &Out,
708                                uint64_t &ErrorInfo,
709                                bool MatchingInlineAsm) override;
710   unsigned MatchInstruction(OperandVector &Operands, MCInst &Inst,
711                             SmallVectorImpl<NearMissInfo> &NearMisses,
712                             bool MatchingInlineAsm, bool &EmitInITBlock,
713                             MCStreamer &Out);
714 
715   struct NearMissMessage {
716     SMLoc Loc;
717     SmallString<128> Message;
718   };
719 
720   const char *getCustomOperandDiag(ARMMatchResultTy MatchError);
721 
722   void FilterNearMisses(SmallVectorImpl<NearMissInfo> &NearMissesIn,
723                         SmallVectorImpl<NearMissMessage> &NearMissesOut,
724                         SMLoc IDLoc, OperandVector &Operands);
725   void ReportNearMisses(SmallVectorImpl<NearMissInfo> &NearMisses, SMLoc IDLoc,
726                         OperandVector &Operands);
727 
728   void doBeforeLabelEmit(MCSymbol *Symbol) override;
729 
730   void onLabelParsed(MCSymbol *Symbol) override;
731 };
732 
733 /// ARMOperand - Instances of this class represent a parsed ARM machine
734 /// operand.
735 class ARMOperand : public MCParsedAsmOperand {
736   enum KindTy {
737     k_CondCode,
738     k_VPTPred,
739     k_CCOut,
740     k_ITCondMask,
741     k_CoprocNum,
742     k_CoprocReg,
743     k_CoprocOption,
744     k_Immediate,
745     k_MemBarrierOpt,
746     k_InstSyncBarrierOpt,
747     k_TraceSyncBarrierOpt,
748     k_Memory,
749     k_PostIndexRegister,
750     k_MSRMask,
751     k_BankedReg,
752     k_ProcIFlags,
753     k_VectorIndex,
754     k_Register,
755     k_RegisterList,
756     k_RegisterListWithAPSR,
757     k_DPRRegisterList,
758     k_SPRRegisterList,
759     k_FPSRegisterListWithVPR,
760     k_FPDRegisterListWithVPR,
761     k_VectorList,
762     k_VectorListAllLanes,
763     k_VectorListIndexed,
764     k_ShiftedRegister,
765     k_ShiftedImmediate,
766     k_ShifterImmediate,
767     k_RotateImmediate,
768     k_ModifiedImmediate,
769     k_ConstantPoolImmediate,
770     k_BitfieldDescriptor,
771     k_Token,
772   } Kind;
773 
774   SMLoc StartLoc, EndLoc, AlignmentLoc;
775   SmallVector<unsigned, 8> Registers;
776 
777   struct CCOp {
778     ARMCC::CondCodes Val;
779   };
780 
781   struct VCCOp {
782     ARMVCC::VPTCodes Val;
783   };
784 
785   struct CopOp {
786     unsigned Val;
787   };
788 
789   struct CoprocOptionOp {
790     unsigned Val;
791   };
792 
793   struct ITMaskOp {
794     unsigned Mask:4;
795   };
796 
797   struct MBOptOp {
798     ARM_MB::MemBOpt Val;
799   };
800 
801   struct ISBOptOp {
802     ARM_ISB::InstSyncBOpt Val;
803   };
804 
805   struct TSBOptOp {
806     ARM_TSB::TraceSyncBOpt Val;
807   };
808 
809   struct IFlagsOp {
810     ARM_PROC::IFlags Val;
811   };
812 
813   struct MMaskOp {
814     unsigned Val;
815   };
816 
817   struct BankedRegOp {
818     unsigned Val;
819   };
820 
821   struct TokOp {
822     const char *Data;
823     unsigned Length;
824   };
825 
826   struct RegOp {
827     unsigned RegNum;
828   };
829 
830   // A vector register list is a sequential list of 1 to 4 registers.
831   struct VectorListOp {
832     unsigned RegNum;
833     unsigned Count;
834     unsigned LaneIndex;
835     bool isDoubleSpaced;
836   };
837 
838   struct VectorIndexOp {
839     unsigned Val;
840   };
841 
842   struct ImmOp {
843     const MCExpr *Val;
844   };
845 
846   /// Combined record for all forms of ARM address expressions.
847   struct MemoryOp {
848     unsigned BaseRegNum;
849     // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
850     // was specified.
851     const MCExpr *OffsetImm;  // Offset immediate value
852     unsigned OffsetRegNum;    // Offset register num, when OffsetImm == NULL
853     ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
854     unsigned ShiftImm;        // shift for OffsetReg.
855     unsigned Alignment;       // 0 = no alignment specified
856     // n = alignment in bytes (2, 4, 8, 16, or 32)
857     unsigned isNegative : 1;  // Negated OffsetReg? (~'U' bit)
858   };
859 
860   struct PostIdxRegOp {
861     unsigned RegNum;
862     bool isAdd;
863     ARM_AM::ShiftOpc ShiftTy;
864     unsigned ShiftImm;
865   };
866 
867   struct ShifterImmOp {
868     bool isASR;
869     unsigned Imm;
870   };
871 
872   struct RegShiftedRegOp {
873     ARM_AM::ShiftOpc ShiftTy;
874     unsigned SrcReg;
875     unsigned ShiftReg;
876     unsigned ShiftImm;
877   };
878 
879   struct RegShiftedImmOp {
880     ARM_AM::ShiftOpc ShiftTy;
881     unsigned SrcReg;
882     unsigned ShiftImm;
883   };
884 
885   struct RotImmOp {
886     unsigned Imm;
887   };
888 
889   struct ModImmOp {
890     unsigned Bits;
891     unsigned Rot;
892   };
893 
894   struct BitfieldOp {
895     unsigned LSB;
896     unsigned Width;
897   };
898 
899   union {
900     struct CCOp CC;
901     struct VCCOp VCC;
902     struct CopOp Cop;
903     struct CoprocOptionOp CoprocOption;
904     struct MBOptOp MBOpt;
905     struct ISBOptOp ISBOpt;
906     struct TSBOptOp TSBOpt;
907     struct ITMaskOp ITMask;
908     struct IFlagsOp IFlags;
909     struct MMaskOp MMask;
910     struct BankedRegOp BankedReg;
911     struct TokOp Tok;
912     struct RegOp Reg;
913     struct VectorListOp VectorList;
914     struct VectorIndexOp VectorIndex;
915     struct ImmOp Imm;
916     struct MemoryOp Memory;
917     struct PostIdxRegOp PostIdxReg;
918     struct ShifterImmOp ShifterImm;
919     struct RegShiftedRegOp RegShiftedReg;
920     struct RegShiftedImmOp RegShiftedImm;
921     struct RotImmOp RotImm;
922     struct ModImmOp ModImm;
923     struct BitfieldOp Bitfield;
924   };
925 
926 public:
927   ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
928 
929   /// getStartLoc - Get the location of the first token of this operand.
930   SMLoc getStartLoc() const override { return StartLoc; }
931 
932   /// getEndLoc - Get the location of the last token of this operand.
933   SMLoc getEndLoc() const override { return EndLoc; }
934 
935   /// getLocRange - Get the range between the first and last token of this
936   /// operand.
937   SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
938 
939   /// getAlignmentLoc - Get the location of the Alignment token of this operand.
940   SMLoc getAlignmentLoc() const {
941     assert(Kind == k_Memory && "Invalid access!");
942     return AlignmentLoc;
943   }
944 
945   ARMCC::CondCodes getCondCode() const {
946     assert(Kind == k_CondCode && "Invalid access!");
947     return CC.Val;
948   }
949 
950   ARMVCC::VPTCodes getVPTPred() const {
951     assert(isVPTPred() && "Invalid access!");
952     return VCC.Val;
953   }
954 
955   unsigned getCoproc() const {
956     assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
957     return Cop.Val;
958   }
959 
960   StringRef getToken() const {
961     assert(Kind == k_Token && "Invalid access!");
962     return StringRef(Tok.Data, Tok.Length);
963   }
964 
965   unsigned getReg() const override {
966     assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
967     return Reg.RegNum;
968   }
969 
970   const SmallVectorImpl<unsigned> &getRegList() const {
971     assert((Kind == k_RegisterList || Kind == k_RegisterListWithAPSR ||
972             Kind == k_DPRRegisterList || Kind == k_SPRRegisterList ||
973             Kind == k_FPSRegisterListWithVPR ||
974             Kind == k_FPDRegisterListWithVPR) &&
975            "Invalid access!");
976     return Registers;
977   }
978 
979   const MCExpr *getImm() const {
980     assert(isImm() && "Invalid access!");
981     return Imm.Val;
982   }
983 
984   const MCExpr *getConstantPoolImm() const {
985     assert(isConstantPoolImm() && "Invalid access!");
986     return Imm.Val;
987   }
988 
989   unsigned getVectorIndex() const {
990     assert(Kind == k_VectorIndex && "Invalid access!");
991     return VectorIndex.Val;
992   }
993 
994   ARM_MB::MemBOpt getMemBarrierOpt() const {
995     assert(Kind == k_MemBarrierOpt && "Invalid access!");
996     return MBOpt.Val;
997   }
998 
999   ARM_ISB::InstSyncBOpt getInstSyncBarrierOpt() const {
1000     assert(Kind == k_InstSyncBarrierOpt && "Invalid access!");
1001     return ISBOpt.Val;
1002   }
1003 
1004   ARM_TSB::TraceSyncBOpt getTraceSyncBarrierOpt() const {
1005     assert(Kind == k_TraceSyncBarrierOpt && "Invalid access!");
1006     return TSBOpt.Val;
1007   }
1008 
1009   ARM_PROC::IFlags getProcIFlags() const {
1010     assert(Kind == k_ProcIFlags && "Invalid access!");
1011     return IFlags.Val;
1012   }
1013 
1014   unsigned getMSRMask() const {
1015     assert(Kind == k_MSRMask && "Invalid access!");
1016     return MMask.Val;
1017   }
1018 
1019   unsigned getBankedReg() const {
1020     assert(Kind == k_BankedReg && "Invalid access!");
1021     return BankedReg.Val;
1022   }
1023 
1024   bool isCoprocNum() const { return Kind == k_CoprocNum; }
1025   bool isCoprocReg() const { return Kind == k_CoprocReg; }
1026   bool isCoprocOption() const { return Kind == k_CoprocOption; }
1027   bool isCondCode() const { return Kind == k_CondCode; }
1028   bool isVPTPred() const { return Kind == k_VPTPred; }
1029   bool isCCOut() const { return Kind == k_CCOut; }
1030   bool isITMask() const { return Kind == k_ITCondMask; }
1031   bool isITCondCode() const { return Kind == k_CondCode; }
1032   bool isImm() const override {
1033     return Kind == k_Immediate;
1034   }
1035 
1036   bool isARMBranchTarget() const {
1037     if (!isImm()) return false;
1038 
1039     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()))
1040       return CE->getValue() % 4 == 0;
1041     return true;
1042   }
1043 
1044 
1045   bool isThumbBranchTarget() const {
1046     if (!isImm()) return false;
1047 
1048     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()))
1049       return CE->getValue() % 2 == 0;
1050     return true;
1051   }
1052 
1053   // checks whether this operand is an unsigned offset which fits is a field
1054   // of specified width and scaled by a specific number of bits
1055   template<unsigned width, unsigned scale>
1056   bool isUnsignedOffset() const {
1057     if (!isImm()) return false;
1058     if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
1059     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
1060       int64_t Val = CE->getValue();
1061       int64_t Align = 1LL << scale;
1062       int64_t Max = Align * ((1LL << width) - 1);
1063       return ((Val % Align) == 0) && (Val >= 0) && (Val <= Max);
1064     }
1065     return false;
1066   }
1067 
1068   // checks whether this operand is an signed offset which fits is a field
1069   // of specified width and scaled by a specific number of bits
1070   template<unsigned width, unsigned scale>
1071   bool isSignedOffset() const {
1072     if (!isImm()) return false;
1073     if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
1074     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
1075       int64_t Val = CE->getValue();
1076       int64_t Align = 1LL << scale;
1077       int64_t Max = Align * ((1LL << (width-1)) - 1);
1078       int64_t Min = -Align * (1LL << (width-1));
1079       return ((Val % Align) == 0) && (Val >= Min) && (Val <= Max);
1080     }
1081     return false;
1082   }
1083 
1084   // checks whether this operand is an offset suitable for the LE /
1085   // LETP instructions in Arm v8.1M
1086   bool isLEOffset() const {
1087     if (!isImm()) return false;
1088     if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
1089     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
1090       int64_t Val = CE->getValue();
1091       return Val < 0 && Val >= -4094 && (Val & 1) == 0;
1092     }
1093     return false;
1094   }
1095 
1096   // checks whether this operand is a memory operand computed as an offset
1097   // applied to PC. the offset may have 8 bits of magnitude and is represented
1098   // with two bits of shift. textually it may be either [pc, #imm], #imm or
1099   // relocable expression...
1100   bool isThumbMemPC() const {
1101     int64_t Val = 0;
1102     if (isImm()) {
1103       if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
1104       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val);
1105       if (!CE) return false;
1106       Val = CE->getValue();
1107     }
1108     else if (isGPRMem()) {
1109       if(!Memory.OffsetImm || Memory.OffsetRegNum) return false;
1110       if(Memory.BaseRegNum != ARM::PC) return false;
1111       if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm))
1112         Val = CE->getValue();
1113       else
1114         return false;
1115     }
1116     else return false;
1117     return ((Val % 4) == 0) && (Val >= 0) && (Val <= 1020);
1118   }
1119 
1120   bool isFPImm() const {
1121     if (!isImm()) return false;
1122     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1123     if (!CE) return false;
1124     int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
1125     return Val != -1;
1126   }
1127 
1128   template<int64_t N, int64_t M>
1129   bool isImmediate() const {
1130     if (!isImm()) return false;
1131     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1132     if (!CE) return false;
1133     int64_t Value = CE->getValue();
1134     return Value >= N && Value <= M;
1135   }
1136 
1137   template<int64_t N, int64_t M>
1138   bool isImmediateS4() const {
1139     if (!isImm()) return false;
1140     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1141     if (!CE) return false;
1142     int64_t Value = CE->getValue();
1143     return ((Value & 3) == 0) && Value >= N && Value <= M;
1144   }
1145   template<int64_t N, int64_t M>
1146   bool isImmediateS2() const {
1147     if (!isImm()) return false;
1148     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1149     if (!CE) return false;
1150     int64_t Value = CE->getValue();
1151     return ((Value & 1) == 0) && Value >= N && Value <= M;
1152   }
1153   bool isFBits16() const {
1154     return isImmediate<0, 17>();
1155   }
1156   bool isFBits32() const {
1157     return isImmediate<1, 33>();
1158   }
1159   bool isImm8s4() const {
1160     return isImmediateS4<-1020, 1020>();
1161   }
1162   bool isImm7s4() const {
1163     return isImmediateS4<-508, 508>();
1164   }
1165   bool isImm7Shift0() const {
1166     return isImmediate<-127, 127>();
1167   }
1168   bool isImm7Shift1() const {
1169     return isImmediateS2<-255, 255>();
1170   }
1171   bool isImm7Shift2() const {
1172     return isImmediateS4<-511, 511>();
1173   }
1174   bool isImm7() const {
1175     return isImmediate<-127, 127>();
1176   }
1177   bool isImm0_1020s4() const {
1178     return isImmediateS4<0, 1020>();
1179   }
1180   bool isImm0_508s4() const {
1181     return isImmediateS4<0, 508>();
1182   }
1183   bool isImm0_508s4Neg() const {
1184     if (!isImm()) return false;
1185     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1186     if (!CE) return false;
1187     int64_t Value = -CE->getValue();
1188     // explicitly exclude zero. we want that to use the normal 0_508 version.
1189     return ((Value & 3) == 0) && Value > 0 && Value <= 508;
1190   }
1191 
1192   bool isImm0_4095Neg() const {
1193     if (!isImm()) return false;
1194     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1195     if (!CE) return false;
1196     // isImm0_4095Neg is used with 32-bit immediates only.
1197     // 32-bit immediates are zero extended to 64-bit when parsed,
1198     // thus simple -CE->getValue() results in a big negative number,
1199     // not a small positive number as intended
1200     if ((CE->getValue() >> 32) > 0) return false;
1201     uint32_t Value = -static_cast<uint32_t>(CE->getValue());
1202     return Value > 0 && Value < 4096;
1203   }
1204 
1205   bool isImm0_7() const {
1206     return isImmediate<0, 7>();
1207   }
1208 
1209   bool isImm1_16() const {
1210     return isImmediate<1, 16>();
1211   }
1212 
1213   bool isImm1_32() const {
1214     return isImmediate<1, 32>();
1215   }
1216 
1217   bool isImm8_255() const {
1218     return isImmediate<8, 255>();
1219   }
1220 
1221   bool isImm256_65535Expr() const {
1222     if (!isImm()) return false;
1223     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1224     // If it's not a constant expression, it'll generate a fixup and be
1225     // handled later.
1226     if (!CE) return true;
1227     int64_t Value = CE->getValue();
1228     return Value >= 256 && Value < 65536;
1229   }
1230 
1231   bool isImm0_65535Expr() const {
1232     if (!isImm()) return false;
1233     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1234     // If it's not a constant expression, it'll generate a fixup and be
1235     // handled later.
1236     if (!CE) return true;
1237     int64_t Value = CE->getValue();
1238     return Value >= 0 && Value < 65536;
1239   }
1240 
1241   bool isImm24bit() const {
1242     return isImmediate<0, 0xffffff + 1>();
1243   }
1244 
1245   bool isImmThumbSR() const {
1246     return isImmediate<1, 33>();
1247   }
1248 
1249   template<int shift>
1250   bool isExpImmValue(uint64_t Value) const {
1251     uint64_t mask = (1 << shift) - 1;
1252     if ((Value & mask) != 0 || (Value >> shift) > 0xff)
1253       return false;
1254     return true;
1255   }
1256 
1257   template<int shift>
1258   bool isExpImm() const {
1259     if (!isImm()) return false;
1260     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1261     if (!CE) return false;
1262 
1263     return isExpImmValue<shift>(CE->getValue());
1264   }
1265 
1266   template<int shift, int size>
1267   bool isInvertedExpImm() const {
1268     if (!isImm()) return false;
1269     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1270     if (!CE) return false;
1271 
1272     uint64_t OriginalValue = CE->getValue();
1273     uint64_t InvertedValue = OriginalValue ^ (((uint64_t)1 << size) - 1);
1274     return isExpImmValue<shift>(InvertedValue);
1275   }
1276 
1277   bool isPKHLSLImm() const {
1278     return isImmediate<0, 32>();
1279   }
1280 
1281   bool isPKHASRImm() const {
1282     return isImmediate<0, 33>();
1283   }
1284 
1285   bool isAdrLabel() const {
1286     // If we have an immediate that's not a constant, treat it as a label
1287     // reference needing a fixup.
1288     if (isImm() && !isa<MCConstantExpr>(getImm()))
1289       return true;
1290 
1291     // If it is a constant, it must fit into a modified immediate encoding.
1292     if (!isImm()) return false;
1293     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1294     if (!CE) return false;
1295     int64_t Value = CE->getValue();
1296     return (ARM_AM::getSOImmVal(Value) != -1 ||
1297             ARM_AM::getSOImmVal(-Value) != -1);
1298   }
1299 
1300   bool isT2SOImm() const {
1301     // If we have an immediate that's not a constant, treat it as an expression
1302     // needing a fixup.
1303     if (isImm() && !isa<MCConstantExpr>(getImm())) {
1304       // We want to avoid matching :upper16: and :lower16: as we want these
1305       // expressions to match in isImm0_65535Expr()
1306       const ARMMCExpr *ARM16Expr = dyn_cast<ARMMCExpr>(getImm());
1307       return (!ARM16Expr || (ARM16Expr->getKind() != ARMMCExpr::VK_ARM_HI16 &&
1308                              ARM16Expr->getKind() != ARMMCExpr::VK_ARM_LO16));
1309     }
1310     if (!isImm()) return false;
1311     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1312     if (!CE) return false;
1313     int64_t Value = CE->getValue();
1314     return ARM_AM::getT2SOImmVal(Value) != -1;
1315   }
1316 
1317   bool isT2SOImmNot() const {
1318     if (!isImm()) return false;
1319     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1320     if (!CE) return false;
1321     int64_t Value = CE->getValue();
1322     return ARM_AM::getT2SOImmVal(Value) == -1 &&
1323       ARM_AM::getT2SOImmVal(~Value) != -1;
1324   }
1325 
1326   bool isT2SOImmNeg() const {
1327     if (!isImm()) return false;
1328     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1329     if (!CE) return false;
1330     int64_t Value = CE->getValue();
1331     // Only use this when not representable as a plain so_imm.
1332     return ARM_AM::getT2SOImmVal(Value) == -1 &&
1333       ARM_AM::getT2SOImmVal(-Value) != -1;
1334   }
1335 
1336   bool isSetEndImm() const {
1337     if (!isImm()) return false;
1338     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1339     if (!CE) return false;
1340     int64_t Value = CE->getValue();
1341     return Value == 1 || Value == 0;
1342   }
1343 
1344   bool isReg() const override { return Kind == k_Register; }
1345   bool isRegList() const { return Kind == k_RegisterList; }
1346   bool isRegListWithAPSR() const {
1347     return Kind == k_RegisterListWithAPSR || Kind == k_RegisterList;
1348   }
1349   bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
1350   bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
1351   bool isFPSRegListWithVPR() const { return Kind == k_FPSRegisterListWithVPR; }
1352   bool isFPDRegListWithVPR() const { return Kind == k_FPDRegisterListWithVPR; }
1353   bool isToken() const override { return Kind == k_Token; }
1354   bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
1355   bool isInstSyncBarrierOpt() const { return Kind == k_InstSyncBarrierOpt; }
1356   bool isTraceSyncBarrierOpt() const { return Kind == k_TraceSyncBarrierOpt; }
1357   bool isMem() const override {
1358       return isGPRMem() || isMVEMem();
1359   }
1360   bool isMVEMem() const {
1361     if (Kind != k_Memory)
1362       return false;
1363     if (Memory.BaseRegNum &&
1364         !ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Memory.BaseRegNum) &&
1365         !ARMMCRegisterClasses[ARM::MQPRRegClassID].contains(Memory.BaseRegNum))
1366       return false;
1367     if (Memory.OffsetRegNum &&
1368         !ARMMCRegisterClasses[ARM::MQPRRegClassID].contains(
1369             Memory.OffsetRegNum))
1370       return false;
1371     return true;
1372   }
1373   bool isGPRMem() const {
1374     if (Kind != k_Memory)
1375       return false;
1376     if (Memory.BaseRegNum &&
1377         !ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Memory.BaseRegNum))
1378       return false;
1379     if (Memory.OffsetRegNum &&
1380         !ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Memory.OffsetRegNum))
1381       return false;
1382     return true;
1383   }
1384   bool isShifterImm() const { return Kind == k_ShifterImmediate; }
1385   bool isRegShiftedReg() const {
1386     return Kind == k_ShiftedRegister &&
1387            ARMMCRegisterClasses[ARM::GPRRegClassID].contains(
1388                RegShiftedReg.SrcReg) &&
1389            ARMMCRegisterClasses[ARM::GPRRegClassID].contains(
1390                RegShiftedReg.ShiftReg);
1391   }
1392   bool isRegShiftedImm() const {
1393     return Kind == k_ShiftedImmediate &&
1394            ARMMCRegisterClasses[ARM::GPRRegClassID].contains(
1395                RegShiftedImm.SrcReg);
1396   }
1397   bool isRotImm() const { return Kind == k_RotateImmediate; }
1398 
1399   template<unsigned Min, unsigned Max>
1400   bool isPowerTwoInRange() const {
1401     if (!isImm()) return false;
1402     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1403     if (!CE) return false;
1404     int64_t Value = CE->getValue();
1405     return Value > 0 && countPopulation((uint64_t)Value) == 1 &&
1406            Value >= Min && Value <= Max;
1407   }
1408   bool isModImm() const { return Kind == k_ModifiedImmediate; }
1409 
1410   bool isModImmNot() const {
1411     if (!isImm()) return false;
1412     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1413     if (!CE) return false;
1414     int64_t Value = CE->getValue();
1415     return ARM_AM::getSOImmVal(~Value) != -1;
1416   }
1417 
1418   bool isModImmNeg() const {
1419     if (!isImm()) return false;
1420     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1421     if (!CE) return false;
1422     int64_t Value = CE->getValue();
1423     return ARM_AM::getSOImmVal(Value) == -1 &&
1424       ARM_AM::getSOImmVal(-Value) != -1;
1425   }
1426 
1427   bool isThumbModImmNeg1_7() const {
1428     if (!isImm()) return false;
1429     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1430     if (!CE) return false;
1431     int32_t Value = -(int32_t)CE->getValue();
1432     return 0 < Value && Value < 8;
1433   }
1434 
1435   bool isThumbModImmNeg8_255() const {
1436     if (!isImm()) return false;
1437     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1438     if (!CE) return false;
1439     int32_t Value = -(int32_t)CE->getValue();
1440     return 7 < Value && Value < 256;
1441   }
1442 
1443   bool isConstantPoolImm() const { return Kind == k_ConstantPoolImmediate; }
1444   bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
1445   bool isPostIdxRegShifted() const {
1446     return Kind == k_PostIndexRegister &&
1447            ARMMCRegisterClasses[ARM::GPRRegClassID].contains(PostIdxReg.RegNum);
1448   }
1449   bool isPostIdxReg() const {
1450     return isPostIdxRegShifted() && PostIdxReg.ShiftTy == ARM_AM::no_shift;
1451   }
1452   bool isMemNoOffset(bool alignOK = false, unsigned Alignment = 0) const {
1453     if (!isGPRMem())
1454       return false;
1455     // No offset of any kind.
1456     return Memory.OffsetRegNum == 0 && Memory.OffsetImm == nullptr &&
1457      (alignOK || Memory.Alignment == Alignment);
1458   }
1459   bool isMemNoOffsetT2(bool alignOK = false, unsigned Alignment = 0) const {
1460     if (!isGPRMem())
1461       return false;
1462 
1463     if (!ARMMCRegisterClasses[ARM::GPRnopcRegClassID].contains(
1464             Memory.BaseRegNum))
1465       return false;
1466 
1467     // No offset of any kind.
1468     return Memory.OffsetRegNum == 0 && Memory.OffsetImm == nullptr &&
1469      (alignOK || Memory.Alignment == Alignment);
1470   }
1471   bool isMemNoOffsetT2NoSp(bool alignOK = false, unsigned Alignment = 0) const {
1472     if (!isGPRMem())
1473       return false;
1474 
1475     if (!ARMMCRegisterClasses[ARM::rGPRRegClassID].contains(
1476             Memory.BaseRegNum))
1477       return false;
1478 
1479     // No offset of any kind.
1480     return Memory.OffsetRegNum == 0 && Memory.OffsetImm == nullptr &&
1481      (alignOK || Memory.Alignment == Alignment);
1482   }
1483   bool isMemNoOffsetT(bool alignOK = false, unsigned Alignment = 0) const {
1484     if (!isGPRMem())
1485       return false;
1486 
1487     if (!ARMMCRegisterClasses[ARM::tGPRRegClassID].contains(
1488             Memory.BaseRegNum))
1489       return false;
1490 
1491     // No offset of any kind.
1492     return Memory.OffsetRegNum == 0 && Memory.OffsetImm == nullptr &&
1493      (alignOK || Memory.Alignment == Alignment);
1494   }
1495   bool isMemPCRelImm12() const {
1496     if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1497       return false;
1498     // Base register must be PC.
1499     if (Memory.BaseRegNum != ARM::PC)
1500       return false;
1501     // Immediate offset in range [-4095, 4095].
1502     if (!Memory.OffsetImm) return true;
1503     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1504       int64_t Val = CE->getValue();
1505       return (Val > -4096 && Val < 4096) ||
1506              (Val == std::numeric_limits<int32_t>::min());
1507     }
1508     return false;
1509   }
1510 
1511   bool isAlignedMemory() const {
1512     return isMemNoOffset(true);
1513   }
1514 
1515   bool isAlignedMemoryNone() const {
1516     return isMemNoOffset(false, 0);
1517   }
1518 
1519   bool isDupAlignedMemoryNone() const {
1520     return isMemNoOffset(false, 0);
1521   }
1522 
1523   bool isAlignedMemory16() const {
1524     if (isMemNoOffset(false, 2)) // alignment in bytes for 16-bits is 2.
1525       return true;
1526     return isMemNoOffset(false, 0);
1527   }
1528 
1529   bool isDupAlignedMemory16() const {
1530     if (isMemNoOffset(false, 2)) // alignment in bytes for 16-bits is 2.
1531       return true;
1532     return isMemNoOffset(false, 0);
1533   }
1534 
1535   bool isAlignedMemory32() const {
1536     if (isMemNoOffset(false, 4)) // alignment in bytes for 32-bits is 4.
1537       return true;
1538     return isMemNoOffset(false, 0);
1539   }
1540 
1541   bool isDupAlignedMemory32() const {
1542     if (isMemNoOffset(false, 4)) // alignment in bytes for 32-bits is 4.
1543       return true;
1544     return isMemNoOffset(false, 0);
1545   }
1546 
1547   bool isAlignedMemory64() const {
1548     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1549       return true;
1550     return isMemNoOffset(false, 0);
1551   }
1552 
1553   bool isDupAlignedMemory64() const {
1554     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1555       return true;
1556     return isMemNoOffset(false, 0);
1557   }
1558 
1559   bool isAlignedMemory64or128() const {
1560     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1561       return true;
1562     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1563       return true;
1564     return isMemNoOffset(false, 0);
1565   }
1566 
1567   bool isDupAlignedMemory64or128() const {
1568     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1569       return true;
1570     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1571       return true;
1572     return isMemNoOffset(false, 0);
1573   }
1574 
1575   bool isAlignedMemory64or128or256() const {
1576     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1577       return true;
1578     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1579       return true;
1580     if (isMemNoOffset(false, 32)) // alignment in bytes for 256-bits is 32.
1581       return true;
1582     return isMemNoOffset(false, 0);
1583   }
1584 
1585   bool isAddrMode2() const {
1586     if (!isGPRMem() || Memory.Alignment != 0) return false;
1587     // Check for register offset.
1588     if (Memory.OffsetRegNum) return true;
1589     // Immediate offset in range [-4095, 4095].
1590     if (!Memory.OffsetImm) return true;
1591     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1592       int64_t Val = CE->getValue();
1593       return Val > -4096 && Val < 4096;
1594     }
1595     return false;
1596   }
1597 
1598   bool isAM2OffsetImm() const {
1599     if (!isImm()) return false;
1600     // Immediate offset in range [-4095, 4095].
1601     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1602     if (!CE) return false;
1603     int64_t Val = CE->getValue();
1604     return (Val == std::numeric_limits<int32_t>::min()) ||
1605            (Val > -4096 && Val < 4096);
1606   }
1607 
1608   bool isAddrMode3() const {
1609     // If we have an immediate that's not a constant, treat it as a label
1610     // reference needing a fixup. If it is a constant, it's something else
1611     // and we reject it.
1612     if (isImm() && !isa<MCConstantExpr>(getImm()))
1613       return true;
1614     if (!isGPRMem() || Memory.Alignment != 0) return false;
1615     // No shifts are legal for AM3.
1616     if (Memory.ShiftType != ARM_AM::no_shift) return false;
1617     // Check for register offset.
1618     if (Memory.OffsetRegNum) return true;
1619     // Immediate offset in range [-255, 255].
1620     if (!Memory.OffsetImm) return true;
1621     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1622       int64_t Val = CE->getValue();
1623       // The #-0 offset is encoded as std::numeric_limits<int32_t>::min(), and
1624       // we have to check for this too.
1625       return (Val > -256 && Val < 256) ||
1626              Val == std::numeric_limits<int32_t>::min();
1627     }
1628     return false;
1629   }
1630 
1631   bool isAM3Offset() const {
1632     if (isPostIdxReg())
1633       return true;
1634     if (!isImm())
1635       return false;
1636     // Immediate offset in range [-255, 255].
1637     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1638     if (!CE) return false;
1639     int64_t Val = CE->getValue();
1640     // Special case, #-0 is std::numeric_limits<int32_t>::min().
1641     return (Val > -256 && Val < 256) ||
1642            Val == std::numeric_limits<int32_t>::min();
1643   }
1644 
1645   bool isAddrMode5() const {
1646     // If we have an immediate that's not a constant, treat it as a label
1647     // reference needing a fixup. If it is a constant, it's something else
1648     // and we reject it.
1649     if (isImm() && !isa<MCConstantExpr>(getImm()))
1650       return true;
1651     if (!isGPRMem() || Memory.Alignment != 0) return false;
1652     // Check for register offset.
1653     if (Memory.OffsetRegNum) return false;
1654     // Immediate offset in range [-1020, 1020] and a multiple of 4.
1655     if (!Memory.OffsetImm) return true;
1656     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1657       int64_t Val = CE->getValue();
1658       return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
1659              Val == std::numeric_limits<int32_t>::min();
1660     }
1661     return false;
1662   }
1663 
1664   bool isAddrMode5FP16() const {
1665     // If we have an immediate that's not a constant, treat it as a label
1666     // reference needing a fixup. If it is a constant, it's something else
1667     // and we reject it.
1668     if (isImm() && !isa<MCConstantExpr>(getImm()))
1669       return true;
1670     if (!isGPRMem() || Memory.Alignment != 0) return false;
1671     // Check for register offset.
1672     if (Memory.OffsetRegNum) return false;
1673     // Immediate offset in range [-510, 510] and a multiple of 2.
1674     if (!Memory.OffsetImm) return true;
1675     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1676       int64_t Val = CE->getValue();
1677       return (Val >= -510 && Val <= 510 && ((Val & 1) == 0)) ||
1678              Val == std::numeric_limits<int32_t>::min();
1679     }
1680     return false;
1681   }
1682 
1683   bool isMemTBB() const {
1684     if (!isGPRMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1685         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
1686       return false;
1687     return true;
1688   }
1689 
1690   bool isMemTBH() const {
1691     if (!isGPRMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1692         Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
1693         Memory.Alignment != 0 )
1694       return false;
1695     return true;
1696   }
1697 
1698   bool isMemRegOffset() const {
1699     if (!isGPRMem() || !Memory.OffsetRegNum || Memory.Alignment != 0)
1700       return false;
1701     return true;
1702   }
1703 
1704   bool isT2MemRegOffset() const {
1705     if (!isGPRMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1706         Memory.Alignment != 0 || Memory.BaseRegNum == ARM::PC)
1707       return false;
1708     // Only lsl #{0, 1, 2, 3} allowed.
1709     if (Memory.ShiftType == ARM_AM::no_shift)
1710       return true;
1711     if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
1712       return false;
1713     return true;
1714   }
1715 
1716   bool isMemThumbRR() const {
1717     // Thumb reg+reg addressing is simple. Just two registers, a base and
1718     // an offset. No shifts, negations or any other complicating factors.
1719     if (!isGPRMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1720         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
1721       return false;
1722     return isARMLowRegister(Memory.BaseRegNum) &&
1723       (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
1724   }
1725 
1726   bool isMemThumbRIs4() const {
1727     if (!isGPRMem() || Memory.OffsetRegNum != 0 ||
1728         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1729       return false;
1730     // Immediate offset, multiple of 4 in range [0, 124].
1731     if (!Memory.OffsetImm) return true;
1732     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1733       int64_t Val = CE->getValue();
1734       return Val >= 0 && Val <= 124 && (Val % 4) == 0;
1735     }
1736     return false;
1737   }
1738 
1739   bool isMemThumbRIs2() const {
1740     if (!isGPRMem() || Memory.OffsetRegNum != 0 ||
1741         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1742       return false;
1743     // Immediate offset, multiple of 4 in range [0, 62].
1744     if (!Memory.OffsetImm) return true;
1745     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1746       int64_t Val = CE->getValue();
1747       return Val >= 0 && Val <= 62 && (Val % 2) == 0;
1748     }
1749     return false;
1750   }
1751 
1752   bool isMemThumbRIs1() const {
1753     if (!isGPRMem() || Memory.OffsetRegNum != 0 ||
1754         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1755       return false;
1756     // Immediate offset in range [0, 31].
1757     if (!Memory.OffsetImm) return true;
1758     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1759       int64_t Val = CE->getValue();
1760       return Val >= 0 && Val <= 31;
1761     }
1762     return false;
1763   }
1764 
1765   bool isMemThumbSPI() const {
1766     if (!isGPRMem() || Memory.OffsetRegNum != 0 ||
1767         Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
1768       return false;
1769     // Immediate offset, multiple of 4 in range [0, 1020].
1770     if (!Memory.OffsetImm) return true;
1771     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1772       int64_t Val = CE->getValue();
1773       return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
1774     }
1775     return false;
1776   }
1777 
1778   bool isMemImm8s4Offset() const {
1779     // If we have an immediate that's not a constant, treat it as a label
1780     // reference needing a fixup. If it is a constant, it's something else
1781     // and we reject it.
1782     if (isImm() && !isa<MCConstantExpr>(getImm()))
1783       return true;
1784     if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1785       return false;
1786     // Immediate offset a multiple of 4 in range [-1020, 1020].
1787     if (!Memory.OffsetImm) return true;
1788     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1789       int64_t Val = CE->getValue();
1790       // Special case, #-0 is std::numeric_limits<int32_t>::min().
1791       return (Val >= -1020 && Val <= 1020 && (Val & 3) == 0) ||
1792              Val == std::numeric_limits<int32_t>::min();
1793     }
1794     return false;
1795   }
1796 
1797   bool isMemImm7s4Offset() const {
1798     // If we have an immediate that's not a constant, treat it as a label
1799     // reference needing a fixup. If it is a constant, it's something else
1800     // and we reject it.
1801     if (isImm() && !isa<MCConstantExpr>(getImm()))
1802       return true;
1803     if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0 ||
1804         !ARMMCRegisterClasses[ARM::GPRnopcRegClassID].contains(
1805             Memory.BaseRegNum))
1806       return false;
1807     // Immediate offset a multiple of 4 in range [-508, 508].
1808     if (!Memory.OffsetImm) return true;
1809     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1810       int64_t Val = CE->getValue();
1811       // Special case, #-0 is INT32_MIN.
1812       return (Val >= -508 && Val <= 508 && (Val & 3) == 0) || Val == INT32_MIN;
1813     }
1814     return false;
1815   }
1816 
1817   bool isMemImm0_1020s4Offset() const {
1818     if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1819       return false;
1820     // Immediate offset a multiple of 4 in range [0, 1020].
1821     if (!Memory.OffsetImm) return true;
1822     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1823       int64_t Val = CE->getValue();
1824       return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
1825     }
1826     return false;
1827   }
1828 
1829   bool isMemImm8Offset() const {
1830     if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1831       return false;
1832     // Base reg of PC isn't allowed for these encodings.
1833     if (Memory.BaseRegNum == ARM::PC) return false;
1834     // Immediate offset in range [-255, 255].
1835     if (!Memory.OffsetImm) return true;
1836     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1837       int64_t Val = CE->getValue();
1838       return (Val == std::numeric_limits<int32_t>::min()) ||
1839              (Val > -256 && Val < 256);
1840     }
1841     return false;
1842   }
1843 
1844   template<unsigned Bits, unsigned RegClassID>
1845   bool isMemImm7ShiftedOffset() const {
1846     if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0 ||
1847         !ARMMCRegisterClasses[RegClassID].contains(Memory.BaseRegNum))
1848       return false;
1849 
1850     // Expect an immediate offset equal to an element of the range
1851     // [-127, 127], shifted left by Bits.
1852 
1853     if (!Memory.OffsetImm) return true;
1854     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1855       int64_t Val = CE->getValue();
1856 
1857       // INT32_MIN is a special-case value (indicating the encoding with
1858       // zero offset and the subtract bit set)
1859       if (Val == INT32_MIN)
1860         return true;
1861 
1862       unsigned Divisor = 1U << Bits;
1863 
1864       // Check that the low bits are zero
1865       if (Val % Divisor != 0)
1866         return false;
1867 
1868       // Check that the remaining offset is within range.
1869       Val /= Divisor;
1870       return (Val >= -127 && Val <= 127);
1871     }
1872     return false;
1873   }
1874 
1875   template <int shift> bool isMemRegRQOffset() const {
1876     if (!isMVEMem() || Memory.OffsetImm != 0 || Memory.Alignment != 0)
1877       return false;
1878 
1879     if (!ARMMCRegisterClasses[ARM::GPRnopcRegClassID].contains(
1880             Memory.BaseRegNum))
1881       return false;
1882     if (!ARMMCRegisterClasses[ARM::MQPRRegClassID].contains(
1883             Memory.OffsetRegNum))
1884       return false;
1885 
1886     if (shift == 0 && Memory.ShiftType != ARM_AM::no_shift)
1887       return false;
1888 
1889     if (shift > 0 &&
1890         (Memory.ShiftType != ARM_AM::uxtw || Memory.ShiftImm != shift))
1891       return false;
1892 
1893     return true;
1894   }
1895 
1896   template <int shift> bool isMemRegQOffset() const {
1897     if (!isMVEMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1898       return false;
1899 
1900     if (!ARMMCRegisterClasses[ARM::MQPRRegClassID].contains(
1901             Memory.BaseRegNum))
1902       return false;
1903 
1904     if (!Memory.OffsetImm)
1905       return true;
1906     static_assert(shift < 56,
1907                   "Such that we dont shift by a value higher than 62");
1908     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1909       int64_t Val = CE->getValue();
1910 
1911       // The value must be a multiple of (1 << shift)
1912       if ((Val & ((1U << shift) - 1)) != 0)
1913         return false;
1914 
1915       // And be in the right range, depending on the amount that it is shifted
1916       // by.  Shift 0, is equal to 7 unsigned bits, the sign bit is set
1917       // separately.
1918       int64_t Range = (1U << (7 + shift)) - 1;
1919       return (Val == INT32_MIN) || (Val > -Range && Val < Range);
1920     }
1921     return false;
1922   }
1923 
1924   bool isMemPosImm8Offset() const {
1925     if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1926       return false;
1927     // Immediate offset in range [0, 255].
1928     if (!Memory.OffsetImm) return true;
1929     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1930       int64_t Val = CE->getValue();
1931       return Val >= 0 && Val < 256;
1932     }
1933     return false;
1934   }
1935 
1936   bool isMemNegImm8Offset() const {
1937     if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1938       return false;
1939     // Base reg of PC isn't allowed for these encodings.
1940     if (Memory.BaseRegNum == ARM::PC) return false;
1941     // Immediate offset in range [-255, -1].
1942     if (!Memory.OffsetImm) return false;
1943     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1944       int64_t Val = CE->getValue();
1945       return (Val == std::numeric_limits<int32_t>::min()) ||
1946              (Val > -256 && Val < 0);
1947     }
1948     return false;
1949   }
1950 
1951   bool isMemUImm12Offset() const {
1952     if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1953       return false;
1954     // Immediate offset in range [0, 4095].
1955     if (!Memory.OffsetImm) return true;
1956     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1957       int64_t Val = CE->getValue();
1958       return (Val >= 0 && Val < 4096);
1959     }
1960     return false;
1961   }
1962 
1963   bool isMemImm12Offset() const {
1964     // If we have an immediate that's not a constant, treat it as a label
1965     // reference needing a fixup. If it is a constant, it's something else
1966     // and we reject it.
1967 
1968     if (isImm() && !isa<MCConstantExpr>(getImm()))
1969       return true;
1970 
1971     if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1972       return false;
1973     // Immediate offset in range [-4095, 4095].
1974     if (!Memory.OffsetImm) return true;
1975     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1976       int64_t Val = CE->getValue();
1977       return (Val > -4096 && Val < 4096) ||
1978              (Val == std::numeric_limits<int32_t>::min());
1979     }
1980     // If we have an immediate that's not a constant, treat it as a
1981     // symbolic expression needing a fixup.
1982     return true;
1983   }
1984 
1985   bool isConstPoolAsmImm() const {
1986     // Delay processing of Constant Pool Immediate, this will turn into
1987     // a constant. Match no other operand
1988     return (isConstantPoolImm());
1989   }
1990 
1991   bool isPostIdxImm8() const {
1992     if (!isImm()) return false;
1993     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1994     if (!CE) return false;
1995     int64_t Val = CE->getValue();
1996     return (Val > -256 && Val < 256) ||
1997            (Val == std::numeric_limits<int32_t>::min());
1998   }
1999 
2000   bool isPostIdxImm8s4() const {
2001     if (!isImm()) return false;
2002     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2003     if (!CE) return false;
2004     int64_t Val = CE->getValue();
2005     return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
2006            (Val == std::numeric_limits<int32_t>::min());
2007   }
2008 
2009   bool isMSRMask() const { return Kind == k_MSRMask; }
2010   bool isBankedReg() const { return Kind == k_BankedReg; }
2011   bool isProcIFlags() const { return Kind == k_ProcIFlags; }
2012 
2013   // NEON operands.
2014   bool isSingleSpacedVectorList() const {
2015     return Kind == k_VectorList && !VectorList.isDoubleSpaced;
2016   }
2017 
2018   bool isDoubleSpacedVectorList() const {
2019     return Kind == k_VectorList && VectorList.isDoubleSpaced;
2020   }
2021 
2022   bool isVecListOneD() const {
2023     if (!isSingleSpacedVectorList()) return false;
2024     return VectorList.Count == 1;
2025   }
2026 
2027   bool isVecListTwoMQ() const {
2028     return isSingleSpacedVectorList() && VectorList.Count == 2 &&
2029            ARMMCRegisterClasses[ARM::MQPRRegClassID].contains(
2030                VectorList.RegNum);
2031   }
2032 
2033   bool isVecListDPair() const {
2034     if (!isSingleSpacedVectorList()) return false;
2035     return (ARMMCRegisterClasses[ARM::DPairRegClassID]
2036               .contains(VectorList.RegNum));
2037   }
2038 
2039   bool isVecListThreeD() const {
2040     if (!isSingleSpacedVectorList()) return false;
2041     return VectorList.Count == 3;
2042   }
2043 
2044   bool isVecListFourD() const {
2045     if (!isSingleSpacedVectorList()) return false;
2046     return VectorList.Count == 4;
2047   }
2048 
2049   bool isVecListDPairSpaced() const {
2050     if (Kind != k_VectorList) return false;
2051     if (isSingleSpacedVectorList()) return false;
2052     return (ARMMCRegisterClasses[ARM::DPairSpcRegClassID]
2053               .contains(VectorList.RegNum));
2054   }
2055 
2056   bool isVecListThreeQ() const {
2057     if (!isDoubleSpacedVectorList()) return false;
2058     return VectorList.Count == 3;
2059   }
2060 
2061   bool isVecListFourQ() const {
2062     if (!isDoubleSpacedVectorList()) return false;
2063     return VectorList.Count == 4;
2064   }
2065 
2066   bool isVecListFourMQ() const {
2067     return isSingleSpacedVectorList() && VectorList.Count == 4 &&
2068            ARMMCRegisterClasses[ARM::MQPRRegClassID].contains(
2069                VectorList.RegNum);
2070   }
2071 
2072   bool isSingleSpacedVectorAllLanes() const {
2073     return Kind == k_VectorListAllLanes && !VectorList.isDoubleSpaced;
2074   }
2075 
2076   bool isDoubleSpacedVectorAllLanes() const {
2077     return Kind == k_VectorListAllLanes && VectorList.isDoubleSpaced;
2078   }
2079 
2080   bool isVecListOneDAllLanes() const {
2081     if (!isSingleSpacedVectorAllLanes()) return false;
2082     return VectorList.Count == 1;
2083   }
2084 
2085   bool isVecListDPairAllLanes() const {
2086     if (!isSingleSpacedVectorAllLanes()) return false;
2087     return (ARMMCRegisterClasses[ARM::DPairRegClassID]
2088               .contains(VectorList.RegNum));
2089   }
2090 
2091   bool isVecListDPairSpacedAllLanes() const {
2092     if (!isDoubleSpacedVectorAllLanes()) return false;
2093     return VectorList.Count == 2;
2094   }
2095 
2096   bool isVecListThreeDAllLanes() const {
2097     if (!isSingleSpacedVectorAllLanes()) return false;
2098     return VectorList.Count == 3;
2099   }
2100 
2101   bool isVecListThreeQAllLanes() const {
2102     if (!isDoubleSpacedVectorAllLanes()) return false;
2103     return VectorList.Count == 3;
2104   }
2105 
2106   bool isVecListFourDAllLanes() const {
2107     if (!isSingleSpacedVectorAllLanes()) return false;
2108     return VectorList.Count == 4;
2109   }
2110 
2111   bool isVecListFourQAllLanes() const {
2112     if (!isDoubleSpacedVectorAllLanes()) return false;
2113     return VectorList.Count == 4;
2114   }
2115 
2116   bool isSingleSpacedVectorIndexed() const {
2117     return Kind == k_VectorListIndexed && !VectorList.isDoubleSpaced;
2118   }
2119 
2120   bool isDoubleSpacedVectorIndexed() const {
2121     return Kind == k_VectorListIndexed && VectorList.isDoubleSpaced;
2122   }
2123 
2124   bool isVecListOneDByteIndexed() const {
2125     if (!isSingleSpacedVectorIndexed()) return false;
2126     return VectorList.Count == 1 && VectorList.LaneIndex <= 7;
2127   }
2128 
2129   bool isVecListOneDHWordIndexed() const {
2130     if (!isSingleSpacedVectorIndexed()) return false;
2131     return VectorList.Count == 1 && VectorList.LaneIndex <= 3;
2132   }
2133 
2134   bool isVecListOneDWordIndexed() const {
2135     if (!isSingleSpacedVectorIndexed()) return false;
2136     return VectorList.Count == 1 && VectorList.LaneIndex <= 1;
2137   }
2138 
2139   bool isVecListTwoDByteIndexed() const {
2140     if (!isSingleSpacedVectorIndexed()) return false;
2141     return VectorList.Count == 2 && VectorList.LaneIndex <= 7;
2142   }
2143 
2144   bool isVecListTwoDHWordIndexed() const {
2145     if (!isSingleSpacedVectorIndexed()) return false;
2146     return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
2147   }
2148 
2149   bool isVecListTwoQWordIndexed() const {
2150     if (!isDoubleSpacedVectorIndexed()) return false;
2151     return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
2152   }
2153 
2154   bool isVecListTwoQHWordIndexed() const {
2155     if (!isDoubleSpacedVectorIndexed()) return false;
2156     return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
2157   }
2158 
2159   bool isVecListTwoDWordIndexed() const {
2160     if (!isSingleSpacedVectorIndexed()) return false;
2161     return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
2162   }
2163 
2164   bool isVecListThreeDByteIndexed() const {
2165     if (!isSingleSpacedVectorIndexed()) return false;
2166     return VectorList.Count == 3 && VectorList.LaneIndex <= 7;
2167   }
2168 
2169   bool isVecListThreeDHWordIndexed() const {
2170     if (!isSingleSpacedVectorIndexed()) return false;
2171     return VectorList.Count == 3 && VectorList.LaneIndex <= 3;
2172   }
2173 
2174   bool isVecListThreeQWordIndexed() const {
2175     if (!isDoubleSpacedVectorIndexed()) return false;
2176     return VectorList.Count == 3 && VectorList.LaneIndex <= 1;
2177   }
2178 
2179   bool isVecListThreeQHWordIndexed() const {
2180     if (!isDoubleSpacedVectorIndexed()) return false;
2181     return VectorList.Count == 3 && VectorList.LaneIndex <= 3;
2182   }
2183 
2184   bool isVecListThreeDWordIndexed() const {
2185     if (!isSingleSpacedVectorIndexed()) return false;
2186     return VectorList.Count == 3 && VectorList.LaneIndex <= 1;
2187   }
2188 
2189   bool isVecListFourDByteIndexed() const {
2190     if (!isSingleSpacedVectorIndexed()) return false;
2191     return VectorList.Count == 4 && VectorList.LaneIndex <= 7;
2192   }
2193 
2194   bool isVecListFourDHWordIndexed() const {
2195     if (!isSingleSpacedVectorIndexed()) return false;
2196     return VectorList.Count == 4 && VectorList.LaneIndex <= 3;
2197   }
2198 
2199   bool isVecListFourQWordIndexed() const {
2200     if (!isDoubleSpacedVectorIndexed()) return false;
2201     return VectorList.Count == 4 && VectorList.LaneIndex <= 1;
2202   }
2203 
2204   bool isVecListFourQHWordIndexed() const {
2205     if (!isDoubleSpacedVectorIndexed()) return false;
2206     return VectorList.Count == 4 && VectorList.LaneIndex <= 3;
2207   }
2208 
2209   bool isVecListFourDWordIndexed() const {
2210     if (!isSingleSpacedVectorIndexed()) return false;
2211     return VectorList.Count == 4 && VectorList.LaneIndex <= 1;
2212   }
2213 
2214   bool isVectorIndex() const { return Kind == k_VectorIndex; }
2215 
2216   template <unsigned NumLanes>
2217   bool isVectorIndexInRange() const {
2218     if (Kind != k_VectorIndex) return false;
2219     return VectorIndex.Val < NumLanes;
2220   }
2221 
2222   bool isVectorIndex8()  const { return isVectorIndexInRange<8>(); }
2223   bool isVectorIndex16() const { return isVectorIndexInRange<4>(); }
2224   bool isVectorIndex32() const { return isVectorIndexInRange<2>(); }
2225   bool isVectorIndex64() const { return isVectorIndexInRange<1>(); }
2226 
2227   template<int PermittedValue, int OtherPermittedValue>
2228   bool isMVEPairVectorIndex() const {
2229     if (Kind != k_VectorIndex) return false;
2230     return VectorIndex.Val == PermittedValue ||
2231            VectorIndex.Val == OtherPermittedValue;
2232   }
2233 
2234   bool isNEONi8splat() const {
2235     if (!isImm()) return false;
2236     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2237     // Must be a constant.
2238     if (!CE) return false;
2239     int64_t Value = CE->getValue();
2240     // i8 value splatted across 8 bytes. The immediate is just the 8 byte
2241     // value.
2242     return Value >= 0 && Value < 256;
2243   }
2244 
2245   bool isNEONi16splat() const {
2246     if (isNEONByteReplicate(2))
2247       return false; // Leave that for bytes replication and forbid by default.
2248     if (!isImm())
2249       return false;
2250     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2251     // Must be a constant.
2252     if (!CE) return false;
2253     unsigned Value = CE->getValue();
2254     return ARM_AM::isNEONi16splat(Value);
2255   }
2256 
2257   bool isNEONi16splatNot() const {
2258     if (!isImm())
2259       return false;
2260     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2261     // Must be a constant.
2262     if (!CE) return false;
2263     unsigned Value = CE->getValue();
2264     return ARM_AM::isNEONi16splat(~Value & 0xffff);
2265   }
2266 
2267   bool isNEONi32splat() const {
2268     if (isNEONByteReplicate(4))
2269       return false; // Leave that for bytes replication and forbid by default.
2270     if (!isImm())
2271       return false;
2272     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2273     // Must be a constant.
2274     if (!CE) return false;
2275     unsigned Value = CE->getValue();
2276     return ARM_AM::isNEONi32splat(Value);
2277   }
2278 
2279   bool isNEONi32splatNot() const {
2280     if (!isImm())
2281       return false;
2282     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2283     // Must be a constant.
2284     if (!CE) return false;
2285     unsigned Value = CE->getValue();
2286     return ARM_AM::isNEONi32splat(~Value);
2287   }
2288 
2289   static bool isValidNEONi32vmovImm(int64_t Value) {
2290     // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
2291     // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
2292     return ((Value & 0xffffffffffffff00) == 0) ||
2293            ((Value & 0xffffffffffff00ff) == 0) ||
2294            ((Value & 0xffffffffff00ffff) == 0) ||
2295            ((Value & 0xffffffff00ffffff) == 0) ||
2296            ((Value & 0xffffffffffff00ff) == 0xff) ||
2297            ((Value & 0xffffffffff00ffff) == 0xffff);
2298   }
2299 
2300   bool isNEONReplicate(unsigned Width, unsigned NumElems, bool Inv) const {
2301     assert((Width == 8 || Width == 16 || Width == 32) &&
2302            "Invalid element width");
2303     assert(NumElems * Width <= 64 && "Invalid result width");
2304 
2305     if (!isImm())
2306       return false;
2307     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2308     // Must be a constant.
2309     if (!CE)
2310       return false;
2311     int64_t Value = CE->getValue();
2312     if (!Value)
2313       return false; // Don't bother with zero.
2314     if (Inv)
2315       Value = ~Value;
2316 
2317     uint64_t Mask = (1ull << Width) - 1;
2318     uint64_t Elem = Value & Mask;
2319     if (Width == 16 && (Elem & 0x00ff) != 0 && (Elem & 0xff00) != 0)
2320       return false;
2321     if (Width == 32 && !isValidNEONi32vmovImm(Elem))
2322       return false;
2323 
2324     for (unsigned i = 1; i < NumElems; ++i) {
2325       Value >>= Width;
2326       if ((Value & Mask) != Elem)
2327         return false;
2328     }
2329     return true;
2330   }
2331 
2332   bool isNEONByteReplicate(unsigned NumBytes) const {
2333     return isNEONReplicate(8, NumBytes, false);
2334   }
2335 
2336   static void checkNeonReplicateArgs(unsigned FromW, unsigned ToW) {
2337     assert((FromW == 8 || FromW == 16 || FromW == 32) &&
2338            "Invalid source width");
2339     assert((ToW == 16 || ToW == 32 || ToW == 64) &&
2340            "Invalid destination width");
2341     assert(FromW < ToW && "ToW is not less than FromW");
2342   }
2343 
2344   template<unsigned FromW, unsigned ToW>
2345   bool isNEONmovReplicate() const {
2346     checkNeonReplicateArgs(FromW, ToW);
2347     if (ToW == 64 && isNEONi64splat())
2348       return false;
2349     return isNEONReplicate(FromW, ToW / FromW, false);
2350   }
2351 
2352   template<unsigned FromW, unsigned ToW>
2353   bool isNEONinvReplicate() const {
2354     checkNeonReplicateArgs(FromW, ToW);
2355     return isNEONReplicate(FromW, ToW / FromW, true);
2356   }
2357 
2358   bool isNEONi32vmov() const {
2359     if (isNEONByteReplicate(4))
2360       return false; // Let it to be classified as byte-replicate case.
2361     if (!isImm())
2362       return false;
2363     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2364     // Must be a constant.
2365     if (!CE)
2366       return false;
2367     return isValidNEONi32vmovImm(CE->getValue());
2368   }
2369 
2370   bool isNEONi32vmovNeg() const {
2371     if (!isImm()) return false;
2372     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2373     // Must be a constant.
2374     if (!CE) return false;
2375     return isValidNEONi32vmovImm(~CE->getValue());
2376   }
2377 
2378   bool isNEONi64splat() const {
2379     if (!isImm()) return false;
2380     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2381     // Must be a constant.
2382     if (!CE) return false;
2383     uint64_t Value = CE->getValue();
2384     // i64 value with each byte being either 0 or 0xff.
2385     for (unsigned i = 0; i < 8; ++i, Value >>= 8)
2386       if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
2387     return true;
2388   }
2389 
2390   template<int64_t Angle, int64_t Remainder>
2391   bool isComplexRotation() const {
2392     if (!isImm()) return false;
2393 
2394     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2395     if (!CE) return false;
2396     uint64_t Value = CE->getValue();
2397 
2398     return (Value % Angle == Remainder && Value <= 270);
2399   }
2400 
2401   bool isMVELongShift() const {
2402     if (!isImm()) return false;
2403     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2404     // Must be a constant.
2405     if (!CE) return false;
2406     uint64_t Value = CE->getValue();
2407     return Value >= 1 && Value <= 32;
2408   }
2409 
2410   bool isMveSaturateOp() const {
2411     if (!isImm()) return false;
2412     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2413     if (!CE) return false;
2414     uint64_t Value = CE->getValue();
2415     return Value == 48 || Value == 64;
2416   }
2417 
2418   bool isITCondCodeNoAL() const {
2419     if (!isITCondCode()) return false;
2420     ARMCC::CondCodes CC = getCondCode();
2421     return CC != ARMCC::AL;
2422   }
2423 
2424   bool isITCondCodeRestrictedI() const {
2425     if (!isITCondCode())
2426       return false;
2427     ARMCC::CondCodes CC = getCondCode();
2428     return CC == ARMCC::EQ || CC == ARMCC::NE;
2429   }
2430 
2431   bool isITCondCodeRestrictedS() const {
2432     if (!isITCondCode())
2433       return false;
2434     ARMCC::CondCodes CC = getCondCode();
2435     return CC == ARMCC::LT || CC == ARMCC::GT || CC == ARMCC::LE ||
2436            CC == ARMCC::GE;
2437   }
2438 
2439   bool isITCondCodeRestrictedU() const {
2440     if (!isITCondCode())
2441       return false;
2442     ARMCC::CondCodes CC = getCondCode();
2443     return CC == ARMCC::HS || CC == ARMCC::HI;
2444   }
2445 
2446   bool isITCondCodeRestrictedFP() const {
2447     if (!isITCondCode())
2448       return false;
2449     ARMCC::CondCodes CC = getCondCode();
2450     return CC == ARMCC::EQ || CC == ARMCC::NE || CC == ARMCC::LT ||
2451            CC == ARMCC::GT || CC == ARMCC::LE || CC == ARMCC::GE;
2452   }
2453 
2454   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
2455     // Add as immediates when possible.  Null MCExpr = 0.
2456     if (!Expr)
2457       Inst.addOperand(MCOperand::createImm(0));
2458     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
2459       Inst.addOperand(MCOperand::createImm(CE->getValue()));
2460     else
2461       Inst.addOperand(MCOperand::createExpr(Expr));
2462   }
2463 
2464   void addARMBranchTargetOperands(MCInst &Inst, unsigned N) const {
2465     assert(N == 1 && "Invalid number of operands!");
2466     addExpr(Inst, getImm());
2467   }
2468 
2469   void addThumbBranchTargetOperands(MCInst &Inst, unsigned N) const {
2470     assert(N == 1 && "Invalid number of operands!");
2471     addExpr(Inst, getImm());
2472   }
2473 
2474   void addCondCodeOperands(MCInst &Inst, unsigned N) const {
2475     assert(N == 2 && "Invalid number of operands!");
2476     Inst.addOperand(MCOperand::createImm(unsigned(getCondCode())));
2477     unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
2478     Inst.addOperand(MCOperand::createReg(RegNum));
2479   }
2480 
2481   void addVPTPredNOperands(MCInst &Inst, unsigned N) const {
2482     assert(N == 3 && "Invalid number of operands!");
2483     Inst.addOperand(MCOperand::createImm(unsigned(getVPTPred())));
2484     unsigned RegNum = getVPTPred() == ARMVCC::None ? 0: ARM::P0;
2485     Inst.addOperand(MCOperand::createReg(RegNum));
2486     Inst.addOperand(MCOperand::createReg(0));
2487   }
2488 
2489   void addVPTPredROperands(MCInst &Inst, unsigned N) const {
2490     assert(N == 4 && "Invalid number of operands!");
2491     addVPTPredNOperands(Inst, N-1);
2492     unsigned RegNum;
2493     if (getVPTPred() == ARMVCC::None) {
2494       RegNum = 0;
2495     } else {
2496       unsigned NextOpIndex = Inst.getNumOperands();
2497       const MCInstrDesc &MCID = ARMInsts[Inst.getOpcode()];
2498       int TiedOp = MCID.getOperandConstraint(NextOpIndex, MCOI::TIED_TO);
2499       assert(TiedOp >= 0 &&
2500              "Inactive register in vpred_r is not tied to an output!");
2501       RegNum = Inst.getOperand(TiedOp).getReg();
2502     }
2503     Inst.addOperand(MCOperand::createReg(RegNum));
2504   }
2505 
2506   void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
2507     assert(N == 1 && "Invalid number of operands!");
2508     Inst.addOperand(MCOperand::createImm(getCoproc()));
2509   }
2510 
2511   void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
2512     assert(N == 1 && "Invalid number of operands!");
2513     Inst.addOperand(MCOperand::createImm(getCoproc()));
2514   }
2515 
2516   void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
2517     assert(N == 1 && "Invalid number of operands!");
2518     Inst.addOperand(MCOperand::createImm(CoprocOption.Val));
2519   }
2520 
2521   void addITMaskOperands(MCInst &Inst, unsigned N) const {
2522     assert(N == 1 && "Invalid number of operands!");
2523     Inst.addOperand(MCOperand::createImm(ITMask.Mask));
2524   }
2525 
2526   void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
2527     assert(N == 1 && "Invalid number of operands!");
2528     Inst.addOperand(MCOperand::createImm(unsigned(getCondCode())));
2529   }
2530 
2531   void addITCondCodeInvOperands(MCInst &Inst, unsigned N) const {
2532     assert(N == 1 && "Invalid number of operands!");
2533     Inst.addOperand(MCOperand::createImm(unsigned(ARMCC::getOppositeCondition(getCondCode()))));
2534   }
2535 
2536   void addCCOutOperands(MCInst &Inst, unsigned N) const {
2537     assert(N == 1 && "Invalid number of operands!");
2538     Inst.addOperand(MCOperand::createReg(getReg()));
2539   }
2540 
2541   void addRegOperands(MCInst &Inst, unsigned N) const {
2542     assert(N == 1 && "Invalid number of operands!");
2543     Inst.addOperand(MCOperand::createReg(getReg()));
2544   }
2545 
2546   void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
2547     assert(N == 3 && "Invalid number of operands!");
2548     assert(isRegShiftedReg() &&
2549            "addRegShiftedRegOperands() on non-RegShiftedReg!");
2550     Inst.addOperand(MCOperand::createReg(RegShiftedReg.SrcReg));
2551     Inst.addOperand(MCOperand::createReg(RegShiftedReg.ShiftReg));
2552     Inst.addOperand(MCOperand::createImm(
2553       ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
2554   }
2555 
2556   void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
2557     assert(N == 2 && "Invalid number of operands!");
2558     assert(isRegShiftedImm() &&
2559            "addRegShiftedImmOperands() on non-RegShiftedImm!");
2560     Inst.addOperand(MCOperand::createReg(RegShiftedImm.SrcReg));
2561     // Shift of #32 is encoded as 0 where permitted
2562     unsigned Imm = (RegShiftedImm.ShiftImm == 32 ? 0 : RegShiftedImm.ShiftImm);
2563     Inst.addOperand(MCOperand::createImm(
2564       ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, Imm)));
2565   }
2566 
2567   void addShifterImmOperands(MCInst &Inst, unsigned N) const {
2568     assert(N == 1 && "Invalid number of operands!");
2569     Inst.addOperand(MCOperand::createImm((ShifterImm.isASR << 5) |
2570                                          ShifterImm.Imm));
2571   }
2572 
2573   void addRegListOperands(MCInst &Inst, unsigned N) const {
2574     assert(N == 1 && "Invalid number of operands!");
2575     const SmallVectorImpl<unsigned> &RegList = getRegList();
2576     for (SmallVectorImpl<unsigned>::const_iterator
2577            I = RegList.begin(), E = RegList.end(); I != E; ++I)
2578       Inst.addOperand(MCOperand::createReg(*I));
2579   }
2580 
2581   void addRegListWithAPSROperands(MCInst &Inst, unsigned N) const {
2582     assert(N == 1 && "Invalid number of operands!");
2583     const SmallVectorImpl<unsigned> &RegList = getRegList();
2584     for (SmallVectorImpl<unsigned>::const_iterator
2585            I = RegList.begin(), E = RegList.end(); I != E; ++I)
2586       Inst.addOperand(MCOperand::createReg(*I));
2587   }
2588 
2589   void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
2590     addRegListOperands(Inst, N);
2591   }
2592 
2593   void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
2594     addRegListOperands(Inst, N);
2595   }
2596 
2597   void addFPSRegListWithVPROperands(MCInst &Inst, unsigned N) const {
2598     addRegListOperands(Inst, N);
2599   }
2600 
2601   void addFPDRegListWithVPROperands(MCInst &Inst, unsigned N) const {
2602     addRegListOperands(Inst, N);
2603   }
2604 
2605   void addRotImmOperands(MCInst &Inst, unsigned N) const {
2606     assert(N == 1 && "Invalid number of operands!");
2607     // Encoded as val>>3. The printer handles display as 8, 16, 24.
2608     Inst.addOperand(MCOperand::createImm(RotImm.Imm >> 3));
2609   }
2610 
2611   void addModImmOperands(MCInst &Inst, unsigned N) const {
2612     assert(N == 1 && "Invalid number of operands!");
2613 
2614     // Support for fixups (MCFixup)
2615     if (isImm())
2616       return addImmOperands(Inst, N);
2617 
2618     Inst.addOperand(MCOperand::createImm(ModImm.Bits | (ModImm.Rot << 7)));
2619   }
2620 
2621   void addModImmNotOperands(MCInst &Inst, unsigned N) const {
2622     assert(N == 1 && "Invalid number of operands!");
2623     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2624     uint32_t Enc = ARM_AM::getSOImmVal(~CE->getValue());
2625     Inst.addOperand(MCOperand::createImm(Enc));
2626   }
2627 
2628   void addModImmNegOperands(MCInst &Inst, unsigned N) const {
2629     assert(N == 1 && "Invalid number of operands!");
2630     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2631     uint32_t Enc = ARM_AM::getSOImmVal(-CE->getValue());
2632     Inst.addOperand(MCOperand::createImm(Enc));
2633   }
2634 
2635   void addThumbModImmNeg8_255Operands(MCInst &Inst, unsigned N) const {
2636     assert(N == 1 && "Invalid number of operands!");
2637     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2638     uint32_t Val = -CE->getValue();
2639     Inst.addOperand(MCOperand::createImm(Val));
2640   }
2641 
2642   void addThumbModImmNeg1_7Operands(MCInst &Inst, unsigned N) const {
2643     assert(N == 1 && "Invalid number of operands!");
2644     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2645     uint32_t Val = -CE->getValue();
2646     Inst.addOperand(MCOperand::createImm(Val));
2647   }
2648 
2649   void addBitfieldOperands(MCInst &Inst, unsigned N) const {
2650     assert(N == 1 && "Invalid number of operands!");
2651     // Munge the lsb/width into a bitfield mask.
2652     unsigned lsb = Bitfield.LSB;
2653     unsigned width = Bitfield.Width;
2654     // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
2655     uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
2656                       (32 - (lsb + width)));
2657     Inst.addOperand(MCOperand::createImm(Mask));
2658   }
2659 
2660   void addImmOperands(MCInst &Inst, unsigned N) const {
2661     assert(N == 1 && "Invalid number of operands!");
2662     addExpr(Inst, getImm());
2663   }
2664 
2665   void addFBits16Operands(MCInst &Inst, unsigned N) const {
2666     assert(N == 1 && "Invalid number of operands!");
2667     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2668     Inst.addOperand(MCOperand::createImm(16 - CE->getValue()));
2669   }
2670 
2671   void addFBits32Operands(MCInst &Inst, unsigned N) const {
2672     assert(N == 1 && "Invalid number of operands!");
2673     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2674     Inst.addOperand(MCOperand::createImm(32 - CE->getValue()));
2675   }
2676 
2677   void addFPImmOperands(MCInst &Inst, unsigned N) const {
2678     assert(N == 1 && "Invalid number of operands!");
2679     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2680     int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
2681     Inst.addOperand(MCOperand::createImm(Val));
2682   }
2683 
2684   void addImm8s4Operands(MCInst &Inst, unsigned N) const {
2685     assert(N == 1 && "Invalid number of operands!");
2686     // FIXME: We really want to scale the value here, but the LDRD/STRD
2687     // instruction don't encode operands that way yet.
2688     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2689     Inst.addOperand(MCOperand::createImm(CE->getValue()));
2690   }
2691 
2692   void addImm7s4Operands(MCInst &Inst, unsigned N) const {
2693     assert(N == 1 && "Invalid number of operands!");
2694     // FIXME: We really want to scale the value here, but the VSTR/VLDR_VSYSR
2695     // instruction don't encode operands that way yet.
2696     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2697     Inst.addOperand(MCOperand::createImm(CE->getValue()));
2698   }
2699 
2700   void addImm7Shift0Operands(MCInst &Inst, unsigned N) const {
2701     assert(N == 1 && "Invalid number of operands!");
2702     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2703     Inst.addOperand(MCOperand::createImm(CE->getValue()));
2704   }
2705 
2706   void addImm7Shift1Operands(MCInst &Inst, unsigned N) const {
2707     assert(N == 1 && "Invalid number of operands!");
2708     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2709     Inst.addOperand(MCOperand::createImm(CE->getValue()));
2710   }
2711 
2712   void addImm7Shift2Operands(MCInst &Inst, unsigned N) const {
2713     assert(N == 1 && "Invalid number of operands!");
2714     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2715     Inst.addOperand(MCOperand::createImm(CE->getValue()));
2716   }
2717 
2718   void addImm7Operands(MCInst &Inst, unsigned N) const {
2719     assert(N == 1 && "Invalid number of operands!");
2720     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2721     Inst.addOperand(MCOperand::createImm(CE->getValue()));
2722   }
2723 
2724   void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
2725     assert(N == 1 && "Invalid number of operands!");
2726     // The immediate is scaled by four in the encoding and is stored
2727     // in the MCInst as such. Lop off the low two bits here.
2728     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2729     Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
2730   }
2731 
2732   void addImm0_508s4NegOperands(MCInst &Inst, unsigned N) const {
2733     assert(N == 1 && "Invalid number of operands!");
2734     // The immediate is scaled by four in the encoding and is stored
2735     // in the MCInst as such. Lop off the low two bits here.
2736     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2737     Inst.addOperand(MCOperand::createImm(-(CE->getValue() / 4)));
2738   }
2739 
2740   void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
2741     assert(N == 1 && "Invalid number of operands!");
2742     // The immediate is scaled by four in the encoding and is stored
2743     // in the MCInst as such. Lop off the low two bits here.
2744     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2745     Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
2746   }
2747 
2748   void addImm1_16Operands(MCInst &Inst, unsigned N) const {
2749     assert(N == 1 && "Invalid number of operands!");
2750     // The constant encodes as the immediate-1, and we store in the instruction
2751     // the bits as encoded, so subtract off one here.
2752     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2753     Inst.addOperand(MCOperand::createImm(CE->getValue() - 1));
2754   }
2755 
2756   void addImm1_32Operands(MCInst &Inst, unsigned N) const {
2757     assert(N == 1 && "Invalid number of operands!");
2758     // The constant encodes as the immediate-1, and we store in the instruction
2759     // the bits as encoded, so subtract off one here.
2760     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2761     Inst.addOperand(MCOperand::createImm(CE->getValue() - 1));
2762   }
2763 
2764   void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
2765     assert(N == 1 && "Invalid number of operands!");
2766     // The constant encodes as the immediate, except for 32, which encodes as
2767     // zero.
2768     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2769     unsigned Imm = CE->getValue();
2770     Inst.addOperand(MCOperand::createImm((Imm == 32 ? 0 : Imm)));
2771   }
2772 
2773   void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
2774     assert(N == 1 && "Invalid number of operands!");
2775     // An ASR value of 32 encodes as 0, so that's how we want to add it to
2776     // the instruction as well.
2777     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2778     int Val = CE->getValue();
2779     Inst.addOperand(MCOperand::createImm(Val == 32 ? 0 : Val));
2780   }
2781 
2782   void addT2SOImmNotOperands(MCInst &Inst, unsigned N) const {
2783     assert(N == 1 && "Invalid number of operands!");
2784     // The operand is actually a t2_so_imm, but we have its bitwise
2785     // negation in the assembly source, so twiddle it here.
2786     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2787     Inst.addOperand(MCOperand::createImm(~(uint32_t)CE->getValue()));
2788   }
2789 
2790   void addT2SOImmNegOperands(MCInst &Inst, unsigned N) const {
2791     assert(N == 1 && "Invalid number of operands!");
2792     // The operand is actually a t2_so_imm, but we have its
2793     // negation in the assembly source, so twiddle it here.
2794     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2795     Inst.addOperand(MCOperand::createImm(-(uint32_t)CE->getValue()));
2796   }
2797 
2798   void addImm0_4095NegOperands(MCInst &Inst, unsigned N) const {
2799     assert(N == 1 && "Invalid number of operands!");
2800     // The operand is actually an imm0_4095, but we have its
2801     // negation in the assembly source, so twiddle it here.
2802     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2803     Inst.addOperand(MCOperand::createImm(-(uint32_t)CE->getValue()));
2804   }
2805 
2806   void addUnsignedOffset_b8s2Operands(MCInst &Inst, unsigned N) const {
2807     if(const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
2808       Inst.addOperand(MCOperand::createImm(CE->getValue() >> 2));
2809       return;
2810     }
2811     const MCSymbolRefExpr *SR = cast<MCSymbolRefExpr>(Imm.Val);
2812     Inst.addOperand(MCOperand::createExpr(SR));
2813   }
2814 
2815   void addThumbMemPCOperands(MCInst &Inst, unsigned N) const {
2816     assert(N == 1 && "Invalid number of operands!");
2817     if (isImm()) {
2818       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2819       if (CE) {
2820         Inst.addOperand(MCOperand::createImm(CE->getValue()));
2821         return;
2822       }
2823       const MCSymbolRefExpr *SR = cast<MCSymbolRefExpr>(Imm.Val);
2824       Inst.addOperand(MCOperand::createExpr(SR));
2825       return;
2826     }
2827 
2828     assert(isGPRMem()  && "Unknown value type!");
2829     assert(isa<MCConstantExpr>(Memory.OffsetImm) && "Unknown value type!");
2830     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm))
2831       Inst.addOperand(MCOperand::createImm(CE->getValue()));
2832     else
2833       Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
2834   }
2835 
2836   void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
2837     assert(N == 1 && "Invalid number of operands!");
2838     Inst.addOperand(MCOperand::createImm(unsigned(getMemBarrierOpt())));
2839   }
2840 
2841   void addInstSyncBarrierOptOperands(MCInst &Inst, unsigned N) const {
2842     assert(N == 1 && "Invalid number of operands!");
2843     Inst.addOperand(MCOperand::createImm(unsigned(getInstSyncBarrierOpt())));
2844   }
2845 
2846   void addTraceSyncBarrierOptOperands(MCInst &Inst, unsigned N) const {
2847     assert(N == 1 && "Invalid number of operands!");
2848     Inst.addOperand(MCOperand::createImm(unsigned(getTraceSyncBarrierOpt())));
2849   }
2850 
2851   void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
2852     assert(N == 1 && "Invalid number of operands!");
2853     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2854   }
2855 
2856   void addMemNoOffsetT2Operands(MCInst &Inst, unsigned N) const {
2857     assert(N == 1 && "Invalid number of operands!");
2858     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2859   }
2860 
2861   void addMemNoOffsetT2NoSpOperands(MCInst &Inst, unsigned N) const {
2862     assert(N == 1 && "Invalid number of operands!");
2863     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2864   }
2865 
2866   void addMemNoOffsetTOperands(MCInst &Inst, unsigned N) const {
2867     assert(N == 1 && "Invalid number of operands!");
2868     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2869   }
2870 
2871   void addMemPCRelImm12Operands(MCInst &Inst, unsigned N) const {
2872     assert(N == 1 && "Invalid number of operands!");
2873     if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm))
2874       Inst.addOperand(MCOperand::createImm(CE->getValue()));
2875     else
2876       Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
2877   }
2878 
2879   void addAdrLabelOperands(MCInst &Inst, unsigned N) const {
2880     assert(N == 1 && "Invalid number of operands!");
2881     assert(isImm() && "Not an immediate!");
2882 
2883     // If we have an immediate that's not a constant, treat it as a label
2884     // reference needing a fixup.
2885     if (!isa<MCConstantExpr>(getImm())) {
2886       Inst.addOperand(MCOperand::createExpr(getImm()));
2887       return;
2888     }
2889 
2890     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2891     int Val = CE->getValue();
2892     Inst.addOperand(MCOperand::createImm(Val));
2893   }
2894 
2895   void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
2896     assert(N == 2 && "Invalid number of operands!");
2897     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2898     Inst.addOperand(MCOperand::createImm(Memory.Alignment));
2899   }
2900 
2901   void addDupAlignedMemoryNoneOperands(MCInst &Inst, unsigned N) const {
2902     addAlignedMemoryOperands(Inst, N);
2903   }
2904 
2905   void addAlignedMemoryNoneOperands(MCInst &Inst, unsigned N) const {
2906     addAlignedMemoryOperands(Inst, N);
2907   }
2908 
2909   void addAlignedMemory16Operands(MCInst &Inst, unsigned N) const {
2910     addAlignedMemoryOperands(Inst, N);
2911   }
2912 
2913   void addDupAlignedMemory16Operands(MCInst &Inst, unsigned N) const {
2914     addAlignedMemoryOperands(Inst, N);
2915   }
2916 
2917   void addAlignedMemory32Operands(MCInst &Inst, unsigned N) const {
2918     addAlignedMemoryOperands(Inst, N);
2919   }
2920 
2921   void addDupAlignedMemory32Operands(MCInst &Inst, unsigned N) const {
2922     addAlignedMemoryOperands(Inst, N);
2923   }
2924 
2925   void addAlignedMemory64Operands(MCInst &Inst, unsigned N) const {
2926     addAlignedMemoryOperands(Inst, N);
2927   }
2928 
2929   void addDupAlignedMemory64Operands(MCInst &Inst, unsigned N) const {
2930     addAlignedMemoryOperands(Inst, N);
2931   }
2932 
2933   void addAlignedMemory64or128Operands(MCInst &Inst, unsigned N) const {
2934     addAlignedMemoryOperands(Inst, N);
2935   }
2936 
2937   void addDupAlignedMemory64or128Operands(MCInst &Inst, unsigned N) const {
2938     addAlignedMemoryOperands(Inst, N);
2939   }
2940 
2941   void addAlignedMemory64or128or256Operands(MCInst &Inst, unsigned N) const {
2942     addAlignedMemoryOperands(Inst, N);
2943   }
2944 
2945   void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
2946     assert(N == 3 && "Invalid number of operands!");
2947     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2948     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2949     if (!Memory.OffsetRegNum) {
2950       if (!Memory.OffsetImm)
2951         Inst.addOperand(MCOperand::createImm(0));
2952       else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
2953         int32_t Val = CE->getValue();
2954         ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2955         // Special case for #-0
2956         if (Val == std::numeric_limits<int32_t>::min())
2957           Val = 0;
2958         if (Val < 0)
2959           Val = -Val;
2960         Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
2961         Inst.addOperand(MCOperand::createImm(Val));
2962       } else
2963         Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
2964     } else {
2965       // For register offset, we encode the shift type and negation flag
2966       // here.
2967       int32_t Val =
2968           ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
2969                             Memory.ShiftImm, Memory.ShiftType);
2970       Inst.addOperand(MCOperand::createImm(Val));
2971     }
2972   }
2973 
2974   void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
2975     assert(N == 2 && "Invalid number of operands!");
2976     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2977     assert(CE && "non-constant AM2OffsetImm operand!");
2978     int32_t Val = CE->getValue();
2979     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2980     // Special case for #-0
2981     if (Val == std::numeric_limits<int32_t>::min()) Val = 0;
2982     if (Val < 0) Val = -Val;
2983     Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
2984     Inst.addOperand(MCOperand::createReg(0));
2985     Inst.addOperand(MCOperand::createImm(Val));
2986   }
2987 
2988   void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
2989     assert(N == 3 && "Invalid number of operands!");
2990     // If we have an immediate that's not a constant, treat it as a label
2991     // reference needing a fixup. If it is a constant, it's something else
2992     // and we reject it.
2993     if (isImm()) {
2994       Inst.addOperand(MCOperand::createExpr(getImm()));
2995       Inst.addOperand(MCOperand::createReg(0));
2996       Inst.addOperand(MCOperand::createImm(0));
2997       return;
2998     }
2999 
3000     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3001     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
3002     if (!Memory.OffsetRegNum) {
3003       if (!Memory.OffsetImm)
3004         Inst.addOperand(MCOperand::createImm(0));
3005       else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
3006         int32_t Val = CE->getValue();
3007         ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
3008         // Special case for #-0
3009         if (Val == std::numeric_limits<int32_t>::min())
3010           Val = 0;
3011         if (Val < 0)
3012           Val = -Val;
3013         Val = ARM_AM::getAM3Opc(AddSub, Val);
3014         Inst.addOperand(MCOperand::createImm(Val));
3015       } else
3016         Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
3017     } else {
3018       // For register offset, we encode the shift type and negation flag
3019       // here.
3020       int32_t Val =
3021           ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
3022       Inst.addOperand(MCOperand::createImm(Val));
3023     }
3024   }
3025 
3026   void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
3027     assert(N == 2 && "Invalid number of operands!");
3028     if (Kind == k_PostIndexRegister) {
3029       int32_t Val =
3030         ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
3031       Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
3032       Inst.addOperand(MCOperand::createImm(Val));
3033       return;
3034     }
3035 
3036     // Constant offset.
3037     const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
3038     int32_t Val = CE->getValue();
3039     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
3040     // Special case for #-0
3041     if (Val == std::numeric_limits<int32_t>::min()) Val = 0;
3042     if (Val < 0) Val = -Val;
3043     Val = ARM_AM::getAM3Opc(AddSub, Val);
3044     Inst.addOperand(MCOperand::createReg(0));
3045     Inst.addOperand(MCOperand::createImm(Val));
3046   }
3047 
3048   void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
3049     assert(N == 2 && "Invalid number of operands!");
3050     // If we have an immediate that's not a constant, treat it as a label
3051     // reference needing a fixup. If it is a constant, it's something else
3052     // and we reject it.
3053     if (isImm()) {
3054       Inst.addOperand(MCOperand::createExpr(getImm()));
3055       Inst.addOperand(MCOperand::createImm(0));
3056       return;
3057     }
3058 
3059     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3060     if (!Memory.OffsetImm)
3061       Inst.addOperand(MCOperand::createImm(0));
3062     else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
3063       // The lower two bits are always zero and as such are not encoded.
3064       int32_t Val = CE->getValue() / 4;
3065       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
3066       // Special case for #-0
3067       if (Val == std::numeric_limits<int32_t>::min())
3068         Val = 0;
3069       if (Val < 0)
3070         Val = -Val;
3071       Val = ARM_AM::getAM5Opc(AddSub, Val);
3072       Inst.addOperand(MCOperand::createImm(Val));
3073     } else
3074       Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
3075   }
3076 
3077   void addAddrMode5FP16Operands(MCInst &Inst, unsigned N) const {
3078     assert(N == 2 && "Invalid number of operands!");
3079     // If we have an immediate that's not a constant, treat it as a label
3080     // reference needing a fixup. If it is a constant, it's something else
3081     // and we reject it.
3082     if (isImm()) {
3083       Inst.addOperand(MCOperand::createExpr(getImm()));
3084       Inst.addOperand(MCOperand::createImm(0));
3085       return;
3086     }
3087 
3088     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3089     // The lower bit is always zero and as such is not encoded.
3090     if (!Memory.OffsetImm)
3091       Inst.addOperand(MCOperand::createImm(0));
3092     else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
3093       int32_t Val = CE->getValue() / 2;
3094       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
3095       // Special case for #-0
3096       if (Val == std::numeric_limits<int32_t>::min())
3097         Val = 0;
3098       if (Val < 0)
3099         Val = -Val;
3100       Val = ARM_AM::getAM5FP16Opc(AddSub, Val);
3101       Inst.addOperand(MCOperand::createImm(Val));
3102     } else
3103       Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
3104   }
3105 
3106   void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
3107     assert(N == 2 && "Invalid number of operands!");
3108     // If we have an immediate that's not a constant, treat it as a label
3109     // reference needing a fixup. If it is a constant, it's something else
3110     // and we reject it.
3111     if (isImm()) {
3112       Inst.addOperand(MCOperand::createExpr(getImm()));
3113       Inst.addOperand(MCOperand::createImm(0));
3114       return;
3115     }
3116 
3117     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3118     addExpr(Inst, Memory.OffsetImm);
3119   }
3120 
3121   void addMemImm7s4OffsetOperands(MCInst &Inst, unsigned N) const {
3122     assert(N == 2 && "Invalid number of operands!");
3123     // If we have an immediate that's not a constant, treat it as a label
3124     // reference needing a fixup. If it is a constant, it's something else
3125     // and we reject it.
3126     if (isImm()) {
3127       Inst.addOperand(MCOperand::createExpr(getImm()));
3128       Inst.addOperand(MCOperand::createImm(0));
3129       return;
3130     }
3131 
3132     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3133     addExpr(Inst, Memory.OffsetImm);
3134   }
3135 
3136   void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
3137     assert(N == 2 && "Invalid number of operands!");
3138     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3139     if (!Memory.OffsetImm)
3140       Inst.addOperand(MCOperand::createImm(0));
3141     else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm))
3142       // The lower two bits are always zero and as such are not encoded.
3143       Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
3144     else
3145       Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
3146   }
3147 
3148   void addMemImmOffsetOperands(MCInst &Inst, unsigned N) const {
3149     assert(N == 2 && "Invalid number of operands!");
3150     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3151     addExpr(Inst, Memory.OffsetImm);
3152   }
3153 
3154   void addMemRegRQOffsetOperands(MCInst &Inst, unsigned N) const {
3155     assert(N == 2 && "Invalid number of operands!");
3156     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3157     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
3158   }
3159 
3160   void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
3161     assert(N == 2 && "Invalid number of operands!");
3162     // If this is an immediate, it's a label reference.
3163     if (isImm()) {
3164       addExpr(Inst, getImm());
3165       Inst.addOperand(MCOperand::createImm(0));
3166       return;
3167     }
3168 
3169     // Otherwise, it's a normal memory reg+offset.
3170     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3171     addExpr(Inst, Memory.OffsetImm);
3172   }
3173 
3174   void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
3175     assert(N == 2 && "Invalid number of operands!");
3176     // If this is an immediate, it's a label reference.
3177     if (isImm()) {
3178       addExpr(Inst, getImm());
3179       Inst.addOperand(MCOperand::createImm(0));
3180       return;
3181     }
3182 
3183     // Otherwise, it's a normal memory reg+offset.
3184     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3185     addExpr(Inst, Memory.OffsetImm);
3186   }
3187 
3188   void addConstPoolAsmImmOperands(MCInst &Inst, unsigned N) const {
3189     assert(N == 1 && "Invalid number of operands!");
3190     // This is container for the immediate that we will create the constant
3191     // pool from
3192     addExpr(Inst, getConstantPoolImm());
3193   }
3194 
3195   void addMemTBBOperands(MCInst &Inst, unsigned N) const {
3196     assert(N == 2 && "Invalid number of operands!");
3197     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3198     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
3199   }
3200 
3201   void addMemTBHOperands(MCInst &Inst, unsigned N) const {
3202     assert(N == 2 && "Invalid number of operands!");
3203     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3204     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
3205   }
3206 
3207   void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
3208     assert(N == 3 && "Invalid number of operands!");
3209     unsigned Val =
3210       ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
3211                         Memory.ShiftImm, Memory.ShiftType);
3212     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3213     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
3214     Inst.addOperand(MCOperand::createImm(Val));
3215   }
3216 
3217   void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
3218     assert(N == 3 && "Invalid number of operands!");
3219     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3220     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
3221     Inst.addOperand(MCOperand::createImm(Memory.ShiftImm));
3222   }
3223 
3224   void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
3225     assert(N == 2 && "Invalid number of operands!");
3226     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3227     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
3228   }
3229 
3230   void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
3231     assert(N == 2 && "Invalid number of operands!");
3232     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3233     if (!Memory.OffsetImm)
3234       Inst.addOperand(MCOperand::createImm(0));
3235     else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm))
3236       // The lower two bits are always zero and as such are not encoded.
3237       Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
3238     else
3239       Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
3240   }
3241 
3242   void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
3243     assert(N == 2 && "Invalid number of operands!");
3244     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3245     if (!Memory.OffsetImm)
3246       Inst.addOperand(MCOperand::createImm(0));
3247     else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm))
3248       Inst.addOperand(MCOperand::createImm(CE->getValue() / 2));
3249     else
3250       Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
3251   }
3252 
3253   void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
3254     assert(N == 2 && "Invalid number of operands!");
3255     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3256     addExpr(Inst, Memory.OffsetImm);
3257   }
3258 
3259   void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
3260     assert(N == 2 && "Invalid number of operands!");
3261     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3262     if (!Memory.OffsetImm)
3263       Inst.addOperand(MCOperand::createImm(0));
3264     else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm))
3265       // The lower two bits are always zero and as such are not encoded.
3266       Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
3267     else
3268       Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
3269   }
3270 
3271   void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
3272     assert(N == 1 && "Invalid number of operands!");
3273     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
3274     assert(CE && "non-constant post-idx-imm8 operand!");
3275     int Imm = CE->getValue();
3276     bool isAdd = Imm >= 0;
3277     if (Imm == std::numeric_limits<int32_t>::min()) Imm = 0;
3278     Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
3279     Inst.addOperand(MCOperand::createImm(Imm));
3280   }
3281 
3282   void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
3283     assert(N == 1 && "Invalid number of operands!");
3284     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
3285     assert(CE && "non-constant post-idx-imm8s4 operand!");
3286     int Imm = CE->getValue();
3287     bool isAdd = Imm >= 0;
3288     if (Imm == std::numeric_limits<int32_t>::min()) Imm = 0;
3289     // Immediate is scaled by 4.
3290     Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
3291     Inst.addOperand(MCOperand::createImm(Imm));
3292   }
3293 
3294   void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
3295     assert(N == 2 && "Invalid number of operands!");
3296     Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
3297     Inst.addOperand(MCOperand::createImm(PostIdxReg.isAdd));
3298   }
3299 
3300   void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
3301     assert(N == 2 && "Invalid number of operands!");
3302     Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
3303     // The sign, shift type, and shift amount are encoded in a single operand
3304     // using the AM2 encoding helpers.
3305     ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
3306     unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
3307                                      PostIdxReg.ShiftTy);
3308     Inst.addOperand(MCOperand::createImm(Imm));
3309   }
3310 
3311   void addPowerTwoOperands(MCInst &Inst, unsigned N) const {
3312     assert(N == 1 && "Invalid number of operands!");
3313     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3314     Inst.addOperand(MCOperand::createImm(CE->getValue()));
3315   }
3316 
3317   void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
3318     assert(N == 1 && "Invalid number of operands!");
3319     Inst.addOperand(MCOperand::createImm(unsigned(getMSRMask())));
3320   }
3321 
3322   void addBankedRegOperands(MCInst &Inst, unsigned N) const {
3323     assert(N == 1 && "Invalid number of operands!");
3324     Inst.addOperand(MCOperand::createImm(unsigned(getBankedReg())));
3325   }
3326 
3327   void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
3328     assert(N == 1 && "Invalid number of operands!");
3329     Inst.addOperand(MCOperand::createImm(unsigned(getProcIFlags())));
3330   }
3331 
3332   void addVecListOperands(MCInst &Inst, unsigned N) const {
3333     assert(N == 1 && "Invalid number of operands!");
3334     Inst.addOperand(MCOperand::createReg(VectorList.RegNum));
3335   }
3336 
3337   void addMVEVecListOperands(MCInst &Inst, unsigned N) const {
3338     assert(N == 1 && "Invalid number of operands!");
3339 
3340     // When we come here, the VectorList field will identify a range
3341     // of q-registers by its base register and length, and it will
3342     // have already been error-checked to be the expected length of
3343     // range and contain only q-regs in the range q0-q7. So we can
3344     // count on the base register being in the range q0-q6 (for 2
3345     // regs) or q0-q4 (for 4)
3346     //
3347     // The MVE instructions taking a register range of this kind will
3348     // need an operand in the MQQPR or MQQQQPR class, representing the
3349     // entire range as a unit. So we must translate into that class,
3350     // by finding the index of the base register in the MQPR reg
3351     // class, and returning the super-register at the corresponding
3352     // index in the target class.
3353 
3354     const MCRegisterClass *RC_in = &ARMMCRegisterClasses[ARM::MQPRRegClassID];
3355     const MCRegisterClass *RC_out =
3356         (VectorList.Count == 2) ? &ARMMCRegisterClasses[ARM::MQQPRRegClassID]
3357                                 : &ARMMCRegisterClasses[ARM::MQQQQPRRegClassID];
3358 
3359     unsigned I, E = RC_out->getNumRegs();
3360     for (I = 0; I < E; I++)
3361       if (RC_in->getRegister(I) == VectorList.RegNum)
3362         break;
3363     assert(I < E && "Invalid vector list start register!");
3364 
3365     Inst.addOperand(MCOperand::createReg(RC_out->getRegister(I)));
3366   }
3367 
3368   void addVecListIndexedOperands(MCInst &Inst, unsigned N) const {
3369     assert(N == 2 && "Invalid number of operands!");
3370     Inst.addOperand(MCOperand::createReg(VectorList.RegNum));
3371     Inst.addOperand(MCOperand::createImm(VectorList.LaneIndex));
3372   }
3373 
3374   void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
3375     assert(N == 1 && "Invalid number of operands!");
3376     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
3377   }
3378 
3379   void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
3380     assert(N == 1 && "Invalid number of operands!");
3381     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
3382   }
3383 
3384   void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
3385     assert(N == 1 && "Invalid number of operands!");
3386     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
3387   }
3388 
3389   void addVectorIndex64Operands(MCInst &Inst, unsigned N) const {
3390     assert(N == 1 && "Invalid number of operands!");
3391     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
3392   }
3393 
3394   void addMVEVectorIndexOperands(MCInst &Inst, unsigned N) const {
3395     assert(N == 1 && "Invalid number of operands!");
3396     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
3397   }
3398 
3399   void addMVEPairVectorIndexOperands(MCInst &Inst, unsigned N) const {
3400     assert(N == 1 && "Invalid number of operands!");
3401     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
3402   }
3403 
3404   void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
3405     assert(N == 1 && "Invalid number of operands!");
3406     // The immediate encodes the type of constant as well as the value.
3407     // Mask in that this is an i8 splat.
3408     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3409     Inst.addOperand(MCOperand::createImm(CE->getValue() | 0xe00));
3410   }
3411 
3412   void addNEONi16splatOperands(MCInst &Inst, unsigned N) const {
3413     assert(N == 1 && "Invalid number of operands!");
3414     // The immediate encodes the type of constant as well as the value.
3415     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3416     unsigned Value = CE->getValue();
3417     Value = ARM_AM::encodeNEONi16splat(Value);
3418     Inst.addOperand(MCOperand::createImm(Value));
3419   }
3420 
3421   void addNEONi16splatNotOperands(MCInst &Inst, unsigned N) const {
3422     assert(N == 1 && "Invalid number of operands!");
3423     // The immediate encodes the type of constant as well as the value.
3424     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3425     unsigned Value = CE->getValue();
3426     Value = ARM_AM::encodeNEONi16splat(~Value & 0xffff);
3427     Inst.addOperand(MCOperand::createImm(Value));
3428   }
3429 
3430   void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
3431     assert(N == 1 && "Invalid number of operands!");
3432     // The immediate encodes the type of constant as well as the value.
3433     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3434     unsigned Value = CE->getValue();
3435     Value = ARM_AM::encodeNEONi32splat(Value);
3436     Inst.addOperand(MCOperand::createImm(Value));
3437   }
3438 
3439   void addNEONi32splatNotOperands(MCInst &Inst, unsigned N) const {
3440     assert(N == 1 && "Invalid number of operands!");
3441     // The immediate encodes the type of constant as well as the value.
3442     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3443     unsigned Value = CE->getValue();
3444     Value = ARM_AM::encodeNEONi32splat(~Value);
3445     Inst.addOperand(MCOperand::createImm(Value));
3446   }
3447 
3448   void addNEONi8ReplicateOperands(MCInst &Inst, bool Inv) const {
3449     // The immediate encodes the type of constant as well as the value.
3450     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3451     assert((Inst.getOpcode() == ARM::VMOVv8i8 ||
3452             Inst.getOpcode() == ARM::VMOVv16i8) &&
3453           "All instructions that wants to replicate non-zero byte "
3454           "always must be replaced with VMOVv8i8 or VMOVv16i8.");
3455     unsigned Value = CE->getValue();
3456     if (Inv)
3457       Value = ~Value;
3458     unsigned B = Value & 0xff;
3459     B |= 0xe00; // cmode = 0b1110
3460     Inst.addOperand(MCOperand::createImm(B));
3461   }
3462 
3463   void addNEONinvi8ReplicateOperands(MCInst &Inst, unsigned N) const {
3464     assert(N == 1 && "Invalid number of operands!");
3465     addNEONi8ReplicateOperands(Inst, true);
3466   }
3467 
3468   static unsigned encodeNeonVMOVImmediate(unsigned Value) {
3469     if (Value >= 256 && Value <= 0xffff)
3470       Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
3471     else if (Value > 0xffff && Value <= 0xffffff)
3472       Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
3473     else if (Value > 0xffffff)
3474       Value = (Value >> 24) | 0x600;
3475     return Value;
3476   }
3477 
3478   void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
3479     assert(N == 1 && "Invalid number of operands!");
3480     // The immediate encodes the type of constant as well as the value.
3481     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3482     unsigned Value = encodeNeonVMOVImmediate(CE->getValue());
3483     Inst.addOperand(MCOperand::createImm(Value));
3484   }
3485 
3486   void addNEONvmovi8ReplicateOperands(MCInst &Inst, unsigned N) const {
3487     assert(N == 1 && "Invalid number of operands!");
3488     addNEONi8ReplicateOperands(Inst, false);
3489   }
3490 
3491   void addNEONvmovi16ReplicateOperands(MCInst &Inst, unsigned N) const {
3492     assert(N == 1 && "Invalid number of operands!");
3493     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3494     assert((Inst.getOpcode() == ARM::VMOVv4i16 ||
3495             Inst.getOpcode() == ARM::VMOVv8i16 ||
3496             Inst.getOpcode() == ARM::VMVNv4i16 ||
3497             Inst.getOpcode() == ARM::VMVNv8i16) &&
3498           "All instructions that want to replicate non-zero half-word "
3499           "always must be replaced with V{MOV,MVN}v{4,8}i16.");
3500     uint64_t Value = CE->getValue();
3501     unsigned Elem = Value & 0xffff;
3502     if (Elem >= 256)
3503       Elem = (Elem >> 8) | 0x200;
3504     Inst.addOperand(MCOperand::createImm(Elem));
3505   }
3506 
3507   void addNEONi32vmovNegOperands(MCInst &Inst, unsigned N) const {
3508     assert(N == 1 && "Invalid number of operands!");
3509     // The immediate encodes the type of constant as well as the value.
3510     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3511     unsigned Value = encodeNeonVMOVImmediate(~CE->getValue());
3512     Inst.addOperand(MCOperand::createImm(Value));
3513   }
3514 
3515   void addNEONvmovi32ReplicateOperands(MCInst &Inst, unsigned N) const {
3516     assert(N == 1 && "Invalid number of operands!");
3517     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3518     assert((Inst.getOpcode() == ARM::VMOVv2i32 ||
3519             Inst.getOpcode() == ARM::VMOVv4i32 ||
3520             Inst.getOpcode() == ARM::VMVNv2i32 ||
3521             Inst.getOpcode() == ARM::VMVNv4i32) &&
3522           "All instructions that want to replicate non-zero word "
3523           "always must be replaced with V{MOV,MVN}v{2,4}i32.");
3524     uint64_t Value = CE->getValue();
3525     unsigned Elem = encodeNeonVMOVImmediate(Value & 0xffffffff);
3526     Inst.addOperand(MCOperand::createImm(Elem));
3527   }
3528 
3529   void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
3530     assert(N == 1 && "Invalid number of operands!");
3531     // The immediate encodes the type of constant as well as the value.
3532     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3533     uint64_t Value = CE->getValue();
3534     unsigned Imm = 0;
3535     for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
3536       Imm |= (Value & 1) << i;
3537     }
3538     Inst.addOperand(MCOperand::createImm(Imm | 0x1e00));
3539   }
3540 
3541   void addComplexRotationEvenOperands(MCInst &Inst, unsigned N) const {
3542     assert(N == 1 && "Invalid number of operands!");
3543     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3544     Inst.addOperand(MCOperand::createImm(CE->getValue() / 90));
3545   }
3546 
3547   void addComplexRotationOddOperands(MCInst &Inst, unsigned N) const {
3548     assert(N == 1 && "Invalid number of operands!");
3549     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3550     Inst.addOperand(MCOperand::createImm((CE->getValue() - 90) / 180));
3551   }
3552 
3553   void addMveSaturateOperands(MCInst &Inst, unsigned N) const {
3554     assert(N == 1 && "Invalid number of operands!");
3555     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3556     unsigned Imm = CE->getValue();
3557     assert((Imm == 48 || Imm == 64) && "Invalid saturate operand");
3558     Inst.addOperand(MCOperand::createImm(Imm == 48 ? 1 : 0));
3559   }
3560 
3561   void print(raw_ostream &OS) const override;
3562 
3563   static std::unique_ptr<ARMOperand> CreateITMask(unsigned Mask, SMLoc S) {
3564     auto Op = std::make_unique<ARMOperand>(k_ITCondMask);
3565     Op->ITMask.Mask = Mask;
3566     Op->StartLoc = S;
3567     Op->EndLoc = S;
3568     return Op;
3569   }
3570 
3571   static std::unique_ptr<ARMOperand> CreateCondCode(ARMCC::CondCodes CC,
3572                                                     SMLoc S) {
3573     auto Op = std::make_unique<ARMOperand>(k_CondCode);
3574     Op->CC.Val = CC;
3575     Op->StartLoc = S;
3576     Op->EndLoc = S;
3577     return Op;
3578   }
3579 
3580   static std::unique_ptr<ARMOperand> CreateVPTPred(ARMVCC::VPTCodes CC,
3581                                                    SMLoc S) {
3582     auto Op = std::make_unique<ARMOperand>(k_VPTPred);
3583     Op->VCC.Val = CC;
3584     Op->StartLoc = S;
3585     Op->EndLoc = S;
3586     return Op;
3587   }
3588 
3589   static std::unique_ptr<ARMOperand> CreateCoprocNum(unsigned CopVal, SMLoc S) {
3590     auto Op = std::make_unique<ARMOperand>(k_CoprocNum);
3591     Op->Cop.Val = CopVal;
3592     Op->StartLoc = S;
3593     Op->EndLoc = S;
3594     return Op;
3595   }
3596 
3597   static std::unique_ptr<ARMOperand> CreateCoprocReg(unsigned CopVal, SMLoc S) {
3598     auto Op = std::make_unique<ARMOperand>(k_CoprocReg);
3599     Op->Cop.Val = CopVal;
3600     Op->StartLoc = S;
3601     Op->EndLoc = S;
3602     return Op;
3603   }
3604 
3605   static std::unique_ptr<ARMOperand> CreateCoprocOption(unsigned Val, SMLoc S,
3606                                                         SMLoc E) {
3607     auto Op = std::make_unique<ARMOperand>(k_CoprocOption);
3608     Op->Cop.Val = Val;
3609     Op->StartLoc = S;
3610     Op->EndLoc = E;
3611     return Op;
3612   }
3613 
3614   static std::unique_ptr<ARMOperand> CreateCCOut(unsigned RegNum, SMLoc S) {
3615     auto Op = std::make_unique<ARMOperand>(k_CCOut);
3616     Op->Reg.RegNum = RegNum;
3617     Op->StartLoc = S;
3618     Op->EndLoc = S;
3619     return Op;
3620   }
3621 
3622   static std::unique_ptr<ARMOperand> CreateToken(StringRef Str, SMLoc S) {
3623     auto Op = std::make_unique<ARMOperand>(k_Token);
3624     Op->Tok.Data = Str.data();
3625     Op->Tok.Length = Str.size();
3626     Op->StartLoc = S;
3627     Op->EndLoc = S;
3628     return Op;
3629   }
3630 
3631   static std::unique_ptr<ARMOperand> CreateReg(unsigned RegNum, SMLoc S,
3632                                                SMLoc E) {
3633     auto Op = std::make_unique<ARMOperand>(k_Register);
3634     Op->Reg.RegNum = RegNum;
3635     Op->StartLoc = S;
3636     Op->EndLoc = E;
3637     return Op;
3638   }
3639 
3640   static std::unique_ptr<ARMOperand>
3641   CreateShiftedRegister(ARM_AM::ShiftOpc ShTy, unsigned SrcReg,
3642                         unsigned ShiftReg, unsigned ShiftImm, SMLoc S,
3643                         SMLoc E) {
3644     auto Op = std::make_unique<ARMOperand>(k_ShiftedRegister);
3645     Op->RegShiftedReg.ShiftTy = ShTy;
3646     Op->RegShiftedReg.SrcReg = SrcReg;
3647     Op->RegShiftedReg.ShiftReg = ShiftReg;
3648     Op->RegShiftedReg.ShiftImm = ShiftImm;
3649     Op->StartLoc = S;
3650     Op->EndLoc = E;
3651     return Op;
3652   }
3653 
3654   static std::unique_ptr<ARMOperand>
3655   CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy, unsigned SrcReg,
3656                          unsigned ShiftImm, SMLoc S, SMLoc E) {
3657     auto Op = std::make_unique<ARMOperand>(k_ShiftedImmediate);
3658     Op->RegShiftedImm.ShiftTy = ShTy;
3659     Op->RegShiftedImm.SrcReg = SrcReg;
3660     Op->RegShiftedImm.ShiftImm = ShiftImm;
3661     Op->StartLoc = S;
3662     Op->EndLoc = E;
3663     return Op;
3664   }
3665 
3666   static std::unique_ptr<ARMOperand> CreateShifterImm(bool isASR, unsigned Imm,
3667                                                       SMLoc S, SMLoc E) {
3668     auto Op = std::make_unique<ARMOperand>(k_ShifterImmediate);
3669     Op->ShifterImm.isASR = isASR;
3670     Op->ShifterImm.Imm = Imm;
3671     Op->StartLoc = S;
3672     Op->EndLoc = E;
3673     return Op;
3674   }
3675 
3676   static std::unique_ptr<ARMOperand> CreateRotImm(unsigned Imm, SMLoc S,
3677                                                   SMLoc E) {
3678     auto Op = std::make_unique<ARMOperand>(k_RotateImmediate);
3679     Op->RotImm.Imm = Imm;
3680     Op->StartLoc = S;
3681     Op->EndLoc = E;
3682     return Op;
3683   }
3684 
3685   static std::unique_ptr<ARMOperand> CreateModImm(unsigned Bits, unsigned Rot,
3686                                                   SMLoc S, SMLoc E) {
3687     auto Op = std::make_unique<ARMOperand>(k_ModifiedImmediate);
3688     Op->ModImm.Bits = Bits;
3689     Op->ModImm.Rot = Rot;
3690     Op->StartLoc = S;
3691     Op->EndLoc = E;
3692     return Op;
3693   }
3694 
3695   static std::unique_ptr<ARMOperand>
3696   CreateConstantPoolImm(const MCExpr *Val, SMLoc S, SMLoc E) {
3697     auto Op = std::make_unique<ARMOperand>(k_ConstantPoolImmediate);
3698     Op->Imm.Val = Val;
3699     Op->StartLoc = S;
3700     Op->EndLoc = E;
3701     return Op;
3702   }
3703 
3704   static std::unique_ptr<ARMOperand>
3705   CreateBitfield(unsigned LSB, unsigned Width, SMLoc S, SMLoc E) {
3706     auto Op = std::make_unique<ARMOperand>(k_BitfieldDescriptor);
3707     Op->Bitfield.LSB = LSB;
3708     Op->Bitfield.Width = Width;
3709     Op->StartLoc = S;
3710     Op->EndLoc = E;
3711     return Op;
3712   }
3713 
3714   static std::unique_ptr<ARMOperand>
3715   CreateRegList(SmallVectorImpl<std::pair<unsigned, unsigned>> &Regs,
3716                 SMLoc StartLoc, SMLoc EndLoc) {
3717     assert(Regs.size() > 0 && "RegList contains no registers?");
3718     KindTy Kind = k_RegisterList;
3719 
3720     if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(
3721             Regs.front().second)) {
3722       if (Regs.back().second == ARM::VPR)
3723         Kind = k_FPDRegisterListWithVPR;
3724       else
3725         Kind = k_DPRRegisterList;
3726     } else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(
3727                    Regs.front().second)) {
3728       if (Regs.back().second == ARM::VPR)
3729         Kind = k_FPSRegisterListWithVPR;
3730       else
3731         Kind = k_SPRRegisterList;
3732     }
3733 
3734     if (Kind == k_RegisterList && Regs.back().second == ARM::APSR)
3735       Kind = k_RegisterListWithAPSR;
3736 
3737     assert(llvm::is_sorted(Regs) && "Register list must be sorted by encoding");
3738 
3739     auto Op = std::make_unique<ARMOperand>(Kind);
3740     for (const auto &P : Regs)
3741       Op->Registers.push_back(P.second);
3742 
3743     Op->StartLoc = StartLoc;
3744     Op->EndLoc = EndLoc;
3745     return Op;
3746   }
3747 
3748   static std::unique_ptr<ARMOperand> CreateVectorList(unsigned RegNum,
3749                                                       unsigned Count,
3750                                                       bool isDoubleSpaced,
3751                                                       SMLoc S, SMLoc E) {
3752     auto Op = std::make_unique<ARMOperand>(k_VectorList);
3753     Op->VectorList.RegNum = RegNum;
3754     Op->VectorList.Count = Count;
3755     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
3756     Op->StartLoc = S;
3757     Op->EndLoc = E;
3758     return Op;
3759   }
3760 
3761   static std::unique_ptr<ARMOperand>
3762   CreateVectorListAllLanes(unsigned RegNum, unsigned Count, bool isDoubleSpaced,
3763                            SMLoc S, SMLoc E) {
3764     auto Op = std::make_unique<ARMOperand>(k_VectorListAllLanes);
3765     Op->VectorList.RegNum = RegNum;
3766     Op->VectorList.Count = Count;
3767     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
3768     Op->StartLoc = S;
3769     Op->EndLoc = E;
3770     return Op;
3771   }
3772 
3773   static std::unique_ptr<ARMOperand>
3774   CreateVectorListIndexed(unsigned RegNum, unsigned Count, unsigned Index,
3775                           bool isDoubleSpaced, SMLoc S, SMLoc E) {
3776     auto Op = std::make_unique<ARMOperand>(k_VectorListIndexed);
3777     Op->VectorList.RegNum = RegNum;
3778     Op->VectorList.Count = Count;
3779     Op->VectorList.LaneIndex = Index;
3780     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
3781     Op->StartLoc = S;
3782     Op->EndLoc = E;
3783     return Op;
3784   }
3785 
3786   static std::unique_ptr<ARMOperand>
3787   CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E, MCContext &Ctx) {
3788     auto Op = std::make_unique<ARMOperand>(k_VectorIndex);
3789     Op->VectorIndex.Val = Idx;
3790     Op->StartLoc = S;
3791     Op->EndLoc = E;
3792     return Op;
3793   }
3794 
3795   static std::unique_ptr<ARMOperand> CreateImm(const MCExpr *Val, SMLoc S,
3796                                                SMLoc E) {
3797     auto Op = std::make_unique<ARMOperand>(k_Immediate);
3798     Op->Imm.Val = Val;
3799     Op->StartLoc = S;
3800     Op->EndLoc = E;
3801     return Op;
3802   }
3803 
3804   static std::unique_ptr<ARMOperand>
3805   CreateMem(unsigned BaseRegNum, const MCExpr *OffsetImm, unsigned OffsetRegNum,
3806             ARM_AM::ShiftOpc ShiftType, unsigned ShiftImm, unsigned Alignment,
3807             bool isNegative, SMLoc S, SMLoc E, SMLoc AlignmentLoc = SMLoc()) {
3808     auto Op = std::make_unique<ARMOperand>(k_Memory);
3809     Op->Memory.BaseRegNum = BaseRegNum;
3810     Op->Memory.OffsetImm = OffsetImm;
3811     Op->Memory.OffsetRegNum = OffsetRegNum;
3812     Op->Memory.ShiftType = ShiftType;
3813     Op->Memory.ShiftImm = ShiftImm;
3814     Op->Memory.Alignment = Alignment;
3815     Op->Memory.isNegative = isNegative;
3816     Op->StartLoc = S;
3817     Op->EndLoc = E;
3818     Op->AlignmentLoc = AlignmentLoc;
3819     return Op;
3820   }
3821 
3822   static std::unique_ptr<ARMOperand>
3823   CreatePostIdxReg(unsigned RegNum, bool isAdd, ARM_AM::ShiftOpc ShiftTy,
3824                    unsigned ShiftImm, SMLoc S, SMLoc E) {
3825     auto Op = std::make_unique<ARMOperand>(k_PostIndexRegister);
3826     Op->PostIdxReg.RegNum = RegNum;
3827     Op->PostIdxReg.isAdd = isAdd;
3828     Op->PostIdxReg.ShiftTy = ShiftTy;
3829     Op->PostIdxReg.ShiftImm = ShiftImm;
3830     Op->StartLoc = S;
3831     Op->EndLoc = E;
3832     return Op;
3833   }
3834 
3835   static std::unique_ptr<ARMOperand> CreateMemBarrierOpt(ARM_MB::MemBOpt Opt,
3836                                                          SMLoc S) {
3837     auto Op = std::make_unique<ARMOperand>(k_MemBarrierOpt);
3838     Op->MBOpt.Val = Opt;
3839     Op->StartLoc = S;
3840     Op->EndLoc = S;
3841     return Op;
3842   }
3843 
3844   static std::unique_ptr<ARMOperand>
3845   CreateInstSyncBarrierOpt(ARM_ISB::InstSyncBOpt Opt, SMLoc S) {
3846     auto Op = std::make_unique<ARMOperand>(k_InstSyncBarrierOpt);
3847     Op->ISBOpt.Val = Opt;
3848     Op->StartLoc = S;
3849     Op->EndLoc = S;
3850     return Op;
3851   }
3852 
3853   static std::unique_ptr<ARMOperand>
3854   CreateTraceSyncBarrierOpt(ARM_TSB::TraceSyncBOpt Opt, SMLoc S) {
3855     auto Op = std::make_unique<ARMOperand>(k_TraceSyncBarrierOpt);
3856     Op->TSBOpt.Val = Opt;
3857     Op->StartLoc = S;
3858     Op->EndLoc = S;
3859     return Op;
3860   }
3861 
3862   static std::unique_ptr<ARMOperand> CreateProcIFlags(ARM_PROC::IFlags IFlags,
3863                                                       SMLoc S) {
3864     auto Op = std::make_unique<ARMOperand>(k_ProcIFlags);
3865     Op->IFlags.Val = IFlags;
3866     Op->StartLoc = S;
3867     Op->EndLoc = S;
3868     return Op;
3869   }
3870 
3871   static std::unique_ptr<ARMOperand> CreateMSRMask(unsigned MMask, SMLoc S) {
3872     auto Op = std::make_unique<ARMOperand>(k_MSRMask);
3873     Op->MMask.Val = MMask;
3874     Op->StartLoc = S;
3875     Op->EndLoc = S;
3876     return Op;
3877   }
3878 
3879   static std::unique_ptr<ARMOperand> CreateBankedReg(unsigned Reg, SMLoc S) {
3880     auto Op = std::make_unique<ARMOperand>(k_BankedReg);
3881     Op->BankedReg.Val = Reg;
3882     Op->StartLoc = S;
3883     Op->EndLoc = S;
3884     return Op;
3885   }
3886 };
3887 
3888 } // end anonymous namespace.
3889 
3890 void ARMOperand::print(raw_ostream &OS) const {
3891   auto RegName = [](unsigned Reg) {
3892     if (Reg)
3893       return ARMInstPrinter::getRegisterName(Reg);
3894     else
3895       return "noreg";
3896   };
3897 
3898   switch (Kind) {
3899   case k_CondCode:
3900     OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
3901     break;
3902   case k_VPTPred:
3903     OS << "<ARMVCC::" << ARMVPTPredToString(getVPTPred()) << ">";
3904     break;
3905   case k_CCOut:
3906     OS << "<ccout " << RegName(getReg()) << ">";
3907     break;
3908   case k_ITCondMask: {
3909     static const char *const MaskStr[] = {
3910       "(invalid)", "(tttt)", "(ttt)", "(ttte)",
3911       "(tt)",      "(ttet)", "(tte)", "(ttee)",
3912       "(t)",       "(tett)", "(tet)", "(tete)",
3913       "(te)",      "(teet)", "(tee)", "(teee)",
3914     };
3915     assert((ITMask.Mask & 0xf) == ITMask.Mask);
3916     OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
3917     break;
3918   }
3919   case k_CoprocNum:
3920     OS << "<coprocessor number: " << getCoproc() << ">";
3921     break;
3922   case k_CoprocReg:
3923     OS << "<coprocessor register: " << getCoproc() << ">";
3924     break;
3925   case k_CoprocOption:
3926     OS << "<coprocessor option: " << CoprocOption.Val << ">";
3927     break;
3928   case k_MSRMask:
3929     OS << "<mask: " << getMSRMask() << ">";
3930     break;
3931   case k_BankedReg:
3932     OS << "<banked reg: " << getBankedReg() << ">";
3933     break;
3934   case k_Immediate:
3935     OS << *getImm();
3936     break;
3937   case k_MemBarrierOpt:
3938     OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt(), false) << ">";
3939     break;
3940   case k_InstSyncBarrierOpt:
3941     OS << "<ARM_ISB::" << InstSyncBOptToString(getInstSyncBarrierOpt()) << ">";
3942     break;
3943   case k_TraceSyncBarrierOpt:
3944     OS << "<ARM_TSB::" << TraceSyncBOptToString(getTraceSyncBarrierOpt()) << ">";
3945     break;
3946   case k_Memory:
3947     OS << "<memory";
3948     if (Memory.BaseRegNum)
3949       OS << " base:" << RegName(Memory.BaseRegNum);
3950     if (Memory.OffsetImm)
3951       OS << " offset-imm:" << *Memory.OffsetImm;
3952     if (Memory.OffsetRegNum)
3953       OS << " offset-reg:" << (Memory.isNegative ? "-" : "")
3954          << RegName(Memory.OffsetRegNum);
3955     if (Memory.ShiftType != ARM_AM::no_shift) {
3956       OS << " shift-type:" << ARM_AM::getShiftOpcStr(Memory.ShiftType);
3957       OS << " shift-imm:" << Memory.ShiftImm;
3958     }
3959     if (Memory.Alignment)
3960       OS << " alignment:" << Memory.Alignment;
3961     OS << ">";
3962     break;
3963   case k_PostIndexRegister:
3964     OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
3965        << RegName(PostIdxReg.RegNum);
3966     if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
3967       OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
3968          << PostIdxReg.ShiftImm;
3969     OS << ">";
3970     break;
3971   case k_ProcIFlags: {
3972     OS << "<ARM_PROC::";
3973     unsigned IFlags = getProcIFlags();
3974     for (int i=2; i >= 0; --i)
3975       if (IFlags & (1 << i))
3976         OS << ARM_PROC::IFlagsToString(1 << i);
3977     OS << ">";
3978     break;
3979   }
3980   case k_Register:
3981     OS << "<register " << RegName(getReg()) << ">";
3982     break;
3983   case k_ShifterImmediate:
3984     OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
3985        << " #" << ShifterImm.Imm << ">";
3986     break;
3987   case k_ShiftedRegister:
3988     OS << "<so_reg_reg " << RegName(RegShiftedReg.SrcReg) << " "
3989        << ARM_AM::getShiftOpcStr(RegShiftedReg.ShiftTy) << " "
3990        << RegName(RegShiftedReg.ShiftReg) << ">";
3991     break;
3992   case k_ShiftedImmediate:
3993     OS << "<so_reg_imm " << RegName(RegShiftedImm.SrcReg) << " "
3994        << ARM_AM::getShiftOpcStr(RegShiftedImm.ShiftTy) << " #"
3995        << RegShiftedImm.ShiftImm << ">";
3996     break;
3997   case k_RotateImmediate:
3998     OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
3999     break;
4000   case k_ModifiedImmediate:
4001     OS << "<mod_imm #" << ModImm.Bits << ", #"
4002        <<  ModImm.Rot << ")>";
4003     break;
4004   case k_ConstantPoolImmediate:
4005     OS << "<constant_pool_imm #" << *getConstantPoolImm();
4006     break;
4007   case k_BitfieldDescriptor:
4008     OS << "<bitfield " << "lsb: " << Bitfield.LSB
4009        << ", width: " << Bitfield.Width << ">";
4010     break;
4011   case k_RegisterList:
4012   case k_RegisterListWithAPSR:
4013   case k_DPRRegisterList:
4014   case k_SPRRegisterList:
4015   case k_FPSRegisterListWithVPR:
4016   case k_FPDRegisterListWithVPR: {
4017     OS << "<register_list ";
4018 
4019     const SmallVectorImpl<unsigned> &RegList = getRegList();
4020     for (SmallVectorImpl<unsigned>::const_iterator
4021            I = RegList.begin(), E = RegList.end(); I != E; ) {
4022       OS << RegName(*I);
4023       if (++I < E) OS << ", ";
4024     }
4025 
4026     OS << ">";
4027     break;
4028   }
4029   case k_VectorList:
4030     OS << "<vector_list " << VectorList.Count << " * "
4031        << RegName(VectorList.RegNum) << ">";
4032     break;
4033   case k_VectorListAllLanes:
4034     OS << "<vector_list(all lanes) " << VectorList.Count << " * "
4035        << RegName(VectorList.RegNum) << ">";
4036     break;
4037   case k_VectorListIndexed:
4038     OS << "<vector_list(lane " << VectorList.LaneIndex << ") "
4039        << VectorList.Count << " * " << RegName(VectorList.RegNum) << ">";
4040     break;
4041   case k_Token:
4042     OS << "'" << getToken() << "'";
4043     break;
4044   case k_VectorIndex:
4045     OS << "<vectorindex " << getVectorIndex() << ">";
4046     break;
4047   }
4048 }
4049 
4050 /// @name Auto-generated Match Functions
4051 /// {
4052 
4053 static unsigned MatchRegisterName(StringRef Name);
4054 
4055 /// }
4056 
4057 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
4058                                  SMLoc &StartLoc, SMLoc &EndLoc) {
4059   const AsmToken &Tok = getParser().getTok();
4060   StartLoc = Tok.getLoc();
4061   EndLoc = Tok.getEndLoc();
4062   RegNo = tryParseRegister();
4063 
4064   return (RegNo == (unsigned)-1);
4065 }
4066 
4067 OperandMatchResultTy ARMAsmParser::tryParseRegister(unsigned &RegNo,
4068                                                     SMLoc &StartLoc,
4069                                                     SMLoc &EndLoc) {
4070   if (ParseRegister(RegNo, StartLoc, EndLoc))
4071     return MatchOperand_NoMatch;
4072   return MatchOperand_Success;
4073 }
4074 
4075 /// Try to parse a register name.  The token must be an Identifier when called,
4076 /// and if it is a register name the token is eaten and the register number is
4077 /// returned.  Otherwise return -1.
4078 int ARMAsmParser::tryParseRegister() {
4079   MCAsmParser &Parser = getParser();
4080   const AsmToken &Tok = Parser.getTok();
4081   if (Tok.isNot(AsmToken::Identifier)) return -1;
4082 
4083   std::string lowerCase = Tok.getString().lower();
4084   unsigned RegNum = MatchRegisterName(lowerCase);
4085   if (!RegNum) {
4086     RegNum = StringSwitch<unsigned>(lowerCase)
4087       .Case("r13", ARM::SP)
4088       .Case("r14", ARM::LR)
4089       .Case("r15", ARM::PC)
4090       .Case("ip", ARM::R12)
4091       // Additional register name aliases for 'gas' compatibility.
4092       .Case("a1", ARM::R0)
4093       .Case("a2", ARM::R1)
4094       .Case("a3", ARM::R2)
4095       .Case("a4", ARM::R3)
4096       .Case("v1", ARM::R4)
4097       .Case("v2", ARM::R5)
4098       .Case("v3", ARM::R6)
4099       .Case("v4", ARM::R7)
4100       .Case("v5", ARM::R8)
4101       .Case("v6", ARM::R9)
4102       .Case("v7", ARM::R10)
4103       .Case("v8", ARM::R11)
4104       .Case("sb", ARM::R9)
4105       .Case("sl", ARM::R10)
4106       .Case("fp", ARM::R11)
4107       .Default(0);
4108   }
4109   if (!RegNum) {
4110     // Check for aliases registered via .req. Canonicalize to lower case.
4111     // That's more consistent since register names are case insensitive, and
4112     // it's how the original entry was passed in from MC/MCParser/AsmParser.
4113     StringMap<unsigned>::const_iterator Entry = RegisterReqs.find(lowerCase);
4114     // If no match, return failure.
4115     if (Entry == RegisterReqs.end())
4116       return -1;
4117     Parser.Lex(); // Eat identifier token.
4118     return Entry->getValue();
4119   }
4120 
4121   // Some FPUs only have 16 D registers, so D16-D31 are invalid
4122   if (!hasD32() && RegNum >= ARM::D16 && RegNum <= ARM::D31)
4123     return -1;
4124 
4125   Parser.Lex(); // Eat identifier token.
4126 
4127   return RegNum;
4128 }
4129 
4130 // Try to parse a shifter  (e.g., "lsl <amt>"). On success, return 0.
4131 // If a recoverable error occurs, return 1. If an irrecoverable error
4132 // occurs, return -1. An irrecoverable error is one where tokens have been
4133 // consumed in the process of trying to parse the shifter (i.e., when it is
4134 // indeed a shifter operand, but malformed).
4135 int ARMAsmParser::tryParseShiftRegister(OperandVector &Operands) {
4136   MCAsmParser &Parser = getParser();
4137   SMLoc S = Parser.getTok().getLoc();
4138   const AsmToken &Tok = Parser.getTok();
4139   if (Tok.isNot(AsmToken::Identifier))
4140     return -1;
4141 
4142   std::string lowerCase = Tok.getString().lower();
4143   ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
4144       .Case("asl", ARM_AM::lsl)
4145       .Case("lsl", ARM_AM::lsl)
4146       .Case("lsr", ARM_AM::lsr)
4147       .Case("asr", ARM_AM::asr)
4148       .Case("ror", ARM_AM::ror)
4149       .Case("rrx", ARM_AM::rrx)
4150       .Default(ARM_AM::no_shift);
4151 
4152   if (ShiftTy == ARM_AM::no_shift)
4153     return 1;
4154 
4155   Parser.Lex(); // Eat the operator.
4156 
4157   // The source register for the shift has already been added to the
4158   // operand list, so we need to pop it off and combine it into the shifted
4159   // register operand instead.
4160   std::unique_ptr<ARMOperand> PrevOp(
4161       (ARMOperand *)Operands.pop_back_val().release());
4162   if (!PrevOp->isReg())
4163     return Error(PrevOp->getStartLoc(), "shift must be of a register");
4164   int SrcReg = PrevOp->getReg();
4165 
4166   SMLoc EndLoc;
4167   int64_t Imm = 0;
4168   int ShiftReg = 0;
4169   if (ShiftTy == ARM_AM::rrx) {
4170     // RRX Doesn't have an explicit shift amount. The encoder expects
4171     // the shift register to be the same as the source register. Seems odd,
4172     // but OK.
4173     ShiftReg = SrcReg;
4174   } else {
4175     // Figure out if this is shifted by a constant or a register (for non-RRX).
4176     if (Parser.getTok().is(AsmToken::Hash) ||
4177         Parser.getTok().is(AsmToken::Dollar)) {
4178       Parser.Lex(); // Eat hash.
4179       SMLoc ImmLoc = Parser.getTok().getLoc();
4180       const MCExpr *ShiftExpr = nullptr;
4181       if (getParser().parseExpression(ShiftExpr, EndLoc)) {
4182         Error(ImmLoc, "invalid immediate shift value");
4183         return -1;
4184       }
4185       // The expression must be evaluatable as an immediate.
4186       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
4187       if (!CE) {
4188         Error(ImmLoc, "invalid immediate shift value");
4189         return -1;
4190       }
4191       // Range check the immediate.
4192       // lsl, ror: 0 <= imm <= 31
4193       // lsr, asr: 0 <= imm <= 32
4194       Imm = CE->getValue();
4195       if (Imm < 0 ||
4196           ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
4197           ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
4198         Error(ImmLoc, "immediate shift value out of range");
4199         return -1;
4200       }
4201       // shift by zero is a nop. Always send it through as lsl.
4202       // ('as' compatibility)
4203       if (Imm == 0)
4204         ShiftTy = ARM_AM::lsl;
4205     } else if (Parser.getTok().is(AsmToken::Identifier)) {
4206       SMLoc L = Parser.getTok().getLoc();
4207       EndLoc = Parser.getTok().getEndLoc();
4208       ShiftReg = tryParseRegister();
4209       if (ShiftReg == -1) {
4210         Error(L, "expected immediate or register in shift operand");
4211         return -1;
4212       }
4213     } else {
4214       Error(Parser.getTok().getLoc(),
4215             "expected immediate or register in shift operand");
4216       return -1;
4217     }
4218   }
4219 
4220   if (ShiftReg && ShiftTy != ARM_AM::rrx)
4221     Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
4222                                                          ShiftReg, Imm,
4223                                                          S, EndLoc));
4224   else
4225     Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
4226                                                           S, EndLoc));
4227 
4228   return 0;
4229 }
4230 
4231 /// Try to parse a register name.  The token must be an Identifier when called.
4232 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
4233 /// if there is a "writeback". 'true' if it's not a register.
4234 ///
4235 /// TODO this is likely to change to allow different register types and or to
4236 /// parse for a specific register type.
4237 bool ARMAsmParser::tryParseRegisterWithWriteBack(OperandVector &Operands) {
4238   MCAsmParser &Parser = getParser();
4239   SMLoc RegStartLoc = Parser.getTok().getLoc();
4240   SMLoc RegEndLoc = Parser.getTok().getEndLoc();
4241   int RegNo = tryParseRegister();
4242   if (RegNo == -1)
4243     return true;
4244 
4245   Operands.push_back(ARMOperand::CreateReg(RegNo, RegStartLoc, RegEndLoc));
4246 
4247   const AsmToken &ExclaimTok = Parser.getTok();
4248   if (ExclaimTok.is(AsmToken::Exclaim)) {
4249     Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
4250                                                ExclaimTok.getLoc()));
4251     Parser.Lex(); // Eat exclaim token
4252     return false;
4253   }
4254 
4255   // Also check for an index operand. This is only legal for vector registers,
4256   // but that'll get caught OK in operand matching, so we don't need to
4257   // explicitly filter everything else out here.
4258   if (Parser.getTok().is(AsmToken::LBrac)) {
4259     SMLoc SIdx = Parser.getTok().getLoc();
4260     Parser.Lex(); // Eat left bracket token.
4261 
4262     const MCExpr *ImmVal;
4263     if (getParser().parseExpression(ImmVal))
4264       return true;
4265     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
4266     if (!MCE)
4267       return TokError("immediate value expected for vector index");
4268 
4269     if (Parser.getTok().isNot(AsmToken::RBrac))
4270       return Error(Parser.getTok().getLoc(), "']' expected");
4271 
4272     SMLoc E = Parser.getTok().getEndLoc();
4273     Parser.Lex(); // Eat right bracket token.
4274 
4275     Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
4276                                                      SIdx, E,
4277                                                      getContext()));
4278   }
4279 
4280   return false;
4281 }
4282 
4283 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
4284 /// instruction with a symbolic operand name.
4285 /// We accept "crN" syntax for GAS compatibility.
4286 /// <operand-name> ::= <prefix><number>
4287 /// If CoprocOp is 'c', then:
4288 ///   <prefix> ::= c | cr
4289 /// If CoprocOp is 'p', then :
4290 ///   <prefix> ::= p
4291 /// <number> ::= integer in range [0, 15]
4292 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
4293   // Use the same layout as the tablegen'erated register name matcher. Ugly,
4294   // but efficient.
4295   if (Name.size() < 2 || Name[0] != CoprocOp)
4296     return -1;
4297   Name = (Name[1] == 'r') ? Name.drop_front(2) : Name.drop_front();
4298 
4299   switch (Name.size()) {
4300   default: return -1;
4301   case 1:
4302     switch (Name[0]) {
4303     default:  return -1;
4304     case '0': return 0;
4305     case '1': return 1;
4306     case '2': return 2;
4307     case '3': return 3;
4308     case '4': return 4;
4309     case '5': return 5;
4310     case '6': return 6;
4311     case '7': return 7;
4312     case '8': return 8;
4313     case '9': return 9;
4314     }
4315   case 2:
4316     if (Name[0] != '1')
4317       return -1;
4318     switch (Name[1]) {
4319     default:  return -1;
4320     // CP10 and CP11 are VFP/NEON and so vector instructions should be used.
4321     // However, old cores (v5/v6) did use them in that way.
4322     case '0': return 10;
4323     case '1': return 11;
4324     case '2': return 12;
4325     case '3': return 13;
4326     case '4': return 14;
4327     case '5': return 15;
4328     }
4329   }
4330 }
4331 
4332 /// parseITCondCode - Try to parse a condition code for an IT instruction.
4333 OperandMatchResultTy
4334 ARMAsmParser::parseITCondCode(OperandVector &Operands) {
4335   MCAsmParser &Parser = getParser();
4336   SMLoc S = Parser.getTok().getLoc();
4337   const AsmToken &Tok = Parser.getTok();
4338   if (!Tok.is(AsmToken::Identifier))
4339     return MatchOperand_NoMatch;
4340   unsigned CC = ARMCondCodeFromString(Tok.getString());
4341   if (CC == ~0U)
4342     return MatchOperand_NoMatch;
4343   Parser.Lex(); // Eat the token.
4344 
4345   Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
4346 
4347   return MatchOperand_Success;
4348 }
4349 
4350 /// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
4351 /// token must be an Identifier when called, and if it is a coprocessor
4352 /// number, the token is eaten and the operand is added to the operand list.
4353 OperandMatchResultTy
4354 ARMAsmParser::parseCoprocNumOperand(OperandVector &Operands) {
4355   MCAsmParser &Parser = getParser();
4356   SMLoc S = Parser.getTok().getLoc();
4357   const AsmToken &Tok = Parser.getTok();
4358   if (Tok.isNot(AsmToken::Identifier))
4359     return MatchOperand_NoMatch;
4360 
4361   int Num = MatchCoprocessorOperandName(Tok.getString().lower(), 'p');
4362   if (Num == -1)
4363     return MatchOperand_NoMatch;
4364   if (!isValidCoprocessorNumber(Num, getSTI().getFeatureBits()))
4365     return MatchOperand_NoMatch;
4366 
4367   Parser.Lex(); // Eat identifier token.
4368   Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
4369   return MatchOperand_Success;
4370 }
4371 
4372 /// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
4373 /// token must be an Identifier when called, and if it is a coprocessor
4374 /// number, the token is eaten and the operand is added to the operand list.
4375 OperandMatchResultTy
4376 ARMAsmParser::parseCoprocRegOperand(OperandVector &Operands) {
4377   MCAsmParser &Parser = getParser();
4378   SMLoc S = Parser.getTok().getLoc();
4379   const AsmToken &Tok = Parser.getTok();
4380   if (Tok.isNot(AsmToken::Identifier))
4381     return MatchOperand_NoMatch;
4382 
4383   int Reg = MatchCoprocessorOperandName(Tok.getString().lower(), 'c');
4384   if (Reg == -1)
4385     return MatchOperand_NoMatch;
4386 
4387   Parser.Lex(); // Eat identifier token.
4388   Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
4389   return MatchOperand_Success;
4390 }
4391 
4392 /// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
4393 /// coproc_option : '{' imm0_255 '}'
4394 OperandMatchResultTy
4395 ARMAsmParser::parseCoprocOptionOperand(OperandVector &Operands) {
4396   MCAsmParser &Parser = getParser();
4397   SMLoc S = Parser.getTok().getLoc();
4398 
4399   // If this isn't a '{', this isn't a coprocessor immediate operand.
4400   if (Parser.getTok().isNot(AsmToken::LCurly))
4401     return MatchOperand_NoMatch;
4402   Parser.Lex(); // Eat the '{'
4403 
4404   const MCExpr *Expr;
4405   SMLoc Loc = Parser.getTok().getLoc();
4406   if (getParser().parseExpression(Expr)) {
4407     Error(Loc, "illegal expression");
4408     return MatchOperand_ParseFail;
4409   }
4410   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
4411   if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
4412     Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
4413     return MatchOperand_ParseFail;
4414   }
4415   int Val = CE->getValue();
4416 
4417   // Check for and consume the closing '}'
4418   if (Parser.getTok().isNot(AsmToken::RCurly))
4419     return MatchOperand_ParseFail;
4420   SMLoc E = Parser.getTok().getEndLoc();
4421   Parser.Lex(); // Eat the '}'
4422 
4423   Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
4424   return MatchOperand_Success;
4425 }
4426 
4427 // For register list parsing, we need to map from raw GPR register numbering
4428 // to the enumeration values. The enumeration values aren't sorted by
4429 // register number due to our using "sp", "lr" and "pc" as canonical names.
4430 static unsigned getNextRegister(unsigned Reg) {
4431   // If this is a GPR, we need to do it manually, otherwise we can rely
4432   // on the sort ordering of the enumeration since the other reg-classes
4433   // are sane.
4434   if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
4435     return Reg + 1;
4436   switch(Reg) {
4437   default: llvm_unreachable("Invalid GPR number!");
4438   case ARM::R0:  return ARM::R1;  case ARM::R1:  return ARM::R2;
4439   case ARM::R2:  return ARM::R3;  case ARM::R3:  return ARM::R4;
4440   case ARM::R4:  return ARM::R5;  case ARM::R5:  return ARM::R6;
4441   case ARM::R6:  return ARM::R7;  case ARM::R7:  return ARM::R8;
4442   case ARM::R8:  return ARM::R9;  case ARM::R9:  return ARM::R10;
4443   case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
4444   case ARM::R12: return ARM::SP;  case ARM::SP:  return ARM::LR;
4445   case ARM::LR:  return ARM::PC;  case ARM::PC:  return ARM::R0;
4446   }
4447 }
4448 
4449 // Insert an <Encoding, Register> pair in an ordered vector. Return true on
4450 // success, or false, if duplicate encoding found.
4451 static bool
4452 insertNoDuplicates(SmallVectorImpl<std::pair<unsigned, unsigned>> &Regs,
4453                    unsigned Enc, unsigned Reg) {
4454   Regs.emplace_back(Enc, Reg);
4455   for (auto I = Regs.rbegin(), J = I + 1, E = Regs.rend(); J != E; ++I, ++J) {
4456     if (J->first == Enc) {
4457       Regs.erase(J.base());
4458       return false;
4459     }
4460     if (J->first < Enc)
4461       break;
4462     std::swap(*I, *J);
4463   }
4464   return true;
4465 }
4466 
4467 /// Parse a register list.
4468 bool ARMAsmParser::parseRegisterList(OperandVector &Operands, bool EnforceOrder,
4469                                      bool AllowRAAC) {
4470   MCAsmParser &Parser = getParser();
4471   if (Parser.getTok().isNot(AsmToken::LCurly))
4472     return TokError("Token is not a Left Curly Brace");
4473   SMLoc S = Parser.getTok().getLoc();
4474   Parser.Lex(); // Eat '{' token.
4475   SMLoc RegLoc = Parser.getTok().getLoc();
4476 
4477   // Check the first register in the list to see what register class
4478   // this is a list of.
4479   int Reg = tryParseRegister();
4480   if (Reg == -1)
4481     return Error(RegLoc, "register expected");
4482   if (!AllowRAAC && Reg == ARM::RA_AUTH_CODE)
4483     return Error(RegLoc, "pseudo-register not allowed");
4484   // The reglist instructions have at most 16 registers, so reserve
4485   // space for that many.
4486   int EReg = 0;
4487   SmallVector<std::pair<unsigned, unsigned>, 16> Registers;
4488 
4489   // Allow Q regs and just interpret them as the two D sub-registers.
4490   if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
4491     Reg = getDRegFromQReg(Reg);
4492     EReg = MRI->getEncodingValue(Reg);
4493     Registers.emplace_back(EReg, Reg);
4494     ++Reg;
4495   }
4496   const MCRegisterClass *RC;
4497   if (Reg == ARM::RA_AUTH_CODE ||
4498       ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
4499     RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
4500   else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
4501     RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
4502   else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
4503     RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
4504   else if (ARMMCRegisterClasses[ARM::GPRwithAPSRnospRegClassID].contains(Reg))
4505     RC = &ARMMCRegisterClasses[ARM::GPRwithAPSRnospRegClassID];
4506   else
4507     return Error(RegLoc, "invalid register in register list");
4508 
4509   // Store the register.
4510   EReg = MRI->getEncodingValue(Reg);
4511   Registers.emplace_back(EReg, Reg);
4512 
4513   // This starts immediately after the first register token in the list,
4514   // so we can see either a comma or a minus (range separator) as a legal
4515   // next token.
4516   while (Parser.getTok().is(AsmToken::Comma) ||
4517          Parser.getTok().is(AsmToken::Minus)) {
4518     if (Parser.getTok().is(AsmToken::Minus)) {
4519       if (Reg == ARM::RA_AUTH_CODE)
4520         return Error(RegLoc, "pseudo-register not allowed");
4521       Parser.Lex(); // Eat the minus.
4522       SMLoc AfterMinusLoc = Parser.getTok().getLoc();
4523       int EndReg = tryParseRegister();
4524       if (EndReg == -1)
4525         return Error(AfterMinusLoc, "register expected");
4526       if (EndReg == ARM::RA_AUTH_CODE)
4527         return Error(AfterMinusLoc, "pseudo-register not allowed");
4528       // Allow Q regs and just interpret them as the two D sub-registers.
4529       if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
4530         EndReg = getDRegFromQReg(EndReg) + 1;
4531       // If the register is the same as the start reg, there's nothing
4532       // more to do.
4533       if (Reg == EndReg)
4534         continue;
4535       // The register must be in the same register class as the first.
4536       if ((Reg == ARM::RA_AUTH_CODE &&
4537            RC != &ARMMCRegisterClasses[ARM::GPRRegClassID]) ||
4538           (Reg != ARM::RA_AUTH_CODE && !RC->contains(Reg)))
4539         return Error(AfterMinusLoc, "invalid register in register list");
4540       // Ranges must go from low to high.
4541       if (MRI->getEncodingValue(Reg) > MRI->getEncodingValue(EndReg))
4542         return Error(AfterMinusLoc, "bad range in register list");
4543 
4544       // Add all the registers in the range to the register list.
4545       while (Reg != EndReg) {
4546         Reg = getNextRegister(Reg);
4547         EReg = MRI->getEncodingValue(Reg);
4548         if (!insertNoDuplicates(Registers, EReg, Reg)) {
4549           Warning(AfterMinusLoc, StringRef("duplicated register (") +
4550                                      ARMInstPrinter::getRegisterName(Reg) +
4551                                      ") in register list");
4552         }
4553       }
4554       continue;
4555     }
4556     Parser.Lex(); // Eat the comma.
4557     RegLoc = Parser.getTok().getLoc();
4558     int OldReg = Reg;
4559     const AsmToken RegTok = Parser.getTok();
4560     Reg = tryParseRegister();
4561     if (Reg == -1)
4562       return Error(RegLoc, "register expected");
4563     if (!AllowRAAC && Reg == ARM::RA_AUTH_CODE)
4564       return Error(RegLoc, "pseudo-register not allowed");
4565     // Allow Q regs and just interpret them as the two D sub-registers.
4566     bool isQReg = false;
4567     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
4568       Reg = getDRegFromQReg(Reg);
4569       isQReg = true;
4570     }
4571     if (Reg != ARM::RA_AUTH_CODE && !RC->contains(Reg) &&
4572         RC->getID() == ARMMCRegisterClasses[ARM::GPRRegClassID].getID() &&
4573         ARMMCRegisterClasses[ARM::GPRwithAPSRnospRegClassID].contains(Reg)) {
4574       // switch the register classes, as GPRwithAPSRnospRegClassID is a partial
4575       // subset of GPRRegClassId except it contains APSR as well.
4576       RC = &ARMMCRegisterClasses[ARM::GPRwithAPSRnospRegClassID];
4577     }
4578     if (Reg == ARM::VPR &&
4579         (RC == &ARMMCRegisterClasses[ARM::SPRRegClassID] ||
4580          RC == &ARMMCRegisterClasses[ARM::DPRRegClassID] ||
4581          RC == &ARMMCRegisterClasses[ARM::FPWithVPRRegClassID])) {
4582       RC = &ARMMCRegisterClasses[ARM::FPWithVPRRegClassID];
4583       EReg = MRI->getEncodingValue(Reg);
4584       if (!insertNoDuplicates(Registers, EReg, Reg)) {
4585         Warning(RegLoc, "duplicated register (" + RegTok.getString() +
4586                             ") in register list");
4587       }
4588       continue;
4589     }
4590     // The register must be in the same register class as the first.
4591     if ((Reg == ARM::RA_AUTH_CODE &&
4592          RC != &ARMMCRegisterClasses[ARM::GPRRegClassID]) ||
4593         (Reg != ARM::RA_AUTH_CODE && !RC->contains(Reg)))
4594       return Error(RegLoc, "invalid register in register list");
4595     // In most cases, the list must be monotonically increasing. An
4596     // exception is CLRM, which is order-independent anyway, so
4597     // there's no potential for confusion if you write clrm {r2,r1}
4598     // instead of clrm {r1,r2}.
4599     if (EnforceOrder &&
4600         MRI->getEncodingValue(Reg) < MRI->getEncodingValue(OldReg)) {
4601       if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
4602         Warning(RegLoc, "register list not in ascending order");
4603       else if (!ARMMCRegisterClasses[ARM::GPRwithAPSRnospRegClassID].contains(Reg))
4604         return Error(RegLoc, "register list not in ascending order");
4605     }
4606     // VFP register lists must also be contiguous.
4607     if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
4608         RC != &ARMMCRegisterClasses[ARM::GPRwithAPSRnospRegClassID] &&
4609         Reg != OldReg + 1)
4610       return Error(RegLoc, "non-contiguous register range");
4611     EReg = MRI->getEncodingValue(Reg);
4612     if (!insertNoDuplicates(Registers, EReg, Reg)) {
4613       Warning(RegLoc, "duplicated register (" + RegTok.getString() +
4614                           ") in register list");
4615     }
4616     if (isQReg) {
4617       EReg = MRI->getEncodingValue(++Reg);
4618       Registers.emplace_back(EReg, Reg);
4619     }
4620   }
4621 
4622   if (Parser.getTok().isNot(AsmToken::RCurly))
4623     return Error(Parser.getTok().getLoc(), "'}' expected");
4624   SMLoc E = Parser.getTok().getEndLoc();
4625   Parser.Lex(); // Eat '}' token.
4626 
4627   // Push the register list operand.
4628   Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
4629 
4630   // The ARM system instruction variants for LDM/STM have a '^' token here.
4631   if (Parser.getTok().is(AsmToken::Caret)) {
4632     Operands.push_back(ARMOperand::CreateToken("^",Parser.getTok().getLoc()));
4633     Parser.Lex(); // Eat '^' token.
4634   }
4635 
4636   return false;
4637 }
4638 
4639 // Helper function to parse the lane index for vector lists.
4640 OperandMatchResultTy ARMAsmParser::
4641 parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index, SMLoc &EndLoc) {
4642   MCAsmParser &Parser = getParser();
4643   Index = 0; // Always return a defined index value.
4644   if (Parser.getTok().is(AsmToken::LBrac)) {
4645     Parser.Lex(); // Eat the '['.
4646     if (Parser.getTok().is(AsmToken::RBrac)) {
4647       // "Dn[]" is the 'all lanes' syntax.
4648       LaneKind = AllLanes;
4649       EndLoc = Parser.getTok().getEndLoc();
4650       Parser.Lex(); // Eat the ']'.
4651       return MatchOperand_Success;
4652     }
4653 
4654     // There's an optional '#' token here. Normally there wouldn't be, but
4655     // inline assemble puts one in, and it's friendly to accept that.
4656     if (Parser.getTok().is(AsmToken::Hash))
4657       Parser.Lex(); // Eat '#' or '$'.
4658 
4659     const MCExpr *LaneIndex;
4660     SMLoc Loc = Parser.getTok().getLoc();
4661     if (getParser().parseExpression(LaneIndex)) {
4662       Error(Loc, "illegal expression");
4663       return MatchOperand_ParseFail;
4664     }
4665     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LaneIndex);
4666     if (!CE) {
4667       Error(Loc, "lane index must be empty or an integer");
4668       return MatchOperand_ParseFail;
4669     }
4670     if (Parser.getTok().isNot(AsmToken::RBrac)) {
4671       Error(Parser.getTok().getLoc(), "']' expected");
4672       return MatchOperand_ParseFail;
4673     }
4674     EndLoc = Parser.getTok().getEndLoc();
4675     Parser.Lex(); // Eat the ']'.
4676     int64_t Val = CE->getValue();
4677 
4678     // FIXME: Make this range check context sensitive for .8, .16, .32.
4679     if (Val < 0 || Val > 7) {
4680       Error(Parser.getTok().getLoc(), "lane index out of range");
4681       return MatchOperand_ParseFail;
4682     }
4683     Index = Val;
4684     LaneKind = IndexedLane;
4685     return MatchOperand_Success;
4686   }
4687   LaneKind = NoLanes;
4688   return MatchOperand_Success;
4689 }
4690 
4691 // parse a vector register list
4692 OperandMatchResultTy
4693 ARMAsmParser::parseVectorList(OperandVector &Operands) {
4694   MCAsmParser &Parser = getParser();
4695   VectorLaneTy LaneKind;
4696   unsigned LaneIndex;
4697   SMLoc S = Parser.getTok().getLoc();
4698   // As an extension (to match gas), support a plain D register or Q register
4699   // (without encosing curly braces) as a single or double entry list,
4700   // respectively.
4701   if (!hasMVE() && Parser.getTok().is(AsmToken::Identifier)) {
4702     SMLoc E = Parser.getTok().getEndLoc();
4703     int Reg = tryParseRegister();
4704     if (Reg == -1)
4705       return MatchOperand_NoMatch;
4706     if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg)) {
4707       OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex, E);
4708       if (Res != MatchOperand_Success)
4709         return Res;
4710       switch (LaneKind) {
4711       case NoLanes:
4712         Operands.push_back(ARMOperand::CreateVectorList(Reg, 1, false, S, E));
4713         break;
4714       case AllLanes:
4715         Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 1, false,
4716                                                                 S, E));
4717         break;
4718       case IndexedLane:
4719         Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 1,
4720                                                                LaneIndex,
4721                                                                false, S, E));
4722         break;
4723       }
4724       return MatchOperand_Success;
4725     }
4726     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
4727       Reg = getDRegFromQReg(Reg);
4728       OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex, E);
4729       if (Res != MatchOperand_Success)
4730         return Res;
4731       switch (LaneKind) {
4732       case NoLanes:
4733         Reg = MRI->getMatchingSuperReg(Reg, ARM::dsub_0,
4734                                    &ARMMCRegisterClasses[ARM::DPairRegClassID]);
4735         Operands.push_back(ARMOperand::CreateVectorList(Reg, 2, false, S, E));
4736         break;
4737       case AllLanes:
4738         Reg = MRI->getMatchingSuperReg(Reg, ARM::dsub_0,
4739                                    &ARMMCRegisterClasses[ARM::DPairRegClassID]);
4740         Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 2, false,
4741                                                                 S, E));
4742         break;
4743       case IndexedLane:
4744         Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 2,
4745                                                                LaneIndex,
4746                                                                false, S, E));
4747         break;
4748       }
4749       return MatchOperand_Success;
4750     }
4751     Error(S, "vector register expected");
4752     return MatchOperand_ParseFail;
4753   }
4754 
4755   if (Parser.getTok().isNot(AsmToken::LCurly))
4756     return MatchOperand_NoMatch;
4757 
4758   Parser.Lex(); // Eat '{' token.
4759   SMLoc RegLoc = Parser.getTok().getLoc();
4760 
4761   int Reg = tryParseRegister();
4762   if (Reg == -1) {
4763     Error(RegLoc, "register expected");
4764     return MatchOperand_ParseFail;
4765   }
4766   unsigned Count = 1;
4767   int Spacing = 0;
4768   unsigned FirstReg = Reg;
4769 
4770   if (hasMVE() && !ARMMCRegisterClasses[ARM::MQPRRegClassID].contains(Reg)) {
4771       Error(Parser.getTok().getLoc(), "vector register in range Q0-Q7 expected");
4772       return MatchOperand_ParseFail;
4773   }
4774   // The list is of D registers, but we also allow Q regs and just interpret
4775   // them as the two D sub-registers.
4776   else if (!hasMVE() && ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
4777     FirstReg = Reg = getDRegFromQReg(Reg);
4778     Spacing = 1; // double-spacing requires explicit D registers, otherwise
4779                  // it's ambiguous with four-register single spaced.
4780     ++Reg;
4781     ++Count;
4782   }
4783 
4784   SMLoc E;
4785   if (parseVectorLane(LaneKind, LaneIndex, E) != MatchOperand_Success)
4786     return MatchOperand_ParseFail;
4787 
4788   while (Parser.getTok().is(AsmToken::Comma) ||
4789          Parser.getTok().is(AsmToken::Minus)) {
4790     if (Parser.getTok().is(AsmToken::Minus)) {
4791       if (!Spacing)
4792         Spacing = 1; // Register range implies a single spaced list.
4793       else if (Spacing == 2) {
4794         Error(Parser.getTok().getLoc(),
4795               "sequential registers in double spaced list");
4796         return MatchOperand_ParseFail;
4797       }
4798       Parser.Lex(); // Eat the minus.
4799       SMLoc AfterMinusLoc = Parser.getTok().getLoc();
4800       int EndReg = tryParseRegister();
4801       if (EndReg == -1) {
4802         Error(AfterMinusLoc, "register expected");
4803         return MatchOperand_ParseFail;
4804       }
4805       // Allow Q regs and just interpret them as the two D sub-registers.
4806       if (!hasMVE() && ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
4807         EndReg = getDRegFromQReg(EndReg) + 1;
4808       // If the register is the same as the start reg, there's nothing
4809       // more to do.
4810       if (Reg == EndReg)
4811         continue;
4812       // The register must be in the same register class as the first.
4813       if ((hasMVE() &&
4814            !ARMMCRegisterClasses[ARM::MQPRRegClassID].contains(EndReg)) ||
4815           (!hasMVE() &&
4816            !ARMMCRegisterClasses[ARM::DPRRegClassID].contains(EndReg))) {
4817         Error(AfterMinusLoc, "invalid register in register list");
4818         return MatchOperand_ParseFail;
4819       }
4820       // Ranges must go from low to high.
4821       if (Reg > EndReg) {
4822         Error(AfterMinusLoc, "bad range in register list");
4823         return MatchOperand_ParseFail;
4824       }
4825       // Parse the lane specifier if present.
4826       VectorLaneTy NextLaneKind;
4827       unsigned NextLaneIndex;
4828       if (parseVectorLane(NextLaneKind, NextLaneIndex, E) !=
4829           MatchOperand_Success)
4830         return MatchOperand_ParseFail;
4831       if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
4832         Error(AfterMinusLoc, "mismatched lane index in register list");
4833         return MatchOperand_ParseFail;
4834       }
4835 
4836       // Add all the registers in the range to the register list.
4837       Count += EndReg - Reg;
4838       Reg = EndReg;
4839       continue;
4840     }
4841     Parser.Lex(); // Eat the comma.
4842     RegLoc = Parser.getTok().getLoc();
4843     int OldReg = Reg;
4844     Reg = tryParseRegister();
4845     if (Reg == -1) {
4846       Error(RegLoc, "register expected");
4847       return MatchOperand_ParseFail;
4848     }
4849 
4850     if (hasMVE()) {
4851       if (!ARMMCRegisterClasses[ARM::MQPRRegClassID].contains(Reg)) {
4852         Error(RegLoc, "vector register in range Q0-Q7 expected");
4853         return MatchOperand_ParseFail;
4854       }
4855       Spacing = 1;
4856     }
4857     // vector register lists must be contiguous.
4858     // It's OK to use the enumeration values directly here rather, as the
4859     // VFP register classes have the enum sorted properly.
4860     //
4861     // The list is of D registers, but we also allow Q regs and just interpret
4862     // them as the two D sub-registers.
4863     else if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
4864       if (!Spacing)
4865         Spacing = 1; // Register range implies a single spaced list.
4866       else if (Spacing == 2) {
4867         Error(RegLoc,
4868               "invalid register in double-spaced list (must be 'D' register')");
4869         return MatchOperand_ParseFail;
4870       }
4871       Reg = getDRegFromQReg(Reg);
4872       if (Reg != OldReg + 1) {
4873         Error(RegLoc, "non-contiguous register range");
4874         return MatchOperand_ParseFail;
4875       }
4876       ++Reg;
4877       Count += 2;
4878       // Parse the lane specifier if present.
4879       VectorLaneTy NextLaneKind;
4880       unsigned NextLaneIndex;
4881       SMLoc LaneLoc = Parser.getTok().getLoc();
4882       if (parseVectorLane(NextLaneKind, NextLaneIndex, E) !=
4883           MatchOperand_Success)
4884         return MatchOperand_ParseFail;
4885       if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
4886         Error(LaneLoc, "mismatched lane index in register list");
4887         return MatchOperand_ParseFail;
4888       }
4889       continue;
4890     }
4891     // Normal D register.
4892     // Figure out the register spacing (single or double) of the list if
4893     // we don't know it already.
4894     if (!Spacing)
4895       Spacing = 1 + (Reg == OldReg + 2);
4896 
4897     // Just check that it's contiguous and keep going.
4898     if (Reg != OldReg + Spacing) {
4899       Error(RegLoc, "non-contiguous register range");
4900       return MatchOperand_ParseFail;
4901     }
4902     ++Count;
4903     // Parse the lane specifier if present.
4904     VectorLaneTy NextLaneKind;
4905     unsigned NextLaneIndex;
4906     SMLoc EndLoc = Parser.getTok().getLoc();
4907     if (parseVectorLane(NextLaneKind, NextLaneIndex, E) != MatchOperand_Success)
4908       return MatchOperand_ParseFail;
4909     if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
4910       Error(EndLoc, "mismatched lane index in register list");
4911       return MatchOperand_ParseFail;
4912     }
4913   }
4914 
4915   if (Parser.getTok().isNot(AsmToken::RCurly)) {
4916     Error(Parser.getTok().getLoc(), "'}' expected");
4917     return MatchOperand_ParseFail;
4918   }
4919   E = Parser.getTok().getEndLoc();
4920   Parser.Lex(); // Eat '}' token.
4921 
4922   switch (LaneKind) {
4923   case NoLanes:
4924   case AllLanes: {
4925     // Two-register operands have been converted to the
4926     // composite register classes.
4927     if (Count == 2 && !hasMVE()) {
4928       const MCRegisterClass *RC = (Spacing == 1) ?
4929         &ARMMCRegisterClasses[ARM::DPairRegClassID] :
4930         &ARMMCRegisterClasses[ARM::DPairSpcRegClassID];
4931       FirstReg = MRI->getMatchingSuperReg(FirstReg, ARM::dsub_0, RC);
4932     }
4933     auto Create = (LaneKind == NoLanes ? ARMOperand::CreateVectorList :
4934                    ARMOperand::CreateVectorListAllLanes);
4935     Operands.push_back(Create(FirstReg, Count, (Spacing == 2), S, E));
4936     break;
4937   }
4938   case IndexedLane:
4939     Operands.push_back(ARMOperand::CreateVectorListIndexed(FirstReg, Count,
4940                                                            LaneIndex,
4941                                                            (Spacing == 2),
4942                                                            S, E));
4943     break;
4944   }
4945   return MatchOperand_Success;
4946 }
4947 
4948 /// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
4949 OperandMatchResultTy
4950 ARMAsmParser::parseMemBarrierOptOperand(OperandVector &Operands) {
4951   MCAsmParser &Parser = getParser();
4952   SMLoc S = Parser.getTok().getLoc();
4953   const AsmToken &Tok = Parser.getTok();
4954   unsigned Opt;
4955 
4956   if (Tok.is(AsmToken::Identifier)) {
4957     StringRef OptStr = Tok.getString();
4958 
4959     Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()).lower())
4960       .Case("sy",    ARM_MB::SY)
4961       .Case("st",    ARM_MB::ST)
4962       .Case("ld",    ARM_MB::LD)
4963       .Case("sh",    ARM_MB::ISH)
4964       .Case("ish",   ARM_MB::ISH)
4965       .Case("shst",  ARM_MB::ISHST)
4966       .Case("ishst", ARM_MB::ISHST)
4967       .Case("ishld", ARM_MB::ISHLD)
4968       .Case("nsh",   ARM_MB::NSH)
4969       .Case("un",    ARM_MB::NSH)
4970       .Case("nshst", ARM_MB::NSHST)
4971       .Case("nshld", ARM_MB::NSHLD)
4972       .Case("unst",  ARM_MB::NSHST)
4973       .Case("osh",   ARM_MB::OSH)
4974       .Case("oshst", ARM_MB::OSHST)
4975       .Case("oshld", ARM_MB::OSHLD)
4976       .Default(~0U);
4977 
4978     // ishld, oshld, nshld and ld are only available from ARMv8.
4979     if (!hasV8Ops() && (Opt == ARM_MB::ISHLD || Opt == ARM_MB::OSHLD ||
4980                         Opt == ARM_MB::NSHLD || Opt == ARM_MB::LD))
4981       Opt = ~0U;
4982 
4983     if (Opt == ~0U)
4984       return MatchOperand_NoMatch;
4985 
4986     Parser.Lex(); // Eat identifier token.
4987   } else if (Tok.is(AsmToken::Hash) ||
4988              Tok.is(AsmToken::Dollar) ||
4989              Tok.is(AsmToken::Integer)) {
4990     if (Parser.getTok().isNot(AsmToken::Integer))
4991       Parser.Lex(); // Eat '#' or '$'.
4992     SMLoc Loc = Parser.getTok().getLoc();
4993 
4994     const MCExpr *MemBarrierID;
4995     if (getParser().parseExpression(MemBarrierID)) {
4996       Error(Loc, "illegal expression");
4997       return MatchOperand_ParseFail;
4998     }
4999 
5000     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(MemBarrierID);
5001     if (!CE) {
5002       Error(Loc, "constant expression expected");
5003       return MatchOperand_ParseFail;
5004     }
5005 
5006     int Val = CE->getValue();
5007     if (Val & ~0xf) {
5008       Error(Loc, "immediate value out of range");
5009       return MatchOperand_ParseFail;
5010     }
5011 
5012     Opt = ARM_MB::RESERVED_0 + Val;
5013   } else
5014     return MatchOperand_ParseFail;
5015 
5016   Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
5017   return MatchOperand_Success;
5018 }
5019 
5020 OperandMatchResultTy
5021 ARMAsmParser::parseTraceSyncBarrierOptOperand(OperandVector &Operands) {
5022   MCAsmParser &Parser = getParser();
5023   SMLoc S = Parser.getTok().getLoc();
5024   const AsmToken &Tok = Parser.getTok();
5025 
5026   if (Tok.isNot(AsmToken::Identifier))
5027      return MatchOperand_NoMatch;
5028 
5029   if (!Tok.getString().equals_insensitive("csync"))
5030     return MatchOperand_NoMatch;
5031 
5032   Parser.Lex(); // Eat identifier token.
5033 
5034   Operands.push_back(ARMOperand::CreateTraceSyncBarrierOpt(ARM_TSB::CSYNC, S));
5035   return MatchOperand_Success;
5036 }
5037 
5038 /// parseInstSyncBarrierOptOperand - Try to parse ISB inst sync barrier options.
5039 OperandMatchResultTy
5040 ARMAsmParser::parseInstSyncBarrierOptOperand(OperandVector &Operands) {
5041   MCAsmParser &Parser = getParser();
5042   SMLoc S = Parser.getTok().getLoc();
5043   const AsmToken &Tok = Parser.getTok();
5044   unsigned Opt;
5045 
5046   if (Tok.is(AsmToken::Identifier)) {
5047     StringRef OptStr = Tok.getString();
5048 
5049     if (OptStr.equals_insensitive("sy"))
5050       Opt = ARM_ISB::SY;
5051     else
5052       return MatchOperand_NoMatch;
5053 
5054     Parser.Lex(); // Eat identifier token.
5055   } else if (Tok.is(AsmToken::Hash) ||
5056              Tok.is(AsmToken::Dollar) ||
5057              Tok.is(AsmToken::Integer)) {
5058     if (Parser.getTok().isNot(AsmToken::Integer))
5059       Parser.Lex(); // Eat '#' or '$'.
5060     SMLoc Loc = Parser.getTok().getLoc();
5061 
5062     const MCExpr *ISBarrierID;
5063     if (getParser().parseExpression(ISBarrierID)) {
5064       Error(Loc, "illegal expression");
5065       return MatchOperand_ParseFail;
5066     }
5067 
5068     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ISBarrierID);
5069     if (!CE) {
5070       Error(Loc, "constant expression expected");
5071       return MatchOperand_ParseFail;
5072     }
5073 
5074     int Val = CE->getValue();
5075     if (Val & ~0xf) {
5076       Error(Loc, "immediate value out of range");
5077       return MatchOperand_ParseFail;
5078     }
5079 
5080     Opt = ARM_ISB::RESERVED_0 + Val;
5081   } else
5082     return MatchOperand_ParseFail;
5083 
5084   Operands.push_back(ARMOperand::CreateInstSyncBarrierOpt(
5085           (ARM_ISB::InstSyncBOpt)Opt, S));
5086   return MatchOperand_Success;
5087 }
5088 
5089 
5090 /// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
5091 OperandMatchResultTy
5092 ARMAsmParser::parseProcIFlagsOperand(OperandVector &Operands) {
5093   MCAsmParser &Parser = getParser();
5094   SMLoc S = Parser.getTok().getLoc();
5095   const AsmToken &Tok = Parser.getTok();
5096   if (!Tok.is(AsmToken::Identifier))
5097     return MatchOperand_NoMatch;
5098   StringRef IFlagsStr = Tok.getString();
5099 
5100   // An iflags string of "none" is interpreted to mean that none of the AIF
5101   // bits are set.  Not a terribly useful instruction, but a valid encoding.
5102   unsigned IFlags = 0;
5103   if (IFlagsStr != "none") {
5104         for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
5105       unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1).lower())
5106         .Case("a", ARM_PROC::A)
5107         .Case("i", ARM_PROC::I)
5108         .Case("f", ARM_PROC::F)
5109         .Default(~0U);
5110 
5111       // If some specific iflag is already set, it means that some letter is
5112       // present more than once, this is not acceptable.
5113       if (Flag == ~0U || (IFlags & Flag))
5114         return MatchOperand_NoMatch;
5115 
5116       IFlags |= Flag;
5117     }
5118   }
5119 
5120   Parser.Lex(); // Eat identifier token.
5121   Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
5122   return MatchOperand_Success;
5123 }
5124 
5125 /// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
5126 OperandMatchResultTy
5127 ARMAsmParser::parseMSRMaskOperand(OperandVector &Operands) {
5128   MCAsmParser &Parser = getParser();
5129   SMLoc S = Parser.getTok().getLoc();
5130   const AsmToken &Tok = Parser.getTok();
5131 
5132   if (Tok.is(AsmToken::Integer)) {
5133     int64_t Val = Tok.getIntVal();
5134     if (Val > 255 || Val < 0) {
5135       return MatchOperand_NoMatch;
5136     }
5137     unsigned SYSmvalue = Val & 0xFF;
5138     Parser.Lex();
5139     Operands.push_back(ARMOperand::CreateMSRMask(SYSmvalue, S));
5140     return MatchOperand_Success;
5141   }
5142 
5143   if (!Tok.is(AsmToken::Identifier))
5144     return MatchOperand_NoMatch;
5145   StringRef Mask = Tok.getString();
5146 
5147   if (isMClass()) {
5148     auto TheReg = ARMSysReg::lookupMClassSysRegByName(Mask.lower());
5149     if (!TheReg || !TheReg->hasRequiredFeatures(getSTI().getFeatureBits()))
5150       return MatchOperand_NoMatch;
5151 
5152     unsigned SYSmvalue = TheReg->Encoding & 0xFFF;
5153 
5154     Parser.Lex(); // Eat identifier token.
5155     Operands.push_back(ARMOperand::CreateMSRMask(SYSmvalue, S));
5156     return MatchOperand_Success;
5157   }
5158 
5159   // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
5160   size_t Start = 0, Next = Mask.find('_');
5161   StringRef Flags = "";
5162   std::string SpecReg = Mask.slice(Start, Next).lower();
5163   if (Next != StringRef::npos)
5164     Flags = Mask.slice(Next+1, Mask.size());
5165 
5166   // FlagsVal contains the complete mask:
5167   // 3-0: Mask
5168   // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
5169   unsigned FlagsVal = 0;
5170 
5171   if (SpecReg == "apsr") {
5172     FlagsVal = StringSwitch<unsigned>(Flags)
5173     .Case("nzcvq",  0x8) // same as CPSR_f
5174     .Case("g",      0x4) // same as CPSR_s
5175     .Case("nzcvqg", 0xc) // same as CPSR_fs
5176     .Default(~0U);
5177 
5178     if (FlagsVal == ~0U) {
5179       if (!Flags.empty())
5180         return MatchOperand_NoMatch;
5181       else
5182         FlagsVal = 8; // No flag
5183     }
5184   } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
5185     // cpsr_all is an alias for cpsr_fc, as is plain cpsr.
5186     if (Flags == "all" || Flags == "")
5187       Flags = "fc";
5188     for (int i = 0, e = Flags.size(); i != e; ++i) {
5189       unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
5190       .Case("c", 1)
5191       .Case("x", 2)
5192       .Case("s", 4)
5193       .Case("f", 8)
5194       .Default(~0U);
5195 
5196       // If some specific flag is already set, it means that some letter is
5197       // present more than once, this is not acceptable.
5198       if (Flag == ~0U || (FlagsVal & Flag))
5199         return MatchOperand_NoMatch;
5200       FlagsVal |= Flag;
5201     }
5202   } else // No match for special register.
5203     return MatchOperand_NoMatch;
5204 
5205   // Special register without flags is NOT equivalent to "fc" flags.
5206   // NOTE: This is a divergence from gas' behavior.  Uncommenting the following
5207   // two lines would enable gas compatibility at the expense of breaking
5208   // round-tripping.
5209   //
5210   // if (!FlagsVal)
5211   //  FlagsVal = 0x9;
5212 
5213   // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
5214   if (SpecReg == "spsr")
5215     FlagsVal |= 16;
5216 
5217   Parser.Lex(); // Eat identifier token.
5218   Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
5219   return MatchOperand_Success;
5220 }
5221 
5222 /// parseBankedRegOperand - Try to parse a banked register (e.g. "lr_irq") for
5223 /// use in the MRS/MSR instructions added to support virtualization.
5224 OperandMatchResultTy
5225 ARMAsmParser::parseBankedRegOperand(OperandVector &Operands) {
5226   MCAsmParser &Parser = getParser();
5227   SMLoc S = Parser.getTok().getLoc();
5228   const AsmToken &Tok = Parser.getTok();
5229   if (!Tok.is(AsmToken::Identifier))
5230     return MatchOperand_NoMatch;
5231   StringRef RegName = Tok.getString();
5232 
5233   auto TheReg = ARMBankedReg::lookupBankedRegByName(RegName.lower());
5234   if (!TheReg)
5235     return MatchOperand_NoMatch;
5236   unsigned Encoding = TheReg->Encoding;
5237 
5238   Parser.Lex(); // Eat identifier token.
5239   Operands.push_back(ARMOperand::CreateBankedReg(Encoding, S));
5240   return MatchOperand_Success;
5241 }
5242 
5243 OperandMatchResultTy
5244 ARMAsmParser::parsePKHImm(OperandVector &Operands, StringRef Op, int Low,
5245                           int High) {
5246   MCAsmParser &Parser = getParser();
5247   const AsmToken &Tok = Parser.getTok();
5248   if (Tok.isNot(AsmToken::Identifier)) {
5249     Error(Parser.getTok().getLoc(), Op + " operand expected.");
5250     return MatchOperand_ParseFail;
5251   }
5252   StringRef ShiftName = Tok.getString();
5253   std::string LowerOp = Op.lower();
5254   std::string UpperOp = Op.upper();
5255   if (ShiftName != LowerOp && ShiftName != UpperOp) {
5256     Error(Parser.getTok().getLoc(), Op + " operand expected.");
5257     return MatchOperand_ParseFail;
5258   }
5259   Parser.Lex(); // Eat shift type token.
5260 
5261   // There must be a '#' and a shift amount.
5262   if (Parser.getTok().isNot(AsmToken::Hash) &&
5263       Parser.getTok().isNot(AsmToken::Dollar)) {
5264     Error(Parser.getTok().getLoc(), "'#' expected");
5265     return MatchOperand_ParseFail;
5266   }
5267   Parser.Lex(); // Eat hash token.
5268 
5269   const MCExpr *ShiftAmount;
5270   SMLoc Loc = Parser.getTok().getLoc();
5271   SMLoc EndLoc;
5272   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
5273     Error(Loc, "illegal expression");
5274     return MatchOperand_ParseFail;
5275   }
5276   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
5277   if (!CE) {
5278     Error(Loc, "constant expression expected");
5279     return MatchOperand_ParseFail;
5280   }
5281   int Val = CE->getValue();
5282   if (Val < Low || Val > High) {
5283     Error(Loc, "immediate value out of range");
5284     return MatchOperand_ParseFail;
5285   }
5286 
5287   Operands.push_back(ARMOperand::CreateImm(CE, Loc, EndLoc));
5288 
5289   return MatchOperand_Success;
5290 }
5291 
5292 OperandMatchResultTy
5293 ARMAsmParser::parseSetEndImm(OperandVector &Operands) {
5294   MCAsmParser &Parser = getParser();
5295   const AsmToken &Tok = Parser.getTok();
5296   SMLoc S = Tok.getLoc();
5297   if (Tok.isNot(AsmToken::Identifier)) {
5298     Error(S, "'be' or 'le' operand expected");
5299     return MatchOperand_ParseFail;
5300   }
5301   int Val = StringSwitch<int>(Tok.getString().lower())
5302     .Case("be", 1)
5303     .Case("le", 0)
5304     .Default(-1);
5305   Parser.Lex(); // Eat the token.
5306 
5307   if (Val == -1) {
5308     Error(S, "'be' or 'le' operand expected");
5309     return MatchOperand_ParseFail;
5310   }
5311   Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::create(Val,
5312                                                                   getContext()),
5313                                            S, Tok.getEndLoc()));
5314   return MatchOperand_Success;
5315 }
5316 
5317 /// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
5318 /// instructions. Legal values are:
5319 ///     lsl #n  'n' in [0,31]
5320 ///     asr #n  'n' in [1,32]
5321 ///             n == 32 encoded as n == 0.
5322 OperandMatchResultTy
5323 ARMAsmParser::parseShifterImm(OperandVector &Operands) {
5324   MCAsmParser &Parser = getParser();
5325   const AsmToken &Tok = Parser.getTok();
5326   SMLoc S = Tok.getLoc();
5327   if (Tok.isNot(AsmToken::Identifier)) {
5328     Error(S, "shift operator 'asr' or 'lsl' expected");
5329     return MatchOperand_ParseFail;
5330   }
5331   StringRef ShiftName = Tok.getString();
5332   bool isASR;
5333   if (ShiftName == "lsl" || ShiftName == "LSL")
5334     isASR = false;
5335   else if (ShiftName == "asr" || ShiftName == "ASR")
5336     isASR = true;
5337   else {
5338     Error(S, "shift operator 'asr' or 'lsl' expected");
5339     return MatchOperand_ParseFail;
5340   }
5341   Parser.Lex(); // Eat the operator.
5342 
5343   // A '#' and a shift amount.
5344   if (Parser.getTok().isNot(AsmToken::Hash) &&
5345       Parser.getTok().isNot(AsmToken::Dollar)) {
5346     Error(Parser.getTok().getLoc(), "'#' expected");
5347     return MatchOperand_ParseFail;
5348   }
5349   Parser.Lex(); // Eat hash token.
5350   SMLoc ExLoc = Parser.getTok().getLoc();
5351 
5352   const MCExpr *ShiftAmount;
5353   SMLoc EndLoc;
5354   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
5355     Error(ExLoc, "malformed shift expression");
5356     return MatchOperand_ParseFail;
5357   }
5358   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
5359   if (!CE) {
5360     Error(ExLoc, "shift amount must be an immediate");
5361     return MatchOperand_ParseFail;
5362   }
5363 
5364   int64_t Val = CE->getValue();
5365   if (isASR) {
5366     // Shift amount must be in [1,32]
5367     if (Val < 1 || Val > 32) {
5368       Error(ExLoc, "'asr' shift amount must be in range [1,32]");
5369       return MatchOperand_ParseFail;
5370     }
5371     // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
5372     if (isThumb() && Val == 32) {
5373       Error(ExLoc, "'asr #32' shift amount not allowed in Thumb mode");
5374       return MatchOperand_ParseFail;
5375     }
5376     if (Val == 32) Val = 0;
5377   } else {
5378     // Shift amount must be in [1,32]
5379     if (Val < 0 || Val > 31) {
5380       Error(ExLoc, "'lsr' shift amount must be in range [0,31]");
5381       return MatchOperand_ParseFail;
5382     }
5383   }
5384 
5385   Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, EndLoc));
5386 
5387   return MatchOperand_Success;
5388 }
5389 
5390 /// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
5391 /// of instructions. Legal values are:
5392 ///     ror #n  'n' in {0, 8, 16, 24}
5393 OperandMatchResultTy
5394 ARMAsmParser::parseRotImm(OperandVector &Operands) {
5395   MCAsmParser &Parser = getParser();
5396   const AsmToken &Tok = Parser.getTok();
5397   SMLoc S = Tok.getLoc();
5398   if (Tok.isNot(AsmToken::Identifier))
5399     return MatchOperand_NoMatch;
5400   StringRef ShiftName = Tok.getString();
5401   if (ShiftName != "ror" && ShiftName != "ROR")
5402     return MatchOperand_NoMatch;
5403   Parser.Lex(); // Eat the operator.
5404 
5405   // A '#' and a rotate amount.
5406   if (Parser.getTok().isNot(AsmToken::Hash) &&
5407       Parser.getTok().isNot(AsmToken::Dollar)) {
5408     Error(Parser.getTok().getLoc(), "'#' expected");
5409     return MatchOperand_ParseFail;
5410   }
5411   Parser.Lex(); // Eat hash token.
5412   SMLoc ExLoc = Parser.getTok().getLoc();
5413 
5414   const MCExpr *ShiftAmount;
5415   SMLoc EndLoc;
5416   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
5417     Error(ExLoc, "malformed rotate expression");
5418     return MatchOperand_ParseFail;
5419   }
5420   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
5421   if (!CE) {
5422     Error(ExLoc, "rotate amount must be an immediate");
5423     return MatchOperand_ParseFail;
5424   }
5425 
5426   int64_t Val = CE->getValue();
5427   // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
5428   // normally, zero is represented in asm by omitting the rotate operand
5429   // entirely.
5430   if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
5431     Error(ExLoc, "'ror' rotate amount must be 8, 16, or 24");
5432     return MatchOperand_ParseFail;
5433   }
5434 
5435   Operands.push_back(ARMOperand::CreateRotImm(Val, S, EndLoc));
5436 
5437   return MatchOperand_Success;
5438 }
5439 
5440 OperandMatchResultTy
5441 ARMAsmParser::parseModImm(OperandVector &Operands) {
5442   MCAsmParser &Parser = getParser();
5443   MCAsmLexer &Lexer = getLexer();
5444   int64_t Imm1, Imm2;
5445 
5446   SMLoc S = Parser.getTok().getLoc();
5447 
5448   // 1) A mod_imm operand can appear in the place of a register name:
5449   //   add r0, #mod_imm
5450   //   add r0, r0, #mod_imm
5451   // to correctly handle the latter, we bail out as soon as we see an
5452   // identifier.
5453   //
5454   // 2) Similarly, we do not want to parse into complex operands:
5455   //   mov r0, #mod_imm
5456   //   mov r0, :lower16:(_foo)
5457   if (Parser.getTok().is(AsmToken::Identifier) ||
5458       Parser.getTok().is(AsmToken::Colon))
5459     return MatchOperand_NoMatch;
5460 
5461   // Hash (dollar) is optional as per the ARMARM
5462   if (Parser.getTok().is(AsmToken::Hash) ||
5463       Parser.getTok().is(AsmToken::Dollar)) {
5464     // Avoid parsing into complex operands (#:)
5465     if (Lexer.peekTok().is(AsmToken::Colon))
5466       return MatchOperand_NoMatch;
5467 
5468     // Eat the hash (dollar)
5469     Parser.Lex();
5470   }
5471 
5472   SMLoc Sx1, Ex1;
5473   Sx1 = Parser.getTok().getLoc();
5474   const MCExpr *Imm1Exp;
5475   if (getParser().parseExpression(Imm1Exp, Ex1)) {
5476     Error(Sx1, "malformed expression");
5477     return MatchOperand_ParseFail;
5478   }
5479 
5480   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm1Exp);
5481 
5482   if (CE) {
5483     // Immediate must fit within 32-bits
5484     Imm1 = CE->getValue();
5485     int Enc = ARM_AM::getSOImmVal(Imm1);
5486     if (Enc != -1 && Parser.getTok().is(AsmToken::EndOfStatement)) {
5487       // We have a match!
5488       Operands.push_back(ARMOperand::CreateModImm((Enc & 0xFF),
5489                                                   (Enc & 0xF00) >> 7,
5490                                                   Sx1, Ex1));
5491       return MatchOperand_Success;
5492     }
5493 
5494     // We have parsed an immediate which is not for us, fallback to a plain
5495     // immediate. This can happen for instruction aliases. For an example,
5496     // ARMInstrInfo.td defines the alias [mov <-> mvn] which can transform
5497     // a mov (mvn) with a mod_imm_neg/mod_imm_not operand into the opposite
5498     // instruction with a mod_imm operand. The alias is defined such that the
5499     // parser method is shared, that's why we have to do this here.
5500     if (Parser.getTok().is(AsmToken::EndOfStatement)) {
5501       Operands.push_back(ARMOperand::CreateImm(Imm1Exp, Sx1, Ex1));
5502       return MatchOperand_Success;
5503     }
5504   } else {
5505     // Operands like #(l1 - l2) can only be evaluated at a later stage (via an
5506     // MCFixup). Fallback to a plain immediate.
5507     Operands.push_back(ARMOperand::CreateImm(Imm1Exp, Sx1, Ex1));
5508     return MatchOperand_Success;
5509   }
5510 
5511   // From this point onward, we expect the input to be a (#bits, #rot) pair
5512   if (Parser.getTok().isNot(AsmToken::Comma)) {
5513     Error(Sx1, "expected modified immediate operand: #[0, 255], #even[0-30]");
5514     return MatchOperand_ParseFail;
5515   }
5516 
5517   if (Imm1 & ~0xFF) {
5518     Error(Sx1, "immediate operand must a number in the range [0, 255]");
5519     return MatchOperand_ParseFail;
5520   }
5521 
5522   // Eat the comma
5523   Parser.Lex();
5524 
5525   // Repeat for #rot
5526   SMLoc Sx2, Ex2;
5527   Sx2 = Parser.getTok().getLoc();
5528 
5529   // Eat the optional hash (dollar)
5530   if (Parser.getTok().is(AsmToken::Hash) ||
5531       Parser.getTok().is(AsmToken::Dollar))
5532     Parser.Lex();
5533 
5534   const MCExpr *Imm2Exp;
5535   if (getParser().parseExpression(Imm2Exp, Ex2)) {
5536     Error(Sx2, "malformed expression");
5537     return MatchOperand_ParseFail;
5538   }
5539 
5540   CE = dyn_cast<MCConstantExpr>(Imm2Exp);
5541 
5542   if (CE) {
5543     Imm2 = CE->getValue();
5544     if (!(Imm2 & ~0x1E)) {
5545       // We have a match!
5546       Operands.push_back(ARMOperand::CreateModImm(Imm1, Imm2, S, Ex2));
5547       return MatchOperand_Success;
5548     }
5549     Error(Sx2, "immediate operand must an even number in the range [0, 30]");
5550     return MatchOperand_ParseFail;
5551   } else {
5552     Error(Sx2, "constant expression expected");
5553     return MatchOperand_ParseFail;
5554   }
5555 }
5556 
5557 OperandMatchResultTy
5558 ARMAsmParser::parseBitfield(OperandVector &Operands) {
5559   MCAsmParser &Parser = getParser();
5560   SMLoc S = Parser.getTok().getLoc();
5561   // The bitfield descriptor is really two operands, the LSB and the width.
5562   if (Parser.getTok().isNot(AsmToken::Hash) &&
5563       Parser.getTok().isNot(AsmToken::Dollar)) {
5564     Error(Parser.getTok().getLoc(), "'#' expected");
5565     return MatchOperand_ParseFail;
5566   }
5567   Parser.Lex(); // Eat hash token.
5568 
5569   const MCExpr *LSBExpr;
5570   SMLoc E = Parser.getTok().getLoc();
5571   if (getParser().parseExpression(LSBExpr)) {
5572     Error(E, "malformed immediate expression");
5573     return MatchOperand_ParseFail;
5574   }
5575   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
5576   if (!CE) {
5577     Error(E, "'lsb' operand must be an immediate");
5578     return MatchOperand_ParseFail;
5579   }
5580 
5581   int64_t LSB = CE->getValue();
5582   // The LSB must be in the range [0,31]
5583   if (LSB < 0 || LSB > 31) {
5584     Error(E, "'lsb' operand must be in the range [0,31]");
5585     return MatchOperand_ParseFail;
5586   }
5587   E = Parser.getTok().getLoc();
5588 
5589   // Expect another immediate operand.
5590   if (Parser.getTok().isNot(AsmToken::Comma)) {
5591     Error(Parser.getTok().getLoc(), "too few operands");
5592     return MatchOperand_ParseFail;
5593   }
5594   Parser.Lex(); // Eat hash token.
5595   if (Parser.getTok().isNot(AsmToken::Hash) &&
5596       Parser.getTok().isNot(AsmToken::Dollar)) {
5597     Error(Parser.getTok().getLoc(), "'#' expected");
5598     return MatchOperand_ParseFail;
5599   }
5600   Parser.Lex(); // Eat hash token.
5601 
5602   const MCExpr *WidthExpr;
5603   SMLoc EndLoc;
5604   if (getParser().parseExpression(WidthExpr, EndLoc)) {
5605     Error(E, "malformed immediate expression");
5606     return MatchOperand_ParseFail;
5607   }
5608   CE = dyn_cast<MCConstantExpr>(WidthExpr);
5609   if (!CE) {
5610     Error(E, "'width' operand must be an immediate");
5611     return MatchOperand_ParseFail;
5612   }
5613 
5614   int64_t Width = CE->getValue();
5615   // The LSB must be in the range [1,32-lsb]
5616   if (Width < 1 || Width > 32 - LSB) {
5617     Error(E, "'width' operand must be in the range [1,32-lsb]");
5618     return MatchOperand_ParseFail;
5619   }
5620 
5621   Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, EndLoc));
5622 
5623   return MatchOperand_Success;
5624 }
5625 
5626 OperandMatchResultTy
5627 ARMAsmParser::parsePostIdxReg(OperandVector &Operands) {
5628   // Check for a post-index addressing register operand. Specifically:
5629   // postidx_reg := '+' register {, shift}
5630   //              | '-' register {, shift}
5631   //              | register {, shift}
5632 
5633   // This method must return MatchOperand_NoMatch without consuming any tokens
5634   // in the case where there is no match, as other alternatives take other
5635   // parse methods.
5636   MCAsmParser &Parser = getParser();
5637   AsmToken Tok = Parser.getTok();
5638   SMLoc S = Tok.getLoc();
5639   bool haveEaten = false;
5640   bool isAdd = true;
5641   if (Tok.is(AsmToken::Plus)) {
5642     Parser.Lex(); // Eat the '+' token.
5643     haveEaten = true;
5644   } else if (Tok.is(AsmToken::Minus)) {
5645     Parser.Lex(); // Eat the '-' token.
5646     isAdd = false;
5647     haveEaten = true;
5648   }
5649 
5650   SMLoc E = Parser.getTok().getEndLoc();
5651   int Reg = tryParseRegister();
5652   if (Reg == -1) {
5653     if (!haveEaten)
5654       return MatchOperand_NoMatch;
5655     Error(Parser.getTok().getLoc(), "register expected");
5656     return MatchOperand_ParseFail;
5657   }
5658 
5659   ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
5660   unsigned ShiftImm = 0;
5661   if (Parser.getTok().is(AsmToken::Comma)) {
5662     Parser.Lex(); // Eat the ','.
5663     if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
5664       return MatchOperand_ParseFail;
5665 
5666     // FIXME: Only approximates end...may include intervening whitespace.
5667     E = Parser.getTok().getLoc();
5668   }
5669 
5670   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
5671                                                   ShiftImm, S, E));
5672 
5673   return MatchOperand_Success;
5674 }
5675 
5676 OperandMatchResultTy
5677 ARMAsmParser::parseAM3Offset(OperandVector &Operands) {
5678   // Check for a post-index addressing register operand. Specifically:
5679   // am3offset := '+' register
5680   //              | '-' register
5681   //              | register
5682   //              | # imm
5683   //              | # + imm
5684   //              | # - imm
5685 
5686   // This method must return MatchOperand_NoMatch without consuming any tokens
5687   // in the case where there is no match, as other alternatives take other
5688   // parse methods.
5689   MCAsmParser &Parser = getParser();
5690   AsmToken Tok = Parser.getTok();
5691   SMLoc S = Tok.getLoc();
5692 
5693   // Do immediates first, as we always parse those if we have a '#'.
5694   if (Parser.getTok().is(AsmToken::Hash) ||
5695       Parser.getTok().is(AsmToken::Dollar)) {
5696     Parser.Lex(); // Eat '#' or '$'.
5697     // Explicitly look for a '-', as we need to encode negative zero
5698     // differently.
5699     bool isNegative = Parser.getTok().is(AsmToken::Minus);
5700     const MCExpr *Offset;
5701     SMLoc E;
5702     if (getParser().parseExpression(Offset, E))
5703       return MatchOperand_ParseFail;
5704     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
5705     if (!CE) {
5706       Error(S, "constant expression expected");
5707       return MatchOperand_ParseFail;
5708     }
5709     // Negative zero is encoded as the flag value
5710     // std::numeric_limits<int32_t>::min().
5711     int32_t Val = CE->getValue();
5712     if (isNegative && Val == 0)
5713       Val = std::numeric_limits<int32_t>::min();
5714 
5715     Operands.push_back(
5716       ARMOperand::CreateImm(MCConstantExpr::create(Val, getContext()), S, E));
5717 
5718     return MatchOperand_Success;
5719   }
5720 
5721   bool haveEaten = false;
5722   bool isAdd = true;
5723   if (Tok.is(AsmToken::Plus)) {
5724     Parser.Lex(); // Eat the '+' token.
5725     haveEaten = true;
5726   } else if (Tok.is(AsmToken::Minus)) {
5727     Parser.Lex(); // Eat the '-' token.
5728     isAdd = false;
5729     haveEaten = true;
5730   }
5731 
5732   Tok = Parser.getTok();
5733   int Reg = tryParseRegister();
5734   if (Reg == -1) {
5735     if (!haveEaten)
5736       return MatchOperand_NoMatch;
5737     Error(Tok.getLoc(), "register expected");
5738     return MatchOperand_ParseFail;
5739   }
5740 
5741   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
5742                                                   0, S, Tok.getEndLoc()));
5743 
5744   return MatchOperand_Success;
5745 }
5746 
5747 /// Convert parsed operands to MCInst.  Needed here because this instruction
5748 /// only has two register operands, but multiplication is commutative so
5749 /// assemblers should accept both "mul rD, rN, rD" and "mul rD, rD, rN".
5750 void ARMAsmParser::cvtThumbMultiply(MCInst &Inst,
5751                                     const OperandVector &Operands) {
5752   ((ARMOperand &)*Operands[3]).addRegOperands(Inst, 1);
5753   ((ARMOperand &)*Operands[1]).addCCOutOperands(Inst, 1);
5754   // If we have a three-operand form, make sure to set Rn to be the operand
5755   // that isn't the same as Rd.
5756   unsigned RegOp = 4;
5757   if (Operands.size() == 6 &&
5758       ((ARMOperand &)*Operands[4]).getReg() ==
5759           ((ARMOperand &)*Operands[3]).getReg())
5760     RegOp = 5;
5761   ((ARMOperand &)*Operands[RegOp]).addRegOperands(Inst, 1);
5762   Inst.addOperand(Inst.getOperand(0));
5763   ((ARMOperand &)*Operands[2]).addCondCodeOperands(Inst, 2);
5764 }
5765 
5766 void ARMAsmParser::cvtThumbBranches(MCInst &Inst,
5767                                     const OperandVector &Operands) {
5768   int CondOp = -1, ImmOp = -1;
5769   switch(Inst.getOpcode()) {
5770     case ARM::tB:
5771     case ARM::tBcc:  CondOp = 1; ImmOp = 2; break;
5772 
5773     case ARM::t2B:
5774     case ARM::t2Bcc: CondOp = 1; ImmOp = 3; break;
5775 
5776     default: llvm_unreachable("Unexpected instruction in cvtThumbBranches");
5777   }
5778   // first decide whether or not the branch should be conditional
5779   // by looking at it's location relative to an IT block
5780   if(inITBlock()) {
5781     // inside an IT block we cannot have any conditional branches. any
5782     // such instructions needs to be converted to unconditional form
5783     switch(Inst.getOpcode()) {
5784       case ARM::tBcc: Inst.setOpcode(ARM::tB); break;
5785       case ARM::t2Bcc: Inst.setOpcode(ARM::t2B); break;
5786     }
5787   } else {
5788     // outside IT blocks we can only have unconditional branches with AL
5789     // condition code or conditional branches with non-AL condition code
5790     unsigned Cond = static_cast<ARMOperand &>(*Operands[CondOp]).getCondCode();
5791     switch(Inst.getOpcode()) {
5792       case ARM::tB:
5793       case ARM::tBcc:
5794         Inst.setOpcode(Cond == ARMCC::AL ? ARM::tB : ARM::tBcc);
5795         break;
5796       case ARM::t2B:
5797       case ARM::t2Bcc:
5798         Inst.setOpcode(Cond == ARMCC::AL ? ARM::t2B : ARM::t2Bcc);
5799         break;
5800     }
5801   }
5802 
5803   // now decide on encoding size based on branch target range
5804   switch(Inst.getOpcode()) {
5805     // classify tB as either t2B or t1B based on range of immediate operand
5806     case ARM::tB: {
5807       ARMOperand &op = static_cast<ARMOperand &>(*Operands[ImmOp]);
5808       if (!op.isSignedOffset<11, 1>() && isThumb() && hasV8MBaseline())
5809         Inst.setOpcode(ARM::t2B);
5810       break;
5811     }
5812     // classify tBcc as either t2Bcc or t1Bcc based on range of immediate operand
5813     case ARM::tBcc: {
5814       ARMOperand &op = static_cast<ARMOperand &>(*Operands[ImmOp]);
5815       if (!op.isSignedOffset<8, 1>() && isThumb() && hasV8MBaseline())
5816         Inst.setOpcode(ARM::t2Bcc);
5817       break;
5818     }
5819   }
5820   ((ARMOperand &)*Operands[ImmOp]).addImmOperands(Inst, 1);
5821   ((ARMOperand &)*Operands[CondOp]).addCondCodeOperands(Inst, 2);
5822 }
5823 
5824 void ARMAsmParser::cvtMVEVMOVQtoDReg(
5825   MCInst &Inst, const OperandVector &Operands) {
5826 
5827   // mnemonic, condition code, Rt, Rt2, Qd, idx, Qd again, idx2
5828   assert(Operands.size() == 8);
5829 
5830   ((ARMOperand &)*Operands[2]).addRegOperands(Inst, 1); // Rt
5831   ((ARMOperand &)*Operands[3]).addRegOperands(Inst, 1); // Rt2
5832   ((ARMOperand &)*Operands[4]).addRegOperands(Inst, 1); // Qd
5833   ((ARMOperand &)*Operands[5]).addMVEPairVectorIndexOperands(Inst, 1); // idx
5834   // skip second copy of Qd in Operands[6]
5835   ((ARMOperand &)*Operands[7]).addMVEPairVectorIndexOperands(Inst, 1); // idx2
5836   ((ARMOperand &)*Operands[1]).addCondCodeOperands(Inst, 2); // condition code
5837 }
5838 
5839 /// Parse an ARM memory expression, return false if successful else return true
5840 /// or an error.  The first token must be a '[' when called.
5841 bool ARMAsmParser::parseMemory(OperandVector &Operands) {
5842   MCAsmParser &Parser = getParser();
5843   SMLoc S, E;
5844   if (Parser.getTok().isNot(AsmToken::LBrac))
5845     return TokError("Token is not a Left Bracket");
5846   S = Parser.getTok().getLoc();
5847   Parser.Lex(); // Eat left bracket token.
5848 
5849   const AsmToken &BaseRegTok = Parser.getTok();
5850   int BaseRegNum = tryParseRegister();
5851   if (BaseRegNum == -1)
5852     return Error(BaseRegTok.getLoc(), "register expected");
5853 
5854   // The next token must either be a comma, a colon or a closing bracket.
5855   const AsmToken &Tok = Parser.getTok();
5856   if (!Tok.is(AsmToken::Colon) && !Tok.is(AsmToken::Comma) &&
5857       !Tok.is(AsmToken::RBrac))
5858     return Error(Tok.getLoc(), "malformed memory operand");
5859 
5860   if (Tok.is(AsmToken::RBrac)) {
5861     E = Tok.getEndLoc();
5862     Parser.Lex(); // Eat right bracket token.
5863 
5864     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, 0,
5865                                              ARM_AM::no_shift, 0, 0, false,
5866                                              S, E));
5867 
5868     // If there's a pre-indexing writeback marker, '!', just add it as a token
5869     // operand. It's rather odd, but syntactically valid.
5870     if (Parser.getTok().is(AsmToken::Exclaim)) {
5871       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
5872       Parser.Lex(); // Eat the '!'.
5873     }
5874 
5875     return false;
5876   }
5877 
5878   assert((Tok.is(AsmToken::Colon) || Tok.is(AsmToken::Comma)) &&
5879          "Lost colon or comma in memory operand?!");
5880   if (Tok.is(AsmToken::Comma)) {
5881     Parser.Lex(); // Eat the comma.
5882   }
5883 
5884   // If we have a ':', it's an alignment specifier.
5885   if (Parser.getTok().is(AsmToken::Colon)) {
5886     Parser.Lex(); // Eat the ':'.
5887     E = Parser.getTok().getLoc();
5888     SMLoc AlignmentLoc = Tok.getLoc();
5889 
5890     const MCExpr *Expr;
5891     if (getParser().parseExpression(Expr))
5892      return true;
5893 
5894     // The expression has to be a constant. Memory references with relocations
5895     // don't come through here, as they use the <label> forms of the relevant
5896     // instructions.
5897     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
5898     if (!CE)
5899       return Error (E, "constant expression expected");
5900 
5901     unsigned Align = 0;
5902     switch (CE->getValue()) {
5903     default:
5904       return Error(E,
5905                    "alignment specifier must be 16, 32, 64, 128, or 256 bits");
5906     case 16:  Align = 2; break;
5907     case 32:  Align = 4; break;
5908     case 64:  Align = 8; break;
5909     case 128: Align = 16; break;
5910     case 256: Align = 32; break;
5911     }
5912 
5913     // Now we should have the closing ']'
5914     if (Parser.getTok().isNot(AsmToken::RBrac))
5915       return Error(Parser.getTok().getLoc(), "']' expected");
5916     E = Parser.getTok().getEndLoc();
5917     Parser.Lex(); // Eat right bracket token.
5918 
5919     // Don't worry about range checking the value here. That's handled by
5920     // the is*() predicates.
5921     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, 0,
5922                                              ARM_AM::no_shift, 0, Align,
5923                                              false, S, E, AlignmentLoc));
5924 
5925     // If there's a pre-indexing writeback marker, '!', just add it as a token
5926     // operand.
5927     if (Parser.getTok().is(AsmToken::Exclaim)) {
5928       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
5929       Parser.Lex(); // Eat the '!'.
5930     }
5931 
5932     return false;
5933   }
5934 
5935   // If we have a '#' or '$', it's an immediate offset, else assume it's a
5936   // register offset. Be friendly and also accept a plain integer or expression
5937   // (without a leading hash) for gas compatibility.
5938   if (Parser.getTok().is(AsmToken::Hash) ||
5939       Parser.getTok().is(AsmToken::Dollar) ||
5940       Parser.getTok().is(AsmToken::LParen) ||
5941       Parser.getTok().is(AsmToken::Integer)) {
5942     if (Parser.getTok().is(AsmToken::Hash) ||
5943         Parser.getTok().is(AsmToken::Dollar))
5944       Parser.Lex(); // Eat '#' or '$'
5945     E = Parser.getTok().getLoc();
5946 
5947     bool isNegative = getParser().getTok().is(AsmToken::Minus);
5948     const MCExpr *Offset, *AdjustedOffset;
5949     if (getParser().parseExpression(Offset))
5950      return true;
5951 
5952     if (const auto *CE = dyn_cast<MCConstantExpr>(Offset)) {
5953       // If the constant was #-0, represent it as
5954       // std::numeric_limits<int32_t>::min().
5955       int32_t Val = CE->getValue();
5956       if (isNegative && Val == 0)
5957         CE = MCConstantExpr::create(std::numeric_limits<int32_t>::min(),
5958                                     getContext());
5959       // Don't worry about range checking the value here. That's handled by
5960       // the is*() predicates.
5961       AdjustedOffset = CE;
5962     } else
5963       AdjustedOffset = Offset;
5964     Operands.push_back(ARMOperand::CreateMem(
5965         BaseRegNum, AdjustedOffset, 0, ARM_AM::no_shift, 0, 0, false, S, E));
5966 
5967     // Now we should have the closing ']'
5968     if (Parser.getTok().isNot(AsmToken::RBrac))
5969       return Error(Parser.getTok().getLoc(), "']' expected");
5970     E = Parser.getTok().getEndLoc();
5971     Parser.Lex(); // Eat right bracket token.
5972 
5973     // If there's a pre-indexing writeback marker, '!', just add it as a token
5974     // operand.
5975     if (Parser.getTok().is(AsmToken::Exclaim)) {
5976       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
5977       Parser.Lex(); // Eat the '!'.
5978     }
5979 
5980     return false;
5981   }
5982 
5983   // The register offset is optionally preceded by a '+' or '-'
5984   bool isNegative = false;
5985   if (Parser.getTok().is(AsmToken::Minus)) {
5986     isNegative = true;
5987     Parser.Lex(); // Eat the '-'.
5988   } else if (Parser.getTok().is(AsmToken::Plus)) {
5989     // Nothing to do.
5990     Parser.Lex(); // Eat the '+'.
5991   }
5992 
5993   E = Parser.getTok().getLoc();
5994   int OffsetRegNum = tryParseRegister();
5995   if (OffsetRegNum == -1)
5996     return Error(E, "register expected");
5997 
5998   // If there's a shift operator, handle it.
5999   ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
6000   unsigned ShiftImm = 0;
6001   if (Parser.getTok().is(AsmToken::Comma)) {
6002     Parser.Lex(); // Eat the ','.
6003     if (parseMemRegOffsetShift(ShiftType, ShiftImm))
6004       return true;
6005   }
6006 
6007   // Now we should have the closing ']'
6008   if (Parser.getTok().isNot(AsmToken::RBrac))
6009     return Error(Parser.getTok().getLoc(), "']' expected");
6010   E = Parser.getTok().getEndLoc();
6011   Parser.Lex(); // Eat right bracket token.
6012 
6013   Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, OffsetRegNum,
6014                                            ShiftType, ShiftImm, 0, isNegative,
6015                                            S, E));
6016 
6017   // If there's a pre-indexing writeback marker, '!', just add it as a token
6018   // operand.
6019   if (Parser.getTok().is(AsmToken::Exclaim)) {
6020     Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
6021     Parser.Lex(); // Eat the '!'.
6022   }
6023 
6024   return false;
6025 }
6026 
6027 /// parseMemRegOffsetShift - one of these two:
6028 ///   ( lsl | lsr | asr | ror ) , # shift_amount
6029 ///   rrx
6030 /// return true if it parses a shift otherwise it returns false.
6031 bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
6032                                           unsigned &Amount) {
6033   MCAsmParser &Parser = getParser();
6034   SMLoc Loc = Parser.getTok().getLoc();
6035   const AsmToken &Tok = Parser.getTok();
6036   if (Tok.isNot(AsmToken::Identifier))
6037     return Error(Loc, "illegal shift operator");
6038   StringRef ShiftName = Tok.getString();
6039   if (ShiftName == "lsl" || ShiftName == "LSL" ||
6040       ShiftName == "asl" || ShiftName == "ASL")
6041     St = ARM_AM::lsl;
6042   else if (ShiftName == "lsr" || ShiftName == "LSR")
6043     St = ARM_AM::lsr;
6044   else if (ShiftName == "asr" || ShiftName == "ASR")
6045     St = ARM_AM::asr;
6046   else if (ShiftName == "ror" || ShiftName == "ROR")
6047     St = ARM_AM::ror;
6048   else if (ShiftName == "rrx" || ShiftName == "RRX")
6049     St = ARM_AM::rrx;
6050   else if (ShiftName == "uxtw" || ShiftName == "UXTW")
6051     St = ARM_AM::uxtw;
6052   else
6053     return Error(Loc, "illegal shift operator");
6054   Parser.Lex(); // Eat shift type token.
6055 
6056   // rrx stands alone.
6057   Amount = 0;
6058   if (St != ARM_AM::rrx) {
6059     Loc = Parser.getTok().getLoc();
6060     // A '#' and a shift amount.
6061     const AsmToken &HashTok = Parser.getTok();
6062     if (HashTok.isNot(AsmToken::Hash) &&
6063         HashTok.isNot(AsmToken::Dollar))
6064       return Error(HashTok.getLoc(), "'#' expected");
6065     Parser.Lex(); // Eat hash token.
6066 
6067     const MCExpr *Expr;
6068     if (getParser().parseExpression(Expr))
6069       return true;
6070     // Range check the immediate.
6071     // lsl, ror: 0 <= imm <= 31
6072     // lsr, asr: 0 <= imm <= 32
6073     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
6074     if (!CE)
6075       return Error(Loc, "shift amount must be an immediate");
6076     int64_t Imm = CE->getValue();
6077     if (Imm < 0 ||
6078         ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
6079         ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
6080       return Error(Loc, "immediate shift value out of range");
6081     // If <ShiftTy> #0, turn it into a no_shift.
6082     if (Imm == 0)
6083       St = ARM_AM::lsl;
6084     // For consistency, treat lsr #32 and asr #32 as having immediate value 0.
6085     if (Imm == 32)
6086       Imm = 0;
6087     Amount = Imm;
6088   }
6089 
6090   return false;
6091 }
6092 
6093 /// parseFPImm - A floating point immediate expression operand.
6094 OperandMatchResultTy
6095 ARMAsmParser::parseFPImm(OperandVector &Operands) {
6096   MCAsmParser &Parser = getParser();
6097   // Anything that can accept a floating point constant as an operand
6098   // needs to go through here, as the regular parseExpression is
6099   // integer only.
6100   //
6101   // This routine still creates a generic Immediate operand, containing
6102   // a bitcast of the 64-bit floating point value. The various operands
6103   // that accept floats can check whether the value is valid for them
6104   // via the standard is*() predicates.
6105 
6106   SMLoc S = Parser.getTok().getLoc();
6107 
6108   if (Parser.getTok().isNot(AsmToken::Hash) &&
6109       Parser.getTok().isNot(AsmToken::Dollar))
6110     return MatchOperand_NoMatch;
6111 
6112   // Disambiguate the VMOV forms that can accept an FP immediate.
6113   // vmov.f32 <sreg>, #imm
6114   // vmov.f64 <dreg>, #imm
6115   // vmov.f32 <dreg>, #imm  @ vector f32x2
6116   // vmov.f32 <qreg>, #imm  @ vector f32x4
6117   //
6118   // There are also the NEON VMOV instructions which expect an
6119   // integer constant. Make sure we don't try to parse an FPImm
6120   // for these:
6121   // vmov.i{8|16|32|64} <dreg|qreg>, #imm
6122   ARMOperand &TyOp = static_cast<ARMOperand &>(*Operands[2]);
6123   bool isVmovf = TyOp.isToken() &&
6124                  (TyOp.getToken() == ".f32" || TyOp.getToken() == ".f64" ||
6125                   TyOp.getToken() == ".f16");
6126   ARMOperand &Mnemonic = static_cast<ARMOperand &>(*Operands[0]);
6127   bool isFconst = Mnemonic.isToken() && (Mnemonic.getToken() == "fconstd" ||
6128                                          Mnemonic.getToken() == "fconsts");
6129   if (!(isVmovf || isFconst))
6130     return MatchOperand_NoMatch;
6131 
6132   Parser.Lex(); // Eat '#' or '$'.
6133 
6134   // Handle negation, as that still comes through as a separate token.
6135   bool isNegative = false;
6136   if (Parser.getTok().is(AsmToken::Minus)) {
6137     isNegative = true;
6138     Parser.Lex();
6139   }
6140   const AsmToken &Tok = Parser.getTok();
6141   SMLoc Loc = Tok.getLoc();
6142   if (Tok.is(AsmToken::Real) && isVmovf) {
6143     APFloat RealVal(APFloat::IEEEsingle(), Tok.getString());
6144     uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
6145     // If we had a '-' in front, toggle the sign bit.
6146     IntVal ^= (uint64_t)isNegative << 31;
6147     Parser.Lex(); // Eat the token.
6148     Operands.push_back(ARMOperand::CreateImm(
6149           MCConstantExpr::create(IntVal, getContext()),
6150           S, Parser.getTok().getLoc()));
6151     return MatchOperand_Success;
6152   }
6153   // Also handle plain integers. Instructions which allow floating point
6154   // immediates also allow a raw encoded 8-bit value.
6155   if (Tok.is(AsmToken::Integer) && isFconst) {
6156     int64_t Val = Tok.getIntVal();
6157     Parser.Lex(); // Eat the token.
6158     if (Val > 255 || Val < 0) {
6159       Error(Loc, "encoded floating point value out of range");
6160       return MatchOperand_ParseFail;
6161     }
6162     float RealVal = ARM_AM::getFPImmFloat(Val);
6163     Val = APFloat(RealVal).bitcastToAPInt().getZExtValue();
6164 
6165     Operands.push_back(ARMOperand::CreateImm(
6166         MCConstantExpr::create(Val, getContext()), S,
6167         Parser.getTok().getLoc()));
6168     return MatchOperand_Success;
6169   }
6170 
6171   Error(Loc, "invalid floating point immediate");
6172   return MatchOperand_ParseFail;
6173 }
6174 
6175 /// Parse a arm instruction operand.  For now this parses the operand regardless
6176 /// of the mnemonic.
6177 bool ARMAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
6178   MCAsmParser &Parser = getParser();
6179   SMLoc S, E;
6180 
6181   // Check if the current operand has a custom associated parser, if so, try to
6182   // custom parse the operand, or fallback to the general approach.
6183   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
6184   if (ResTy == MatchOperand_Success)
6185     return false;
6186   // If there wasn't a custom match, try the generic matcher below. Otherwise,
6187   // there was a match, but an error occurred, in which case, just return that
6188   // the operand parsing failed.
6189   if (ResTy == MatchOperand_ParseFail)
6190     return true;
6191 
6192   switch (getLexer().getKind()) {
6193   default:
6194     Error(Parser.getTok().getLoc(), "unexpected token in operand");
6195     return true;
6196   case AsmToken::Identifier: {
6197     // If we've seen a branch mnemonic, the next operand must be a label.  This
6198     // is true even if the label is a register name.  So "br r1" means branch to
6199     // label "r1".
6200     bool ExpectLabel = Mnemonic == "b" || Mnemonic == "bl";
6201     if (!ExpectLabel) {
6202       if (!tryParseRegisterWithWriteBack(Operands))
6203         return false;
6204       int Res = tryParseShiftRegister(Operands);
6205       if (Res == 0) // success
6206         return false;
6207       else if (Res == -1) // irrecoverable error
6208         return true;
6209       // If this is VMRS, check for the apsr_nzcv operand.
6210       if (Mnemonic == "vmrs" &&
6211           Parser.getTok().getString().equals_insensitive("apsr_nzcv")) {
6212         S = Parser.getTok().getLoc();
6213         Parser.Lex();
6214         Operands.push_back(ARMOperand::CreateToken("APSR_nzcv", S));
6215         return false;
6216       }
6217     }
6218 
6219     // Fall though for the Identifier case that is not a register or a
6220     // special name.
6221     LLVM_FALLTHROUGH;
6222   }
6223   case AsmToken::LParen:  // parenthesized expressions like (_strcmp-4)
6224   case AsmToken::Integer: // things like 1f and 2b as a branch targets
6225   case AsmToken::String:  // quoted label names.
6226   case AsmToken::Dot: {   // . as a branch target
6227     // This was not a register so parse other operands that start with an
6228     // identifier (like labels) as expressions and create them as immediates.
6229     const MCExpr *IdVal;
6230     S = Parser.getTok().getLoc();
6231     if (getParser().parseExpression(IdVal))
6232       return true;
6233     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
6234     Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
6235     return false;
6236   }
6237   case AsmToken::LBrac:
6238     return parseMemory(Operands);
6239   case AsmToken::LCurly:
6240     return parseRegisterList(Operands, !Mnemonic.startswith("clr"));
6241   case AsmToken::Dollar:
6242   case AsmToken::Hash: {
6243     // #42 -> immediate
6244     // $ 42 -> immediate
6245     // $foo -> symbol name
6246     // $42 -> symbol name
6247     S = Parser.getTok().getLoc();
6248 
6249     // Favor the interpretation of $-prefixed operands as symbol names.
6250     // Cases where immediates are explicitly expected are handled by their
6251     // specific ParseMethod implementations.
6252     auto AdjacentToken = getLexer().peekTok(/*ShouldSkipSpace=*/false);
6253     bool ExpectIdentifier = Parser.getTok().is(AsmToken::Dollar) &&
6254                             (AdjacentToken.is(AsmToken::Identifier) ||
6255                              AdjacentToken.is(AsmToken::Integer));
6256     if (!ExpectIdentifier) {
6257       // Token is not part of identifier. Drop leading $ or # before parsing
6258       // expression.
6259       Parser.Lex();
6260     }
6261 
6262     if (Parser.getTok().isNot(AsmToken::Colon)) {
6263       bool IsNegative = Parser.getTok().is(AsmToken::Minus);
6264       const MCExpr *ImmVal;
6265       if (getParser().parseExpression(ImmVal))
6266         return true;
6267       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
6268       if (CE) {
6269         int32_t Val = CE->getValue();
6270         if (IsNegative && Val == 0)
6271           ImmVal = MCConstantExpr::create(std::numeric_limits<int32_t>::min(),
6272                                           getContext());
6273       }
6274       E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
6275       Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
6276 
6277       // There can be a trailing '!' on operands that we want as a separate
6278       // '!' Token operand. Handle that here. For example, the compatibility
6279       // alias for 'srsdb sp!, #imm' is 'srsdb #imm!'.
6280       if (Parser.getTok().is(AsmToken::Exclaim)) {
6281         Operands.push_back(ARMOperand::CreateToken(Parser.getTok().getString(),
6282                                                    Parser.getTok().getLoc()));
6283         Parser.Lex(); // Eat exclaim token
6284       }
6285       return false;
6286     }
6287     // w/ a ':' after the '#', it's just like a plain ':'.
6288     LLVM_FALLTHROUGH;
6289   }
6290   case AsmToken::Colon: {
6291     S = Parser.getTok().getLoc();
6292     // ":lower16:" and ":upper16:" expression prefixes
6293     // FIXME: Check it's an expression prefix,
6294     // e.g. (FOO - :lower16:BAR) isn't legal.
6295     ARMMCExpr::VariantKind RefKind;
6296     if (parsePrefix(RefKind))
6297       return true;
6298 
6299     const MCExpr *SubExprVal;
6300     if (getParser().parseExpression(SubExprVal))
6301       return true;
6302 
6303     const MCExpr *ExprVal = ARMMCExpr::create(RefKind, SubExprVal,
6304                                               getContext());
6305     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
6306     Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
6307     return false;
6308   }
6309   case AsmToken::Equal: {
6310     S = Parser.getTok().getLoc();
6311     if (Mnemonic != "ldr") // only parse for ldr pseudo (e.g. ldr r0, =val)
6312       return Error(S, "unexpected token in operand");
6313     Parser.Lex(); // Eat '='
6314     const MCExpr *SubExprVal;
6315     if (getParser().parseExpression(SubExprVal))
6316       return true;
6317     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
6318 
6319     // execute-only: we assume that assembly programmers know what they are
6320     // doing and allow literal pool creation here
6321     Operands.push_back(ARMOperand::CreateConstantPoolImm(SubExprVal, S, E));
6322     return false;
6323   }
6324   }
6325 }
6326 
6327 // parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
6328 //  :lower16: and :upper16:.
6329 bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
6330   MCAsmParser &Parser = getParser();
6331   RefKind = ARMMCExpr::VK_ARM_None;
6332 
6333   // consume an optional '#' (GNU compatibility)
6334   if (getLexer().is(AsmToken::Hash))
6335     Parser.Lex();
6336 
6337   // :lower16: and :upper16: modifiers
6338   assert(getLexer().is(AsmToken::Colon) && "expected a :");
6339   Parser.Lex(); // Eat ':'
6340 
6341   if (getLexer().isNot(AsmToken::Identifier)) {
6342     Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
6343     return true;
6344   }
6345 
6346   enum {
6347     COFF = (1 << MCContext::IsCOFF),
6348     ELF = (1 << MCContext::IsELF),
6349     MACHO = (1 << MCContext::IsMachO),
6350     WASM = (1 << MCContext::IsWasm),
6351   };
6352   static const struct PrefixEntry {
6353     const char *Spelling;
6354     ARMMCExpr::VariantKind VariantKind;
6355     uint8_t SupportedFormats;
6356   } PrefixEntries[] = {
6357     { "lower16", ARMMCExpr::VK_ARM_LO16, COFF | ELF | MACHO },
6358     { "upper16", ARMMCExpr::VK_ARM_HI16, COFF | ELF | MACHO },
6359   };
6360 
6361   StringRef IDVal = Parser.getTok().getIdentifier();
6362 
6363   const auto &Prefix =
6364       llvm::find_if(PrefixEntries, [&IDVal](const PrefixEntry &PE) {
6365         return PE.Spelling == IDVal;
6366       });
6367   if (Prefix == std::end(PrefixEntries)) {
6368     Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
6369     return true;
6370   }
6371 
6372   uint8_t CurrentFormat;
6373   switch (getContext().getObjectFileType()) {
6374   case MCContext::IsMachO:
6375     CurrentFormat = MACHO;
6376     break;
6377   case MCContext::IsELF:
6378     CurrentFormat = ELF;
6379     break;
6380   case MCContext::IsCOFF:
6381     CurrentFormat = COFF;
6382     break;
6383   case MCContext::IsWasm:
6384     CurrentFormat = WASM;
6385     break;
6386   case MCContext::IsGOFF:
6387   case MCContext::IsXCOFF:
6388     llvm_unreachable("unexpected object format");
6389     break;
6390   }
6391 
6392   if (~Prefix->SupportedFormats & CurrentFormat) {
6393     Error(Parser.getTok().getLoc(),
6394           "cannot represent relocation in the current file format");
6395     return true;
6396   }
6397 
6398   RefKind = Prefix->VariantKind;
6399   Parser.Lex();
6400 
6401   if (getLexer().isNot(AsmToken::Colon)) {
6402     Error(Parser.getTok().getLoc(), "unexpected token after prefix");
6403     return true;
6404   }
6405   Parser.Lex(); // Eat the last ':'
6406 
6407   return false;
6408 }
6409 
6410 /// Given a mnemonic, split out possible predication code and carry
6411 /// setting letters to form a canonical mnemonic and flags.
6412 //
6413 // FIXME: Would be nice to autogen this.
6414 // FIXME: This is a bit of a maze of special cases.
6415 StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
6416                                       StringRef ExtraToken,
6417                                       unsigned &PredicationCode,
6418                                       unsigned &VPTPredicationCode,
6419                                       bool &CarrySetting,
6420                                       unsigned &ProcessorIMod,
6421                                       StringRef &ITMask) {
6422   PredicationCode = ARMCC::AL;
6423   VPTPredicationCode = ARMVCC::None;
6424   CarrySetting = false;
6425   ProcessorIMod = 0;
6426 
6427   // Ignore some mnemonics we know aren't predicated forms.
6428   //
6429   // FIXME: Would be nice to autogen this.
6430   if ((Mnemonic == "movs" && isThumb()) ||
6431       Mnemonic == "teq"   || Mnemonic == "vceq"   || Mnemonic == "svc"   ||
6432       Mnemonic == "mls"   || Mnemonic == "smmls"  || Mnemonic == "vcls"  ||
6433       Mnemonic == "vmls"  || Mnemonic == "vnmls"  || Mnemonic == "vacge" ||
6434       Mnemonic == "vcge"  || Mnemonic == "vclt"   || Mnemonic == "vacgt" ||
6435       Mnemonic == "vaclt" || Mnemonic == "vacle"  || Mnemonic == "hlt" ||
6436       Mnemonic == "vcgt"  || Mnemonic == "vcle"   || Mnemonic == "smlal" ||
6437       Mnemonic == "umaal" || Mnemonic == "umlal"  || Mnemonic == "vabal" ||
6438       Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal" ||
6439       Mnemonic == "fmuls" || Mnemonic == "vmaxnm" || Mnemonic == "vminnm" ||
6440       Mnemonic == "vcvta" || Mnemonic == "vcvtn"  || Mnemonic == "vcvtp" ||
6441       Mnemonic == "vcvtm" || Mnemonic == "vrinta" || Mnemonic == "vrintn" ||
6442       Mnemonic == "vrintp" || Mnemonic == "vrintm" || Mnemonic == "hvc" ||
6443       Mnemonic.startswith("vsel") || Mnemonic == "vins" || Mnemonic == "vmovx" ||
6444       Mnemonic == "bxns"  || Mnemonic == "blxns" ||
6445       Mnemonic == "vdot"  || Mnemonic == "vmmla" ||
6446       Mnemonic == "vudot" || Mnemonic == "vsdot" ||
6447       Mnemonic == "vcmla" || Mnemonic == "vcadd" ||
6448       Mnemonic == "vfmal" || Mnemonic == "vfmsl" ||
6449       Mnemonic == "wls"   || Mnemonic == "le"    || Mnemonic == "dls" ||
6450       Mnemonic == "csel"  || Mnemonic == "csinc" ||
6451       Mnemonic == "csinv" || Mnemonic == "csneg" || Mnemonic == "cinc" ||
6452       Mnemonic == "cinv"  || Mnemonic == "cneg"  || Mnemonic == "cset" ||
6453       Mnemonic == "csetm" ||
6454       Mnemonic == "aut"   || Mnemonic == "pac" || Mnemonic == "pacbti" ||
6455       Mnemonic == "bti")
6456     return Mnemonic;
6457 
6458   // First, split out any predication code. Ignore mnemonics we know aren't
6459   // predicated but do have a carry-set and so weren't caught above.
6460   if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
6461       Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
6462       Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
6463       Mnemonic != "sbcs" && Mnemonic != "rscs" &&
6464       !(hasMVE() &&
6465         (Mnemonic == "vmine" ||
6466          Mnemonic == "vshle" || Mnemonic == "vshlt" || Mnemonic == "vshllt" ||
6467          Mnemonic == "vrshle" || Mnemonic == "vrshlt" ||
6468          Mnemonic == "vmvne" || Mnemonic == "vorne" ||
6469          Mnemonic == "vnege" || Mnemonic == "vnegt" ||
6470          Mnemonic == "vmule" || Mnemonic == "vmult" ||
6471          Mnemonic == "vrintne" ||
6472          Mnemonic == "vcmult" || Mnemonic == "vcmule" ||
6473          Mnemonic == "vpsele" || Mnemonic == "vpselt" ||
6474          Mnemonic.startswith("vq")))) {
6475     unsigned CC = ARMCondCodeFromString(Mnemonic.substr(Mnemonic.size()-2));
6476     if (CC != ~0U) {
6477       Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
6478       PredicationCode = CC;
6479     }
6480   }
6481 
6482   // Next, determine if we have a carry setting bit. We explicitly ignore all
6483   // the instructions we know end in 's'.
6484   if (Mnemonic.endswith("s") &&
6485       !(Mnemonic == "cps" || Mnemonic == "mls" ||
6486         Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
6487         Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
6488         Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
6489         Mnemonic == "vrsqrts" || Mnemonic == "srs" || Mnemonic == "flds" ||
6490         Mnemonic == "fmrs" || Mnemonic == "fsqrts" || Mnemonic == "fsubs" ||
6491         Mnemonic == "fsts" || Mnemonic == "fcpys" || Mnemonic == "fdivs" ||
6492         Mnemonic == "fmuls" || Mnemonic == "fcmps" || Mnemonic == "fcmpzs" ||
6493         Mnemonic == "vfms" || Mnemonic == "vfnms" || Mnemonic == "fconsts" ||
6494         Mnemonic == "bxns" || Mnemonic == "blxns" || Mnemonic == "vfmas" ||
6495         Mnemonic == "vmlas" ||
6496         (Mnemonic == "movs" && isThumb()))) {
6497     Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
6498     CarrySetting = true;
6499   }
6500 
6501   // The "cps" instruction can have a interrupt mode operand which is glued into
6502   // the mnemonic. Check if this is the case, split it and parse the imod op
6503   if (Mnemonic.startswith("cps")) {
6504     // Split out any imod code.
6505     unsigned IMod =
6506       StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
6507       .Case("ie", ARM_PROC::IE)
6508       .Case("id", ARM_PROC::ID)
6509       .Default(~0U);
6510     if (IMod != ~0U) {
6511       Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
6512       ProcessorIMod = IMod;
6513     }
6514   }
6515 
6516   if (isMnemonicVPTPredicable(Mnemonic, ExtraToken) && Mnemonic != "vmovlt" &&
6517       Mnemonic != "vshllt" && Mnemonic != "vrshrnt" && Mnemonic != "vshrnt" &&
6518       Mnemonic != "vqrshrunt" && Mnemonic != "vqshrunt" &&
6519       Mnemonic != "vqrshrnt" && Mnemonic != "vqshrnt" && Mnemonic != "vmullt" &&
6520       Mnemonic != "vqmovnt" && Mnemonic != "vqmovunt" &&
6521       Mnemonic != "vqmovnt" && Mnemonic != "vmovnt" && Mnemonic != "vqdmullt" &&
6522       Mnemonic != "vpnot" && Mnemonic != "vcvtt" && Mnemonic != "vcvt") {
6523     unsigned CC = ARMVectorCondCodeFromString(Mnemonic.substr(Mnemonic.size()-1));
6524     if (CC != ~0U) {
6525       Mnemonic = Mnemonic.slice(0, Mnemonic.size()-1);
6526       VPTPredicationCode = CC;
6527     }
6528     return Mnemonic;
6529   }
6530 
6531   // The "it" instruction has the condition mask on the end of the mnemonic.
6532   if (Mnemonic.startswith("it")) {
6533     ITMask = Mnemonic.slice(2, Mnemonic.size());
6534     Mnemonic = Mnemonic.slice(0, 2);
6535   }
6536 
6537   if (Mnemonic.startswith("vpst")) {
6538     ITMask = Mnemonic.slice(4, Mnemonic.size());
6539     Mnemonic = Mnemonic.slice(0, 4);
6540   }
6541   else if (Mnemonic.startswith("vpt")) {
6542     ITMask = Mnemonic.slice(3, Mnemonic.size());
6543     Mnemonic = Mnemonic.slice(0, 3);
6544   }
6545 
6546   return Mnemonic;
6547 }
6548 
6549 /// Given a canonical mnemonic, determine if the instruction ever allows
6550 /// inclusion of carry set or predication code operands.
6551 //
6552 // FIXME: It would be nice to autogen this.
6553 void ARMAsmParser::getMnemonicAcceptInfo(StringRef Mnemonic,
6554                                          StringRef ExtraToken,
6555                                          StringRef FullInst,
6556                                          bool &CanAcceptCarrySet,
6557                                          bool &CanAcceptPredicationCode,
6558                                          bool &CanAcceptVPTPredicationCode) {
6559   CanAcceptVPTPredicationCode = isMnemonicVPTPredicable(Mnemonic, ExtraToken);
6560 
6561   CanAcceptCarrySet =
6562       Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
6563       Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
6564       Mnemonic == "add" || Mnemonic == "adc" || Mnemonic == "mul" ||
6565       Mnemonic == "bic" || Mnemonic == "asr" || Mnemonic == "orr" ||
6566       Mnemonic == "mvn" || Mnemonic == "rsb" || Mnemonic == "rsc" ||
6567       Mnemonic == "orn" || Mnemonic == "sbc" || Mnemonic == "eor" ||
6568       Mnemonic == "neg" || Mnemonic == "vfm" || Mnemonic == "vfnm" ||
6569       (!isThumb() &&
6570        (Mnemonic == "smull" || Mnemonic == "mov" || Mnemonic == "mla" ||
6571         Mnemonic == "smlal" || Mnemonic == "umlal" || Mnemonic == "umull"));
6572 
6573   if (Mnemonic == "bkpt" || Mnemonic == "cbnz" || Mnemonic == "setend" ||
6574       Mnemonic == "cps" || Mnemonic == "it" || Mnemonic == "cbz" ||
6575       Mnemonic == "trap" || Mnemonic == "hlt" || Mnemonic == "udf" ||
6576       Mnemonic.startswith("crc32") || Mnemonic.startswith("cps") ||
6577       Mnemonic.startswith("vsel") || Mnemonic == "vmaxnm" ||
6578       Mnemonic == "vminnm" || Mnemonic == "vcvta" || Mnemonic == "vcvtn" ||
6579       Mnemonic == "vcvtp" || Mnemonic == "vcvtm" || Mnemonic == "vrinta" ||
6580       Mnemonic == "vrintn" || Mnemonic == "vrintp" || Mnemonic == "vrintm" ||
6581       Mnemonic.startswith("aes") || Mnemonic == "hvc" || Mnemonic == "setpan" ||
6582       Mnemonic.startswith("sha1") || Mnemonic.startswith("sha256") ||
6583       (FullInst.startswith("vmull") && FullInst.endswith(".p64")) ||
6584       Mnemonic == "vmovx" || Mnemonic == "vins" ||
6585       Mnemonic == "vudot" || Mnemonic == "vsdot" ||
6586       Mnemonic == "vcmla" || Mnemonic == "vcadd" ||
6587       Mnemonic == "vfmal" || Mnemonic == "vfmsl" ||
6588       Mnemonic == "vfmat" || Mnemonic == "vfmab" ||
6589       Mnemonic == "vdot"  || Mnemonic == "vmmla" ||
6590       Mnemonic == "sb"    || Mnemonic == "ssbb"  ||
6591       Mnemonic == "pssbb" || Mnemonic == "vsmmla" ||
6592       Mnemonic == "vummla" || Mnemonic == "vusmmla" ||
6593       Mnemonic == "vusdot" || Mnemonic == "vsudot" ||
6594       Mnemonic == "bfcsel" || Mnemonic == "wls" ||
6595       Mnemonic == "dls" || Mnemonic == "le" || Mnemonic == "csel" ||
6596       Mnemonic == "csinc" || Mnemonic == "csinv" || Mnemonic == "csneg" ||
6597       Mnemonic == "cinc" || Mnemonic == "cinv" || Mnemonic == "cneg" ||
6598       Mnemonic == "cset" || Mnemonic == "csetm" ||
6599       (hasCDE() && MS.isCDEInstr(Mnemonic) &&
6600        !MS.isITPredicableCDEInstr(Mnemonic)) ||
6601       Mnemonic.startswith("vpt") || Mnemonic.startswith("vpst") ||
6602       Mnemonic == "pac" || Mnemonic == "pacbti" || Mnemonic == "aut" ||
6603       Mnemonic == "bti" ||
6604       (hasMVE() &&
6605        (Mnemonic.startswith("vst2") || Mnemonic.startswith("vld2") ||
6606         Mnemonic.startswith("vst4") || Mnemonic.startswith("vld4") ||
6607         Mnemonic.startswith("wlstp") || Mnemonic.startswith("dlstp") ||
6608         Mnemonic.startswith("letp")))) {
6609     // These mnemonics are never predicable
6610     CanAcceptPredicationCode = false;
6611   } else if (!isThumb()) {
6612     // Some instructions are only predicable in Thumb mode
6613     CanAcceptPredicationCode =
6614         Mnemonic != "cdp2" && Mnemonic != "clrex" && Mnemonic != "mcr2" &&
6615         Mnemonic != "mcrr2" && Mnemonic != "mrc2" && Mnemonic != "mrrc2" &&
6616         Mnemonic != "dmb" && Mnemonic != "dfb" && Mnemonic != "dsb" &&
6617         Mnemonic != "isb" && Mnemonic != "pld" && Mnemonic != "pli" &&
6618         Mnemonic != "pldw" && Mnemonic != "ldc2" && Mnemonic != "ldc2l" &&
6619         Mnemonic != "stc2" && Mnemonic != "stc2l" &&
6620         Mnemonic != "tsb" &&
6621         !Mnemonic.startswith("rfe") && !Mnemonic.startswith("srs");
6622   } else if (isThumbOne()) {
6623     if (hasV6MOps())
6624       CanAcceptPredicationCode = Mnemonic != "movs";
6625     else
6626       CanAcceptPredicationCode = Mnemonic != "nop" && Mnemonic != "movs";
6627   } else
6628     CanAcceptPredicationCode = true;
6629 }
6630 
6631 // Some Thumb instructions have two operand forms that are not
6632 // available as three operand, convert to two operand form if possible.
6633 //
6634 // FIXME: We would really like to be able to tablegen'erate this.
6635 void ARMAsmParser::tryConvertingToTwoOperandForm(StringRef Mnemonic,
6636                                                  bool CarrySetting,
6637                                                  OperandVector &Operands) {
6638   if (Operands.size() != 6)
6639     return;
6640 
6641   const auto &Op3 = static_cast<ARMOperand &>(*Operands[3]);
6642         auto &Op4 = static_cast<ARMOperand &>(*Operands[4]);
6643   if (!Op3.isReg() || !Op4.isReg())
6644     return;
6645 
6646   auto Op3Reg = Op3.getReg();
6647   auto Op4Reg = Op4.getReg();
6648 
6649   // For most Thumb2 cases we just generate the 3 operand form and reduce
6650   // it in processInstruction(), but the 3 operand form of ADD (t2ADDrr)
6651   // won't accept SP or PC so we do the transformation here taking care
6652   // with immediate range in the 'add sp, sp #imm' case.
6653   auto &Op5 = static_cast<ARMOperand &>(*Operands[5]);
6654   if (isThumbTwo()) {
6655     if (Mnemonic != "add")
6656       return;
6657     bool TryTransform = Op3Reg == ARM::PC || Op4Reg == ARM::PC ||
6658                         (Op5.isReg() && Op5.getReg() == ARM::PC);
6659     if (!TryTransform) {
6660       TryTransform = (Op3Reg == ARM::SP || Op4Reg == ARM::SP ||
6661                       (Op5.isReg() && Op5.getReg() == ARM::SP)) &&
6662                      !(Op3Reg == ARM::SP && Op4Reg == ARM::SP &&
6663                        Op5.isImm() && !Op5.isImm0_508s4());
6664     }
6665     if (!TryTransform)
6666       return;
6667   } else if (!isThumbOne())
6668     return;
6669 
6670   if (!(Mnemonic == "add" || Mnemonic == "sub" || Mnemonic == "and" ||
6671         Mnemonic == "eor" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
6672         Mnemonic == "asr" || Mnemonic == "adc" || Mnemonic == "sbc" ||
6673         Mnemonic == "ror" || Mnemonic == "orr" || Mnemonic == "bic"))
6674     return;
6675 
6676   // If first 2 operands of a 3 operand instruction are the same
6677   // then transform to 2 operand version of the same instruction
6678   // e.g. 'adds r0, r0, #1' transforms to 'adds r0, #1'
6679   bool Transform = Op3Reg == Op4Reg;
6680 
6681   // For communtative operations, we might be able to transform if we swap
6682   // Op4 and Op5.  The 'ADD Rdm, SP, Rdm' form is already handled specially
6683   // as tADDrsp.
6684   const ARMOperand *LastOp = &Op5;
6685   bool Swap = false;
6686   if (!Transform && Op5.isReg() && Op3Reg == Op5.getReg() &&
6687       ((Mnemonic == "add" && Op4Reg != ARM::SP) ||
6688        Mnemonic == "and" || Mnemonic == "eor" ||
6689        Mnemonic == "adc" || Mnemonic == "orr")) {
6690     Swap = true;
6691     LastOp = &Op4;
6692     Transform = true;
6693   }
6694 
6695   // If both registers are the same then remove one of them from
6696   // the operand list, with certain exceptions.
6697   if (Transform) {
6698     // Don't transform 'adds Rd, Rd, Rm' or 'sub{s} Rd, Rd, Rm' because the
6699     // 2 operand forms don't exist.
6700     if (((Mnemonic == "add" && CarrySetting) || Mnemonic == "sub") &&
6701         LastOp->isReg())
6702       Transform = false;
6703 
6704     // Don't transform 'add/sub{s} Rd, Rd, #imm' if the immediate fits into
6705     // 3-bits because the ARMARM says not to.
6706     if ((Mnemonic == "add" || Mnemonic == "sub") && LastOp->isImm0_7())
6707       Transform = false;
6708   }
6709 
6710   if (Transform) {
6711     if (Swap)
6712       std::swap(Op4, Op5);
6713     Operands.erase(Operands.begin() + 3);
6714   }
6715 }
6716 
6717 bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
6718                                           OperandVector &Operands) {
6719   // FIXME: This is all horribly hacky. We really need a better way to deal
6720   // with optional operands like this in the matcher table.
6721 
6722   // The 'mov' mnemonic is special. One variant has a cc_out operand, while
6723   // another does not. Specifically, the MOVW instruction does not. So we
6724   // special case it here and remove the defaulted (non-setting) cc_out
6725   // operand if that's the instruction we're trying to match.
6726   //
6727   // We do this as post-processing of the explicit operands rather than just
6728   // conditionally adding the cc_out in the first place because we need
6729   // to check the type of the parsed immediate operand.
6730   if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
6731       !static_cast<ARMOperand &>(*Operands[4]).isModImm() &&
6732       static_cast<ARMOperand &>(*Operands[4]).isImm0_65535Expr() &&
6733       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0)
6734     return true;
6735 
6736   // Register-register 'add' for thumb does not have a cc_out operand
6737   // when there are only two register operands.
6738   if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
6739       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
6740       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
6741       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0)
6742     return true;
6743   // Register-register 'add' for thumb does not have a cc_out operand
6744   // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
6745   // have to check the immediate range here since Thumb2 has a variant
6746   // that can handle a different range and has a cc_out operand.
6747   if (((isThumb() && Mnemonic == "add") ||
6748        (isThumbTwo() && Mnemonic == "sub")) &&
6749       Operands.size() == 6 && static_cast<ARMOperand &>(*Operands[3]).isReg() &&
6750       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
6751       static_cast<ARMOperand &>(*Operands[4]).getReg() == ARM::SP &&
6752       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
6753       ((Mnemonic == "add" && static_cast<ARMOperand &>(*Operands[5]).isReg()) ||
6754        static_cast<ARMOperand &>(*Operands[5]).isImm0_1020s4()))
6755     return true;
6756   // For Thumb2, add/sub immediate does not have a cc_out operand for the
6757   // imm0_4095 variant. That's the least-preferred variant when
6758   // selecting via the generic "add" mnemonic, so to know that we
6759   // should remove the cc_out operand, we have to explicitly check that
6760   // it's not one of the other variants. Ugh.
6761   if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
6762       Operands.size() == 6 && static_cast<ARMOperand &>(*Operands[3]).isReg() &&
6763       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
6764       static_cast<ARMOperand &>(*Operands[5]).isImm()) {
6765     // Nest conditions rather than one big 'if' statement for readability.
6766     //
6767     // If both registers are low, we're in an IT block, and the immediate is
6768     // in range, we should use encoding T1 instead, which has a cc_out.
6769     if (inITBlock() &&
6770         isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) &&
6771         isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) &&
6772         static_cast<ARMOperand &>(*Operands[5]).isImm0_7())
6773       return false;
6774     // Check against T3. If the second register is the PC, this is an
6775     // alternate form of ADR, which uses encoding T4, so check for that too.
6776     if (static_cast<ARMOperand &>(*Operands[4]).getReg() != ARM::PC &&
6777         (static_cast<ARMOperand &>(*Operands[5]).isT2SOImm() ||
6778          static_cast<ARMOperand &>(*Operands[5]).isT2SOImmNeg()))
6779       return false;
6780 
6781     // Otherwise, we use encoding T4, which does not have a cc_out
6782     // operand.
6783     return true;
6784   }
6785 
6786   // The thumb2 multiply instruction doesn't have a CCOut register, so
6787   // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
6788   // use the 16-bit encoding or not.
6789   if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
6790       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
6791       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
6792       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
6793       static_cast<ARMOperand &>(*Operands[5]).isReg() &&
6794       // If the registers aren't low regs, the destination reg isn't the
6795       // same as one of the source regs, or the cc_out operand is zero
6796       // outside of an IT block, we have to use the 32-bit encoding, so
6797       // remove the cc_out operand.
6798       (!isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) ||
6799        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) ||
6800        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[5]).getReg()) ||
6801        !inITBlock() || (static_cast<ARMOperand &>(*Operands[3]).getReg() !=
6802                             static_cast<ARMOperand &>(*Operands[5]).getReg() &&
6803                         static_cast<ARMOperand &>(*Operands[3]).getReg() !=
6804                             static_cast<ARMOperand &>(*Operands[4]).getReg())))
6805     return true;
6806 
6807   // Also check the 'mul' syntax variant that doesn't specify an explicit
6808   // destination register.
6809   if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 5 &&
6810       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
6811       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
6812       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
6813       // If the registers aren't low regs  or the cc_out operand is zero
6814       // outside of an IT block, we have to use the 32-bit encoding, so
6815       // remove the cc_out operand.
6816       (!isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) ||
6817        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) ||
6818        !inITBlock()))
6819     return true;
6820 
6821   // Register-register 'add/sub' for thumb does not have a cc_out operand
6822   // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
6823   // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
6824   // right, this will result in better diagnostics (which operand is off)
6825   // anyway.
6826   if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
6827       (Operands.size() == 5 || Operands.size() == 6) &&
6828       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
6829       static_cast<ARMOperand &>(*Operands[3]).getReg() == ARM::SP &&
6830       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
6831       (static_cast<ARMOperand &>(*Operands[4]).isImm() ||
6832        (Operands.size() == 6 &&
6833         static_cast<ARMOperand &>(*Operands[5]).isImm()))) {
6834     // Thumb2 (add|sub){s}{p}.w GPRnopc, sp, #{T2SOImm} has cc_out
6835     return (!(isThumbTwo() &&
6836               (static_cast<ARMOperand &>(*Operands[4]).isT2SOImm() ||
6837                static_cast<ARMOperand &>(*Operands[4]).isT2SOImmNeg())));
6838   }
6839   // Fixme: Should join all the thumb+thumb2 (add|sub) in a single if case
6840   // Thumb2 ADD r0, #4095 -> ADDW r0, r0, #4095 (T4)
6841   // Thumb2 SUB r0, #4095 -> SUBW r0, r0, #4095
6842   if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
6843       (Operands.size() == 5) &&
6844       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
6845       static_cast<ARMOperand &>(*Operands[3]).getReg() != ARM::SP &&
6846       static_cast<ARMOperand &>(*Operands[3]).getReg() != ARM::PC &&
6847       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
6848       static_cast<ARMOperand &>(*Operands[4]).isImm()) {
6849     const ARMOperand &IMM = static_cast<ARMOperand &>(*Operands[4]);
6850     if (IMM.isT2SOImm() || IMM.isT2SOImmNeg())
6851       return false; // add.w / sub.w
6852     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(IMM.getImm())) {
6853       const int64_t Value = CE->getValue();
6854       // Thumb1 imm8 sub / add
6855       if ((Value < ((1 << 7) - 1) << 2) && inITBlock() && (!(Value & 3)) &&
6856           isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()))
6857         return false;
6858       return true; // Thumb2 T4 addw / subw
6859     }
6860   }
6861   return false;
6862 }
6863 
6864 bool ARMAsmParser::shouldOmitPredicateOperand(StringRef Mnemonic,
6865                                               OperandVector &Operands) {
6866   // VRINT{Z, X} have a predicate operand in VFP, but not in NEON
6867   unsigned RegIdx = 3;
6868   if ((((Mnemonic == "vrintz" || Mnemonic == "vrintx") && !hasMVE()) ||
6869       Mnemonic == "vrintr") &&
6870       (static_cast<ARMOperand &>(*Operands[2]).getToken() == ".f32" ||
6871        static_cast<ARMOperand &>(*Operands[2]).getToken() == ".f16")) {
6872     if (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
6873         (static_cast<ARMOperand &>(*Operands[3]).getToken() == ".f32" ||
6874          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".f16"))
6875       RegIdx = 4;
6876 
6877     if (static_cast<ARMOperand &>(*Operands[RegIdx]).isReg() &&
6878         (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(
6879              static_cast<ARMOperand &>(*Operands[RegIdx]).getReg()) ||
6880          ARMMCRegisterClasses[ARM::QPRRegClassID].contains(
6881              static_cast<ARMOperand &>(*Operands[RegIdx]).getReg())))
6882       return true;
6883   }
6884   return false;
6885 }
6886 
6887 bool ARMAsmParser::shouldOmitVectorPredicateOperand(StringRef Mnemonic,
6888                                                     OperandVector &Operands) {
6889   if (!hasMVE() || Operands.size() < 3)
6890     return true;
6891 
6892   if (Mnemonic.startswith("vld2") || Mnemonic.startswith("vld4") ||
6893       Mnemonic.startswith("vst2") || Mnemonic.startswith("vst4"))
6894     return true;
6895 
6896   if (Mnemonic.startswith("vctp") || Mnemonic.startswith("vpnot"))
6897     return false;
6898 
6899   if (Mnemonic.startswith("vmov") &&
6900       !(Mnemonic.startswith("vmovl") || Mnemonic.startswith("vmovn") ||
6901         Mnemonic.startswith("vmovx"))) {
6902     for (auto &Operand : Operands) {
6903       if (static_cast<ARMOperand &>(*Operand).isVectorIndex() ||
6904           ((*Operand).isReg() &&
6905            (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(
6906              (*Operand).getReg()) ||
6907             ARMMCRegisterClasses[ARM::DPRRegClassID].contains(
6908               (*Operand).getReg())))) {
6909         return true;
6910       }
6911     }
6912     return false;
6913   } else {
6914     for (auto &Operand : Operands) {
6915       // We check the larger class QPR instead of just the legal class
6916       // MQPR, to more accurately report errors when using Q registers
6917       // outside of the allowed range.
6918       if (static_cast<ARMOperand &>(*Operand).isVectorIndex() ||
6919           (Operand->isReg() &&
6920            (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(
6921              Operand->getReg()))))
6922         return false;
6923     }
6924     return true;
6925   }
6926 }
6927 
6928 static bool isDataTypeToken(StringRef Tok) {
6929   return Tok == ".8" || Tok == ".16" || Tok == ".32" || Tok == ".64" ||
6930     Tok == ".i8" || Tok == ".i16" || Tok == ".i32" || Tok == ".i64" ||
6931     Tok == ".u8" || Tok == ".u16" || Tok == ".u32" || Tok == ".u64" ||
6932     Tok == ".s8" || Tok == ".s16" || Tok == ".s32" || Tok == ".s64" ||
6933     Tok == ".p8" || Tok == ".p16" || Tok == ".f32" || Tok == ".f64" ||
6934     Tok == ".f" || Tok == ".d";
6935 }
6936 
6937 // FIXME: This bit should probably be handled via an explicit match class
6938 // in the .td files that matches the suffix instead of having it be
6939 // a literal string token the way it is now.
6940 static bool doesIgnoreDataTypeSuffix(StringRef Mnemonic, StringRef DT) {
6941   return Mnemonic.startswith("vldm") || Mnemonic.startswith("vstm");
6942 }
6943 
6944 static void applyMnemonicAliases(StringRef &Mnemonic,
6945                                  const FeatureBitset &Features,
6946                                  unsigned VariantID);
6947 
6948 // The GNU assembler has aliases of ldrd and strd with the second register
6949 // omitted. We don't have a way to do that in tablegen, so fix it up here.
6950 //
6951 // We have to be careful to not emit an invalid Rt2 here, because the rest of
6952 // the assembly parser could then generate confusing diagnostics refering to
6953 // it. If we do find anything that prevents us from doing the transformation we
6954 // bail out, and let the assembly parser report an error on the instruction as
6955 // it is written.
6956 void ARMAsmParser::fixupGNULDRDAlias(StringRef Mnemonic,
6957                                      OperandVector &Operands) {
6958   if (Mnemonic != "ldrd" && Mnemonic != "strd")
6959     return;
6960   if (Operands.size() < 4)
6961     return;
6962 
6963   ARMOperand &Op2 = static_cast<ARMOperand &>(*Operands[2]);
6964   ARMOperand &Op3 = static_cast<ARMOperand &>(*Operands[3]);
6965 
6966   if (!Op2.isReg())
6967     return;
6968   if (!Op3.isGPRMem())
6969     return;
6970 
6971   const MCRegisterClass &GPR = MRI->getRegClass(ARM::GPRRegClassID);
6972   if (!GPR.contains(Op2.getReg()))
6973     return;
6974 
6975   unsigned RtEncoding = MRI->getEncodingValue(Op2.getReg());
6976   if (!isThumb() && (RtEncoding & 1)) {
6977     // In ARM mode, the registers must be from an aligned pair, this
6978     // restriction does not apply in Thumb mode.
6979     return;
6980   }
6981   if (Op2.getReg() == ARM::PC)
6982     return;
6983   unsigned PairedReg = GPR.getRegister(RtEncoding + 1);
6984   if (!PairedReg || PairedReg == ARM::PC ||
6985       (PairedReg == ARM::SP && !hasV8Ops()))
6986     return;
6987 
6988   Operands.insert(
6989       Operands.begin() + 3,
6990       ARMOperand::CreateReg(PairedReg, Op2.getStartLoc(), Op2.getEndLoc()));
6991 }
6992 
6993 // Dual-register instruction have the following syntax:
6994 // <mnemonic> <predicate>? <coproc>, <Rdest>, <Rdest+1>, <Rsrc>, ..., #imm
6995 // This function tries to remove <Rdest+1> and replace <Rdest> with a pair
6996 // operand. If the conversion fails an error is diagnosed, and the function
6997 // returns true.
6998 bool ARMAsmParser::CDEConvertDualRegOperand(StringRef Mnemonic,
6999                                             OperandVector &Operands) {
7000   assert(MS.isCDEDualRegInstr(Mnemonic));
7001   bool isPredicable =
7002       Mnemonic == "cx1da" || Mnemonic == "cx2da" || Mnemonic == "cx3da";
7003   size_t NumPredOps = isPredicable ? 1 : 0;
7004 
7005   if (Operands.size() <= 3 + NumPredOps)
7006     return false;
7007 
7008   StringRef Op2Diag(
7009       "operand must be an even-numbered register in the range [r0, r10]");
7010 
7011   const MCParsedAsmOperand &Op2 = *Operands[2 + NumPredOps];
7012   if (!Op2.isReg())
7013     return Error(Op2.getStartLoc(), Op2Diag);
7014 
7015   unsigned RNext;
7016   unsigned RPair;
7017   switch (Op2.getReg()) {
7018   default:
7019     return Error(Op2.getStartLoc(), Op2Diag);
7020   case ARM::R0:
7021     RNext = ARM::R1;
7022     RPair = ARM::R0_R1;
7023     break;
7024   case ARM::R2:
7025     RNext = ARM::R3;
7026     RPair = ARM::R2_R3;
7027     break;
7028   case ARM::R4:
7029     RNext = ARM::R5;
7030     RPair = ARM::R4_R5;
7031     break;
7032   case ARM::R6:
7033     RNext = ARM::R7;
7034     RPair = ARM::R6_R7;
7035     break;
7036   case ARM::R8:
7037     RNext = ARM::R9;
7038     RPair = ARM::R8_R9;
7039     break;
7040   case ARM::R10:
7041     RNext = ARM::R11;
7042     RPair = ARM::R10_R11;
7043     break;
7044   }
7045 
7046   const MCParsedAsmOperand &Op3 = *Operands[3 + NumPredOps];
7047   if (!Op3.isReg() || Op3.getReg() != RNext)
7048     return Error(Op3.getStartLoc(), "operand must be a consecutive register");
7049 
7050   Operands.erase(Operands.begin() + 3 + NumPredOps);
7051   Operands[2 + NumPredOps] =
7052       ARMOperand::CreateReg(RPair, Op2.getStartLoc(), Op2.getEndLoc());
7053   return false;
7054 }
7055 
7056 /// Parse an arm instruction mnemonic followed by its operands.
7057 bool ARMAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
7058                                     SMLoc NameLoc, OperandVector &Operands) {
7059   MCAsmParser &Parser = getParser();
7060 
7061   // Apply mnemonic aliases before doing anything else, as the destination
7062   // mnemonic may include suffices and we want to handle them normally.
7063   // The generic tblgen'erated code does this later, at the start of
7064   // MatchInstructionImpl(), but that's too late for aliases that include
7065   // any sort of suffix.
7066   const FeatureBitset &AvailableFeatures = getAvailableFeatures();
7067   unsigned AssemblerDialect = getParser().getAssemblerDialect();
7068   applyMnemonicAliases(Name, AvailableFeatures, AssemblerDialect);
7069 
7070   // First check for the ARM-specific .req directive.
7071   if (Parser.getTok().is(AsmToken::Identifier) &&
7072       Parser.getTok().getIdentifier().lower() == ".req") {
7073     parseDirectiveReq(Name, NameLoc);
7074     // We always return 'error' for this, as we're done with this
7075     // statement and don't need to match the 'instruction."
7076     return true;
7077   }
7078 
7079   // Create the leading tokens for the mnemonic, split by '.' characters.
7080   size_t Start = 0, Next = Name.find('.');
7081   StringRef Mnemonic = Name.slice(Start, Next);
7082   StringRef ExtraToken = Name.slice(Next, Name.find(' ', Next + 1));
7083 
7084   // Split out the predication code and carry setting flag from the mnemonic.
7085   unsigned PredicationCode;
7086   unsigned VPTPredicationCode;
7087   unsigned ProcessorIMod;
7088   bool CarrySetting;
7089   StringRef ITMask;
7090   Mnemonic = splitMnemonic(Mnemonic, ExtraToken, PredicationCode, VPTPredicationCode,
7091                            CarrySetting, ProcessorIMod, ITMask);
7092 
7093   // In Thumb1, only the branch (B) instruction can be predicated.
7094   if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
7095     return Error(NameLoc, "conditional execution not supported in Thumb1");
7096   }
7097 
7098   Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
7099 
7100   // Handle the mask for IT and VPT instructions. In ARMOperand and
7101   // MCOperand, this is stored in a format independent of the
7102   // condition code: the lowest set bit indicates the end of the
7103   // encoding, and above that, a 1 bit indicates 'else', and an 0
7104   // indicates 'then'. E.g.
7105   //    IT    -> 1000
7106   //    ITx   -> x100    (ITT -> 0100, ITE -> 1100)
7107   //    ITxy  -> xy10    (e.g. ITET -> 1010)
7108   //    ITxyz -> xyz1    (e.g. ITEET -> 1101)
7109   // Note: See the ARM::PredBlockMask enum in
7110   //   /lib/Target/ARM/Utils/ARMBaseInfo.h
7111   if (Mnemonic == "it" || Mnemonic.startswith("vpt") ||
7112       Mnemonic.startswith("vpst")) {
7113     SMLoc Loc = Mnemonic == "it"  ? SMLoc::getFromPointer(NameLoc.getPointer() + 2) :
7114                 Mnemonic == "vpt" ? SMLoc::getFromPointer(NameLoc.getPointer() + 3) :
7115                                     SMLoc::getFromPointer(NameLoc.getPointer() + 4);
7116     if (ITMask.size() > 3) {
7117       if (Mnemonic == "it")
7118         return Error(Loc, "too many conditions on IT instruction");
7119       return Error(Loc, "too many conditions on VPT instruction");
7120     }
7121     unsigned Mask = 8;
7122     for (char Pos : llvm::reverse(ITMask)) {
7123       if (Pos != 't' && Pos != 'e') {
7124         return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
7125       }
7126       Mask >>= 1;
7127       if (Pos == 'e')
7128         Mask |= 8;
7129     }
7130     Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
7131   }
7132 
7133   // FIXME: This is all a pretty gross hack. We should automatically handle
7134   // optional operands like this via tblgen.
7135 
7136   // Next, add the CCOut and ConditionCode operands, if needed.
7137   //
7138   // For mnemonics which can ever incorporate a carry setting bit or predication
7139   // code, our matching model involves us always generating CCOut and
7140   // ConditionCode operands to match the mnemonic "as written" and then we let
7141   // the matcher deal with finding the right instruction or generating an
7142   // appropriate error.
7143   bool CanAcceptCarrySet, CanAcceptPredicationCode, CanAcceptVPTPredicationCode;
7144   getMnemonicAcceptInfo(Mnemonic, ExtraToken, Name, CanAcceptCarrySet,
7145                         CanAcceptPredicationCode, CanAcceptVPTPredicationCode);
7146 
7147   // If we had a carry-set on an instruction that can't do that, issue an
7148   // error.
7149   if (!CanAcceptCarrySet && CarrySetting) {
7150     return Error(NameLoc, "instruction '" + Mnemonic +
7151                  "' can not set flags, but 's' suffix specified");
7152   }
7153   // If we had a predication code on an instruction that can't do that, issue an
7154   // error.
7155   if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
7156     return Error(NameLoc, "instruction '" + Mnemonic +
7157                  "' is not predicable, but condition code specified");
7158   }
7159 
7160   // If we had a VPT predication code on an instruction that can't do that, issue an
7161   // error.
7162   if (!CanAcceptVPTPredicationCode && VPTPredicationCode != ARMVCC::None) {
7163     return Error(NameLoc, "instruction '" + Mnemonic +
7164                  "' is not VPT predicable, but VPT code T/E is specified");
7165   }
7166 
7167   // Add the carry setting operand, if necessary.
7168   if (CanAcceptCarrySet) {
7169     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
7170     Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
7171                                                Loc));
7172   }
7173 
7174   // Add the predication code operand, if necessary.
7175   if (CanAcceptPredicationCode) {
7176     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
7177                                       CarrySetting);
7178     Operands.push_back(ARMOperand::CreateCondCode(
7179                        ARMCC::CondCodes(PredicationCode), Loc));
7180   }
7181 
7182   // Add the VPT predication code operand, if necessary.
7183   // FIXME: We don't add them for the instructions filtered below as these can
7184   // have custom operands which need special parsing.  This parsing requires
7185   // the operand to be in the same place in the OperandVector as their
7186   // definition in tblgen.  Since these instructions may also have the
7187   // scalar predication operand we do not add the vector one and leave until
7188   // now to fix it up.
7189   if (CanAcceptVPTPredicationCode && Mnemonic != "vmov" &&
7190       !Mnemonic.startswith("vcmp") &&
7191       !(Mnemonic.startswith("vcvt") && Mnemonic != "vcvta" &&
7192         Mnemonic != "vcvtn" && Mnemonic != "vcvtp" && Mnemonic != "vcvtm")) {
7193     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
7194                                       CarrySetting);
7195     Operands.push_back(ARMOperand::CreateVPTPred(
7196                          ARMVCC::VPTCodes(VPTPredicationCode), Loc));
7197   }
7198 
7199   // Add the processor imod operand, if necessary.
7200   if (ProcessorIMod) {
7201     Operands.push_back(ARMOperand::CreateImm(
7202           MCConstantExpr::create(ProcessorIMod, getContext()),
7203                                  NameLoc, NameLoc));
7204   } else if (Mnemonic == "cps" && isMClass()) {
7205     return Error(NameLoc, "instruction 'cps' requires effect for M-class");
7206   }
7207 
7208   // Add the remaining tokens in the mnemonic.
7209   while (Next != StringRef::npos) {
7210     Start = Next;
7211     Next = Name.find('.', Start + 1);
7212     ExtraToken = Name.slice(Start, Next);
7213 
7214     // Some NEON instructions have an optional datatype suffix that is
7215     // completely ignored. Check for that.
7216     if (isDataTypeToken(ExtraToken) &&
7217         doesIgnoreDataTypeSuffix(Mnemonic, ExtraToken))
7218       continue;
7219 
7220     // For for ARM mode generate an error if the .n qualifier is used.
7221     if (ExtraToken == ".n" && !isThumb()) {
7222       SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
7223       return Error(Loc, "instruction with .n (narrow) qualifier not allowed in "
7224                    "arm mode");
7225     }
7226 
7227     // The .n qualifier is always discarded as that is what the tables
7228     // and matcher expect.  In ARM mode the .w qualifier has no effect,
7229     // so discard it to avoid errors that can be caused by the matcher.
7230     if (ExtraToken != ".n" && (isThumb() || ExtraToken != ".w")) {
7231       SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
7232       Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
7233     }
7234   }
7235 
7236   // Read the remaining operands.
7237   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7238     // Read the first operand.
7239     if (parseOperand(Operands, Mnemonic)) {
7240       return true;
7241     }
7242 
7243     while (parseOptionalToken(AsmToken::Comma)) {
7244       // Parse and remember the operand.
7245       if (parseOperand(Operands, Mnemonic)) {
7246         return true;
7247       }
7248     }
7249   }
7250 
7251   if (parseToken(AsmToken::EndOfStatement, "unexpected token in argument list"))
7252     return true;
7253 
7254   tryConvertingToTwoOperandForm(Mnemonic, CarrySetting, Operands);
7255 
7256   if (hasCDE() && MS.isCDEInstr(Mnemonic)) {
7257     // Dual-register instructions use even-odd register pairs as their
7258     // destination operand, in assembly such pair is spelled as two
7259     // consecutive registers, without any special syntax. ConvertDualRegOperand
7260     // tries to convert such operand into register pair, e.g. r2, r3 -> r2_r3.
7261     // It returns true, if an error message has been emitted. If the function
7262     // returns false, the function either succeeded or an error (e.g. missing
7263     // operand) will be diagnosed elsewhere.
7264     if (MS.isCDEDualRegInstr(Mnemonic)) {
7265       bool GotError = CDEConvertDualRegOperand(Mnemonic, Operands);
7266       if (GotError)
7267         return GotError;
7268     }
7269   }
7270 
7271   // Some instructions, mostly Thumb, have forms for the same mnemonic that
7272   // do and don't have a cc_out optional-def operand. With some spot-checks
7273   // of the operand list, we can figure out which variant we're trying to
7274   // parse and adjust accordingly before actually matching. We shouldn't ever
7275   // try to remove a cc_out operand that was explicitly set on the
7276   // mnemonic, of course (CarrySetting == true). Reason number #317 the
7277   // table driven matcher doesn't fit well with the ARM instruction set.
7278   if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands))
7279     Operands.erase(Operands.begin() + 1);
7280 
7281   // Some instructions have the same mnemonic, but don't always
7282   // have a predicate. Distinguish them here and delete the
7283   // appropriate predicate if needed.  This could be either the scalar
7284   // predication code or the vector predication code.
7285   if (PredicationCode == ARMCC::AL &&
7286       shouldOmitPredicateOperand(Mnemonic, Operands))
7287     Operands.erase(Operands.begin() + 1);
7288 
7289 
7290   if (hasMVE()) {
7291     if (!shouldOmitVectorPredicateOperand(Mnemonic, Operands) &&
7292         Mnemonic == "vmov" && PredicationCode == ARMCC::LT) {
7293       // Very nasty hack to deal with the vector predicated variant of vmovlt
7294       // the scalar predicated vmov with condition 'lt'.  We can not tell them
7295       // apart until we have parsed their operands.
7296       Operands.erase(Operands.begin() + 1);
7297       Operands.erase(Operands.begin());
7298       SMLoc MLoc = SMLoc::getFromPointer(NameLoc.getPointer());
7299       SMLoc PLoc = SMLoc::getFromPointer(NameLoc.getPointer() +
7300                                          Mnemonic.size() - 1 + CarrySetting);
7301       Operands.insert(Operands.begin(),
7302                       ARMOperand::CreateVPTPred(ARMVCC::None, PLoc));
7303       Operands.insert(Operands.begin(),
7304                       ARMOperand::CreateToken(StringRef("vmovlt"), MLoc));
7305     } else if (Mnemonic == "vcvt" && PredicationCode == ARMCC::NE &&
7306                !shouldOmitVectorPredicateOperand(Mnemonic, Operands)) {
7307       // Another nasty hack to deal with the ambiguity between vcvt with scalar
7308       // predication 'ne' and vcvtn with vector predication 'e'.  As above we
7309       // can only distinguish between the two after we have parsed their
7310       // operands.
7311       Operands.erase(Operands.begin() + 1);
7312       Operands.erase(Operands.begin());
7313       SMLoc MLoc = SMLoc::getFromPointer(NameLoc.getPointer());
7314       SMLoc PLoc = SMLoc::getFromPointer(NameLoc.getPointer() +
7315                                          Mnemonic.size() - 1 + CarrySetting);
7316       Operands.insert(Operands.begin(),
7317                       ARMOperand::CreateVPTPred(ARMVCC::Else, PLoc));
7318       Operands.insert(Operands.begin(),
7319                       ARMOperand::CreateToken(StringRef("vcvtn"), MLoc));
7320     } else if (Mnemonic == "vmul" && PredicationCode == ARMCC::LT &&
7321                !shouldOmitVectorPredicateOperand(Mnemonic, Operands)) {
7322       // Another hack, this time to distinguish between scalar predicated vmul
7323       // with 'lt' predication code and the vector instruction vmullt with
7324       // vector predication code "none"
7325       Operands.erase(Operands.begin() + 1);
7326       Operands.erase(Operands.begin());
7327       SMLoc MLoc = SMLoc::getFromPointer(NameLoc.getPointer());
7328       Operands.insert(Operands.begin(),
7329                       ARMOperand::CreateToken(StringRef("vmullt"), MLoc));
7330     }
7331     // For vmov and vcmp, as mentioned earlier, we did not add the vector
7332     // predication code, since these may contain operands that require
7333     // special parsing.  So now we have to see if they require vector
7334     // predication and replace the scalar one with the vector predication
7335     // operand if that is the case.
7336     else if (Mnemonic == "vmov" || Mnemonic.startswith("vcmp") ||
7337              (Mnemonic.startswith("vcvt") && !Mnemonic.startswith("vcvta") &&
7338               !Mnemonic.startswith("vcvtn") && !Mnemonic.startswith("vcvtp") &&
7339               !Mnemonic.startswith("vcvtm"))) {
7340       if (!shouldOmitVectorPredicateOperand(Mnemonic, Operands)) {
7341         // We could not split the vector predicate off vcvt because it might
7342         // have been the scalar vcvtt instruction.  Now we know its a vector
7343         // instruction, we still need to check whether its the vector
7344         // predicated vcvt with 'Then' predication or the vector vcvtt.  We can
7345         // distinguish the two based on the suffixes, if it is any of
7346         // ".f16.f32", ".f32.f16", ".f16.f64" or ".f64.f16" then it is the vcvtt.
7347         if (Mnemonic.startswith("vcvtt") && Operands.size() >= 4) {
7348           auto Sz1 = static_cast<ARMOperand &>(*Operands[2]);
7349           auto Sz2 = static_cast<ARMOperand &>(*Operands[3]);
7350           if (!(Sz1.isToken() && Sz1.getToken().startswith(".f") &&
7351               Sz2.isToken() && Sz2.getToken().startswith(".f"))) {
7352             Operands.erase(Operands.begin());
7353             SMLoc MLoc = SMLoc::getFromPointer(NameLoc.getPointer());
7354             VPTPredicationCode = ARMVCC::Then;
7355 
7356             Mnemonic = Mnemonic.substr(0, 4);
7357             Operands.insert(Operands.begin(),
7358                             ARMOperand::CreateToken(Mnemonic, MLoc));
7359           }
7360         }
7361         Operands.erase(Operands.begin() + 1);
7362         SMLoc PLoc = SMLoc::getFromPointer(NameLoc.getPointer() +
7363                                           Mnemonic.size() + CarrySetting);
7364         Operands.insert(Operands.begin() + 1,
7365                         ARMOperand::CreateVPTPred(
7366                             ARMVCC::VPTCodes(VPTPredicationCode), PLoc));
7367       }
7368     } else if (CanAcceptVPTPredicationCode) {
7369       // For all other instructions, make sure only one of the two
7370       // predication operands is left behind, depending on whether we should
7371       // use the vector predication.
7372       if (shouldOmitVectorPredicateOperand(Mnemonic, Operands)) {
7373         if (CanAcceptPredicationCode)
7374           Operands.erase(Operands.begin() + 2);
7375         else
7376           Operands.erase(Operands.begin() + 1);
7377       } else if (CanAcceptPredicationCode && PredicationCode == ARMCC::AL) {
7378         Operands.erase(Operands.begin() + 1);
7379       }
7380     }
7381   }
7382 
7383   if (VPTPredicationCode != ARMVCC::None) {
7384     bool usedVPTPredicationCode = false;
7385     for (unsigned I = 1; I < Operands.size(); ++I)
7386       if (static_cast<ARMOperand &>(*Operands[I]).isVPTPred())
7387         usedVPTPredicationCode = true;
7388     if (!usedVPTPredicationCode) {
7389       // If we have a VPT predication code and we haven't just turned it
7390       // into an operand, then it was a mistake for splitMnemonic to
7391       // separate it from the rest of the mnemonic in the first place,
7392       // and this may lead to wrong disassembly (e.g. scalar floating
7393       // point VCMPE is actually a different instruction from VCMP, so
7394       // we mustn't treat them the same). In that situation, glue it
7395       // back on.
7396       Mnemonic = Name.slice(0, Mnemonic.size() + 1);
7397       Operands.erase(Operands.begin());
7398       Operands.insert(Operands.begin(),
7399                       ARMOperand::CreateToken(Mnemonic, NameLoc));
7400     }
7401   }
7402 
7403     // ARM mode 'blx' need special handling, as the register operand version
7404     // is predicable, but the label operand version is not. So, we can't rely
7405     // on the Mnemonic based checking to correctly figure out when to put
7406     // a k_CondCode operand in the list. If we're trying to match the label
7407     // version, remove the k_CondCode operand here.
7408     if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
7409         static_cast<ARMOperand &>(*Operands[2]).isImm())
7410       Operands.erase(Operands.begin() + 1);
7411 
7412     // Adjust operands of ldrexd/strexd to MCK_GPRPair.
7413     // ldrexd/strexd require even/odd GPR pair. To enforce this constraint,
7414     // a single GPRPair reg operand is used in the .td file to replace the two
7415     // GPRs. However, when parsing from asm, the two GRPs cannot be
7416     // automatically
7417     // expressed as a GPRPair, so we have to manually merge them.
7418     // FIXME: We would really like to be able to tablegen'erate this.
7419     if (!isThumb() && Operands.size() > 4 &&
7420         (Mnemonic == "ldrexd" || Mnemonic == "strexd" || Mnemonic == "ldaexd" ||
7421          Mnemonic == "stlexd")) {
7422       bool isLoad = (Mnemonic == "ldrexd" || Mnemonic == "ldaexd");
7423       unsigned Idx = isLoad ? 2 : 3;
7424       ARMOperand &Op1 = static_cast<ARMOperand &>(*Operands[Idx]);
7425       ARMOperand &Op2 = static_cast<ARMOperand &>(*Operands[Idx + 1]);
7426 
7427       const MCRegisterClass &MRC = MRI->getRegClass(ARM::GPRRegClassID);
7428       // Adjust only if Op1 and Op2 are GPRs.
7429       if (Op1.isReg() && Op2.isReg() && MRC.contains(Op1.getReg()) &&
7430           MRC.contains(Op2.getReg())) {
7431         unsigned Reg1 = Op1.getReg();
7432         unsigned Reg2 = Op2.getReg();
7433         unsigned Rt = MRI->getEncodingValue(Reg1);
7434         unsigned Rt2 = MRI->getEncodingValue(Reg2);
7435 
7436         // Rt2 must be Rt + 1 and Rt must be even.
7437         if (Rt + 1 != Rt2 || (Rt & 1)) {
7438           return Error(Op2.getStartLoc(),
7439                        isLoad ? "destination operands must be sequential"
7440                               : "source operands must be sequential");
7441         }
7442         unsigned NewReg = MRI->getMatchingSuperReg(
7443             Reg1, ARM::gsub_0, &(MRI->getRegClass(ARM::GPRPairRegClassID)));
7444         Operands[Idx] =
7445             ARMOperand::CreateReg(NewReg, Op1.getStartLoc(), Op2.getEndLoc());
7446         Operands.erase(Operands.begin() + Idx + 1);
7447       }
7448   }
7449 
7450   // GNU Assembler extension (compatibility).
7451   fixupGNULDRDAlias(Mnemonic, Operands);
7452 
7453   // FIXME: As said above, this is all a pretty gross hack.  This instruction
7454   // does not fit with other "subs" and tblgen.
7455   // Adjust operands of B9.3.19 SUBS PC, LR, #imm (Thumb2) system instruction
7456   // so the Mnemonic is the original name "subs" and delete the predicate
7457   // operand so it will match the table entry.
7458   if (isThumbTwo() && Mnemonic == "sub" && Operands.size() == 6 &&
7459       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
7460       static_cast<ARMOperand &>(*Operands[3]).getReg() == ARM::PC &&
7461       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
7462       static_cast<ARMOperand &>(*Operands[4]).getReg() == ARM::LR &&
7463       static_cast<ARMOperand &>(*Operands[5]).isImm()) {
7464     Operands.front() = ARMOperand::CreateToken(Name, NameLoc);
7465     Operands.erase(Operands.begin() + 1);
7466   }
7467   return false;
7468 }
7469 
7470 // Validate context-sensitive operand constraints.
7471 
7472 // return 'true' if register list contains non-low GPR registers,
7473 // 'false' otherwise. If Reg is in the register list or is HiReg, set
7474 // 'containsReg' to true.
7475 static bool checkLowRegisterList(const MCInst &Inst, unsigned OpNo,
7476                                  unsigned Reg, unsigned HiReg,
7477                                  bool &containsReg) {
7478   containsReg = false;
7479   for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
7480     unsigned OpReg = Inst.getOperand(i).getReg();
7481     if (OpReg == Reg)
7482       containsReg = true;
7483     // Anything other than a low register isn't legal here.
7484     if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
7485       return true;
7486   }
7487   return false;
7488 }
7489 
7490 // Check if the specified regisgter is in the register list of the inst,
7491 // starting at the indicated operand number.
7492 static bool listContainsReg(const MCInst &Inst, unsigned OpNo, unsigned Reg) {
7493   for (unsigned i = OpNo, e = Inst.getNumOperands(); i < e; ++i) {
7494     unsigned OpReg = Inst.getOperand(i).getReg();
7495     if (OpReg == Reg)
7496       return true;
7497   }
7498   return false;
7499 }
7500 
7501 // Return true if instruction has the interesting property of being
7502 // allowed in IT blocks, but not being predicable.
7503 static bool instIsBreakpoint(const MCInst &Inst) {
7504     return Inst.getOpcode() == ARM::tBKPT ||
7505            Inst.getOpcode() == ARM::BKPT ||
7506            Inst.getOpcode() == ARM::tHLT ||
7507            Inst.getOpcode() == ARM::HLT;
7508 }
7509 
7510 bool ARMAsmParser::validatetLDMRegList(const MCInst &Inst,
7511                                        const OperandVector &Operands,
7512                                        unsigned ListNo, bool IsARPop) {
7513   const ARMOperand &Op = static_cast<const ARMOperand &>(*Operands[ListNo]);
7514   bool HasWritebackToken = Op.isToken() && Op.getToken() == "!";
7515 
7516   bool ListContainsSP = listContainsReg(Inst, ListNo, ARM::SP);
7517   bool ListContainsLR = listContainsReg(Inst, ListNo, ARM::LR);
7518   bool ListContainsPC = listContainsReg(Inst, ListNo, ARM::PC);
7519 
7520   if (!IsARPop && ListContainsSP)
7521     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
7522                  "SP may not be in the register list");
7523   else if (ListContainsPC && ListContainsLR)
7524     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
7525                  "PC and LR may not be in the register list simultaneously");
7526   return false;
7527 }
7528 
7529 bool ARMAsmParser::validatetSTMRegList(const MCInst &Inst,
7530                                        const OperandVector &Operands,
7531                                        unsigned ListNo) {
7532   const ARMOperand &Op = static_cast<const ARMOperand &>(*Operands[ListNo]);
7533   bool HasWritebackToken = Op.isToken() && Op.getToken() == "!";
7534 
7535   bool ListContainsSP = listContainsReg(Inst, ListNo, ARM::SP);
7536   bool ListContainsPC = listContainsReg(Inst, ListNo, ARM::PC);
7537 
7538   if (ListContainsSP && ListContainsPC)
7539     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
7540                  "SP and PC may not be in the register list");
7541   else if (ListContainsSP)
7542     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
7543                  "SP may not be in the register list");
7544   else if (ListContainsPC)
7545     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
7546                  "PC may not be in the register list");
7547   return false;
7548 }
7549 
7550 bool ARMAsmParser::validateLDRDSTRD(MCInst &Inst,
7551                                     const OperandVector &Operands,
7552                                     bool Load, bool ARMMode, bool Writeback) {
7553   unsigned RtIndex = Load || !Writeback ? 0 : 1;
7554   unsigned Rt = MRI->getEncodingValue(Inst.getOperand(RtIndex).getReg());
7555   unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(RtIndex + 1).getReg());
7556 
7557   if (ARMMode) {
7558     // Rt can't be R14.
7559     if (Rt == 14)
7560       return Error(Operands[3]->getStartLoc(),
7561                   "Rt can't be R14");
7562 
7563     // Rt must be even-numbered.
7564     if ((Rt & 1) == 1)
7565       return Error(Operands[3]->getStartLoc(),
7566                    "Rt must be even-numbered");
7567 
7568     // Rt2 must be Rt + 1.
7569     if (Rt2 != Rt + 1) {
7570       if (Load)
7571         return Error(Operands[3]->getStartLoc(),
7572                      "destination operands must be sequential");
7573       else
7574         return Error(Operands[3]->getStartLoc(),
7575                      "source operands must be sequential");
7576     }
7577 
7578     // FIXME: Diagnose m == 15
7579     // FIXME: Diagnose ldrd with m == t || m == t2.
7580   }
7581 
7582   if (!ARMMode && Load) {
7583     if (Rt2 == Rt)
7584       return Error(Operands[3]->getStartLoc(),
7585                    "destination operands can't be identical");
7586   }
7587 
7588   if (Writeback) {
7589     unsigned Rn = MRI->getEncodingValue(Inst.getOperand(3).getReg());
7590 
7591     if (Rn == Rt || Rn == Rt2) {
7592       if (Load)
7593         return Error(Operands[3]->getStartLoc(),
7594                      "base register needs to be different from destination "
7595                      "registers");
7596       else
7597         return Error(Operands[3]->getStartLoc(),
7598                      "source register and base register can't be identical");
7599     }
7600 
7601     // FIXME: Diagnose ldrd/strd with writeback and n == 15.
7602     // (Except the immediate form of ldrd?)
7603   }
7604 
7605   return false;
7606 }
7607 
7608 static int findFirstVectorPredOperandIdx(const MCInstrDesc &MCID) {
7609   for (unsigned i = 0; i < MCID.NumOperands; ++i) {
7610     if (ARM::isVpred(MCID.OpInfo[i].OperandType))
7611       return i;
7612   }
7613   return -1;
7614 }
7615 
7616 static bool isVectorPredicable(const MCInstrDesc &MCID) {
7617   return findFirstVectorPredOperandIdx(MCID) != -1;
7618 }
7619 
7620 // FIXME: We would really like to be able to tablegen'erate this.
7621 bool ARMAsmParser::validateInstruction(MCInst &Inst,
7622                                        const OperandVector &Operands) {
7623   const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
7624   SMLoc Loc = Operands[0]->getStartLoc();
7625 
7626   // Check the IT block state first.
7627   // NOTE: BKPT and HLT instructions have the interesting property of being
7628   // allowed in IT blocks, but not being predicable. They just always execute.
7629   if (inITBlock() && !instIsBreakpoint(Inst)) {
7630     // The instruction must be predicable.
7631     if (!MCID.isPredicable())
7632       return Error(Loc, "instructions in IT block must be predicable");
7633     ARMCC::CondCodes Cond = ARMCC::CondCodes(
7634         Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm());
7635     if (Cond != currentITCond()) {
7636       // Find the condition code Operand to get its SMLoc information.
7637       SMLoc CondLoc;
7638       for (unsigned I = 1; I < Operands.size(); ++I)
7639         if (static_cast<ARMOperand &>(*Operands[I]).isCondCode())
7640           CondLoc = Operands[I]->getStartLoc();
7641       return Error(CondLoc, "incorrect condition in IT block; got '" +
7642                                 StringRef(ARMCondCodeToString(Cond)) +
7643                                 "', but expected '" +
7644                                 ARMCondCodeToString(currentITCond()) + "'");
7645     }
7646   // Check for non-'al' condition codes outside of the IT block.
7647   } else if (isThumbTwo() && MCID.isPredicable() &&
7648              Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
7649              ARMCC::AL && Inst.getOpcode() != ARM::tBcc &&
7650              Inst.getOpcode() != ARM::t2Bcc &&
7651              Inst.getOpcode() != ARM::t2BFic) {
7652     return Error(Loc, "predicated instructions must be in IT block");
7653   } else if (!isThumb() && !useImplicitITARM() && MCID.isPredicable() &&
7654              Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
7655                  ARMCC::AL) {
7656     return Warning(Loc, "predicated instructions should be in IT block");
7657   } else if (!MCID.isPredicable()) {
7658     // Check the instruction doesn't have a predicate operand anyway
7659     // that it's not allowed to use. Sometimes this happens in order
7660     // to keep instructions the same shape even though one cannot
7661     // legally be predicated, e.g. vmul.f16 vs vmul.f32.
7662     for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i) {
7663       if (MCID.OpInfo[i].isPredicate()) {
7664         if (Inst.getOperand(i).getImm() != ARMCC::AL)
7665           return Error(Loc, "instruction is not predicable");
7666         break;
7667       }
7668     }
7669   }
7670 
7671   // PC-setting instructions in an IT block, but not the last instruction of
7672   // the block, are UNPREDICTABLE.
7673   if (inExplicitITBlock() && !lastInITBlock() && isITBlockTerminator(Inst)) {
7674     return Error(Loc, "instruction must be outside of IT block or the last instruction in an IT block");
7675   }
7676 
7677   if (inVPTBlock() && !instIsBreakpoint(Inst)) {
7678     unsigned Bit = extractITMaskBit(VPTState.Mask, VPTState.CurPosition);
7679     if (!isVectorPredicable(MCID))
7680       return Error(Loc, "instruction in VPT block must be predicable");
7681     unsigned Pred = Inst.getOperand(findFirstVectorPredOperandIdx(MCID)).getImm();
7682     unsigned VPTPred = Bit ? ARMVCC::Else : ARMVCC::Then;
7683     if (Pred != VPTPred) {
7684       SMLoc PredLoc;
7685       for (unsigned I = 1; I < Operands.size(); ++I)
7686         if (static_cast<ARMOperand &>(*Operands[I]).isVPTPred())
7687           PredLoc = Operands[I]->getStartLoc();
7688       return Error(PredLoc, "incorrect predication in VPT block; got '" +
7689                    StringRef(ARMVPTPredToString(ARMVCC::VPTCodes(Pred))) +
7690                    "', but expected '" +
7691                    ARMVPTPredToString(ARMVCC::VPTCodes(VPTPred)) + "'");
7692     }
7693   }
7694   else if (isVectorPredicable(MCID) &&
7695            Inst.getOperand(findFirstVectorPredOperandIdx(MCID)).getImm() !=
7696            ARMVCC::None)
7697     return Error(Loc, "VPT predicated instructions must be in VPT block");
7698 
7699   const unsigned Opcode = Inst.getOpcode();
7700   switch (Opcode) {
7701   case ARM::t2IT: {
7702     // Encoding is unpredictable if it ever results in a notional 'NV'
7703     // predicate. Since we don't parse 'NV' directly this means an 'AL'
7704     // predicate with an "else" mask bit.
7705     unsigned Cond = Inst.getOperand(0).getImm();
7706     unsigned Mask = Inst.getOperand(1).getImm();
7707 
7708     // Conditions only allowing a 't' are those with no set bit except
7709     // the lowest-order one that indicates the end of the sequence. In
7710     // other words, powers of 2.
7711     if (Cond == ARMCC::AL && countPopulation(Mask) != 1)
7712       return Error(Loc, "unpredictable IT predicate sequence");
7713     break;
7714   }
7715   case ARM::LDRD:
7716     if (validateLDRDSTRD(Inst, Operands, /*Load*/true, /*ARMMode*/true,
7717                          /*Writeback*/false))
7718       return true;
7719     break;
7720   case ARM::LDRD_PRE:
7721   case ARM::LDRD_POST:
7722     if (validateLDRDSTRD(Inst, Operands, /*Load*/true, /*ARMMode*/true,
7723                          /*Writeback*/true))
7724       return true;
7725     break;
7726   case ARM::t2LDRDi8:
7727     if (validateLDRDSTRD(Inst, Operands, /*Load*/true, /*ARMMode*/false,
7728                          /*Writeback*/false))
7729       return true;
7730     break;
7731   case ARM::t2LDRD_PRE:
7732   case ARM::t2LDRD_POST:
7733     if (validateLDRDSTRD(Inst, Operands, /*Load*/true, /*ARMMode*/false,
7734                          /*Writeback*/true))
7735       return true;
7736     break;
7737   case ARM::t2BXJ: {
7738     const unsigned RmReg = Inst.getOperand(0).getReg();
7739     // Rm = SP is no longer unpredictable in v8-A
7740     if (RmReg == ARM::SP && !hasV8Ops())
7741       return Error(Operands[2]->getStartLoc(),
7742                    "r13 (SP) is an unpredictable operand to BXJ");
7743     return false;
7744   }
7745   case ARM::STRD:
7746     if (validateLDRDSTRD(Inst, Operands, /*Load*/false, /*ARMMode*/true,
7747                          /*Writeback*/false))
7748       return true;
7749     break;
7750   case ARM::STRD_PRE:
7751   case ARM::STRD_POST:
7752     if (validateLDRDSTRD(Inst, Operands, /*Load*/false, /*ARMMode*/true,
7753                          /*Writeback*/true))
7754       return true;
7755     break;
7756   case ARM::t2STRD_PRE:
7757   case ARM::t2STRD_POST:
7758     if (validateLDRDSTRD(Inst, Operands, /*Load*/false, /*ARMMode*/false,
7759                          /*Writeback*/true))
7760       return true;
7761     break;
7762   case ARM::STR_PRE_IMM:
7763   case ARM::STR_PRE_REG:
7764   case ARM::t2STR_PRE:
7765   case ARM::STR_POST_IMM:
7766   case ARM::STR_POST_REG:
7767   case ARM::t2STR_POST:
7768   case ARM::STRH_PRE:
7769   case ARM::t2STRH_PRE:
7770   case ARM::STRH_POST:
7771   case ARM::t2STRH_POST:
7772   case ARM::STRB_PRE_IMM:
7773   case ARM::STRB_PRE_REG:
7774   case ARM::t2STRB_PRE:
7775   case ARM::STRB_POST_IMM:
7776   case ARM::STRB_POST_REG:
7777   case ARM::t2STRB_POST: {
7778     // Rt must be different from Rn.
7779     const unsigned Rt = MRI->getEncodingValue(Inst.getOperand(1).getReg());
7780     const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(2).getReg());
7781 
7782     if (Rt == Rn)
7783       return Error(Operands[3]->getStartLoc(),
7784                    "source register and base register can't be identical");
7785     return false;
7786   }
7787   case ARM::t2LDR_PRE_imm:
7788   case ARM::t2LDR_POST_imm:
7789   case ARM::t2STR_PRE_imm:
7790   case ARM::t2STR_POST_imm: {
7791     // Rt must be different from Rn.
7792     const unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
7793     const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(1).getReg());
7794 
7795     if (Rt == Rn)
7796       return Error(Operands[3]->getStartLoc(),
7797                    "destination register and base register can't be identical");
7798     if (Inst.getOpcode() == ARM::t2LDR_POST_imm ||
7799         Inst.getOpcode() == ARM::t2STR_POST_imm) {
7800       int Imm = Inst.getOperand(2).getImm();
7801       if (Imm > 255 || Imm < -255)
7802         return Error(Operands[5]->getStartLoc(),
7803                      "operand must be in range [-255, 255]");
7804     }
7805     if (Inst.getOpcode() == ARM::t2STR_PRE_imm ||
7806         Inst.getOpcode() == ARM::t2STR_POST_imm) {
7807       if (Inst.getOperand(0).getReg() == ARM::PC) {
7808         return Error(Operands[3]->getStartLoc(),
7809                      "operand must be a register in range [r0, r14]");
7810       }
7811     }
7812     return false;
7813   }
7814   case ARM::LDR_PRE_IMM:
7815   case ARM::LDR_PRE_REG:
7816   case ARM::t2LDR_PRE:
7817   case ARM::LDR_POST_IMM:
7818   case ARM::LDR_POST_REG:
7819   case ARM::t2LDR_POST:
7820   case ARM::LDRH_PRE:
7821   case ARM::t2LDRH_PRE:
7822   case ARM::LDRH_POST:
7823   case ARM::t2LDRH_POST:
7824   case ARM::LDRSH_PRE:
7825   case ARM::t2LDRSH_PRE:
7826   case ARM::LDRSH_POST:
7827   case ARM::t2LDRSH_POST:
7828   case ARM::LDRB_PRE_IMM:
7829   case ARM::LDRB_PRE_REG:
7830   case ARM::t2LDRB_PRE:
7831   case ARM::LDRB_POST_IMM:
7832   case ARM::LDRB_POST_REG:
7833   case ARM::t2LDRB_POST:
7834   case ARM::LDRSB_PRE:
7835   case ARM::t2LDRSB_PRE:
7836   case ARM::LDRSB_POST:
7837   case ARM::t2LDRSB_POST: {
7838     // Rt must be different from Rn.
7839     const unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
7840     const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(2).getReg());
7841 
7842     if (Rt == Rn)
7843       return Error(Operands[3]->getStartLoc(),
7844                    "destination register and base register can't be identical");
7845     return false;
7846   }
7847 
7848   case ARM::MVE_VLDRBU8_rq:
7849   case ARM::MVE_VLDRBU16_rq:
7850   case ARM::MVE_VLDRBS16_rq:
7851   case ARM::MVE_VLDRBU32_rq:
7852   case ARM::MVE_VLDRBS32_rq:
7853   case ARM::MVE_VLDRHU16_rq:
7854   case ARM::MVE_VLDRHU16_rq_u:
7855   case ARM::MVE_VLDRHU32_rq:
7856   case ARM::MVE_VLDRHU32_rq_u:
7857   case ARM::MVE_VLDRHS32_rq:
7858   case ARM::MVE_VLDRHS32_rq_u:
7859   case ARM::MVE_VLDRWU32_rq:
7860   case ARM::MVE_VLDRWU32_rq_u:
7861   case ARM::MVE_VLDRDU64_rq:
7862   case ARM::MVE_VLDRDU64_rq_u:
7863   case ARM::MVE_VLDRWU32_qi:
7864   case ARM::MVE_VLDRWU32_qi_pre:
7865   case ARM::MVE_VLDRDU64_qi:
7866   case ARM::MVE_VLDRDU64_qi_pre: {
7867     // Qd must be different from Qm.
7868     unsigned QdIdx = 0, QmIdx = 2;
7869     bool QmIsPointer = false;
7870     switch (Opcode) {
7871     case ARM::MVE_VLDRWU32_qi:
7872     case ARM::MVE_VLDRDU64_qi:
7873       QmIdx = 1;
7874       QmIsPointer = true;
7875       break;
7876     case ARM::MVE_VLDRWU32_qi_pre:
7877     case ARM::MVE_VLDRDU64_qi_pre:
7878       QdIdx = 1;
7879       QmIsPointer = true;
7880       break;
7881     }
7882 
7883     const unsigned Qd = MRI->getEncodingValue(Inst.getOperand(QdIdx).getReg());
7884     const unsigned Qm = MRI->getEncodingValue(Inst.getOperand(QmIdx).getReg());
7885 
7886     if (Qd == Qm) {
7887       return Error(Operands[3]->getStartLoc(),
7888                    Twine("destination vector register and vector ") +
7889                    (QmIsPointer ? "pointer" : "offset") +
7890                    " register can't be identical");
7891     }
7892     return false;
7893   }
7894 
7895   case ARM::SBFX:
7896   case ARM::t2SBFX:
7897   case ARM::UBFX:
7898   case ARM::t2UBFX: {
7899     // Width must be in range [1, 32-lsb].
7900     unsigned LSB = Inst.getOperand(2).getImm();
7901     unsigned Widthm1 = Inst.getOperand(3).getImm();
7902     if (Widthm1 >= 32 - LSB)
7903       return Error(Operands[5]->getStartLoc(),
7904                    "bitfield width must be in range [1,32-lsb]");
7905     return false;
7906   }
7907   // Notionally handles ARM::tLDMIA_UPD too.
7908   case ARM::tLDMIA: {
7909     // If we're parsing Thumb2, the .w variant is available and handles
7910     // most cases that are normally illegal for a Thumb1 LDM instruction.
7911     // We'll make the transformation in processInstruction() if necessary.
7912     //
7913     // Thumb LDM instructions are writeback iff the base register is not
7914     // in the register list.
7915     unsigned Rn = Inst.getOperand(0).getReg();
7916     bool HasWritebackToken =
7917         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
7918          static_cast<ARMOperand &>(*Operands[3]).getToken() == "!");
7919     bool ListContainsBase;
7920     if (checkLowRegisterList(Inst, 3, Rn, 0, ListContainsBase) && !isThumbTwo())
7921       return Error(Operands[3 + HasWritebackToken]->getStartLoc(),
7922                    "registers must be in range r0-r7");
7923     // If we should have writeback, then there should be a '!' token.
7924     if (!ListContainsBase && !HasWritebackToken && !isThumbTwo())
7925       return Error(Operands[2]->getStartLoc(),
7926                    "writeback operator '!' expected");
7927     // If we should not have writeback, there must not be a '!'. This is
7928     // true even for the 32-bit wide encodings.
7929     if (ListContainsBase && HasWritebackToken)
7930       return Error(Operands[3]->getStartLoc(),
7931                    "writeback operator '!' not allowed when base register "
7932                    "in register list");
7933 
7934     if (validatetLDMRegList(Inst, Operands, 3))
7935       return true;
7936     break;
7937   }
7938   case ARM::LDMIA_UPD:
7939   case ARM::LDMDB_UPD:
7940   case ARM::LDMIB_UPD:
7941   case ARM::LDMDA_UPD:
7942     // ARM variants loading and updating the same register are only officially
7943     // UNPREDICTABLE on v7 upwards. Goodness knows what they did before.
7944     if (!hasV7Ops())
7945       break;
7946     if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
7947       return Error(Operands.back()->getStartLoc(),
7948                    "writeback register not allowed in register list");
7949     break;
7950   case ARM::t2LDMIA:
7951   case ARM::t2LDMDB:
7952     if (validatetLDMRegList(Inst, Operands, 3))
7953       return true;
7954     break;
7955   case ARM::t2STMIA:
7956   case ARM::t2STMDB:
7957     if (validatetSTMRegList(Inst, Operands, 3))
7958       return true;
7959     break;
7960   case ARM::t2LDMIA_UPD:
7961   case ARM::t2LDMDB_UPD:
7962   case ARM::t2STMIA_UPD:
7963   case ARM::t2STMDB_UPD:
7964     if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
7965       return Error(Operands.back()->getStartLoc(),
7966                    "writeback register not allowed in register list");
7967 
7968     if (Opcode == ARM::t2LDMIA_UPD || Opcode == ARM::t2LDMDB_UPD) {
7969       if (validatetLDMRegList(Inst, Operands, 3))
7970         return true;
7971     } else {
7972       if (validatetSTMRegList(Inst, Operands, 3))
7973         return true;
7974     }
7975     break;
7976 
7977   case ARM::sysLDMIA_UPD:
7978   case ARM::sysLDMDA_UPD:
7979   case ARM::sysLDMDB_UPD:
7980   case ARM::sysLDMIB_UPD:
7981     if (!listContainsReg(Inst, 3, ARM::PC))
7982       return Error(Operands[4]->getStartLoc(),
7983                    "writeback register only allowed on system LDM "
7984                    "if PC in register-list");
7985     break;
7986   case ARM::sysSTMIA_UPD:
7987   case ARM::sysSTMDA_UPD:
7988   case ARM::sysSTMDB_UPD:
7989   case ARM::sysSTMIB_UPD:
7990     return Error(Operands[2]->getStartLoc(),
7991                  "system STM cannot have writeback register");
7992   case ARM::tMUL:
7993     // The second source operand must be the same register as the destination
7994     // operand.
7995     //
7996     // In this case, we must directly check the parsed operands because the
7997     // cvtThumbMultiply() function is written in such a way that it guarantees
7998     // this first statement is always true for the new Inst.  Essentially, the
7999     // destination is unconditionally copied into the second source operand
8000     // without checking to see if it matches what we actually parsed.
8001     if (Operands.size() == 6 && (((ARMOperand &)*Operands[3]).getReg() !=
8002                                  ((ARMOperand &)*Operands[5]).getReg()) &&
8003         (((ARMOperand &)*Operands[3]).getReg() !=
8004          ((ARMOperand &)*Operands[4]).getReg())) {
8005       return Error(Operands[3]->getStartLoc(),
8006                    "destination register must match source register");
8007     }
8008     break;
8009 
8010   // Like for ldm/stm, push and pop have hi-reg handling version in Thumb2,
8011   // so only issue a diagnostic for thumb1. The instructions will be
8012   // switched to the t2 encodings in processInstruction() if necessary.
8013   case ARM::tPOP: {
8014     bool ListContainsBase;
8015     if (checkLowRegisterList(Inst, 2, 0, ARM::PC, ListContainsBase) &&
8016         !isThumbTwo())
8017       return Error(Operands[2]->getStartLoc(),
8018                    "registers must be in range r0-r7 or pc");
8019     if (validatetLDMRegList(Inst, Operands, 2, !isMClass()))
8020       return true;
8021     break;
8022   }
8023   case ARM::tPUSH: {
8024     bool ListContainsBase;
8025     if (checkLowRegisterList(Inst, 2, 0, ARM::LR, ListContainsBase) &&
8026         !isThumbTwo())
8027       return Error(Operands[2]->getStartLoc(),
8028                    "registers must be in range r0-r7 or lr");
8029     if (validatetSTMRegList(Inst, Operands, 2))
8030       return true;
8031     break;
8032   }
8033   case ARM::tSTMIA_UPD: {
8034     bool ListContainsBase, InvalidLowList;
8035     InvalidLowList = checkLowRegisterList(Inst, 4, Inst.getOperand(0).getReg(),
8036                                           0, ListContainsBase);
8037     if (InvalidLowList && !isThumbTwo())
8038       return Error(Operands[4]->getStartLoc(),
8039                    "registers must be in range r0-r7");
8040 
8041     // This would be converted to a 32-bit stm, but that's not valid if the
8042     // writeback register is in the list.
8043     if (InvalidLowList && ListContainsBase)
8044       return Error(Operands[4]->getStartLoc(),
8045                    "writeback operator '!' not allowed when base register "
8046                    "in register list");
8047 
8048     if (validatetSTMRegList(Inst, Operands, 4))
8049       return true;
8050     break;
8051   }
8052   case ARM::tADDrSP:
8053     // If the non-SP source operand and the destination operand are not the
8054     // same, we need thumb2 (for the wide encoding), or we have an error.
8055     if (!isThumbTwo() &&
8056         Inst.getOperand(0).getReg() != Inst.getOperand(2).getReg()) {
8057       return Error(Operands[4]->getStartLoc(),
8058                    "source register must be the same as destination");
8059     }
8060     break;
8061 
8062   case ARM::t2ADDrr:
8063   case ARM::t2ADDrs:
8064   case ARM::t2SUBrr:
8065   case ARM::t2SUBrs:
8066     if (Inst.getOperand(0).getReg() == ARM::SP &&
8067         Inst.getOperand(1).getReg() != ARM::SP)
8068       return Error(Operands[4]->getStartLoc(),
8069                    "source register must be sp if destination is sp");
8070     break;
8071 
8072   // Final range checking for Thumb unconditional branch instructions.
8073   case ARM::tB:
8074     if (!(static_cast<ARMOperand &>(*Operands[2])).isSignedOffset<11, 1>())
8075       return Error(Operands[2]->getStartLoc(), "branch target out of range");
8076     break;
8077   case ARM::t2B: {
8078     int op = (Operands[2]->isImm()) ? 2 : 3;
8079     ARMOperand &Operand = static_cast<ARMOperand &>(*Operands[op]);
8080     // Delay the checks of symbolic expressions until they are resolved.
8081     if (!isa<MCBinaryExpr>(Operand.getImm()) &&
8082         !Operand.isSignedOffset<24, 1>())
8083       return Error(Operands[op]->getStartLoc(), "branch target out of range");
8084     break;
8085   }
8086   // Final range checking for Thumb conditional branch instructions.
8087   case ARM::tBcc:
8088     if (!static_cast<ARMOperand &>(*Operands[2]).isSignedOffset<8, 1>())
8089       return Error(Operands[2]->getStartLoc(), "branch target out of range");
8090     break;
8091   case ARM::t2Bcc: {
8092     int Op = (Operands[2]->isImm()) ? 2 : 3;
8093     if (!static_cast<ARMOperand &>(*Operands[Op]).isSignedOffset<20, 1>())
8094       return Error(Operands[Op]->getStartLoc(), "branch target out of range");
8095     break;
8096   }
8097   case ARM::tCBZ:
8098   case ARM::tCBNZ: {
8099     if (!static_cast<ARMOperand &>(*Operands[2]).isUnsignedOffset<6, 1>())
8100       return Error(Operands[2]->getStartLoc(), "branch target out of range");
8101     break;
8102   }
8103   case ARM::MOVi16:
8104   case ARM::MOVTi16:
8105   case ARM::t2MOVi16:
8106   case ARM::t2MOVTi16:
8107     {
8108     // We want to avoid misleadingly allowing something like "mov r0, <symbol>"
8109     // especially when we turn it into a movw and the expression <symbol> does
8110     // not have a :lower16: or :upper16 as part of the expression.  We don't
8111     // want the behavior of silently truncating, which can be unexpected and
8112     // lead to bugs that are difficult to find since this is an easy mistake
8113     // to make.
8114     int i = (Operands[3]->isImm()) ? 3 : 4;
8115     ARMOperand &Op = static_cast<ARMOperand &>(*Operands[i]);
8116     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm());
8117     if (CE) break;
8118     const MCExpr *E = dyn_cast<MCExpr>(Op.getImm());
8119     if (!E) break;
8120     const ARMMCExpr *ARM16Expr = dyn_cast<ARMMCExpr>(E);
8121     if (!ARM16Expr || (ARM16Expr->getKind() != ARMMCExpr::VK_ARM_HI16 &&
8122                        ARM16Expr->getKind() != ARMMCExpr::VK_ARM_LO16))
8123       return Error(
8124           Op.getStartLoc(),
8125           "immediate expression for mov requires :lower16: or :upper16");
8126     break;
8127   }
8128   case ARM::HINT:
8129   case ARM::t2HINT: {
8130     unsigned Imm8 = Inst.getOperand(0).getImm();
8131     unsigned Pred = Inst.getOperand(1).getImm();
8132     // ESB is not predicable (pred must be AL). Without the RAS extension, this
8133     // behaves as any other unallocated hint.
8134     if (Imm8 == 0x10 && Pred != ARMCC::AL && hasRAS())
8135       return Error(Operands[1]->getStartLoc(), "instruction 'esb' is not "
8136                                                "predicable, but condition "
8137                                                "code specified");
8138     if (Imm8 == 0x14 && Pred != ARMCC::AL)
8139       return Error(Operands[1]->getStartLoc(), "instruction 'csdb' is not "
8140                                                "predicable, but condition "
8141                                                "code specified");
8142     break;
8143   }
8144   case ARM::t2BFi:
8145   case ARM::t2BFr:
8146   case ARM::t2BFLi:
8147   case ARM::t2BFLr: {
8148     if (!static_cast<ARMOperand &>(*Operands[2]).isUnsignedOffset<4, 1>() ||
8149         (Inst.getOperand(0).isImm() && Inst.getOperand(0).getImm() == 0))
8150       return Error(Operands[2]->getStartLoc(),
8151                    "branch location out of range or not a multiple of 2");
8152 
8153     if (Opcode == ARM::t2BFi) {
8154       if (!static_cast<ARMOperand &>(*Operands[3]).isSignedOffset<16, 1>())
8155         return Error(Operands[3]->getStartLoc(),
8156                      "branch target out of range or not a multiple of 2");
8157     } else if (Opcode == ARM::t2BFLi) {
8158       if (!static_cast<ARMOperand &>(*Operands[3]).isSignedOffset<18, 1>())
8159         return Error(Operands[3]->getStartLoc(),
8160                      "branch target out of range or not a multiple of 2");
8161     }
8162     break;
8163   }
8164   case ARM::t2BFic: {
8165     if (!static_cast<ARMOperand &>(*Operands[1]).isUnsignedOffset<4, 1>() ||
8166         (Inst.getOperand(0).isImm() && Inst.getOperand(0).getImm() == 0))
8167       return Error(Operands[1]->getStartLoc(),
8168                    "branch location out of range or not a multiple of 2");
8169 
8170     if (!static_cast<ARMOperand &>(*Operands[2]).isSignedOffset<16, 1>())
8171       return Error(Operands[2]->getStartLoc(),
8172                    "branch target out of range or not a multiple of 2");
8173 
8174     assert(Inst.getOperand(0).isImm() == Inst.getOperand(2).isImm() &&
8175            "branch location and else branch target should either both be "
8176            "immediates or both labels");
8177 
8178     if (Inst.getOperand(0).isImm() && Inst.getOperand(2).isImm()) {
8179       int Diff = Inst.getOperand(2).getImm() - Inst.getOperand(0).getImm();
8180       if (Diff != 4 && Diff != 2)
8181         return Error(
8182             Operands[3]->getStartLoc(),
8183             "else branch target must be 2 or 4 greater than the branch location");
8184     }
8185     break;
8186   }
8187   case ARM::t2CLRM: {
8188     for (unsigned i = 2; i < Inst.getNumOperands(); i++) {
8189       if (Inst.getOperand(i).isReg() &&
8190           !ARMMCRegisterClasses[ARM::GPRwithAPSRnospRegClassID].contains(
8191               Inst.getOperand(i).getReg())) {
8192         return Error(Operands[2]->getStartLoc(),
8193                      "invalid register in register list. Valid registers are "
8194                      "r0-r12, lr/r14 and APSR.");
8195       }
8196     }
8197     break;
8198   }
8199   case ARM::DSB:
8200   case ARM::t2DSB: {
8201 
8202     if (Inst.getNumOperands() < 2)
8203       break;
8204 
8205     unsigned Option = Inst.getOperand(0).getImm();
8206     unsigned Pred = Inst.getOperand(1).getImm();
8207 
8208     // SSBB and PSSBB (DSB #0|#4) are not predicable (pred must be AL).
8209     if (Option == 0 && Pred != ARMCC::AL)
8210       return Error(Operands[1]->getStartLoc(),
8211                    "instruction 'ssbb' is not predicable, but condition code "
8212                    "specified");
8213     if (Option == 4 && Pred != ARMCC::AL)
8214       return Error(Operands[1]->getStartLoc(),
8215                    "instruction 'pssbb' is not predicable, but condition code "
8216                    "specified");
8217     break;
8218   }
8219   case ARM::VMOVRRS: {
8220     // Source registers must be sequential.
8221     const unsigned Sm = MRI->getEncodingValue(Inst.getOperand(2).getReg());
8222     const unsigned Sm1 = MRI->getEncodingValue(Inst.getOperand(3).getReg());
8223     if (Sm1 != Sm + 1)
8224       return Error(Operands[5]->getStartLoc(),
8225                    "source operands must be sequential");
8226     break;
8227   }
8228   case ARM::VMOVSRR: {
8229     // Destination registers must be sequential.
8230     const unsigned Sm = MRI->getEncodingValue(Inst.getOperand(0).getReg());
8231     const unsigned Sm1 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
8232     if (Sm1 != Sm + 1)
8233       return Error(Operands[3]->getStartLoc(),
8234                    "destination operands must be sequential");
8235     break;
8236   }
8237   case ARM::VLDMDIA:
8238   case ARM::VSTMDIA: {
8239     ARMOperand &Op = static_cast<ARMOperand&>(*Operands[3]);
8240     auto &RegList = Op.getRegList();
8241     if (RegList.size() < 1 || RegList.size() > 16)
8242       return Error(Operands[3]->getStartLoc(),
8243                    "list of registers must be at least 1 and at most 16");
8244     break;
8245   }
8246   case ARM::MVE_VQDMULLs32bh:
8247   case ARM::MVE_VQDMULLs32th:
8248   case ARM::MVE_VCMULf32:
8249   case ARM::MVE_VMULLBs32:
8250   case ARM::MVE_VMULLTs32:
8251   case ARM::MVE_VMULLBu32:
8252   case ARM::MVE_VMULLTu32: {
8253     if (Operands[3]->getReg() == Operands[4]->getReg()) {
8254       return Error (Operands[3]->getStartLoc(),
8255                     "Qd register and Qn register can't be identical");
8256     }
8257     if (Operands[3]->getReg() == Operands[5]->getReg()) {
8258       return Error (Operands[3]->getStartLoc(),
8259                     "Qd register and Qm register can't be identical");
8260     }
8261     break;
8262   }
8263   case ARM::MVE_VMOV_rr_q: {
8264     if (Operands[4]->getReg() != Operands[6]->getReg())
8265       return Error (Operands[4]->getStartLoc(), "Q-registers must be the same");
8266     if (static_cast<ARMOperand &>(*Operands[5]).getVectorIndex() !=
8267         static_cast<ARMOperand &>(*Operands[7]).getVectorIndex() + 2)
8268       return Error (Operands[5]->getStartLoc(), "Q-register indexes must be 2 and 0 or 3 and 1");
8269     break;
8270   }
8271   case ARM::MVE_VMOV_q_rr: {
8272     if (Operands[2]->getReg() != Operands[4]->getReg())
8273       return Error (Operands[2]->getStartLoc(), "Q-registers must be the same");
8274     if (static_cast<ARMOperand &>(*Operands[3]).getVectorIndex() !=
8275         static_cast<ARMOperand &>(*Operands[5]).getVectorIndex() + 2)
8276       return Error (Operands[3]->getStartLoc(), "Q-register indexes must be 2 and 0 or 3 and 1");
8277     break;
8278   }
8279   case ARM::UMAAL:
8280   case ARM::UMLAL:
8281   case ARM::UMULL:
8282   case ARM::t2UMAAL:
8283   case ARM::t2UMLAL:
8284   case ARM::t2UMULL:
8285   case ARM::SMLAL:
8286   case ARM::SMLALBB:
8287   case ARM::SMLALBT:
8288   case ARM::SMLALD:
8289   case ARM::SMLALDX:
8290   case ARM::SMLALTB:
8291   case ARM::SMLALTT:
8292   case ARM::SMLSLD:
8293   case ARM::SMLSLDX:
8294   case ARM::SMULL:
8295   case ARM::t2SMLAL:
8296   case ARM::t2SMLALBB:
8297   case ARM::t2SMLALBT:
8298   case ARM::t2SMLALD:
8299   case ARM::t2SMLALDX:
8300   case ARM::t2SMLALTB:
8301   case ARM::t2SMLALTT:
8302   case ARM::t2SMLSLD:
8303   case ARM::t2SMLSLDX:
8304   case ARM::t2SMULL: {
8305     unsigned RdHi = Inst.getOperand(0).getReg();
8306     unsigned RdLo = Inst.getOperand(1).getReg();
8307     if(RdHi == RdLo) {
8308       return Error(Loc,
8309                    "unpredictable instruction, RdHi and RdLo must be different");
8310     }
8311     break;
8312   }
8313 
8314   case ARM::CDE_CX1:
8315   case ARM::CDE_CX1A:
8316   case ARM::CDE_CX1D:
8317   case ARM::CDE_CX1DA:
8318   case ARM::CDE_CX2:
8319   case ARM::CDE_CX2A:
8320   case ARM::CDE_CX2D:
8321   case ARM::CDE_CX2DA:
8322   case ARM::CDE_CX3:
8323   case ARM::CDE_CX3A:
8324   case ARM::CDE_CX3D:
8325   case ARM::CDE_CX3DA:
8326   case ARM::CDE_VCX1_vec:
8327   case ARM::CDE_VCX1_fpsp:
8328   case ARM::CDE_VCX1_fpdp:
8329   case ARM::CDE_VCX1A_vec:
8330   case ARM::CDE_VCX1A_fpsp:
8331   case ARM::CDE_VCX1A_fpdp:
8332   case ARM::CDE_VCX2_vec:
8333   case ARM::CDE_VCX2_fpsp:
8334   case ARM::CDE_VCX2_fpdp:
8335   case ARM::CDE_VCX2A_vec:
8336   case ARM::CDE_VCX2A_fpsp:
8337   case ARM::CDE_VCX2A_fpdp:
8338   case ARM::CDE_VCX3_vec:
8339   case ARM::CDE_VCX3_fpsp:
8340   case ARM::CDE_VCX3_fpdp:
8341   case ARM::CDE_VCX3A_vec:
8342   case ARM::CDE_VCX3A_fpsp:
8343   case ARM::CDE_VCX3A_fpdp: {
8344     assert(Inst.getOperand(1).isImm() &&
8345            "CDE operand 1 must be a coprocessor ID");
8346     int64_t Coproc = Inst.getOperand(1).getImm();
8347     if (Coproc < 8 && !ARM::isCDECoproc(Coproc, *STI))
8348       return Error(Operands[1]->getStartLoc(),
8349                    "coprocessor must be configured as CDE");
8350     else if (Coproc >= 8)
8351       return Error(Operands[1]->getStartLoc(),
8352                    "coprocessor must be in the range [p0, p7]");
8353     break;
8354   }
8355 
8356   case ARM::t2CDP:
8357   case ARM::t2CDP2:
8358   case ARM::t2LDC2L_OFFSET:
8359   case ARM::t2LDC2L_OPTION:
8360   case ARM::t2LDC2L_POST:
8361   case ARM::t2LDC2L_PRE:
8362   case ARM::t2LDC2_OFFSET:
8363   case ARM::t2LDC2_OPTION:
8364   case ARM::t2LDC2_POST:
8365   case ARM::t2LDC2_PRE:
8366   case ARM::t2LDCL_OFFSET:
8367   case ARM::t2LDCL_OPTION:
8368   case ARM::t2LDCL_POST:
8369   case ARM::t2LDCL_PRE:
8370   case ARM::t2LDC_OFFSET:
8371   case ARM::t2LDC_OPTION:
8372   case ARM::t2LDC_POST:
8373   case ARM::t2LDC_PRE:
8374   case ARM::t2MCR:
8375   case ARM::t2MCR2:
8376   case ARM::t2MCRR:
8377   case ARM::t2MCRR2:
8378   case ARM::t2MRC:
8379   case ARM::t2MRC2:
8380   case ARM::t2MRRC:
8381   case ARM::t2MRRC2:
8382   case ARM::t2STC2L_OFFSET:
8383   case ARM::t2STC2L_OPTION:
8384   case ARM::t2STC2L_POST:
8385   case ARM::t2STC2L_PRE:
8386   case ARM::t2STC2_OFFSET:
8387   case ARM::t2STC2_OPTION:
8388   case ARM::t2STC2_POST:
8389   case ARM::t2STC2_PRE:
8390   case ARM::t2STCL_OFFSET:
8391   case ARM::t2STCL_OPTION:
8392   case ARM::t2STCL_POST:
8393   case ARM::t2STCL_PRE:
8394   case ARM::t2STC_OFFSET:
8395   case ARM::t2STC_OPTION:
8396   case ARM::t2STC_POST:
8397   case ARM::t2STC_PRE: {
8398     unsigned Opcode = Inst.getOpcode();
8399     // Inst.getOperand indexes operands in the (oops ...) and (iops ...) dags,
8400     // CopInd is the index of the coprocessor operand.
8401     size_t CopInd = 0;
8402     if (Opcode == ARM::t2MRRC || Opcode == ARM::t2MRRC2)
8403       CopInd = 2;
8404     else if (Opcode == ARM::t2MRC || Opcode == ARM::t2MRC2)
8405       CopInd = 1;
8406     assert(Inst.getOperand(CopInd).isImm() &&
8407            "Operand must be a coprocessor ID");
8408     int64_t Coproc = Inst.getOperand(CopInd).getImm();
8409     // Operands[2] is the coprocessor operand at syntactic level
8410     if (ARM::isCDECoproc(Coproc, *STI))
8411       return Error(Operands[2]->getStartLoc(),
8412                    "coprocessor must be configured as GCP");
8413     break;
8414   }
8415   }
8416 
8417   return false;
8418 }
8419 
8420 static unsigned getRealVSTOpcode(unsigned Opc, unsigned &Spacing) {
8421   switch(Opc) {
8422   default: llvm_unreachable("unexpected opcode!");
8423   // VST1LN
8424   case ARM::VST1LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST1LNd8_UPD;
8425   case ARM::VST1LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST1LNd16_UPD;
8426   case ARM::VST1LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST1LNd32_UPD;
8427   case ARM::VST1LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST1LNd8_UPD;
8428   case ARM::VST1LNdWB_register_Asm_16: Spacing = 1; return ARM::VST1LNd16_UPD;
8429   case ARM::VST1LNdWB_register_Asm_32: Spacing = 1; return ARM::VST1LNd32_UPD;
8430   case ARM::VST1LNdAsm_8:  Spacing = 1; return ARM::VST1LNd8;
8431   case ARM::VST1LNdAsm_16: Spacing = 1; return ARM::VST1LNd16;
8432   case ARM::VST1LNdAsm_32: Spacing = 1; return ARM::VST1LNd32;
8433 
8434   // VST2LN
8435   case ARM::VST2LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST2LNd8_UPD;
8436   case ARM::VST2LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST2LNd16_UPD;
8437   case ARM::VST2LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST2LNd32_UPD;
8438   case ARM::VST2LNqWB_fixed_Asm_16: Spacing = 2; return ARM::VST2LNq16_UPD;
8439   case ARM::VST2LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST2LNq32_UPD;
8440 
8441   case ARM::VST2LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST2LNd8_UPD;
8442   case ARM::VST2LNdWB_register_Asm_16: Spacing = 1; return ARM::VST2LNd16_UPD;
8443   case ARM::VST2LNdWB_register_Asm_32: Spacing = 1; return ARM::VST2LNd32_UPD;
8444   case ARM::VST2LNqWB_register_Asm_16: Spacing = 2; return ARM::VST2LNq16_UPD;
8445   case ARM::VST2LNqWB_register_Asm_32: Spacing = 2; return ARM::VST2LNq32_UPD;
8446 
8447   case ARM::VST2LNdAsm_8:  Spacing = 1; return ARM::VST2LNd8;
8448   case ARM::VST2LNdAsm_16: Spacing = 1; return ARM::VST2LNd16;
8449   case ARM::VST2LNdAsm_32: Spacing = 1; return ARM::VST2LNd32;
8450   case ARM::VST2LNqAsm_16: Spacing = 2; return ARM::VST2LNq16;
8451   case ARM::VST2LNqAsm_32: Spacing = 2; return ARM::VST2LNq32;
8452 
8453   // VST3LN
8454   case ARM::VST3LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST3LNd8_UPD;
8455   case ARM::VST3LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST3LNd16_UPD;
8456   case ARM::VST3LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST3LNd32_UPD;
8457   case ARM::VST3LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VST3LNq16_UPD;
8458   case ARM::VST3LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST3LNq32_UPD;
8459   case ARM::VST3LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST3LNd8_UPD;
8460   case ARM::VST3LNdWB_register_Asm_16: Spacing = 1; return ARM::VST3LNd16_UPD;
8461   case ARM::VST3LNdWB_register_Asm_32: Spacing = 1; return ARM::VST3LNd32_UPD;
8462   case ARM::VST3LNqWB_register_Asm_16: Spacing = 2; return ARM::VST3LNq16_UPD;
8463   case ARM::VST3LNqWB_register_Asm_32: Spacing = 2; return ARM::VST3LNq32_UPD;
8464   case ARM::VST3LNdAsm_8:  Spacing = 1; return ARM::VST3LNd8;
8465   case ARM::VST3LNdAsm_16: Spacing = 1; return ARM::VST3LNd16;
8466   case ARM::VST3LNdAsm_32: Spacing = 1; return ARM::VST3LNd32;
8467   case ARM::VST3LNqAsm_16: Spacing = 2; return ARM::VST3LNq16;
8468   case ARM::VST3LNqAsm_32: Spacing = 2; return ARM::VST3LNq32;
8469 
8470   // VST3
8471   case ARM::VST3dWB_fixed_Asm_8:  Spacing = 1; return ARM::VST3d8_UPD;
8472   case ARM::VST3dWB_fixed_Asm_16: Spacing = 1; return ARM::VST3d16_UPD;
8473   case ARM::VST3dWB_fixed_Asm_32: Spacing = 1; return ARM::VST3d32_UPD;
8474   case ARM::VST3qWB_fixed_Asm_8:  Spacing = 2; return ARM::VST3q8_UPD;
8475   case ARM::VST3qWB_fixed_Asm_16: Spacing = 2; return ARM::VST3q16_UPD;
8476   case ARM::VST3qWB_fixed_Asm_32: Spacing = 2; return ARM::VST3q32_UPD;
8477   case ARM::VST3dWB_register_Asm_8:  Spacing = 1; return ARM::VST3d8_UPD;
8478   case ARM::VST3dWB_register_Asm_16: Spacing = 1; return ARM::VST3d16_UPD;
8479   case ARM::VST3dWB_register_Asm_32: Spacing = 1; return ARM::VST3d32_UPD;
8480   case ARM::VST3qWB_register_Asm_8:  Spacing = 2; return ARM::VST3q8_UPD;
8481   case ARM::VST3qWB_register_Asm_16: Spacing = 2; return ARM::VST3q16_UPD;
8482   case ARM::VST3qWB_register_Asm_32: Spacing = 2; return ARM::VST3q32_UPD;
8483   case ARM::VST3dAsm_8:  Spacing = 1; return ARM::VST3d8;
8484   case ARM::VST3dAsm_16: Spacing = 1; return ARM::VST3d16;
8485   case ARM::VST3dAsm_32: Spacing = 1; return ARM::VST3d32;
8486   case ARM::VST3qAsm_8:  Spacing = 2; return ARM::VST3q8;
8487   case ARM::VST3qAsm_16: Spacing = 2; return ARM::VST3q16;
8488   case ARM::VST3qAsm_32: Spacing = 2; return ARM::VST3q32;
8489 
8490   // VST4LN
8491   case ARM::VST4LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST4LNd8_UPD;
8492   case ARM::VST4LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST4LNd16_UPD;
8493   case ARM::VST4LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST4LNd32_UPD;
8494   case ARM::VST4LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VST4LNq16_UPD;
8495   case ARM::VST4LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST4LNq32_UPD;
8496   case ARM::VST4LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST4LNd8_UPD;
8497   case ARM::VST4LNdWB_register_Asm_16: Spacing = 1; return ARM::VST4LNd16_UPD;
8498   case ARM::VST4LNdWB_register_Asm_32: Spacing = 1; return ARM::VST4LNd32_UPD;
8499   case ARM::VST4LNqWB_register_Asm_16: Spacing = 2; return ARM::VST4LNq16_UPD;
8500   case ARM::VST4LNqWB_register_Asm_32: Spacing = 2; return ARM::VST4LNq32_UPD;
8501   case ARM::VST4LNdAsm_8:  Spacing = 1; return ARM::VST4LNd8;
8502   case ARM::VST4LNdAsm_16: Spacing = 1; return ARM::VST4LNd16;
8503   case ARM::VST4LNdAsm_32: Spacing = 1; return ARM::VST4LNd32;
8504   case ARM::VST4LNqAsm_16: Spacing = 2; return ARM::VST4LNq16;
8505   case ARM::VST4LNqAsm_32: Spacing = 2; return ARM::VST4LNq32;
8506 
8507   // VST4
8508   case ARM::VST4dWB_fixed_Asm_8:  Spacing = 1; return ARM::VST4d8_UPD;
8509   case ARM::VST4dWB_fixed_Asm_16: Spacing = 1; return ARM::VST4d16_UPD;
8510   case ARM::VST4dWB_fixed_Asm_32: Spacing = 1; return ARM::VST4d32_UPD;
8511   case ARM::VST4qWB_fixed_Asm_8:  Spacing = 2; return ARM::VST4q8_UPD;
8512   case ARM::VST4qWB_fixed_Asm_16: Spacing = 2; return ARM::VST4q16_UPD;
8513   case ARM::VST4qWB_fixed_Asm_32: Spacing = 2; return ARM::VST4q32_UPD;
8514   case ARM::VST4dWB_register_Asm_8:  Spacing = 1; return ARM::VST4d8_UPD;
8515   case ARM::VST4dWB_register_Asm_16: Spacing = 1; return ARM::VST4d16_UPD;
8516   case ARM::VST4dWB_register_Asm_32: Spacing = 1; return ARM::VST4d32_UPD;
8517   case ARM::VST4qWB_register_Asm_8:  Spacing = 2; return ARM::VST4q8_UPD;
8518   case ARM::VST4qWB_register_Asm_16: Spacing = 2; return ARM::VST4q16_UPD;
8519   case ARM::VST4qWB_register_Asm_32: Spacing = 2; return ARM::VST4q32_UPD;
8520   case ARM::VST4dAsm_8:  Spacing = 1; return ARM::VST4d8;
8521   case ARM::VST4dAsm_16: Spacing = 1; return ARM::VST4d16;
8522   case ARM::VST4dAsm_32: Spacing = 1; return ARM::VST4d32;
8523   case ARM::VST4qAsm_8:  Spacing = 2; return ARM::VST4q8;
8524   case ARM::VST4qAsm_16: Spacing = 2; return ARM::VST4q16;
8525   case ARM::VST4qAsm_32: Spacing = 2; return ARM::VST4q32;
8526   }
8527 }
8528 
8529 static unsigned getRealVLDOpcode(unsigned Opc, unsigned &Spacing) {
8530   switch(Opc) {
8531   default: llvm_unreachable("unexpected opcode!");
8532   // VLD1LN
8533   case ARM::VLD1LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD1LNd8_UPD;
8534   case ARM::VLD1LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD1LNd16_UPD;
8535   case ARM::VLD1LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD1LNd32_UPD;
8536   case ARM::VLD1LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD1LNd8_UPD;
8537   case ARM::VLD1LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD1LNd16_UPD;
8538   case ARM::VLD1LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD1LNd32_UPD;
8539   case ARM::VLD1LNdAsm_8:  Spacing = 1; return ARM::VLD1LNd8;
8540   case ARM::VLD1LNdAsm_16: Spacing = 1; return ARM::VLD1LNd16;
8541   case ARM::VLD1LNdAsm_32: Spacing = 1; return ARM::VLD1LNd32;
8542 
8543   // VLD2LN
8544   case ARM::VLD2LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD2LNd8_UPD;
8545   case ARM::VLD2LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD2LNd16_UPD;
8546   case ARM::VLD2LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD2LNd32_UPD;
8547   case ARM::VLD2LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD2LNq16_UPD;
8548   case ARM::VLD2LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD2LNq32_UPD;
8549   case ARM::VLD2LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD2LNd8_UPD;
8550   case ARM::VLD2LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD2LNd16_UPD;
8551   case ARM::VLD2LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD2LNd32_UPD;
8552   case ARM::VLD2LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD2LNq16_UPD;
8553   case ARM::VLD2LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD2LNq32_UPD;
8554   case ARM::VLD2LNdAsm_8:  Spacing = 1; return ARM::VLD2LNd8;
8555   case ARM::VLD2LNdAsm_16: Spacing = 1; return ARM::VLD2LNd16;
8556   case ARM::VLD2LNdAsm_32: Spacing = 1; return ARM::VLD2LNd32;
8557   case ARM::VLD2LNqAsm_16: Spacing = 2; return ARM::VLD2LNq16;
8558   case ARM::VLD2LNqAsm_32: Spacing = 2; return ARM::VLD2LNq32;
8559 
8560   // VLD3DUP
8561   case ARM::VLD3DUPdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3DUPd8_UPD;
8562   case ARM::VLD3DUPdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3DUPd16_UPD;
8563   case ARM::VLD3DUPdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3DUPd32_UPD;
8564   case ARM::VLD3DUPqWB_fixed_Asm_8: Spacing = 1; return ARM::VLD3DUPq8_UPD;
8565   case ARM::VLD3DUPqWB_fixed_Asm_16: Spacing = 2; return ARM::VLD3DUPq16_UPD;
8566   case ARM::VLD3DUPqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3DUPq32_UPD;
8567   case ARM::VLD3DUPdWB_register_Asm_8:  Spacing = 1; return ARM::VLD3DUPd8_UPD;
8568   case ARM::VLD3DUPdWB_register_Asm_16: Spacing = 1; return ARM::VLD3DUPd16_UPD;
8569   case ARM::VLD3DUPdWB_register_Asm_32: Spacing = 1; return ARM::VLD3DUPd32_UPD;
8570   case ARM::VLD3DUPqWB_register_Asm_8: Spacing = 2; return ARM::VLD3DUPq8_UPD;
8571   case ARM::VLD3DUPqWB_register_Asm_16: Spacing = 2; return ARM::VLD3DUPq16_UPD;
8572   case ARM::VLD3DUPqWB_register_Asm_32: Spacing = 2; return ARM::VLD3DUPq32_UPD;
8573   case ARM::VLD3DUPdAsm_8:  Spacing = 1; return ARM::VLD3DUPd8;
8574   case ARM::VLD3DUPdAsm_16: Spacing = 1; return ARM::VLD3DUPd16;
8575   case ARM::VLD3DUPdAsm_32: Spacing = 1; return ARM::VLD3DUPd32;
8576   case ARM::VLD3DUPqAsm_8: Spacing = 2; return ARM::VLD3DUPq8;
8577   case ARM::VLD3DUPqAsm_16: Spacing = 2; return ARM::VLD3DUPq16;
8578   case ARM::VLD3DUPqAsm_32: Spacing = 2; return ARM::VLD3DUPq32;
8579 
8580   // VLD3LN
8581   case ARM::VLD3LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3LNd8_UPD;
8582   case ARM::VLD3LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3LNd16_UPD;
8583   case ARM::VLD3LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3LNd32_UPD;
8584   case ARM::VLD3LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3LNq16_UPD;
8585   case ARM::VLD3LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3LNq32_UPD;
8586   case ARM::VLD3LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD3LNd8_UPD;
8587   case ARM::VLD3LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD3LNd16_UPD;
8588   case ARM::VLD3LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD3LNd32_UPD;
8589   case ARM::VLD3LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD3LNq16_UPD;
8590   case ARM::VLD3LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD3LNq32_UPD;
8591   case ARM::VLD3LNdAsm_8:  Spacing = 1; return ARM::VLD3LNd8;
8592   case ARM::VLD3LNdAsm_16: Spacing = 1; return ARM::VLD3LNd16;
8593   case ARM::VLD3LNdAsm_32: Spacing = 1; return ARM::VLD3LNd32;
8594   case ARM::VLD3LNqAsm_16: Spacing = 2; return ARM::VLD3LNq16;
8595   case ARM::VLD3LNqAsm_32: Spacing = 2; return ARM::VLD3LNq32;
8596 
8597   // VLD3
8598   case ARM::VLD3dWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3d8_UPD;
8599   case ARM::VLD3dWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3d16_UPD;
8600   case ARM::VLD3dWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3d32_UPD;
8601   case ARM::VLD3qWB_fixed_Asm_8:  Spacing = 2; return ARM::VLD3q8_UPD;
8602   case ARM::VLD3qWB_fixed_Asm_16: Spacing = 2; return ARM::VLD3q16_UPD;
8603   case ARM::VLD3qWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3q32_UPD;
8604   case ARM::VLD3dWB_register_Asm_8:  Spacing = 1; return ARM::VLD3d8_UPD;
8605   case ARM::VLD3dWB_register_Asm_16: Spacing = 1; return ARM::VLD3d16_UPD;
8606   case ARM::VLD3dWB_register_Asm_32: Spacing = 1; return ARM::VLD3d32_UPD;
8607   case ARM::VLD3qWB_register_Asm_8:  Spacing = 2; return ARM::VLD3q8_UPD;
8608   case ARM::VLD3qWB_register_Asm_16: Spacing = 2; return ARM::VLD3q16_UPD;
8609   case ARM::VLD3qWB_register_Asm_32: Spacing = 2; return ARM::VLD3q32_UPD;
8610   case ARM::VLD3dAsm_8:  Spacing = 1; return ARM::VLD3d8;
8611   case ARM::VLD3dAsm_16: Spacing = 1; return ARM::VLD3d16;
8612   case ARM::VLD3dAsm_32: Spacing = 1; return ARM::VLD3d32;
8613   case ARM::VLD3qAsm_8:  Spacing = 2; return ARM::VLD3q8;
8614   case ARM::VLD3qAsm_16: Spacing = 2; return ARM::VLD3q16;
8615   case ARM::VLD3qAsm_32: Spacing = 2; return ARM::VLD3q32;
8616 
8617   // VLD4LN
8618   case ARM::VLD4LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4LNd8_UPD;
8619   case ARM::VLD4LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4LNd16_UPD;
8620   case ARM::VLD4LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4LNd32_UPD;
8621   case ARM::VLD4LNqWB_fixed_Asm_16: Spacing = 2; return ARM::VLD4LNq16_UPD;
8622   case ARM::VLD4LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4LNq32_UPD;
8623   case ARM::VLD4LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD4LNd8_UPD;
8624   case ARM::VLD4LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD4LNd16_UPD;
8625   case ARM::VLD4LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD4LNd32_UPD;
8626   case ARM::VLD4LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD4LNq16_UPD;
8627   case ARM::VLD4LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD4LNq32_UPD;
8628   case ARM::VLD4LNdAsm_8:  Spacing = 1; return ARM::VLD4LNd8;
8629   case ARM::VLD4LNdAsm_16: Spacing = 1; return ARM::VLD4LNd16;
8630   case ARM::VLD4LNdAsm_32: Spacing = 1; return ARM::VLD4LNd32;
8631   case ARM::VLD4LNqAsm_16: Spacing = 2; return ARM::VLD4LNq16;
8632   case ARM::VLD4LNqAsm_32: Spacing = 2; return ARM::VLD4LNq32;
8633 
8634   // VLD4DUP
8635   case ARM::VLD4DUPdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4DUPd8_UPD;
8636   case ARM::VLD4DUPdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4DUPd16_UPD;
8637   case ARM::VLD4DUPdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4DUPd32_UPD;
8638   case ARM::VLD4DUPqWB_fixed_Asm_8: Spacing = 1; return ARM::VLD4DUPq8_UPD;
8639   case ARM::VLD4DUPqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4DUPq16_UPD;
8640   case ARM::VLD4DUPqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4DUPq32_UPD;
8641   case ARM::VLD4DUPdWB_register_Asm_8:  Spacing = 1; return ARM::VLD4DUPd8_UPD;
8642   case ARM::VLD4DUPdWB_register_Asm_16: Spacing = 1; return ARM::VLD4DUPd16_UPD;
8643   case ARM::VLD4DUPdWB_register_Asm_32: Spacing = 1; return ARM::VLD4DUPd32_UPD;
8644   case ARM::VLD4DUPqWB_register_Asm_8: Spacing = 2; return ARM::VLD4DUPq8_UPD;
8645   case ARM::VLD4DUPqWB_register_Asm_16: Spacing = 2; return ARM::VLD4DUPq16_UPD;
8646   case ARM::VLD4DUPqWB_register_Asm_32: Spacing = 2; return ARM::VLD4DUPq32_UPD;
8647   case ARM::VLD4DUPdAsm_8:  Spacing = 1; return ARM::VLD4DUPd8;
8648   case ARM::VLD4DUPdAsm_16: Spacing = 1; return ARM::VLD4DUPd16;
8649   case ARM::VLD4DUPdAsm_32: Spacing = 1; return ARM::VLD4DUPd32;
8650   case ARM::VLD4DUPqAsm_8: Spacing = 2; return ARM::VLD4DUPq8;
8651   case ARM::VLD4DUPqAsm_16: Spacing = 2; return ARM::VLD4DUPq16;
8652   case ARM::VLD4DUPqAsm_32: Spacing = 2; return ARM::VLD4DUPq32;
8653 
8654   // VLD4
8655   case ARM::VLD4dWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4d8_UPD;
8656   case ARM::VLD4dWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4d16_UPD;
8657   case ARM::VLD4dWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4d32_UPD;
8658   case ARM::VLD4qWB_fixed_Asm_8:  Spacing = 2; return ARM::VLD4q8_UPD;
8659   case ARM::VLD4qWB_fixed_Asm_16: Spacing = 2; return ARM::VLD4q16_UPD;
8660   case ARM::VLD4qWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4q32_UPD;
8661   case ARM::VLD4dWB_register_Asm_8:  Spacing = 1; return ARM::VLD4d8_UPD;
8662   case ARM::VLD4dWB_register_Asm_16: Spacing = 1; return ARM::VLD4d16_UPD;
8663   case ARM::VLD4dWB_register_Asm_32: Spacing = 1; return ARM::VLD4d32_UPD;
8664   case ARM::VLD4qWB_register_Asm_8:  Spacing = 2; return ARM::VLD4q8_UPD;
8665   case ARM::VLD4qWB_register_Asm_16: Spacing = 2; return ARM::VLD4q16_UPD;
8666   case ARM::VLD4qWB_register_Asm_32: Spacing = 2; return ARM::VLD4q32_UPD;
8667   case ARM::VLD4dAsm_8:  Spacing = 1; return ARM::VLD4d8;
8668   case ARM::VLD4dAsm_16: Spacing = 1; return ARM::VLD4d16;
8669   case ARM::VLD4dAsm_32: Spacing = 1; return ARM::VLD4d32;
8670   case ARM::VLD4qAsm_8:  Spacing = 2; return ARM::VLD4q8;
8671   case ARM::VLD4qAsm_16: Spacing = 2; return ARM::VLD4q16;
8672   case ARM::VLD4qAsm_32: Spacing = 2; return ARM::VLD4q32;
8673   }
8674 }
8675 
8676 bool ARMAsmParser::processInstruction(MCInst &Inst,
8677                                       const OperandVector &Operands,
8678                                       MCStreamer &Out) {
8679   // Check if we have the wide qualifier, because if it's present we
8680   // must avoid selecting a 16-bit thumb instruction.
8681   bool HasWideQualifier = false;
8682   for (auto &Op : Operands) {
8683     ARMOperand &ARMOp = static_cast<ARMOperand&>(*Op);
8684     if (ARMOp.isToken() && ARMOp.getToken() == ".w") {
8685       HasWideQualifier = true;
8686       break;
8687     }
8688   }
8689 
8690   switch (Inst.getOpcode()) {
8691   // Alias for alternate form of 'ldr{,b}t Rt, [Rn], #imm' instruction.
8692   case ARM::LDRT_POST:
8693   case ARM::LDRBT_POST: {
8694     const unsigned Opcode =
8695       (Inst.getOpcode() == ARM::LDRT_POST) ? ARM::LDRT_POST_IMM
8696                                            : ARM::LDRBT_POST_IMM;
8697     MCInst TmpInst;
8698     TmpInst.setOpcode(Opcode);
8699     TmpInst.addOperand(Inst.getOperand(0));
8700     TmpInst.addOperand(Inst.getOperand(1));
8701     TmpInst.addOperand(Inst.getOperand(1));
8702     TmpInst.addOperand(MCOperand::createReg(0));
8703     TmpInst.addOperand(MCOperand::createImm(0));
8704     TmpInst.addOperand(Inst.getOperand(2));
8705     TmpInst.addOperand(Inst.getOperand(3));
8706     Inst = TmpInst;
8707     return true;
8708   }
8709   // Alias for 'ldr{sb,h,sh}t Rt, [Rn] {, #imm}' for ommitted immediate.
8710   case ARM::LDRSBTii:
8711   case ARM::LDRHTii:
8712   case ARM::LDRSHTii: {
8713     MCInst TmpInst;
8714 
8715     if (Inst.getOpcode() == ARM::LDRSBTii)
8716       TmpInst.setOpcode(ARM::LDRSBTi);
8717     else if (Inst.getOpcode() == ARM::LDRHTii)
8718       TmpInst.setOpcode(ARM::LDRHTi);
8719     else if (Inst.getOpcode() == ARM::LDRSHTii)
8720       TmpInst.setOpcode(ARM::LDRSHTi);
8721     TmpInst.addOperand(Inst.getOperand(0));
8722     TmpInst.addOperand(Inst.getOperand(1));
8723     TmpInst.addOperand(Inst.getOperand(1));
8724     TmpInst.addOperand(MCOperand::createImm(256));
8725     TmpInst.addOperand(Inst.getOperand(2));
8726     Inst = TmpInst;
8727     return true;
8728   }
8729   // Alias for alternate form of 'str{,b}t Rt, [Rn], #imm' instruction.
8730   case ARM::STRT_POST:
8731   case ARM::STRBT_POST: {
8732     const unsigned Opcode =
8733       (Inst.getOpcode() == ARM::STRT_POST) ? ARM::STRT_POST_IMM
8734                                            : ARM::STRBT_POST_IMM;
8735     MCInst TmpInst;
8736     TmpInst.setOpcode(Opcode);
8737     TmpInst.addOperand(Inst.getOperand(1));
8738     TmpInst.addOperand(Inst.getOperand(0));
8739     TmpInst.addOperand(Inst.getOperand(1));
8740     TmpInst.addOperand(MCOperand::createReg(0));
8741     TmpInst.addOperand(MCOperand::createImm(0));
8742     TmpInst.addOperand(Inst.getOperand(2));
8743     TmpInst.addOperand(Inst.getOperand(3));
8744     Inst = TmpInst;
8745     return true;
8746   }
8747   // Alias for alternate form of 'ADR Rd, #imm' instruction.
8748   case ARM::ADDri: {
8749     if (Inst.getOperand(1).getReg() != ARM::PC ||
8750         Inst.getOperand(5).getReg() != 0 ||
8751         !(Inst.getOperand(2).isExpr() || Inst.getOperand(2).isImm()))
8752       return false;
8753     MCInst TmpInst;
8754     TmpInst.setOpcode(ARM::ADR);
8755     TmpInst.addOperand(Inst.getOperand(0));
8756     if (Inst.getOperand(2).isImm()) {
8757       // Immediate (mod_imm) will be in its encoded form, we must unencode it
8758       // before passing it to the ADR instruction.
8759       unsigned Enc = Inst.getOperand(2).getImm();
8760       TmpInst.addOperand(MCOperand::createImm(
8761         ARM_AM::rotr32(Enc & 0xFF, (Enc & 0xF00) >> 7)));
8762     } else {
8763       // Turn PC-relative expression into absolute expression.
8764       // Reading PC provides the start of the current instruction + 8 and
8765       // the transform to adr is biased by that.
8766       MCSymbol *Dot = getContext().createTempSymbol();
8767       Out.emitLabel(Dot);
8768       const MCExpr *OpExpr = Inst.getOperand(2).getExpr();
8769       const MCExpr *InstPC = MCSymbolRefExpr::create(Dot,
8770                                                      MCSymbolRefExpr::VK_None,
8771                                                      getContext());
8772       const MCExpr *Const8 = MCConstantExpr::create(8, getContext());
8773       const MCExpr *ReadPC = MCBinaryExpr::createAdd(InstPC, Const8,
8774                                                      getContext());
8775       const MCExpr *FixupAddr = MCBinaryExpr::createAdd(ReadPC, OpExpr,
8776                                                         getContext());
8777       TmpInst.addOperand(MCOperand::createExpr(FixupAddr));
8778     }
8779     TmpInst.addOperand(Inst.getOperand(3));
8780     TmpInst.addOperand(Inst.getOperand(4));
8781     Inst = TmpInst;
8782     return true;
8783   }
8784   // Aliases for imm syntax of LDR instructions.
8785   case ARM::t2LDR_PRE_imm:
8786   case ARM::t2LDR_POST_imm: {
8787     MCInst TmpInst;
8788     TmpInst.setOpcode(Inst.getOpcode() == ARM::t2LDR_PRE_imm ? ARM::t2LDR_PRE
8789                                                              : ARM::t2LDR_POST);
8790     TmpInst.addOperand(Inst.getOperand(0)); // Rt
8791     TmpInst.addOperand(Inst.getOperand(4)); // Rt_wb
8792     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8793     TmpInst.addOperand(Inst.getOperand(2)); // imm
8794     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8795     Inst = TmpInst;
8796     return true;
8797   }
8798   // Aliases for imm syntax of STR instructions.
8799   case ARM::t2STR_PRE_imm:
8800   case ARM::t2STR_POST_imm: {
8801     MCInst TmpInst;
8802     TmpInst.setOpcode(Inst.getOpcode() == ARM::t2STR_PRE_imm ? ARM::t2STR_PRE
8803                                                              : ARM::t2STR_POST);
8804     TmpInst.addOperand(Inst.getOperand(4)); // Rt_wb
8805     TmpInst.addOperand(Inst.getOperand(0)); // Rt
8806     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8807     TmpInst.addOperand(Inst.getOperand(2)); // imm
8808     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8809     Inst = TmpInst;
8810     return true;
8811   }
8812   // Aliases for alternate PC+imm syntax of LDR instructions.
8813   case ARM::t2LDRpcrel:
8814     // Select the narrow version if the immediate will fit.
8815     if (Inst.getOperand(1).getImm() > 0 &&
8816         Inst.getOperand(1).getImm() <= 0xff &&
8817         !HasWideQualifier)
8818       Inst.setOpcode(ARM::tLDRpci);
8819     else
8820       Inst.setOpcode(ARM::t2LDRpci);
8821     return true;
8822   case ARM::t2LDRBpcrel:
8823     Inst.setOpcode(ARM::t2LDRBpci);
8824     return true;
8825   case ARM::t2LDRHpcrel:
8826     Inst.setOpcode(ARM::t2LDRHpci);
8827     return true;
8828   case ARM::t2LDRSBpcrel:
8829     Inst.setOpcode(ARM::t2LDRSBpci);
8830     return true;
8831   case ARM::t2LDRSHpcrel:
8832     Inst.setOpcode(ARM::t2LDRSHpci);
8833     return true;
8834   case ARM::LDRConstPool:
8835   case ARM::tLDRConstPool:
8836   case ARM::t2LDRConstPool: {
8837     // Pseudo instruction ldr rt, =immediate is converted to a
8838     // MOV rt, immediate if immediate is known and representable
8839     // otherwise we create a constant pool entry that we load from.
8840     MCInst TmpInst;
8841     if (Inst.getOpcode() == ARM::LDRConstPool)
8842       TmpInst.setOpcode(ARM::LDRi12);
8843     else if (Inst.getOpcode() == ARM::tLDRConstPool)
8844       TmpInst.setOpcode(ARM::tLDRpci);
8845     else if (Inst.getOpcode() == ARM::t2LDRConstPool)
8846       TmpInst.setOpcode(ARM::t2LDRpci);
8847     const ARMOperand &PoolOperand =
8848       (HasWideQualifier ?
8849        static_cast<ARMOperand &>(*Operands[4]) :
8850        static_cast<ARMOperand &>(*Operands[3]));
8851     const MCExpr *SubExprVal = PoolOperand.getConstantPoolImm();
8852     // If SubExprVal is a constant we may be able to use a MOV
8853     if (isa<MCConstantExpr>(SubExprVal) &&
8854         Inst.getOperand(0).getReg() != ARM::PC &&
8855         Inst.getOperand(0).getReg() != ARM::SP) {
8856       int64_t Value =
8857         (int64_t) (cast<MCConstantExpr>(SubExprVal))->getValue();
8858       bool UseMov  = true;
8859       bool MovHasS = true;
8860       if (Inst.getOpcode() == ARM::LDRConstPool) {
8861         // ARM Constant
8862         if (ARM_AM::getSOImmVal(Value) != -1) {
8863           Value = ARM_AM::getSOImmVal(Value);
8864           TmpInst.setOpcode(ARM::MOVi);
8865         }
8866         else if (ARM_AM::getSOImmVal(~Value) != -1) {
8867           Value = ARM_AM::getSOImmVal(~Value);
8868           TmpInst.setOpcode(ARM::MVNi);
8869         }
8870         else if (hasV6T2Ops() &&
8871                  Value >=0 && Value < 65536) {
8872           TmpInst.setOpcode(ARM::MOVi16);
8873           MovHasS = false;
8874         }
8875         else
8876           UseMov = false;
8877       }
8878       else {
8879         // Thumb/Thumb2 Constant
8880         if (hasThumb2() &&
8881             ARM_AM::getT2SOImmVal(Value) != -1)
8882           TmpInst.setOpcode(ARM::t2MOVi);
8883         else if (hasThumb2() &&
8884                  ARM_AM::getT2SOImmVal(~Value) != -1) {
8885           TmpInst.setOpcode(ARM::t2MVNi);
8886           Value = ~Value;
8887         }
8888         else if (hasV8MBaseline() &&
8889                  Value >=0 && Value < 65536) {
8890           TmpInst.setOpcode(ARM::t2MOVi16);
8891           MovHasS = false;
8892         }
8893         else
8894           UseMov = false;
8895       }
8896       if (UseMov) {
8897         TmpInst.addOperand(Inst.getOperand(0));           // Rt
8898         TmpInst.addOperand(MCOperand::createImm(Value));  // Immediate
8899         TmpInst.addOperand(Inst.getOperand(2));           // CondCode
8900         TmpInst.addOperand(Inst.getOperand(3));           // CondCode
8901         if (MovHasS)
8902           TmpInst.addOperand(MCOperand::createReg(0));    // S
8903         Inst = TmpInst;
8904         return true;
8905       }
8906     }
8907     // No opportunity to use MOV/MVN create constant pool
8908     const MCExpr *CPLoc =
8909       getTargetStreamer().addConstantPoolEntry(SubExprVal,
8910                                                PoolOperand.getStartLoc());
8911     TmpInst.addOperand(Inst.getOperand(0));           // Rt
8912     TmpInst.addOperand(MCOperand::createExpr(CPLoc)); // offset to constpool
8913     if (TmpInst.getOpcode() == ARM::LDRi12)
8914       TmpInst.addOperand(MCOperand::createImm(0));    // unused offset
8915     TmpInst.addOperand(Inst.getOperand(2));           // CondCode
8916     TmpInst.addOperand(Inst.getOperand(3));           // CondCode
8917     Inst = TmpInst;
8918     return true;
8919   }
8920   // Handle NEON VST complex aliases.
8921   case ARM::VST1LNdWB_register_Asm_8:
8922   case ARM::VST1LNdWB_register_Asm_16:
8923   case ARM::VST1LNdWB_register_Asm_32: {
8924     MCInst TmpInst;
8925     // Shuffle the operands around so the lane index operand is in the
8926     // right place.
8927     unsigned Spacing;
8928     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8929     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
8930     TmpInst.addOperand(Inst.getOperand(2)); // Rn
8931     TmpInst.addOperand(Inst.getOperand(3)); // alignment
8932     TmpInst.addOperand(Inst.getOperand(4)); // Rm
8933     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8934     TmpInst.addOperand(Inst.getOperand(1)); // lane
8935     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
8936     TmpInst.addOperand(Inst.getOperand(6));
8937     Inst = TmpInst;
8938     return true;
8939   }
8940 
8941   case ARM::VST2LNdWB_register_Asm_8:
8942   case ARM::VST2LNdWB_register_Asm_16:
8943   case ARM::VST2LNdWB_register_Asm_32:
8944   case ARM::VST2LNqWB_register_Asm_16:
8945   case ARM::VST2LNqWB_register_Asm_32: {
8946     MCInst TmpInst;
8947     // Shuffle the operands around so the lane index operand is in the
8948     // right place.
8949     unsigned Spacing;
8950     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8951     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
8952     TmpInst.addOperand(Inst.getOperand(2)); // Rn
8953     TmpInst.addOperand(Inst.getOperand(3)); // alignment
8954     TmpInst.addOperand(Inst.getOperand(4)); // Rm
8955     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8956     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8957                                             Spacing));
8958     TmpInst.addOperand(Inst.getOperand(1)); // lane
8959     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
8960     TmpInst.addOperand(Inst.getOperand(6));
8961     Inst = TmpInst;
8962     return true;
8963   }
8964 
8965   case ARM::VST3LNdWB_register_Asm_8:
8966   case ARM::VST3LNdWB_register_Asm_16:
8967   case ARM::VST3LNdWB_register_Asm_32:
8968   case ARM::VST3LNqWB_register_Asm_16:
8969   case ARM::VST3LNqWB_register_Asm_32: {
8970     MCInst TmpInst;
8971     // Shuffle the operands around so the lane index operand is in the
8972     // right place.
8973     unsigned Spacing;
8974     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8975     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
8976     TmpInst.addOperand(Inst.getOperand(2)); // Rn
8977     TmpInst.addOperand(Inst.getOperand(3)); // alignment
8978     TmpInst.addOperand(Inst.getOperand(4)); // Rm
8979     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8980     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8981                                             Spacing));
8982     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8983                                             Spacing * 2));
8984     TmpInst.addOperand(Inst.getOperand(1)); // lane
8985     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
8986     TmpInst.addOperand(Inst.getOperand(6));
8987     Inst = TmpInst;
8988     return true;
8989   }
8990 
8991   case ARM::VST4LNdWB_register_Asm_8:
8992   case ARM::VST4LNdWB_register_Asm_16:
8993   case ARM::VST4LNdWB_register_Asm_32:
8994   case ARM::VST4LNqWB_register_Asm_16:
8995   case ARM::VST4LNqWB_register_Asm_32: {
8996     MCInst TmpInst;
8997     // Shuffle the operands around so the lane index operand is in the
8998     // right place.
8999     unsigned Spacing;
9000     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
9001     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
9002     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9003     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9004     TmpInst.addOperand(Inst.getOperand(4)); // Rm
9005     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9006     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9007                                             Spacing));
9008     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9009                                             Spacing * 2));
9010     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9011                                             Spacing * 3));
9012     TmpInst.addOperand(Inst.getOperand(1)); // lane
9013     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
9014     TmpInst.addOperand(Inst.getOperand(6));
9015     Inst = TmpInst;
9016     return true;
9017   }
9018 
9019   case ARM::VST1LNdWB_fixed_Asm_8:
9020   case ARM::VST1LNdWB_fixed_Asm_16:
9021   case ARM::VST1LNdWB_fixed_Asm_32: {
9022     MCInst TmpInst;
9023     // Shuffle the operands around so the lane index operand is in the
9024     // right place.
9025     unsigned Spacing;
9026     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
9027     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
9028     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9029     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9030     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
9031     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9032     TmpInst.addOperand(Inst.getOperand(1)); // lane
9033     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9034     TmpInst.addOperand(Inst.getOperand(5));
9035     Inst = TmpInst;
9036     return true;
9037   }
9038 
9039   case ARM::VST2LNdWB_fixed_Asm_8:
9040   case ARM::VST2LNdWB_fixed_Asm_16:
9041   case ARM::VST2LNdWB_fixed_Asm_32:
9042   case ARM::VST2LNqWB_fixed_Asm_16:
9043   case ARM::VST2LNqWB_fixed_Asm_32: {
9044     MCInst TmpInst;
9045     // Shuffle the operands around so the lane index operand is in the
9046     // right place.
9047     unsigned Spacing;
9048     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
9049     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
9050     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9051     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9052     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
9053     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9054     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9055                                             Spacing));
9056     TmpInst.addOperand(Inst.getOperand(1)); // lane
9057     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9058     TmpInst.addOperand(Inst.getOperand(5));
9059     Inst = TmpInst;
9060     return true;
9061   }
9062 
9063   case ARM::VST3LNdWB_fixed_Asm_8:
9064   case ARM::VST3LNdWB_fixed_Asm_16:
9065   case ARM::VST3LNdWB_fixed_Asm_32:
9066   case ARM::VST3LNqWB_fixed_Asm_16:
9067   case ARM::VST3LNqWB_fixed_Asm_32: {
9068     MCInst TmpInst;
9069     // Shuffle the operands around so the lane index operand is in the
9070     // right place.
9071     unsigned Spacing;
9072     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
9073     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
9074     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9075     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9076     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
9077     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9078     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9079                                             Spacing));
9080     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9081                                             Spacing * 2));
9082     TmpInst.addOperand(Inst.getOperand(1)); // lane
9083     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9084     TmpInst.addOperand(Inst.getOperand(5));
9085     Inst = TmpInst;
9086     return true;
9087   }
9088 
9089   case ARM::VST4LNdWB_fixed_Asm_8:
9090   case ARM::VST4LNdWB_fixed_Asm_16:
9091   case ARM::VST4LNdWB_fixed_Asm_32:
9092   case ARM::VST4LNqWB_fixed_Asm_16:
9093   case ARM::VST4LNqWB_fixed_Asm_32: {
9094     MCInst TmpInst;
9095     // Shuffle the operands around so the lane index operand is in the
9096     // right place.
9097     unsigned Spacing;
9098     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
9099     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
9100     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9101     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9102     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
9103     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9104     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9105                                             Spacing));
9106     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9107                                             Spacing * 2));
9108     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9109                                             Spacing * 3));
9110     TmpInst.addOperand(Inst.getOperand(1)); // lane
9111     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9112     TmpInst.addOperand(Inst.getOperand(5));
9113     Inst = TmpInst;
9114     return true;
9115   }
9116 
9117   case ARM::VST1LNdAsm_8:
9118   case ARM::VST1LNdAsm_16:
9119   case ARM::VST1LNdAsm_32: {
9120     MCInst TmpInst;
9121     // Shuffle the operands around so the lane index operand is in the
9122     // right place.
9123     unsigned Spacing;
9124     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
9125     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9126     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9127     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9128     TmpInst.addOperand(Inst.getOperand(1)); // lane
9129     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9130     TmpInst.addOperand(Inst.getOperand(5));
9131     Inst = TmpInst;
9132     return true;
9133   }
9134 
9135   case ARM::VST2LNdAsm_8:
9136   case ARM::VST2LNdAsm_16:
9137   case ARM::VST2LNdAsm_32:
9138   case ARM::VST2LNqAsm_16:
9139   case ARM::VST2LNqAsm_32: {
9140     MCInst TmpInst;
9141     // Shuffle the operands around so the lane index operand is in the
9142     // right place.
9143     unsigned Spacing;
9144     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
9145     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9146     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9147     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9148     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9149                                             Spacing));
9150     TmpInst.addOperand(Inst.getOperand(1)); // lane
9151     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9152     TmpInst.addOperand(Inst.getOperand(5));
9153     Inst = TmpInst;
9154     return true;
9155   }
9156 
9157   case ARM::VST3LNdAsm_8:
9158   case ARM::VST3LNdAsm_16:
9159   case ARM::VST3LNdAsm_32:
9160   case ARM::VST3LNqAsm_16:
9161   case ARM::VST3LNqAsm_32: {
9162     MCInst TmpInst;
9163     // Shuffle the operands around so the lane index operand is in the
9164     // right place.
9165     unsigned Spacing;
9166     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
9167     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9168     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9169     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9170     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9171                                             Spacing));
9172     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9173                                             Spacing * 2));
9174     TmpInst.addOperand(Inst.getOperand(1)); // lane
9175     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9176     TmpInst.addOperand(Inst.getOperand(5));
9177     Inst = TmpInst;
9178     return true;
9179   }
9180 
9181   case ARM::VST4LNdAsm_8:
9182   case ARM::VST4LNdAsm_16:
9183   case ARM::VST4LNdAsm_32:
9184   case ARM::VST4LNqAsm_16:
9185   case ARM::VST4LNqAsm_32: {
9186     MCInst TmpInst;
9187     // Shuffle the operands around so the lane index operand is in the
9188     // right place.
9189     unsigned Spacing;
9190     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
9191     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9192     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9193     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9194     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9195                                             Spacing));
9196     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9197                                             Spacing * 2));
9198     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9199                                             Spacing * 3));
9200     TmpInst.addOperand(Inst.getOperand(1)); // lane
9201     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9202     TmpInst.addOperand(Inst.getOperand(5));
9203     Inst = TmpInst;
9204     return true;
9205   }
9206 
9207   // Handle NEON VLD complex aliases.
9208   case ARM::VLD1LNdWB_register_Asm_8:
9209   case ARM::VLD1LNdWB_register_Asm_16:
9210   case ARM::VLD1LNdWB_register_Asm_32: {
9211     MCInst TmpInst;
9212     // Shuffle the operands around so the lane index operand is in the
9213     // right place.
9214     unsigned Spacing;
9215     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9216     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9217     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
9218     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9219     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9220     TmpInst.addOperand(Inst.getOperand(4)); // Rm
9221     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
9222     TmpInst.addOperand(Inst.getOperand(1)); // lane
9223     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
9224     TmpInst.addOperand(Inst.getOperand(6));
9225     Inst = TmpInst;
9226     return true;
9227   }
9228 
9229   case ARM::VLD2LNdWB_register_Asm_8:
9230   case ARM::VLD2LNdWB_register_Asm_16:
9231   case ARM::VLD2LNdWB_register_Asm_32:
9232   case ARM::VLD2LNqWB_register_Asm_16:
9233   case ARM::VLD2LNqWB_register_Asm_32: {
9234     MCInst TmpInst;
9235     // Shuffle the operands around so the lane index operand is in the
9236     // right place.
9237     unsigned Spacing;
9238     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9239     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9240     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9241                                             Spacing));
9242     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
9243     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9244     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9245     TmpInst.addOperand(Inst.getOperand(4)); // Rm
9246     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
9247     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9248                                             Spacing));
9249     TmpInst.addOperand(Inst.getOperand(1)); // lane
9250     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
9251     TmpInst.addOperand(Inst.getOperand(6));
9252     Inst = TmpInst;
9253     return true;
9254   }
9255 
9256   case ARM::VLD3LNdWB_register_Asm_8:
9257   case ARM::VLD3LNdWB_register_Asm_16:
9258   case ARM::VLD3LNdWB_register_Asm_32:
9259   case ARM::VLD3LNqWB_register_Asm_16:
9260   case ARM::VLD3LNqWB_register_Asm_32: {
9261     MCInst TmpInst;
9262     // Shuffle the operands around so the lane index operand is in the
9263     // right place.
9264     unsigned Spacing;
9265     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9266     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9267     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9268                                             Spacing));
9269     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9270                                             Spacing * 2));
9271     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
9272     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9273     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9274     TmpInst.addOperand(Inst.getOperand(4)); // Rm
9275     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
9276     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9277                                             Spacing));
9278     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9279                                             Spacing * 2));
9280     TmpInst.addOperand(Inst.getOperand(1)); // lane
9281     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
9282     TmpInst.addOperand(Inst.getOperand(6));
9283     Inst = TmpInst;
9284     return true;
9285   }
9286 
9287   case ARM::VLD4LNdWB_register_Asm_8:
9288   case ARM::VLD4LNdWB_register_Asm_16:
9289   case ARM::VLD4LNdWB_register_Asm_32:
9290   case ARM::VLD4LNqWB_register_Asm_16:
9291   case ARM::VLD4LNqWB_register_Asm_32: {
9292     MCInst TmpInst;
9293     // Shuffle the operands around so the lane index operand is in the
9294     // right place.
9295     unsigned Spacing;
9296     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9297     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9298     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9299                                             Spacing));
9300     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9301                                             Spacing * 2));
9302     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9303                                             Spacing * 3));
9304     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
9305     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9306     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9307     TmpInst.addOperand(Inst.getOperand(4)); // Rm
9308     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
9309     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9310                                             Spacing));
9311     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9312                                             Spacing * 2));
9313     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9314                                             Spacing * 3));
9315     TmpInst.addOperand(Inst.getOperand(1)); // lane
9316     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
9317     TmpInst.addOperand(Inst.getOperand(6));
9318     Inst = TmpInst;
9319     return true;
9320   }
9321 
9322   case ARM::VLD1LNdWB_fixed_Asm_8:
9323   case ARM::VLD1LNdWB_fixed_Asm_16:
9324   case ARM::VLD1LNdWB_fixed_Asm_32: {
9325     MCInst TmpInst;
9326     // Shuffle the operands around so the lane index operand is in the
9327     // right place.
9328     unsigned Spacing;
9329     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9330     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9331     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
9332     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9333     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9334     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
9335     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
9336     TmpInst.addOperand(Inst.getOperand(1)); // lane
9337     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9338     TmpInst.addOperand(Inst.getOperand(5));
9339     Inst = TmpInst;
9340     return true;
9341   }
9342 
9343   case ARM::VLD2LNdWB_fixed_Asm_8:
9344   case ARM::VLD2LNdWB_fixed_Asm_16:
9345   case ARM::VLD2LNdWB_fixed_Asm_32:
9346   case ARM::VLD2LNqWB_fixed_Asm_16:
9347   case ARM::VLD2LNqWB_fixed_Asm_32: {
9348     MCInst TmpInst;
9349     // Shuffle the operands around so the lane index operand is in the
9350     // right place.
9351     unsigned Spacing;
9352     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9353     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9354     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9355                                             Spacing));
9356     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
9357     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9358     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9359     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
9360     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
9361     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9362                                             Spacing));
9363     TmpInst.addOperand(Inst.getOperand(1)); // lane
9364     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9365     TmpInst.addOperand(Inst.getOperand(5));
9366     Inst = TmpInst;
9367     return true;
9368   }
9369 
9370   case ARM::VLD3LNdWB_fixed_Asm_8:
9371   case ARM::VLD3LNdWB_fixed_Asm_16:
9372   case ARM::VLD3LNdWB_fixed_Asm_32:
9373   case ARM::VLD3LNqWB_fixed_Asm_16:
9374   case ARM::VLD3LNqWB_fixed_Asm_32: {
9375     MCInst TmpInst;
9376     // Shuffle the operands around so the lane index operand is in the
9377     // right place.
9378     unsigned Spacing;
9379     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9380     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9381     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9382                                             Spacing));
9383     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9384                                             Spacing * 2));
9385     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
9386     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9387     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9388     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
9389     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
9390     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9391                                             Spacing));
9392     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9393                                             Spacing * 2));
9394     TmpInst.addOperand(Inst.getOperand(1)); // lane
9395     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9396     TmpInst.addOperand(Inst.getOperand(5));
9397     Inst = TmpInst;
9398     return true;
9399   }
9400 
9401   case ARM::VLD4LNdWB_fixed_Asm_8:
9402   case ARM::VLD4LNdWB_fixed_Asm_16:
9403   case ARM::VLD4LNdWB_fixed_Asm_32:
9404   case ARM::VLD4LNqWB_fixed_Asm_16:
9405   case ARM::VLD4LNqWB_fixed_Asm_32: {
9406     MCInst TmpInst;
9407     // Shuffle the operands around so the lane index operand is in the
9408     // right place.
9409     unsigned Spacing;
9410     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9411     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9412     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9413                                             Spacing));
9414     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9415                                             Spacing * 2));
9416     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9417                                             Spacing * 3));
9418     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
9419     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9420     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9421     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
9422     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
9423     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9424                                             Spacing));
9425     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9426                                             Spacing * 2));
9427     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9428                                             Spacing * 3));
9429     TmpInst.addOperand(Inst.getOperand(1)); // lane
9430     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9431     TmpInst.addOperand(Inst.getOperand(5));
9432     Inst = TmpInst;
9433     return true;
9434   }
9435 
9436   case ARM::VLD1LNdAsm_8:
9437   case ARM::VLD1LNdAsm_16:
9438   case ARM::VLD1LNdAsm_32: {
9439     MCInst TmpInst;
9440     // Shuffle the operands around so the lane index operand is in the
9441     // right place.
9442     unsigned Spacing;
9443     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9444     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9445     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9446     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9447     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
9448     TmpInst.addOperand(Inst.getOperand(1)); // lane
9449     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9450     TmpInst.addOperand(Inst.getOperand(5));
9451     Inst = TmpInst;
9452     return true;
9453   }
9454 
9455   case ARM::VLD2LNdAsm_8:
9456   case ARM::VLD2LNdAsm_16:
9457   case ARM::VLD2LNdAsm_32:
9458   case ARM::VLD2LNqAsm_16:
9459   case ARM::VLD2LNqAsm_32: {
9460     MCInst TmpInst;
9461     // Shuffle the operands around so the lane index operand is in the
9462     // right place.
9463     unsigned Spacing;
9464     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9465     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9466     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9467                                             Spacing));
9468     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9469     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9470     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
9471     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9472                                             Spacing));
9473     TmpInst.addOperand(Inst.getOperand(1)); // lane
9474     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9475     TmpInst.addOperand(Inst.getOperand(5));
9476     Inst = TmpInst;
9477     return true;
9478   }
9479 
9480   case ARM::VLD3LNdAsm_8:
9481   case ARM::VLD3LNdAsm_16:
9482   case ARM::VLD3LNdAsm_32:
9483   case ARM::VLD3LNqAsm_16:
9484   case ARM::VLD3LNqAsm_32: {
9485     MCInst TmpInst;
9486     // Shuffle the operands around so the lane index operand is in the
9487     // right place.
9488     unsigned Spacing;
9489     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9490     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9491     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9492                                             Spacing));
9493     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9494                                             Spacing * 2));
9495     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9496     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9497     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
9498     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9499                                             Spacing));
9500     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9501                                             Spacing * 2));
9502     TmpInst.addOperand(Inst.getOperand(1)); // lane
9503     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9504     TmpInst.addOperand(Inst.getOperand(5));
9505     Inst = TmpInst;
9506     return true;
9507   }
9508 
9509   case ARM::VLD4LNdAsm_8:
9510   case ARM::VLD4LNdAsm_16:
9511   case ARM::VLD4LNdAsm_32:
9512   case ARM::VLD4LNqAsm_16:
9513   case ARM::VLD4LNqAsm_32: {
9514     MCInst TmpInst;
9515     // Shuffle the operands around so the lane index operand is in the
9516     // right place.
9517     unsigned Spacing;
9518     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9519     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9520     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9521                                             Spacing));
9522     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9523                                             Spacing * 2));
9524     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9525                                             Spacing * 3));
9526     TmpInst.addOperand(Inst.getOperand(2)); // Rn
9527     TmpInst.addOperand(Inst.getOperand(3)); // alignment
9528     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
9529     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9530                                             Spacing));
9531     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9532                                             Spacing * 2));
9533     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9534                                             Spacing * 3));
9535     TmpInst.addOperand(Inst.getOperand(1)); // lane
9536     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9537     TmpInst.addOperand(Inst.getOperand(5));
9538     Inst = TmpInst;
9539     return true;
9540   }
9541 
9542   // VLD3DUP single 3-element structure to all lanes instructions.
9543   case ARM::VLD3DUPdAsm_8:
9544   case ARM::VLD3DUPdAsm_16:
9545   case ARM::VLD3DUPdAsm_32:
9546   case ARM::VLD3DUPqAsm_8:
9547   case ARM::VLD3DUPqAsm_16:
9548   case ARM::VLD3DUPqAsm_32: {
9549     MCInst TmpInst;
9550     unsigned Spacing;
9551     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9552     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9553     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9554                                             Spacing));
9555     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9556                                             Spacing * 2));
9557     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9558     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9559     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
9560     TmpInst.addOperand(Inst.getOperand(4));
9561     Inst = TmpInst;
9562     return true;
9563   }
9564 
9565   case ARM::VLD3DUPdWB_fixed_Asm_8:
9566   case ARM::VLD3DUPdWB_fixed_Asm_16:
9567   case ARM::VLD3DUPdWB_fixed_Asm_32:
9568   case ARM::VLD3DUPqWB_fixed_Asm_8:
9569   case ARM::VLD3DUPqWB_fixed_Asm_16:
9570   case ARM::VLD3DUPqWB_fixed_Asm_32: {
9571     MCInst TmpInst;
9572     unsigned Spacing;
9573     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9574     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9575     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9576                                             Spacing));
9577     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9578                                             Spacing * 2));
9579     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9580     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
9581     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9582     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
9583     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
9584     TmpInst.addOperand(Inst.getOperand(4));
9585     Inst = TmpInst;
9586     return true;
9587   }
9588 
9589   case ARM::VLD3DUPdWB_register_Asm_8:
9590   case ARM::VLD3DUPdWB_register_Asm_16:
9591   case ARM::VLD3DUPdWB_register_Asm_32:
9592   case ARM::VLD3DUPqWB_register_Asm_8:
9593   case ARM::VLD3DUPqWB_register_Asm_16:
9594   case ARM::VLD3DUPqWB_register_Asm_32: {
9595     MCInst TmpInst;
9596     unsigned Spacing;
9597     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9598     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9599     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9600                                             Spacing));
9601     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9602                                             Spacing * 2));
9603     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9604     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
9605     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9606     TmpInst.addOperand(Inst.getOperand(3)); // Rm
9607     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9608     TmpInst.addOperand(Inst.getOperand(5));
9609     Inst = TmpInst;
9610     return true;
9611   }
9612 
9613   // VLD3 multiple 3-element structure instructions.
9614   case ARM::VLD3dAsm_8:
9615   case ARM::VLD3dAsm_16:
9616   case ARM::VLD3dAsm_32:
9617   case ARM::VLD3qAsm_8:
9618   case ARM::VLD3qAsm_16:
9619   case ARM::VLD3qAsm_32: {
9620     MCInst TmpInst;
9621     unsigned Spacing;
9622     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9623     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9624     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9625                                             Spacing));
9626     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9627                                             Spacing * 2));
9628     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9629     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9630     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
9631     TmpInst.addOperand(Inst.getOperand(4));
9632     Inst = TmpInst;
9633     return true;
9634   }
9635 
9636   case ARM::VLD3dWB_fixed_Asm_8:
9637   case ARM::VLD3dWB_fixed_Asm_16:
9638   case ARM::VLD3dWB_fixed_Asm_32:
9639   case ARM::VLD3qWB_fixed_Asm_8:
9640   case ARM::VLD3qWB_fixed_Asm_16:
9641   case ARM::VLD3qWB_fixed_Asm_32: {
9642     MCInst TmpInst;
9643     unsigned Spacing;
9644     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9645     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9646     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9647                                             Spacing));
9648     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9649                                             Spacing * 2));
9650     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9651     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
9652     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9653     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
9654     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
9655     TmpInst.addOperand(Inst.getOperand(4));
9656     Inst = TmpInst;
9657     return true;
9658   }
9659 
9660   case ARM::VLD3dWB_register_Asm_8:
9661   case ARM::VLD3dWB_register_Asm_16:
9662   case ARM::VLD3dWB_register_Asm_32:
9663   case ARM::VLD3qWB_register_Asm_8:
9664   case ARM::VLD3qWB_register_Asm_16:
9665   case ARM::VLD3qWB_register_Asm_32: {
9666     MCInst TmpInst;
9667     unsigned Spacing;
9668     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9669     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9670     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9671                                             Spacing));
9672     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9673                                             Spacing * 2));
9674     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9675     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
9676     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9677     TmpInst.addOperand(Inst.getOperand(3)); // Rm
9678     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9679     TmpInst.addOperand(Inst.getOperand(5));
9680     Inst = TmpInst;
9681     return true;
9682   }
9683 
9684   // VLD4DUP single 3-element structure to all lanes instructions.
9685   case ARM::VLD4DUPdAsm_8:
9686   case ARM::VLD4DUPdAsm_16:
9687   case ARM::VLD4DUPdAsm_32:
9688   case ARM::VLD4DUPqAsm_8:
9689   case ARM::VLD4DUPqAsm_16:
9690   case ARM::VLD4DUPqAsm_32: {
9691     MCInst TmpInst;
9692     unsigned Spacing;
9693     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9694     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9695     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9696                                             Spacing));
9697     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9698                                             Spacing * 2));
9699     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9700                                             Spacing * 3));
9701     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9702     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9703     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
9704     TmpInst.addOperand(Inst.getOperand(4));
9705     Inst = TmpInst;
9706     return true;
9707   }
9708 
9709   case ARM::VLD4DUPdWB_fixed_Asm_8:
9710   case ARM::VLD4DUPdWB_fixed_Asm_16:
9711   case ARM::VLD4DUPdWB_fixed_Asm_32:
9712   case ARM::VLD4DUPqWB_fixed_Asm_8:
9713   case ARM::VLD4DUPqWB_fixed_Asm_16:
9714   case ARM::VLD4DUPqWB_fixed_Asm_32: {
9715     MCInst TmpInst;
9716     unsigned Spacing;
9717     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9718     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9719     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9720                                             Spacing));
9721     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9722                                             Spacing * 2));
9723     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9724                                             Spacing * 3));
9725     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9726     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
9727     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9728     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
9729     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
9730     TmpInst.addOperand(Inst.getOperand(4));
9731     Inst = TmpInst;
9732     return true;
9733   }
9734 
9735   case ARM::VLD4DUPdWB_register_Asm_8:
9736   case ARM::VLD4DUPdWB_register_Asm_16:
9737   case ARM::VLD4DUPdWB_register_Asm_32:
9738   case ARM::VLD4DUPqWB_register_Asm_8:
9739   case ARM::VLD4DUPqWB_register_Asm_16:
9740   case ARM::VLD4DUPqWB_register_Asm_32: {
9741     MCInst TmpInst;
9742     unsigned Spacing;
9743     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9744     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9745     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9746                                             Spacing));
9747     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9748                                             Spacing * 2));
9749     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9750                                             Spacing * 3));
9751     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9752     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
9753     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9754     TmpInst.addOperand(Inst.getOperand(3)); // Rm
9755     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9756     TmpInst.addOperand(Inst.getOperand(5));
9757     Inst = TmpInst;
9758     return true;
9759   }
9760 
9761   // VLD4 multiple 4-element structure instructions.
9762   case ARM::VLD4dAsm_8:
9763   case ARM::VLD4dAsm_16:
9764   case ARM::VLD4dAsm_32:
9765   case ARM::VLD4qAsm_8:
9766   case ARM::VLD4qAsm_16:
9767   case ARM::VLD4qAsm_32: {
9768     MCInst TmpInst;
9769     unsigned Spacing;
9770     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9771     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9772     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9773                                             Spacing));
9774     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9775                                             Spacing * 2));
9776     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9777                                             Spacing * 3));
9778     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9779     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9780     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
9781     TmpInst.addOperand(Inst.getOperand(4));
9782     Inst = TmpInst;
9783     return true;
9784   }
9785 
9786   case ARM::VLD4dWB_fixed_Asm_8:
9787   case ARM::VLD4dWB_fixed_Asm_16:
9788   case ARM::VLD4dWB_fixed_Asm_32:
9789   case ARM::VLD4qWB_fixed_Asm_8:
9790   case ARM::VLD4qWB_fixed_Asm_16:
9791   case ARM::VLD4qWB_fixed_Asm_32: {
9792     MCInst TmpInst;
9793     unsigned Spacing;
9794     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9795     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9796     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9797                                             Spacing));
9798     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9799                                             Spacing * 2));
9800     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9801                                             Spacing * 3));
9802     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9803     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
9804     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9805     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
9806     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
9807     TmpInst.addOperand(Inst.getOperand(4));
9808     Inst = TmpInst;
9809     return true;
9810   }
9811 
9812   case ARM::VLD4dWB_register_Asm_8:
9813   case ARM::VLD4dWB_register_Asm_16:
9814   case ARM::VLD4dWB_register_Asm_32:
9815   case ARM::VLD4qWB_register_Asm_8:
9816   case ARM::VLD4qWB_register_Asm_16:
9817   case ARM::VLD4qWB_register_Asm_32: {
9818     MCInst TmpInst;
9819     unsigned Spacing;
9820     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
9821     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9822     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9823                                             Spacing));
9824     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9825                                             Spacing * 2));
9826     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9827                                             Spacing * 3));
9828     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9829     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
9830     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9831     TmpInst.addOperand(Inst.getOperand(3)); // Rm
9832     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9833     TmpInst.addOperand(Inst.getOperand(5));
9834     Inst = TmpInst;
9835     return true;
9836   }
9837 
9838   // VST3 multiple 3-element structure instructions.
9839   case ARM::VST3dAsm_8:
9840   case ARM::VST3dAsm_16:
9841   case ARM::VST3dAsm_32:
9842   case ARM::VST3qAsm_8:
9843   case ARM::VST3qAsm_16:
9844   case ARM::VST3qAsm_32: {
9845     MCInst TmpInst;
9846     unsigned Spacing;
9847     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
9848     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9849     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9850     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9851     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9852                                             Spacing));
9853     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9854                                             Spacing * 2));
9855     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
9856     TmpInst.addOperand(Inst.getOperand(4));
9857     Inst = TmpInst;
9858     return true;
9859   }
9860 
9861   case ARM::VST3dWB_fixed_Asm_8:
9862   case ARM::VST3dWB_fixed_Asm_16:
9863   case ARM::VST3dWB_fixed_Asm_32:
9864   case ARM::VST3qWB_fixed_Asm_8:
9865   case ARM::VST3qWB_fixed_Asm_16:
9866   case ARM::VST3qWB_fixed_Asm_32: {
9867     MCInst TmpInst;
9868     unsigned Spacing;
9869     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
9870     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9871     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
9872     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9873     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
9874     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9875     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9876                                             Spacing));
9877     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9878                                             Spacing * 2));
9879     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
9880     TmpInst.addOperand(Inst.getOperand(4));
9881     Inst = TmpInst;
9882     return true;
9883   }
9884 
9885   case ARM::VST3dWB_register_Asm_8:
9886   case ARM::VST3dWB_register_Asm_16:
9887   case ARM::VST3dWB_register_Asm_32:
9888   case ARM::VST3qWB_register_Asm_8:
9889   case ARM::VST3qWB_register_Asm_16:
9890   case ARM::VST3qWB_register_Asm_32: {
9891     MCInst TmpInst;
9892     unsigned Spacing;
9893     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
9894     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9895     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
9896     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9897     TmpInst.addOperand(Inst.getOperand(3)); // Rm
9898     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9899     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9900                                             Spacing));
9901     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9902                                             Spacing * 2));
9903     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9904     TmpInst.addOperand(Inst.getOperand(5));
9905     Inst = TmpInst;
9906     return true;
9907   }
9908 
9909   // VST4 multiple 3-element structure instructions.
9910   case ARM::VST4dAsm_8:
9911   case ARM::VST4dAsm_16:
9912   case ARM::VST4dAsm_32:
9913   case ARM::VST4qAsm_8:
9914   case ARM::VST4qAsm_16:
9915   case ARM::VST4qAsm_32: {
9916     MCInst TmpInst;
9917     unsigned Spacing;
9918     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
9919     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9920     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9921     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9922     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9923                                             Spacing));
9924     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9925                                             Spacing * 2));
9926     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9927                                             Spacing * 3));
9928     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
9929     TmpInst.addOperand(Inst.getOperand(4));
9930     Inst = TmpInst;
9931     return true;
9932   }
9933 
9934   case ARM::VST4dWB_fixed_Asm_8:
9935   case ARM::VST4dWB_fixed_Asm_16:
9936   case ARM::VST4dWB_fixed_Asm_32:
9937   case ARM::VST4qWB_fixed_Asm_8:
9938   case ARM::VST4qWB_fixed_Asm_16:
9939   case ARM::VST4qWB_fixed_Asm_32: {
9940     MCInst TmpInst;
9941     unsigned Spacing;
9942     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
9943     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9944     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
9945     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9946     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
9947     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9948     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9949                                             Spacing));
9950     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9951                                             Spacing * 2));
9952     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9953                                             Spacing * 3));
9954     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
9955     TmpInst.addOperand(Inst.getOperand(4));
9956     Inst = TmpInst;
9957     return true;
9958   }
9959 
9960   case ARM::VST4dWB_register_Asm_8:
9961   case ARM::VST4dWB_register_Asm_16:
9962   case ARM::VST4dWB_register_Asm_32:
9963   case ARM::VST4qWB_register_Asm_8:
9964   case ARM::VST4qWB_register_Asm_16:
9965   case ARM::VST4qWB_register_Asm_32: {
9966     MCInst TmpInst;
9967     unsigned Spacing;
9968     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
9969     TmpInst.addOperand(Inst.getOperand(1)); // Rn
9970     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
9971     TmpInst.addOperand(Inst.getOperand(2)); // alignment
9972     TmpInst.addOperand(Inst.getOperand(3)); // Rm
9973     TmpInst.addOperand(Inst.getOperand(0)); // Vd
9974     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9975                                             Spacing));
9976     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9977                                             Spacing * 2));
9978     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
9979                                             Spacing * 3));
9980     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
9981     TmpInst.addOperand(Inst.getOperand(5));
9982     Inst = TmpInst;
9983     return true;
9984   }
9985 
9986   // Handle encoding choice for the shift-immediate instructions.
9987   case ARM::t2LSLri:
9988   case ARM::t2LSRri:
9989   case ARM::t2ASRri:
9990     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
9991         isARMLowRegister(Inst.getOperand(1).getReg()) &&
9992         Inst.getOperand(5).getReg() == (inITBlock() ? 0 : ARM::CPSR) &&
9993         !HasWideQualifier) {
9994       unsigned NewOpc;
9995       switch (Inst.getOpcode()) {
9996       default: llvm_unreachable("unexpected opcode");
9997       case ARM::t2LSLri: NewOpc = ARM::tLSLri; break;
9998       case ARM::t2LSRri: NewOpc = ARM::tLSRri; break;
9999       case ARM::t2ASRri: NewOpc = ARM::tASRri; break;
10000       }
10001       // The Thumb1 operands aren't in the same order. Awesome, eh?
10002       MCInst TmpInst;
10003       TmpInst.setOpcode(NewOpc);
10004       TmpInst.addOperand(Inst.getOperand(0));
10005       TmpInst.addOperand(Inst.getOperand(5));
10006       TmpInst.addOperand(Inst.getOperand(1));
10007       TmpInst.addOperand(Inst.getOperand(2));
10008       TmpInst.addOperand(Inst.getOperand(3));
10009       TmpInst.addOperand(Inst.getOperand(4));
10010       Inst = TmpInst;
10011       return true;
10012     }
10013     return false;
10014 
10015   // Handle the Thumb2 mode MOV complex aliases.
10016   case ARM::t2MOVsr:
10017   case ARM::t2MOVSsr: {
10018     // Which instruction to expand to depends on the CCOut operand and
10019     // whether we're in an IT block if the register operands are low
10020     // registers.
10021     bool isNarrow = false;
10022     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
10023         isARMLowRegister(Inst.getOperand(1).getReg()) &&
10024         isARMLowRegister(Inst.getOperand(2).getReg()) &&
10025         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
10026         inITBlock() == (Inst.getOpcode() == ARM::t2MOVsr) &&
10027         !HasWideQualifier)
10028       isNarrow = true;
10029     MCInst TmpInst;
10030     unsigned newOpc;
10031     switch(ARM_AM::getSORegShOp(Inst.getOperand(3).getImm())) {
10032     default: llvm_unreachable("unexpected opcode!");
10033     case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRrr : ARM::t2ASRrr; break;
10034     case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRrr : ARM::t2LSRrr; break;
10035     case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLrr : ARM::t2LSLrr; break;
10036     case ARM_AM::ror: newOpc = isNarrow ? ARM::tROR   : ARM::t2RORrr; break;
10037     }
10038     TmpInst.setOpcode(newOpc);
10039     TmpInst.addOperand(Inst.getOperand(0)); // Rd
10040     if (isNarrow)
10041       TmpInst.addOperand(MCOperand::createReg(
10042           Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
10043     TmpInst.addOperand(Inst.getOperand(1)); // Rn
10044     TmpInst.addOperand(Inst.getOperand(2)); // Rm
10045     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
10046     TmpInst.addOperand(Inst.getOperand(5));
10047     if (!isNarrow)
10048       TmpInst.addOperand(MCOperand::createReg(
10049           Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
10050     Inst = TmpInst;
10051     return true;
10052   }
10053   case ARM::t2MOVsi:
10054   case ARM::t2MOVSsi: {
10055     // Which instruction to expand to depends on the CCOut operand and
10056     // whether we're in an IT block if the register operands are low
10057     // registers.
10058     bool isNarrow = false;
10059     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
10060         isARMLowRegister(Inst.getOperand(1).getReg()) &&
10061         inITBlock() == (Inst.getOpcode() == ARM::t2MOVsi) &&
10062         !HasWideQualifier)
10063       isNarrow = true;
10064     MCInst TmpInst;
10065     unsigned newOpc;
10066     unsigned Shift = ARM_AM::getSORegShOp(Inst.getOperand(2).getImm());
10067     unsigned Amount = ARM_AM::getSORegOffset(Inst.getOperand(2).getImm());
10068     bool isMov = false;
10069     // MOV rd, rm, LSL #0 is actually a MOV instruction
10070     if (Shift == ARM_AM::lsl && Amount == 0) {
10071       isMov = true;
10072       // The 16-bit encoding of MOV rd, rm, LSL #N is explicitly encoding T2 of
10073       // MOV (register) in the ARMv8-A and ARMv8-M manuals, and immediate 0 is
10074       // unpredictable in an IT block so the 32-bit encoding T3 has to be used
10075       // instead.
10076       if (inITBlock()) {
10077         isNarrow = false;
10078       }
10079       newOpc = isNarrow ? ARM::tMOVSr : ARM::t2MOVr;
10080     } else {
10081       switch(Shift) {
10082       default: llvm_unreachable("unexpected opcode!");
10083       case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRri : ARM::t2ASRri; break;
10084       case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRri : ARM::t2LSRri; break;
10085       case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLri : ARM::t2LSLri; break;
10086       case ARM_AM::ror: newOpc = ARM::t2RORri; isNarrow = false; break;
10087       case ARM_AM::rrx: isNarrow = false; newOpc = ARM::t2RRX; break;
10088       }
10089     }
10090     if (Amount == 32) Amount = 0;
10091     TmpInst.setOpcode(newOpc);
10092     TmpInst.addOperand(Inst.getOperand(0)); // Rd
10093     if (isNarrow && !isMov)
10094       TmpInst.addOperand(MCOperand::createReg(
10095           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
10096     TmpInst.addOperand(Inst.getOperand(1)); // Rn
10097     if (newOpc != ARM::t2RRX && !isMov)
10098       TmpInst.addOperand(MCOperand::createImm(Amount));
10099     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
10100     TmpInst.addOperand(Inst.getOperand(4));
10101     if (!isNarrow)
10102       TmpInst.addOperand(MCOperand::createReg(
10103           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
10104     Inst = TmpInst;
10105     return true;
10106   }
10107   // Handle the ARM mode MOV complex aliases.
10108   case ARM::ASRr:
10109   case ARM::LSRr:
10110   case ARM::LSLr:
10111   case ARM::RORr: {
10112     ARM_AM::ShiftOpc ShiftTy;
10113     switch(Inst.getOpcode()) {
10114     default: llvm_unreachable("unexpected opcode!");
10115     case ARM::ASRr: ShiftTy = ARM_AM::asr; break;
10116     case ARM::LSRr: ShiftTy = ARM_AM::lsr; break;
10117     case ARM::LSLr: ShiftTy = ARM_AM::lsl; break;
10118     case ARM::RORr: ShiftTy = ARM_AM::ror; break;
10119     }
10120     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, 0);
10121     MCInst TmpInst;
10122     TmpInst.setOpcode(ARM::MOVsr);
10123     TmpInst.addOperand(Inst.getOperand(0)); // Rd
10124     TmpInst.addOperand(Inst.getOperand(1)); // Rn
10125     TmpInst.addOperand(Inst.getOperand(2)); // Rm
10126     TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
10127     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
10128     TmpInst.addOperand(Inst.getOperand(4));
10129     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
10130     Inst = TmpInst;
10131     return true;
10132   }
10133   case ARM::ASRi:
10134   case ARM::LSRi:
10135   case ARM::LSLi:
10136   case ARM::RORi: {
10137     ARM_AM::ShiftOpc ShiftTy;
10138     switch(Inst.getOpcode()) {
10139     default: llvm_unreachable("unexpected opcode!");
10140     case ARM::ASRi: ShiftTy = ARM_AM::asr; break;
10141     case ARM::LSRi: ShiftTy = ARM_AM::lsr; break;
10142     case ARM::LSLi: ShiftTy = ARM_AM::lsl; break;
10143     case ARM::RORi: ShiftTy = ARM_AM::ror; break;
10144     }
10145     // A shift by zero is a plain MOVr, not a MOVsi.
10146     unsigned Amt = Inst.getOperand(2).getImm();
10147     unsigned Opc = Amt == 0 ? ARM::MOVr : ARM::MOVsi;
10148     // A shift by 32 should be encoded as 0 when permitted
10149     if (Amt == 32 && (ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr))
10150       Amt = 0;
10151     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, Amt);
10152     MCInst TmpInst;
10153     TmpInst.setOpcode(Opc);
10154     TmpInst.addOperand(Inst.getOperand(0)); // Rd
10155     TmpInst.addOperand(Inst.getOperand(1)); // Rn
10156     if (Opc == ARM::MOVsi)
10157       TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
10158     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
10159     TmpInst.addOperand(Inst.getOperand(4));
10160     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
10161     Inst = TmpInst;
10162     return true;
10163   }
10164   case ARM::RRXi: {
10165     unsigned Shifter = ARM_AM::getSORegOpc(ARM_AM::rrx, 0);
10166     MCInst TmpInst;
10167     TmpInst.setOpcode(ARM::MOVsi);
10168     TmpInst.addOperand(Inst.getOperand(0)); // Rd
10169     TmpInst.addOperand(Inst.getOperand(1)); // Rn
10170     TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
10171     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
10172     TmpInst.addOperand(Inst.getOperand(3));
10173     TmpInst.addOperand(Inst.getOperand(4)); // cc_out
10174     Inst = TmpInst;
10175     return true;
10176   }
10177   case ARM::t2LDMIA_UPD: {
10178     // If this is a load of a single register, then we should use
10179     // a post-indexed LDR instruction instead, per the ARM ARM.
10180     if (Inst.getNumOperands() != 5)
10181       return false;
10182     MCInst TmpInst;
10183     TmpInst.setOpcode(ARM::t2LDR_POST);
10184     TmpInst.addOperand(Inst.getOperand(4)); // Rt
10185     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
10186     TmpInst.addOperand(Inst.getOperand(1)); // Rn
10187     TmpInst.addOperand(MCOperand::createImm(4));
10188     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
10189     TmpInst.addOperand(Inst.getOperand(3));
10190     Inst = TmpInst;
10191     return true;
10192   }
10193   case ARM::t2STMDB_UPD: {
10194     // If this is a store of a single register, then we should use
10195     // a pre-indexed STR instruction instead, per the ARM ARM.
10196     if (Inst.getNumOperands() != 5)
10197       return false;
10198     MCInst TmpInst;
10199     TmpInst.setOpcode(ARM::t2STR_PRE);
10200     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
10201     TmpInst.addOperand(Inst.getOperand(4)); // Rt
10202     TmpInst.addOperand(Inst.getOperand(1)); // Rn
10203     TmpInst.addOperand(MCOperand::createImm(-4));
10204     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
10205     TmpInst.addOperand(Inst.getOperand(3));
10206     Inst = TmpInst;
10207     return true;
10208   }
10209   case ARM::LDMIA_UPD:
10210     // If this is a load of a single register via a 'pop', then we should use
10211     // a post-indexed LDR instruction instead, per the ARM ARM.
10212     if (static_cast<ARMOperand &>(*Operands[0]).getToken() == "pop" &&
10213         Inst.getNumOperands() == 5) {
10214       MCInst TmpInst;
10215       TmpInst.setOpcode(ARM::LDR_POST_IMM);
10216       TmpInst.addOperand(Inst.getOperand(4)); // Rt
10217       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
10218       TmpInst.addOperand(Inst.getOperand(1)); // Rn
10219       TmpInst.addOperand(MCOperand::createReg(0));  // am2offset
10220       TmpInst.addOperand(MCOperand::createImm(4));
10221       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
10222       TmpInst.addOperand(Inst.getOperand(3));
10223       Inst = TmpInst;
10224       return true;
10225     }
10226     break;
10227   case ARM::STMDB_UPD:
10228     // If this is a store of a single register via a 'push', then we should use
10229     // a pre-indexed STR instruction instead, per the ARM ARM.
10230     if (static_cast<ARMOperand &>(*Operands[0]).getToken() == "push" &&
10231         Inst.getNumOperands() == 5) {
10232       MCInst TmpInst;
10233       TmpInst.setOpcode(ARM::STR_PRE_IMM);
10234       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
10235       TmpInst.addOperand(Inst.getOperand(4)); // Rt
10236       TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
10237       TmpInst.addOperand(MCOperand::createImm(-4));
10238       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
10239       TmpInst.addOperand(Inst.getOperand(3));
10240       Inst = TmpInst;
10241     }
10242     break;
10243   case ARM::t2ADDri12:
10244   case ARM::t2SUBri12:
10245   case ARM::t2ADDspImm12:
10246   case ARM::t2SUBspImm12: {
10247     // If the immediate fits for encoding T3 and the generic
10248     // mnemonic was used, encoding T3 is preferred.
10249     const StringRef Token = static_cast<ARMOperand &>(*Operands[0]).getToken();
10250     if ((Token != "add" && Token != "sub") ||
10251         ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
10252       break;
10253     switch (Inst.getOpcode()) {
10254     case ARM::t2ADDri12:
10255       Inst.setOpcode(ARM::t2ADDri);
10256       break;
10257     case ARM::t2SUBri12:
10258       Inst.setOpcode(ARM::t2SUBri);
10259       break;
10260     case ARM::t2ADDspImm12:
10261       Inst.setOpcode(ARM::t2ADDspImm);
10262       break;
10263     case ARM::t2SUBspImm12:
10264       Inst.setOpcode(ARM::t2SUBspImm);
10265       break;
10266     }
10267 
10268     Inst.addOperand(MCOperand::createReg(0)); // cc_out
10269     return true;
10270   }
10271   case ARM::tADDi8:
10272     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
10273     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
10274     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
10275     // to encoding T1 if <Rd> is omitted."
10276     if ((unsigned)Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
10277       Inst.setOpcode(ARM::tADDi3);
10278       return true;
10279     }
10280     break;
10281   case ARM::tSUBi8:
10282     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
10283     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
10284     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
10285     // to encoding T1 if <Rd> is omitted."
10286     if ((unsigned)Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
10287       Inst.setOpcode(ARM::tSUBi3);
10288       return true;
10289     }
10290     break;
10291   case ARM::t2ADDri:
10292   case ARM::t2SUBri: {
10293     // If the destination and first source operand are the same, and
10294     // the flags are compatible with the current IT status, use encoding T2
10295     // instead of T3. For compatibility with the system 'as'. Make sure the
10296     // wide encoding wasn't explicit.
10297     if (Inst.getOperand(0).getReg() != Inst.getOperand(1).getReg() ||
10298         !isARMLowRegister(Inst.getOperand(0).getReg()) ||
10299         (Inst.getOperand(2).isImm() &&
10300          (unsigned)Inst.getOperand(2).getImm() > 255) ||
10301         Inst.getOperand(5).getReg() != (inITBlock() ? 0 : ARM::CPSR) ||
10302         HasWideQualifier)
10303       break;
10304     MCInst TmpInst;
10305     TmpInst.setOpcode(Inst.getOpcode() == ARM::t2ADDri ?
10306                       ARM::tADDi8 : ARM::tSUBi8);
10307     TmpInst.addOperand(Inst.getOperand(0));
10308     TmpInst.addOperand(Inst.getOperand(5));
10309     TmpInst.addOperand(Inst.getOperand(0));
10310     TmpInst.addOperand(Inst.getOperand(2));
10311     TmpInst.addOperand(Inst.getOperand(3));
10312     TmpInst.addOperand(Inst.getOperand(4));
10313     Inst = TmpInst;
10314     return true;
10315   }
10316   case ARM::t2ADDspImm:
10317   case ARM::t2SUBspImm: {
10318     // Prefer T1 encoding if possible
10319     if (Inst.getOperand(5).getReg() != 0 || HasWideQualifier)
10320       break;
10321     unsigned V = Inst.getOperand(2).getImm();
10322     if (V & 3 || V > ((1 << 7) - 1) << 2)
10323       break;
10324     MCInst TmpInst;
10325     TmpInst.setOpcode(Inst.getOpcode() == ARM::t2ADDspImm ? ARM::tADDspi
10326                                                           : ARM::tSUBspi);
10327     TmpInst.addOperand(MCOperand::createReg(ARM::SP)); // destination reg
10328     TmpInst.addOperand(MCOperand::createReg(ARM::SP)); // source reg
10329     TmpInst.addOperand(MCOperand::createImm(V / 4));   // immediate
10330     TmpInst.addOperand(Inst.getOperand(3));            // pred
10331     TmpInst.addOperand(Inst.getOperand(4));
10332     Inst = TmpInst;
10333     return true;
10334   }
10335   case ARM::t2ADDrr: {
10336     // If the destination and first source operand are the same, and
10337     // there's no setting of the flags, use encoding T2 instead of T3.
10338     // Note that this is only for ADD, not SUB. This mirrors the system
10339     // 'as' behaviour.  Also take advantage of ADD being commutative.
10340     // Make sure the wide encoding wasn't explicit.
10341     bool Swap = false;
10342     auto DestReg = Inst.getOperand(0).getReg();
10343     bool Transform = DestReg == Inst.getOperand(1).getReg();
10344     if (!Transform && DestReg == Inst.getOperand(2).getReg()) {
10345       Transform = true;
10346       Swap = true;
10347     }
10348     if (!Transform ||
10349         Inst.getOperand(5).getReg() != 0 ||
10350         HasWideQualifier)
10351       break;
10352     MCInst TmpInst;
10353     TmpInst.setOpcode(ARM::tADDhirr);
10354     TmpInst.addOperand(Inst.getOperand(0));
10355     TmpInst.addOperand(Inst.getOperand(0));
10356     TmpInst.addOperand(Inst.getOperand(Swap ? 1 : 2));
10357     TmpInst.addOperand(Inst.getOperand(3));
10358     TmpInst.addOperand(Inst.getOperand(4));
10359     Inst = TmpInst;
10360     return true;
10361   }
10362   case ARM::tADDrSP:
10363     // If the non-SP source operand and the destination operand are not the
10364     // same, we need to use the 32-bit encoding if it's available.
10365     if (Inst.getOperand(0).getReg() != Inst.getOperand(2).getReg()) {
10366       Inst.setOpcode(ARM::t2ADDrr);
10367       Inst.addOperand(MCOperand::createReg(0)); // cc_out
10368       return true;
10369     }
10370     break;
10371   case ARM::tB:
10372     // A Thumb conditional branch outside of an IT block is a tBcc.
10373     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()) {
10374       Inst.setOpcode(ARM::tBcc);
10375       return true;
10376     }
10377     break;
10378   case ARM::t2B:
10379     // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
10380     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()){
10381       Inst.setOpcode(ARM::t2Bcc);
10382       return true;
10383     }
10384     break;
10385   case ARM::t2Bcc:
10386     // If the conditional is AL or we're in an IT block, we really want t2B.
10387     if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock()) {
10388       Inst.setOpcode(ARM::t2B);
10389       return true;
10390     }
10391     break;
10392   case ARM::tBcc:
10393     // If the conditional is AL, we really want tB.
10394     if (Inst.getOperand(1).getImm() == ARMCC::AL) {
10395       Inst.setOpcode(ARM::tB);
10396       return true;
10397     }
10398     break;
10399   case ARM::tLDMIA: {
10400     // If the register list contains any high registers, or if the writeback
10401     // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
10402     // instead if we're in Thumb2. Otherwise, this should have generated
10403     // an error in validateInstruction().
10404     unsigned Rn = Inst.getOperand(0).getReg();
10405     bool hasWritebackToken =
10406         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
10407          static_cast<ARMOperand &>(*Operands[3]).getToken() == "!");
10408     bool listContainsBase;
10409     if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
10410         (!listContainsBase && !hasWritebackToken) ||
10411         (listContainsBase && hasWritebackToken)) {
10412       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
10413       assert(isThumbTwo());
10414       Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
10415       // If we're switching to the updating version, we need to insert
10416       // the writeback tied operand.
10417       if (hasWritebackToken)
10418         Inst.insert(Inst.begin(),
10419                     MCOperand::createReg(Inst.getOperand(0).getReg()));
10420       return true;
10421     }
10422     break;
10423   }
10424   case ARM::tSTMIA_UPD: {
10425     // If the register list contains any high registers, we need to use
10426     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
10427     // should have generated an error in validateInstruction().
10428     unsigned Rn = Inst.getOperand(0).getReg();
10429     bool listContainsBase;
10430     if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
10431       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
10432       assert(isThumbTwo());
10433       Inst.setOpcode(ARM::t2STMIA_UPD);
10434       return true;
10435     }
10436     break;
10437   }
10438   case ARM::tPOP: {
10439     bool listContainsBase;
10440     // If the register list contains any high registers, we need to use
10441     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
10442     // should have generated an error in validateInstruction().
10443     if (!checkLowRegisterList(Inst, 2, 0, ARM::PC, listContainsBase))
10444       return false;
10445     assert(isThumbTwo());
10446     Inst.setOpcode(ARM::t2LDMIA_UPD);
10447     // Add the base register and writeback operands.
10448     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
10449     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
10450     return true;
10451   }
10452   case ARM::tPUSH: {
10453     bool listContainsBase;
10454     if (!checkLowRegisterList(Inst, 2, 0, ARM::LR, listContainsBase))
10455       return false;
10456     assert(isThumbTwo());
10457     Inst.setOpcode(ARM::t2STMDB_UPD);
10458     // Add the base register and writeback operands.
10459     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
10460     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
10461     return true;
10462   }
10463   case ARM::t2MOVi:
10464     // If we can use the 16-bit encoding and the user didn't explicitly
10465     // request the 32-bit variant, transform it here.
10466     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
10467         (Inst.getOperand(1).isImm() &&
10468          (unsigned)Inst.getOperand(1).getImm() <= 255) &&
10469         Inst.getOperand(4).getReg() == (inITBlock() ? 0 : ARM::CPSR) &&
10470         !HasWideQualifier) {
10471       // The operands aren't in the same order for tMOVi8...
10472       MCInst TmpInst;
10473       TmpInst.setOpcode(ARM::tMOVi8);
10474       TmpInst.addOperand(Inst.getOperand(0));
10475       TmpInst.addOperand(Inst.getOperand(4));
10476       TmpInst.addOperand(Inst.getOperand(1));
10477       TmpInst.addOperand(Inst.getOperand(2));
10478       TmpInst.addOperand(Inst.getOperand(3));
10479       Inst = TmpInst;
10480       return true;
10481     }
10482     break;
10483 
10484   case ARM::t2MOVr:
10485     // If we can use the 16-bit encoding and the user didn't explicitly
10486     // request the 32-bit variant, transform it here.
10487     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
10488         isARMLowRegister(Inst.getOperand(1).getReg()) &&
10489         Inst.getOperand(2).getImm() == ARMCC::AL &&
10490         Inst.getOperand(4).getReg() == ARM::CPSR &&
10491         !HasWideQualifier) {
10492       // The operands aren't the same for tMOV[S]r... (no cc_out)
10493       MCInst TmpInst;
10494       unsigned Op = Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr;
10495       TmpInst.setOpcode(Op);
10496       TmpInst.addOperand(Inst.getOperand(0));
10497       TmpInst.addOperand(Inst.getOperand(1));
10498       if (Op == ARM::tMOVr) {
10499         TmpInst.addOperand(Inst.getOperand(2));
10500         TmpInst.addOperand(Inst.getOperand(3));
10501       }
10502       Inst = TmpInst;
10503       return true;
10504     }
10505     break;
10506 
10507   case ARM::t2SXTH:
10508   case ARM::t2SXTB:
10509   case ARM::t2UXTH:
10510   case ARM::t2UXTB:
10511     // If we can use the 16-bit encoding and the user didn't explicitly
10512     // request the 32-bit variant, transform it here.
10513     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
10514         isARMLowRegister(Inst.getOperand(1).getReg()) &&
10515         Inst.getOperand(2).getImm() == 0 &&
10516         !HasWideQualifier) {
10517       unsigned NewOpc;
10518       switch (Inst.getOpcode()) {
10519       default: llvm_unreachable("Illegal opcode!");
10520       case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
10521       case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
10522       case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
10523       case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
10524       }
10525       // The operands aren't the same for thumb1 (no rotate operand).
10526       MCInst TmpInst;
10527       TmpInst.setOpcode(NewOpc);
10528       TmpInst.addOperand(Inst.getOperand(0));
10529       TmpInst.addOperand(Inst.getOperand(1));
10530       TmpInst.addOperand(Inst.getOperand(3));
10531       TmpInst.addOperand(Inst.getOperand(4));
10532       Inst = TmpInst;
10533       return true;
10534     }
10535     break;
10536 
10537   case ARM::MOVsi: {
10538     ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(2).getImm());
10539     // rrx shifts and asr/lsr of #32 is encoded as 0
10540     if (SOpc == ARM_AM::rrx || SOpc == ARM_AM::asr || SOpc == ARM_AM::lsr)
10541       return false;
10542     if (ARM_AM::getSORegOffset(Inst.getOperand(2).getImm()) == 0) {
10543       // Shifting by zero is accepted as a vanilla 'MOVr'
10544       MCInst TmpInst;
10545       TmpInst.setOpcode(ARM::MOVr);
10546       TmpInst.addOperand(Inst.getOperand(0));
10547       TmpInst.addOperand(Inst.getOperand(1));
10548       TmpInst.addOperand(Inst.getOperand(3));
10549       TmpInst.addOperand(Inst.getOperand(4));
10550       TmpInst.addOperand(Inst.getOperand(5));
10551       Inst = TmpInst;
10552       return true;
10553     }
10554     return false;
10555   }
10556   case ARM::ANDrsi:
10557   case ARM::ORRrsi:
10558   case ARM::EORrsi:
10559   case ARM::BICrsi:
10560   case ARM::SUBrsi:
10561   case ARM::ADDrsi: {
10562     unsigned newOpc;
10563     ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(3).getImm());
10564     if (SOpc == ARM_AM::rrx) return false;
10565     switch (Inst.getOpcode()) {
10566     default: llvm_unreachable("unexpected opcode!");
10567     case ARM::ANDrsi: newOpc = ARM::ANDrr; break;
10568     case ARM::ORRrsi: newOpc = ARM::ORRrr; break;
10569     case ARM::EORrsi: newOpc = ARM::EORrr; break;
10570     case ARM::BICrsi: newOpc = ARM::BICrr; break;
10571     case ARM::SUBrsi: newOpc = ARM::SUBrr; break;
10572     case ARM::ADDrsi: newOpc = ARM::ADDrr; break;
10573     }
10574     // If the shift is by zero, use the non-shifted instruction definition.
10575     // The exception is for right shifts, where 0 == 32
10576     if (ARM_AM::getSORegOffset(Inst.getOperand(3).getImm()) == 0 &&
10577         !(SOpc == ARM_AM::lsr || SOpc == ARM_AM::asr)) {
10578       MCInst TmpInst;
10579       TmpInst.setOpcode(newOpc);
10580       TmpInst.addOperand(Inst.getOperand(0));
10581       TmpInst.addOperand(Inst.getOperand(1));
10582       TmpInst.addOperand(Inst.getOperand(2));
10583       TmpInst.addOperand(Inst.getOperand(4));
10584       TmpInst.addOperand(Inst.getOperand(5));
10585       TmpInst.addOperand(Inst.getOperand(6));
10586       Inst = TmpInst;
10587       return true;
10588     }
10589     return false;
10590   }
10591   case ARM::ITasm:
10592   case ARM::t2IT: {
10593     // Set up the IT block state according to the IT instruction we just
10594     // matched.
10595     assert(!inITBlock() && "nested IT blocks?!");
10596     startExplicitITBlock(ARMCC::CondCodes(Inst.getOperand(0).getImm()),
10597                          Inst.getOperand(1).getImm());
10598     break;
10599   }
10600   case ARM::t2LSLrr:
10601   case ARM::t2LSRrr:
10602   case ARM::t2ASRrr:
10603   case ARM::t2SBCrr:
10604   case ARM::t2RORrr:
10605   case ARM::t2BICrr:
10606     // Assemblers should use the narrow encodings of these instructions when permissible.
10607     if ((isARMLowRegister(Inst.getOperand(1).getReg()) &&
10608          isARMLowRegister(Inst.getOperand(2).getReg())) &&
10609         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
10610         Inst.getOperand(5).getReg() == (inITBlock() ? 0 : ARM::CPSR) &&
10611         !HasWideQualifier) {
10612       unsigned NewOpc;
10613       switch (Inst.getOpcode()) {
10614         default: llvm_unreachable("unexpected opcode");
10615         case ARM::t2LSLrr: NewOpc = ARM::tLSLrr; break;
10616         case ARM::t2LSRrr: NewOpc = ARM::tLSRrr; break;
10617         case ARM::t2ASRrr: NewOpc = ARM::tASRrr; break;
10618         case ARM::t2SBCrr: NewOpc = ARM::tSBC; break;
10619         case ARM::t2RORrr: NewOpc = ARM::tROR; break;
10620         case ARM::t2BICrr: NewOpc = ARM::tBIC; break;
10621       }
10622       MCInst TmpInst;
10623       TmpInst.setOpcode(NewOpc);
10624       TmpInst.addOperand(Inst.getOperand(0));
10625       TmpInst.addOperand(Inst.getOperand(5));
10626       TmpInst.addOperand(Inst.getOperand(1));
10627       TmpInst.addOperand(Inst.getOperand(2));
10628       TmpInst.addOperand(Inst.getOperand(3));
10629       TmpInst.addOperand(Inst.getOperand(4));
10630       Inst = TmpInst;
10631       return true;
10632     }
10633     return false;
10634 
10635   case ARM::t2ANDrr:
10636   case ARM::t2EORrr:
10637   case ARM::t2ADCrr:
10638   case ARM::t2ORRrr:
10639     // Assemblers should use the narrow encodings of these instructions when permissible.
10640     // These instructions are special in that they are commutable, so shorter encodings
10641     // are available more often.
10642     if ((isARMLowRegister(Inst.getOperand(1).getReg()) &&
10643          isARMLowRegister(Inst.getOperand(2).getReg())) &&
10644         (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() ||
10645          Inst.getOperand(0).getReg() == Inst.getOperand(2).getReg()) &&
10646         Inst.getOperand(5).getReg() == (inITBlock() ? 0 : ARM::CPSR) &&
10647         !HasWideQualifier) {
10648       unsigned NewOpc;
10649       switch (Inst.getOpcode()) {
10650         default: llvm_unreachable("unexpected opcode");
10651         case ARM::t2ADCrr: NewOpc = ARM::tADC; break;
10652         case ARM::t2ANDrr: NewOpc = ARM::tAND; break;
10653         case ARM::t2EORrr: NewOpc = ARM::tEOR; break;
10654         case ARM::t2ORRrr: NewOpc = ARM::tORR; break;
10655       }
10656       MCInst TmpInst;
10657       TmpInst.setOpcode(NewOpc);
10658       TmpInst.addOperand(Inst.getOperand(0));
10659       TmpInst.addOperand(Inst.getOperand(5));
10660       if (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()) {
10661         TmpInst.addOperand(Inst.getOperand(1));
10662         TmpInst.addOperand(Inst.getOperand(2));
10663       } else {
10664         TmpInst.addOperand(Inst.getOperand(2));
10665         TmpInst.addOperand(Inst.getOperand(1));
10666       }
10667       TmpInst.addOperand(Inst.getOperand(3));
10668       TmpInst.addOperand(Inst.getOperand(4));
10669       Inst = TmpInst;
10670       return true;
10671     }
10672     return false;
10673   case ARM::MVE_VPST:
10674   case ARM::MVE_VPTv16i8:
10675   case ARM::MVE_VPTv8i16:
10676   case ARM::MVE_VPTv4i32:
10677   case ARM::MVE_VPTv16u8:
10678   case ARM::MVE_VPTv8u16:
10679   case ARM::MVE_VPTv4u32:
10680   case ARM::MVE_VPTv16s8:
10681   case ARM::MVE_VPTv8s16:
10682   case ARM::MVE_VPTv4s32:
10683   case ARM::MVE_VPTv4f32:
10684   case ARM::MVE_VPTv8f16:
10685   case ARM::MVE_VPTv16i8r:
10686   case ARM::MVE_VPTv8i16r:
10687   case ARM::MVE_VPTv4i32r:
10688   case ARM::MVE_VPTv16u8r:
10689   case ARM::MVE_VPTv8u16r:
10690   case ARM::MVE_VPTv4u32r:
10691   case ARM::MVE_VPTv16s8r:
10692   case ARM::MVE_VPTv8s16r:
10693   case ARM::MVE_VPTv4s32r:
10694   case ARM::MVE_VPTv4f32r:
10695   case ARM::MVE_VPTv8f16r: {
10696     assert(!inVPTBlock() && "Nested VPT blocks are not allowed");
10697     MCOperand &MO = Inst.getOperand(0);
10698     VPTState.Mask = MO.getImm();
10699     VPTState.CurPosition = 0;
10700     break;
10701   }
10702   }
10703   return false;
10704 }
10705 
10706 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
10707   // 16-bit thumb arithmetic instructions either require or preclude the 'S'
10708   // suffix depending on whether they're in an IT block or not.
10709   unsigned Opc = Inst.getOpcode();
10710   const MCInstrDesc &MCID = MII.get(Opc);
10711   if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
10712     assert(MCID.hasOptionalDef() &&
10713            "optionally flag setting instruction missing optional def operand");
10714     assert(MCID.NumOperands == Inst.getNumOperands() &&
10715            "operand count mismatch!");
10716     // Find the optional-def operand (cc_out).
10717     unsigned OpNo;
10718     for (OpNo = 0;
10719          !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
10720          ++OpNo)
10721       ;
10722     // If we're parsing Thumb1, reject it completely.
10723     if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
10724       return Match_RequiresFlagSetting;
10725     // If we're parsing Thumb2, which form is legal depends on whether we're
10726     // in an IT block.
10727     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
10728         !inITBlock())
10729       return Match_RequiresITBlock;
10730     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
10731         inITBlock())
10732       return Match_RequiresNotITBlock;
10733     // LSL with zero immediate is not allowed in an IT block
10734     if (Opc == ARM::tLSLri && Inst.getOperand(3).getImm() == 0 && inITBlock())
10735       return Match_RequiresNotITBlock;
10736   } else if (isThumbOne()) {
10737     // Some high-register supporting Thumb1 encodings only allow both registers
10738     // to be from r0-r7 when in Thumb2.
10739     if (Opc == ARM::tADDhirr && !hasV6MOps() &&
10740         isARMLowRegister(Inst.getOperand(1).getReg()) &&
10741         isARMLowRegister(Inst.getOperand(2).getReg()))
10742       return Match_RequiresThumb2;
10743     // Others only require ARMv6 or later.
10744     else if (Opc == ARM::tMOVr && !hasV6Ops() &&
10745              isARMLowRegister(Inst.getOperand(0).getReg()) &&
10746              isARMLowRegister(Inst.getOperand(1).getReg()))
10747       return Match_RequiresV6;
10748   }
10749 
10750   // Before ARMv8 the rules for when SP is allowed in t2MOVr are more complex
10751   // than the loop below can handle, so it uses the GPRnopc register class and
10752   // we do SP handling here.
10753   if (Opc == ARM::t2MOVr && !hasV8Ops())
10754   {
10755     // SP as both source and destination is not allowed
10756     if (Inst.getOperand(0).getReg() == ARM::SP &&
10757         Inst.getOperand(1).getReg() == ARM::SP)
10758       return Match_RequiresV8;
10759     // When flags-setting SP as either source or destination is not allowed
10760     if (Inst.getOperand(4).getReg() == ARM::CPSR &&
10761         (Inst.getOperand(0).getReg() == ARM::SP ||
10762          Inst.getOperand(1).getReg() == ARM::SP))
10763       return Match_RequiresV8;
10764   }
10765 
10766   switch (Inst.getOpcode()) {
10767   case ARM::VMRS:
10768   case ARM::VMSR:
10769   case ARM::VMRS_FPCXTS:
10770   case ARM::VMRS_FPCXTNS:
10771   case ARM::VMSR_FPCXTS:
10772   case ARM::VMSR_FPCXTNS:
10773   case ARM::VMRS_FPSCR_NZCVQC:
10774   case ARM::VMSR_FPSCR_NZCVQC:
10775   case ARM::FMSTAT:
10776   case ARM::VMRS_VPR:
10777   case ARM::VMRS_P0:
10778   case ARM::VMSR_VPR:
10779   case ARM::VMSR_P0:
10780     // Use of SP for VMRS/VMSR is only allowed in ARM mode with the exception of
10781     // ARMv8-A.
10782     if (Inst.getOperand(0).isReg() && Inst.getOperand(0).getReg() == ARM::SP &&
10783         (isThumb() && !hasV8Ops()))
10784       return Match_InvalidOperand;
10785     break;
10786   case ARM::t2TBB:
10787   case ARM::t2TBH:
10788     // Rn = sp is only allowed with ARMv8-A
10789     if (!hasV8Ops() && (Inst.getOperand(0).getReg() == ARM::SP))
10790       return Match_RequiresV8;
10791     break;
10792   default:
10793     break;
10794   }
10795 
10796   for (unsigned I = 0; I < MCID.NumOperands; ++I)
10797     if (MCID.OpInfo[I].RegClass == ARM::rGPRRegClassID) {
10798       // rGPRRegClass excludes PC, and also excluded SP before ARMv8
10799       const auto &Op = Inst.getOperand(I);
10800       if (!Op.isReg()) {
10801         // This can happen in awkward cases with tied operands, e.g. a
10802         // writeback load/store with a complex addressing mode in
10803         // which there's an output operand corresponding to the
10804         // updated written-back base register: the Tablegen-generated
10805         // AsmMatcher will have written a placeholder operand to that
10806         // slot in the form of an immediate 0, because it can't
10807         // generate the register part of the complex addressing-mode
10808         // operand ahead of time.
10809         continue;
10810       }
10811 
10812       unsigned Reg = Op.getReg();
10813       if ((Reg == ARM::SP) && !hasV8Ops())
10814         return Match_RequiresV8;
10815       else if (Reg == ARM::PC)
10816         return Match_InvalidOperand;
10817     }
10818 
10819   return Match_Success;
10820 }
10821 
10822 namespace llvm {
10823 
10824 template <> inline bool IsCPSRDead<MCInst>(const MCInst *Instr) {
10825   return true; // In an assembly source, no need to second-guess
10826 }
10827 
10828 } // end namespace llvm
10829 
10830 // Returns true if Inst is unpredictable if it is in and IT block, but is not
10831 // the last instruction in the block.
10832 bool ARMAsmParser::isITBlockTerminator(MCInst &Inst) const {
10833   const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
10834 
10835   // All branch & call instructions terminate IT blocks with the exception of
10836   // SVC.
10837   if (MCID.isTerminator() || (MCID.isCall() && Inst.getOpcode() != ARM::tSVC) ||
10838       MCID.isReturn() || MCID.isBranch() || MCID.isIndirectBranch())
10839     return true;
10840 
10841   // Any arithmetic instruction which writes to the PC also terminates the IT
10842   // block.
10843   if (MCID.hasDefOfPhysReg(Inst, ARM::PC, *MRI))
10844     return true;
10845 
10846   return false;
10847 }
10848 
10849 unsigned ARMAsmParser::MatchInstruction(OperandVector &Operands, MCInst &Inst,
10850                                           SmallVectorImpl<NearMissInfo> &NearMisses,
10851                                           bool MatchingInlineAsm,
10852                                           bool &EmitInITBlock,
10853                                           MCStreamer &Out) {
10854   // If we can't use an implicit IT block here, just match as normal.
10855   if (inExplicitITBlock() || !isThumbTwo() || !useImplicitITThumb())
10856     return MatchInstructionImpl(Operands, Inst, &NearMisses, MatchingInlineAsm);
10857 
10858   // Try to match the instruction in an extension of the current IT block (if
10859   // there is one).
10860   if (inImplicitITBlock()) {
10861     extendImplicitITBlock(ITState.Cond);
10862     if (MatchInstructionImpl(Operands, Inst, nullptr, MatchingInlineAsm) ==
10863             Match_Success) {
10864       // The match succeded, but we still have to check that the instruction is
10865       // valid in this implicit IT block.
10866       const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
10867       if (MCID.isPredicable()) {
10868         ARMCC::CondCodes InstCond =
10869             (ARMCC::CondCodes)Inst.getOperand(MCID.findFirstPredOperandIdx())
10870                 .getImm();
10871         ARMCC::CondCodes ITCond = currentITCond();
10872         if (InstCond == ITCond) {
10873           EmitInITBlock = true;
10874           return Match_Success;
10875         } else if (InstCond == ARMCC::getOppositeCondition(ITCond)) {
10876           invertCurrentITCondition();
10877           EmitInITBlock = true;
10878           return Match_Success;
10879         }
10880       }
10881     }
10882     rewindImplicitITPosition();
10883   }
10884 
10885   // Finish the current IT block, and try to match outside any IT block.
10886   flushPendingInstructions(Out);
10887   unsigned PlainMatchResult =
10888       MatchInstructionImpl(Operands, Inst, &NearMisses, MatchingInlineAsm);
10889   if (PlainMatchResult == Match_Success) {
10890     const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
10891     if (MCID.isPredicable()) {
10892       ARMCC::CondCodes InstCond =
10893           (ARMCC::CondCodes)Inst.getOperand(MCID.findFirstPredOperandIdx())
10894               .getImm();
10895       // Some forms of the branch instruction have their own condition code
10896       // fields, so can be conditionally executed without an IT block.
10897       if (Inst.getOpcode() == ARM::tBcc || Inst.getOpcode() == ARM::t2Bcc) {
10898         EmitInITBlock = false;
10899         return Match_Success;
10900       }
10901       if (InstCond == ARMCC::AL) {
10902         EmitInITBlock = false;
10903         return Match_Success;
10904       }
10905     } else {
10906       EmitInITBlock = false;
10907       return Match_Success;
10908     }
10909   }
10910 
10911   // Try to match in a new IT block. The matcher doesn't check the actual
10912   // condition, so we create an IT block with a dummy condition, and fix it up
10913   // once we know the actual condition.
10914   startImplicitITBlock();
10915   if (MatchInstructionImpl(Operands, Inst, nullptr, MatchingInlineAsm) ==
10916       Match_Success) {
10917     const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
10918     if (MCID.isPredicable()) {
10919       ITState.Cond =
10920           (ARMCC::CondCodes)Inst.getOperand(MCID.findFirstPredOperandIdx())
10921               .getImm();
10922       EmitInITBlock = true;
10923       return Match_Success;
10924     }
10925   }
10926   discardImplicitITBlock();
10927 
10928   // If none of these succeed, return the error we got when trying to match
10929   // outside any IT blocks.
10930   EmitInITBlock = false;
10931   return PlainMatchResult;
10932 }
10933 
10934 static std::string ARMMnemonicSpellCheck(StringRef S, const FeatureBitset &FBS,
10935                                          unsigned VariantID = 0);
10936 
10937 static const char *getSubtargetFeatureName(uint64_t Val);
10938 bool ARMAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
10939                                            OperandVector &Operands,
10940                                            MCStreamer &Out, uint64_t &ErrorInfo,
10941                                            bool MatchingInlineAsm) {
10942   MCInst Inst;
10943   unsigned MatchResult;
10944   bool PendConditionalInstruction = false;
10945 
10946   SmallVector<NearMissInfo, 4> NearMisses;
10947   MatchResult = MatchInstruction(Operands, Inst, NearMisses, MatchingInlineAsm,
10948                                  PendConditionalInstruction, Out);
10949 
10950   switch (MatchResult) {
10951   case Match_Success:
10952     LLVM_DEBUG(dbgs() << "Parsed as: ";
10953                Inst.dump_pretty(dbgs(), MII.getName(Inst.getOpcode()));
10954                dbgs() << "\n");
10955 
10956     // Context sensitive operand constraints aren't handled by the matcher,
10957     // so check them here.
10958     if (validateInstruction(Inst, Operands)) {
10959       // Still progress the IT block, otherwise one wrong condition causes
10960       // nasty cascading errors.
10961       forwardITPosition();
10962       forwardVPTPosition();
10963       return true;
10964     }
10965 
10966     { // processInstruction() updates inITBlock state, we need to save it away
10967       bool wasInITBlock = inITBlock();
10968 
10969       // Some instructions need post-processing to, for example, tweak which
10970       // encoding is selected. Loop on it while changes happen so the
10971       // individual transformations can chain off each other. E.g.,
10972       // tPOP(r8)->t2LDMIA_UPD(sp,r8)->t2STR_POST(sp,r8)
10973       while (processInstruction(Inst, Operands, Out))
10974         LLVM_DEBUG(dbgs() << "Changed to: ";
10975                    Inst.dump_pretty(dbgs(), MII.getName(Inst.getOpcode()));
10976                    dbgs() << "\n");
10977 
10978       // Only after the instruction is fully processed, we can validate it
10979       if (wasInITBlock && hasV8Ops() && isThumb() &&
10980           !isV8EligibleForIT(&Inst) && !getTargetOptions().MCNoDeprecatedWarn) {
10981         Warning(IDLoc, "deprecated instruction in IT block");
10982       }
10983     }
10984 
10985     // Only move forward at the very end so that everything in validate
10986     // and process gets a consistent answer about whether we're in an IT
10987     // block.
10988     forwardITPosition();
10989     forwardVPTPosition();
10990 
10991     // ITasm is an ARM mode pseudo-instruction that just sets the ITblock and
10992     // doesn't actually encode.
10993     if (Inst.getOpcode() == ARM::ITasm)
10994       return false;
10995 
10996     Inst.setLoc(IDLoc);
10997     if (PendConditionalInstruction) {
10998       PendingConditionalInsts.push_back(Inst);
10999       if (isITBlockFull() || isITBlockTerminator(Inst))
11000         flushPendingInstructions(Out);
11001     } else {
11002       Out.emitInstruction(Inst, getSTI());
11003     }
11004     return false;
11005   case Match_NearMisses:
11006     ReportNearMisses(NearMisses, IDLoc, Operands);
11007     return true;
11008   case Match_MnemonicFail: {
11009     FeatureBitset FBS = ComputeAvailableFeatures(getSTI().getFeatureBits());
11010     std::string Suggestion = ARMMnemonicSpellCheck(
11011       ((ARMOperand &)*Operands[0]).getToken(), FBS);
11012     return Error(IDLoc, "invalid instruction" + Suggestion,
11013                  ((ARMOperand &)*Operands[0]).getLocRange());
11014   }
11015   }
11016 
11017   llvm_unreachable("Implement any new match types added!");
11018 }
11019 
11020 /// parseDirective parses the arm specific directives
11021 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
11022   const MCContext::Environment Format = getContext().getObjectFileType();
11023   bool IsMachO = Format == MCContext::IsMachO;
11024   bool IsCOFF = Format == MCContext::IsCOFF;
11025 
11026   std::string IDVal = DirectiveID.getIdentifier().lower();
11027   if (IDVal == ".word")
11028     parseLiteralValues(4, DirectiveID.getLoc());
11029   else if (IDVal == ".short" || IDVal == ".hword")
11030     parseLiteralValues(2, DirectiveID.getLoc());
11031   else if (IDVal == ".thumb")
11032     parseDirectiveThumb(DirectiveID.getLoc());
11033   else if (IDVal == ".arm")
11034     parseDirectiveARM(DirectiveID.getLoc());
11035   else if (IDVal == ".thumb_func")
11036     parseDirectiveThumbFunc(DirectiveID.getLoc());
11037   else if (IDVal == ".code")
11038     parseDirectiveCode(DirectiveID.getLoc());
11039   else if (IDVal == ".syntax")
11040     parseDirectiveSyntax(DirectiveID.getLoc());
11041   else if (IDVal == ".unreq")
11042     parseDirectiveUnreq(DirectiveID.getLoc());
11043   else if (IDVal == ".fnend")
11044     parseDirectiveFnEnd(DirectiveID.getLoc());
11045   else if (IDVal == ".cantunwind")
11046     parseDirectiveCantUnwind(DirectiveID.getLoc());
11047   else if (IDVal == ".personality")
11048     parseDirectivePersonality(DirectiveID.getLoc());
11049   else if (IDVal == ".handlerdata")
11050     parseDirectiveHandlerData(DirectiveID.getLoc());
11051   else if (IDVal == ".setfp")
11052     parseDirectiveSetFP(DirectiveID.getLoc());
11053   else if (IDVal == ".pad")
11054     parseDirectivePad(DirectiveID.getLoc());
11055   else if (IDVal == ".save")
11056     parseDirectiveRegSave(DirectiveID.getLoc(), false);
11057   else if (IDVal == ".vsave")
11058     parseDirectiveRegSave(DirectiveID.getLoc(), true);
11059   else if (IDVal == ".ltorg" || IDVal == ".pool")
11060     parseDirectiveLtorg(DirectiveID.getLoc());
11061   else if (IDVal == ".even")
11062     parseDirectiveEven(DirectiveID.getLoc());
11063   else if (IDVal == ".personalityindex")
11064     parseDirectivePersonalityIndex(DirectiveID.getLoc());
11065   else if (IDVal == ".unwind_raw")
11066     parseDirectiveUnwindRaw(DirectiveID.getLoc());
11067   else if (IDVal == ".movsp")
11068     parseDirectiveMovSP(DirectiveID.getLoc());
11069   else if (IDVal == ".arch_extension")
11070     parseDirectiveArchExtension(DirectiveID.getLoc());
11071   else if (IDVal == ".align")
11072     return parseDirectiveAlign(DirectiveID.getLoc()); // Use Generic on failure.
11073   else if (IDVal == ".thumb_set")
11074     parseDirectiveThumbSet(DirectiveID.getLoc());
11075   else if (IDVal == ".inst")
11076     parseDirectiveInst(DirectiveID.getLoc());
11077   else if (IDVal == ".inst.n")
11078     parseDirectiveInst(DirectiveID.getLoc(), 'n');
11079   else if (IDVal == ".inst.w")
11080     parseDirectiveInst(DirectiveID.getLoc(), 'w');
11081   else if (!IsMachO && !IsCOFF) {
11082     if (IDVal == ".arch")
11083       parseDirectiveArch(DirectiveID.getLoc());
11084     else if (IDVal == ".cpu")
11085       parseDirectiveCPU(DirectiveID.getLoc());
11086     else if (IDVal == ".eabi_attribute")
11087       parseDirectiveEabiAttr(DirectiveID.getLoc());
11088     else if (IDVal == ".fpu")
11089       parseDirectiveFPU(DirectiveID.getLoc());
11090     else if (IDVal == ".fnstart")
11091       parseDirectiveFnStart(DirectiveID.getLoc());
11092     else if (IDVal == ".object_arch")
11093       parseDirectiveObjectArch(DirectiveID.getLoc());
11094     else if (IDVal == ".tlsdescseq")
11095       parseDirectiveTLSDescSeq(DirectiveID.getLoc());
11096     else
11097       return true;
11098   } else
11099     return true;
11100   return false;
11101 }
11102 
11103 /// parseLiteralValues
11104 ///  ::= .hword expression [, expression]*
11105 ///  ::= .short expression [, expression]*
11106 ///  ::= .word expression [, expression]*
11107 bool ARMAsmParser::parseLiteralValues(unsigned Size, SMLoc L) {
11108   auto parseOne = [&]() -> bool {
11109     const MCExpr *Value;
11110     if (getParser().parseExpression(Value))
11111       return true;
11112     getParser().getStreamer().emitValue(Value, Size, L);
11113     return false;
11114   };
11115   return (parseMany(parseOne));
11116 }
11117 
11118 /// parseDirectiveThumb
11119 ///  ::= .thumb
11120 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
11121   if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive") ||
11122       check(!hasThumb(), L, "target does not support Thumb mode"))
11123     return true;
11124 
11125   if (!isThumb())
11126     SwitchMode();
11127 
11128   getParser().getStreamer().emitAssemblerFlag(MCAF_Code16);
11129   return false;
11130 }
11131 
11132 /// parseDirectiveARM
11133 ///  ::= .arm
11134 bool ARMAsmParser::parseDirectiveARM(SMLoc L) {
11135   if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive") ||
11136       check(!hasARM(), L, "target does not support ARM mode"))
11137     return true;
11138 
11139   if (isThumb())
11140     SwitchMode();
11141   getParser().getStreamer().emitAssemblerFlag(MCAF_Code32);
11142   return false;
11143 }
11144 
11145 void ARMAsmParser::doBeforeLabelEmit(MCSymbol *Symbol) {
11146   // We need to flush the current implicit IT block on a label, because it is
11147   // not legal to branch into an IT block.
11148   flushPendingInstructions(getStreamer());
11149 }
11150 
11151 void ARMAsmParser::onLabelParsed(MCSymbol *Symbol) {
11152   if (NextSymbolIsThumb) {
11153     getParser().getStreamer().emitThumbFunc(Symbol);
11154     NextSymbolIsThumb = false;
11155   }
11156 }
11157 
11158 /// parseDirectiveThumbFunc
11159 ///  ::= .thumbfunc symbol_name
11160 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
11161   MCAsmParser &Parser = getParser();
11162   const auto Format = getContext().getObjectFileType();
11163   bool IsMachO = Format == MCContext::IsMachO;
11164 
11165   // Darwin asm has (optionally) function name after .thumb_func direction
11166   // ELF doesn't
11167 
11168   if (IsMachO) {
11169     if (Parser.getTok().is(AsmToken::Identifier) ||
11170         Parser.getTok().is(AsmToken::String)) {
11171       MCSymbol *Func = getParser().getContext().getOrCreateSymbol(
11172           Parser.getTok().getIdentifier());
11173       getParser().getStreamer().emitThumbFunc(Func);
11174       Parser.Lex();
11175       if (parseToken(AsmToken::EndOfStatement,
11176                      "unexpected token in '.thumb_func' directive"))
11177         return true;
11178       return false;
11179     }
11180   }
11181 
11182   if (parseToken(AsmToken::EndOfStatement,
11183                  "unexpected token in '.thumb_func' directive"))
11184     return true;
11185 
11186   // .thumb_func implies .thumb
11187   if (!isThumb())
11188     SwitchMode();
11189 
11190   getParser().getStreamer().emitAssemblerFlag(MCAF_Code16);
11191 
11192   NextSymbolIsThumb = true;
11193   return false;
11194 }
11195 
11196 /// parseDirectiveSyntax
11197 ///  ::= .syntax unified | divided
11198 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
11199   MCAsmParser &Parser = getParser();
11200   const AsmToken &Tok = Parser.getTok();
11201   if (Tok.isNot(AsmToken::Identifier)) {
11202     Error(L, "unexpected token in .syntax directive");
11203     return false;
11204   }
11205 
11206   StringRef Mode = Tok.getString();
11207   Parser.Lex();
11208   if (check(Mode == "divided" || Mode == "DIVIDED", L,
11209             "'.syntax divided' arm assembly not supported") ||
11210       check(Mode != "unified" && Mode != "UNIFIED", L,
11211             "unrecognized syntax mode in .syntax directive") ||
11212       parseToken(AsmToken::EndOfStatement, "unexpected token in directive"))
11213     return true;
11214 
11215   // TODO tell the MC streamer the mode
11216   // getParser().getStreamer().Emit???();
11217   return false;
11218 }
11219 
11220 /// parseDirectiveCode
11221 ///  ::= .code 16 | 32
11222 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
11223   MCAsmParser &Parser = getParser();
11224   const AsmToken &Tok = Parser.getTok();
11225   if (Tok.isNot(AsmToken::Integer))
11226     return Error(L, "unexpected token in .code directive");
11227   int64_t Val = Parser.getTok().getIntVal();
11228   if (Val != 16 && Val != 32) {
11229     Error(L, "invalid operand to .code directive");
11230     return false;
11231   }
11232   Parser.Lex();
11233 
11234   if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive"))
11235     return true;
11236 
11237   if (Val == 16) {
11238     if (!hasThumb())
11239       return Error(L, "target does not support Thumb mode");
11240 
11241     if (!isThumb())
11242       SwitchMode();
11243     getParser().getStreamer().emitAssemblerFlag(MCAF_Code16);
11244   } else {
11245     if (!hasARM())
11246       return Error(L, "target does not support ARM mode");
11247 
11248     if (isThumb())
11249       SwitchMode();
11250     getParser().getStreamer().emitAssemblerFlag(MCAF_Code32);
11251   }
11252 
11253   return false;
11254 }
11255 
11256 /// parseDirectiveReq
11257 ///  ::= name .req registername
11258 bool ARMAsmParser::parseDirectiveReq(StringRef Name, SMLoc L) {
11259   MCAsmParser &Parser = getParser();
11260   Parser.Lex(); // Eat the '.req' token.
11261   unsigned Reg;
11262   SMLoc SRegLoc, ERegLoc;
11263   if (check(ParseRegister(Reg, SRegLoc, ERegLoc), SRegLoc,
11264             "register name expected") ||
11265       parseToken(AsmToken::EndOfStatement,
11266                  "unexpected input in .req directive."))
11267     return true;
11268 
11269   if (RegisterReqs.insert(std::make_pair(Name, Reg)).first->second != Reg)
11270     return Error(SRegLoc,
11271                  "redefinition of '" + Name + "' does not match original.");
11272 
11273   return false;
11274 }
11275 
11276 /// parseDirectiveUneq
11277 ///  ::= .unreq registername
11278 bool ARMAsmParser::parseDirectiveUnreq(SMLoc L) {
11279   MCAsmParser &Parser = getParser();
11280   if (Parser.getTok().isNot(AsmToken::Identifier))
11281     return Error(L, "unexpected input in .unreq directive.");
11282   RegisterReqs.erase(Parser.getTok().getIdentifier().lower());
11283   Parser.Lex(); // Eat the identifier.
11284   if (parseToken(AsmToken::EndOfStatement,
11285                  "unexpected input in '.unreq' directive"))
11286     return true;
11287   return false;
11288 }
11289 
11290 // After changing arch/CPU, try to put the ARM/Thumb mode back to what it was
11291 // before, if supported by the new target, or emit mapping symbols for the mode
11292 // switch.
11293 void ARMAsmParser::FixModeAfterArchChange(bool WasThumb, SMLoc Loc) {
11294   if (WasThumb != isThumb()) {
11295     if (WasThumb && hasThumb()) {
11296       // Stay in Thumb mode
11297       SwitchMode();
11298     } else if (!WasThumb && hasARM()) {
11299       // Stay in ARM mode
11300       SwitchMode();
11301     } else {
11302       // Mode switch forced, because the new arch doesn't support the old mode.
11303       getParser().getStreamer().emitAssemblerFlag(isThumb() ? MCAF_Code16
11304                                                             : MCAF_Code32);
11305       // Warn about the implcit mode switch. GAS does not switch modes here,
11306       // but instead stays in the old mode, reporting an error on any following
11307       // instructions as the mode does not exist on the target.
11308       Warning(Loc, Twine("new target does not support ") +
11309                        (WasThumb ? "thumb" : "arm") + " mode, switching to " +
11310                        (!WasThumb ? "thumb" : "arm") + " mode");
11311     }
11312   }
11313 }
11314 
11315 /// parseDirectiveArch
11316 ///  ::= .arch token
11317 bool ARMAsmParser::parseDirectiveArch(SMLoc L) {
11318   StringRef Arch = getParser().parseStringToEndOfStatement().trim();
11319   ARM::ArchKind ID = ARM::parseArch(Arch);
11320 
11321   if (ID == ARM::ArchKind::INVALID)
11322     return Error(L, "Unknown arch name");
11323 
11324   bool WasThumb = isThumb();
11325   Triple T;
11326   MCSubtargetInfo &STI = copySTI();
11327   STI.setDefaultFeatures("", /*TuneCPU*/ "",
11328                          ("+" + ARM::getArchName(ID)).str());
11329   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
11330   FixModeAfterArchChange(WasThumb, L);
11331 
11332   getTargetStreamer().emitArch(ID);
11333   return false;
11334 }
11335 
11336 /// parseDirectiveEabiAttr
11337 ///  ::= .eabi_attribute int, int [, "str"]
11338 ///  ::= .eabi_attribute Tag_name, int [, "str"]
11339 bool ARMAsmParser::parseDirectiveEabiAttr(SMLoc L) {
11340   MCAsmParser &Parser = getParser();
11341   int64_t Tag;
11342   SMLoc TagLoc;
11343   TagLoc = Parser.getTok().getLoc();
11344   if (Parser.getTok().is(AsmToken::Identifier)) {
11345     StringRef Name = Parser.getTok().getIdentifier();
11346     Optional<unsigned> Ret = ELFAttrs::attrTypeFromString(
11347         Name, ARMBuildAttrs::getARMAttributeTags());
11348     if (!Ret.hasValue()) {
11349       Error(TagLoc, "attribute name not recognised: " + Name);
11350       return false;
11351     }
11352     Tag = Ret.getValue();
11353     Parser.Lex();
11354   } else {
11355     const MCExpr *AttrExpr;
11356 
11357     TagLoc = Parser.getTok().getLoc();
11358     if (Parser.parseExpression(AttrExpr))
11359       return true;
11360 
11361     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(AttrExpr);
11362     if (check(!CE, TagLoc, "expected numeric constant"))
11363       return true;
11364 
11365     Tag = CE->getValue();
11366   }
11367 
11368   if (Parser.parseToken(AsmToken::Comma, "comma expected"))
11369     return true;
11370 
11371   StringRef StringValue = "";
11372   bool IsStringValue = false;
11373 
11374   int64_t IntegerValue = 0;
11375   bool IsIntegerValue = false;
11376 
11377   if (Tag == ARMBuildAttrs::CPU_raw_name || Tag == ARMBuildAttrs::CPU_name)
11378     IsStringValue = true;
11379   else if (Tag == ARMBuildAttrs::compatibility) {
11380     IsStringValue = true;
11381     IsIntegerValue = true;
11382   } else if (Tag < 32 || Tag % 2 == 0)
11383     IsIntegerValue = true;
11384   else if (Tag % 2 == 1)
11385     IsStringValue = true;
11386   else
11387     llvm_unreachable("invalid tag type");
11388 
11389   if (IsIntegerValue) {
11390     const MCExpr *ValueExpr;
11391     SMLoc ValueExprLoc = Parser.getTok().getLoc();
11392     if (Parser.parseExpression(ValueExpr))
11393       return true;
11394 
11395     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ValueExpr);
11396     if (!CE)
11397       return Error(ValueExprLoc, "expected numeric constant");
11398     IntegerValue = CE->getValue();
11399   }
11400 
11401   if (Tag == ARMBuildAttrs::compatibility) {
11402     if (Parser.parseToken(AsmToken::Comma, "comma expected"))
11403       return true;
11404   }
11405 
11406   if (IsStringValue) {
11407     if (Parser.getTok().isNot(AsmToken::String))
11408       return Error(Parser.getTok().getLoc(), "bad string constant");
11409 
11410     StringValue = Parser.getTok().getStringContents();
11411     Parser.Lex();
11412   }
11413 
11414   if (Parser.parseToken(AsmToken::EndOfStatement,
11415                         "unexpected token in '.eabi_attribute' directive"))
11416     return true;
11417 
11418   if (IsIntegerValue && IsStringValue) {
11419     assert(Tag == ARMBuildAttrs::compatibility);
11420     getTargetStreamer().emitIntTextAttribute(Tag, IntegerValue, StringValue);
11421   } else if (IsIntegerValue)
11422     getTargetStreamer().emitAttribute(Tag, IntegerValue);
11423   else if (IsStringValue)
11424     getTargetStreamer().emitTextAttribute(Tag, StringValue);
11425   return false;
11426 }
11427 
11428 /// parseDirectiveCPU
11429 ///  ::= .cpu str
11430 bool ARMAsmParser::parseDirectiveCPU(SMLoc L) {
11431   StringRef CPU = getParser().parseStringToEndOfStatement().trim();
11432   getTargetStreamer().emitTextAttribute(ARMBuildAttrs::CPU_name, CPU);
11433 
11434   // FIXME: This is using table-gen data, but should be moved to
11435   // ARMTargetParser once that is table-gen'd.
11436   if (!getSTI().isCPUStringValid(CPU))
11437     return Error(L, "Unknown CPU name");
11438 
11439   bool WasThumb = isThumb();
11440   MCSubtargetInfo &STI = copySTI();
11441   STI.setDefaultFeatures(CPU, /*TuneCPU*/ CPU, "");
11442   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
11443   FixModeAfterArchChange(WasThumb, L);
11444 
11445   return false;
11446 }
11447 
11448 /// parseDirectiveFPU
11449 ///  ::= .fpu str
11450 bool ARMAsmParser::parseDirectiveFPU(SMLoc L) {
11451   SMLoc FPUNameLoc = getTok().getLoc();
11452   StringRef FPU = getParser().parseStringToEndOfStatement().trim();
11453 
11454   unsigned ID = ARM::parseFPU(FPU);
11455   std::vector<StringRef> Features;
11456   if (!ARM::getFPUFeatures(ID, Features))
11457     return Error(FPUNameLoc, "Unknown FPU name");
11458 
11459   MCSubtargetInfo &STI = copySTI();
11460   for (auto Feature : Features)
11461     STI.ApplyFeatureFlag(Feature);
11462   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
11463 
11464   getTargetStreamer().emitFPU(ID);
11465   return false;
11466 }
11467 
11468 /// parseDirectiveFnStart
11469 ///  ::= .fnstart
11470 bool ARMAsmParser::parseDirectiveFnStart(SMLoc L) {
11471   if (parseToken(AsmToken::EndOfStatement,
11472                  "unexpected token in '.fnstart' directive"))
11473     return true;
11474 
11475   if (UC.hasFnStart()) {
11476     Error(L, ".fnstart starts before the end of previous one");
11477     UC.emitFnStartLocNotes();
11478     return true;
11479   }
11480 
11481   // Reset the unwind directives parser state
11482   UC.reset();
11483 
11484   getTargetStreamer().emitFnStart();
11485 
11486   UC.recordFnStart(L);
11487   return false;
11488 }
11489 
11490 /// parseDirectiveFnEnd
11491 ///  ::= .fnend
11492 bool ARMAsmParser::parseDirectiveFnEnd(SMLoc L) {
11493   if (parseToken(AsmToken::EndOfStatement,
11494                  "unexpected token in '.fnend' directive"))
11495     return true;
11496   // Check the ordering of unwind directives
11497   if (!UC.hasFnStart())
11498     return Error(L, ".fnstart must precede .fnend directive");
11499 
11500   // Reset the unwind directives parser state
11501   getTargetStreamer().emitFnEnd();
11502 
11503   UC.reset();
11504   return false;
11505 }
11506 
11507 /// parseDirectiveCantUnwind
11508 ///  ::= .cantunwind
11509 bool ARMAsmParser::parseDirectiveCantUnwind(SMLoc L) {
11510   if (parseToken(AsmToken::EndOfStatement,
11511                  "unexpected token in '.cantunwind' directive"))
11512     return true;
11513 
11514   UC.recordCantUnwind(L);
11515   // Check the ordering of unwind directives
11516   if (check(!UC.hasFnStart(), L, ".fnstart must precede .cantunwind directive"))
11517     return true;
11518 
11519   if (UC.hasHandlerData()) {
11520     Error(L, ".cantunwind can't be used with .handlerdata directive");
11521     UC.emitHandlerDataLocNotes();
11522     return true;
11523   }
11524   if (UC.hasPersonality()) {
11525     Error(L, ".cantunwind can't be used with .personality directive");
11526     UC.emitPersonalityLocNotes();
11527     return true;
11528   }
11529 
11530   getTargetStreamer().emitCantUnwind();
11531   return false;
11532 }
11533 
11534 /// parseDirectivePersonality
11535 ///  ::= .personality name
11536 bool ARMAsmParser::parseDirectivePersonality(SMLoc L) {
11537   MCAsmParser &Parser = getParser();
11538   bool HasExistingPersonality = UC.hasPersonality();
11539 
11540   // Parse the name of the personality routine
11541   if (Parser.getTok().isNot(AsmToken::Identifier))
11542     return Error(L, "unexpected input in .personality directive.");
11543   StringRef Name(Parser.getTok().getIdentifier());
11544   Parser.Lex();
11545 
11546   if (parseToken(AsmToken::EndOfStatement,
11547                  "unexpected token in '.personality' directive"))
11548     return true;
11549 
11550   UC.recordPersonality(L);
11551 
11552   // Check the ordering of unwind directives
11553   if (!UC.hasFnStart())
11554     return Error(L, ".fnstart must precede .personality directive");
11555   if (UC.cantUnwind()) {
11556     Error(L, ".personality can't be used with .cantunwind directive");
11557     UC.emitCantUnwindLocNotes();
11558     return true;
11559   }
11560   if (UC.hasHandlerData()) {
11561     Error(L, ".personality must precede .handlerdata directive");
11562     UC.emitHandlerDataLocNotes();
11563     return true;
11564   }
11565   if (HasExistingPersonality) {
11566     Error(L, "multiple personality directives");
11567     UC.emitPersonalityLocNotes();
11568     return true;
11569   }
11570 
11571   MCSymbol *PR = getParser().getContext().getOrCreateSymbol(Name);
11572   getTargetStreamer().emitPersonality(PR);
11573   return false;
11574 }
11575 
11576 /// parseDirectiveHandlerData
11577 ///  ::= .handlerdata
11578 bool ARMAsmParser::parseDirectiveHandlerData(SMLoc L) {
11579   if (parseToken(AsmToken::EndOfStatement,
11580                  "unexpected token in '.handlerdata' directive"))
11581     return true;
11582 
11583   UC.recordHandlerData(L);
11584   // Check the ordering of unwind directives
11585   if (!UC.hasFnStart())
11586     return Error(L, ".fnstart must precede .personality directive");
11587   if (UC.cantUnwind()) {
11588     Error(L, ".handlerdata can't be used with .cantunwind directive");
11589     UC.emitCantUnwindLocNotes();
11590     return true;
11591   }
11592 
11593   getTargetStreamer().emitHandlerData();
11594   return false;
11595 }
11596 
11597 /// parseDirectiveSetFP
11598 ///  ::= .setfp fpreg, spreg [, offset]
11599 bool ARMAsmParser::parseDirectiveSetFP(SMLoc L) {
11600   MCAsmParser &Parser = getParser();
11601   // Check the ordering of unwind directives
11602   if (check(!UC.hasFnStart(), L, ".fnstart must precede .setfp directive") ||
11603       check(UC.hasHandlerData(), L,
11604             ".setfp must precede .handlerdata directive"))
11605     return true;
11606 
11607   // Parse fpreg
11608   SMLoc FPRegLoc = Parser.getTok().getLoc();
11609   int FPReg = tryParseRegister();
11610 
11611   if (check(FPReg == -1, FPRegLoc, "frame pointer register expected") ||
11612       Parser.parseToken(AsmToken::Comma, "comma expected"))
11613     return true;
11614 
11615   // Parse spreg
11616   SMLoc SPRegLoc = Parser.getTok().getLoc();
11617   int SPReg = tryParseRegister();
11618   if (check(SPReg == -1, SPRegLoc, "stack pointer register expected") ||
11619       check(SPReg != ARM::SP && SPReg != UC.getFPReg(), SPRegLoc,
11620             "register should be either $sp or the latest fp register"))
11621     return true;
11622 
11623   // Update the frame pointer register
11624   UC.saveFPReg(FPReg);
11625 
11626   // Parse offset
11627   int64_t Offset = 0;
11628   if (Parser.parseOptionalToken(AsmToken::Comma)) {
11629     if (Parser.getTok().isNot(AsmToken::Hash) &&
11630         Parser.getTok().isNot(AsmToken::Dollar))
11631       return Error(Parser.getTok().getLoc(), "'#' expected");
11632     Parser.Lex(); // skip hash token.
11633 
11634     const MCExpr *OffsetExpr;
11635     SMLoc ExLoc = Parser.getTok().getLoc();
11636     SMLoc EndLoc;
11637     if (getParser().parseExpression(OffsetExpr, EndLoc))
11638       return Error(ExLoc, "malformed setfp offset");
11639     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
11640     if (check(!CE, ExLoc, "setfp offset must be an immediate"))
11641       return true;
11642     Offset = CE->getValue();
11643   }
11644 
11645   if (Parser.parseToken(AsmToken::EndOfStatement))
11646     return true;
11647 
11648   getTargetStreamer().emitSetFP(static_cast<unsigned>(FPReg),
11649                                 static_cast<unsigned>(SPReg), Offset);
11650   return false;
11651 }
11652 
11653 /// parseDirective
11654 ///  ::= .pad offset
11655 bool ARMAsmParser::parseDirectivePad(SMLoc L) {
11656   MCAsmParser &Parser = getParser();
11657   // Check the ordering of unwind directives
11658   if (!UC.hasFnStart())
11659     return Error(L, ".fnstart must precede .pad directive");
11660   if (UC.hasHandlerData())
11661     return Error(L, ".pad must precede .handlerdata directive");
11662 
11663   // Parse the offset
11664   if (Parser.getTok().isNot(AsmToken::Hash) &&
11665       Parser.getTok().isNot(AsmToken::Dollar))
11666     return Error(Parser.getTok().getLoc(), "'#' expected");
11667   Parser.Lex(); // skip hash token.
11668 
11669   const MCExpr *OffsetExpr;
11670   SMLoc ExLoc = Parser.getTok().getLoc();
11671   SMLoc EndLoc;
11672   if (getParser().parseExpression(OffsetExpr, EndLoc))
11673     return Error(ExLoc, "malformed pad offset");
11674   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
11675   if (!CE)
11676     return Error(ExLoc, "pad offset must be an immediate");
11677 
11678   if (parseToken(AsmToken::EndOfStatement,
11679                  "unexpected token in '.pad' directive"))
11680     return true;
11681 
11682   getTargetStreamer().emitPad(CE->getValue());
11683   return false;
11684 }
11685 
11686 /// parseDirectiveRegSave
11687 ///  ::= .save  { registers }
11688 ///  ::= .vsave { registers }
11689 bool ARMAsmParser::parseDirectiveRegSave(SMLoc L, bool IsVector) {
11690   // Check the ordering of unwind directives
11691   if (!UC.hasFnStart())
11692     return Error(L, ".fnstart must precede .save or .vsave directives");
11693   if (UC.hasHandlerData())
11694     return Error(L, ".save or .vsave must precede .handlerdata directive");
11695 
11696   // RAII object to make sure parsed operands are deleted.
11697   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Operands;
11698 
11699   // Parse the register list
11700   if (parseRegisterList(Operands, true, true) ||
11701       parseToken(AsmToken::EndOfStatement, "unexpected token in directive"))
11702     return true;
11703   ARMOperand &Op = (ARMOperand &)*Operands[0];
11704   if (!IsVector && !Op.isRegList())
11705     return Error(L, ".save expects GPR registers");
11706   if (IsVector && !Op.isDPRRegList())
11707     return Error(L, ".vsave expects DPR registers");
11708 
11709   getTargetStreamer().emitRegSave(Op.getRegList(), IsVector);
11710   return false;
11711 }
11712 
11713 /// parseDirectiveInst
11714 ///  ::= .inst opcode [, ...]
11715 ///  ::= .inst.n opcode [, ...]
11716 ///  ::= .inst.w opcode [, ...]
11717 bool ARMAsmParser::parseDirectiveInst(SMLoc Loc, char Suffix) {
11718   int Width = 4;
11719 
11720   if (isThumb()) {
11721     switch (Suffix) {
11722     case 'n':
11723       Width = 2;
11724       break;
11725     case 'w':
11726       break;
11727     default:
11728       Width = 0;
11729       break;
11730     }
11731   } else {
11732     if (Suffix)
11733       return Error(Loc, "width suffixes are invalid in ARM mode");
11734   }
11735 
11736   auto parseOne = [&]() -> bool {
11737     const MCExpr *Expr;
11738     if (getParser().parseExpression(Expr))
11739       return true;
11740     const MCConstantExpr *Value = dyn_cast_or_null<MCConstantExpr>(Expr);
11741     if (!Value) {
11742       return Error(Loc, "expected constant expression");
11743     }
11744 
11745     char CurSuffix = Suffix;
11746     switch (Width) {
11747     case 2:
11748       if (Value->getValue() > 0xffff)
11749         return Error(Loc, "inst.n operand is too big, use inst.w instead");
11750       break;
11751     case 4:
11752       if (Value->getValue() > 0xffffffff)
11753         return Error(Loc, StringRef(Suffix ? "inst.w" : "inst") +
11754                               " operand is too big");
11755       break;
11756     case 0:
11757       // Thumb mode, no width indicated. Guess from the opcode, if possible.
11758       if (Value->getValue() < 0xe800)
11759         CurSuffix = 'n';
11760       else if (Value->getValue() >= 0xe8000000)
11761         CurSuffix = 'w';
11762       else
11763         return Error(Loc, "cannot determine Thumb instruction size, "
11764                           "use inst.n/inst.w instead");
11765       break;
11766     default:
11767       llvm_unreachable("only supported widths are 2 and 4");
11768     }
11769 
11770     getTargetStreamer().emitInst(Value->getValue(), CurSuffix);
11771     return false;
11772   };
11773 
11774   if (parseOptionalToken(AsmToken::EndOfStatement))
11775     return Error(Loc, "expected expression following directive");
11776   if (parseMany(parseOne))
11777     return true;
11778   return false;
11779 }
11780 
11781 /// parseDirectiveLtorg
11782 ///  ::= .ltorg | .pool
11783 bool ARMAsmParser::parseDirectiveLtorg(SMLoc L) {
11784   if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive"))
11785     return true;
11786   getTargetStreamer().emitCurrentConstantPool();
11787   return false;
11788 }
11789 
11790 bool ARMAsmParser::parseDirectiveEven(SMLoc L) {
11791   const MCSection *Section = getStreamer().getCurrentSectionOnly();
11792 
11793   if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive"))
11794     return true;
11795 
11796   if (!Section) {
11797     getStreamer().initSections(false, getSTI());
11798     Section = getStreamer().getCurrentSectionOnly();
11799   }
11800 
11801   assert(Section && "must have section to emit alignment");
11802   if (Section->UseCodeAlign())
11803     getStreamer().emitCodeAlignment(2, &getSTI());
11804   else
11805     getStreamer().emitValueToAlignment(2);
11806 
11807   return false;
11808 }
11809 
11810 /// parseDirectivePersonalityIndex
11811 ///   ::= .personalityindex index
11812 bool ARMAsmParser::parseDirectivePersonalityIndex(SMLoc L) {
11813   MCAsmParser &Parser = getParser();
11814   bool HasExistingPersonality = UC.hasPersonality();
11815 
11816   const MCExpr *IndexExpression;
11817   SMLoc IndexLoc = Parser.getTok().getLoc();
11818   if (Parser.parseExpression(IndexExpression) ||
11819       parseToken(AsmToken::EndOfStatement,
11820                  "unexpected token in '.personalityindex' directive")) {
11821     return true;
11822   }
11823 
11824   UC.recordPersonalityIndex(L);
11825 
11826   if (!UC.hasFnStart()) {
11827     return Error(L, ".fnstart must precede .personalityindex directive");
11828   }
11829   if (UC.cantUnwind()) {
11830     Error(L, ".personalityindex cannot be used with .cantunwind");
11831     UC.emitCantUnwindLocNotes();
11832     return true;
11833   }
11834   if (UC.hasHandlerData()) {
11835     Error(L, ".personalityindex must precede .handlerdata directive");
11836     UC.emitHandlerDataLocNotes();
11837     return true;
11838   }
11839   if (HasExistingPersonality) {
11840     Error(L, "multiple personality directives");
11841     UC.emitPersonalityLocNotes();
11842     return true;
11843   }
11844 
11845   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(IndexExpression);
11846   if (!CE)
11847     return Error(IndexLoc, "index must be a constant number");
11848   if (CE->getValue() < 0 || CE->getValue() >= ARM::EHABI::NUM_PERSONALITY_INDEX)
11849     return Error(IndexLoc,
11850                  "personality routine index should be in range [0-3]");
11851 
11852   getTargetStreamer().emitPersonalityIndex(CE->getValue());
11853   return false;
11854 }
11855 
11856 /// parseDirectiveUnwindRaw
11857 ///   ::= .unwind_raw offset, opcode [, opcode...]
11858 bool ARMAsmParser::parseDirectiveUnwindRaw(SMLoc L) {
11859   MCAsmParser &Parser = getParser();
11860   int64_t StackOffset;
11861   const MCExpr *OffsetExpr;
11862   SMLoc OffsetLoc = getLexer().getLoc();
11863 
11864   if (!UC.hasFnStart())
11865     return Error(L, ".fnstart must precede .unwind_raw directives");
11866   if (getParser().parseExpression(OffsetExpr))
11867     return Error(OffsetLoc, "expected expression");
11868 
11869   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
11870   if (!CE)
11871     return Error(OffsetLoc, "offset must be a constant");
11872 
11873   StackOffset = CE->getValue();
11874 
11875   if (Parser.parseToken(AsmToken::Comma, "expected comma"))
11876     return true;
11877 
11878   SmallVector<uint8_t, 16> Opcodes;
11879 
11880   auto parseOne = [&]() -> bool {
11881     const MCExpr *OE = nullptr;
11882     SMLoc OpcodeLoc = getLexer().getLoc();
11883     if (check(getLexer().is(AsmToken::EndOfStatement) ||
11884                   Parser.parseExpression(OE),
11885               OpcodeLoc, "expected opcode expression"))
11886       return true;
11887     const MCConstantExpr *OC = dyn_cast<MCConstantExpr>(OE);
11888     if (!OC)
11889       return Error(OpcodeLoc, "opcode value must be a constant");
11890     const int64_t Opcode = OC->getValue();
11891     if (Opcode & ~0xff)
11892       return Error(OpcodeLoc, "invalid opcode");
11893     Opcodes.push_back(uint8_t(Opcode));
11894     return false;
11895   };
11896 
11897   // Must have at least 1 element
11898   SMLoc OpcodeLoc = getLexer().getLoc();
11899   if (parseOptionalToken(AsmToken::EndOfStatement))
11900     return Error(OpcodeLoc, "expected opcode expression");
11901   if (parseMany(parseOne))
11902     return true;
11903 
11904   getTargetStreamer().emitUnwindRaw(StackOffset, Opcodes);
11905   return false;
11906 }
11907 
11908 /// parseDirectiveTLSDescSeq
11909 ///   ::= .tlsdescseq tls-variable
11910 bool ARMAsmParser::parseDirectiveTLSDescSeq(SMLoc L) {
11911   MCAsmParser &Parser = getParser();
11912 
11913   if (getLexer().isNot(AsmToken::Identifier))
11914     return TokError("expected variable after '.tlsdescseq' directive");
11915 
11916   const MCSymbolRefExpr *SRE =
11917     MCSymbolRefExpr::create(Parser.getTok().getIdentifier(),
11918                             MCSymbolRefExpr::VK_ARM_TLSDESCSEQ, getContext());
11919   Lex();
11920 
11921   if (parseToken(AsmToken::EndOfStatement,
11922                  "unexpected token in '.tlsdescseq' directive"))
11923     return true;
11924 
11925   getTargetStreamer().AnnotateTLSDescriptorSequence(SRE);
11926   return false;
11927 }
11928 
11929 /// parseDirectiveMovSP
11930 ///  ::= .movsp reg [, #offset]
11931 bool ARMAsmParser::parseDirectiveMovSP(SMLoc L) {
11932   MCAsmParser &Parser = getParser();
11933   if (!UC.hasFnStart())
11934     return Error(L, ".fnstart must precede .movsp directives");
11935   if (UC.getFPReg() != ARM::SP)
11936     return Error(L, "unexpected .movsp directive");
11937 
11938   SMLoc SPRegLoc = Parser.getTok().getLoc();
11939   int SPReg = tryParseRegister();
11940   if (SPReg == -1)
11941     return Error(SPRegLoc, "register expected");
11942   if (SPReg == ARM::SP || SPReg == ARM::PC)
11943     return Error(SPRegLoc, "sp and pc are not permitted in .movsp directive");
11944 
11945   int64_t Offset = 0;
11946   if (Parser.parseOptionalToken(AsmToken::Comma)) {
11947     if (Parser.parseToken(AsmToken::Hash, "expected #constant"))
11948       return true;
11949 
11950     const MCExpr *OffsetExpr;
11951     SMLoc OffsetLoc = Parser.getTok().getLoc();
11952 
11953     if (Parser.parseExpression(OffsetExpr))
11954       return Error(OffsetLoc, "malformed offset expression");
11955 
11956     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
11957     if (!CE)
11958       return Error(OffsetLoc, "offset must be an immediate constant");
11959 
11960     Offset = CE->getValue();
11961   }
11962 
11963   if (parseToken(AsmToken::EndOfStatement,
11964                  "unexpected token in '.movsp' directive"))
11965     return true;
11966 
11967   getTargetStreamer().emitMovSP(SPReg, Offset);
11968   UC.saveFPReg(SPReg);
11969 
11970   return false;
11971 }
11972 
11973 /// parseDirectiveObjectArch
11974 ///   ::= .object_arch name
11975 bool ARMAsmParser::parseDirectiveObjectArch(SMLoc L) {
11976   MCAsmParser &Parser = getParser();
11977   if (getLexer().isNot(AsmToken::Identifier))
11978     return Error(getLexer().getLoc(), "unexpected token");
11979 
11980   StringRef Arch = Parser.getTok().getString();
11981   SMLoc ArchLoc = Parser.getTok().getLoc();
11982   Lex();
11983 
11984   ARM::ArchKind ID = ARM::parseArch(Arch);
11985 
11986   if (ID == ARM::ArchKind::INVALID)
11987     return Error(ArchLoc, "unknown architecture '" + Arch + "'");
11988   if (parseToken(AsmToken::EndOfStatement))
11989     return true;
11990 
11991   getTargetStreamer().emitObjectArch(ID);
11992   return false;
11993 }
11994 
11995 /// parseDirectiveAlign
11996 ///   ::= .align
11997 bool ARMAsmParser::parseDirectiveAlign(SMLoc L) {
11998   // NOTE: if this is not the end of the statement, fall back to the target
11999   // agnostic handling for this directive which will correctly handle this.
12000   if (parseOptionalToken(AsmToken::EndOfStatement)) {
12001     // '.align' is target specifically handled to mean 2**2 byte alignment.
12002     const MCSection *Section = getStreamer().getCurrentSectionOnly();
12003     assert(Section && "must have section to emit alignment");
12004     if (Section->UseCodeAlign())
12005       getStreamer().emitCodeAlignment(4, &getSTI(), 0);
12006     else
12007       getStreamer().emitValueToAlignment(4, 0, 1, 0);
12008     return false;
12009   }
12010   return true;
12011 }
12012 
12013 /// parseDirectiveThumbSet
12014 ///  ::= .thumb_set name, value
12015 bool ARMAsmParser::parseDirectiveThumbSet(SMLoc L) {
12016   MCAsmParser &Parser = getParser();
12017 
12018   StringRef Name;
12019   if (check(Parser.parseIdentifier(Name),
12020             "expected identifier after '.thumb_set'") ||
12021       parseToken(AsmToken::Comma, "expected comma after name '" + Name + "'"))
12022     return true;
12023 
12024   MCSymbol *Sym;
12025   const MCExpr *Value;
12026   if (MCParserUtils::parseAssignmentExpression(Name, /* allow_redef */ true,
12027                                                Parser, Sym, Value))
12028     return true;
12029 
12030   getTargetStreamer().emitThumbSet(Sym, Value);
12031   return false;
12032 }
12033 
12034 /// Force static initialization.
12035 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeARMAsmParser() {
12036   RegisterMCAsmParser<ARMAsmParser> X(getTheARMLETarget());
12037   RegisterMCAsmParser<ARMAsmParser> Y(getTheARMBETarget());
12038   RegisterMCAsmParser<ARMAsmParser> A(getTheThumbLETarget());
12039   RegisterMCAsmParser<ARMAsmParser> B(getTheThumbBETarget());
12040 }
12041 
12042 #define GET_REGISTER_MATCHER
12043 #define GET_SUBTARGET_FEATURE_NAME
12044 #define GET_MATCHER_IMPLEMENTATION
12045 #define GET_MNEMONIC_SPELL_CHECKER
12046 #include "ARMGenAsmMatcher.inc"
12047 
12048 // Some diagnostics need to vary with subtarget features, so they are handled
12049 // here. For example, the DPR class has either 16 or 32 registers, depending
12050 // on the FPU available.
12051 const char *
12052 ARMAsmParser::getCustomOperandDiag(ARMMatchResultTy MatchError) {
12053   switch (MatchError) {
12054   // rGPR contains sp starting with ARMv8.
12055   case Match_rGPR:
12056     return hasV8Ops() ? "operand must be a register in range [r0, r14]"
12057                       : "operand must be a register in range [r0, r12] or r14";
12058   // DPR contains 16 registers for some FPUs, and 32 for others.
12059   case Match_DPR:
12060     return hasD32() ? "operand must be a register in range [d0, d31]"
12061                     : "operand must be a register in range [d0, d15]";
12062   case Match_DPR_RegList:
12063     return hasD32() ? "operand must be a list of registers in range [d0, d31]"
12064                     : "operand must be a list of registers in range [d0, d15]";
12065 
12066   // For all other diags, use the static string from tablegen.
12067   default:
12068     return getMatchKindDiag(MatchError);
12069   }
12070 }
12071 
12072 // Process the list of near-misses, throwing away ones we don't want to report
12073 // to the user, and converting the rest to a source location and string that
12074 // should be reported.
12075 void
12076 ARMAsmParser::FilterNearMisses(SmallVectorImpl<NearMissInfo> &NearMissesIn,
12077                                SmallVectorImpl<NearMissMessage> &NearMissesOut,
12078                                SMLoc IDLoc, OperandVector &Operands) {
12079   // TODO: If operand didn't match, sub in a dummy one and run target
12080   // predicate, so that we can avoid reporting near-misses that are invalid?
12081   // TODO: Many operand types dont have SuperClasses set, so we report
12082   // redundant ones.
12083   // TODO: Some operands are superclasses of registers (e.g.
12084   // MCK_RegShiftedImm), we don't have any way to represent that currently.
12085   // TODO: This is not all ARM-specific, can some of it be factored out?
12086 
12087   // Record some information about near-misses that we have already seen, so
12088   // that we can avoid reporting redundant ones. For example, if there are
12089   // variants of an instruction that take 8- and 16-bit immediates, we want
12090   // to only report the widest one.
12091   std::multimap<unsigned, unsigned> OperandMissesSeen;
12092   SmallSet<FeatureBitset, 4> FeatureMissesSeen;
12093   bool ReportedTooFewOperands = false;
12094 
12095   // Process the near-misses in reverse order, so that we see more general ones
12096   // first, and so can avoid emitting more specific ones.
12097   for (NearMissInfo &I : reverse(NearMissesIn)) {
12098     switch (I.getKind()) {
12099     case NearMissInfo::NearMissOperand: {
12100       SMLoc OperandLoc =
12101           ((ARMOperand &)*Operands[I.getOperandIndex()]).getStartLoc();
12102       const char *OperandDiag =
12103           getCustomOperandDiag((ARMMatchResultTy)I.getOperandError());
12104 
12105       // If we have already emitted a message for a superclass, don't also report
12106       // the sub-class. We consider all operand classes that we don't have a
12107       // specialised diagnostic for to be equal for the propose of this check,
12108       // so that we don't report the generic error multiple times on the same
12109       // operand.
12110       unsigned DupCheckMatchClass = OperandDiag ? I.getOperandClass() : ~0U;
12111       auto PrevReports = OperandMissesSeen.equal_range(I.getOperandIndex());
12112       if (std::any_of(PrevReports.first, PrevReports.second,
12113                       [DupCheckMatchClass](
12114                           const std::pair<unsigned, unsigned> Pair) {
12115             if (DupCheckMatchClass == ~0U || Pair.second == ~0U)
12116               return Pair.second == DupCheckMatchClass;
12117             else
12118               return isSubclass((MatchClassKind)DupCheckMatchClass,
12119                                 (MatchClassKind)Pair.second);
12120           }))
12121         break;
12122       OperandMissesSeen.insert(
12123           std::make_pair(I.getOperandIndex(), DupCheckMatchClass));
12124 
12125       NearMissMessage Message;
12126       Message.Loc = OperandLoc;
12127       if (OperandDiag) {
12128         Message.Message = OperandDiag;
12129       } else if (I.getOperandClass() == InvalidMatchClass) {
12130         Message.Message = "too many operands for instruction";
12131       } else {
12132         Message.Message = "invalid operand for instruction";
12133         LLVM_DEBUG(
12134             dbgs() << "Missing diagnostic string for operand class "
12135                    << getMatchClassName((MatchClassKind)I.getOperandClass())
12136                    << I.getOperandClass() << ", error " << I.getOperandError()
12137                    << ", opcode " << MII.getName(I.getOpcode()) << "\n");
12138       }
12139       NearMissesOut.emplace_back(Message);
12140       break;
12141     }
12142     case NearMissInfo::NearMissFeature: {
12143       const FeatureBitset &MissingFeatures = I.getFeatures();
12144       // Don't report the same set of features twice.
12145       if (FeatureMissesSeen.count(MissingFeatures))
12146         break;
12147       FeatureMissesSeen.insert(MissingFeatures);
12148 
12149       // Special case: don't report a feature set which includes arm-mode for
12150       // targets that don't have ARM mode.
12151       if (MissingFeatures.test(Feature_IsARMBit) && !hasARM())
12152         break;
12153       // Don't report any near-misses that both require switching instruction
12154       // set, and adding other subtarget features.
12155       if (isThumb() && MissingFeatures.test(Feature_IsARMBit) &&
12156           MissingFeatures.count() > 1)
12157         break;
12158       if (!isThumb() && MissingFeatures.test(Feature_IsThumbBit) &&
12159           MissingFeatures.count() > 1)
12160         break;
12161       if (!isThumb() && MissingFeatures.test(Feature_IsThumb2Bit) &&
12162           (MissingFeatures & ~FeatureBitset({Feature_IsThumb2Bit,
12163                                              Feature_IsThumbBit})).any())
12164         break;
12165       if (isMClass() && MissingFeatures.test(Feature_HasNEONBit))
12166         break;
12167 
12168       NearMissMessage Message;
12169       Message.Loc = IDLoc;
12170       raw_svector_ostream OS(Message.Message);
12171 
12172       OS << "instruction requires:";
12173       for (unsigned i = 0, e = MissingFeatures.size(); i != e; ++i)
12174         if (MissingFeatures.test(i))
12175           OS << ' ' << getSubtargetFeatureName(i);
12176 
12177       NearMissesOut.emplace_back(Message);
12178 
12179       break;
12180     }
12181     case NearMissInfo::NearMissPredicate: {
12182       NearMissMessage Message;
12183       Message.Loc = IDLoc;
12184       switch (I.getPredicateError()) {
12185       case Match_RequiresNotITBlock:
12186         Message.Message = "flag setting instruction only valid outside IT block";
12187         break;
12188       case Match_RequiresITBlock:
12189         Message.Message = "instruction only valid inside IT block";
12190         break;
12191       case Match_RequiresV6:
12192         Message.Message = "instruction variant requires ARMv6 or later";
12193         break;
12194       case Match_RequiresThumb2:
12195         Message.Message = "instruction variant requires Thumb2";
12196         break;
12197       case Match_RequiresV8:
12198         Message.Message = "instruction variant requires ARMv8 or later";
12199         break;
12200       case Match_RequiresFlagSetting:
12201         Message.Message = "no flag-preserving variant of this instruction available";
12202         break;
12203       case Match_InvalidOperand:
12204         Message.Message = "invalid operand for instruction";
12205         break;
12206       default:
12207         llvm_unreachable("Unhandled target predicate error");
12208         break;
12209       }
12210       NearMissesOut.emplace_back(Message);
12211       break;
12212     }
12213     case NearMissInfo::NearMissTooFewOperands: {
12214       if (!ReportedTooFewOperands) {
12215         SMLoc EndLoc = ((ARMOperand &)*Operands.back()).getEndLoc();
12216         NearMissesOut.emplace_back(NearMissMessage{
12217             EndLoc, StringRef("too few operands for instruction")});
12218         ReportedTooFewOperands = true;
12219       }
12220       break;
12221     }
12222     case NearMissInfo::NoNearMiss:
12223       // This should never leave the matcher.
12224       llvm_unreachable("not a near-miss");
12225       break;
12226     }
12227   }
12228 }
12229 
12230 void ARMAsmParser::ReportNearMisses(SmallVectorImpl<NearMissInfo> &NearMisses,
12231                                     SMLoc IDLoc, OperandVector &Operands) {
12232   SmallVector<NearMissMessage, 4> Messages;
12233   FilterNearMisses(NearMisses, Messages, IDLoc, Operands);
12234 
12235   if (Messages.size() == 0) {
12236     // No near-misses were found, so the best we can do is "invalid
12237     // instruction".
12238     Error(IDLoc, "invalid instruction");
12239   } else if (Messages.size() == 1) {
12240     // One near miss was found, report it as the sole error.
12241     Error(Messages[0].Loc, Messages[0].Message);
12242   } else {
12243     // More than one near miss, so report a generic "invalid instruction"
12244     // error, followed by notes for each of the near-misses.
12245     Error(IDLoc, "invalid instruction, any one of the following would fix this:");
12246     for (auto &M : Messages) {
12247       Note(M.Loc, M.Message);
12248     }
12249   }
12250 }
12251 
12252 bool ARMAsmParser::enableArchExtFeature(StringRef Name, SMLoc &ExtLoc) {
12253   // FIXME: This structure should be moved inside ARMTargetParser
12254   // when we start to table-generate them, and we can use the ARM
12255   // flags below, that were generated by table-gen.
12256   static const struct {
12257     const uint64_t Kind;
12258     const FeatureBitset ArchCheck;
12259     const FeatureBitset Features;
12260   } Extensions[] = {
12261       {ARM::AEK_CRC, {Feature_HasV8Bit}, {ARM::FeatureCRC}},
12262       {ARM::AEK_AES,
12263        {Feature_HasV8Bit},
12264        {ARM::FeatureAES, ARM::FeatureNEON, ARM::FeatureFPARMv8}},
12265       {ARM::AEK_SHA2,
12266        {Feature_HasV8Bit},
12267        {ARM::FeatureSHA2, ARM::FeatureNEON, ARM::FeatureFPARMv8}},
12268       {ARM::AEK_CRYPTO,
12269        {Feature_HasV8Bit},
12270        {ARM::FeatureCrypto, ARM::FeatureNEON, ARM::FeatureFPARMv8}},
12271       {ARM::AEK_FP,
12272        {Feature_HasV8Bit},
12273        {ARM::FeatureVFP2_SP, ARM::FeatureFPARMv8}},
12274       {(ARM::AEK_HWDIVTHUMB | ARM::AEK_HWDIVARM),
12275        {Feature_HasV7Bit, Feature_IsNotMClassBit},
12276        {ARM::FeatureHWDivThumb, ARM::FeatureHWDivARM}},
12277       {ARM::AEK_MP,
12278        {Feature_HasV7Bit, Feature_IsNotMClassBit},
12279        {ARM::FeatureMP}},
12280       {ARM::AEK_SIMD,
12281        {Feature_HasV8Bit},
12282        {ARM::FeatureNEON, ARM::FeatureVFP2_SP, ARM::FeatureFPARMv8}},
12283       {ARM::AEK_SEC, {Feature_HasV6KBit}, {ARM::FeatureTrustZone}},
12284       // FIXME: Only available in A-class, isel not predicated
12285       {ARM::AEK_VIRT, {Feature_HasV7Bit}, {ARM::FeatureVirtualization}},
12286       {ARM::AEK_FP16,
12287        {Feature_HasV8_2aBit},
12288        {ARM::FeatureFPARMv8, ARM::FeatureFullFP16}},
12289       {ARM::AEK_RAS, {Feature_HasV8Bit}, {ARM::FeatureRAS}},
12290       {ARM::AEK_LOB, {Feature_HasV8_1MMainlineBit}, {ARM::FeatureLOB}},
12291       {ARM::AEK_PACBTI, {Feature_HasV8_1MMainlineBit}, {ARM::FeaturePACBTI}},
12292       // FIXME: Unsupported extensions.
12293       {ARM::AEK_OS, {}, {}},
12294       {ARM::AEK_IWMMXT, {}, {}},
12295       {ARM::AEK_IWMMXT2, {}, {}},
12296       {ARM::AEK_MAVERICK, {}, {}},
12297       {ARM::AEK_XSCALE, {}, {}},
12298   };
12299   bool EnableFeature = true;
12300   if (Name.startswith_insensitive("no")) {
12301     EnableFeature = false;
12302     Name = Name.substr(2);
12303   }
12304   uint64_t FeatureKind = ARM::parseArchExt(Name);
12305   if (FeatureKind == ARM::AEK_INVALID)
12306     return Error(ExtLoc, "unknown architectural extension: " + Name);
12307 
12308   for (const auto &Extension : Extensions) {
12309     if (Extension.Kind != FeatureKind)
12310       continue;
12311 
12312     if (Extension.Features.none())
12313       return Error(ExtLoc, "unsupported architectural extension: " + Name);
12314 
12315     if ((getAvailableFeatures() & Extension.ArchCheck) != Extension.ArchCheck)
12316       return Error(ExtLoc, "architectural extension '" + Name +
12317                                "' is not "
12318                                "allowed for the current base architecture");
12319 
12320     MCSubtargetInfo &STI = copySTI();
12321     if (EnableFeature) {
12322       STI.SetFeatureBitsTransitively(Extension.Features);
12323     } else {
12324       STI.ClearFeatureBitsTransitively(Extension.Features);
12325     }
12326     FeatureBitset Features = ComputeAvailableFeatures(STI.getFeatureBits());
12327     setAvailableFeatures(Features);
12328     return true;
12329   }
12330   return false;
12331 }
12332 
12333 /// parseDirectiveArchExtension
12334 ///   ::= .arch_extension [no]feature
12335 bool ARMAsmParser::parseDirectiveArchExtension(SMLoc L) {
12336 
12337   MCAsmParser &Parser = getParser();
12338 
12339   if (getLexer().isNot(AsmToken::Identifier))
12340     return Error(getLexer().getLoc(), "expected architecture extension name");
12341 
12342   StringRef Name = Parser.getTok().getString();
12343   SMLoc ExtLoc = Parser.getTok().getLoc();
12344   Lex();
12345 
12346   if (parseToken(AsmToken::EndOfStatement,
12347                  "unexpected token in '.arch_extension' directive"))
12348     return true;
12349 
12350   if (Name == "nocrypto") {
12351     enableArchExtFeature("nosha2", ExtLoc);
12352     enableArchExtFeature("noaes", ExtLoc);
12353   }
12354 
12355   if (enableArchExtFeature(Name, ExtLoc))
12356     return false;
12357 
12358   return Error(ExtLoc, "unknown architectural extension: " + Name);
12359 }
12360 
12361 // Define this matcher function after the auto-generated include so we
12362 // have the match class enum definitions.
12363 unsigned ARMAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
12364                                                   unsigned Kind) {
12365   ARMOperand &Op = static_cast<ARMOperand &>(AsmOp);
12366   // If the kind is a token for a literal immediate, check if our asm
12367   // operand matches. This is for InstAliases which have a fixed-value
12368   // immediate in the syntax.
12369   switch (Kind) {
12370   default: break;
12371   case MCK__HASH_0:
12372     if (Op.isImm())
12373       if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm()))
12374         if (CE->getValue() == 0)
12375           return Match_Success;
12376     break;
12377   case MCK__HASH_8:
12378     if (Op.isImm())
12379       if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm()))
12380         if (CE->getValue() == 8)
12381           return Match_Success;
12382     break;
12383   case MCK__HASH_16:
12384     if (Op.isImm())
12385       if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm()))
12386         if (CE->getValue() == 16)
12387           return Match_Success;
12388     break;
12389   case MCK_ModImm:
12390     if (Op.isImm()) {
12391       const MCExpr *SOExpr = Op.getImm();
12392       int64_t Value;
12393       if (!SOExpr->evaluateAsAbsolute(Value))
12394         return Match_Success;
12395       assert((Value >= std::numeric_limits<int32_t>::min() &&
12396               Value <= std::numeric_limits<uint32_t>::max()) &&
12397              "expression value must be representable in 32 bits");
12398     }
12399     break;
12400   case MCK_rGPR:
12401     if (hasV8Ops() && Op.isReg() && Op.getReg() == ARM::SP)
12402       return Match_Success;
12403     return Match_rGPR;
12404   case MCK_GPRPair:
12405     if (Op.isReg() &&
12406         MRI->getRegClass(ARM::GPRRegClassID).contains(Op.getReg()))
12407       return Match_Success;
12408     break;
12409   }
12410   return Match_InvalidOperand;
12411 }
12412 
12413 bool ARMAsmParser::isMnemonicVPTPredicable(StringRef Mnemonic,
12414                                            StringRef ExtraToken) {
12415   if (!hasMVE())
12416     return false;
12417 
12418   return Mnemonic.startswith("vabav") || Mnemonic.startswith("vaddv") ||
12419          Mnemonic.startswith("vaddlv") || Mnemonic.startswith("vminnmv") ||
12420          Mnemonic.startswith("vminnmav") || Mnemonic.startswith("vminv") ||
12421          Mnemonic.startswith("vminav") || Mnemonic.startswith("vmaxnmv") ||
12422          Mnemonic.startswith("vmaxnmav") || Mnemonic.startswith("vmaxv") ||
12423          Mnemonic.startswith("vmaxav") || Mnemonic.startswith("vmladav") ||
12424          Mnemonic.startswith("vrmlaldavh") || Mnemonic.startswith("vrmlalvh") ||
12425          Mnemonic.startswith("vmlsdav") || Mnemonic.startswith("vmlav") ||
12426          Mnemonic.startswith("vmlaldav") || Mnemonic.startswith("vmlalv") ||
12427          Mnemonic.startswith("vmaxnm") || Mnemonic.startswith("vminnm") ||
12428          Mnemonic.startswith("vmax") || Mnemonic.startswith("vmin") ||
12429          Mnemonic.startswith("vshlc") || Mnemonic.startswith("vmovlt") ||
12430          Mnemonic.startswith("vmovlb") || Mnemonic.startswith("vshll") ||
12431          Mnemonic.startswith("vrshrn") || Mnemonic.startswith("vshrn") ||
12432          Mnemonic.startswith("vqrshrun") || Mnemonic.startswith("vqshrun") ||
12433          Mnemonic.startswith("vqrshrn") || Mnemonic.startswith("vqshrn") ||
12434          Mnemonic.startswith("vbic") || Mnemonic.startswith("vrev64") ||
12435          Mnemonic.startswith("vrev32") || Mnemonic.startswith("vrev16") ||
12436          Mnemonic.startswith("vmvn") || Mnemonic.startswith("veor") ||
12437          Mnemonic.startswith("vorn") || Mnemonic.startswith("vorr") ||
12438          Mnemonic.startswith("vand") || Mnemonic.startswith("vmul") ||
12439          Mnemonic.startswith("vqrdmulh") || Mnemonic.startswith("vqdmulh") ||
12440          Mnemonic.startswith("vsub") || Mnemonic.startswith("vadd") ||
12441          Mnemonic.startswith("vqsub") || Mnemonic.startswith("vqadd") ||
12442          Mnemonic.startswith("vabd") || Mnemonic.startswith("vrhadd") ||
12443          Mnemonic.startswith("vhsub") || Mnemonic.startswith("vhadd") ||
12444          Mnemonic.startswith("vdup") || Mnemonic.startswith("vcls") ||
12445          Mnemonic.startswith("vclz") || Mnemonic.startswith("vneg") ||
12446          Mnemonic.startswith("vabs") || Mnemonic.startswith("vqneg") ||
12447          Mnemonic.startswith("vqabs") ||
12448          (Mnemonic.startswith("vrint") && Mnemonic != "vrintr") ||
12449          Mnemonic.startswith("vcmla") || Mnemonic.startswith("vfma") ||
12450          Mnemonic.startswith("vfms") || Mnemonic.startswith("vcadd") ||
12451          Mnemonic.startswith("vadd") || Mnemonic.startswith("vsub") ||
12452          Mnemonic.startswith("vshl") || Mnemonic.startswith("vqshl") ||
12453          Mnemonic.startswith("vqrshl") || Mnemonic.startswith("vrshl") ||
12454          Mnemonic.startswith("vsri") || Mnemonic.startswith("vsli") ||
12455          Mnemonic.startswith("vrshr") || Mnemonic.startswith("vshr") ||
12456          Mnemonic.startswith("vpsel") || Mnemonic.startswith("vcmp") ||
12457          Mnemonic.startswith("vqdmladh") || Mnemonic.startswith("vqrdmladh") ||
12458          Mnemonic.startswith("vqdmlsdh") || Mnemonic.startswith("vqrdmlsdh") ||
12459          Mnemonic.startswith("vcmul") || Mnemonic.startswith("vrmulh") ||
12460          Mnemonic.startswith("vqmovn") || Mnemonic.startswith("vqmovun") ||
12461          Mnemonic.startswith("vmovnt") || Mnemonic.startswith("vmovnb") ||
12462          Mnemonic.startswith("vmaxa") || Mnemonic.startswith("vmaxnma") ||
12463          Mnemonic.startswith("vhcadd") || Mnemonic.startswith("vadc") ||
12464          Mnemonic.startswith("vsbc") || Mnemonic.startswith("vrshr") ||
12465          Mnemonic.startswith("vshr") || Mnemonic.startswith("vstrb") ||
12466          Mnemonic.startswith("vldrb") ||
12467          (Mnemonic.startswith("vstrh") && Mnemonic != "vstrhi") ||
12468          (Mnemonic.startswith("vldrh") && Mnemonic != "vldrhi") ||
12469          Mnemonic.startswith("vstrw") || Mnemonic.startswith("vldrw") ||
12470          Mnemonic.startswith("vldrd") || Mnemonic.startswith("vstrd") ||
12471          Mnemonic.startswith("vqdmull") || Mnemonic.startswith("vbrsr") ||
12472          Mnemonic.startswith("vfmas") || Mnemonic.startswith("vmlas") ||
12473          Mnemonic.startswith("vmla") || Mnemonic.startswith("vqdmlash") ||
12474          Mnemonic.startswith("vqdmlah") || Mnemonic.startswith("vqrdmlash") ||
12475          Mnemonic.startswith("vqrdmlah") || Mnemonic.startswith("viwdup") ||
12476          Mnemonic.startswith("vdwdup") || Mnemonic.startswith("vidup") ||
12477          Mnemonic.startswith("vddup") || Mnemonic.startswith("vctp") ||
12478          Mnemonic.startswith("vpnot") || Mnemonic.startswith("vbic") ||
12479          Mnemonic.startswith("vrmlsldavh") || Mnemonic.startswith("vmlsldav") ||
12480          Mnemonic.startswith("vcvt") ||
12481          MS.isVPTPredicableCDEInstr(Mnemonic) ||
12482          (Mnemonic.startswith("vmov") &&
12483           !(ExtraToken == ".f16" || ExtraToken == ".32" ||
12484             ExtraToken == ".16" || ExtraToken == ".8"));
12485 }
12486