1 //===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "ARMFeatures.h"
11 #include "Utils/ARMBaseInfo.h"
12 #include "MCTargetDesc/ARMAddressingModes.h"
13 #include "MCTargetDesc/ARMBaseInfo.h"
14 #include "MCTargetDesc/ARMMCExpr.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/BinaryFormat/COFF.h"
22 #include "llvm/BinaryFormat/ELF.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCAssembler.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
27 #include "llvm/MC/MCELFStreamer.h"
28 #include "llvm/MC/MCExpr.h"
29 #include "llvm/MC/MCInst.h"
30 #include "llvm/MC/MCInstrDesc.h"
31 #include "llvm/MC/MCInstrInfo.h"
32 #include "llvm/MC/MCObjectFileInfo.h"
33 #include "llvm/MC/MCParser/MCAsmLexer.h"
34 #include "llvm/MC/MCParser/MCAsmParser.h"
35 #include "llvm/MC/MCParser/MCAsmParserUtils.h"
36 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
37 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
38 #include "llvm/MC/MCRegisterInfo.h"
39 #include "llvm/MC/MCSection.h"
40 #include "llvm/MC/MCStreamer.h"
41 #include "llvm/MC/MCSubtargetInfo.h"
42 #include "llvm/MC/MCSymbol.h"
43 #include "llvm/Support/ARMBuildAttributes.h"
44 #include "llvm/Support/ARMEHABI.h"
45 #include "llvm/Support/CommandLine.h"
46 #include "llvm/Support/Debug.h"
47 #include "llvm/Support/MathExtras.h"
48 #include "llvm/Support/SourceMgr.h"
49 #include "llvm/Support/TargetParser.h"
50 #include "llvm/Support/TargetRegistry.h"
51 #include "llvm/Support/raw_ostream.h"
52 
53 using namespace llvm;
54 
55 namespace {
56 
57 enum class ImplicitItModeTy { Always, Never, ARMOnly, ThumbOnly };
58 
59 static cl::opt<ImplicitItModeTy> ImplicitItMode(
60     "arm-implicit-it", cl::init(ImplicitItModeTy::ARMOnly),
61     cl::desc("Allow conditional instructions outdside of an IT block"),
62     cl::values(clEnumValN(ImplicitItModeTy::Always, "always",
63                           "Accept in both ISAs, emit implicit ITs in Thumb"),
64                clEnumValN(ImplicitItModeTy::Never, "never",
65                           "Warn in ARM, reject in Thumb"),
66                clEnumValN(ImplicitItModeTy::ARMOnly, "arm",
67                           "Accept in ARM, reject in Thumb"),
68                clEnumValN(ImplicitItModeTy::ThumbOnly, "thumb",
69                           "Warn in ARM, emit implicit ITs in Thumb")));
70 
71 static cl::opt<bool> AddBuildAttributes("arm-add-build-attributes",
72                                         cl::init(false));
73 
74 class ARMOperand;
75 
76 enum VectorLaneTy { NoLanes, AllLanes, IndexedLane };
77 
78 class UnwindContext {
79   MCAsmParser &Parser;
80 
81   typedef SmallVector<SMLoc, 4> Locs;
82 
83   Locs FnStartLocs;
84   Locs CantUnwindLocs;
85   Locs PersonalityLocs;
86   Locs PersonalityIndexLocs;
87   Locs HandlerDataLocs;
88   int FPReg;
89 
90 public:
91   UnwindContext(MCAsmParser &P) : Parser(P), FPReg(ARM::SP) {}
92 
93   bool hasFnStart() const { return !FnStartLocs.empty(); }
94   bool cantUnwind() const { return !CantUnwindLocs.empty(); }
95   bool hasHandlerData() const { return !HandlerDataLocs.empty(); }
96   bool hasPersonality() const {
97     return !(PersonalityLocs.empty() && PersonalityIndexLocs.empty());
98   }
99 
100   void recordFnStart(SMLoc L) { FnStartLocs.push_back(L); }
101   void recordCantUnwind(SMLoc L) { CantUnwindLocs.push_back(L); }
102   void recordPersonality(SMLoc L) { PersonalityLocs.push_back(L); }
103   void recordHandlerData(SMLoc L) { HandlerDataLocs.push_back(L); }
104   void recordPersonalityIndex(SMLoc L) { PersonalityIndexLocs.push_back(L); }
105 
106   void saveFPReg(int Reg) { FPReg = Reg; }
107   int getFPReg() const { return FPReg; }
108 
109   void emitFnStartLocNotes() const {
110     for (Locs::const_iterator FI = FnStartLocs.begin(), FE = FnStartLocs.end();
111          FI != FE; ++FI)
112       Parser.Note(*FI, ".fnstart was specified here");
113   }
114   void emitCantUnwindLocNotes() const {
115     for (Locs::const_iterator UI = CantUnwindLocs.begin(),
116                               UE = CantUnwindLocs.end(); UI != UE; ++UI)
117       Parser.Note(*UI, ".cantunwind was specified here");
118   }
119   void emitHandlerDataLocNotes() const {
120     for (Locs::const_iterator HI = HandlerDataLocs.begin(),
121                               HE = HandlerDataLocs.end(); HI != HE; ++HI)
122       Parser.Note(*HI, ".handlerdata was specified here");
123   }
124   void emitPersonalityLocNotes() const {
125     for (Locs::const_iterator PI = PersonalityLocs.begin(),
126                               PE = PersonalityLocs.end(),
127                               PII = PersonalityIndexLocs.begin(),
128                               PIE = PersonalityIndexLocs.end();
129          PI != PE || PII != PIE;) {
130       if (PI != PE && (PII == PIE || PI->getPointer() < PII->getPointer()))
131         Parser.Note(*PI++, ".personality was specified here");
132       else if (PII != PIE && (PI == PE || PII->getPointer() < PI->getPointer()))
133         Parser.Note(*PII++, ".personalityindex was specified here");
134       else
135         llvm_unreachable(".personality and .personalityindex cannot be "
136                          "at the same location");
137     }
138   }
139 
140   void reset() {
141     FnStartLocs = Locs();
142     CantUnwindLocs = Locs();
143     PersonalityLocs = Locs();
144     HandlerDataLocs = Locs();
145     PersonalityIndexLocs = Locs();
146     FPReg = ARM::SP;
147   }
148 };
149 
150 class ARMAsmParser : public MCTargetAsmParser {
151   const MCInstrInfo &MII;
152   const MCRegisterInfo *MRI;
153   UnwindContext UC;
154 
155   ARMTargetStreamer &getTargetStreamer() {
156     assert(getParser().getStreamer().getTargetStreamer() &&
157            "do not have a target streamer");
158     MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
159     return static_cast<ARMTargetStreamer &>(TS);
160   }
161 
162   // Map of register aliases registers via the .req directive.
163   StringMap<unsigned> RegisterReqs;
164 
165   bool NextSymbolIsThumb;
166 
167   bool useImplicitITThumb() const {
168     return ImplicitItMode == ImplicitItModeTy::Always ||
169            ImplicitItMode == ImplicitItModeTy::ThumbOnly;
170   }
171 
172   bool useImplicitITARM() const {
173     return ImplicitItMode == ImplicitItModeTy::Always ||
174            ImplicitItMode == ImplicitItModeTy::ARMOnly;
175   }
176 
177   struct {
178     ARMCC::CondCodes Cond;    // Condition for IT block.
179     unsigned Mask:4;          // Condition mask for instructions.
180                               // Starting at first 1 (from lsb).
181                               //   '1'  condition as indicated in IT.
182                               //   '0'  inverse of condition (else).
183                               // Count of instructions in IT block is
184                               // 4 - trailingzeroes(mask)
185                               // Note that this does not have the same encoding
186                               // as in the IT instruction, which also depends
187                               // on the low bit of the condition code.
188 
189     unsigned CurPosition;     // Current position in parsing of IT
190                               // block. In range [0,4], with 0 being the IT
191                               // instruction itself. Initialized according to
192                               // count of instructions in block.  ~0U if no
193                               // active IT block.
194 
195     bool IsExplicit;          // true  - The IT instruction was present in the
196                               //         input, we should not modify it.
197                               // false - The IT instruction was added
198                               //         implicitly, we can extend it if that
199                               //         would be legal.
200   } ITState;
201 
202   llvm::SmallVector<MCInst, 4> PendingConditionalInsts;
203 
204   void flushPendingInstructions(MCStreamer &Out) override {
205     if (!inImplicitITBlock()) {
206       assert(PendingConditionalInsts.size() == 0);
207       return;
208     }
209 
210     // Emit the IT instruction
211     unsigned Mask = getITMaskEncoding();
212     MCInst ITInst;
213     ITInst.setOpcode(ARM::t2IT);
214     ITInst.addOperand(MCOperand::createImm(ITState.Cond));
215     ITInst.addOperand(MCOperand::createImm(Mask));
216     Out.EmitInstruction(ITInst, getSTI());
217 
218     // Emit the conditonal instructions
219     assert(PendingConditionalInsts.size() <= 4);
220     for (const MCInst &Inst : PendingConditionalInsts) {
221       Out.EmitInstruction(Inst, getSTI());
222     }
223     PendingConditionalInsts.clear();
224 
225     // Clear the IT state
226     ITState.Mask = 0;
227     ITState.CurPosition = ~0U;
228   }
229 
230   bool inITBlock() { return ITState.CurPosition != ~0U; }
231   bool inExplicitITBlock() { return inITBlock() && ITState.IsExplicit; }
232   bool inImplicitITBlock() { return inITBlock() && !ITState.IsExplicit; }
233   bool lastInITBlock() {
234     return ITState.CurPosition == 4 - countTrailingZeros(ITState.Mask);
235   }
236   void forwardITPosition() {
237     if (!inITBlock()) return;
238     // Move to the next instruction in the IT block, if there is one. If not,
239     // mark the block as done, except for implicit IT blocks, which we leave
240     // open until we find an instruction that can't be added to it.
241     unsigned TZ = countTrailingZeros(ITState.Mask);
242     if (++ITState.CurPosition == 5 - TZ && ITState.IsExplicit)
243       ITState.CurPosition = ~0U; // Done with the IT block after this.
244   }
245 
246   // Rewind the state of the current IT block, removing the last slot from it.
247   void rewindImplicitITPosition() {
248     assert(inImplicitITBlock());
249     assert(ITState.CurPosition > 1);
250     ITState.CurPosition--;
251     unsigned TZ = countTrailingZeros(ITState.Mask);
252     unsigned NewMask = 0;
253     NewMask |= ITState.Mask & (0xC << TZ);
254     NewMask |= 0x2 << TZ;
255     ITState.Mask = NewMask;
256   }
257 
258   // Rewind the state of the current IT block, removing the last slot from it.
259   // If we were at the first slot, this closes the IT block.
260   void discardImplicitITBlock() {
261     assert(inImplicitITBlock());
262     assert(ITState.CurPosition == 1);
263     ITState.CurPosition = ~0U;
264     return;
265   }
266 
267   // Return the low-subreg of a given Q register.
268   unsigned getDRegFromQReg(unsigned QReg) const {
269     return MRI->getSubReg(QReg, ARM::dsub_0);
270   }
271 
272   // Get the encoding of the IT mask, as it will appear in an IT instruction.
273   unsigned getITMaskEncoding() {
274     assert(inITBlock());
275     unsigned Mask = ITState.Mask;
276     unsigned TZ = countTrailingZeros(Mask);
277     if ((ITState.Cond & 1) == 0) {
278       assert(Mask && TZ <= 3 && "illegal IT mask value!");
279       Mask ^= (0xE << TZ) & 0xF;
280     }
281     return Mask;
282   }
283 
284   // Get the condition code corresponding to the current IT block slot.
285   ARMCC::CondCodes currentITCond() {
286     unsigned MaskBit;
287     if (ITState.CurPosition == 1)
288       MaskBit = 1;
289     else
290       MaskBit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
291 
292     return MaskBit ? ITState.Cond : ARMCC::getOppositeCondition(ITState.Cond);
293   }
294 
295   // Invert the condition of the current IT block slot without changing any
296   // other slots in the same block.
297   void invertCurrentITCondition() {
298     if (ITState.CurPosition == 1) {
299       ITState.Cond = ARMCC::getOppositeCondition(ITState.Cond);
300     } else {
301       ITState.Mask ^= 1 << (5 - ITState.CurPosition);
302     }
303   }
304 
305   // Returns true if the current IT block is full (all 4 slots used).
306   bool isITBlockFull() {
307     return inITBlock() && (ITState.Mask & 1);
308   }
309 
310   // Extend the current implicit IT block to have one more slot with the given
311   // condition code.
312   void extendImplicitITBlock(ARMCC::CondCodes Cond) {
313     assert(inImplicitITBlock());
314     assert(!isITBlockFull());
315     assert(Cond == ITState.Cond ||
316            Cond == ARMCC::getOppositeCondition(ITState.Cond));
317     unsigned TZ = countTrailingZeros(ITState.Mask);
318     unsigned NewMask = 0;
319     // Keep any existing condition bits.
320     NewMask |= ITState.Mask & (0xE << TZ);
321     // Insert the new condition bit.
322     NewMask |= (Cond == ITState.Cond) << TZ;
323     // Move the trailing 1 down one bit.
324     NewMask |= 1 << (TZ - 1);
325     ITState.Mask = NewMask;
326   }
327 
328   // Create a new implicit IT block with a dummy condition code.
329   void startImplicitITBlock() {
330     assert(!inITBlock());
331     ITState.Cond = ARMCC::AL;
332     ITState.Mask = 8;
333     ITState.CurPosition = 1;
334     ITState.IsExplicit = false;
335     return;
336   }
337 
338   // Create a new explicit IT block with the given condition and mask. The mask
339   // should be in the parsed format, with a 1 implying 't', regardless of the
340   // low bit of the condition.
341   void startExplicitITBlock(ARMCC::CondCodes Cond, unsigned Mask) {
342     assert(!inITBlock());
343     ITState.Cond = Cond;
344     ITState.Mask = Mask;
345     ITState.CurPosition = 0;
346     ITState.IsExplicit = true;
347     return;
348   }
349 
350   void Note(SMLoc L, const Twine &Msg, SMRange Range = None) {
351     return getParser().Note(L, Msg, Range);
352   }
353   bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) {
354     return getParser().Warning(L, Msg, Range);
355   }
356   bool Error(SMLoc L, const Twine &Msg, SMRange Range = None) {
357     return getParser().Error(L, Msg, Range);
358   }
359 
360   bool validatetLDMRegList(const MCInst &Inst, const OperandVector &Operands,
361                            unsigned ListNo, bool IsARPop = false);
362   bool validatetSTMRegList(const MCInst &Inst, const OperandVector &Operands,
363                            unsigned ListNo);
364 
365   int tryParseRegister();
366   bool tryParseRegisterWithWriteBack(OperandVector &);
367   int tryParseShiftRegister(OperandVector &);
368   bool parseRegisterList(OperandVector &);
369   bool parseMemory(OperandVector &);
370   bool parseOperand(OperandVector &, StringRef Mnemonic);
371   bool parsePrefix(ARMMCExpr::VariantKind &RefKind);
372   bool parseMemRegOffsetShift(ARM_AM::ShiftOpc &ShiftType,
373                               unsigned &ShiftAmount);
374   bool parseLiteralValues(unsigned Size, SMLoc L);
375   bool parseDirectiveThumb(SMLoc L);
376   bool parseDirectiveARM(SMLoc L);
377   bool parseDirectiveThumbFunc(SMLoc L);
378   bool parseDirectiveCode(SMLoc L);
379   bool parseDirectiveSyntax(SMLoc L);
380   bool parseDirectiveReq(StringRef Name, SMLoc L);
381   bool parseDirectiveUnreq(SMLoc L);
382   bool parseDirectiveArch(SMLoc L);
383   bool parseDirectiveEabiAttr(SMLoc L);
384   bool parseDirectiveCPU(SMLoc L);
385   bool parseDirectiveFPU(SMLoc L);
386   bool parseDirectiveFnStart(SMLoc L);
387   bool parseDirectiveFnEnd(SMLoc L);
388   bool parseDirectiveCantUnwind(SMLoc L);
389   bool parseDirectivePersonality(SMLoc L);
390   bool parseDirectiveHandlerData(SMLoc L);
391   bool parseDirectiveSetFP(SMLoc L);
392   bool parseDirectivePad(SMLoc L);
393   bool parseDirectiveRegSave(SMLoc L, bool IsVector);
394   bool parseDirectiveInst(SMLoc L, char Suffix = '\0');
395   bool parseDirectiveLtorg(SMLoc L);
396   bool parseDirectiveEven(SMLoc L);
397   bool parseDirectivePersonalityIndex(SMLoc L);
398   bool parseDirectiveUnwindRaw(SMLoc L);
399   bool parseDirectiveTLSDescSeq(SMLoc L);
400   bool parseDirectiveMovSP(SMLoc L);
401   bool parseDirectiveObjectArch(SMLoc L);
402   bool parseDirectiveArchExtension(SMLoc L);
403   bool parseDirectiveAlign(SMLoc L);
404   bool parseDirectiveThumbSet(SMLoc L);
405 
406   StringRef splitMnemonic(StringRef Mnemonic, unsigned &PredicationCode,
407                           bool &CarrySetting, unsigned &ProcessorIMod,
408                           StringRef &ITMask);
409   void getMnemonicAcceptInfo(StringRef Mnemonic, StringRef FullInst,
410                              bool &CanAcceptCarrySet,
411                              bool &CanAcceptPredicationCode);
412 
413   void tryConvertingToTwoOperandForm(StringRef Mnemonic, bool CarrySetting,
414                                      OperandVector &Operands);
415   bool isThumb() const {
416     // FIXME: Can tablegen auto-generate this?
417     return getSTI().getFeatureBits()[ARM::ModeThumb];
418   }
419   bool isThumbOne() const {
420     return isThumb() && !getSTI().getFeatureBits()[ARM::FeatureThumb2];
421   }
422   bool isThumbTwo() const {
423     return isThumb() && getSTI().getFeatureBits()[ARM::FeatureThumb2];
424   }
425   bool hasThumb() const {
426     return getSTI().getFeatureBits()[ARM::HasV4TOps];
427   }
428   bool hasThumb2() const {
429     return getSTI().getFeatureBits()[ARM::FeatureThumb2];
430   }
431   bool hasV6Ops() const {
432     return getSTI().getFeatureBits()[ARM::HasV6Ops];
433   }
434   bool hasV6T2Ops() const {
435     return getSTI().getFeatureBits()[ARM::HasV6T2Ops];
436   }
437   bool hasV6MOps() const {
438     return getSTI().getFeatureBits()[ARM::HasV6MOps];
439   }
440   bool hasV7Ops() const {
441     return getSTI().getFeatureBits()[ARM::HasV7Ops];
442   }
443   bool hasV8Ops() const {
444     return getSTI().getFeatureBits()[ARM::HasV8Ops];
445   }
446   bool hasV8MBaseline() const {
447     return getSTI().getFeatureBits()[ARM::HasV8MBaselineOps];
448   }
449   bool hasV8MMainline() const {
450     return getSTI().getFeatureBits()[ARM::HasV8MMainlineOps];
451   }
452   bool has8MSecExt() const {
453     return getSTI().getFeatureBits()[ARM::Feature8MSecExt];
454   }
455   bool hasARM() const {
456     return !getSTI().getFeatureBits()[ARM::FeatureNoARM];
457   }
458   bool hasDSP() const {
459     return getSTI().getFeatureBits()[ARM::FeatureDSP];
460   }
461   bool hasD16() const {
462     return getSTI().getFeatureBits()[ARM::FeatureD16];
463   }
464   bool hasV8_1aOps() const {
465     return getSTI().getFeatureBits()[ARM::HasV8_1aOps];
466   }
467   bool hasRAS() const {
468     return getSTI().getFeatureBits()[ARM::FeatureRAS];
469   }
470 
471   void SwitchMode() {
472     MCSubtargetInfo &STI = copySTI();
473     uint64_t FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
474     setAvailableFeatures(FB);
475   }
476   void FixModeAfterArchChange(bool WasThumb, SMLoc Loc);
477   bool isMClass() const {
478     return getSTI().getFeatureBits()[ARM::FeatureMClass];
479   }
480 
481   /// @name Auto-generated Match Functions
482   /// {
483 
484 #define GET_ASSEMBLER_HEADER
485 #include "ARMGenAsmMatcher.inc"
486 
487   /// }
488 
489   OperandMatchResultTy parseITCondCode(OperandVector &);
490   OperandMatchResultTy parseCoprocNumOperand(OperandVector &);
491   OperandMatchResultTy parseCoprocRegOperand(OperandVector &);
492   OperandMatchResultTy parseCoprocOptionOperand(OperandVector &);
493   OperandMatchResultTy parseMemBarrierOptOperand(OperandVector &);
494   OperandMatchResultTy parseInstSyncBarrierOptOperand(OperandVector &);
495   OperandMatchResultTy parseProcIFlagsOperand(OperandVector &);
496   OperandMatchResultTy parseMSRMaskOperand(OperandVector &);
497   OperandMatchResultTy parseBankedRegOperand(OperandVector &);
498   OperandMatchResultTy parsePKHImm(OperandVector &O, StringRef Op, int Low,
499                                    int High);
500   OperandMatchResultTy parsePKHLSLImm(OperandVector &O) {
501     return parsePKHImm(O, "lsl", 0, 31);
502   }
503   OperandMatchResultTy parsePKHASRImm(OperandVector &O) {
504     return parsePKHImm(O, "asr", 1, 32);
505   }
506   OperandMatchResultTy parseSetEndImm(OperandVector &);
507   OperandMatchResultTy parseShifterImm(OperandVector &);
508   OperandMatchResultTy parseRotImm(OperandVector &);
509   OperandMatchResultTy parseModImm(OperandVector &);
510   OperandMatchResultTy parseBitfield(OperandVector &);
511   OperandMatchResultTy parsePostIdxReg(OperandVector &);
512   OperandMatchResultTy parseAM3Offset(OperandVector &);
513   OperandMatchResultTy parseFPImm(OperandVector &);
514   OperandMatchResultTy parseVectorList(OperandVector &);
515   OperandMatchResultTy parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index,
516                                        SMLoc &EndLoc);
517 
518   // Asm Match Converter Methods
519   void cvtThumbMultiply(MCInst &Inst, const OperandVector &);
520   void cvtThumbBranches(MCInst &Inst, const OperandVector &);
521 
522   bool validateInstruction(MCInst &Inst, const OperandVector &Ops);
523   bool processInstruction(MCInst &Inst, const OperandVector &Ops, MCStreamer &Out);
524   bool shouldOmitCCOutOperand(StringRef Mnemonic, OperandVector &Operands);
525   bool shouldOmitPredicateOperand(StringRef Mnemonic, OperandVector &Operands);
526   bool isITBlockTerminator(MCInst &Inst) const;
527 
528 public:
529   enum ARMMatchResultTy {
530     Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
531     Match_RequiresNotITBlock,
532     Match_RequiresV6,
533     Match_RequiresThumb2,
534     Match_RequiresV8,
535     Match_RequiresFlagSetting,
536 #define GET_OPERAND_DIAGNOSTIC_TYPES
537 #include "ARMGenAsmMatcher.inc"
538 
539   };
540 
541   ARMAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
542                const MCInstrInfo &MII, const MCTargetOptions &Options)
543     : MCTargetAsmParser(Options, STI), MII(MII), UC(Parser) {
544     MCAsmParserExtension::Initialize(Parser);
545 
546     // Cache the MCRegisterInfo.
547     MRI = getContext().getRegisterInfo();
548 
549     // Initialize the set of available features.
550     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
551 
552     // Add build attributes based on the selected target.
553     if (AddBuildAttributes)
554       getTargetStreamer().emitTargetAttributes(STI);
555 
556     // Not in an ITBlock to start with.
557     ITState.CurPosition = ~0U;
558 
559     NextSymbolIsThumb = false;
560   }
561 
562   // Implementation of the MCTargetAsmParser interface:
563   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
564   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
565                         SMLoc NameLoc, OperandVector &Operands) override;
566   bool ParseDirective(AsmToken DirectiveID) override;
567 
568   unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
569                                       unsigned Kind) override;
570   unsigned checkTargetMatchPredicate(MCInst &Inst) override;
571 
572   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
573                                OperandVector &Operands, MCStreamer &Out,
574                                uint64_t &ErrorInfo,
575                                bool MatchingInlineAsm) override;
576   unsigned MatchInstruction(OperandVector &Operands, MCInst &Inst,
577                             uint64_t &ErrorInfo, bool MatchingInlineAsm,
578                             bool &EmitInITBlock, MCStreamer &Out);
579   void onLabelParsed(MCSymbol *Symbol) override;
580 };
581 } // end anonymous namespace
582 
583 namespace {
584 
585 /// ARMOperand - Instances of this class represent a parsed ARM machine
586 /// operand.
587 class ARMOperand : public MCParsedAsmOperand {
588   enum KindTy {
589     k_CondCode,
590     k_CCOut,
591     k_ITCondMask,
592     k_CoprocNum,
593     k_CoprocReg,
594     k_CoprocOption,
595     k_Immediate,
596     k_MemBarrierOpt,
597     k_InstSyncBarrierOpt,
598     k_Memory,
599     k_PostIndexRegister,
600     k_MSRMask,
601     k_BankedReg,
602     k_ProcIFlags,
603     k_VectorIndex,
604     k_Register,
605     k_RegisterList,
606     k_DPRRegisterList,
607     k_SPRRegisterList,
608     k_VectorList,
609     k_VectorListAllLanes,
610     k_VectorListIndexed,
611     k_ShiftedRegister,
612     k_ShiftedImmediate,
613     k_ShifterImmediate,
614     k_RotateImmediate,
615     k_ModifiedImmediate,
616     k_ConstantPoolImmediate,
617     k_BitfieldDescriptor,
618     k_Token,
619   } Kind;
620 
621   SMLoc StartLoc, EndLoc, AlignmentLoc;
622   SmallVector<unsigned, 8> Registers;
623 
624   struct CCOp {
625     ARMCC::CondCodes Val;
626   };
627 
628   struct CopOp {
629     unsigned Val;
630   };
631 
632   struct CoprocOptionOp {
633     unsigned Val;
634   };
635 
636   struct ITMaskOp {
637     unsigned Mask:4;
638   };
639 
640   struct MBOptOp {
641     ARM_MB::MemBOpt Val;
642   };
643 
644   struct ISBOptOp {
645     ARM_ISB::InstSyncBOpt Val;
646   };
647 
648   struct IFlagsOp {
649     ARM_PROC::IFlags Val;
650   };
651 
652   struct MMaskOp {
653     unsigned Val;
654   };
655 
656   struct BankedRegOp {
657     unsigned Val;
658   };
659 
660   struct TokOp {
661     const char *Data;
662     unsigned Length;
663   };
664 
665   struct RegOp {
666     unsigned RegNum;
667   };
668 
669   // A vector register list is a sequential list of 1 to 4 registers.
670   struct VectorListOp {
671     unsigned RegNum;
672     unsigned Count;
673     unsigned LaneIndex;
674     bool isDoubleSpaced;
675   };
676 
677   struct VectorIndexOp {
678     unsigned Val;
679   };
680 
681   struct ImmOp {
682     const MCExpr *Val;
683   };
684 
685   /// Combined record for all forms of ARM address expressions.
686   struct MemoryOp {
687     unsigned BaseRegNum;
688     // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
689     // was specified.
690     const MCConstantExpr *OffsetImm;  // Offset immediate value
691     unsigned OffsetRegNum;    // Offset register num, when OffsetImm == NULL
692     ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
693     unsigned ShiftImm;        // shift for OffsetReg.
694     unsigned Alignment;       // 0 = no alignment specified
695     // n = alignment in bytes (2, 4, 8, 16, or 32)
696     unsigned isNegative : 1;  // Negated OffsetReg? (~'U' bit)
697   };
698 
699   struct PostIdxRegOp {
700     unsigned RegNum;
701     bool isAdd;
702     ARM_AM::ShiftOpc ShiftTy;
703     unsigned ShiftImm;
704   };
705 
706   struct ShifterImmOp {
707     bool isASR;
708     unsigned Imm;
709   };
710 
711   struct RegShiftedRegOp {
712     ARM_AM::ShiftOpc ShiftTy;
713     unsigned SrcReg;
714     unsigned ShiftReg;
715     unsigned ShiftImm;
716   };
717 
718   struct RegShiftedImmOp {
719     ARM_AM::ShiftOpc ShiftTy;
720     unsigned SrcReg;
721     unsigned ShiftImm;
722   };
723 
724   struct RotImmOp {
725     unsigned Imm;
726   };
727 
728   struct ModImmOp {
729     unsigned Bits;
730     unsigned Rot;
731   };
732 
733   struct BitfieldOp {
734     unsigned LSB;
735     unsigned Width;
736   };
737 
738   union {
739     struct CCOp CC;
740     struct CopOp Cop;
741     struct CoprocOptionOp CoprocOption;
742     struct MBOptOp MBOpt;
743     struct ISBOptOp ISBOpt;
744     struct ITMaskOp ITMask;
745     struct IFlagsOp IFlags;
746     struct MMaskOp MMask;
747     struct BankedRegOp BankedReg;
748     struct TokOp Tok;
749     struct RegOp Reg;
750     struct VectorListOp VectorList;
751     struct VectorIndexOp VectorIndex;
752     struct ImmOp Imm;
753     struct MemoryOp Memory;
754     struct PostIdxRegOp PostIdxReg;
755     struct ShifterImmOp ShifterImm;
756     struct RegShiftedRegOp RegShiftedReg;
757     struct RegShiftedImmOp RegShiftedImm;
758     struct RotImmOp RotImm;
759     struct ModImmOp ModImm;
760     struct BitfieldOp Bitfield;
761   };
762 
763 public:
764   ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
765 
766   /// getStartLoc - Get the location of the first token of this operand.
767   SMLoc getStartLoc() const override { return StartLoc; }
768   /// getEndLoc - Get the location of the last token of this operand.
769   SMLoc getEndLoc() const override { return EndLoc; }
770   /// getLocRange - Get the range between the first and last token of this
771   /// operand.
772   SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
773 
774   /// getAlignmentLoc - Get the location of the Alignment token of this operand.
775   SMLoc getAlignmentLoc() const {
776     assert(Kind == k_Memory && "Invalid access!");
777     return AlignmentLoc;
778   }
779 
780   ARMCC::CondCodes getCondCode() const {
781     assert(Kind == k_CondCode && "Invalid access!");
782     return CC.Val;
783   }
784 
785   unsigned getCoproc() const {
786     assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
787     return Cop.Val;
788   }
789 
790   StringRef getToken() const {
791     assert(Kind == k_Token && "Invalid access!");
792     return StringRef(Tok.Data, Tok.Length);
793   }
794 
795   unsigned getReg() const override {
796     assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
797     return Reg.RegNum;
798   }
799 
800   const SmallVectorImpl<unsigned> &getRegList() const {
801     assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
802             Kind == k_SPRRegisterList) && "Invalid access!");
803     return Registers;
804   }
805 
806   const MCExpr *getImm() const {
807     assert(isImm() && "Invalid access!");
808     return Imm.Val;
809   }
810 
811   const MCExpr *getConstantPoolImm() const {
812     assert(isConstantPoolImm() && "Invalid access!");
813     return Imm.Val;
814   }
815 
816   unsigned getVectorIndex() const {
817     assert(Kind == k_VectorIndex && "Invalid access!");
818     return VectorIndex.Val;
819   }
820 
821   ARM_MB::MemBOpt getMemBarrierOpt() const {
822     assert(Kind == k_MemBarrierOpt && "Invalid access!");
823     return MBOpt.Val;
824   }
825 
826   ARM_ISB::InstSyncBOpt getInstSyncBarrierOpt() const {
827     assert(Kind == k_InstSyncBarrierOpt && "Invalid access!");
828     return ISBOpt.Val;
829   }
830 
831   ARM_PROC::IFlags getProcIFlags() const {
832     assert(Kind == k_ProcIFlags && "Invalid access!");
833     return IFlags.Val;
834   }
835 
836   unsigned getMSRMask() const {
837     assert(Kind == k_MSRMask && "Invalid access!");
838     return MMask.Val;
839   }
840 
841   unsigned getBankedReg() const {
842     assert(Kind == k_BankedReg && "Invalid access!");
843     return BankedReg.Val;
844   }
845 
846   bool isCoprocNum() const { return Kind == k_CoprocNum; }
847   bool isCoprocReg() const { return Kind == k_CoprocReg; }
848   bool isCoprocOption() const { return Kind == k_CoprocOption; }
849   bool isCondCode() const { return Kind == k_CondCode; }
850   bool isCCOut() const { return Kind == k_CCOut; }
851   bool isITMask() const { return Kind == k_ITCondMask; }
852   bool isITCondCode() const { return Kind == k_CondCode; }
853   bool isImm() const override {
854     return Kind == k_Immediate;
855   }
856 
857   bool isARMBranchTarget() const {
858     if (!isImm()) return false;
859 
860     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()))
861       return CE->getValue() % 4 == 0;
862     return true;
863   }
864 
865 
866   bool isThumbBranchTarget() const {
867     if (!isImm()) return false;
868 
869     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()))
870       return CE->getValue() % 2 == 0;
871     return true;
872   }
873 
874   // checks whether this operand is an unsigned offset which fits is a field
875   // of specified width and scaled by a specific number of bits
876   template<unsigned width, unsigned scale>
877   bool isUnsignedOffset() const {
878     if (!isImm()) return false;
879     if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
880     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
881       int64_t Val = CE->getValue();
882       int64_t Align = 1LL << scale;
883       int64_t Max = Align * ((1LL << width) - 1);
884       return ((Val % Align) == 0) && (Val >= 0) && (Val <= Max);
885     }
886     return false;
887   }
888   // checks whether this operand is an signed offset which fits is a field
889   // of specified width and scaled by a specific number of bits
890   template<unsigned width, unsigned scale>
891   bool isSignedOffset() const {
892     if (!isImm()) return false;
893     if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
894     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
895       int64_t Val = CE->getValue();
896       int64_t Align = 1LL << scale;
897       int64_t Max = Align * ((1LL << (width-1)) - 1);
898       int64_t Min = -Align * (1LL << (width-1));
899       return ((Val % Align) == 0) && (Val >= Min) && (Val <= Max);
900     }
901     return false;
902   }
903 
904   // checks whether this operand is a memory operand computed as an offset
905   // applied to PC. the offset may have 8 bits of magnitude and is represented
906   // with two bits of shift. textually it may be either [pc, #imm], #imm or
907   // relocable expression...
908   bool isThumbMemPC() const {
909     int64_t Val = 0;
910     if (isImm()) {
911       if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
912       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val);
913       if (!CE) return false;
914       Val = CE->getValue();
915     }
916     else if (isMem()) {
917       if(!Memory.OffsetImm || Memory.OffsetRegNum) return false;
918       if(Memory.BaseRegNum != ARM::PC) return false;
919       Val = Memory.OffsetImm->getValue();
920     }
921     else return false;
922     return ((Val % 4) == 0) && (Val >= 0) && (Val <= 1020);
923   }
924   bool isFPImm() const {
925     if (!isImm()) return false;
926     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
927     if (!CE) return false;
928     int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
929     return Val != -1;
930   }
931 
932   template<int64_t N, int64_t M>
933   bool isImmediate() const {
934     if (!isImm()) return false;
935     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
936     if (!CE) return false;
937     int64_t Value = CE->getValue();
938     return Value >= N && Value <= M;
939   }
940   template<int64_t N, int64_t M>
941   bool isImmediateS4() const {
942     if (!isImm()) return false;
943     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
944     if (!CE) return false;
945     int64_t Value = CE->getValue();
946     return ((Value & 3) == 0) && Value >= N && Value <= M;
947   }
948   bool isFBits16() const {
949     return isImmediate<0, 17>();
950   }
951   bool isFBits32() const {
952     return isImmediate<1, 33>();
953   }
954   bool isImm8s4() const {
955     return isImmediateS4<-1020, 1020>();
956   }
957   bool isImm0_1020s4() const {
958     return isImmediateS4<0, 1020>();
959   }
960   bool isImm0_508s4() const {
961     return isImmediateS4<0, 508>();
962   }
963   bool isImm0_508s4Neg() const {
964     if (!isImm()) return false;
965     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
966     if (!CE) return false;
967     int64_t Value = -CE->getValue();
968     // explicitly exclude zero. we want that to use the normal 0_508 version.
969     return ((Value & 3) == 0) && Value > 0 && Value <= 508;
970   }
971   bool isImm0_4095Neg() const {
972     if (!isImm()) return false;
973     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
974     if (!CE) return false;
975     int64_t Value = -CE->getValue();
976     return Value > 0 && Value < 4096;
977   }
978   bool isImm0_7() const {
979     return isImmediate<0, 7>();
980   }
981   bool isImm1_16() const {
982     return isImmediate<1, 16>();
983   }
984   bool isImm1_32() const {
985     return isImmediate<1, 32>();
986   }
987   bool isImm8_255() const {
988     return isImmediate<8, 255>();
989   }
990   bool isImm256_65535Expr() const {
991     if (!isImm()) return false;
992     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
993     // If it's not a constant expression, it'll generate a fixup and be
994     // handled later.
995     if (!CE) return true;
996     int64_t Value = CE->getValue();
997     return Value >= 256 && Value < 65536;
998   }
999   bool isImm0_65535Expr() const {
1000     if (!isImm()) return false;
1001     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1002     // If it's not a constant expression, it'll generate a fixup and be
1003     // handled later.
1004     if (!CE) return true;
1005     int64_t Value = CE->getValue();
1006     return Value >= 0 && Value < 65536;
1007   }
1008   bool isImm24bit() const {
1009     return isImmediate<0, 0xffffff + 1>();
1010   }
1011   bool isImmThumbSR() const {
1012     return isImmediate<1, 33>();
1013   }
1014   bool isPKHLSLImm() const {
1015     return isImmediate<0, 32>();
1016   }
1017   bool isPKHASRImm() const {
1018     return isImmediate<0, 33>();
1019   }
1020   bool isAdrLabel() const {
1021     // If we have an immediate that's not a constant, treat it as a label
1022     // reference needing a fixup.
1023     if (isImm() && !isa<MCConstantExpr>(getImm()))
1024       return true;
1025 
1026     // If it is a constant, it must fit into a modified immediate encoding.
1027     if (!isImm()) return false;
1028     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1029     if (!CE) return false;
1030     int64_t Value = CE->getValue();
1031     return (ARM_AM::getSOImmVal(Value) != -1 ||
1032             ARM_AM::getSOImmVal(-Value) != -1);
1033   }
1034   bool isT2SOImm() const {
1035     // If we have an immediate that's not a constant, treat it as an expression
1036     // needing a fixup.
1037     if (isImm() && !isa<MCConstantExpr>(getImm())) {
1038       // We want to avoid matching :upper16: and :lower16: as we want these
1039       // expressions to match in isImm0_65535Expr()
1040       const ARMMCExpr *ARM16Expr = dyn_cast<ARMMCExpr>(getImm());
1041       return (!ARM16Expr || (ARM16Expr->getKind() != ARMMCExpr::VK_ARM_HI16 &&
1042                              ARM16Expr->getKind() != ARMMCExpr::VK_ARM_LO16));
1043     }
1044     if (!isImm()) return false;
1045     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1046     if (!CE) return false;
1047     int64_t Value = CE->getValue();
1048     return ARM_AM::getT2SOImmVal(Value) != -1;
1049   }
1050   bool isT2SOImmNot() const {
1051     if (!isImm()) return false;
1052     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1053     if (!CE) return false;
1054     int64_t Value = CE->getValue();
1055     return ARM_AM::getT2SOImmVal(Value) == -1 &&
1056       ARM_AM::getT2SOImmVal(~Value) != -1;
1057   }
1058   bool isT2SOImmNeg() const {
1059     if (!isImm()) return false;
1060     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1061     if (!CE) return false;
1062     int64_t Value = CE->getValue();
1063     // Only use this when not representable as a plain so_imm.
1064     return ARM_AM::getT2SOImmVal(Value) == -1 &&
1065       ARM_AM::getT2SOImmVal(-Value) != -1;
1066   }
1067   bool isSetEndImm() const {
1068     if (!isImm()) return false;
1069     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1070     if (!CE) return false;
1071     int64_t Value = CE->getValue();
1072     return Value == 1 || Value == 0;
1073   }
1074   bool isReg() const override { return Kind == k_Register; }
1075   bool isRegList() const { return Kind == k_RegisterList; }
1076   bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
1077   bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
1078   bool isToken() const override { return Kind == k_Token; }
1079   bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
1080   bool isInstSyncBarrierOpt() const { return Kind == k_InstSyncBarrierOpt; }
1081   bool isMem() const override { return Kind == k_Memory; }
1082   bool isShifterImm() const { return Kind == k_ShifterImmediate; }
1083   bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
1084   bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
1085   bool isRotImm() const { return Kind == k_RotateImmediate; }
1086   bool isModImm() const { return Kind == k_ModifiedImmediate; }
1087   bool isModImmNot() const {
1088     if (!isImm()) return false;
1089     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1090     if (!CE) return false;
1091     int64_t Value = CE->getValue();
1092     return ARM_AM::getSOImmVal(~Value) != -1;
1093   }
1094   bool isModImmNeg() const {
1095     if (!isImm()) return false;
1096     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1097     if (!CE) return false;
1098     int64_t Value = CE->getValue();
1099     return ARM_AM::getSOImmVal(Value) == -1 &&
1100       ARM_AM::getSOImmVal(-Value) != -1;
1101   }
1102   bool isThumbModImmNeg1_7() const {
1103     if (!isImm()) return false;
1104     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1105     if (!CE) return false;
1106     int32_t Value = -(int32_t)CE->getValue();
1107     return 0 < Value && Value < 8;
1108   }
1109   bool isThumbModImmNeg8_255() const {
1110     if (!isImm()) return false;
1111     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1112     if (!CE) return false;
1113     int32_t Value = -(int32_t)CE->getValue();
1114     return 7 < Value && Value < 256;
1115   }
1116   bool isConstantPoolImm() const { return Kind == k_ConstantPoolImmediate; }
1117   bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
1118   bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
1119   bool isPostIdxReg() const {
1120     return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy ==ARM_AM::no_shift;
1121   }
1122   bool isMemNoOffset(bool alignOK = false, unsigned Alignment = 0) const {
1123     if (!isMem())
1124       return false;
1125     // No offset of any kind.
1126     return Memory.OffsetRegNum == 0 && Memory.OffsetImm == nullptr &&
1127      (alignOK || Memory.Alignment == Alignment);
1128   }
1129   bool isMemPCRelImm12() const {
1130     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1131       return false;
1132     // Base register must be PC.
1133     if (Memory.BaseRegNum != ARM::PC)
1134       return false;
1135     // Immediate offset in range [-4095, 4095].
1136     if (!Memory.OffsetImm) return true;
1137     int64_t Val = Memory.OffsetImm->getValue();
1138     return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
1139   }
1140   bool isAlignedMemory() const {
1141     return isMemNoOffset(true);
1142   }
1143   bool isAlignedMemoryNone() const {
1144     return isMemNoOffset(false, 0);
1145   }
1146   bool isDupAlignedMemoryNone() const {
1147     return isMemNoOffset(false, 0);
1148   }
1149   bool isAlignedMemory16() const {
1150     if (isMemNoOffset(false, 2)) // alignment in bytes for 16-bits is 2.
1151       return true;
1152     return isMemNoOffset(false, 0);
1153   }
1154   bool isDupAlignedMemory16() const {
1155     if (isMemNoOffset(false, 2)) // alignment in bytes for 16-bits is 2.
1156       return true;
1157     return isMemNoOffset(false, 0);
1158   }
1159   bool isAlignedMemory32() const {
1160     if (isMemNoOffset(false, 4)) // alignment in bytes for 32-bits is 4.
1161       return true;
1162     return isMemNoOffset(false, 0);
1163   }
1164   bool isDupAlignedMemory32() const {
1165     if (isMemNoOffset(false, 4)) // alignment in bytes for 32-bits is 4.
1166       return true;
1167     return isMemNoOffset(false, 0);
1168   }
1169   bool isAlignedMemory64() const {
1170     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1171       return true;
1172     return isMemNoOffset(false, 0);
1173   }
1174   bool isDupAlignedMemory64() const {
1175     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1176       return true;
1177     return isMemNoOffset(false, 0);
1178   }
1179   bool isAlignedMemory64or128() const {
1180     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1181       return true;
1182     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1183       return true;
1184     return isMemNoOffset(false, 0);
1185   }
1186   bool isDupAlignedMemory64or128() const {
1187     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1188       return true;
1189     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1190       return true;
1191     return isMemNoOffset(false, 0);
1192   }
1193   bool isAlignedMemory64or128or256() const {
1194     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1195       return true;
1196     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1197       return true;
1198     if (isMemNoOffset(false, 32)) // alignment in bytes for 256-bits is 32.
1199       return true;
1200     return isMemNoOffset(false, 0);
1201   }
1202   bool isAddrMode2() const {
1203     if (!isMem() || Memory.Alignment != 0) return false;
1204     // Check for register offset.
1205     if (Memory.OffsetRegNum) return true;
1206     // Immediate offset in range [-4095, 4095].
1207     if (!Memory.OffsetImm) return true;
1208     int64_t Val = Memory.OffsetImm->getValue();
1209     return Val > -4096 && Val < 4096;
1210   }
1211   bool isAM2OffsetImm() const {
1212     if (!isImm()) return false;
1213     // Immediate offset in range [-4095, 4095].
1214     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1215     if (!CE) return false;
1216     int64_t Val = CE->getValue();
1217     return (Val == INT32_MIN) || (Val > -4096 && Val < 4096);
1218   }
1219   bool isAddrMode3() const {
1220     // If we have an immediate that's not a constant, treat it as a label
1221     // reference needing a fixup. If it is a constant, it's something else
1222     // and we reject it.
1223     if (isImm() && !isa<MCConstantExpr>(getImm()))
1224       return true;
1225     if (!isMem() || Memory.Alignment != 0) return false;
1226     // No shifts are legal for AM3.
1227     if (Memory.ShiftType != ARM_AM::no_shift) return false;
1228     // Check for register offset.
1229     if (Memory.OffsetRegNum) return true;
1230     // Immediate offset in range [-255, 255].
1231     if (!Memory.OffsetImm) return true;
1232     int64_t Val = Memory.OffsetImm->getValue();
1233     // The #-0 offset is encoded as INT32_MIN, and we have to check
1234     // for this too.
1235     return (Val > -256 && Val < 256) || Val == INT32_MIN;
1236   }
1237   bool isAM3Offset() const {
1238     if (Kind != k_Immediate && Kind != k_PostIndexRegister)
1239       return false;
1240     if (Kind == k_PostIndexRegister)
1241       return PostIdxReg.ShiftTy == ARM_AM::no_shift;
1242     // Immediate offset in range [-255, 255].
1243     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1244     if (!CE) return false;
1245     int64_t Val = CE->getValue();
1246     // Special case, #-0 is INT32_MIN.
1247     return (Val > -256 && Val < 256) || Val == INT32_MIN;
1248   }
1249   bool isAddrMode5() const {
1250     // If we have an immediate that's not a constant, treat it as a label
1251     // reference needing a fixup. If it is a constant, it's something else
1252     // and we reject it.
1253     if (isImm() && !isa<MCConstantExpr>(getImm()))
1254       return true;
1255     if (!isMem() || Memory.Alignment != 0) return false;
1256     // Check for register offset.
1257     if (Memory.OffsetRegNum) return false;
1258     // Immediate offset in range [-1020, 1020] and a multiple of 4.
1259     if (!Memory.OffsetImm) return true;
1260     int64_t Val = Memory.OffsetImm->getValue();
1261     return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
1262       Val == INT32_MIN;
1263   }
1264   bool isAddrMode5FP16() const {
1265     // If we have an immediate that's not a constant, treat it as a label
1266     // reference needing a fixup. If it is a constant, it's something else
1267     // and we reject it.
1268     if (isImm() && !isa<MCConstantExpr>(getImm()))
1269       return true;
1270     if (!isMem() || Memory.Alignment != 0) return false;
1271     // Check for register offset.
1272     if (Memory.OffsetRegNum) return false;
1273     // Immediate offset in range [-510, 510] and a multiple of 2.
1274     if (!Memory.OffsetImm) return true;
1275     int64_t Val = Memory.OffsetImm->getValue();
1276     return (Val >= -510 && Val <= 510 && ((Val & 1) == 0)) || Val == INT32_MIN;
1277   }
1278   bool isMemTBB() const {
1279     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1280         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
1281       return false;
1282     return true;
1283   }
1284   bool isMemTBH() const {
1285     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1286         Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
1287         Memory.Alignment != 0 )
1288       return false;
1289     return true;
1290   }
1291   bool isMemRegOffset() const {
1292     if (!isMem() || !Memory.OffsetRegNum || Memory.Alignment != 0)
1293       return false;
1294     return true;
1295   }
1296   bool isT2MemRegOffset() const {
1297     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1298         Memory.Alignment != 0 || Memory.BaseRegNum == ARM::PC)
1299       return false;
1300     // Only lsl #{0, 1, 2, 3} allowed.
1301     if (Memory.ShiftType == ARM_AM::no_shift)
1302       return true;
1303     if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
1304       return false;
1305     return true;
1306   }
1307   bool isMemThumbRR() const {
1308     // Thumb reg+reg addressing is simple. Just two registers, a base and
1309     // an offset. No shifts, negations or any other complicating factors.
1310     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1311         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
1312       return false;
1313     return isARMLowRegister(Memory.BaseRegNum) &&
1314       (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
1315   }
1316   bool isMemThumbRIs4() const {
1317     if (!isMem() || Memory.OffsetRegNum != 0 ||
1318         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1319       return false;
1320     // Immediate offset, multiple of 4 in range [0, 124].
1321     if (!Memory.OffsetImm) return true;
1322     int64_t Val = Memory.OffsetImm->getValue();
1323     return Val >= 0 && Val <= 124 && (Val % 4) == 0;
1324   }
1325   bool isMemThumbRIs2() const {
1326     if (!isMem() || Memory.OffsetRegNum != 0 ||
1327         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1328       return false;
1329     // Immediate offset, multiple of 4 in range [0, 62].
1330     if (!Memory.OffsetImm) return true;
1331     int64_t Val = Memory.OffsetImm->getValue();
1332     return Val >= 0 && Val <= 62 && (Val % 2) == 0;
1333   }
1334   bool isMemThumbRIs1() const {
1335     if (!isMem() || Memory.OffsetRegNum != 0 ||
1336         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1337       return false;
1338     // Immediate offset in range [0, 31].
1339     if (!Memory.OffsetImm) return true;
1340     int64_t Val = Memory.OffsetImm->getValue();
1341     return Val >= 0 && Val <= 31;
1342   }
1343   bool isMemThumbSPI() const {
1344     if (!isMem() || Memory.OffsetRegNum != 0 ||
1345         Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
1346       return false;
1347     // Immediate offset, multiple of 4 in range [0, 1020].
1348     if (!Memory.OffsetImm) return true;
1349     int64_t Val = Memory.OffsetImm->getValue();
1350     return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
1351   }
1352   bool isMemImm8s4Offset() const {
1353     // If we have an immediate that's not a constant, treat it as a label
1354     // reference needing a fixup. If it is a constant, it's something else
1355     // and we reject it.
1356     if (isImm() && !isa<MCConstantExpr>(getImm()))
1357       return true;
1358     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1359       return false;
1360     // Immediate offset a multiple of 4 in range [-1020, 1020].
1361     if (!Memory.OffsetImm) return true;
1362     int64_t Val = Memory.OffsetImm->getValue();
1363     // Special case, #-0 is INT32_MIN.
1364     return (Val >= -1020 && Val <= 1020 && (Val & 3) == 0) || Val == INT32_MIN;
1365   }
1366   bool isMemImm0_1020s4Offset() const {
1367     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1368       return false;
1369     // Immediate offset a multiple of 4 in range [0, 1020].
1370     if (!Memory.OffsetImm) return true;
1371     int64_t Val = Memory.OffsetImm->getValue();
1372     return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
1373   }
1374   bool isMemImm8Offset() const {
1375     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1376       return false;
1377     // Base reg of PC isn't allowed for these encodings.
1378     if (Memory.BaseRegNum == ARM::PC) return false;
1379     // Immediate offset in range [-255, 255].
1380     if (!Memory.OffsetImm) return true;
1381     int64_t Val = Memory.OffsetImm->getValue();
1382     return (Val == INT32_MIN) || (Val > -256 && Val < 256);
1383   }
1384   bool isMemPosImm8Offset() const {
1385     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1386       return false;
1387     // Immediate offset in range [0, 255].
1388     if (!Memory.OffsetImm) return true;
1389     int64_t Val = Memory.OffsetImm->getValue();
1390     return Val >= 0 && Val < 256;
1391   }
1392   bool isMemNegImm8Offset() const {
1393     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1394       return false;
1395     // Base reg of PC isn't allowed for these encodings.
1396     if (Memory.BaseRegNum == ARM::PC) return false;
1397     // Immediate offset in range [-255, -1].
1398     if (!Memory.OffsetImm) return false;
1399     int64_t Val = Memory.OffsetImm->getValue();
1400     return (Val == INT32_MIN) || (Val > -256 && Val < 0);
1401   }
1402   bool isMemUImm12Offset() const {
1403     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1404       return false;
1405     // Immediate offset in range [0, 4095].
1406     if (!Memory.OffsetImm) return true;
1407     int64_t Val = Memory.OffsetImm->getValue();
1408     return (Val >= 0 && Val < 4096);
1409   }
1410   bool isMemImm12Offset() const {
1411     // If we have an immediate that's not a constant, treat it as a label
1412     // reference needing a fixup. If it is a constant, it's something else
1413     // and we reject it.
1414 
1415     if (isImm() && !isa<MCConstantExpr>(getImm()))
1416       return true;
1417 
1418     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1419       return false;
1420     // Immediate offset in range [-4095, 4095].
1421     if (!Memory.OffsetImm) return true;
1422     int64_t Val = Memory.OffsetImm->getValue();
1423     return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
1424   }
1425   bool isConstPoolAsmImm() const {
1426     // Delay processing of Constant Pool Immediate, this will turn into
1427     // a constant. Match no other operand
1428     return (isConstantPoolImm());
1429   }
1430   bool isPostIdxImm8() const {
1431     if (!isImm()) return false;
1432     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1433     if (!CE) return false;
1434     int64_t Val = CE->getValue();
1435     return (Val > -256 && Val < 256) || (Val == INT32_MIN);
1436   }
1437   bool isPostIdxImm8s4() const {
1438     if (!isImm()) return false;
1439     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1440     if (!CE) return false;
1441     int64_t Val = CE->getValue();
1442     return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
1443       (Val == INT32_MIN);
1444   }
1445 
1446   bool isMSRMask() const { return Kind == k_MSRMask; }
1447   bool isBankedReg() const { return Kind == k_BankedReg; }
1448   bool isProcIFlags() const { return Kind == k_ProcIFlags; }
1449 
1450   // NEON operands.
1451   bool isSingleSpacedVectorList() const {
1452     return Kind == k_VectorList && !VectorList.isDoubleSpaced;
1453   }
1454   bool isDoubleSpacedVectorList() const {
1455     return Kind == k_VectorList && VectorList.isDoubleSpaced;
1456   }
1457   bool isVecListOneD() const {
1458     if (!isSingleSpacedVectorList()) return false;
1459     return VectorList.Count == 1;
1460   }
1461 
1462   bool isVecListDPair() const {
1463     if (!isSingleSpacedVectorList()) return false;
1464     return (ARMMCRegisterClasses[ARM::DPairRegClassID]
1465               .contains(VectorList.RegNum));
1466   }
1467 
1468   bool isVecListThreeD() const {
1469     if (!isSingleSpacedVectorList()) return false;
1470     return VectorList.Count == 3;
1471   }
1472 
1473   bool isVecListFourD() const {
1474     if (!isSingleSpacedVectorList()) return false;
1475     return VectorList.Count == 4;
1476   }
1477 
1478   bool isVecListDPairSpaced() const {
1479     if (Kind != k_VectorList) return false;
1480     if (isSingleSpacedVectorList()) return false;
1481     return (ARMMCRegisterClasses[ARM::DPairSpcRegClassID]
1482               .contains(VectorList.RegNum));
1483   }
1484 
1485   bool isVecListThreeQ() const {
1486     if (!isDoubleSpacedVectorList()) return false;
1487     return VectorList.Count == 3;
1488   }
1489 
1490   bool isVecListFourQ() const {
1491     if (!isDoubleSpacedVectorList()) return false;
1492     return VectorList.Count == 4;
1493   }
1494 
1495   bool isSingleSpacedVectorAllLanes() const {
1496     return Kind == k_VectorListAllLanes && !VectorList.isDoubleSpaced;
1497   }
1498   bool isDoubleSpacedVectorAllLanes() const {
1499     return Kind == k_VectorListAllLanes && VectorList.isDoubleSpaced;
1500   }
1501   bool isVecListOneDAllLanes() const {
1502     if (!isSingleSpacedVectorAllLanes()) return false;
1503     return VectorList.Count == 1;
1504   }
1505 
1506   bool isVecListDPairAllLanes() const {
1507     if (!isSingleSpacedVectorAllLanes()) return false;
1508     return (ARMMCRegisterClasses[ARM::DPairRegClassID]
1509               .contains(VectorList.RegNum));
1510   }
1511 
1512   bool isVecListDPairSpacedAllLanes() const {
1513     if (!isDoubleSpacedVectorAllLanes()) return false;
1514     return VectorList.Count == 2;
1515   }
1516 
1517   bool isVecListThreeDAllLanes() const {
1518     if (!isSingleSpacedVectorAllLanes()) return false;
1519     return VectorList.Count == 3;
1520   }
1521 
1522   bool isVecListThreeQAllLanes() const {
1523     if (!isDoubleSpacedVectorAllLanes()) return false;
1524     return VectorList.Count == 3;
1525   }
1526 
1527   bool isVecListFourDAllLanes() const {
1528     if (!isSingleSpacedVectorAllLanes()) return false;
1529     return VectorList.Count == 4;
1530   }
1531 
1532   bool isVecListFourQAllLanes() const {
1533     if (!isDoubleSpacedVectorAllLanes()) return false;
1534     return VectorList.Count == 4;
1535   }
1536 
1537   bool isSingleSpacedVectorIndexed() const {
1538     return Kind == k_VectorListIndexed && !VectorList.isDoubleSpaced;
1539   }
1540   bool isDoubleSpacedVectorIndexed() const {
1541     return Kind == k_VectorListIndexed && VectorList.isDoubleSpaced;
1542   }
1543   bool isVecListOneDByteIndexed() const {
1544     if (!isSingleSpacedVectorIndexed()) return false;
1545     return VectorList.Count == 1 && VectorList.LaneIndex <= 7;
1546   }
1547 
1548   bool isVecListOneDHWordIndexed() const {
1549     if (!isSingleSpacedVectorIndexed()) return false;
1550     return VectorList.Count == 1 && VectorList.LaneIndex <= 3;
1551   }
1552 
1553   bool isVecListOneDWordIndexed() const {
1554     if (!isSingleSpacedVectorIndexed()) return false;
1555     return VectorList.Count == 1 && VectorList.LaneIndex <= 1;
1556   }
1557 
1558   bool isVecListTwoDByteIndexed() const {
1559     if (!isSingleSpacedVectorIndexed()) return false;
1560     return VectorList.Count == 2 && VectorList.LaneIndex <= 7;
1561   }
1562 
1563   bool isVecListTwoDHWordIndexed() const {
1564     if (!isSingleSpacedVectorIndexed()) return false;
1565     return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
1566   }
1567 
1568   bool isVecListTwoQWordIndexed() const {
1569     if (!isDoubleSpacedVectorIndexed()) return false;
1570     return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
1571   }
1572 
1573   bool isVecListTwoQHWordIndexed() const {
1574     if (!isDoubleSpacedVectorIndexed()) return false;
1575     return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
1576   }
1577 
1578   bool isVecListTwoDWordIndexed() const {
1579     if (!isSingleSpacedVectorIndexed()) return false;
1580     return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
1581   }
1582 
1583   bool isVecListThreeDByteIndexed() const {
1584     if (!isSingleSpacedVectorIndexed()) return false;
1585     return VectorList.Count == 3 && VectorList.LaneIndex <= 7;
1586   }
1587 
1588   bool isVecListThreeDHWordIndexed() const {
1589     if (!isSingleSpacedVectorIndexed()) return false;
1590     return VectorList.Count == 3 && VectorList.LaneIndex <= 3;
1591   }
1592 
1593   bool isVecListThreeQWordIndexed() const {
1594     if (!isDoubleSpacedVectorIndexed()) return false;
1595     return VectorList.Count == 3 && VectorList.LaneIndex <= 1;
1596   }
1597 
1598   bool isVecListThreeQHWordIndexed() const {
1599     if (!isDoubleSpacedVectorIndexed()) return false;
1600     return VectorList.Count == 3 && VectorList.LaneIndex <= 3;
1601   }
1602 
1603   bool isVecListThreeDWordIndexed() const {
1604     if (!isSingleSpacedVectorIndexed()) return false;
1605     return VectorList.Count == 3 && VectorList.LaneIndex <= 1;
1606   }
1607 
1608   bool isVecListFourDByteIndexed() const {
1609     if (!isSingleSpacedVectorIndexed()) return false;
1610     return VectorList.Count == 4 && VectorList.LaneIndex <= 7;
1611   }
1612 
1613   bool isVecListFourDHWordIndexed() const {
1614     if (!isSingleSpacedVectorIndexed()) return false;
1615     return VectorList.Count == 4 && VectorList.LaneIndex <= 3;
1616   }
1617 
1618   bool isVecListFourQWordIndexed() const {
1619     if (!isDoubleSpacedVectorIndexed()) return false;
1620     return VectorList.Count == 4 && VectorList.LaneIndex <= 1;
1621   }
1622 
1623   bool isVecListFourQHWordIndexed() const {
1624     if (!isDoubleSpacedVectorIndexed()) return false;
1625     return VectorList.Count == 4 && VectorList.LaneIndex <= 3;
1626   }
1627 
1628   bool isVecListFourDWordIndexed() const {
1629     if (!isSingleSpacedVectorIndexed()) return false;
1630     return VectorList.Count == 4 && VectorList.LaneIndex <= 1;
1631   }
1632 
1633   bool isVectorIndex8() const {
1634     if (Kind != k_VectorIndex) return false;
1635     return VectorIndex.Val < 8;
1636   }
1637   bool isVectorIndex16() const {
1638     if (Kind != k_VectorIndex) return false;
1639     return VectorIndex.Val < 4;
1640   }
1641   bool isVectorIndex32() const {
1642     if (Kind != k_VectorIndex) return false;
1643     return VectorIndex.Val < 2;
1644   }
1645 
1646   bool isNEONi8splat() const {
1647     if (!isImm()) return false;
1648     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1649     // Must be a constant.
1650     if (!CE) return false;
1651     int64_t Value = CE->getValue();
1652     // i8 value splatted across 8 bytes. The immediate is just the 8 byte
1653     // value.
1654     return Value >= 0 && Value < 256;
1655   }
1656 
1657   bool isNEONi16splat() const {
1658     if (isNEONByteReplicate(2))
1659       return false; // Leave that for bytes replication and forbid by default.
1660     if (!isImm())
1661       return false;
1662     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1663     // Must be a constant.
1664     if (!CE) return false;
1665     unsigned Value = CE->getValue();
1666     return ARM_AM::isNEONi16splat(Value);
1667   }
1668 
1669   bool isNEONi16splatNot() const {
1670     if (!isImm())
1671       return false;
1672     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1673     // Must be a constant.
1674     if (!CE) return false;
1675     unsigned Value = CE->getValue();
1676     return ARM_AM::isNEONi16splat(~Value & 0xffff);
1677   }
1678 
1679   bool isNEONi32splat() const {
1680     if (isNEONByteReplicate(4))
1681       return false; // Leave that for bytes replication and forbid by default.
1682     if (!isImm())
1683       return false;
1684     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1685     // Must be a constant.
1686     if (!CE) return false;
1687     unsigned Value = CE->getValue();
1688     return ARM_AM::isNEONi32splat(Value);
1689   }
1690 
1691   bool isNEONi32splatNot() const {
1692     if (!isImm())
1693       return false;
1694     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1695     // Must be a constant.
1696     if (!CE) return false;
1697     unsigned Value = CE->getValue();
1698     return ARM_AM::isNEONi32splat(~Value);
1699   }
1700 
1701   bool isNEONByteReplicate(unsigned NumBytes) const {
1702     if (!isImm())
1703       return false;
1704     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1705     // Must be a constant.
1706     if (!CE)
1707       return false;
1708     int64_t Value = CE->getValue();
1709     if (!Value)
1710       return false; // Don't bother with zero.
1711 
1712     unsigned char B = Value & 0xff;
1713     for (unsigned i = 1; i < NumBytes; ++i) {
1714       Value >>= 8;
1715       if ((Value & 0xff) != B)
1716         return false;
1717     }
1718     return true;
1719   }
1720   bool isNEONi16ByteReplicate() const { return isNEONByteReplicate(2); }
1721   bool isNEONi32ByteReplicate() const { return isNEONByteReplicate(4); }
1722   bool isNEONi32vmov() const {
1723     if (isNEONByteReplicate(4))
1724       return false; // Let it to be classified as byte-replicate case.
1725     if (!isImm())
1726       return false;
1727     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1728     // Must be a constant.
1729     if (!CE)
1730       return false;
1731     int64_t Value = CE->getValue();
1732     // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1733     // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1734     // FIXME: This is probably wrong and a copy and paste from previous example
1735     return (Value >= 0 && Value < 256) ||
1736       (Value >= 0x0100 && Value <= 0xff00) ||
1737       (Value >= 0x010000 && Value <= 0xff0000) ||
1738       (Value >= 0x01000000 && Value <= 0xff000000) ||
1739       (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1740       (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1741   }
1742   bool isNEONi32vmovNeg() const {
1743     if (!isImm()) return false;
1744     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1745     // Must be a constant.
1746     if (!CE) return false;
1747     int64_t Value = ~CE->getValue();
1748     // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1749     // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1750     // FIXME: This is probably wrong and a copy and paste from previous example
1751     return (Value >= 0 && Value < 256) ||
1752       (Value >= 0x0100 && Value <= 0xff00) ||
1753       (Value >= 0x010000 && Value <= 0xff0000) ||
1754       (Value >= 0x01000000 && Value <= 0xff000000) ||
1755       (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1756       (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1757   }
1758 
1759   bool isNEONi64splat() const {
1760     if (!isImm()) return false;
1761     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1762     // Must be a constant.
1763     if (!CE) return false;
1764     uint64_t Value = CE->getValue();
1765     // i64 value with each byte being either 0 or 0xff.
1766     for (unsigned i = 0; i < 8; ++i, Value >>= 8)
1767       if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
1768     return true;
1769   }
1770 
1771   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
1772     // Add as immediates when possible.  Null MCExpr = 0.
1773     if (!Expr)
1774       Inst.addOperand(MCOperand::createImm(0));
1775     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
1776       Inst.addOperand(MCOperand::createImm(CE->getValue()));
1777     else
1778       Inst.addOperand(MCOperand::createExpr(Expr));
1779   }
1780 
1781   void addARMBranchTargetOperands(MCInst &Inst, unsigned N) const {
1782     assert(N == 1 && "Invalid number of operands!");
1783     addExpr(Inst, getImm());
1784   }
1785 
1786   void addThumbBranchTargetOperands(MCInst &Inst, unsigned N) const {
1787     assert(N == 1 && "Invalid number of operands!");
1788     addExpr(Inst, getImm());
1789   }
1790 
1791   void addCondCodeOperands(MCInst &Inst, unsigned N) const {
1792     assert(N == 2 && "Invalid number of operands!");
1793     Inst.addOperand(MCOperand::createImm(unsigned(getCondCode())));
1794     unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
1795     Inst.addOperand(MCOperand::createReg(RegNum));
1796   }
1797 
1798   void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
1799     assert(N == 1 && "Invalid number of operands!");
1800     Inst.addOperand(MCOperand::createImm(getCoproc()));
1801   }
1802 
1803   void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
1804     assert(N == 1 && "Invalid number of operands!");
1805     Inst.addOperand(MCOperand::createImm(getCoproc()));
1806   }
1807 
1808   void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
1809     assert(N == 1 && "Invalid number of operands!");
1810     Inst.addOperand(MCOperand::createImm(CoprocOption.Val));
1811   }
1812 
1813   void addITMaskOperands(MCInst &Inst, unsigned N) const {
1814     assert(N == 1 && "Invalid number of operands!");
1815     Inst.addOperand(MCOperand::createImm(ITMask.Mask));
1816   }
1817 
1818   void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
1819     assert(N == 1 && "Invalid number of operands!");
1820     Inst.addOperand(MCOperand::createImm(unsigned(getCondCode())));
1821   }
1822 
1823   void addCCOutOperands(MCInst &Inst, unsigned N) const {
1824     assert(N == 1 && "Invalid number of operands!");
1825     Inst.addOperand(MCOperand::createReg(getReg()));
1826   }
1827 
1828   void addRegOperands(MCInst &Inst, unsigned N) const {
1829     assert(N == 1 && "Invalid number of operands!");
1830     Inst.addOperand(MCOperand::createReg(getReg()));
1831   }
1832 
1833   void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
1834     assert(N == 3 && "Invalid number of operands!");
1835     assert(isRegShiftedReg() &&
1836            "addRegShiftedRegOperands() on non-RegShiftedReg!");
1837     Inst.addOperand(MCOperand::createReg(RegShiftedReg.SrcReg));
1838     Inst.addOperand(MCOperand::createReg(RegShiftedReg.ShiftReg));
1839     Inst.addOperand(MCOperand::createImm(
1840       ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
1841   }
1842 
1843   void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
1844     assert(N == 2 && "Invalid number of operands!");
1845     assert(isRegShiftedImm() &&
1846            "addRegShiftedImmOperands() on non-RegShiftedImm!");
1847     Inst.addOperand(MCOperand::createReg(RegShiftedImm.SrcReg));
1848     // Shift of #32 is encoded as 0 where permitted
1849     unsigned Imm = (RegShiftedImm.ShiftImm == 32 ? 0 : RegShiftedImm.ShiftImm);
1850     Inst.addOperand(MCOperand::createImm(
1851       ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, Imm)));
1852   }
1853 
1854   void addShifterImmOperands(MCInst &Inst, unsigned N) const {
1855     assert(N == 1 && "Invalid number of operands!");
1856     Inst.addOperand(MCOperand::createImm((ShifterImm.isASR << 5) |
1857                                          ShifterImm.Imm));
1858   }
1859 
1860   void addRegListOperands(MCInst &Inst, unsigned N) const {
1861     assert(N == 1 && "Invalid number of operands!");
1862     const SmallVectorImpl<unsigned> &RegList = getRegList();
1863     for (SmallVectorImpl<unsigned>::const_iterator
1864            I = RegList.begin(), E = RegList.end(); I != E; ++I)
1865       Inst.addOperand(MCOperand::createReg(*I));
1866   }
1867 
1868   void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
1869     addRegListOperands(Inst, N);
1870   }
1871 
1872   void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
1873     addRegListOperands(Inst, N);
1874   }
1875 
1876   void addRotImmOperands(MCInst &Inst, unsigned N) const {
1877     assert(N == 1 && "Invalid number of operands!");
1878     // Encoded as val>>3. The printer handles display as 8, 16, 24.
1879     Inst.addOperand(MCOperand::createImm(RotImm.Imm >> 3));
1880   }
1881 
1882   void addModImmOperands(MCInst &Inst, unsigned N) const {
1883     assert(N == 1 && "Invalid number of operands!");
1884 
1885     // Support for fixups (MCFixup)
1886     if (isImm())
1887       return addImmOperands(Inst, N);
1888 
1889     Inst.addOperand(MCOperand::createImm(ModImm.Bits | (ModImm.Rot << 7)));
1890   }
1891 
1892   void addModImmNotOperands(MCInst &Inst, unsigned N) const {
1893     assert(N == 1 && "Invalid number of operands!");
1894     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1895     uint32_t Enc = ARM_AM::getSOImmVal(~CE->getValue());
1896     Inst.addOperand(MCOperand::createImm(Enc));
1897   }
1898 
1899   void addModImmNegOperands(MCInst &Inst, unsigned N) const {
1900     assert(N == 1 && "Invalid number of operands!");
1901     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1902     uint32_t Enc = ARM_AM::getSOImmVal(-CE->getValue());
1903     Inst.addOperand(MCOperand::createImm(Enc));
1904   }
1905 
1906   void addThumbModImmNeg8_255Operands(MCInst &Inst, unsigned N) const {
1907     assert(N == 1 && "Invalid number of operands!");
1908     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1909     uint32_t Val = -CE->getValue();
1910     Inst.addOperand(MCOperand::createImm(Val));
1911   }
1912 
1913   void addThumbModImmNeg1_7Operands(MCInst &Inst, unsigned N) const {
1914     assert(N == 1 && "Invalid number of operands!");
1915     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1916     uint32_t Val = -CE->getValue();
1917     Inst.addOperand(MCOperand::createImm(Val));
1918   }
1919 
1920   void addBitfieldOperands(MCInst &Inst, unsigned N) const {
1921     assert(N == 1 && "Invalid number of operands!");
1922     // Munge the lsb/width into a bitfield mask.
1923     unsigned lsb = Bitfield.LSB;
1924     unsigned width = Bitfield.Width;
1925     // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
1926     uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
1927                       (32 - (lsb + width)));
1928     Inst.addOperand(MCOperand::createImm(Mask));
1929   }
1930 
1931   void addImmOperands(MCInst &Inst, unsigned N) const {
1932     assert(N == 1 && "Invalid number of operands!");
1933     addExpr(Inst, getImm());
1934   }
1935 
1936   void addFBits16Operands(MCInst &Inst, unsigned N) const {
1937     assert(N == 1 && "Invalid number of operands!");
1938     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1939     Inst.addOperand(MCOperand::createImm(16 - CE->getValue()));
1940   }
1941 
1942   void addFBits32Operands(MCInst &Inst, unsigned N) const {
1943     assert(N == 1 && "Invalid number of operands!");
1944     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1945     Inst.addOperand(MCOperand::createImm(32 - CE->getValue()));
1946   }
1947 
1948   void addFPImmOperands(MCInst &Inst, unsigned N) const {
1949     assert(N == 1 && "Invalid number of operands!");
1950     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1951     int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
1952     Inst.addOperand(MCOperand::createImm(Val));
1953   }
1954 
1955   void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1956     assert(N == 1 && "Invalid number of operands!");
1957     // FIXME: We really want to scale the value here, but the LDRD/STRD
1958     // instruction don't encode operands that way yet.
1959     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1960     Inst.addOperand(MCOperand::createImm(CE->getValue()));
1961   }
1962 
1963   void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1964     assert(N == 1 && "Invalid number of operands!");
1965     // The immediate is scaled by four in the encoding and is stored
1966     // in the MCInst as such. Lop off the low two bits here.
1967     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1968     Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
1969   }
1970 
1971   void addImm0_508s4NegOperands(MCInst &Inst, unsigned N) const {
1972     assert(N == 1 && "Invalid number of operands!");
1973     // The immediate is scaled by four in the encoding and is stored
1974     // in the MCInst as such. Lop off the low two bits here.
1975     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1976     Inst.addOperand(MCOperand::createImm(-(CE->getValue() / 4)));
1977   }
1978 
1979   void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1980     assert(N == 1 && "Invalid number of operands!");
1981     // The immediate is scaled by four in the encoding and is stored
1982     // in the MCInst as such. Lop off the low two bits here.
1983     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1984     Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
1985   }
1986 
1987   void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1988     assert(N == 1 && "Invalid number of operands!");
1989     // The constant encodes as the immediate-1, and we store in the instruction
1990     // the bits as encoded, so subtract off one here.
1991     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1992     Inst.addOperand(MCOperand::createImm(CE->getValue() - 1));
1993   }
1994 
1995   void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1996     assert(N == 1 && "Invalid number of operands!");
1997     // The constant encodes as the immediate-1, and we store in the instruction
1998     // the bits as encoded, so subtract off one here.
1999     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2000     Inst.addOperand(MCOperand::createImm(CE->getValue() - 1));
2001   }
2002 
2003   void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
2004     assert(N == 1 && "Invalid number of operands!");
2005     // The constant encodes as the immediate, except for 32, which encodes as
2006     // zero.
2007     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2008     unsigned Imm = CE->getValue();
2009     Inst.addOperand(MCOperand::createImm((Imm == 32 ? 0 : Imm)));
2010   }
2011 
2012   void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
2013     assert(N == 1 && "Invalid number of operands!");
2014     // An ASR value of 32 encodes as 0, so that's how we want to add it to
2015     // the instruction as well.
2016     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2017     int Val = CE->getValue();
2018     Inst.addOperand(MCOperand::createImm(Val == 32 ? 0 : Val));
2019   }
2020 
2021   void addT2SOImmNotOperands(MCInst &Inst, unsigned N) const {
2022     assert(N == 1 && "Invalid number of operands!");
2023     // The operand is actually a t2_so_imm, but we have its bitwise
2024     // negation in the assembly source, so twiddle it here.
2025     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2026     Inst.addOperand(MCOperand::createImm(~(uint32_t)CE->getValue()));
2027   }
2028 
2029   void addT2SOImmNegOperands(MCInst &Inst, unsigned N) const {
2030     assert(N == 1 && "Invalid number of operands!");
2031     // The operand is actually a t2_so_imm, but we have its
2032     // negation in the assembly source, so twiddle it here.
2033     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2034     Inst.addOperand(MCOperand::createImm(-(uint32_t)CE->getValue()));
2035   }
2036 
2037   void addImm0_4095NegOperands(MCInst &Inst, unsigned N) const {
2038     assert(N == 1 && "Invalid number of operands!");
2039     // The operand is actually an imm0_4095, but we have its
2040     // negation in the assembly source, so twiddle it here.
2041     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2042     Inst.addOperand(MCOperand::createImm(-CE->getValue()));
2043   }
2044 
2045   void addUnsignedOffset_b8s2Operands(MCInst &Inst, unsigned N) const {
2046     if(const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
2047       Inst.addOperand(MCOperand::createImm(CE->getValue() >> 2));
2048       return;
2049     }
2050 
2051     const MCSymbolRefExpr *SR = dyn_cast<MCSymbolRefExpr>(Imm.Val);
2052     assert(SR && "Unknown value type!");
2053     Inst.addOperand(MCOperand::createExpr(SR));
2054   }
2055 
2056   void addThumbMemPCOperands(MCInst &Inst, unsigned N) const {
2057     assert(N == 1 && "Invalid number of operands!");
2058     if (isImm()) {
2059       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2060       if (CE) {
2061         Inst.addOperand(MCOperand::createImm(CE->getValue()));
2062         return;
2063       }
2064 
2065       const MCSymbolRefExpr *SR = dyn_cast<MCSymbolRefExpr>(Imm.Val);
2066 
2067       assert(SR && "Unknown value type!");
2068       Inst.addOperand(MCOperand::createExpr(SR));
2069       return;
2070     }
2071 
2072     assert(isMem()  && "Unknown value type!");
2073     assert(isa<MCConstantExpr>(Memory.OffsetImm) && "Unknown value type!");
2074     Inst.addOperand(MCOperand::createImm(Memory.OffsetImm->getValue()));
2075   }
2076 
2077   void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
2078     assert(N == 1 && "Invalid number of operands!");
2079     Inst.addOperand(MCOperand::createImm(unsigned(getMemBarrierOpt())));
2080   }
2081 
2082   void addInstSyncBarrierOptOperands(MCInst &Inst, unsigned N) const {
2083     assert(N == 1 && "Invalid number of operands!");
2084     Inst.addOperand(MCOperand::createImm(unsigned(getInstSyncBarrierOpt())));
2085   }
2086 
2087   void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
2088     assert(N == 1 && "Invalid number of operands!");
2089     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2090   }
2091 
2092   void addMemPCRelImm12Operands(MCInst &Inst, unsigned N) const {
2093     assert(N == 1 && "Invalid number of operands!");
2094     int32_t Imm = Memory.OffsetImm->getValue();
2095     Inst.addOperand(MCOperand::createImm(Imm));
2096   }
2097 
2098   void addAdrLabelOperands(MCInst &Inst, unsigned N) const {
2099     assert(N == 1 && "Invalid number of operands!");
2100     assert(isImm() && "Not an immediate!");
2101 
2102     // If we have an immediate that's not a constant, treat it as a label
2103     // reference needing a fixup.
2104     if (!isa<MCConstantExpr>(getImm())) {
2105       Inst.addOperand(MCOperand::createExpr(getImm()));
2106       return;
2107     }
2108 
2109     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2110     int Val = CE->getValue();
2111     Inst.addOperand(MCOperand::createImm(Val));
2112   }
2113 
2114   void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
2115     assert(N == 2 && "Invalid number of operands!");
2116     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2117     Inst.addOperand(MCOperand::createImm(Memory.Alignment));
2118   }
2119 
2120   void addDupAlignedMemoryNoneOperands(MCInst &Inst, unsigned N) const {
2121     addAlignedMemoryOperands(Inst, N);
2122   }
2123 
2124   void addAlignedMemoryNoneOperands(MCInst &Inst, unsigned N) const {
2125     addAlignedMemoryOperands(Inst, N);
2126   }
2127 
2128   void addAlignedMemory16Operands(MCInst &Inst, unsigned N) const {
2129     addAlignedMemoryOperands(Inst, N);
2130   }
2131 
2132   void addDupAlignedMemory16Operands(MCInst &Inst, unsigned N) const {
2133     addAlignedMemoryOperands(Inst, N);
2134   }
2135 
2136   void addAlignedMemory32Operands(MCInst &Inst, unsigned N) const {
2137     addAlignedMemoryOperands(Inst, N);
2138   }
2139 
2140   void addDupAlignedMemory32Operands(MCInst &Inst, unsigned N) const {
2141     addAlignedMemoryOperands(Inst, N);
2142   }
2143 
2144   void addAlignedMemory64Operands(MCInst &Inst, unsigned N) const {
2145     addAlignedMemoryOperands(Inst, N);
2146   }
2147 
2148   void addDupAlignedMemory64Operands(MCInst &Inst, unsigned N) const {
2149     addAlignedMemoryOperands(Inst, N);
2150   }
2151 
2152   void addAlignedMemory64or128Operands(MCInst &Inst, unsigned N) const {
2153     addAlignedMemoryOperands(Inst, N);
2154   }
2155 
2156   void addDupAlignedMemory64or128Operands(MCInst &Inst, unsigned N) const {
2157     addAlignedMemoryOperands(Inst, N);
2158   }
2159 
2160   void addAlignedMemory64or128or256Operands(MCInst &Inst, unsigned N) const {
2161     addAlignedMemoryOperands(Inst, N);
2162   }
2163 
2164   void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
2165     assert(N == 3 && "Invalid number of operands!");
2166     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2167     if (!Memory.OffsetRegNum) {
2168       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2169       // Special case for #-0
2170       if (Val == INT32_MIN) Val = 0;
2171       if (Val < 0) Val = -Val;
2172       Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
2173     } else {
2174       // For register offset, we encode the shift type and negation flag
2175       // here.
2176       Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
2177                               Memory.ShiftImm, Memory.ShiftType);
2178     }
2179     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2180     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2181     Inst.addOperand(MCOperand::createImm(Val));
2182   }
2183 
2184   void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
2185     assert(N == 2 && "Invalid number of operands!");
2186     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2187     assert(CE && "non-constant AM2OffsetImm operand!");
2188     int32_t Val = CE->getValue();
2189     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2190     // Special case for #-0
2191     if (Val == INT32_MIN) Val = 0;
2192     if (Val < 0) Val = -Val;
2193     Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
2194     Inst.addOperand(MCOperand::createReg(0));
2195     Inst.addOperand(MCOperand::createImm(Val));
2196   }
2197 
2198   void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
2199     assert(N == 3 && "Invalid number of operands!");
2200     // If we have an immediate that's not a constant, treat it as a label
2201     // reference needing a fixup. If it is a constant, it's something else
2202     // and we reject it.
2203     if (isImm()) {
2204       Inst.addOperand(MCOperand::createExpr(getImm()));
2205       Inst.addOperand(MCOperand::createReg(0));
2206       Inst.addOperand(MCOperand::createImm(0));
2207       return;
2208     }
2209 
2210     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2211     if (!Memory.OffsetRegNum) {
2212       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2213       // Special case for #-0
2214       if (Val == INT32_MIN) Val = 0;
2215       if (Val < 0) Val = -Val;
2216       Val = ARM_AM::getAM3Opc(AddSub, Val);
2217     } else {
2218       // For register offset, we encode the shift type and negation flag
2219       // here.
2220       Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
2221     }
2222     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2223     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2224     Inst.addOperand(MCOperand::createImm(Val));
2225   }
2226 
2227   void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
2228     assert(N == 2 && "Invalid number of operands!");
2229     if (Kind == k_PostIndexRegister) {
2230       int32_t Val =
2231         ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
2232       Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
2233       Inst.addOperand(MCOperand::createImm(Val));
2234       return;
2235     }
2236 
2237     // Constant offset.
2238     const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
2239     int32_t Val = CE->getValue();
2240     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2241     // Special case for #-0
2242     if (Val == INT32_MIN) Val = 0;
2243     if (Val < 0) Val = -Val;
2244     Val = ARM_AM::getAM3Opc(AddSub, Val);
2245     Inst.addOperand(MCOperand::createReg(0));
2246     Inst.addOperand(MCOperand::createImm(Val));
2247   }
2248 
2249   void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
2250     assert(N == 2 && "Invalid number of operands!");
2251     // If we have an immediate that's not a constant, treat it as a label
2252     // reference needing a fixup. If it is a constant, it's something else
2253     // and we reject it.
2254     if (isImm()) {
2255       Inst.addOperand(MCOperand::createExpr(getImm()));
2256       Inst.addOperand(MCOperand::createImm(0));
2257       return;
2258     }
2259 
2260     // The lower two bits are always zero and as such are not encoded.
2261     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
2262     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2263     // Special case for #-0
2264     if (Val == INT32_MIN) Val = 0;
2265     if (Val < 0) Val = -Val;
2266     Val = ARM_AM::getAM5Opc(AddSub, Val);
2267     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2268     Inst.addOperand(MCOperand::createImm(Val));
2269   }
2270 
2271   void addAddrMode5FP16Operands(MCInst &Inst, unsigned N) const {
2272     assert(N == 2 && "Invalid number of operands!");
2273     // If we have an immediate that's not a constant, treat it as a label
2274     // reference needing a fixup. If it is a constant, it's something else
2275     // and we reject it.
2276     if (isImm()) {
2277       Inst.addOperand(MCOperand::createExpr(getImm()));
2278       Inst.addOperand(MCOperand::createImm(0));
2279       return;
2280     }
2281 
2282     // The lower bit is always zero and as such is not encoded.
2283     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 2 : 0;
2284     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2285     // Special case for #-0
2286     if (Val == INT32_MIN) Val = 0;
2287     if (Val < 0) Val = -Val;
2288     Val = ARM_AM::getAM5FP16Opc(AddSub, Val);
2289     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2290     Inst.addOperand(MCOperand::createImm(Val));
2291   }
2292 
2293   void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
2294     assert(N == 2 && "Invalid number of operands!");
2295     // If we have an immediate that's not a constant, treat it as a label
2296     // reference needing a fixup. If it is a constant, it's something else
2297     // and we reject it.
2298     if (isImm()) {
2299       Inst.addOperand(MCOperand::createExpr(getImm()));
2300       Inst.addOperand(MCOperand::createImm(0));
2301       return;
2302     }
2303 
2304     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2305     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2306     Inst.addOperand(MCOperand::createImm(Val));
2307   }
2308 
2309   void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
2310     assert(N == 2 && "Invalid number of operands!");
2311     // The lower two bits are always zero and as such are not encoded.
2312     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
2313     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2314     Inst.addOperand(MCOperand::createImm(Val));
2315   }
2316 
2317   void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
2318     assert(N == 2 && "Invalid number of operands!");
2319     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2320     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2321     Inst.addOperand(MCOperand::createImm(Val));
2322   }
2323 
2324   void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
2325     addMemImm8OffsetOperands(Inst, N);
2326   }
2327 
2328   void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
2329     addMemImm8OffsetOperands(Inst, N);
2330   }
2331 
2332   void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
2333     assert(N == 2 && "Invalid number of operands!");
2334     // If this is an immediate, it's a label reference.
2335     if (isImm()) {
2336       addExpr(Inst, getImm());
2337       Inst.addOperand(MCOperand::createImm(0));
2338       return;
2339     }
2340 
2341     // Otherwise, it's a normal memory reg+offset.
2342     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2343     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2344     Inst.addOperand(MCOperand::createImm(Val));
2345   }
2346 
2347   void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
2348     assert(N == 2 && "Invalid number of operands!");
2349     // If this is an immediate, it's a label reference.
2350     if (isImm()) {
2351       addExpr(Inst, getImm());
2352       Inst.addOperand(MCOperand::createImm(0));
2353       return;
2354     }
2355 
2356     // Otherwise, it's a normal memory reg+offset.
2357     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2358     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2359     Inst.addOperand(MCOperand::createImm(Val));
2360   }
2361 
2362   void addConstPoolAsmImmOperands(MCInst &Inst, unsigned N) const {
2363     assert(N == 1 && "Invalid number of operands!");
2364     // This is container for the immediate that we will create the constant
2365     // pool from
2366     addExpr(Inst, getConstantPoolImm());
2367     return;
2368   }
2369 
2370   void addMemTBBOperands(MCInst &Inst, unsigned N) const {
2371     assert(N == 2 && "Invalid number of operands!");
2372     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2373     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2374   }
2375 
2376   void addMemTBHOperands(MCInst &Inst, unsigned N) const {
2377     assert(N == 2 && "Invalid number of operands!");
2378     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2379     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2380   }
2381 
2382   void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
2383     assert(N == 3 && "Invalid number of operands!");
2384     unsigned Val =
2385       ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
2386                         Memory.ShiftImm, Memory.ShiftType);
2387     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2388     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2389     Inst.addOperand(MCOperand::createImm(Val));
2390   }
2391 
2392   void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
2393     assert(N == 3 && "Invalid number of operands!");
2394     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2395     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2396     Inst.addOperand(MCOperand::createImm(Memory.ShiftImm));
2397   }
2398 
2399   void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
2400     assert(N == 2 && "Invalid number of operands!");
2401     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2402     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2403   }
2404 
2405   void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
2406     assert(N == 2 && "Invalid number of operands!");
2407     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
2408     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2409     Inst.addOperand(MCOperand::createImm(Val));
2410   }
2411 
2412   void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
2413     assert(N == 2 && "Invalid number of operands!");
2414     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
2415     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2416     Inst.addOperand(MCOperand::createImm(Val));
2417   }
2418 
2419   void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
2420     assert(N == 2 && "Invalid number of operands!");
2421     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
2422     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2423     Inst.addOperand(MCOperand::createImm(Val));
2424   }
2425 
2426   void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
2427     assert(N == 2 && "Invalid number of operands!");
2428     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
2429     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2430     Inst.addOperand(MCOperand::createImm(Val));
2431   }
2432 
2433   void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
2434     assert(N == 1 && "Invalid number of operands!");
2435     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2436     assert(CE && "non-constant post-idx-imm8 operand!");
2437     int Imm = CE->getValue();
2438     bool isAdd = Imm >= 0;
2439     if (Imm == INT32_MIN) Imm = 0;
2440     Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
2441     Inst.addOperand(MCOperand::createImm(Imm));
2442   }
2443 
2444   void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
2445     assert(N == 1 && "Invalid number of operands!");
2446     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2447     assert(CE && "non-constant post-idx-imm8s4 operand!");
2448     int Imm = CE->getValue();
2449     bool isAdd = Imm >= 0;
2450     if (Imm == INT32_MIN) Imm = 0;
2451     // Immediate is scaled by 4.
2452     Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
2453     Inst.addOperand(MCOperand::createImm(Imm));
2454   }
2455 
2456   void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
2457     assert(N == 2 && "Invalid number of operands!");
2458     Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
2459     Inst.addOperand(MCOperand::createImm(PostIdxReg.isAdd));
2460   }
2461 
2462   void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
2463     assert(N == 2 && "Invalid number of operands!");
2464     Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
2465     // The sign, shift type, and shift amount are encoded in a single operand
2466     // using the AM2 encoding helpers.
2467     ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
2468     unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
2469                                      PostIdxReg.ShiftTy);
2470     Inst.addOperand(MCOperand::createImm(Imm));
2471   }
2472 
2473   void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
2474     assert(N == 1 && "Invalid number of operands!");
2475     Inst.addOperand(MCOperand::createImm(unsigned(getMSRMask())));
2476   }
2477 
2478   void addBankedRegOperands(MCInst &Inst, unsigned N) const {
2479     assert(N == 1 && "Invalid number of operands!");
2480     Inst.addOperand(MCOperand::createImm(unsigned(getBankedReg())));
2481   }
2482 
2483   void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
2484     assert(N == 1 && "Invalid number of operands!");
2485     Inst.addOperand(MCOperand::createImm(unsigned(getProcIFlags())));
2486   }
2487 
2488   void addVecListOperands(MCInst &Inst, unsigned N) const {
2489     assert(N == 1 && "Invalid number of operands!");
2490     Inst.addOperand(MCOperand::createReg(VectorList.RegNum));
2491   }
2492 
2493   void addVecListIndexedOperands(MCInst &Inst, unsigned N) const {
2494     assert(N == 2 && "Invalid number of operands!");
2495     Inst.addOperand(MCOperand::createReg(VectorList.RegNum));
2496     Inst.addOperand(MCOperand::createImm(VectorList.LaneIndex));
2497   }
2498 
2499   void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
2500     assert(N == 1 && "Invalid number of operands!");
2501     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
2502   }
2503 
2504   void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
2505     assert(N == 1 && "Invalid number of operands!");
2506     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
2507   }
2508 
2509   void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
2510     assert(N == 1 && "Invalid number of operands!");
2511     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
2512   }
2513 
2514   void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
2515     assert(N == 1 && "Invalid number of operands!");
2516     // The immediate encodes the type of constant as well as the value.
2517     // Mask in that this is an i8 splat.
2518     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2519     Inst.addOperand(MCOperand::createImm(CE->getValue() | 0xe00));
2520   }
2521 
2522   void addNEONi16splatOperands(MCInst &Inst, unsigned N) const {
2523     assert(N == 1 && "Invalid number of operands!");
2524     // The immediate encodes the type of constant as well as the value.
2525     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2526     unsigned Value = CE->getValue();
2527     Value = ARM_AM::encodeNEONi16splat(Value);
2528     Inst.addOperand(MCOperand::createImm(Value));
2529   }
2530 
2531   void addNEONi16splatNotOperands(MCInst &Inst, unsigned N) const {
2532     assert(N == 1 && "Invalid number of operands!");
2533     // The immediate encodes the type of constant as well as the value.
2534     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2535     unsigned Value = CE->getValue();
2536     Value = ARM_AM::encodeNEONi16splat(~Value & 0xffff);
2537     Inst.addOperand(MCOperand::createImm(Value));
2538   }
2539 
2540   void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
2541     assert(N == 1 && "Invalid number of operands!");
2542     // The immediate encodes the type of constant as well as the value.
2543     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2544     unsigned Value = CE->getValue();
2545     Value = ARM_AM::encodeNEONi32splat(Value);
2546     Inst.addOperand(MCOperand::createImm(Value));
2547   }
2548 
2549   void addNEONi32splatNotOperands(MCInst &Inst, unsigned N) const {
2550     assert(N == 1 && "Invalid number of operands!");
2551     // The immediate encodes the type of constant as well as the value.
2552     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2553     unsigned Value = CE->getValue();
2554     Value = ARM_AM::encodeNEONi32splat(~Value);
2555     Inst.addOperand(MCOperand::createImm(Value));
2556   }
2557 
2558   void addNEONinvByteReplicateOperands(MCInst &Inst, unsigned N) const {
2559     assert(N == 1 && "Invalid number of operands!");
2560     // The immediate encodes the type of constant as well as the value.
2561     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2562     unsigned Value = CE->getValue();
2563     assert((Inst.getOpcode() == ARM::VMOVv8i8 ||
2564             Inst.getOpcode() == ARM::VMOVv16i8) &&
2565            "All vmvn instructions that wants to replicate non-zero byte "
2566            "always must be replaced with VMOVv8i8 or VMOVv16i8.");
2567     unsigned B = ((~Value) & 0xff);
2568     B |= 0xe00; // cmode = 0b1110
2569     Inst.addOperand(MCOperand::createImm(B));
2570   }
2571   void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
2572     assert(N == 1 && "Invalid number of operands!");
2573     // The immediate encodes the type of constant as well as the value.
2574     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2575     unsigned Value = CE->getValue();
2576     if (Value >= 256 && Value <= 0xffff)
2577       Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
2578     else if (Value > 0xffff && Value <= 0xffffff)
2579       Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
2580     else if (Value > 0xffffff)
2581       Value = (Value >> 24) | 0x600;
2582     Inst.addOperand(MCOperand::createImm(Value));
2583   }
2584 
2585   void addNEONvmovByteReplicateOperands(MCInst &Inst, unsigned N) const {
2586     assert(N == 1 && "Invalid number of operands!");
2587     // The immediate encodes the type of constant as well as the value.
2588     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2589     unsigned Value = CE->getValue();
2590     assert((Inst.getOpcode() == ARM::VMOVv8i8 ||
2591             Inst.getOpcode() == ARM::VMOVv16i8) &&
2592            "All instructions that wants to replicate non-zero byte "
2593            "always must be replaced with VMOVv8i8 or VMOVv16i8.");
2594     unsigned B = Value & 0xff;
2595     B |= 0xe00; // cmode = 0b1110
2596     Inst.addOperand(MCOperand::createImm(B));
2597   }
2598   void addNEONi32vmovNegOperands(MCInst &Inst, unsigned N) const {
2599     assert(N == 1 && "Invalid number of operands!");
2600     // The immediate encodes the type of constant as well as the value.
2601     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2602     unsigned Value = ~CE->getValue();
2603     if (Value >= 256 && Value <= 0xffff)
2604       Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
2605     else if (Value > 0xffff && Value <= 0xffffff)
2606       Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
2607     else if (Value > 0xffffff)
2608       Value = (Value >> 24) | 0x600;
2609     Inst.addOperand(MCOperand::createImm(Value));
2610   }
2611 
2612   void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
2613     assert(N == 1 && "Invalid number of operands!");
2614     // The immediate encodes the type of constant as well as the value.
2615     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2616     uint64_t Value = CE->getValue();
2617     unsigned Imm = 0;
2618     for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
2619       Imm |= (Value & 1) << i;
2620     }
2621     Inst.addOperand(MCOperand::createImm(Imm | 0x1e00));
2622   }
2623 
2624   void print(raw_ostream &OS) const override;
2625 
2626   static std::unique_ptr<ARMOperand> CreateITMask(unsigned Mask, SMLoc S) {
2627     auto Op = make_unique<ARMOperand>(k_ITCondMask);
2628     Op->ITMask.Mask = Mask;
2629     Op->StartLoc = S;
2630     Op->EndLoc = S;
2631     return Op;
2632   }
2633 
2634   static std::unique_ptr<ARMOperand> CreateCondCode(ARMCC::CondCodes CC,
2635                                                     SMLoc S) {
2636     auto Op = make_unique<ARMOperand>(k_CondCode);
2637     Op->CC.Val = CC;
2638     Op->StartLoc = S;
2639     Op->EndLoc = S;
2640     return Op;
2641   }
2642 
2643   static std::unique_ptr<ARMOperand> CreateCoprocNum(unsigned CopVal, SMLoc S) {
2644     auto Op = make_unique<ARMOperand>(k_CoprocNum);
2645     Op->Cop.Val = CopVal;
2646     Op->StartLoc = S;
2647     Op->EndLoc = S;
2648     return Op;
2649   }
2650 
2651   static std::unique_ptr<ARMOperand> CreateCoprocReg(unsigned CopVal, SMLoc S) {
2652     auto Op = make_unique<ARMOperand>(k_CoprocReg);
2653     Op->Cop.Val = CopVal;
2654     Op->StartLoc = S;
2655     Op->EndLoc = S;
2656     return Op;
2657   }
2658 
2659   static std::unique_ptr<ARMOperand> CreateCoprocOption(unsigned Val, SMLoc S,
2660                                                         SMLoc E) {
2661     auto Op = make_unique<ARMOperand>(k_CoprocOption);
2662     Op->Cop.Val = Val;
2663     Op->StartLoc = S;
2664     Op->EndLoc = E;
2665     return Op;
2666   }
2667 
2668   static std::unique_ptr<ARMOperand> CreateCCOut(unsigned RegNum, SMLoc S) {
2669     auto Op = make_unique<ARMOperand>(k_CCOut);
2670     Op->Reg.RegNum = RegNum;
2671     Op->StartLoc = S;
2672     Op->EndLoc = S;
2673     return Op;
2674   }
2675 
2676   static std::unique_ptr<ARMOperand> CreateToken(StringRef Str, SMLoc S) {
2677     auto Op = make_unique<ARMOperand>(k_Token);
2678     Op->Tok.Data = Str.data();
2679     Op->Tok.Length = Str.size();
2680     Op->StartLoc = S;
2681     Op->EndLoc = S;
2682     return Op;
2683   }
2684 
2685   static std::unique_ptr<ARMOperand> CreateReg(unsigned RegNum, SMLoc S,
2686                                                SMLoc E) {
2687     auto Op = make_unique<ARMOperand>(k_Register);
2688     Op->Reg.RegNum = RegNum;
2689     Op->StartLoc = S;
2690     Op->EndLoc = E;
2691     return Op;
2692   }
2693 
2694   static std::unique_ptr<ARMOperand>
2695   CreateShiftedRegister(ARM_AM::ShiftOpc ShTy, unsigned SrcReg,
2696                         unsigned ShiftReg, unsigned ShiftImm, SMLoc S,
2697                         SMLoc E) {
2698     auto Op = make_unique<ARMOperand>(k_ShiftedRegister);
2699     Op->RegShiftedReg.ShiftTy = ShTy;
2700     Op->RegShiftedReg.SrcReg = SrcReg;
2701     Op->RegShiftedReg.ShiftReg = ShiftReg;
2702     Op->RegShiftedReg.ShiftImm = ShiftImm;
2703     Op->StartLoc = S;
2704     Op->EndLoc = E;
2705     return Op;
2706   }
2707 
2708   static std::unique_ptr<ARMOperand>
2709   CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy, unsigned SrcReg,
2710                          unsigned ShiftImm, SMLoc S, SMLoc E) {
2711     auto Op = make_unique<ARMOperand>(k_ShiftedImmediate);
2712     Op->RegShiftedImm.ShiftTy = ShTy;
2713     Op->RegShiftedImm.SrcReg = SrcReg;
2714     Op->RegShiftedImm.ShiftImm = ShiftImm;
2715     Op->StartLoc = S;
2716     Op->EndLoc = E;
2717     return Op;
2718   }
2719 
2720   static std::unique_ptr<ARMOperand> CreateShifterImm(bool isASR, unsigned Imm,
2721                                                       SMLoc S, SMLoc E) {
2722     auto Op = make_unique<ARMOperand>(k_ShifterImmediate);
2723     Op->ShifterImm.isASR = isASR;
2724     Op->ShifterImm.Imm = Imm;
2725     Op->StartLoc = S;
2726     Op->EndLoc = E;
2727     return Op;
2728   }
2729 
2730   static std::unique_ptr<ARMOperand> CreateRotImm(unsigned Imm, SMLoc S,
2731                                                   SMLoc E) {
2732     auto Op = make_unique<ARMOperand>(k_RotateImmediate);
2733     Op->RotImm.Imm = Imm;
2734     Op->StartLoc = S;
2735     Op->EndLoc = E;
2736     return Op;
2737   }
2738 
2739   static std::unique_ptr<ARMOperand> CreateModImm(unsigned Bits, unsigned Rot,
2740                                                   SMLoc S, SMLoc E) {
2741     auto Op = make_unique<ARMOperand>(k_ModifiedImmediate);
2742     Op->ModImm.Bits = Bits;
2743     Op->ModImm.Rot = Rot;
2744     Op->StartLoc = S;
2745     Op->EndLoc = E;
2746     return Op;
2747   }
2748 
2749   static std::unique_ptr<ARMOperand>
2750   CreateConstantPoolImm(const MCExpr *Val, SMLoc S, SMLoc E) {
2751     auto Op = make_unique<ARMOperand>(k_ConstantPoolImmediate);
2752     Op->Imm.Val = Val;
2753     Op->StartLoc = S;
2754     Op->EndLoc = E;
2755     return Op;
2756   }
2757 
2758   static std::unique_ptr<ARMOperand>
2759   CreateBitfield(unsigned LSB, unsigned Width, SMLoc S, SMLoc E) {
2760     auto Op = make_unique<ARMOperand>(k_BitfieldDescriptor);
2761     Op->Bitfield.LSB = LSB;
2762     Op->Bitfield.Width = Width;
2763     Op->StartLoc = S;
2764     Op->EndLoc = E;
2765     return Op;
2766   }
2767 
2768   static std::unique_ptr<ARMOperand>
2769   CreateRegList(SmallVectorImpl<std::pair<unsigned, unsigned>> &Regs,
2770                 SMLoc StartLoc, SMLoc EndLoc) {
2771     assert (Regs.size() > 0 && "RegList contains no registers?");
2772     KindTy Kind = k_RegisterList;
2773 
2774     if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().second))
2775       Kind = k_DPRRegisterList;
2776     else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
2777              contains(Regs.front().second))
2778       Kind = k_SPRRegisterList;
2779 
2780     // Sort based on the register encoding values.
2781     array_pod_sort(Regs.begin(), Regs.end());
2782 
2783     auto Op = make_unique<ARMOperand>(Kind);
2784     for (SmallVectorImpl<std::pair<unsigned, unsigned> >::const_iterator
2785            I = Regs.begin(), E = Regs.end(); I != E; ++I)
2786       Op->Registers.push_back(I->second);
2787     Op->StartLoc = StartLoc;
2788     Op->EndLoc = EndLoc;
2789     return Op;
2790   }
2791 
2792   static std::unique_ptr<ARMOperand> CreateVectorList(unsigned RegNum,
2793                                                       unsigned Count,
2794                                                       bool isDoubleSpaced,
2795                                                       SMLoc S, SMLoc E) {
2796     auto Op = make_unique<ARMOperand>(k_VectorList);
2797     Op->VectorList.RegNum = RegNum;
2798     Op->VectorList.Count = Count;
2799     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2800     Op->StartLoc = S;
2801     Op->EndLoc = E;
2802     return Op;
2803   }
2804 
2805   static std::unique_ptr<ARMOperand>
2806   CreateVectorListAllLanes(unsigned RegNum, unsigned Count, bool isDoubleSpaced,
2807                            SMLoc S, SMLoc E) {
2808     auto Op = make_unique<ARMOperand>(k_VectorListAllLanes);
2809     Op->VectorList.RegNum = RegNum;
2810     Op->VectorList.Count = Count;
2811     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2812     Op->StartLoc = S;
2813     Op->EndLoc = E;
2814     return Op;
2815   }
2816 
2817   static std::unique_ptr<ARMOperand>
2818   CreateVectorListIndexed(unsigned RegNum, unsigned Count, unsigned Index,
2819                           bool isDoubleSpaced, SMLoc S, SMLoc E) {
2820     auto Op = make_unique<ARMOperand>(k_VectorListIndexed);
2821     Op->VectorList.RegNum = RegNum;
2822     Op->VectorList.Count = Count;
2823     Op->VectorList.LaneIndex = Index;
2824     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2825     Op->StartLoc = S;
2826     Op->EndLoc = E;
2827     return Op;
2828   }
2829 
2830   static std::unique_ptr<ARMOperand>
2831   CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E, MCContext &Ctx) {
2832     auto Op = make_unique<ARMOperand>(k_VectorIndex);
2833     Op->VectorIndex.Val = Idx;
2834     Op->StartLoc = S;
2835     Op->EndLoc = E;
2836     return Op;
2837   }
2838 
2839   static std::unique_ptr<ARMOperand> CreateImm(const MCExpr *Val, SMLoc S,
2840                                                SMLoc E) {
2841     auto Op = make_unique<ARMOperand>(k_Immediate);
2842     Op->Imm.Val = Val;
2843     Op->StartLoc = S;
2844     Op->EndLoc = E;
2845     return Op;
2846   }
2847 
2848   static std::unique_ptr<ARMOperand>
2849   CreateMem(unsigned BaseRegNum, const MCConstantExpr *OffsetImm,
2850             unsigned OffsetRegNum, ARM_AM::ShiftOpc ShiftType,
2851             unsigned ShiftImm, unsigned Alignment, bool isNegative, SMLoc S,
2852             SMLoc E, SMLoc AlignmentLoc = SMLoc()) {
2853     auto Op = make_unique<ARMOperand>(k_Memory);
2854     Op->Memory.BaseRegNum = BaseRegNum;
2855     Op->Memory.OffsetImm = OffsetImm;
2856     Op->Memory.OffsetRegNum = OffsetRegNum;
2857     Op->Memory.ShiftType = ShiftType;
2858     Op->Memory.ShiftImm = ShiftImm;
2859     Op->Memory.Alignment = Alignment;
2860     Op->Memory.isNegative = isNegative;
2861     Op->StartLoc = S;
2862     Op->EndLoc = E;
2863     Op->AlignmentLoc = AlignmentLoc;
2864     return Op;
2865   }
2866 
2867   static std::unique_ptr<ARMOperand>
2868   CreatePostIdxReg(unsigned RegNum, bool isAdd, ARM_AM::ShiftOpc ShiftTy,
2869                    unsigned ShiftImm, SMLoc S, SMLoc E) {
2870     auto Op = make_unique<ARMOperand>(k_PostIndexRegister);
2871     Op->PostIdxReg.RegNum = RegNum;
2872     Op->PostIdxReg.isAdd = isAdd;
2873     Op->PostIdxReg.ShiftTy = ShiftTy;
2874     Op->PostIdxReg.ShiftImm = ShiftImm;
2875     Op->StartLoc = S;
2876     Op->EndLoc = E;
2877     return Op;
2878   }
2879 
2880   static std::unique_ptr<ARMOperand> CreateMemBarrierOpt(ARM_MB::MemBOpt Opt,
2881                                                          SMLoc S) {
2882     auto Op = make_unique<ARMOperand>(k_MemBarrierOpt);
2883     Op->MBOpt.Val = Opt;
2884     Op->StartLoc = S;
2885     Op->EndLoc = S;
2886     return Op;
2887   }
2888 
2889   static std::unique_ptr<ARMOperand>
2890   CreateInstSyncBarrierOpt(ARM_ISB::InstSyncBOpt Opt, SMLoc S) {
2891     auto Op = make_unique<ARMOperand>(k_InstSyncBarrierOpt);
2892     Op->ISBOpt.Val = Opt;
2893     Op->StartLoc = S;
2894     Op->EndLoc = S;
2895     return Op;
2896   }
2897 
2898   static std::unique_ptr<ARMOperand> CreateProcIFlags(ARM_PROC::IFlags IFlags,
2899                                                       SMLoc S) {
2900     auto Op = make_unique<ARMOperand>(k_ProcIFlags);
2901     Op->IFlags.Val = IFlags;
2902     Op->StartLoc = S;
2903     Op->EndLoc = S;
2904     return Op;
2905   }
2906 
2907   static std::unique_ptr<ARMOperand> CreateMSRMask(unsigned MMask, SMLoc S) {
2908     auto Op = make_unique<ARMOperand>(k_MSRMask);
2909     Op->MMask.Val = MMask;
2910     Op->StartLoc = S;
2911     Op->EndLoc = S;
2912     return Op;
2913   }
2914 
2915   static std::unique_ptr<ARMOperand> CreateBankedReg(unsigned Reg, SMLoc S) {
2916     auto Op = make_unique<ARMOperand>(k_BankedReg);
2917     Op->BankedReg.Val = Reg;
2918     Op->StartLoc = S;
2919     Op->EndLoc = S;
2920     return Op;
2921   }
2922 };
2923 
2924 } // end anonymous namespace.
2925 
2926 void ARMOperand::print(raw_ostream &OS) const {
2927   switch (Kind) {
2928   case k_CondCode:
2929     OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
2930     break;
2931   case k_CCOut:
2932     OS << "<ccout " << getReg() << ">";
2933     break;
2934   case k_ITCondMask: {
2935     static const char *const MaskStr[] = {
2936       "()", "(t)", "(e)", "(tt)", "(et)", "(te)", "(ee)", "(ttt)", "(ett)",
2937       "(tet)", "(eet)", "(tte)", "(ete)", "(tee)", "(eee)"
2938     };
2939     assert((ITMask.Mask & 0xf) == ITMask.Mask);
2940     OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
2941     break;
2942   }
2943   case k_CoprocNum:
2944     OS << "<coprocessor number: " << getCoproc() << ">";
2945     break;
2946   case k_CoprocReg:
2947     OS << "<coprocessor register: " << getCoproc() << ">";
2948     break;
2949   case k_CoprocOption:
2950     OS << "<coprocessor option: " << CoprocOption.Val << ">";
2951     break;
2952   case k_MSRMask:
2953     OS << "<mask: " << getMSRMask() << ">";
2954     break;
2955   case k_BankedReg:
2956     OS << "<banked reg: " << getBankedReg() << ">";
2957     break;
2958   case k_Immediate:
2959     OS << *getImm();
2960     break;
2961   case k_MemBarrierOpt:
2962     OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt(), false) << ">";
2963     break;
2964   case k_InstSyncBarrierOpt:
2965     OS << "<ARM_ISB::" << InstSyncBOptToString(getInstSyncBarrierOpt()) << ">";
2966     break;
2967   case k_Memory:
2968     OS << "<memory "
2969        << " base:" << Memory.BaseRegNum;
2970     OS << ">";
2971     break;
2972   case k_PostIndexRegister:
2973     OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
2974        << PostIdxReg.RegNum;
2975     if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
2976       OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
2977          << PostIdxReg.ShiftImm;
2978     OS << ">";
2979     break;
2980   case k_ProcIFlags: {
2981     OS << "<ARM_PROC::";
2982     unsigned IFlags = getProcIFlags();
2983     for (int i=2; i >= 0; --i)
2984       if (IFlags & (1 << i))
2985         OS << ARM_PROC::IFlagsToString(1 << i);
2986     OS << ">";
2987     break;
2988   }
2989   case k_Register:
2990     OS << "<register " << getReg() << ">";
2991     break;
2992   case k_ShifterImmediate:
2993     OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
2994        << " #" << ShifterImm.Imm << ">";
2995     break;
2996   case k_ShiftedRegister:
2997     OS << "<so_reg_reg "
2998        << RegShiftedReg.SrcReg << " "
2999        << ARM_AM::getShiftOpcStr(RegShiftedReg.ShiftTy)
3000        << " " << RegShiftedReg.ShiftReg << ">";
3001     break;
3002   case k_ShiftedImmediate:
3003     OS << "<so_reg_imm "
3004        << RegShiftedImm.SrcReg << " "
3005        << ARM_AM::getShiftOpcStr(RegShiftedImm.ShiftTy)
3006        << " #" << RegShiftedImm.ShiftImm << ">";
3007     break;
3008   case k_RotateImmediate:
3009     OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
3010     break;
3011   case k_ModifiedImmediate:
3012     OS << "<mod_imm #" << ModImm.Bits << ", #"
3013        <<  ModImm.Rot << ")>";
3014     break;
3015   case k_ConstantPoolImmediate:
3016     OS << "<constant_pool_imm #" << *getConstantPoolImm();
3017     break;
3018   case k_BitfieldDescriptor:
3019     OS << "<bitfield " << "lsb: " << Bitfield.LSB
3020        << ", width: " << Bitfield.Width << ">";
3021     break;
3022   case k_RegisterList:
3023   case k_DPRRegisterList:
3024   case k_SPRRegisterList: {
3025     OS << "<register_list ";
3026 
3027     const SmallVectorImpl<unsigned> &RegList = getRegList();
3028     for (SmallVectorImpl<unsigned>::const_iterator
3029            I = RegList.begin(), E = RegList.end(); I != E; ) {
3030       OS << *I;
3031       if (++I < E) OS << ", ";
3032     }
3033 
3034     OS << ">";
3035     break;
3036   }
3037   case k_VectorList:
3038     OS << "<vector_list " << VectorList.Count << " * "
3039        << VectorList.RegNum << ">";
3040     break;
3041   case k_VectorListAllLanes:
3042     OS << "<vector_list(all lanes) " << VectorList.Count << " * "
3043        << VectorList.RegNum << ">";
3044     break;
3045   case k_VectorListIndexed:
3046     OS << "<vector_list(lane " << VectorList.LaneIndex << ") "
3047        << VectorList.Count << " * " << VectorList.RegNum << ">";
3048     break;
3049   case k_Token:
3050     OS << "'" << getToken() << "'";
3051     break;
3052   case k_VectorIndex:
3053     OS << "<vectorindex " << getVectorIndex() << ">";
3054     break;
3055   }
3056 }
3057 
3058 /// @name Auto-generated Match Functions
3059 /// {
3060 
3061 static unsigned MatchRegisterName(StringRef Name);
3062 
3063 /// }
3064 
3065 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
3066                                  SMLoc &StartLoc, SMLoc &EndLoc) {
3067   const AsmToken &Tok = getParser().getTok();
3068   StartLoc = Tok.getLoc();
3069   EndLoc = Tok.getEndLoc();
3070   RegNo = tryParseRegister();
3071 
3072   return (RegNo == (unsigned)-1);
3073 }
3074 
3075 /// Try to parse a register name.  The token must be an Identifier when called,
3076 /// and if it is a register name the token is eaten and the register number is
3077 /// returned.  Otherwise return -1.
3078 ///
3079 int ARMAsmParser::tryParseRegister() {
3080   MCAsmParser &Parser = getParser();
3081   const AsmToken &Tok = Parser.getTok();
3082   if (Tok.isNot(AsmToken::Identifier)) return -1;
3083 
3084   std::string lowerCase = Tok.getString().lower();
3085   unsigned RegNum = MatchRegisterName(lowerCase);
3086   if (!RegNum) {
3087     RegNum = StringSwitch<unsigned>(lowerCase)
3088       .Case("r13", ARM::SP)
3089       .Case("r14", ARM::LR)
3090       .Case("r15", ARM::PC)
3091       .Case("ip", ARM::R12)
3092       // Additional register name aliases for 'gas' compatibility.
3093       .Case("a1", ARM::R0)
3094       .Case("a2", ARM::R1)
3095       .Case("a3", ARM::R2)
3096       .Case("a4", ARM::R3)
3097       .Case("v1", ARM::R4)
3098       .Case("v2", ARM::R5)
3099       .Case("v3", ARM::R6)
3100       .Case("v4", ARM::R7)
3101       .Case("v5", ARM::R8)
3102       .Case("v6", ARM::R9)
3103       .Case("v7", ARM::R10)
3104       .Case("v8", ARM::R11)
3105       .Case("sb", ARM::R9)
3106       .Case("sl", ARM::R10)
3107       .Case("fp", ARM::R11)
3108       .Default(0);
3109   }
3110   if (!RegNum) {
3111     // Check for aliases registered via .req. Canonicalize to lower case.
3112     // That's more consistent since register names are case insensitive, and
3113     // it's how the original entry was passed in from MC/MCParser/AsmParser.
3114     StringMap<unsigned>::const_iterator Entry = RegisterReqs.find(lowerCase);
3115     // If no match, return failure.
3116     if (Entry == RegisterReqs.end())
3117       return -1;
3118     Parser.Lex(); // Eat identifier token.
3119     return Entry->getValue();
3120   }
3121 
3122   // Some FPUs only have 16 D registers, so D16-D31 are invalid
3123   if (hasD16() && RegNum >= ARM::D16 && RegNum <= ARM::D31)
3124     return -1;
3125 
3126   Parser.Lex(); // Eat identifier token.
3127 
3128   return RegNum;
3129 }
3130 
3131 // Try to parse a shifter  (e.g., "lsl <amt>"). On success, return 0.
3132 // If a recoverable error occurs, return 1. If an irrecoverable error
3133 // occurs, return -1. An irrecoverable error is one where tokens have been
3134 // consumed in the process of trying to parse the shifter (i.e., when it is
3135 // indeed a shifter operand, but malformed).
3136 int ARMAsmParser::tryParseShiftRegister(OperandVector &Operands) {
3137   MCAsmParser &Parser = getParser();
3138   SMLoc S = Parser.getTok().getLoc();
3139   const AsmToken &Tok = Parser.getTok();
3140   if (Tok.isNot(AsmToken::Identifier))
3141     return -1;
3142 
3143   std::string lowerCase = Tok.getString().lower();
3144   ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
3145       .Case("asl", ARM_AM::lsl)
3146       .Case("lsl", ARM_AM::lsl)
3147       .Case("lsr", ARM_AM::lsr)
3148       .Case("asr", ARM_AM::asr)
3149       .Case("ror", ARM_AM::ror)
3150       .Case("rrx", ARM_AM::rrx)
3151       .Default(ARM_AM::no_shift);
3152 
3153   if (ShiftTy == ARM_AM::no_shift)
3154     return 1;
3155 
3156   Parser.Lex(); // Eat the operator.
3157 
3158   // The source register for the shift has already been added to the
3159   // operand list, so we need to pop it off and combine it into the shifted
3160   // register operand instead.
3161   std::unique_ptr<ARMOperand> PrevOp(
3162       (ARMOperand *)Operands.pop_back_val().release());
3163   if (!PrevOp->isReg())
3164     return Error(PrevOp->getStartLoc(), "shift must be of a register");
3165   int SrcReg = PrevOp->getReg();
3166 
3167   SMLoc EndLoc;
3168   int64_t Imm = 0;
3169   int ShiftReg = 0;
3170   if (ShiftTy == ARM_AM::rrx) {
3171     // RRX Doesn't have an explicit shift amount. The encoder expects
3172     // the shift register to be the same as the source register. Seems odd,
3173     // but OK.
3174     ShiftReg = SrcReg;
3175   } else {
3176     // Figure out if this is shifted by a constant or a register (for non-RRX).
3177     if (Parser.getTok().is(AsmToken::Hash) ||
3178         Parser.getTok().is(AsmToken::Dollar)) {
3179       Parser.Lex(); // Eat hash.
3180       SMLoc ImmLoc = Parser.getTok().getLoc();
3181       const MCExpr *ShiftExpr = nullptr;
3182       if (getParser().parseExpression(ShiftExpr, EndLoc)) {
3183         Error(ImmLoc, "invalid immediate shift value");
3184         return -1;
3185       }
3186       // The expression must be evaluatable as an immediate.
3187       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
3188       if (!CE) {
3189         Error(ImmLoc, "invalid immediate shift value");
3190         return -1;
3191       }
3192       // Range check the immediate.
3193       // lsl, ror: 0 <= imm <= 31
3194       // lsr, asr: 0 <= imm <= 32
3195       Imm = CE->getValue();
3196       if (Imm < 0 ||
3197           ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
3198           ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
3199         Error(ImmLoc, "immediate shift value out of range");
3200         return -1;
3201       }
3202       // shift by zero is a nop. Always send it through as lsl.
3203       // ('as' compatibility)
3204       if (Imm == 0)
3205         ShiftTy = ARM_AM::lsl;
3206     } else if (Parser.getTok().is(AsmToken::Identifier)) {
3207       SMLoc L = Parser.getTok().getLoc();
3208       EndLoc = Parser.getTok().getEndLoc();
3209       ShiftReg = tryParseRegister();
3210       if (ShiftReg == -1) {
3211         Error(L, "expected immediate or register in shift operand");
3212         return -1;
3213       }
3214     } else {
3215       Error(Parser.getTok().getLoc(),
3216             "expected immediate or register in shift operand");
3217       return -1;
3218     }
3219   }
3220 
3221   if (ShiftReg && ShiftTy != ARM_AM::rrx)
3222     Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
3223                                                          ShiftReg, Imm,
3224                                                          S, EndLoc));
3225   else
3226     Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
3227                                                           S, EndLoc));
3228 
3229   return 0;
3230 }
3231 
3232 
3233 /// Try to parse a register name.  The token must be an Identifier when called.
3234 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
3235 /// if there is a "writeback". 'true' if it's not a register.
3236 ///
3237 /// TODO this is likely to change to allow different register types and or to
3238 /// parse for a specific register type.
3239 bool ARMAsmParser::tryParseRegisterWithWriteBack(OperandVector &Operands) {
3240   MCAsmParser &Parser = getParser();
3241   const AsmToken &RegTok = Parser.getTok();
3242   int RegNo = tryParseRegister();
3243   if (RegNo == -1)
3244     return true;
3245 
3246   Operands.push_back(ARMOperand::CreateReg(RegNo, RegTok.getLoc(),
3247                                            RegTok.getEndLoc()));
3248 
3249   const AsmToken &ExclaimTok = Parser.getTok();
3250   if (ExclaimTok.is(AsmToken::Exclaim)) {
3251     Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
3252                                                ExclaimTok.getLoc()));
3253     Parser.Lex(); // Eat exclaim token
3254     return false;
3255   }
3256 
3257   // Also check for an index operand. This is only legal for vector registers,
3258   // but that'll get caught OK in operand matching, so we don't need to
3259   // explicitly filter everything else out here.
3260   if (Parser.getTok().is(AsmToken::LBrac)) {
3261     SMLoc SIdx = Parser.getTok().getLoc();
3262     Parser.Lex(); // Eat left bracket token.
3263 
3264     const MCExpr *ImmVal;
3265     if (getParser().parseExpression(ImmVal))
3266       return true;
3267     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
3268     if (!MCE)
3269       return TokError("immediate value expected for vector index");
3270 
3271     if (Parser.getTok().isNot(AsmToken::RBrac))
3272       return Error(Parser.getTok().getLoc(), "']' expected");
3273 
3274     SMLoc E = Parser.getTok().getEndLoc();
3275     Parser.Lex(); // Eat right bracket token.
3276 
3277     Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
3278                                                      SIdx, E,
3279                                                      getContext()));
3280   }
3281 
3282   return false;
3283 }
3284 
3285 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
3286 /// instruction with a symbolic operand name.
3287 /// We accept "crN" syntax for GAS compatibility.
3288 /// <operand-name> ::= <prefix><number>
3289 /// If CoprocOp is 'c', then:
3290 ///   <prefix> ::= c | cr
3291 /// If CoprocOp is 'p', then :
3292 ///   <prefix> ::= p
3293 /// <number> ::= integer in range [0, 15]
3294 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
3295   // Use the same layout as the tablegen'erated register name matcher. Ugly,
3296   // but efficient.
3297   if (Name.size() < 2 || Name[0] != CoprocOp)
3298     return -1;
3299   Name = (Name[1] == 'r') ? Name.drop_front(2) : Name.drop_front();
3300 
3301   switch (Name.size()) {
3302   default: return -1;
3303   case 1:
3304     switch (Name[0]) {
3305     default:  return -1;
3306     case '0': return 0;
3307     case '1': return 1;
3308     case '2': return 2;
3309     case '3': return 3;
3310     case '4': return 4;
3311     case '5': return 5;
3312     case '6': return 6;
3313     case '7': return 7;
3314     case '8': return 8;
3315     case '9': return 9;
3316     }
3317   case 2:
3318     if (Name[0] != '1')
3319       return -1;
3320     switch (Name[1]) {
3321     default:  return -1;
3322     // CP10 and CP11 are VFP/NEON and so vector instructions should be used.
3323     // However, old cores (v5/v6) did use them in that way.
3324     case '0': return 10;
3325     case '1': return 11;
3326     case '2': return 12;
3327     case '3': return 13;
3328     case '4': return 14;
3329     case '5': return 15;
3330     }
3331   }
3332 }
3333 
3334 /// parseITCondCode - Try to parse a condition code for an IT instruction.
3335 OperandMatchResultTy
3336 ARMAsmParser::parseITCondCode(OperandVector &Operands) {
3337   MCAsmParser &Parser = getParser();
3338   SMLoc S = Parser.getTok().getLoc();
3339   const AsmToken &Tok = Parser.getTok();
3340   if (!Tok.is(AsmToken::Identifier))
3341     return MatchOperand_NoMatch;
3342   unsigned CC = ARMCondCodeFromString(Tok.getString());
3343   if (CC == ~0U)
3344     return MatchOperand_NoMatch;
3345   Parser.Lex(); // Eat the token.
3346 
3347   Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
3348 
3349   return MatchOperand_Success;
3350 }
3351 
3352 /// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
3353 /// token must be an Identifier when called, and if it is a coprocessor
3354 /// number, the token is eaten and the operand is added to the operand list.
3355 OperandMatchResultTy
3356 ARMAsmParser::parseCoprocNumOperand(OperandVector &Operands) {
3357   MCAsmParser &Parser = getParser();
3358   SMLoc S = Parser.getTok().getLoc();
3359   const AsmToken &Tok = Parser.getTok();
3360   if (Tok.isNot(AsmToken::Identifier))
3361     return MatchOperand_NoMatch;
3362 
3363   int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
3364   if (Num == -1)
3365     return MatchOperand_NoMatch;
3366   // ARMv7 and v8 don't allow cp10/cp11 due to VFP/NEON specific instructions
3367   if ((hasV7Ops() || hasV8Ops()) && (Num == 10 || Num == 11))
3368     return MatchOperand_NoMatch;
3369 
3370   Parser.Lex(); // Eat identifier token.
3371   Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
3372   return MatchOperand_Success;
3373 }
3374 
3375 /// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
3376 /// token must be an Identifier when called, and if it is a coprocessor
3377 /// number, the token is eaten and the operand is added to the operand list.
3378 OperandMatchResultTy
3379 ARMAsmParser::parseCoprocRegOperand(OperandVector &Operands) {
3380   MCAsmParser &Parser = getParser();
3381   SMLoc S = Parser.getTok().getLoc();
3382   const AsmToken &Tok = Parser.getTok();
3383   if (Tok.isNot(AsmToken::Identifier))
3384     return MatchOperand_NoMatch;
3385 
3386   int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
3387   if (Reg == -1)
3388     return MatchOperand_NoMatch;
3389 
3390   Parser.Lex(); // Eat identifier token.
3391   Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
3392   return MatchOperand_Success;
3393 }
3394 
3395 /// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
3396 /// coproc_option : '{' imm0_255 '}'
3397 OperandMatchResultTy
3398 ARMAsmParser::parseCoprocOptionOperand(OperandVector &Operands) {
3399   MCAsmParser &Parser = getParser();
3400   SMLoc S = Parser.getTok().getLoc();
3401 
3402   // If this isn't a '{', this isn't a coprocessor immediate operand.
3403   if (Parser.getTok().isNot(AsmToken::LCurly))
3404     return MatchOperand_NoMatch;
3405   Parser.Lex(); // Eat the '{'
3406 
3407   const MCExpr *Expr;
3408   SMLoc Loc = Parser.getTok().getLoc();
3409   if (getParser().parseExpression(Expr)) {
3410     Error(Loc, "illegal expression");
3411     return MatchOperand_ParseFail;
3412   }
3413   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3414   if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
3415     Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
3416     return MatchOperand_ParseFail;
3417   }
3418   int Val = CE->getValue();
3419 
3420   // Check for and consume the closing '}'
3421   if (Parser.getTok().isNot(AsmToken::RCurly))
3422     return MatchOperand_ParseFail;
3423   SMLoc E = Parser.getTok().getEndLoc();
3424   Parser.Lex(); // Eat the '}'
3425 
3426   Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
3427   return MatchOperand_Success;
3428 }
3429 
3430 // For register list parsing, we need to map from raw GPR register numbering
3431 // to the enumeration values. The enumeration values aren't sorted by
3432 // register number due to our using "sp", "lr" and "pc" as canonical names.
3433 static unsigned getNextRegister(unsigned Reg) {
3434   // If this is a GPR, we need to do it manually, otherwise we can rely
3435   // on the sort ordering of the enumeration since the other reg-classes
3436   // are sane.
3437   if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
3438     return Reg + 1;
3439   switch(Reg) {
3440   default: llvm_unreachable("Invalid GPR number!");
3441   case ARM::R0:  return ARM::R1;  case ARM::R1:  return ARM::R2;
3442   case ARM::R2:  return ARM::R3;  case ARM::R3:  return ARM::R4;
3443   case ARM::R4:  return ARM::R5;  case ARM::R5:  return ARM::R6;
3444   case ARM::R6:  return ARM::R7;  case ARM::R7:  return ARM::R8;
3445   case ARM::R8:  return ARM::R9;  case ARM::R9:  return ARM::R10;
3446   case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
3447   case ARM::R12: return ARM::SP;  case ARM::SP:  return ARM::LR;
3448   case ARM::LR:  return ARM::PC;  case ARM::PC:  return ARM::R0;
3449   }
3450 }
3451 
3452 /// Parse a register list.
3453 bool ARMAsmParser::parseRegisterList(OperandVector &Operands) {
3454   MCAsmParser &Parser = getParser();
3455   if (Parser.getTok().isNot(AsmToken::LCurly))
3456     return TokError("Token is not a Left Curly Brace");
3457   SMLoc S = Parser.getTok().getLoc();
3458   Parser.Lex(); // Eat '{' token.
3459   SMLoc RegLoc = Parser.getTok().getLoc();
3460 
3461   // Check the first register in the list to see what register class
3462   // this is a list of.
3463   int Reg = tryParseRegister();
3464   if (Reg == -1)
3465     return Error(RegLoc, "register expected");
3466 
3467   // The reglist instructions have at most 16 registers, so reserve
3468   // space for that many.
3469   int EReg = 0;
3470   SmallVector<std::pair<unsigned, unsigned>, 16> Registers;
3471 
3472   // Allow Q regs and just interpret them as the two D sub-registers.
3473   if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3474     Reg = getDRegFromQReg(Reg);
3475     EReg = MRI->getEncodingValue(Reg);
3476     Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3477     ++Reg;
3478   }
3479   const MCRegisterClass *RC;
3480   if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
3481     RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
3482   else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
3483     RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
3484   else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
3485     RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
3486   else
3487     return Error(RegLoc, "invalid register in register list");
3488 
3489   // Store the register.
3490   EReg = MRI->getEncodingValue(Reg);
3491   Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3492 
3493   // This starts immediately after the first register token in the list,
3494   // so we can see either a comma or a minus (range separator) as a legal
3495   // next token.
3496   while (Parser.getTok().is(AsmToken::Comma) ||
3497          Parser.getTok().is(AsmToken::Minus)) {
3498     if (Parser.getTok().is(AsmToken::Minus)) {
3499       Parser.Lex(); // Eat the minus.
3500       SMLoc AfterMinusLoc = Parser.getTok().getLoc();
3501       int EndReg = tryParseRegister();
3502       if (EndReg == -1)
3503         return Error(AfterMinusLoc, "register expected");
3504       // Allow Q regs and just interpret them as the two D sub-registers.
3505       if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
3506         EndReg = getDRegFromQReg(EndReg) + 1;
3507       // If the register is the same as the start reg, there's nothing
3508       // more to do.
3509       if (Reg == EndReg)
3510         continue;
3511       // The register must be in the same register class as the first.
3512       if (!RC->contains(EndReg))
3513         return Error(AfterMinusLoc, "invalid register in register list");
3514       // Ranges must go from low to high.
3515       if (MRI->getEncodingValue(Reg) > MRI->getEncodingValue(EndReg))
3516         return Error(AfterMinusLoc, "bad range in register list");
3517 
3518       // Add all the registers in the range to the register list.
3519       while (Reg != EndReg) {
3520         Reg = getNextRegister(Reg);
3521         EReg = MRI->getEncodingValue(Reg);
3522         Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3523       }
3524       continue;
3525     }
3526     Parser.Lex(); // Eat the comma.
3527     RegLoc = Parser.getTok().getLoc();
3528     int OldReg = Reg;
3529     const AsmToken RegTok = Parser.getTok();
3530     Reg = tryParseRegister();
3531     if (Reg == -1)
3532       return Error(RegLoc, "register expected");
3533     // Allow Q regs and just interpret them as the two D sub-registers.
3534     bool isQReg = false;
3535     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3536       Reg = getDRegFromQReg(Reg);
3537       isQReg = true;
3538     }
3539     // The register must be in the same register class as the first.
3540     if (!RC->contains(Reg))
3541       return Error(RegLoc, "invalid register in register list");
3542     // List must be monotonically increasing.
3543     if (MRI->getEncodingValue(Reg) < MRI->getEncodingValue(OldReg)) {
3544       if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
3545         Warning(RegLoc, "register list not in ascending order");
3546       else
3547         return Error(RegLoc, "register list not in ascending order");
3548     }
3549     if (MRI->getEncodingValue(Reg) == MRI->getEncodingValue(OldReg)) {
3550       Warning(RegLoc, "duplicated register (" + RegTok.getString() +
3551               ") in register list");
3552       continue;
3553     }
3554     // VFP register lists must also be contiguous.
3555     if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
3556         Reg != OldReg + 1)
3557       return Error(RegLoc, "non-contiguous register range");
3558     EReg = MRI->getEncodingValue(Reg);
3559     Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3560     if (isQReg) {
3561       EReg = MRI->getEncodingValue(++Reg);
3562       Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3563     }
3564   }
3565 
3566   if (Parser.getTok().isNot(AsmToken::RCurly))
3567     return Error(Parser.getTok().getLoc(), "'}' expected");
3568   SMLoc E = Parser.getTok().getEndLoc();
3569   Parser.Lex(); // Eat '}' token.
3570 
3571   // Push the register list operand.
3572   Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
3573 
3574   // The ARM system instruction variants for LDM/STM have a '^' token here.
3575   if (Parser.getTok().is(AsmToken::Caret)) {
3576     Operands.push_back(ARMOperand::CreateToken("^",Parser.getTok().getLoc()));
3577     Parser.Lex(); // Eat '^' token.
3578   }
3579 
3580   return false;
3581 }
3582 
3583 // Helper function to parse the lane index for vector lists.
3584 OperandMatchResultTy ARMAsmParser::
3585 parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index, SMLoc &EndLoc) {
3586   MCAsmParser &Parser = getParser();
3587   Index = 0; // Always return a defined index value.
3588   if (Parser.getTok().is(AsmToken::LBrac)) {
3589     Parser.Lex(); // Eat the '['.
3590     if (Parser.getTok().is(AsmToken::RBrac)) {
3591       // "Dn[]" is the 'all lanes' syntax.
3592       LaneKind = AllLanes;
3593       EndLoc = Parser.getTok().getEndLoc();
3594       Parser.Lex(); // Eat the ']'.
3595       return MatchOperand_Success;
3596     }
3597 
3598     // There's an optional '#' token here. Normally there wouldn't be, but
3599     // inline assemble puts one in, and it's friendly to accept that.
3600     if (Parser.getTok().is(AsmToken::Hash))
3601       Parser.Lex(); // Eat '#' or '$'.
3602 
3603     const MCExpr *LaneIndex;
3604     SMLoc Loc = Parser.getTok().getLoc();
3605     if (getParser().parseExpression(LaneIndex)) {
3606       Error(Loc, "illegal expression");
3607       return MatchOperand_ParseFail;
3608     }
3609     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LaneIndex);
3610     if (!CE) {
3611       Error(Loc, "lane index must be empty or an integer");
3612       return MatchOperand_ParseFail;
3613     }
3614     if (Parser.getTok().isNot(AsmToken::RBrac)) {
3615       Error(Parser.getTok().getLoc(), "']' expected");
3616       return MatchOperand_ParseFail;
3617     }
3618     EndLoc = Parser.getTok().getEndLoc();
3619     Parser.Lex(); // Eat the ']'.
3620     int64_t Val = CE->getValue();
3621 
3622     // FIXME: Make this range check context sensitive for .8, .16, .32.
3623     if (Val < 0 || Val > 7) {
3624       Error(Parser.getTok().getLoc(), "lane index out of range");
3625       return MatchOperand_ParseFail;
3626     }
3627     Index = Val;
3628     LaneKind = IndexedLane;
3629     return MatchOperand_Success;
3630   }
3631   LaneKind = NoLanes;
3632   return MatchOperand_Success;
3633 }
3634 
3635 // parse a vector register list
3636 OperandMatchResultTy
3637 ARMAsmParser::parseVectorList(OperandVector &Operands) {
3638   MCAsmParser &Parser = getParser();
3639   VectorLaneTy LaneKind;
3640   unsigned LaneIndex;
3641   SMLoc S = Parser.getTok().getLoc();
3642   // As an extension (to match gas), support a plain D register or Q register
3643   // (without encosing curly braces) as a single or double entry list,
3644   // respectively.
3645   if (Parser.getTok().is(AsmToken::Identifier)) {
3646     SMLoc E = Parser.getTok().getEndLoc();
3647     int Reg = tryParseRegister();
3648     if (Reg == -1)
3649       return MatchOperand_NoMatch;
3650     if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg)) {
3651       OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex, E);
3652       if (Res != MatchOperand_Success)
3653         return Res;
3654       switch (LaneKind) {
3655       case NoLanes:
3656         Operands.push_back(ARMOperand::CreateVectorList(Reg, 1, false, S, E));
3657         break;
3658       case AllLanes:
3659         Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 1, false,
3660                                                                 S, E));
3661         break;
3662       case IndexedLane:
3663         Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 1,
3664                                                                LaneIndex,
3665                                                                false, S, E));
3666         break;
3667       }
3668       return MatchOperand_Success;
3669     }
3670     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3671       Reg = getDRegFromQReg(Reg);
3672       OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex, E);
3673       if (Res != MatchOperand_Success)
3674         return Res;
3675       switch (LaneKind) {
3676       case NoLanes:
3677         Reg = MRI->getMatchingSuperReg(Reg, ARM::dsub_0,
3678                                    &ARMMCRegisterClasses[ARM::DPairRegClassID]);
3679         Operands.push_back(ARMOperand::CreateVectorList(Reg, 2, false, S, E));
3680         break;
3681       case AllLanes:
3682         Reg = MRI->getMatchingSuperReg(Reg, ARM::dsub_0,
3683                                    &ARMMCRegisterClasses[ARM::DPairRegClassID]);
3684         Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 2, false,
3685                                                                 S, E));
3686         break;
3687       case IndexedLane:
3688         Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 2,
3689                                                                LaneIndex,
3690                                                                false, S, E));
3691         break;
3692       }
3693       return MatchOperand_Success;
3694     }
3695     Error(S, "vector register expected");
3696     return MatchOperand_ParseFail;
3697   }
3698 
3699   if (Parser.getTok().isNot(AsmToken::LCurly))
3700     return MatchOperand_NoMatch;
3701 
3702   Parser.Lex(); // Eat '{' token.
3703   SMLoc RegLoc = Parser.getTok().getLoc();
3704 
3705   int Reg = tryParseRegister();
3706   if (Reg == -1) {
3707     Error(RegLoc, "register expected");
3708     return MatchOperand_ParseFail;
3709   }
3710   unsigned Count = 1;
3711   int Spacing = 0;
3712   unsigned FirstReg = Reg;
3713   // The list is of D registers, but we also allow Q regs and just interpret
3714   // them as the two D sub-registers.
3715   if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3716     FirstReg = Reg = getDRegFromQReg(Reg);
3717     Spacing = 1; // double-spacing requires explicit D registers, otherwise
3718                  // it's ambiguous with four-register single spaced.
3719     ++Reg;
3720     ++Count;
3721   }
3722 
3723   SMLoc E;
3724   if (parseVectorLane(LaneKind, LaneIndex, E) != MatchOperand_Success)
3725     return MatchOperand_ParseFail;
3726 
3727   while (Parser.getTok().is(AsmToken::Comma) ||
3728          Parser.getTok().is(AsmToken::Minus)) {
3729     if (Parser.getTok().is(AsmToken::Minus)) {
3730       if (!Spacing)
3731         Spacing = 1; // Register range implies a single spaced list.
3732       else if (Spacing == 2) {
3733         Error(Parser.getTok().getLoc(),
3734               "sequential registers in double spaced list");
3735         return MatchOperand_ParseFail;
3736       }
3737       Parser.Lex(); // Eat the minus.
3738       SMLoc AfterMinusLoc = Parser.getTok().getLoc();
3739       int EndReg = tryParseRegister();
3740       if (EndReg == -1) {
3741         Error(AfterMinusLoc, "register expected");
3742         return MatchOperand_ParseFail;
3743       }
3744       // Allow Q regs and just interpret them as the two D sub-registers.
3745       if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
3746         EndReg = getDRegFromQReg(EndReg) + 1;
3747       // If the register is the same as the start reg, there's nothing
3748       // more to do.
3749       if (Reg == EndReg)
3750         continue;
3751       // The register must be in the same register class as the first.
3752       if (!ARMMCRegisterClasses[ARM::DPRRegClassID].contains(EndReg)) {
3753         Error(AfterMinusLoc, "invalid register in register list");
3754         return MatchOperand_ParseFail;
3755       }
3756       // Ranges must go from low to high.
3757       if (Reg > EndReg) {
3758         Error(AfterMinusLoc, "bad range in register list");
3759         return MatchOperand_ParseFail;
3760       }
3761       // Parse the lane specifier if present.
3762       VectorLaneTy NextLaneKind;
3763       unsigned NextLaneIndex;
3764       if (parseVectorLane(NextLaneKind, NextLaneIndex, E) !=
3765           MatchOperand_Success)
3766         return MatchOperand_ParseFail;
3767       if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3768         Error(AfterMinusLoc, "mismatched lane index in register list");
3769         return MatchOperand_ParseFail;
3770       }
3771 
3772       // Add all the registers in the range to the register list.
3773       Count += EndReg - Reg;
3774       Reg = EndReg;
3775       continue;
3776     }
3777     Parser.Lex(); // Eat the comma.
3778     RegLoc = Parser.getTok().getLoc();
3779     int OldReg = Reg;
3780     Reg = tryParseRegister();
3781     if (Reg == -1) {
3782       Error(RegLoc, "register expected");
3783       return MatchOperand_ParseFail;
3784     }
3785     // vector register lists must be contiguous.
3786     // It's OK to use the enumeration values directly here rather, as the
3787     // VFP register classes have the enum sorted properly.
3788     //
3789     // The list is of D registers, but we also allow Q regs and just interpret
3790     // them as the two D sub-registers.
3791     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3792       if (!Spacing)
3793         Spacing = 1; // Register range implies a single spaced list.
3794       else if (Spacing == 2) {
3795         Error(RegLoc,
3796               "invalid register in double-spaced list (must be 'D' register')");
3797         return MatchOperand_ParseFail;
3798       }
3799       Reg = getDRegFromQReg(Reg);
3800       if (Reg != OldReg + 1) {
3801         Error(RegLoc, "non-contiguous register range");
3802         return MatchOperand_ParseFail;
3803       }
3804       ++Reg;
3805       Count += 2;
3806       // Parse the lane specifier if present.
3807       VectorLaneTy NextLaneKind;
3808       unsigned NextLaneIndex;
3809       SMLoc LaneLoc = Parser.getTok().getLoc();
3810       if (parseVectorLane(NextLaneKind, NextLaneIndex, E) !=
3811           MatchOperand_Success)
3812         return MatchOperand_ParseFail;
3813       if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3814         Error(LaneLoc, "mismatched lane index in register list");
3815         return MatchOperand_ParseFail;
3816       }
3817       continue;
3818     }
3819     // Normal D register.
3820     // Figure out the register spacing (single or double) of the list if
3821     // we don't know it already.
3822     if (!Spacing)
3823       Spacing = 1 + (Reg == OldReg + 2);
3824 
3825     // Just check that it's contiguous and keep going.
3826     if (Reg != OldReg + Spacing) {
3827       Error(RegLoc, "non-contiguous register range");
3828       return MatchOperand_ParseFail;
3829     }
3830     ++Count;
3831     // Parse the lane specifier if present.
3832     VectorLaneTy NextLaneKind;
3833     unsigned NextLaneIndex;
3834     SMLoc EndLoc = Parser.getTok().getLoc();
3835     if (parseVectorLane(NextLaneKind, NextLaneIndex, E) != MatchOperand_Success)
3836       return MatchOperand_ParseFail;
3837     if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3838       Error(EndLoc, "mismatched lane index in register list");
3839       return MatchOperand_ParseFail;
3840     }
3841   }
3842 
3843   if (Parser.getTok().isNot(AsmToken::RCurly)) {
3844     Error(Parser.getTok().getLoc(), "'}' expected");
3845     return MatchOperand_ParseFail;
3846   }
3847   E = Parser.getTok().getEndLoc();
3848   Parser.Lex(); // Eat '}' token.
3849 
3850   switch (LaneKind) {
3851   case NoLanes:
3852     // Two-register operands have been converted to the
3853     // composite register classes.
3854     if (Count == 2) {
3855       const MCRegisterClass *RC = (Spacing == 1) ?
3856         &ARMMCRegisterClasses[ARM::DPairRegClassID] :
3857         &ARMMCRegisterClasses[ARM::DPairSpcRegClassID];
3858       FirstReg = MRI->getMatchingSuperReg(FirstReg, ARM::dsub_0, RC);
3859     }
3860 
3861     Operands.push_back(ARMOperand::CreateVectorList(FirstReg, Count,
3862                                                     (Spacing == 2), S, E));
3863     break;
3864   case AllLanes:
3865     // Two-register operands have been converted to the
3866     // composite register classes.
3867     if (Count == 2) {
3868       const MCRegisterClass *RC = (Spacing == 1) ?
3869         &ARMMCRegisterClasses[ARM::DPairRegClassID] :
3870         &ARMMCRegisterClasses[ARM::DPairSpcRegClassID];
3871       FirstReg = MRI->getMatchingSuperReg(FirstReg, ARM::dsub_0, RC);
3872     }
3873     Operands.push_back(ARMOperand::CreateVectorListAllLanes(FirstReg, Count,
3874                                                             (Spacing == 2),
3875                                                             S, E));
3876     break;
3877   case IndexedLane:
3878     Operands.push_back(ARMOperand::CreateVectorListIndexed(FirstReg, Count,
3879                                                            LaneIndex,
3880                                                            (Spacing == 2),
3881                                                            S, E));
3882     break;
3883   }
3884   return MatchOperand_Success;
3885 }
3886 
3887 /// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
3888 OperandMatchResultTy
3889 ARMAsmParser::parseMemBarrierOptOperand(OperandVector &Operands) {
3890   MCAsmParser &Parser = getParser();
3891   SMLoc S = Parser.getTok().getLoc();
3892   const AsmToken &Tok = Parser.getTok();
3893   unsigned Opt;
3894 
3895   if (Tok.is(AsmToken::Identifier)) {
3896     StringRef OptStr = Tok.getString();
3897 
3898     Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()).lower())
3899       .Case("sy",    ARM_MB::SY)
3900       .Case("st",    ARM_MB::ST)
3901       .Case("ld",    ARM_MB::LD)
3902       .Case("sh",    ARM_MB::ISH)
3903       .Case("ish",   ARM_MB::ISH)
3904       .Case("shst",  ARM_MB::ISHST)
3905       .Case("ishst", ARM_MB::ISHST)
3906       .Case("ishld", ARM_MB::ISHLD)
3907       .Case("nsh",   ARM_MB::NSH)
3908       .Case("un",    ARM_MB::NSH)
3909       .Case("nshst", ARM_MB::NSHST)
3910       .Case("nshld", ARM_MB::NSHLD)
3911       .Case("unst",  ARM_MB::NSHST)
3912       .Case("osh",   ARM_MB::OSH)
3913       .Case("oshst", ARM_MB::OSHST)
3914       .Case("oshld", ARM_MB::OSHLD)
3915       .Default(~0U);
3916 
3917     // ishld, oshld, nshld and ld are only available from ARMv8.
3918     if (!hasV8Ops() && (Opt == ARM_MB::ISHLD || Opt == ARM_MB::OSHLD ||
3919                         Opt == ARM_MB::NSHLD || Opt == ARM_MB::LD))
3920       Opt = ~0U;
3921 
3922     if (Opt == ~0U)
3923       return MatchOperand_NoMatch;
3924 
3925     Parser.Lex(); // Eat identifier token.
3926   } else if (Tok.is(AsmToken::Hash) ||
3927              Tok.is(AsmToken::Dollar) ||
3928              Tok.is(AsmToken::Integer)) {
3929     if (Parser.getTok().isNot(AsmToken::Integer))
3930       Parser.Lex(); // Eat '#' or '$'.
3931     SMLoc Loc = Parser.getTok().getLoc();
3932 
3933     const MCExpr *MemBarrierID;
3934     if (getParser().parseExpression(MemBarrierID)) {
3935       Error(Loc, "illegal expression");
3936       return MatchOperand_ParseFail;
3937     }
3938 
3939     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(MemBarrierID);
3940     if (!CE) {
3941       Error(Loc, "constant expression expected");
3942       return MatchOperand_ParseFail;
3943     }
3944 
3945     int Val = CE->getValue();
3946     if (Val & ~0xf) {
3947       Error(Loc, "immediate value out of range");
3948       return MatchOperand_ParseFail;
3949     }
3950 
3951     Opt = ARM_MB::RESERVED_0 + Val;
3952   } else
3953     return MatchOperand_ParseFail;
3954 
3955   Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
3956   return MatchOperand_Success;
3957 }
3958 
3959 /// parseInstSyncBarrierOptOperand - Try to parse ISB inst sync barrier options.
3960 OperandMatchResultTy
3961 ARMAsmParser::parseInstSyncBarrierOptOperand(OperandVector &Operands) {
3962   MCAsmParser &Parser = getParser();
3963   SMLoc S = Parser.getTok().getLoc();
3964   const AsmToken &Tok = Parser.getTok();
3965   unsigned Opt;
3966 
3967   if (Tok.is(AsmToken::Identifier)) {
3968     StringRef OptStr = Tok.getString();
3969 
3970     if (OptStr.equals_lower("sy"))
3971       Opt = ARM_ISB::SY;
3972     else
3973       return MatchOperand_NoMatch;
3974 
3975     Parser.Lex(); // Eat identifier token.
3976   } else if (Tok.is(AsmToken::Hash) ||
3977              Tok.is(AsmToken::Dollar) ||
3978              Tok.is(AsmToken::Integer)) {
3979     if (Parser.getTok().isNot(AsmToken::Integer))
3980       Parser.Lex(); // Eat '#' or '$'.
3981     SMLoc Loc = Parser.getTok().getLoc();
3982 
3983     const MCExpr *ISBarrierID;
3984     if (getParser().parseExpression(ISBarrierID)) {
3985       Error(Loc, "illegal expression");
3986       return MatchOperand_ParseFail;
3987     }
3988 
3989     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ISBarrierID);
3990     if (!CE) {
3991       Error(Loc, "constant expression expected");
3992       return MatchOperand_ParseFail;
3993     }
3994 
3995     int Val = CE->getValue();
3996     if (Val & ~0xf) {
3997       Error(Loc, "immediate value out of range");
3998       return MatchOperand_ParseFail;
3999     }
4000 
4001     Opt = ARM_ISB::RESERVED_0 + Val;
4002   } else
4003     return MatchOperand_ParseFail;
4004 
4005   Operands.push_back(ARMOperand::CreateInstSyncBarrierOpt(
4006           (ARM_ISB::InstSyncBOpt)Opt, S));
4007   return MatchOperand_Success;
4008 }
4009 
4010 
4011 /// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
4012 OperandMatchResultTy
4013 ARMAsmParser::parseProcIFlagsOperand(OperandVector &Operands) {
4014   MCAsmParser &Parser = getParser();
4015   SMLoc S = Parser.getTok().getLoc();
4016   const AsmToken &Tok = Parser.getTok();
4017   if (!Tok.is(AsmToken::Identifier))
4018     return MatchOperand_NoMatch;
4019   StringRef IFlagsStr = Tok.getString();
4020 
4021   // An iflags string of "none" is interpreted to mean that none of the AIF
4022   // bits are set.  Not a terribly useful instruction, but a valid encoding.
4023   unsigned IFlags = 0;
4024   if (IFlagsStr != "none") {
4025         for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
4026       unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
4027         .Case("a", ARM_PROC::A)
4028         .Case("i", ARM_PROC::I)
4029         .Case("f", ARM_PROC::F)
4030         .Default(~0U);
4031 
4032       // If some specific iflag is already set, it means that some letter is
4033       // present more than once, this is not acceptable.
4034       if (Flag == ~0U || (IFlags & Flag))
4035         return MatchOperand_NoMatch;
4036 
4037       IFlags |= Flag;
4038     }
4039   }
4040 
4041   Parser.Lex(); // Eat identifier token.
4042   Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
4043   return MatchOperand_Success;
4044 }
4045 
4046 /// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
4047 OperandMatchResultTy
4048 ARMAsmParser::parseMSRMaskOperand(OperandVector &Operands) {
4049   MCAsmParser &Parser = getParser();
4050   SMLoc S = Parser.getTok().getLoc();
4051   const AsmToken &Tok = Parser.getTok();
4052   if (!Tok.is(AsmToken::Identifier))
4053     return MatchOperand_NoMatch;
4054   StringRef Mask = Tok.getString();
4055 
4056   if (isMClass()) {
4057     auto TheReg = ARMSysReg::lookupMClassSysRegByName(Mask.lower());
4058     if (!TheReg || !TheReg->hasRequiredFeatures(getSTI().getFeatureBits()))
4059       return MatchOperand_NoMatch;
4060 
4061     unsigned SYSmvalue = TheReg->Encoding & 0xFFF;
4062 
4063     Parser.Lex(); // Eat identifier token.
4064     Operands.push_back(ARMOperand::CreateMSRMask(SYSmvalue, S));
4065     return MatchOperand_Success;
4066   }
4067 
4068   // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
4069   size_t Start = 0, Next = Mask.find('_');
4070   StringRef Flags = "";
4071   std::string SpecReg = Mask.slice(Start, Next).lower();
4072   if (Next != StringRef::npos)
4073     Flags = Mask.slice(Next+1, Mask.size());
4074 
4075   // FlagsVal contains the complete mask:
4076   // 3-0: Mask
4077   // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
4078   unsigned FlagsVal = 0;
4079 
4080   if (SpecReg == "apsr") {
4081     FlagsVal = StringSwitch<unsigned>(Flags)
4082     .Case("nzcvq",  0x8) // same as CPSR_f
4083     .Case("g",      0x4) // same as CPSR_s
4084     .Case("nzcvqg", 0xc) // same as CPSR_fs
4085     .Default(~0U);
4086 
4087     if (FlagsVal == ~0U) {
4088       if (!Flags.empty())
4089         return MatchOperand_NoMatch;
4090       else
4091         FlagsVal = 8; // No flag
4092     }
4093   } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
4094     // cpsr_all is an alias for cpsr_fc, as is plain cpsr.
4095     if (Flags == "all" || Flags == "")
4096       Flags = "fc";
4097     for (int i = 0, e = Flags.size(); i != e; ++i) {
4098       unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
4099       .Case("c", 1)
4100       .Case("x", 2)
4101       .Case("s", 4)
4102       .Case("f", 8)
4103       .Default(~0U);
4104 
4105       // If some specific flag is already set, it means that some letter is
4106       // present more than once, this is not acceptable.
4107       if (Flag == ~0U || (FlagsVal & Flag))
4108         return MatchOperand_NoMatch;
4109       FlagsVal |= Flag;
4110     }
4111   } else // No match for special register.
4112     return MatchOperand_NoMatch;
4113 
4114   // Special register without flags is NOT equivalent to "fc" flags.
4115   // NOTE: This is a divergence from gas' behavior.  Uncommenting the following
4116   // two lines would enable gas compatibility at the expense of breaking
4117   // round-tripping.
4118   //
4119   // if (!FlagsVal)
4120   //  FlagsVal = 0x9;
4121 
4122   // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
4123   if (SpecReg == "spsr")
4124     FlagsVal |= 16;
4125 
4126   Parser.Lex(); // Eat identifier token.
4127   Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
4128   return MatchOperand_Success;
4129 }
4130 
4131 /// parseBankedRegOperand - Try to parse a banked register (e.g. "lr_irq") for
4132 /// use in the MRS/MSR instructions added to support virtualization.
4133 OperandMatchResultTy
4134 ARMAsmParser::parseBankedRegOperand(OperandVector &Operands) {
4135   MCAsmParser &Parser = getParser();
4136   SMLoc S = Parser.getTok().getLoc();
4137   const AsmToken &Tok = Parser.getTok();
4138   if (!Tok.is(AsmToken::Identifier))
4139     return MatchOperand_NoMatch;
4140   StringRef RegName = Tok.getString();
4141 
4142   auto TheReg = ARMBankedReg::lookupBankedRegByName(RegName.lower());
4143   if (!TheReg)
4144     return MatchOperand_NoMatch;
4145   unsigned Encoding = TheReg->Encoding;
4146 
4147   Parser.Lex(); // Eat identifier token.
4148   Operands.push_back(ARMOperand::CreateBankedReg(Encoding, S));
4149   return MatchOperand_Success;
4150 }
4151 
4152 OperandMatchResultTy
4153 ARMAsmParser::parsePKHImm(OperandVector &Operands, StringRef Op, int Low,
4154                           int High) {
4155   MCAsmParser &Parser = getParser();
4156   const AsmToken &Tok = Parser.getTok();
4157   if (Tok.isNot(AsmToken::Identifier)) {
4158     Error(Parser.getTok().getLoc(), Op + " operand expected.");
4159     return MatchOperand_ParseFail;
4160   }
4161   StringRef ShiftName = Tok.getString();
4162   std::string LowerOp = Op.lower();
4163   std::string UpperOp = Op.upper();
4164   if (ShiftName != LowerOp && ShiftName != UpperOp) {
4165     Error(Parser.getTok().getLoc(), Op + " operand expected.");
4166     return MatchOperand_ParseFail;
4167   }
4168   Parser.Lex(); // Eat shift type token.
4169 
4170   // There must be a '#' and a shift amount.
4171   if (Parser.getTok().isNot(AsmToken::Hash) &&
4172       Parser.getTok().isNot(AsmToken::Dollar)) {
4173     Error(Parser.getTok().getLoc(), "'#' expected");
4174     return MatchOperand_ParseFail;
4175   }
4176   Parser.Lex(); // Eat hash token.
4177 
4178   const MCExpr *ShiftAmount;
4179   SMLoc Loc = Parser.getTok().getLoc();
4180   SMLoc EndLoc;
4181   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
4182     Error(Loc, "illegal expression");
4183     return MatchOperand_ParseFail;
4184   }
4185   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
4186   if (!CE) {
4187     Error(Loc, "constant expression expected");
4188     return MatchOperand_ParseFail;
4189   }
4190   int Val = CE->getValue();
4191   if (Val < Low || Val > High) {
4192     Error(Loc, "immediate value out of range");
4193     return MatchOperand_ParseFail;
4194   }
4195 
4196   Operands.push_back(ARMOperand::CreateImm(CE, Loc, EndLoc));
4197 
4198   return MatchOperand_Success;
4199 }
4200 
4201 OperandMatchResultTy
4202 ARMAsmParser::parseSetEndImm(OperandVector &Operands) {
4203   MCAsmParser &Parser = getParser();
4204   const AsmToken &Tok = Parser.getTok();
4205   SMLoc S = Tok.getLoc();
4206   if (Tok.isNot(AsmToken::Identifier)) {
4207     Error(S, "'be' or 'le' operand expected");
4208     return MatchOperand_ParseFail;
4209   }
4210   int Val = StringSwitch<int>(Tok.getString().lower())
4211     .Case("be", 1)
4212     .Case("le", 0)
4213     .Default(-1);
4214   Parser.Lex(); // Eat the token.
4215 
4216   if (Val == -1) {
4217     Error(S, "'be' or 'le' operand expected");
4218     return MatchOperand_ParseFail;
4219   }
4220   Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::create(Val,
4221                                                                   getContext()),
4222                                            S, Tok.getEndLoc()));
4223   return MatchOperand_Success;
4224 }
4225 
4226 /// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
4227 /// instructions. Legal values are:
4228 ///     lsl #n  'n' in [0,31]
4229 ///     asr #n  'n' in [1,32]
4230 ///             n == 32 encoded as n == 0.
4231 OperandMatchResultTy
4232 ARMAsmParser::parseShifterImm(OperandVector &Operands) {
4233   MCAsmParser &Parser = getParser();
4234   const AsmToken &Tok = Parser.getTok();
4235   SMLoc S = Tok.getLoc();
4236   if (Tok.isNot(AsmToken::Identifier)) {
4237     Error(S, "shift operator 'asr' or 'lsl' expected");
4238     return MatchOperand_ParseFail;
4239   }
4240   StringRef ShiftName = Tok.getString();
4241   bool isASR;
4242   if (ShiftName == "lsl" || ShiftName == "LSL")
4243     isASR = false;
4244   else if (ShiftName == "asr" || ShiftName == "ASR")
4245     isASR = true;
4246   else {
4247     Error(S, "shift operator 'asr' or 'lsl' expected");
4248     return MatchOperand_ParseFail;
4249   }
4250   Parser.Lex(); // Eat the operator.
4251 
4252   // A '#' and a shift amount.
4253   if (Parser.getTok().isNot(AsmToken::Hash) &&
4254       Parser.getTok().isNot(AsmToken::Dollar)) {
4255     Error(Parser.getTok().getLoc(), "'#' expected");
4256     return MatchOperand_ParseFail;
4257   }
4258   Parser.Lex(); // Eat hash token.
4259   SMLoc ExLoc = Parser.getTok().getLoc();
4260 
4261   const MCExpr *ShiftAmount;
4262   SMLoc EndLoc;
4263   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
4264     Error(ExLoc, "malformed shift expression");
4265     return MatchOperand_ParseFail;
4266   }
4267   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
4268   if (!CE) {
4269     Error(ExLoc, "shift amount must be an immediate");
4270     return MatchOperand_ParseFail;
4271   }
4272 
4273   int64_t Val = CE->getValue();
4274   if (isASR) {
4275     // Shift amount must be in [1,32]
4276     if (Val < 1 || Val > 32) {
4277       Error(ExLoc, "'asr' shift amount must be in range [1,32]");
4278       return MatchOperand_ParseFail;
4279     }
4280     // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
4281     if (isThumb() && Val == 32) {
4282       Error(ExLoc, "'asr #32' shift amount not allowed in Thumb mode");
4283       return MatchOperand_ParseFail;
4284     }
4285     if (Val == 32) Val = 0;
4286   } else {
4287     // Shift amount must be in [1,32]
4288     if (Val < 0 || Val > 31) {
4289       Error(ExLoc, "'lsr' shift amount must be in range [0,31]");
4290       return MatchOperand_ParseFail;
4291     }
4292   }
4293 
4294   Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, EndLoc));
4295 
4296   return MatchOperand_Success;
4297 }
4298 
4299 /// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
4300 /// of instructions. Legal values are:
4301 ///     ror #n  'n' in {0, 8, 16, 24}
4302 OperandMatchResultTy
4303 ARMAsmParser::parseRotImm(OperandVector &Operands) {
4304   MCAsmParser &Parser = getParser();
4305   const AsmToken &Tok = Parser.getTok();
4306   SMLoc S = Tok.getLoc();
4307   if (Tok.isNot(AsmToken::Identifier))
4308     return MatchOperand_NoMatch;
4309   StringRef ShiftName = Tok.getString();
4310   if (ShiftName != "ror" && ShiftName != "ROR")
4311     return MatchOperand_NoMatch;
4312   Parser.Lex(); // Eat the operator.
4313 
4314   // A '#' and a rotate amount.
4315   if (Parser.getTok().isNot(AsmToken::Hash) &&
4316       Parser.getTok().isNot(AsmToken::Dollar)) {
4317     Error(Parser.getTok().getLoc(), "'#' expected");
4318     return MatchOperand_ParseFail;
4319   }
4320   Parser.Lex(); // Eat hash token.
4321   SMLoc ExLoc = Parser.getTok().getLoc();
4322 
4323   const MCExpr *ShiftAmount;
4324   SMLoc EndLoc;
4325   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
4326     Error(ExLoc, "malformed rotate expression");
4327     return MatchOperand_ParseFail;
4328   }
4329   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
4330   if (!CE) {
4331     Error(ExLoc, "rotate amount must be an immediate");
4332     return MatchOperand_ParseFail;
4333   }
4334 
4335   int64_t Val = CE->getValue();
4336   // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
4337   // normally, zero is represented in asm by omitting the rotate operand
4338   // entirely.
4339   if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
4340     Error(ExLoc, "'ror' rotate amount must be 8, 16, or 24");
4341     return MatchOperand_ParseFail;
4342   }
4343 
4344   Operands.push_back(ARMOperand::CreateRotImm(Val, S, EndLoc));
4345 
4346   return MatchOperand_Success;
4347 }
4348 
4349 OperandMatchResultTy
4350 ARMAsmParser::parseModImm(OperandVector &Operands) {
4351   MCAsmParser &Parser = getParser();
4352   MCAsmLexer &Lexer = getLexer();
4353   int64_t Imm1, Imm2;
4354 
4355   SMLoc S = Parser.getTok().getLoc();
4356 
4357   // 1) A mod_imm operand can appear in the place of a register name:
4358   //   add r0, #mod_imm
4359   //   add r0, r0, #mod_imm
4360   // to correctly handle the latter, we bail out as soon as we see an
4361   // identifier.
4362   //
4363   // 2) Similarly, we do not want to parse into complex operands:
4364   //   mov r0, #mod_imm
4365   //   mov r0, :lower16:(_foo)
4366   if (Parser.getTok().is(AsmToken::Identifier) ||
4367       Parser.getTok().is(AsmToken::Colon))
4368     return MatchOperand_NoMatch;
4369 
4370   // Hash (dollar) is optional as per the ARMARM
4371   if (Parser.getTok().is(AsmToken::Hash) ||
4372       Parser.getTok().is(AsmToken::Dollar)) {
4373     // Avoid parsing into complex operands (#:)
4374     if (Lexer.peekTok().is(AsmToken::Colon))
4375       return MatchOperand_NoMatch;
4376 
4377     // Eat the hash (dollar)
4378     Parser.Lex();
4379   }
4380 
4381   SMLoc Sx1, Ex1;
4382   Sx1 = Parser.getTok().getLoc();
4383   const MCExpr *Imm1Exp;
4384   if (getParser().parseExpression(Imm1Exp, Ex1)) {
4385     Error(Sx1, "malformed expression");
4386     return MatchOperand_ParseFail;
4387   }
4388 
4389   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm1Exp);
4390 
4391   if (CE) {
4392     // Immediate must fit within 32-bits
4393     Imm1 = CE->getValue();
4394     int Enc = ARM_AM::getSOImmVal(Imm1);
4395     if (Enc != -1 && Parser.getTok().is(AsmToken::EndOfStatement)) {
4396       // We have a match!
4397       Operands.push_back(ARMOperand::CreateModImm((Enc & 0xFF),
4398                                                   (Enc & 0xF00) >> 7,
4399                                                   Sx1, Ex1));
4400       return MatchOperand_Success;
4401     }
4402 
4403     // We have parsed an immediate which is not for us, fallback to a plain
4404     // immediate. This can happen for instruction aliases. For an example,
4405     // ARMInstrInfo.td defines the alias [mov <-> mvn] which can transform
4406     // a mov (mvn) with a mod_imm_neg/mod_imm_not operand into the opposite
4407     // instruction with a mod_imm operand. The alias is defined such that the
4408     // parser method is shared, that's why we have to do this here.
4409     if (Parser.getTok().is(AsmToken::EndOfStatement)) {
4410       Operands.push_back(ARMOperand::CreateImm(Imm1Exp, Sx1, Ex1));
4411       return MatchOperand_Success;
4412     }
4413   } else {
4414     // Operands like #(l1 - l2) can only be evaluated at a later stage (via an
4415     // MCFixup). Fallback to a plain immediate.
4416     Operands.push_back(ARMOperand::CreateImm(Imm1Exp, Sx1, Ex1));
4417     return MatchOperand_Success;
4418   }
4419 
4420   // From this point onward, we expect the input to be a (#bits, #rot) pair
4421   if (Parser.getTok().isNot(AsmToken::Comma)) {
4422     Error(Sx1, "expected modified immediate operand: #[0, 255], #even[0-30]");
4423     return MatchOperand_ParseFail;
4424   }
4425 
4426   if (Imm1 & ~0xFF) {
4427     Error(Sx1, "immediate operand must a number in the range [0, 255]");
4428     return MatchOperand_ParseFail;
4429   }
4430 
4431   // Eat the comma
4432   Parser.Lex();
4433 
4434   // Repeat for #rot
4435   SMLoc Sx2, Ex2;
4436   Sx2 = Parser.getTok().getLoc();
4437 
4438   // Eat the optional hash (dollar)
4439   if (Parser.getTok().is(AsmToken::Hash) ||
4440       Parser.getTok().is(AsmToken::Dollar))
4441     Parser.Lex();
4442 
4443   const MCExpr *Imm2Exp;
4444   if (getParser().parseExpression(Imm2Exp, Ex2)) {
4445     Error(Sx2, "malformed expression");
4446     return MatchOperand_ParseFail;
4447   }
4448 
4449   CE = dyn_cast<MCConstantExpr>(Imm2Exp);
4450 
4451   if (CE) {
4452     Imm2 = CE->getValue();
4453     if (!(Imm2 & ~0x1E)) {
4454       // We have a match!
4455       Operands.push_back(ARMOperand::CreateModImm(Imm1, Imm2, S, Ex2));
4456       return MatchOperand_Success;
4457     }
4458     Error(Sx2, "immediate operand must an even number in the range [0, 30]");
4459     return MatchOperand_ParseFail;
4460   } else {
4461     Error(Sx2, "constant expression expected");
4462     return MatchOperand_ParseFail;
4463   }
4464 }
4465 
4466 OperandMatchResultTy
4467 ARMAsmParser::parseBitfield(OperandVector &Operands) {
4468   MCAsmParser &Parser = getParser();
4469   SMLoc S = Parser.getTok().getLoc();
4470   // The bitfield descriptor is really two operands, the LSB and the width.
4471   if (Parser.getTok().isNot(AsmToken::Hash) &&
4472       Parser.getTok().isNot(AsmToken::Dollar)) {
4473     Error(Parser.getTok().getLoc(), "'#' expected");
4474     return MatchOperand_ParseFail;
4475   }
4476   Parser.Lex(); // Eat hash token.
4477 
4478   const MCExpr *LSBExpr;
4479   SMLoc E = Parser.getTok().getLoc();
4480   if (getParser().parseExpression(LSBExpr)) {
4481     Error(E, "malformed immediate expression");
4482     return MatchOperand_ParseFail;
4483   }
4484   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
4485   if (!CE) {
4486     Error(E, "'lsb' operand must be an immediate");
4487     return MatchOperand_ParseFail;
4488   }
4489 
4490   int64_t LSB = CE->getValue();
4491   // The LSB must be in the range [0,31]
4492   if (LSB < 0 || LSB > 31) {
4493     Error(E, "'lsb' operand must be in the range [0,31]");
4494     return MatchOperand_ParseFail;
4495   }
4496   E = Parser.getTok().getLoc();
4497 
4498   // Expect another immediate operand.
4499   if (Parser.getTok().isNot(AsmToken::Comma)) {
4500     Error(Parser.getTok().getLoc(), "too few operands");
4501     return MatchOperand_ParseFail;
4502   }
4503   Parser.Lex(); // Eat hash token.
4504   if (Parser.getTok().isNot(AsmToken::Hash) &&
4505       Parser.getTok().isNot(AsmToken::Dollar)) {
4506     Error(Parser.getTok().getLoc(), "'#' expected");
4507     return MatchOperand_ParseFail;
4508   }
4509   Parser.Lex(); // Eat hash token.
4510 
4511   const MCExpr *WidthExpr;
4512   SMLoc EndLoc;
4513   if (getParser().parseExpression(WidthExpr, EndLoc)) {
4514     Error(E, "malformed immediate expression");
4515     return MatchOperand_ParseFail;
4516   }
4517   CE = dyn_cast<MCConstantExpr>(WidthExpr);
4518   if (!CE) {
4519     Error(E, "'width' operand must be an immediate");
4520     return MatchOperand_ParseFail;
4521   }
4522 
4523   int64_t Width = CE->getValue();
4524   // The LSB must be in the range [1,32-lsb]
4525   if (Width < 1 || Width > 32 - LSB) {
4526     Error(E, "'width' operand must be in the range [1,32-lsb]");
4527     return MatchOperand_ParseFail;
4528   }
4529 
4530   Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, EndLoc));
4531 
4532   return MatchOperand_Success;
4533 }
4534 
4535 OperandMatchResultTy
4536 ARMAsmParser::parsePostIdxReg(OperandVector &Operands) {
4537   // Check for a post-index addressing register operand. Specifically:
4538   // postidx_reg := '+' register {, shift}
4539   //              | '-' register {, shift}
4540   //              | register {, shift}
4541 
4542   // This method must return MatchOperand_NoMatch without consuming any tokens
4543   // in the case where there is no match, as other alternatives take other
4544   // parse methods.
4545   MCAsmParser &Parser = getParser();
4546   AsmToken Tok = Parser.getTok();
4547   SMLoc S = Tok.getLoc();
4548   bool haveEaten = false;
4549   bool isAdd = true;
4550   if (Tok.is(AsmToken::Plus)) {
4551     Parser.Lex(); // Eat the '+' token.
4552     haveEaten = true;
4553   } else if (Tok.is(AsmToken::Minus)) {
4554     Parser.Lex(); // Eat the '-' token.
4555     isAdd = false;
4556     haveEaten = true;
4557   }
4558 
4559   SMLoc E = Parser.getTok().getEndLoc();
4560   int Reg = tryParseRegister();
4561   if (Reg == -1) {
4562     if (!haveEaten)
4563       return MatchOperand_NoMatch;
4564     Error(Parser.getTok().getLoc(), "register expected");
4565     return MatchOperand_ParseFail;
4566   }
4567 
4568   ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
4569   unsigned ShiftImm = 0;
4570   if (Parser.getTok().is(AsmToken::Comma)) {
4571     Parser.Lex(); // Eat the ','.
4572     if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
4573       return MatchOperand_ParseFail;
4574 
4575     // FIXME: Only approximates end...may include intervening whitespace.
4576     E = Parser.getTok().getLoc();
4577   }
4578 
4579   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
4580                                                   ShiftImm, S, E));
4581 
4582   return MatchOperand_Success;
4583 }
4584 
4585 OperandMatchResultTy
4586 ARMAsmParser::parseAM3Offset(OperandVector &Operands) {
4587   // Check for a post-index addressing register operand. Specifically:
4588   // am3offset := '+' register
4589   //              | '-' register
4590   //              | register
4591   //              | # imm
4592   //              | # + imm
4593   //              | # - imm
4594 
4595   // This method must return MatchOperand_NoMatch without consuming any tokens
4596   // in the case where there is no match, as other alternatives take other
4597   // parse methods.
4598   MCAsmParser &Parser = getParser();
4599   AsmToken Tok = Parser.getTok();
4600   SMLoc S = Tok.getLoc();
4601 
4602   // Do immediates first, as we always parse those if we have a '#'.
4603   if (Parser.getTok().is(AsmToken::Hash) ||
4604       Parser.getTok().is(AsmToken::Dollar)) {
4605     Parser.Lex(); // Eat '#' or '$'.
4606     // Explicitly look for a '-', as we need to encode negative zero
4607     // differently.
4608     bool isNegative = Parser.getTok().is(AsmToken::Minus);
4609     const MCExpr *Offset;
4610     SMLoc E;
4611     if (getParser().parseExpression(Offset, E))
4612       return MatchOperand_ParseFail;
4613     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
4614     if (!CE) {
4615       Error(S, "constant expression expected");
4616       return MatchOperand_ParseFail;
4617     }
4618     // Negative zero is encoded as the flag value INT32_MIN.
4619     int32_t Val = CE->getValue();
4620     if (isNegative && Val == 0)
4621       Val = INT32_MIN;
4622 
4623     Operands.push_back(
4624       ARMOperand::CreateImm(MCConstantExpr::create(Val, getContext()), S, E));
4625 
4626     return MatchOperand_Success;
4627   }
4628 
4629 
4630   bool haveEaten = false;
4631   bool isAdd = true;
4632   if (Tok.is(AsmToken::Plus)) {
4633     Parser.Lex(); // Eat the '+' token.
4634     haveEaten = true;
4635   } else if (Tok.is(AsmToken::Minus)) {
4636     Parser.Lex(); // Eat the '-' token.
4637     isAdd = false;
4638     haveEaten = true;
4639   }
4640 
4641   Tok = Parser.getTok();
4642   int Reg = tryParseRegister();
4643   if (Reg == -1) {
4644     if (!haveEaten)
4645       return MatchOperand_NoMatch;
4646     Error(Tok.getLoc(), "register expected");
4647     return MatchOperand_ParseFail;
4648   }
4649 
4650   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
4651                                                   0, S, Tok.getEndLoc()));
4652 
4653   return MatchOperand_Success;
4654 }
4655 
4656 /// Convert parsed operands to MCInst.  Needed here because this instruction
4657 /// only has two register operands, but multiplication is commutative so
4658 /// assemblers should accept both "mul rD, rN, rD" and "mul rD, rD, rN".
4659 void ARMAsmParser::cvtThumbMultiply(MCInst &Inst,
4660                                     const OperandVector &Operands) {
4661   ((ARMOperand &)*Operands[3]).addRegOperands(Inst, 1);
4662   ((ARMOperand &)*Operands[1]).addCCOutOperands(Inst, 1);
4663   // If we have a three-operand form, make sure to set Rn to be the operand
4664   // that isn't the same as Rd.
4665   unsigned RegOp = 4;
4666   if (Operands.size() == 6 &&
4667       ((ARMOperand &)*Operands[4]).getReg() ==
4668           ((ARMOperand &)*Operands[3]).getReg())
4669     RegOp = 5;
4670   ((ARMOperand &)*Operands[RegOp]).addRegOperands(Inst, 1);
4671   Inst.addOperand(Inst.getOperand(0));
4672   ((ARMOperand &)*Operands[2]).addCondCodeOperands(Inst, 2);
4673 }
4674 
4675 void ARMAsmParser::cvtThumbBranches(MCInst &Inst,
4676                                     const OperandVector &Operands) {
4677   int CondOp = -1, ImmOp = -1;
4678   switch(Inst.getOpcode()) {
4679     case ARM::tB:
4680     case ARM::tBcc:  CondOp = 1; ImmOp = 2; break;
4681 
4682     case ARM::t2B:
4683     case ARM::t2Bcc: CondOp = 1; ImmOp = 3; break;
4684 
4685     default: llvm_unreachable("Unexpected instruction in cvtThumbBranches");
4686   }
4687   // first decide whether or not the branch should be conditional
4688   // by looking at it's location relative to an IT block
4689   if(inITBlock()) {
4690     // inside an IT block we cannot have any conditional branches. any
4691     // such instructions needs to be converted to unconditional form
4692     switch(Inst.getOpcode()) {
4693       case ARM::tBcc: Inst.setOpcode(ARM::tB); break;
4694       case ARM::t2Bcc: Inst.setOpcode(ARM::t2B); break;
4695     }
4696   } else {
4697     // outside IT blocks we can only have unconditional branches with AL
4698     // condition code or conditional branches with non-AL condition code
4699     unsigned Cond = static_cast<ARMOperand &>(*Operands[CondOp]).getCondCode();
4700     switch(Inst.getOpcode()) {
4701       case ARM::tB:
4702       case ARM::tBcc:
4703         Inst.setOpcode(Cond == ARMCC::AL ? ARM::tB : ARM::tBcc);
4704         break;
4705       case ARM::t2B:
4706       case ARM::t2Bcc:
4707         Inst.setOpcode(Cond == ARMCC::AL ? ARM::t2B : ARM::t2Bcc);
4708         break;
4709     }
4710   }
4711 
4712   // now decide on encoding size based on branch target range
4713   switch(Inst.getOpcode()) {
4714     // classify tB as either t2B or t1B based on range of immediate operand
4715     case ARM::tB: {
4716       ARMOperand &op = static_cast<ARMOperand &>(*Operands[ImmOp]);
4717       if (!op.isSignedOffset<11, 1>() && isThumb() && hasV8MBaseline())
4718         Inst.setOpcode(ARM::t2B);
4719       break;
4720     }
4721     // classify tBcc as either t2Bcc or t1Bcc based on range of immediate operand
4722     case ARM::tBcc: {
4723       ARMOperand &op = static_cast<ARMOperand &>(*Operands[ImmOp]);
4724       if (!op.isSignedOffset<8, 1>() && isThumb() && hasV8MBaseline())
4725         Inst.setOpcode(ARM::t2Bcc);
4726       break;
4727     }
4728   }
4729   ((ARMOperand &)*Operands[ImmOp]).addImmOperands(Inst, 1);
4730   ((ARMOperand &)*Operands[CondOp]).addCondCodeOperands(Inst, 2);
4731 }
4732 
4733 /// Parse an ARM memory expression, return false if successful else return true
4734 /// or an error.  The first token must be a '[' when called.
4735 bool ARMAsmParser::parseMemory(OperandVector &Operands) {
4736   MCAsmParser &Parser = getParser();
4737   SMLoc S, E;
4738   if (Parser.getTok().isNot(AsmToken::LBrac))
4739     return TokError("Token is not a Left Bracket");
4740   S = Parser.getTok().getLoc();
4741   Parser.Lex(); // Eat left bracket token.
4742 
4743   const AsmToken &BaseRegTok = Parser.getTok();
4744   int BaseRegNum = tryParseRegister();
4745   if (BaseRegNum == -1)
4746     return Error(BaseRegTok.getLoc(), "register expected");
4747 
4748   // The next token must either be a comma, a colon or a closing bracket.
4749   const AsmToken &Tok = Parser.getTok();
4750   if (!Tok.is(AsmToken::Colon) && !Tok.is(AsmToken::Comma) &&
4751       !Tok.is(AsmToken::RBrac))
4752     return Error(Tok.getLoc(), "malformed memory operand");
4753 
4754   if (Tok.is(AsmToken::RBrac)) {
4755     E = Tok.getEndLoc();
4756     Parser.Lex(); // Eat right bracket token.
4757 
4758     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, 0,
4759                                              ARM_AM::no_shift, 0, 0, false,
4760                                              S, E));
4761 
4762     // If there's a pre-indexing writeback marker, '!', just add it as a token
4763     // operand. It's rather odd, but syntactically valid.
4764     if (Parser.getTok().is(AsmToken::Exclaim)) {
4765       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4766       Parser.Lex(); // Eat the '!'.
4767     }
4768 
4769     return false;
4770   }
4771 
4772   assert((Tok.is(AsmToken::Colon) || Tok.is(AsmToken::Comma)) &&
4773          "Lost colon or comma in memory operand?!");
4774   if (Tok.is(AsmToken::Comma)) {
4775     Parser.Lex(); // Eat the comma.
4776   }
4777 
4778   // If we have a ':', it's an alignment specifier.
4779   if (Parser.getTok().is(AsmToken::Colon)) {
4780     Parser.Lex(); // Eat the ':'.
4781     E = Parser.getTok().getLoc();
4782     SMLoc AlignmentLoc = Tok.getLoc();
4783 
4784     const MCExpr *Expr;
4785     if (getParser().parseExpression(Expr))
4786      return true;
4787 
4788     // The expression has to be a constant. Memory references with relocations
4789     // don't come through here, as they use the <label> forms of the relevant
4790     // instructions.
4791     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
4792     if (!CE)
4793       return Error (E, "constant expression expected");
4794 
4795     unsigned Align = 0;
4796     switch (CE->getValue()) {
4797     default:
4798       return Error(E,
4799                    "alignment specifier must be 16, 32, 64, 128, or 256 bits");
4800     case 16:  Align = 2; break;
4801     case 32:  Align = 4; break;
4802     case 64:  Align = 8; break;
4803     case 128: Align = 16; break;
4804     case 256: Align = 32; break;
4805     }
4806 
4807     // Now we should have the closing ']'
4808     if (Parser.getTok().isNot(AsmToken::RBrac))
4809       return Error(Parser.getTok().getLoc(), "']' expected");
4810     E = Parser.getTok().getEndLoc();
4811     Parser.Lex(); // Eat right bracket token.
4812 
4813     // Don't worry about range checking the value here. That's handled by
4814     // the is*() predicates.
4815     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, 0,
4816                                              ARM_AM::no_shift, 0, Align,
4817                                              false, S, E, AlignmentLoc));
4818 
4819     // If there's a pre-indexing writeback marker, '!', just add it as a token
4820     // operand.
4821     if (Parser.getTok().is(AsmToken::Exclaim)) {
4822       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4823       Parser.Lex(); // Eat the '!'.
4824     }
4825 
4826     return false;
4827   }
4828 
4829   // If we have a '#', it's an immediate offset, else assume it's a register
4830   // offset. Be friendly and also accept a plain integer (without a leading
4831   // hash) for gas compatibility.
4832   if (Parser.getTok().is(AsmToken::Hash) ||
4833       Parser.getTok().is(AsmToken::Dollar) ||
4834       Parser.getTok().is(AsmToken::Integer)) {
4835     if (Parser.getTok().isNot(AsmToken::Integer))
4836       Parser.Lex(); // Eat '#' or '$'.
4837     E = Parser.getTok().getLoc();
4838 
4839     bool isNegative = getParser().getTok().is(AsmToken::Minus);
4840     const MCExpr *Offset;
4841     if (getParser().parseExpression(Offset))
4842      return true;
4843 
4844     // The expression has to be a constant. Memory references with relocations
4845     // don't come through here, as they use the <label> forms of the relevant
4846     // instructions.
4847     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
4848     if (!CE)
4849       return Error (E, "constant expression expected");
4850 
4851     // If the constant was #-0, represent it as INT32_MIN.
4852     int32_t Val = CE->getValue();
4853     if (isNegative && Val == 0)
4854       CE = MCConstantExpr::create(INT32_MIN, getContext());
4855 
4856     // Now we should have the closing ']'
4857     if (Parser.getTok().isNot(AsmToken::RBrac))
4858       return Error(Parser.getTok().getLoc(), "']' expected");
4859     E = Parser.getTok().getEndLoc();
4860     Parser.Lex(); // Eat right bracket token.
4861 
4862     // Don't worry about range checking the value here. That's handled by
4863     // the is*() predicates.
4864     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
4865                                              ARM_AM::no_shift, 0, 0,
4866                                              false, S, E));
4867 
4868     // If there's a pre-indexing writeback marker, '!', just add it as a token
4869     // operand.
4870     if (Parser.getTok().is(AsmToken::Exclaim)) {
4871       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4872       Parser.Lex(); // Eat the '!'.
4873     }
4874 
4875     return false;
4876   }
4877 
4878   // The register offset is optionally preceded by a '+' or '-'
4879   bool isNegative = false;
4880   if (Parser.getTok().is(AsmToken::Minus)) {
4881     isNegative = true;
4882     Parser.Lex(); // Eat the '-'.
4883   } else if (Parser.getTok().is(AsmToken::Plus)) {
4884     // Nothing to do.
4885     Parser.Lex(); // Eat the '+'.
4886   }
4887 
4888   E = Parser.getTok().getLoc();
4889   int OffsetRegNum = tryParseRegister();
4890   if (OffsetRegNum == -1)
4891     return Error(E, "register expected");
4892 
4893   // If there's a shift operator, handle it.
4894   ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
4895   unsigned ShiftImm = 0;
4896   if (Parser.getTok().is(AsmToken::Comma)) {
4897     Parser.Lex(); // Eat the ','.
4898     if (parseMemRegOffsetShift(ShiftType, ShiftImm))
4899       return true;
4900   }
4901 
4902   // Now we should have the closing ']'
4903   if (Parser.getTok().isNot(AsmToken::RBrac))
4904     return Error(Parser.getTok().getLoc(), "']' expected");
4905   E = Parser.getTok().getEndLoc();
4906   Parser.Lex(); // Eat right bracket token.
4907 
4908   Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, OffsetRegNum,
4909                                            ShiftType, ShiftImm, 0, isNegative,
4910                                            S, E));
4911 
4912   // If there's a pre-indexing writeback marker, '!', just add it as a token
4913   // operand.
4914   if (Parser.getTok().is(AsmToken::Exclaim)) {
4915     Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4916     Parser.Lex(); // Eat the '!'.
4917   }
4918 
4919   return false;
4920 }
4921 
4922 /// parseMemRegOffsetShift - one of these two:
4923 ///   ( lsl | lsr | asr | ror ) , # shift_amount
4924 ///   rrx
4925 /// return true if it parses a shift otherwise it returns false.
4926 bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
4927                                           unsigned &Amount) {
4928   MCAsmParser &Parser = getParser();
4929   SMLoc Loc = Parser.getTok().getLoc();
4930   const AsmToken &Tok = Parser.getTok();
4931   if (Tok.isNot(AsmToken::Identifier))
4932     return true;
4933   StringRef ShiftName = Tok.getString();
4934   if (ShiftName == "lsl" || ShiftName == "LSL" ||
4935       ShiftName == "asl" || ShiftName == "ASL")
4936     St = ARM_AM::lsl;
4937   else if (ShiftName == "lsr" || ShiftName == "LSR")
4938     St = ARM_AM::lsr;
4939   else if (ShiftName == "asr" || ShiftName == "ASR")
4940     St = ARM_AM::asr;
4941   else if (ShiftName == "ror" || ShiftName == "ROR")
4942     St = ARM_AM::ror;
4943   else if (ShiftName == "rrx" || ShiftName == "RRX")
4944     St = ARM_AM::rrx;
4945   else
4946     return Error(Loc, "illegal shift operator");
4947   Parser.Lex(); // Eat shift type token.
4948 
4949   // rrx stands alone.
4950   Amount = 0;
4951   if (St != ARM_AM::rrx) {
4952     Loc = Parser.getTok().getLoc();
4953     // A '#' and a shift amount.
4954     const AsmToken &HashTok = Parser.getTok();
4955     if (HashTok.isNot(AsmToken::Hash) &&
4956         HashTok.isNot(AsmToken::Dollar))
4957       return Error(HashTok.getLoc(), "'#' expected");
4958     Parser.Lex(); // Eat hash token.
4959 
4960     const MCExpr *Expr;
4961     if (getParser().parseExpression(Expr))
4962       return true;
4963     // Range check the immediate.
4964     // lsl, ror: 0 <= imm <= 31
4965     // lsr, asr: 0 <= imm <= 32
4966     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
4967     if (!CE)
4968       return Error(Loc, "shift amount must be an immediate");
4969     int64_t Imm = CE->getValue();
4970     if (Imm < 0 ||
4971         ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
4972         ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
4973       return Error(Loc, "immediate shift value out of range");
4974     // If <ShiftTy> #0, turn it into a no_shift.
4975     if (Imm == 0)
4976       St = ARM_AM::lsl;
4977     // For consistency, treat lsr #32 and asr #32 as having immediate value 0.
4978     if (Imm == 32)
4979       Imm = 0;
4980     Amount = Imm;
4981   }
4982 
4983   return false;
4984 }
4985 
4986 /// parseFPImm - A floating point immediate expression operand.
4987 OperandMatchResultTy
4988 ARMAsmParser::parseFPImm(OperandVector &Operands) {
4989   MCAsmParser &Parser = getParser();
4990   // Anything that can accept a floating point constant as an operand
4991   // needs to go through here, as the regular parseExpression is
4992   // integer only.
4993   //
4994   // This routine still creates a generic Immediate operand, containing
4995   // a bitcast of the 64-bit floating point value. The various operands
4996   // that accept floats can check whether the value is valid for them
4997   // via the standard is*() predicates.
4998 
4999   SMLoc S = Parser.getTok().getLoc();
5000 
5001   if (Parser.getTok().isNot(AsmToken::Hash) &&
5002       Parser.getTok().isNot(AsmToken::Dollar))
5003     return MatchOperand_NoMatch;
5004 
5005   // Disambiguate the VMOV forms that can accept an FP immediate.
5006   // vmov.f32 <sreg>, #imm
5007   // vmov.f64 <dreg>, #imm
5008   // vmov.f32 <dreg>, #imm  @ vector f32x2
5009   // vmov.f32 <qreg>, #imm  @ vector f32x4
5010   //
5011   // There are also the NEON VMOV instructions which expect an
5012   // integer constant. Make sure we don't try to parse an FPImm
5013   // for these:
5014   // vmov.i{8|16|32|64} <dreg|qreg>, #imm
5015   ARMOperand &TyOp = static_cast<ARMOperand &>(*Operands[2]);
5016   bool isVmovf = TyOp.isToken() &&
5017                  (TyOp.getToken() == ".f32" || TyOp.getToken() == ".f64" ||
5018                   TyOp.getToken() == ".f16");
5019   ARMOperand &Mnemonic = static_cast<ARMOperand &>(*Operands[0]);
5020   bool isFconst = Mnemonic.isToken() && (Mnemonic.getToken() == "fconstd" ||
5021                                          Mnemonic.getToken() == "fconsts");
5022   if (!(isVmovf || isFconst))
5023     return MatchOperand_NoMatch;
5024 
5025   Parser.Lex(); // Eat '#' or '$'.
5026 
5027   // Handle negation, as that still comes through as a separate token.
5028   bool isNegative = false;
5029   if (Parser.getTok().is(AsmToken::Minus)) {
5030     isNegative = true;
5031     Parser.Lex();
5032   }
5033   const AsmToken &Tok = Parser.getTok();
5034   SMLoc Loc = Tok.getLoc();
5035   if (Tok.is(AsmToken::Real) && isVmovf) {
5036     APFloat RealVal(APFloat::IEEEsingle(), Tok.getString());
5037     uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
5038     // If we had a '-' in front, toggle the sign bit.
5039     IntVal ^= (uint64_t)isNegative << 31;
5040     Parser.Lex(); // Eat the token.
5041     Operands.push_back(ARMOperand::CreateImm(
5042           MCConstantExpr::create(IntVal, getContext()),
5043           S, Parser.getTok().getLoc()));
5044     return MatchOperand_Success;
5045   }
5046   // Also handle plain integers. Instructions which allow floating point
5047   // immediates also allow a raw encoded 8-bit value.
5048   if (Tok.is(AsmToken::Integer) && isFconst) {
5049     int64_t Val = Tok.getIntVal();
5050     Parser.Lex(); // Eat the token.
5051     if (Val > 255 || Val < 0) {
5052       Error(Loc, "encoded floating point value out of range");
5053       return MatchOperand_ParseFail;
5054     }
5055     float RealVal = ARM_AM::getFPImmFloat(Val);
5056     Val = APFloat(RealVal).bitcastToAPInt().getZExtValue();
5057 
5058     Operands.push_back(ARMOperand::CreateImm(
5059         MCConstantExpr::create(Val, getContext()), S,
5060         Parser.getTok().getLoc()));
5061     return MatchOperand_Success;
5062   }
5063 
5064   Error(Loc, "invalid floating point immediate");
5065   return MatchOperand_ParseFail;
5066 }
5067 
5068 /// Parse a arm instruction operand.  For now this parses the operand regardless
5069 /// of the mnemonic.
5070 bool ARMAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
5071   MCAsmParser &Parser = getParser();
5072   SMLoc S, E;
5073 
5074   // Check if the current operand has a custom associated parser, if so, try to
5075   // custom parse the operand, or fallback to the general approach.
5076   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
5077   if (ResTy == MatchOperand_Success)
5078     return false;
5079   // If there wasn't a custom match, try the generic matcher below. Otherwise,
5080   // there was a match, but an error occurred, in which case, just return that
5081   // the operand parsing failed.
5082   if (ResTy == MatchOperand_ParseFail)
5083     return true;
5084 
5085   switch (getLexer().getKind()) {
5086   default:
5087     Error(Parser.getTok().getLoc(), "unexpected token in operand");
5088     return true;
5089   case AsmToken::Identifier: {
5090     // If we've seen a branch mnemonic, the next operand must be a label.  This
5091     // is true even if the label is a register name.  So "br r1" means branch to
5092     // label "r1".
5093     bool ExpectLabel = Mnemonic == "b" || Mnemonic == "bl";
5094     if (!ExpectLabel) {
5095       if (!tryParseRegisterWithWriteBack(Operands))
5096         return false;
5097       int Res = tryParseShiftRegister(Operands);
5098       if (Res == 0) // success
5099         return false;
5100       else if (Res == -1) // irrecoverable error
5101         return true;
5102       // If this is VMRS, check for the apsr_nzcv operand.
5103       if (Mnemonic == "vmrs" &&
5104           Parser.getTok().getString().equals_lower("apsr_nzcv")) {
5105         S = Parser.getTok().getLoc();
5106         Parser.Lex();
5107         Operands.push_back(ARMOperand::CreateToken("APSR_nzcv", S));
5108         return false;
5109       }
5110     }
5111 
5112     // Fall though for the Identifier case that is not a register or a
5113     // special name.
5114     LLVM_FALLTHROUGH;
5115   }
5116   case AsmToken::LParen:  // parenthesized expressions like (_strcmp-4)
5117   case AsmToken::Integer: // things like 1f and 2b as a branch targets
5118   case AsmToken::String:  // quoted label names.
5119   case AsmToken::Dot: {   // . as a branch target
5120     // This was not a register so parse other operands that start with an
5121     // identifier (like labels) as expressions and create them as immediates.
5122     const MCExpr *IdVal;
5123     S = Parser.getTok().getLoc();
5124     if (getParser().parseExpression(IdVal))
5125       return true;
5126     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5127     Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
5128     return false;
5129   }
5130   case AsmToken::LBrac:
5131     return parseMemory(Operands);
5132   case AsmToken::LCurly:
5133     return parseRegisterList(Operands);
5134   case AsmToken::Dollar:
5135   case AsmToken::Hash: {
5136     // #42 -> immediate.
5137     S = Parser.getTok().getLoc();
5138     Parser.Lex();
5139 
5140     if (Parser.getTok().isNot(AsmToken::Colon)) {
5141       bool isNegative = Parser.getTok().is(AsmToken::Minus);
5142       const MCExpr *ImmVal;
5143       if (getParser().parseExpression(ImmVal))
5144         return true;
5145       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
5146       if (CE) {
5147         int32_t Val = CE->getValue();
5148         if (isNegative && Val == 0)
5149           ImmVal = MCConstantExpr::create(INT32_MIN, getContext());
5150       }
5151       E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5152       Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
5153 
5154       // There can be a trailing '!' on operands that we want as a separate
5155       // '!' Token operand. Handle that here. For example, the compatibility
5156       // alias for 'srsdb sp!, #imm' is 'srsdb #imm!'.
5157       if (Parser.getTok().is(AsmToken::Exclaim)) {
5158         Operands.push_back(ARMOperand::CreateToken(Parser.getTok().getString(),
5159                                                    Parser.getTok().getLoc()));
5160         Parser.Lex(); // Eat exclaim token
5161       }
5162       return false;
5163     }
5164     // w/ a ':' after the '#', it's just like a plain ':'.
5165     LLVM_FALLTHROUGH;
5166   }
5167   case AsmToken::Colon: {
5168     S = Parser.getTok().getLoc();
5169     // ":lower16:" and ":upper16:" expression prefixes
5170     // FIXME: Check it's an expression prefix,
5171     // e.g. (FOO - :lower16:BAR) isn't legal.
5172     ARMMCExpr::VariantKind RefKind;
5173     if (parsePrefix(RefKind))
5174       return true;
5175 
5176     const MCExpr *SubExprVal;
5177     if (getParser().parseExpression(SubExprVal))
5178       return true;
5179 
5180     const MCExpr *ExprVal = ARMMCExpr::create(RefKind, SubExprVal,
5181                                               getContext());
5182     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5183     Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
5184     return false;
5185   }
5186   case AsmToken::Equal: {
5187     S = Parser.getTok().getLoc();
5188     if (Mnemonic != "ldr") // only parse for ldr pseudo (e.g. ldr r0, =val)
5189       return Error(S, "unexpected token in operand");
5190     Parser.Lex(); // Eat '='
5191     const MCExpr *SubExprVal;
5192     if (getParser().parseExpression(SubExprVal))
5193       return true;
5194     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5195 
5196     // execute-only: we assume that assembly programmers know what they are
5197     // doing and allow literal pool creation here
5198     Operands.push_back(ARMOperand::CreateConstantPoolImm(SubExprVal, S, E));
5199     return false;
5200   }
5201   }
5202 }
5203 
5204 // parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
5205 //  :lower16: and :upper16:.
5206 bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
5207   MCAsmParser &Parser = getParser();
5208   RefKind = ARMMCExpr::VK_ARM_None;
5209 
5210   // consume an optional '#' (GNU compatibility)
5211   if (getLexer().is(AsmToken::Hash))
5212     Parser.Lex();
5213 
5214   // :lower16: and :upper16: modifiers
5215   assert(getLexer().is(AsmToken::Colon) && "expected a :");
5216   Parser.Lex(); // Eat ':'
5217 
5218   if (getLexer().isNot(AsmToken::Identifier)) {
5219     Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
5220     return true;
5221   }
5222 
5223   enum {
5224     COFF = (1 << MCObjectFileInfo::IsCOFF),
5225     ELF = (1 << MCObjectFileInfo::IsELF),
5226     MACHO = (1 << MCObjectFileInfo::IsMachO),
5227     WASM = (1 << MCObjectFileInfo::IsWasm),
5228   };
5229   static const struct PrefixEntry {
5230     const char *Spelling;
5231     ARMMCExpr::VariantKind VariantKind;
5232     uint8_t SupportedFormats;
5233   } PrefixEntries[] = {
5234     { "lower16", ARMMCExpr::VK_ARM_LO16, COFF | ELF | MACHO },
5235     { "upper16", ARMMCExpr::VK_ARM_HI16, COFF | ELF | MACHO },
5236   };
5237 
5238   StringRef IDVal = Parser.getTok().getIdentifier();
5239 
5240   const auto &Prefix =
5241       std::find_if(std::begin(PrefixEntries), std::end(PrefixEntries),
5242                    [&IDVal](const PrefixEntry &PE) {
5243                       return PE.Spelling == IDVal;
5244                    });
5245   if (Prefix == std::end(PrefixEntries)) {
5246     Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
5247     return true;
5248   }
5249 
5250   uint8_t CurrentFormat;
5251   switch (getContext().getObjectFileInfo()->getObjectFileType()) {
5252   case MCObjectFileInfo::IsMachO:
5253     CurrentFormat = MACHO;
5254     break;
5255   case MCObjectFileInfo::IsELF:
5256     CurrentFormat = ELF;
5257     break;
5258   case MCObjectFileInfo::IsCOFF:
5259     CurrentFormat = COFF;
5260     break;
5261   case MCObjectFileInfo::IsWasm:
5262     CurrentFormat = WASM;
5263     break;
5264   }
5265 
5266   if (~Prefix->SupportedFormats & CurrentFormat) {
5267     Error(Parser.getTok().getLoc(),
5268           "cannot represent relocation in the current file format");
5269     return true;
5270   }
5271 
5272   RefKind = Prefix->VariantKind;
5273   Parser.Lex();
5274 
5275   if (getLexer().isNot(AsmToken::Colon)) {
5276     Error(Parser.getTok().getLoc(), "unexpected token after prefix");
5277     return true;
5278   }
5279   Parser.Lex(); // Eat the last ':'
5280 
5281   return false;
5282 }
5283 
5284 /// \brief Given a mnemonic, split out possible predication code and carry
5285 /// setting letters to form a canonical mnemonic and flags.
5286 //
5287 // FIXME: Would be nice to autogen this.
5288 // FIXME: This is a bit of a maze of special cases.
5289 StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
5290                                       unsigned &PredicationCode,
5291                                       bool &CarrySetting,
5292                                       unsigned &ProcessorIMod,
5293                                       StringRef &ITMask) {
5294   PredicationCode = ARMCC::AL;
5295   CarrySetting = false;
5296   ProcessorIMod = 0;
5297 
5298   // Ignore some mnemonics we know aren't predicated forms.
5299   //
5300   // FIXME: Would be nice to autogen this.
5301   if ((Mnemonic == "movs" && isThumb()) ||
5302       Mnemonic == "teq"   || Mnemonic == "vceq"   || Mnemonic == "svc"   ||
5303       Mnemonic == "mls"   || Mnemonic == "smmls"  || Mnemonic == "vcls"  ||
5304       Mnemonic == "vmls"  || Mnemonic == "vnmls"  || Mnemonic == "vacge" ||
5305       Mnemonic == "vcge"  || Mnemonic == "vclt"   || Mnemonic == "vacgt" ||
5306       Mnemonic == "vaclt" || Mnemonic == "vacle"  || Mnemonic == "hlt" ||
5307       Mnemonic == "vcgt"  || Mnemonic == "vcle"   || Mnemonic == "smlal" ||
5308       Mnemonic == "umaal" || Mnemonic == "umlal"  || Mnemonic == "vabal" ||
5309       Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal" ||
5310       Mnemonic == "fmuls" || Mnemonic == "vmaxnm" || Mnemonic == "vminnm" ||
5311       Mnemonic == "vcvta" || Mnemonic == "vcvtn"  || Mnemonic == "vcvtp" ||
5312       Mnemonic == "vcvtm" || Mnemonic == "vrinta" || Mnemonic == "vrintn" ||
5313       Mnemonic == "vrintp" || Mnemonic == "vrintm" || Mnemonic == "hvc" ||
5314       Mnemonic.startswith("vsel") || Mnemonic == "vins" || Mnemonic == "vmovx" ||
5315       Mnemonic == "bxns"  || Mnemonic == "blxns" ||
5316       Mnemonic == "vudot" || Mnemonic == "vsdot")
5317     return Mnemonic;
5318 
5319   // First, split out any predication code. Ignore mnemonics we know aren't
5320   // predicated but do have a carry-set and so weren't caught above.
5321   if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
5322       Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
5323       Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
5324       Mnemonic != "sbcs" && Mnemonic != "rscs") {
5325     unsigned CC = ARMCondCodeFromString(Mnemonic.substr(Mnemonic.size()-2));
5326     if (CC != ~0U) {
5327       Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
5328       PredicationCode = CC;
5329     }
5330   }
5331 
5332   // Next, determine if we have a carry setting bit. We explicitly ignore all
5333   // the instructions we know end in 's'.
5334   if (Mnemonic.endswith("s") &&
5335       !(Mnemonic == "cps" || Mnemonic == "mls" ||
5336         Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
5337         Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
5338         Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
5339         Mnemonic == "vrsqrts" || Mnemonic == "srs" || Mnemonic == "flds" ||
5340         Mnemonic == "fmrs" || Mnemonic == "fsqrts" || Mnemonic == "fsubs" ||
5341         Mnemonic == "fsts" || Mnemonic == "fcpys" || Mnemonic == "fdivs" ||
5342         Mnemonic == "fmuls" || Mnemonic == "fcmps" || Mnemonic == "fcmpzs" ||
5343         Mnemonic == "vfms" || Mnemonic == "vfnms" || Mnemonic == "fconsts" ||
5344         Mnemonic == "bxns" || Mnemonic == "blxns" ||
5345         (Mnemonic == "movs" && isThumb()))) {
5346     Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
5347     CarrySetting = true;
5348   }
5349 
5350   // The "cps" instruction can have a interrupt mode operand which is glued into
5351   // the mnemonic. Check if this is the case, split it and parse the imod op
5352   if (Mnemonic.startswith("cps")) {
5353     // Split out any imod code.
5354     unsigned IMod =
5355       StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
5356       .Case("ie", ARM_PROC::IE)
5357       .Case("id", ARM_PROC::ID)
5358       .Default(~0U);
5359     if (IMod != ~0U) {
5360       Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
5361       ProcessorIMod = IMod;
5362     }
5363   }
5364 
5365   // The "it" instruction has the condition mask on the end of the mnemonic.
5366   if (Mnemonic.startswith("it")) {
5367     ITMask = Mnemonic.slice(2, Mnemonic.size());
5368     Mnemonic = Mnemonic.slice(0, 2);
5369   }
5370 
5371   return Mnemonic;
5372 }
5373 
5374 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
5375 /// inclusion of carry set or predication code operands.
5376 //
5377 // FIXME: It would be nice to autogen this.
5378 void ARMAsmParser::getMnemonicAcceptInfo(StringRef Mnemonic, StringRef FullInst,
5379                                          bool &CanAcceptCarrySet,
5380                                          bool &CanAcceptPredicationCode) {
5381   CanAcceptCarrySet =
5382       Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
5383       Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
5384       Mnemonic == "add" || Mnemonic == "adc" || Mnemonic == "mul" ||
5385       Mnemonic == "bic" || Mnemonic == "asr" || Mnemonic == "orr" ||
5386       Mnemonic == "mvn" || Mnemonic == "rsb" || Mnemonic == "rsc" ||
5387       Mnemonic == "orn" || Mnemonic == "sbc" || Mnemonic == "eor" ||
5388       Mnemonic == "neg" || Mnemonic == "vfm" || Mnemonic == "vfnm" ||
5389       (!isThumb() &&
5390        (Mnemonic == "smull" || Mnemonic == "mov" || Mnemonic == "mla" ||
5391         Mnemonic == "smlal" || Mnemonic == "umlal" || Mnemonic == "umull"));
5392 
5393   if (Mnemonic == "bkpt" || Mnemonic == "cbnz" || Mnemonic == "setend" ||
5394       Mnemonic == "cps" || Mnemonic == "it" || Mnemonic == "cbz" ||
5395       Mnemonic == "trap" || Mnemonic == "hlt" || Mnemonic == "udf" ||
5396       Mnemonic.startswith("crc32") || Mnemonic.startswith("cps") ||
5397       Mnemonic.startswith("vsel") || Mnemonic == "vmaxnm" ||
5398       Mnemonic == "vminnm" || Mnemonic == "vcvta" || Mnemonic == "vcvtn" ||
5399       Mnemonic == "vcvtp" || Mnemonic == "vcvtm" || Mnemonic == "vrinta" ||
5400       Mnemonic == "vrintn" || Mnemonic == "vrintp" || Mnemonic == "vrintm" ||
5401       Mnemonic.startswith("aes") || Mnemonic == "hvc" || Mnemonic == "setpan" ||
5402       Mnemonic.startswith("sha1") || Mnemonic.startswith("sha256") ||
5403       (FullInst.startswith("vmull") && FullInst.endswith(".p64")) ||
5404       Mnemonic == "vmovx" || Mnemonic == "vins" ||
5405       Mnemonic == "vudot" || Mnemonic == "vsdot") {
5406     // These mnemonics are never predicable
5407     CanAcceptPredicationCode = false;
5408   } else if (!isThumb()) {
5409     // Some instructions are only predicable in Thumb mode
5410     CanAcceptPredicationCode =
5411         Mnemonic != "cdp2" && Mnemonic != "clrex" && Mnemonic != "mcr2" &&
5412         Mnemonic != "mcrr2" && Mnemonic != "mrc2" && Mnemonic != "mrrc2" &&
5413         Mnemonic != "dmb" && Mnemonic != "dsb" && Mnemonic != "isb" &&
5414         Mnemonic != "pld" && Mnemonic != "pli" && Mnemonic != "pldw" &&
5415         Mnemonic != "ldc2" && Mnemonic != "ldc2l" && Mnemonic != "stc2" &&
5416         Mnemonic != "stc2l" && !Mnemonic.startswith("rfe") &&
5417         !Mnemonic.startswith("srs");
5418   } else if (isThumbOne()) {
5419     if (hasV6MOps())
5420       CanAcceptPredicationCode = Mnemonic != "movs";
5421     else
5422       CanAcceptPredicationCode = Mnemonic != "nop" && Mnemonic != "movs";
5423   } else
5424     CanAcceptPredicationCode = true;
5425 }
5426 
5427 // \brief Some Thumb instructions have two operand forms that are not
5428 // available as three operand, convert to two operand form if possible.
5429 //
5430 // FIXME: We would really like to be able to tablegen'erate this.
5431 void ARMAsmParser::tryConvertingToTwoOperandForm(StringRef Mnemonic,
5432                                                  bool CarrySetting,
5433                                                  OperandVector &Operands) {
5434   if (Operands.size() != 6)
5435     return;
5436 
5437   const auto &Op3 = static_cast<ARMOperand &>(*Operands[3]);
5438         auto &Op4 = static_cast<ARMOperand &>(*Operands[4]);
5439   if (!Op3.isReg() || !Op4.isReg())
5440     return;
5441 
5442   auto Op3Reg = Op3.getReg();
5443   auto Op4Reg = Op4.getReg();
5444 
5445   // For most Thumb2 cases we just generate the 3 operand form and reduce
5446   // it in processInstruction(), but the 3 operand form of ADD (t2ADDrr)
5447   // won't accept SP or PC so we do the transformation here taking care
5448   // with immediate range in the 'add sp, sp #imm' case.
5449   auto &Op5 = static_cast<ARMOperand &>(*Operands[5]);
5450   if (isThumbTwo()) {
5451     if (Mnemonic != "add")
5452       return;
5453     bool TryTransform = Op3Reg == ARM::PC || Op4Reg == ARM::PC ||
5454                         (Op5.isReg() && Op5.getReg() == ARM::PC);
5455     if (!TryTransform) {
5456       TryTransform = (Op3Reg == ARM::SP || Op4Reg == ARM::SP ||
5457                       (Op5.isReg() && Op5.getReg() == ARM::SP)) &&
5458                      !(Op3Reg == ARM::SP && Op4Reg == ARM::SP &&
5459                        Op5.isImm() && !Op5.isImm0_508s4());
5460     }
5461     if (!TryTransform)
5462       return;
5463   } else if (!isThumbOne())
5464     return;
5465 
5466   if (!(Mnemonic == "add" || Mnemonic == "sub" || Mnemonic == "and" ||
5467         Mnemonic == "eor" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
5468         Mnemonic == "asr" || Mnemonic == "adc" || Mnemonic == "sbc" ||
5469         Mnemonic == "ror" || Mnemonic == "orr" || Mnemonic == "bic"))
5470     return;
5471 
5472   // If first 2 operands of a 3 operand instruction are the same
5473   // then transform to 2 operand version of the same instruction
5474   // e.g. 'adds r0, r0, #1' transforms to 'adds r0, #1'
5475   bool Transform = Op3Reg == Op4Reg;
5476 
5477   // For communtative operations, we might be able to transform if we swap
5478   // Op4 and Op5.  The 'ADD Rdm, SP, Rdm' form is already handled specially
5479   // as tADDrsp.
5480   const ARMOperand *LastOp = &Op5;
5481   bool Swap = false;
5482   if (!Transform && Op5.isReg() && Op3Reg == Op5.getReg() &&
5483       ((Mnemonic == "add" && Op4Reg != ARM::SP) ||
5484        Mnemonic == "and" || Mnemonic == "eor" ||
5485        Mnemonic == "adc" || Mnemonic == "orr")) {
5486     Swap = true;
5487     LastOp = &Op4;
5488     Transform = true;
5489   }
5490 
5491   // If both registers are the same then remove one of them from
5492   // the operand list, with certain exceptions.
5493   if (Transform) {
5494     // Don't transform 'adds Rd, Rd, Rm' or 'sub{s} Rd, Rd, Rm' because the
5495     // 2 operand forms don't exist.
5496     if (((Mnemonic == "add" && CarrySetting) || Mnemonic == "sub") &&
5497         LastOp->isReg())
5498       Transform = false;
5499 
5500     // Don't transform 'add/sub{s} Rd, Rd, #imm' if the immediate fits into
5501     // 3-bits because the ARMARM says not to.
5502     if ((Mnemonic == "add" || Mnemonic == "sub") && LastOp->isImm0_7())
5503       Transform = false;
5504   }
5505 
5506   if (Transform) {
5507     if (Swap)
5508       std::swap(Op4, Op5);
5509     Operands.erase(Operands.begin() + 3);
5510   }
5511 }
5512 
5513 bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
5514                                           OperandVector &Operands) {
5515   // FIXME: This is all horribly hacky. We really need a better way to deal
5516   // with optional operands like this in the matcher table.
5517 
5518   // The 'mov' mnemonic is special. One variant has a cc_out operand, while
5519   // another does not. Specifically, the MOVW instruction does not. So we
5520   // special case it here and remove the defaulted (non-setting) cc_out
5521   // operand if that's the instruction we're trying to match.
5522   //
5523   // We do this as post-processing of the explicit operands rather than just
5524   // conditionally adding the cc_out in the first place because we need
5525   // to check the type of the parsed immediate operand.
5526   if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
5527       !static_cast<ARMOperand &>(*Operands[4]).isModImm() &&
5528       static_cast<ARMOperand &>(*Operands[4]).isImm0_65535Expr() &&
5529       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0)
5530     return true;
5531 
5532   // Register-register 'add' for thumb does not have a cc_out operand
5533   // when there are only two register operands.
5534   if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
5535       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5536       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5537       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0)
5538     return true;
5539   // Register-register 'add' for thumb does not have a cc_out operand
5540   // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
5541   // have to check the immediate range here since Thumb2 has a variant
5542   // that can handle a different range and has a cc_out operand.
5543   if (((isThumb() && Mnemonic == "add") ||
5544        (isThumbTwo() && Mnemonic == "sub")) &&
5545       Operands.size() == 6 && static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5546       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5547       static_cast<ARMOperand &>(*Operands[4]).getReg() == ARM::SP &&
5548       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5549       ((Mnemonic == "add" && static_cast<ARMOperand &>(*Operands[5]).isReg()) ||
5550        static_cast<ARMOperand &>(*Operands[5]).isImm0_1020s4()))
5551     return true;
5552   // For Thumb2, add/sub immediate does not have a cc_out operand for the
5553   // imm0_4095 variant. That's the least-preferred variant when
5554   // selecting via the generic "add" mnemonic, so to know that we
5555   // should remove the cc_out operand, we have to explicitly check that
5556   // it's not one of the other variants. Ugh.
5557   if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
5558       Operands.size() == 6 && static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5559       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5560       static_cast<ARMOperand &>(*Operands[5]).isImm()) {
5561     // Nest conditions rather than one big 'if' statement for readability.
5562     //
5563     // If both registers are low, we're in an IT block, and the immediate is
5564     // in range, we should use encoding T1 instead, which has a cc_out.
5565     if (inITBlock() &&
5566         isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) &&
5567         isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) &&
5568         static_cast<ARMOperand &>(*Operands[5]).isImm0_7())
5569       return false;
5570     // Check against T3. If the second register is the PC, this is an
5571     // alternate form of ADR, which uses encoding T4, so check for that too.
5572     if (static_cast<ARMOperand &>(*Operands[4]).getReg() != ARM::PC &&
5573         static_cast<ARMOperand &>(*Operands[5]).isT2SOImm())
5574       return false;
5575 
5576     // Otherwise, we use encoding T4, which does not have a cc_out
5577     // operand.
5578     return true;
5579   }
5580 
5581   // The thumb2 multiply instruction doesn't have a CCOut register, so
5582   // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
5583   // use the 16-bit encoding or not.
5584   if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
5585       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5586       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5587       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5588       static_cast<ARMOperand &>(*Operands[5]).isReg() &&
5589       // If the registers aren't low regs, the destination reg isn't the
5590       // same as one of the source regs, or the cc_out operand is zero
5591       // outside of an IT block, we have to use the 32-bit encoding, so
5592       // remove the cc_out operand.
5593       (!isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) ||
5594        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) ||
5595        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[5]).getReg()) ||
5596        !inITBlock() || (static_cast<ARMOperand &>(*Operands[3]).getReg() !=
5597                             static_cast<ARMOperand &>(*Operands[5]).getReg() &&
5598                         static_cast<ARMOperand &>(*Operands[3]).getReg() !=
5599                             static_cast<ARMOperand &>(*Operands[4]).getReg())))
5600     return true;
5601 
5602   // Also check the 'mul' syntax variant that doesn't specify an explicit
5603   // destination register.
5604   if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 5 &&
5605       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5606       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5607       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5608       // If the registers aren't low regs  or the cc_out operand is zero
5609       // outside of an IT block, we have to use the 32-bit encoding, so
5610       // remove the cc_out operand.
5611       (!isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) ||
5612        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) ||
5613        !inITBlock()))
5614     return true;
5615 
5616 
5617 
5618   // Register-register 'add/sub' for thumb does not have a cc_out operand
5619   // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
5620   // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
5621   // right, this will result in better diagnostics (which operand is off)
5622   // anyway.
5623   if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
5624       (Operands.size() == 5 || Operands.size() == 6) &&
5625       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5626       static_cast<ARMOperand &>(*Operands[3]).getReg() == ARM::SP &&
5627       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5628       (static_cast<ARMOperand &>(*Operands[4]).isImm() ||
5629        (Operands.size() == 6 &&
5630         static_cast<ARMOperand &>(*Operands[5]).isImm())))
5631     return true;
5632 
5633   return false;
5634 }
5635 
5636 bool ARMAsmParser::shouldOmitPredicateOperand(StringRef Mnemonic,
5637                                               OperandVector &Operands) {
5638   // VRINT{Z, R, X} have a predicate operand in VFP, but not in NEON
5639   unsigned RegIdx = 3;
5640   if ((Mnemonic == "vrintz" || Mnemonic == "vrintx" || Mnemonic == "vrintr") &&
5641       (static_cast<ARMOperand &>(*Operands[2]).getToken() == ".f32" ||
5642        static_cast<ARMOperand &>(*Operands[2]).getToken() == ".f16")) {
5643     if (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
5644         (static_cast<ARMOperand &>(*Operands[3]).getToken() == ".f32" ||
5645          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".f16"))
5646       RegIdx = 4;
5647 
5648     if (static_cast<ARMOperand &>(*Operands[RegIdx]).isReg() &&
5649         (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(
5650              static_cast<ARMOperand &>(*Operands[RegIdx]).getReg()) ||
5651          ARMMCRegisterClasses[ARM::QPRRegClassID].contains(
5652              static_cast<ARMOperand &>(*Operands[RegIdx]).getReg())))
5653       return true;
5654   }
5655   return false;
5656 }
5657 
5658 static bool isDataTypeToken(StringRef Tok) {
5659   return Tok == ".8" || Tok == ".16" || Tok == ".32" || Tok == ".64" ||
5660     Tok == ".i8" || Tok == ".i16" || Tok == ".i32" || Tok == ".i64" ||
5661     Tok == ".u8" || Tok == ".u16" || Tok == ".u32" || Tok == ".u64" ||
5662     Tok == ".s8" || Tok == ".s16" || Tok == ".s32" || Tok == ".s64" ||
5663     Tok == ".p8" || Tok == ".p16" || Tok == ".f32" || Tok == ".f64" ||
5664     Tok == ".f" || Tok == ".d";
5665 }
5666 
5667 // FIXME: This bit should probably be handled via an explicit match class
5668 // in the .td files that matches the suffix instead of having it be
5669 // a literal string token the way it is now.
5670 static bool doesIgnoreDataTypeSuffix(StringRef Mnemonic, StringRef DT) {
5671   return Mnemonic.startswith("vldm") || Mnemonic.startswith("vstm");
5672 }
5673 static void applyMnemonicAliases(StringRef &Mnemonic, uint64_t Features,
5674                                  unsigned VariantID);
5675 
5676 static bool RequiresVFPRegListValidation(StringRef Inst,
5677                                          bool &AcceptSinglePrecisionOnly,
5678                                          bool &AcceptDoublePrecisionOnly) {
5679   if (Inst.size() < 7)
5680     return false;
5681 
5682   if (Inst.startswith("fldm") || Inst.startswith("fstm")) {
5683     StringRef AddressingMode = Inst.substr(4, 2);
5684     if (AddressingMode == "ia" || AddressingMode == "db" ||
5685         AddressingMode == "ea" || AddressingMode == "fd") {
5686       AcceptSinglePrecisionOnly = Inst[6] == 's';
5687       AcceptDoublePrecisionOnly = Inst[6] == 'd' || Inst[6] == 'x';
5688       return true;
5689     }
5690   }
5691 
5692   return false;
5693 }
5694 
5695 /// Parse an arm instruction mnemonic followed by its operands.
5696 bool ARMAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
5697                                     SMLoc NameLoc, OperandVector &Operands) {
5698   MCAsmParser &Parser = getParser();
5699   // FIXME: Can this be done via tablegen in some fashion?
5700   bool RequireVFPRegisterListCheck;
5701   bool AcceptSinglePrecisionOnly;
5702   bool AcceptDoublePrecisionOnly;
5703   RequireVFPRegisterListCheck =
5704     RequiresVFPRegListValidation(Name, AcceptSinglePrecisionOnly,
5705                                  AcceptDoublePrecisionOnly);
5706 
5707   // Apply mnemonic aliases before doing anything else, as the destination
5708   // mnemonic may include suffices and we want to handle them normally.
5709   // The generic tblgen'erated code does this later, at the start of
5710   // MatchInstructionImpl(), but that's too late for aliases that include
5711   // any sort of suffix.
5712   uint64_t AvailableFeatures = getAvailableFeatures();
5713   unsigned AssemblerDialect = getParser().getAssemblerDialect();
5714   applyMnemonicAliases(Name, AvailableFeatures, AssemblerDialect);
5715 
5716   // First check for the ARM-specific .req directive.
5717   if (Parser.getTok().is(AsmToken::Identifier) &&
5718       Parser.getTok().getIdentifier() == ".req") {
5719     parseDirectiveReq(Name, NameLoc);
5720     // We always return 'error' for this, as we're done with this
5721     // statement and don't need to match the 'instruction."
5722     return true;
5723   }
5724 
5725   // Create the leading tokens for the mnemonic, split by '.' characters.
5726   size_t Start = 0, Next = Name.find('.');
5727   StringRef Mnemonic = Name.slice(Start, Next);
5728 
5729   // Split out the predication code and carry setting flag from the mnemonic.
5730   unsigned PredicationCode;
5731   unsigned ProcessorIMod;
5732   bool CarrySetting;
5733   StringRef ITMask;
5734   Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
5735                            ProcessorIMod, ITMask);
5736 
5737   // In Thumb1, only the branch (B) instruction can be predicated.
5738   if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
5739     return Error(NameLoc, "conditional execution not supported in Thumb1");
5740   }
5741 
5742   Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
5743 
5744   // Handle the IT instruction ITMask. Convert it to a bitmask. This
5745   // is the mask as it will be for the IT encoding if the conditional
5746   // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
5747   // where the conditional bit0 is zero, the instruction post-processing
5748   // will adjust the mask accordingly.
5749   if (Mnemonic == "it") {
5750     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
5751     if (ITMask.size() > 3) {
5752       return Error(Loc, "too many conditions on IT instruction");
5753     }
5754     unsigned Mask = 8;
5755     for (unsigned i = ITMask.size(); i != 0; --i) {
5756       char pos = ITMask[i - 1];
5757       if (pos != 't' && pos != 'e') {
5758         return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
5759       }
5760       Mask >>= 1;
5761       if (ITMask[i - 1] == 't')
5762         Mask |= 8;
5763     }
5764     Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
5765   }
5766 
5767   // FIXME: This is all a pretty gross hack. We should automatically handle
5768   // optional operands like this via tblgen.
5769 
5770   // Next, add the CCOut and ConditionCode operands, if needed.
5771   //
5772   // For mnemonics which can ever incorporate a carry setting bit or predication
5773   // code, our matching model involves us always generating CCOut and
5774   // ConditionCode operands to match the mnemonic "as written" and then we let
5775   // the matcher deal with finding the right instruction or generating an
5776   // appropriate error.
5777   bool CanAcceptCarrySet, CanAcceptPredicationCode;
5778   getMnemonicAcceptInfo(Mnemonic, Name, CanAcceptCarrySet, CanAcceptPredicationCode);
5779 
5780   // If we had a carry-set on an instruction that can't do that, issue an
5781   // error.
5782   if (!CanAcceptCarrySet && CarrySetting) {
5783     return Error(NameLoc, "instruction '" + Mnemonic +
5784                  "' can not set flags, but 's' suffix specified");
5785   }
5786   // If we had a predication code on an instruction that can't do that, issue an
5787   // error.
5788   if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
5789     return Error(NameLoc, "instruction '" + Mnemonic +
5790                  "' is not predicable, but condition code specified");
5791   }
5792 
5793   // Add the carry setting operand, if necessary.
5794   if (CanAcceptCarrySet) {
5795     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
5796     Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
5797                                                Loc));
5798   }
5799 
5800   // Add the predication code operand, if necessary.
5801   if (CanAcceptPredicationCode) {
5802     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
5803                                       CarrySetting);
5804     Operands.push_back(ARMOperand::CreateCondCode(
5805                          ARMCC::CondCodes(PredicationCode), Loc));
5806   }
5807 
5808   // Add the processor imod operand, if necessary.
5809   if (ProcessorIMod) {
5810     Operands.push_back(ARMOperand::CreateImm(
5811           MCConstantExpr::create(ProcessorIMod, getContext()),
5812                                  NameLoc, NameLoc));
5813   } else if (Mnemonic == "cps" && isMClass()) {
5814     return Error(NameLoc, "instruction 'cps' requires effect for M-class");
5815   }
5816 
5817   // Add the remaining tokens in the mnemonic.
5818   while (Next != StringRef::npos) {
5819     Start = Next;
5820     Next = Name.find('.', Start + 1);
5821     StringRef ExtraToken = Name.slice(Start, Next);
5822 
5823     // Some NEON instructions have an optional datatype suffix that is
5824     // completely ignored. Check for that.
5825     if (isDataTypeToken(ExtraToken) &&
5826         doesIgnoreDataTypeSuffix(Mnemonic, ExtraToken))
5827       continue;
5828 
5829     // For for ARM mode generate an error if the .n qualifier is used.
5830     if (ExtraToken == ".n" && !isThumb()) {
5831       SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
5832       return Error(Loc, "instruction with .n (narrow) qualifier not allowed in "
5833                    "arm mode");
5834     }
5835 
5836     // The .n qualifier is always discarded as that is what the tables
5837     // and matcher expect.  In ARM mode the .w qualifier has no effect,
5838     // so discard it to avoid errors that can be caused by the matcher.
5839     if (ExtraToken != ".n" && (isThumb() || ExtraToken != ".w")) {
5840       SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
5841       Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
5842     }
5843   }
5844 
5845   // Read the remaining operands.
5846   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5847     // Read the first operand.
5848     if (parseOperand(Operands, Mnemonic)) {
5849       return true;
5850     }
5851 
5852     while (parseOptionalToken(AsmToken::Comma)) {
5853       // Parse and remember the operand.
5854       if (parseOperand(Operands, Mnemonic)) {
5855         return true;
5856       }
5857     }
5858   }
5859 
5860   if (parseToken(AsmToken::EndOfStatement, "unexpected token in argument list"))
5861     return true;
5862 
5863   if (RequireVFPRegisterListCheck) {
5864     ARMOperand &Op = static_cast<ARMOperand &>(*Operands.back());
5865     if (AcceptSinglePrecisionOnly && !Op.isSPRRegList())
5866       return Error(Op.getStartLoc(),
5867                    "VFP/Neon single precision register expected");
5868     if (AcceptDoublePrecisionOnly && !Op.isDPRRegList())
5869       return Error(Op.getStartLoc(),
5870                    "VFP/Neon double precision register expected");
5871   }
5872 
5873   tryConvertingToTwoOperandForm(Mnemonic, CarrySetting, Operands);
5874 
5875   // Some instructions, mostly Thumb, have forms for the same mnemonic that
5876   // do and don't have a cc_out optional-def operand. With some spot-checks
5877   // of the operand list, we can figure out which variant we're trying to
5878   // parse and adjust accordingly before actually matching. We shouldn't ever
5879   // try to remove a cc_out operand that was explicitly set on the
5880   // mnemonic, of course (CarrySetting == true). Reason number #317 the
5881   // table driven matcher doesn't fit well with the ARM instruction set.
5882   if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands))
5883     Operands.erase(Operands.begin() + 1);
5884 
5885   // Some instructions have the same mnemonic, but don't always
5886   // have a predicate. Distinguish them here and delete the
5887   // predicate if needed.
5888   if (shouldOmitPredicateOperand(Mnemonic, Operands))
5889     Operands.erase(Operands.begin() + 1);
5890 
5891   // ARM mode 'blx' need special handling, as the register operand version
5892   // is predicable, but the label operand version is not. So, we can't rely
5893   // on the Mnemonic based checking to correctly figure out when to put
5894   // a k_CondCode operand in the list. If we're trying to match the label
5895   // version, remove the k_CondCode operand here.
5896   if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
5897       static_cast<ARMOperand &>(*Operands[2]).isImm())
5898     Operands.erase(Operands.begin() + 1);
5899 
5900   // Adjust operands of ldrexd/strexd to MCK_GPRPair.
5901   // ldrexd/strexd require even/odd GPR pair. To enforce this constraint,
5902   // a single GPRPair reg operand is used in the .td file to replace the two
5903   // GPRs. However, when parsing from asm, the two GRPs cannot be automatically
5904   // expressed as a GPRPair, so we have to manually merge them.
5905   // FIXME: We would really like to be able to tablegen'erate this.
5906   if (!isThumb() && Operands.size() > 4 &&
5907       (Mnemonic == "ldrexd" || Mnemonic == "strexd" || Mnemonic == "ldaexd" ||
5908        Mnemonic == "stlexd")) {
5909     bool isLoad = (Mnemonic == "ldrexd" || Mnemonic == "ldaexd");
5910     unsigned Idx = isLoad ? 2 : 3;
5911     ARMOperand &Op1 = static_cast<ARMOperand &>(*Operands[Idx]);
5912     ARMOperand &Op2 = static_cast<ARMOperand &>(*Operands[Idx + 1]);
5913 
5914     const MCRegisterClass& MRC = MRI->getRegClass(ARM::GPRRegClassID);
5915     // Adjust only if Op1 and Op2 are GPRs.
5916     if (Op1.isReg() && Op2.isReg() && MRC.contains(Op1.getReg()) &&
5917         MRC.contains(Op2.getReg())) {
5918       unsigned Reg1 = Op1.getReg();
5919       unsigned Reg2 = Op2.getReg();
5920       unsigned Rt = MRI->getEncodingValue(Reg1);
5921       unsigned Rt2 = MRI->getEncodingValue(Reg2);
5922 
5923       // Rt2 must be Rt + 1 and Rt must be even.
5924       if (Rt + 1 != Rt2 || (Rt & 1)) {
5925         return Error(Op2.getStartLoc(),
5926                      isLoad ? "destination operands must be sequential"
5927                             : "source operands must be sequential");
5928       }
5929       unsigned NewReg = MRI->getMatchingSuperReg(Reg1, ARM::gsub_0,
5930           &(MRI->getRegClass(ARM::GPRPairRegClassID)));
5931       Operands[Idx] =
5932           ARMOperand::CreateReg(NewReg, Op1.getStartLoc(), Op2.getEndLoc());
5933       Operands.erase(Operands.begin() + Idx + 1);
5934     }
5935   }
5936 
5937   // GNU Assembler extension (compatibility)
5938   if ((Mnemonic == "ldrd" || Mnemonic == "strd")) {
5939     ARMOperand &Op2 = static_cast<ARMOperand &>(*Operands[2]);
5940     ARMOperand &Op3 = static_cast<ARMOperand &>(*Operands[3]);
5941     if (Op3.isMem()) {
5942       assert(Op2.isReg() && "expected register argument");
5943 
5944       unsigned SuperReg = MRI->getMatchingSuperReg(
5945           Op2.getReg(), ARM::gsub_0, &MRI->getRegClass(ARM::GPRPairRegClassID));
5946 
5947       assert(SuperReg && "expected register pair");
5948 
5949       unsigned PairedReg = MRI->getSubReg(SuperReg, ARM::gsub_1);
5950 
5951       Operands.insert(
5952           Operands.begin() + 3,
5953           ARMOperand::CreateReg(PairedReg, Op2.getStartLoc(), Op2.getEndLoc()));
5954     }
5955   }
5956 
5957   // FIXME: As said above, this is all a pretty gross hack.  This instruction
5958   // does not fit with other "subs" and tblgen.
5959   // Adjust operands of B9.3.19 SUBS PC, LR, #imm (Thumb2) system instruction
5960   // so the Mnemonic is the original name "subs" and delete the predicate
5961   // operand so it will match the table entry.
5962   if (isThumbTwo() && Mnemonic == "sub" && Operands.size() == 6 &&
5963       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5964       static_cast<ARMOperand &>(*Operands[3]).getReg() == ARM::PC &&
5965       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5966       static_cast<ARMOperand &>(*Operands[4]).getReg() == ARM::LR &&
5967       static_cast<ARMOperand &>(*Operands[5]).isImm()) {
5968     Operands.front() = ARMOperand::CreateToken(Name, NameLoc);
5969     Operands.erase(Operands.begin() + 1);
5970   }
5971   return false;
5972 }
5973 
5974 // Validate context-sensitive operand constraints.
5975 
5976 // return 'true' if register list contains non-low GPR registers,
5977 // 'false' otherwise. If Reg is in the register list or is HiReg, set
5978 // 'containsReg' to true.
5979 static bool checkLowRegisterList(const MCInst &Inst, unsigned OpNo,
5980                                  unsigned Reg, unsigned HiReg,
5981                                  bool &containsReg) {
5982   containsReg = false;
5983   for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
5984     unsigned OpReg = Inst.getOperand(i).getReg();
5985     if (OpReg == Reg)
5986       containsReg = true;
5987     // Anything other than a low register isn't legal here.
5988     if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
5989       return true;
5990   }
5991   return false;
5992 }
5993 
5994 // Check if the specified regisgter is in the register list of the inst,
5995 // starting at the indicated operand number.
5996 static bool listContainsReg(const MCInst &Inst, unsigned OpNo, unsigned Reg) {
5997   for (unsigned i = OpNo, e = Inst.getNumOperands(); i < e; ++i) {
5998     unsigned OpReg = Inst.getOperand(i).getReg();
5999     if (OpReg == Reg)
6000       return true;
6001   }
6002   return false;
6003 }
6004 
6005 // Return true if instruction has the interesting property of being
6006 // allowed in IT blocks, but not being predicable.
6007 static bool instIsBreakpoint(const MCInst &Inst) {
6008     return Inst.getOpcode() == ARM::tBKPT ||
6009            Inst.getOpcode() == ARM::BKPT ||
6010            Inst.getOpcode() == ARM::tHLT ||
6011            Inst.getOpcode() == ARM::HLT;
6012 
6013 }
6014 
6015 bool ARMAsmParser::validatetLDMRegList(const MCInst &Inst,
6016                                        const OperandVector &Operands,
6017                                        unsigned ListNo, bool IsARPop) {
6018   const ARMOperand &Op = static_cast<const ARMOperand &>(*Operands[ListNo]);
6019   bool HasWritebackToken = Op.isToken() && Op.getToken() == "!";
6020 
6021   bool ListContainsSP = listContainsReg(Inst, ListNo, ARM::SP);
6022   bool ListContainsLR = listContainsReg(Inst, ListNo, ARM::LR);
6023   bool ListContainsPC = listContainsReg(Inst, ListNo, ARM::PC);
6024 
6025   if (!IsARPop && ListContainsSP)
6026     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6027                  "SP may not be in the register list");
6028   else if (ListContainsPC && ListContainsLR)
6029     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6030                  "PC and LR may not be in the register list simultaneously");
6031   return false;
6032 }
6033 
6034 bool ARMAsmParser::validatetSTMRegList(const MCInst &Inst,
6035                                        const OperandVector &Operands,
6036                                        unsigned ListNo) {
6037   const ARMOperand &Op = static_cast<const ARMOperand &>(*Operands[ListNo]);
6038   bool HasWritebackToken = Op.isToken() && Op.getToken() == "!";
6039 
6040   bool ListContainsSP = listContainsReg(Inst, ListNo, ARM::SP);
6041   bool ListContainsPC = listContainsReg(Inst, ListNo, ARM::PC);
6042 
6043   if (ListContainsSP && ListContainsPC)
6044     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6045                  "SP and PC may not be in the register list");
6046   else if (ListContainsSP)
6047     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6048                  "SP may not be in the register list");
6049   else if (ListContainsPC)
6050     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6051                  "PC may not be in the register list");
6052   return false;
6053 }
6054 
6055 // FIXME: We would really like to be able to tablegen'erate this.
6056 bool ARMAsmParser::validateInstruction(MCInst &Inst,
6057                                        const OperandVector &Operands) {
6058   const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
6059   SMLoc Loc = Operands[0]->getStartLoc();
6060 
6061   // Check the IT block state first.
6062   // NOTE: BKPT and HLT instructions have the interesting property of being
6063   // allowed in IT blocks, but not being predicable. They just always execute.
6064   if (inITBlock() && !instIsBreakpoint(Inst)) {
6065     // The instruction must be predicable.
6066     if (!MCID.isPredicable())
6067       return Error(Loc, "instructions in IT block must be predicable");
6068     unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
6069     if (Cond != currentITCond()) {
6070       // Find the condition code Operand to get its SMLoc information.
6071       SMLoc CondLoc;
6072       for (unsigned I = 1; I < Operands.size(); ++I)
6073         if (static_cast<ARMOperand &>(*Operands[I]).isCondCode())
6074           CondLoc = Operands[I]->getStartLoc();
6075       return Error(CondLoc, "incorrect condition in IT block; got '" +
6076                    StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
6077                    "', but expected '" +
6078                    ARMCondCodeToString(ARMCC::CondCodes(currentITCond())) + "'");
6079     }
6080   // Check for non-'al' condition codes outside of the IT block.
6081   } else if (isThumbTwo() && MCID.isPredicable() &&
6082              Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
6083              ARMCC::AL && Inst.getOpcode() != ARM::tBcc &&
6084              Inst.getOpcode() != ARM::t2Bcc) {
6085     return Error(Loc, "predicated instructions must be in IT block");
6086   } else if (!isThumb() && !useImplicitITARM() && MCID.isPredicable() &&
6087              Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
6088                  ARMCC::AL) {
6089     return Warning(Loc, "predicated instructions should be in IT block");
6090   }
6091 
6092   // PC-setting instructions in an IT block, but not the last instruction of
6093   // the block, are UNPREDICTABLE.
6094   if (inExplicitITBlock() && !lastInITBlock() && isITBlockTerminator(Inst)) {
6095     return Error(Loc, "instruction must be outside of IT block or the last instruction in an IT block");
6096   }
6097 
6098   const unsigned Opcode = Inst.getOpcode();
6099   switch (Opcode) {
6100   case ARM::LDRD:
6101   case ARM::LDRD_PRE:
6102   case ARM::LDRD_POST: {
6103     const unsigned RtReg = Inst.getOperand(0).getReg();
6104 
6105     // Rt can't be R14.
6106     if (RtReg == ARM::LR)
6107       return Error(Operands[3]->getStartLoc(),
6108                    "Rt can't be R14");
6109 
6110     const unsigned Rt = MRI->getEncodingValue(RtReg);
6111     // Rt must be even-numbered.
6112     if ((Rt & 1) == 1)
6113       return Error(Operands[3]->getStartLoc(),
6114                    "Rt must be even-numbered");
6115 
6116     // Rt2 must be Rt + 1.
6117     const unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6118     if (Rt2 != Rt + 1)
6119       return Error(Operands[3]->getStartLoc(),
6120                    "destination operands must be sequential");
6121 
6122     if (Opcode == ARM::LDRD_PRE || Opcode == ARM::LDRD_POST) {
6123       const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(3).getReg());
6124       // For addressing modes with writeback, the base register needs to be
6125       // different from the destination registers.
6126       if (Rn == Rt || Rn == Rt2)
6127         return Error(Operands[3]->getStartLoc(),
6128                      "base register needs to be different from destination "
6129                      "registers");
6130     }
6131 
6132     return false;
6133   }
6134   case ARM::t2LDRDi8:
6135   case ARM::t2LDRD_PRE:
6136   case ARM::t2LDRD_POST: {
6137     // Rt2 must be different from Rt.
6138     unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
6139     unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6140     if (Rt2 == Rt)
6141       return Error(Operands[3]->getStartLoc(),
6142                    "destination operands can't be identical");
6143     return false;
6144   }
6145   case ARM::t2BXJ: {
6146     const unsigned RmReg = Inst.getOperand(0).getReg();
6147     // Rm = SP is no longer unpredictable in v8-A
6148     if (RmReg == ARM::SP && !hasV8Ops())
6149       return Error(Operands[2]->getStartLoc(),
6150                    "r13 (SP) is an unpredictable operand to BXJ");
6151     return false;
6152   }
6153   case ARM::STRD: {
6154     // Rt2 must be Rt + 1.
6155     unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
6156     unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6157     if (Rt2 != Rt + 1)
6158       return Error(Operands[3]->getStartLoc(),
6159                    "source operands must be sequential");
6160     return false;
6161   }
6162   case ARM::STRD_PRE:
6163   case ARM::STRD_POST: {
6164     // Rt2 must be Rt + 1.
6165     unsigned Rt = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6166     unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(2).getReg());
6167     if (Rt2 != Rt + 1)
6168       return Error(Operands[3]->getStartLoc(),
6169                    "source operands must be sequential");
6170     return false;
6171   }
6172   case ARM::STR_PRE_IMM:
6173   case ARM::STR_PRE_REG:
6174   case ARM::STR_POST_IMM:
6175   case ARM::STR_POST_REG:
6176   case ARM::STRH_PRE:
6177   case ARM::STRH_POST:
6178   case ARM::STRB_PRE_IMM:
6179   case ARM::STRB_PRE_REG:
6180   case ARM::STRB_POST_IMM:
6181   case ARM::STRB_POST_REG: {
6182     // Rt must be different from Rn.
6183     const unsigned Rt = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6184     const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(2).getReg());
6185 
6186     if (Rt == Rn)
6187       return Error(Operands[3]->getStartLoc(),
6188                    "source register and base register can't be identical");
6189     return false;
6190   }
6191   case ARM::LDR_PRE_IMM:
6192   case ARM::LDR_PRE_REG:
6193   case ARM::LDR_POST_IMM:
6194   case ARM::LDR_POST_REG:
6195   case ARM::LDRH_PRE:
6196   case ARM::LDRH_POST:
6197   case ARM::LDRSH_PRE:
6198   case ARM::LDRSH_POST:
6199   case ARM::LDRB_PRE_IMM:
6200   case ARM::LDRB_PRE_REG:
6201   case ARM::LDRB_POST_IMM:
6202   case ARM::LDRB_POST_REG:
6203   case ARM::LDRSB_PRE:
6204   case ARM::LDRSB_POST: {
6205     // Rt must be different from Rn.
6206     const unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
6207     const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(2).getReg());
6208 
6209     if (Rt == Rn)
6210       return Error(Operands[3]->getStartLoc(),
6211                    "destination register and base register can't be identical");
6212     return false;
6213   }
6214   case ARM::SBFX:
6215   case ARM::UBFX: {
6216     // Width must be in range [1, 32-lsb].
6217     unsigned LSB = Inst.getOperand(2).getImm();
6218     unsigned Widthm1 = Inst.getOperand(3).getImm();
6219     if (Widthm1 >= 32 - LSB)
6220       return Error(Operands[5]->getStartLoc(),
6221                    "bitfield width must be in range [1,32-lsb]");
6222     return false;
6223   }
6224   // Notionally handles ARM::tLDMIA_UPD too.
6225   case ARM::tLDMIA: {
6226     // If we're parsing Thumb2, the .w variant is available and handles
6227     // most cases that are normally illegal for a Thumb1 LDM instruction.
6228     // We'll make the transformation in processInstruction() if necessary.
6229     //
6230     // Thumb LDM instructions are writeback iff the base register is not
6231     // in the register list.
6232     unsigned Rn = Inst.getOperand(0).getReg();
6233     bool HasWritebackToken =
6234         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
6235          static_cast<ARMOperand &>(*Operands[3]).getToken() == "!");
6236     bool ListContainsBase;
6237     if (checkLowRegisterList(Inst, 3, Rn, 0, ListContainsBase) && !isThumbTwo())
6238       return Error(Operands[3 + HasWritebackToken]->getStartLoc(),
6239                    "registers must be in range r0-r7");
6240     // If we should have writeback, then there should be a '!' token.
6241     if (!ListContainsBase && !HasWritebackToken && !isThumbTwo())
6242       return Error(Operands[2]->getStartLoc(),
6243                    "writeback operator '!' expected");
6244     // If we should not have writeback, there must not be a '!'. This is
6245     // true even for the 32-bit wide encodings.
6246     if (ListContainsBase && HasWritebackToken)
6247       return Error(Operands[3]->getStartLoc(),
6248                    "writeback operator '!' not allowed when base register "
6249                    "in register list");
6250 
6251     if (validatetLDMRegList(Inst, Operands, 3))
6252       return true;
6253     break;
6254   }
6255   case ARM::LDMIA_UPD:
6256   case ARM::LDMDB_UPD:
6257   case ARM::LDMIB_UPD:
6258   case ARM::LDMDA_UPD:
6259     // ARM variants loading and updating the same register are only officially
6260     // UNPREDICTABLE on v7 upwards. Goodness knows what they did before.
6261     if (!hasV7Ops())
6262       break;
6263     if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
6264       return Error(Operands.back()->getStartLoc(),
6265                    "writeback register not allowed in register list");
6266     break;
6267   case ARM::t2LDMIA:
6268   case ARM::t2LDMDB:
6269     if (validatetLDMRegList(Inst, Operands, 3))
6270       return true;
6271     break;
6272   case ARM::t2STMIA:
6273   case ARM::t2STMDB:
6274     if (validatetSTMRegList(Inst, Operands, 3))
6275       return true;
6276     break;
6277   case ARM::t2LDMIA_UPD:
6278   case ARM::t2LDMDB_UPD:
6279   case ARM::t2STMIA_UPD:
6280   case ARM::t2STMDB_UPD: {
6281     if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
6282       return Error(Operands.back()->getStartLoc(),
6283                    "writeback register not allowed in register list");
6284 
6285     if (Opcode == ARM::t2LDMIA_UPD || Opcode == ARM::t2LDMDB_UPD) {
6286       if (validatetLDMRegList(Inst, Operands, 3))
6287         return true;
6288     } else {
6289       if (validatetSTMRegList(Inst, Operands, 3))
6290         return true;
6291     }
6292     break;
6293   }
6294   case ARM::sysLDMIA_UPD:
6295   case ARM::sysLDMDA_UPD:
6296   case ARM::sysLDMDB_UPD:
6297   case ARM::sysLDMIB_UPD:
6298     if (!listContainsReg(Inst, 3, ARM::PC))
6299       return Error(Operands[4]->getStartLoc(),
6300                    "writeback register only allowed on system LDM "
6301                    "if PC in register-list");
6302     break;
6303   case ARM::sysSTMIA_UPD:
6304   case ARM::sysSTMDA_UPD:
6305   case ARM::sysSTMDB_UPD:
6306   case ARM::sysSTMIB_UPD:
6307     return Error(Operands[2]->getStartLoc(),
6308                  "system STM cannot have writeback register");
6309   case ARM::tMUL: {
6310     // The second source operand must be the same register as the destination
6311     // operand.
6312     //
6313     // In this case, we must directly check the parsed operands because the
6314     // cvtThumbMultiply() function is written in such a way that it guarantees
6315     // this first statement is always true for the new Inst.  Essentially, the
6316     // destination is unconditionally copied into the second source operand
6317     // without checking to see if it matches what we actually parsed.
6318     if (Operands.size() == 6 && (((ARMOperand &)*Operands[3]).getReg() !=
6319                                  ((ARMOperand &)*Operands[5]).getReg()) &&
6320         (((ARMOperand &)*Operands[3]).getReg() !=
6321          ((ARMOperand &)*Operands[4]).getReg())) {
6322       return Error(Operands[3]->getStartLoc(),
6323                    "destination register must match source register");
6324     }
6325     break;
6326   }
6327   // Like for ldm/stm, push and pop have hi-reg handling version in Thumb2,
6328   // so only issue a diagnostic for thumb1. The instructions will be
6329   // switched to the t2 encodings in processInstruction() if necessary.
6330   case ARM::tPOP: {
6331     bool ListContainsBase;
6332     if (checkLowRegisterList(Inst, 2, 0, ARM::PC, ListContainsBase) &&
6333         !isThumbTwo())
6334       return Error(Operands[2]->getStartLoc(),
6335                    "registers must be in range r0-r7 or pc");
6336     if (validatetLDMRegList(Inst, Operands, 2, !isMClass()))
6337       return true;
6338     break;
6339   }
6340   case ARM::tPUSH: {
6341     bool ListContainsBase;
6342     if (checkLowRegisterList(Inst, 2, 0, ARM::LR, ListContainsBase) &&
6343         !isThumbTwo())
6344       return Error(Operands[2]->getStartLoc(),
6345                    "registers must be in range r0-r7 or lr");
6346     if (validatetSTMRegList(Inst, Operands, 2))
6347       return true;
6348     break;
6349   }
6350   case ARM::tSTMIA_UPD: {
6351     bool ListContainsBase, InvalidLowList;
6352     InvalidLowList = checkLowRegisterList(Inst, 4, Inst.getOperand(0).getReg(),
6353                                           0, ListContainsBase);
6354     if (InvalidLowList && !isThumbTwo())
6355       return Error(Operands[4]->getStartLoc(),
6356                    "registers must be in range r0-r7");
6357 
6358     // This would be converted to a 32-bit stm, but that's not valid if the
6359     // writeback register is in the list.
6360     if (InvalidLowList && ListContainsBase)
6361       return Error(Operands[4]->getStartLoc(),
6362                    "writeback operator '!' not allowed when base register "
6363                    "in register list");
6364 
6365     if (validatetSTMRegList(Inst, Operands, 4))
6366       return true;
6367     break;
6368   }
6369   case ARM::tADDrSP: {
6370     // If the non-SP source operand and the destination operand are not the
6371     // same, we need thumb2 (for the wide encoding), or we have an error.
6372     if (!isThumbTwo() &&
6373         Inst.getOperand(0).getReg() != Inst.getOperand(2).getReg()) {
6374       return Error(Operands[4]->getStartLoc(),
6375                    "source register must be the same as destination");
6376     }
6377     break;
6378   }
6379   // Final range checking for Thumb unconditional branch instructions.
6380   case ARM::tB:
6381     if (!(static_cast<ARMOperand &>(*Operands[2])).isSignedOffset<11, 1>())
6382       return Error(Operands[2]->getStartLoc(), "branch target out of range");
6383     break;
6384   case ARM::t2B: {
6385     int op = (Operands[2]->isImm()) ? 2 : 3;
6386     if (!static_cast<ARMOperand &>(*Operands[op]).isSignedOffset<24, 1>())
6387       return Error(Operands[op]->getStartLoc(), "branch target out of range");
6388     break;
6389   }
6390   // Final range checking for Thumb conditional branch instructions.
6391   case ARM::tBcc:
6392     if (!static_cast<ARMOperand &>(*Operands[2]).isSignedOffset<8, 1>())
6393       return Error(Operands[2]->getStartLoc(), "branch target out of range");
6394     break;
6395   case ARM::t2Bcc: {
6396     int Op = (Operands[2]->isImm()) ? 2 : 3;
6397     if (!static_cast<ARMOperand &>(*Operands[Op]).isSignedOffset<20, 1>())
6398       return Error(Operands[Op]->getStartLoc(), "branch target out of range");
6399     break;
6400   }
6401   case ARM::tCBZ:
6402   case ARM::tCBNZ: {
6403     if (!static_cast<ARMOperand &>(*Operands[2]).isUnsignedOffset<6, 1>())
6404       return Error(Operands[2]->getStartLoc(), "branch target out of range");
6405     break;
6406   }
6407   case ARM::MOVi16:
6408   case ARM::MOVTi16:
6409   case ARM::t2MOVi16:
6410   case ARM::t2MOVTi16:
6411     {
6412     // We want to avoid misleadingly allowing something like "mov r0, <symbol>"
6413     // especially when we turn it into a movw and the expression <symbol> does
6414     // not have a :lower16: or :upper16 as part of the expression.  We don't
6415     // want the behavior of silently truncating, which can be unexpected and
6416     // lead to bugs that are difficult to find since this is an easy mistake
6417     // to make.
6418     int i = (Operands[3]->isImm()) ? 3 : 4;
6419     ARMOperand &Op = static_cast<ARMOperand &>(*Operands[i]);
6420     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm());
6421     if (CE) break;
6422     const MCExpr *E = dyn_cast<MCExpr>(Op.getImm());
6423     if (!E) break;
6424     const ARMMCExpr *ARM16Expr = dyn_cast<ARMMCExpr>(E);
6425     if (!ARM16Expr || (ARM16Expr->getKind() != ARMMCExpr::VK_ARM_HI16 &&
6426                        ARM16Expr->getKind() != ARMMCExpr::VK_ARM_LO16))
6427       return Error(
6428           Op.getStartLoc(),
6429           "immediate expression for mov requires :lower16: or :upper16");
6430     break;
6431   }
6432   case ARM::HINT:
6433   case ARM::t2HINT: {
6434     if (hasRAS()) {
6435       // ESB is not predicable (pred must be AL)
6436       unsigned Imm8 = Inst.getOperand(0).getImm();
6437       unsigned Pred = Inst.getOperand(1).getImm();
6438       if (Imm8 == 0x10 && Pred != ARMCC::AL)
6439         return Error(Operands[1]->getStartLoc(), "instruction 'esb' is not "
6440                                                  "predicable, but condition "
6441                                                  "code specified");
6442     }
6443     // Without the RAS extension, this behaves as any other unallocated hint.
6444     break;
6445   }
6446   }
6447 
6448   return false;
6449 }
6450 
6451 static unsigned getRealVSTOpcode(unsigned Opc, unsigned &Spacing) {
6452   switch(Opc) {
6453   default: llvm_unreachable("unexpected opcode!");
6454   // VST1LN
6455   case ARM::VST1LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST1LNd8_UPD;
6456   case ARM::VST1LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST1LNd16_UPD;
6457   case ARM::VST1LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST1LNd32_UPD;
6458   case ARM::VST1LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST1LNd8_UPD;
6459   case ARM::VST1LNdWB_register_Asm_16: Spacing = 1; return ARM::VST1LNd16_UPD;
6460   case ARM::VST1LNdWB_register_Asm_32: Spacing = 1; return ARM::VST1LNd32_UPD;
6461   case ARM::VST1LNdAsm_8:  Spacing = 1; return ARM::VST1LNd8;
6462   case ARM::VST1LNdAsm_16: Spacing = 1; return ARM::VST1LNd16;
6463   case ARM::VST1LNdAsm_32: Spacing = 1; return ARM::VST1LNd32;
6464 
6465   // VST2LN
6466   case ARM::VST2LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST2LNd8_UPD;
6467   case ARM::VST2LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST2LNd16_UPD;
6468   case ARM::VST2LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST2LNd32_UPD;
6469   case ARM::VST2LNqWB_fixed_Asm_16: Spacing = 2; return ARM::VST2LNq16_UPD;
6470   case ARM::VST2LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST2LNq32_UPD;
6471 
6472   case ARM::VST2LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST2LNd8_UPD;
6473   case ARM::VST2LNdWB_register_Asm_16: Spacing = 1; return ARM::VST2LNd16_UPD;
6474   case ARM::VST2LNdWB_register_Asm_32: Spacing = 1; return ARM::VST2LNd32_UPD;
6475   case ARM::VST2LNqWB_register_Asm_16: Spacing = 2; return ARM::VST2LNq16_UPD;
6476   case ARM::VST2LNqWB_register_Asm_32: Spacing = 2; return ARM::VST2LNq32_UPD;
6477 
6478   case ARM::VST2LNdAsm_8:  Spacing = 1; return ARM::VST2LNd8;
6479   case ARM::VST2LNdAsm_16: Spacing = 1; return ARM::VST2LNd16;
6480   case ARM::VST2LNdAsm_32: Spacing = 1; return ARM::VST2LNd32;
6481   case ARM::VST2LNqAsm_16: Spacing = 2; return ARM::VST2LNq16;
6482   case ARM::VST2LNqAsm_32: Spacing = 2; return ARM::VST2LNq32;
6483 
6484   // VST3LN
6485   case ARM::VST3LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST3LNd8_UPD;
6486   case ARM::VST3LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST3LNd16_UPD;
6487   case ARM::VST3LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST3LNd32_UPD;
6488   case ARM::VST3LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VST3LNq16_UPD;
6489   case ARM::VST3LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST3LNq32_UPD;
6490   case ARM::VST3LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST3LNd8_UPD;
6491   case ARM::VST3LNdWB_register_Asm_16: Spacing = 1; return ARM::VST3LNd16_UPD;
6492   case ARM::VST3LNdWB_register_Asm_32: Spacing = 1; return ARM::VST3LNd32_UPD;
6493   case ARM::VST3LNqWB_register_Asm_16: Spacing = 2; return ARM::VST3LNq16_UPD;
6494   case ARM::VST3LNqWB_register_Asm_32: Spacing = 2; return ARM::VST3LNq32_UPD;
6495   case ARM::VST3LNdAsm_8:  Spacing = 1; return ARM::VST3LNd8;
6496   case ARM::VST3LNdAsm_16: Spacing = 1; return ARM::VST3LNd16;
6497   case ARM::VST3LNdAsm_32: Spacing = 1; return ARM::VST3LNd32;
6498   case ARM::VST3LNqAsm_16: Spacing = 2; return ARM::VST3LNq16;
6499   case ARM::VST3LNqAsm_32: Spacing = 2; return ARM::VST3LNq32;
6500 
6501   // VST3
6502   case ARM::VST3dWB_fixed_Asm_8:  Spacing = 1; return ARM::VST3d8_UPD;
6503   case ARM::VST3dWB_fixed_Asm_16: Spacing = 1; return ARM::VST3d16_UPD;
6504   case ARM::VST3dWB_fixed_Asm_32: Spacing = 1; return ARM::VST3d32_UPD;
6505   case ARM::VST3qWB_fixed_Asm_8:  Spacing = 2; return ARM::VST3q8_UPD;
6506   case ARM::VST3qWB_fixed_Asm_16: Spacing = 2; return ARM::VST3q16_UPD;
6507   case ARM::VST3qWB_fixed_Asm_32: Spacing = 2; return ARM::VST3q32_UPD;
6508   case ARM::VST3dWB_register_Asm_8:  Spacing = 1; return ARM::VST3d8_UPD;
6509   case ARM::VST3dWB_register_Asm_16: Spacing = 1; return ARM::VST3d16_UPD;
6510   case ARM::VST3dWB_register_Asm_32: Spacing = 1; return ARM::VST3d32_UPD;
6511   case ARM::VST3qWB_register_Asm_8:  Spacing = 2; return ARM::VST3q8_UPD;
6512   case ARM::VST3qWB_register_Asm_16: Spacing = 2; return ARM::VST3q16_UPD;
6513   case ARM::VST3qWB_register_Asm_32: Spacing = 2; return ARM::VST3q32_UPD;
6514   case ARM::VST3dAsm_8:  Spacing = 1; return ARM::VST3d8;
6515   case ARM::VST3dAsm_16: Spacing = 1; return ARM::VST3d16;
6516   case ARM::VST3dAsm_32: Spacing = 1; return ARM::VST3d32;
6517   case ARM::VST3qAsm_8:  Spacing = 2; return ARM::VST3q8;
6518   case ARM::VST3qAsm_16: Spacing = 2; return ARM::VST3q16;
6519   case ARM::VST3qAsm_32: Spacing = 2; return ARM::VST3q32;
6520 
6521   // VST4LN
6522   case ARM::VST4LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST4LNd8_UPD;
6523   case ARM::VST4LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST4LNd16_UPD;
6524   case ARM::VST4LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST4LNd32_UPD;
6525   case ARM::VST4LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VST4LNq16_UPD;
6526   case ARM::VST4LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST4LNq32_UPD;
6527   case ARM::VST4LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST4LNd8_UPD;
6528   case ARM::VST4LNdWB_register_Asm_16: Spacing = 1; return ARM::VST4LNd16_UPD;
6529   case ARM::VST4LNdWB_register_Asm_32: Spacing = 1; return ARM::VST4LNd32_UPD;
6530   case ARM::VST4LNqWB_register_Asm_16: Spacing = 2; return ARM::VST4LNq16_UPD;
6531   case ARM::VST4LNqWB_register_Asm_32: Spacing = 2; return ARM::VST4LNq32_UPD;
6532   case ARM::VST4LNdAsm_8:  Spacing = 1; return ARM::VST4LNd8;
6533   case ARM::VST4LNdAsm_16: Spacing = 1; return ARM::VST4LNd16;
6534   case ARM::VST4LNdAsm_32: Spacing = 1; return ARM::VST4LNd32;
6535   case ARM::VST4LNqAsm_16: Spacing = 2; return ARM::VST4LNq16;
6536   case ARM::VST4LNqAsm_32: Spacing = 2; return ARM::VST4LNq32;
6537 
6538   // VST4
6539   case ARM::VST4dWB_fixed_Asm_8:  Spacing = 1; return ARM::VST4d8_UPD;
6540   case ARM::VST4dWB_fixed_Asm_16: Spacing = 1; return ARM::VST4d16_UPD;
6541   case ARM::VST4dWB_fixed_Asm_32: Spacing = 1; return ARM::VST4d32_UPD;
6542   case ARM::VST4qWB_fixed_Asm_8:  Spacing = 2; return ARM::VST4q8_UPD;
6543   case ARM::VST4qWB_fixed_Asm_16: Spacing = 2; return ARM::VST4q16_UPD;
6544   case ARM::VST4qWB_fixed_Asm_32: Spacing = 2; return ARM::VST4q32_UPD;
6545   case ARM::VST4dWB_register_Asm_8:  Spacing = 1; return ARM::VST4d8_UPD;
6546   case ARM::VST4dWB_register_Asm_16: Spacing = 1; return ARM::VST4d16_UPD;
6547   case ARM::VST4dWB_register_Asm_32: Spacing = 1; return ARM::VST4d32_UPD;
6548   case ARM::VST4qWB_register_Asm_8:  Spacing = 2; return ARM::VST4q8_UPD;
6549   case ARM::VST4qWB_register_Asm_16: Spacing = 2; return ARM::VST4q16_UPD;
6550   case ARM::VST4qWB_register_Asm_32: Spacing = 2; return ARM::VST4q32_UPD;
6551   case ARM::VST4dAsm_8:  Spacing = 1; return ARM::VST4d8;
6552   case ARM::VST4dAsm_16: Spacing = 1; return ARM::VST4d16;
6553   case ARM::VST4dAsm_32: Spacing = 1; return ARM::VST4d32;
6554   case ARM::VST4qAsm_8:  Spacing = 2; return ARM::VST4q8;
6555   case ARM::VST4qAsm_16: Spacing = 2; return ARM::VST4q16;
6556   case ARM::VST4qAsm_32: Spacing = 2; return ARM::VST4q32;
6557   }
6558 }
6559 
6560 static unsigned getRealVLDOpcode(unsigned Opc, unsigned &Spacing) {
6561   switch(Opc) {
6562   default: llvm_unreachable("unexpected opcode!");
6563   // VLD1LN
6564   case ARM::VLD1LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD1LNd8_UPD;
6565   case ARM::VLD1LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD1LNd16_UPD;
6566   case ARM::VLD1LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD1LNd32_UPD;
6567   case ARM::VLD1LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD1LNd8_UPD;
6568   case ARM::VLD1LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD1LNd16_UPD;
6569   case ARM::VLD1LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD1LNd32_UPD;
6570   case ARM::VLD1LNdAsm_8:  Spacing = 1; return ARM::VLD1LNd8;
6571   case ARM::VLD1LNdAsm_16: Spacing = 1; return ARM::VLD1LNd16;
6572   case ARM::VLD1LNdAsm_32: Spacing = 1; return ARM::VLD1LNd32;
6573 
6574   // VLD2LN
6575   case ARM::VLD2LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD2LNd8_UPD;
6576   case ARM::VLD2LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD2LNd16_UPD;
6577   case ARM::VLD2LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD2LNd32_UPD;
6578   case ARM::VLD2LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD2LNq16_UPD;
6579   case ARM::VLD2LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD2LNq32_UPD;
6580   case ARM::VLD2LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD2LNd8_UPD;
6581   case ARM::VLD2LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD2LNd16_UPD;
6582   case ARM::VLD2LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD2LNd32_UPD;
6583   case ARM::VLD2LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD2LNq16_UPD;
6584   case ARM::VLD2LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD2LNq32_UPD;
6585   case ARM::VLD2LNdAsm_8:  Spacing = 1; return ARM::VLD2LNd8;
6586   case ARM::VLD2LNdAsm_16: Spacing = 1; return ARM::VLD2LNd16;
6587   case ARM::VLD2LNdAsm_32: Spacing = 1; return ARM::VLD2LNd32;
6588   case ARM::VLD2LNqAsm_16: Spacing = 2; return ARM::VLD2LNq16;
6589   case ARM::VLD2LNqAsm_32: Spacing = 2; return ARM::VLD2LNq32;
6590 
6591   // VLD3DUP
6592   case ARM::VLD3DUPdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3DUPd8_UPD;
6593   case ARM::VLD3DUPdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3DUPd16_UPD;
6594   case ARM::VLD3DUPdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3DUPd32_UPD;
6595   case ARM::VLD3DUPqWB_fixed_Asm_8: Spacing = 1; return ARM::VLD3DUPq8_UPD;
6596   case ARM::VLD3DUPqWB_fixed_Asm_16: Spacing = 2; return ARM::VLD3DUPq16_UPD;
6597   case ARM::VLD3DUPqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3DUPq32_UPD;
6598   case ARM::VLD3DUPdWB_register_Asm_8:  Spacing = 1; return ARM::VLD3DUPd8_UPD;
6599   case ARM::VLD3DUPdWB_register_Asm_16: Spacing = 1; return ARM::VLD3DUPd16_UPD;
6600   case ARM::VLD3DUPdWB_register_Asm_32: Spacing = 1; return ARM::VLD3DUPd32_UPD;
6601   case ARM::VLD3DUPqWB_register_Asm_8: Spacing = 2; return ARM::VLD3DUPq8_UPD;
6602   case ARM::VLD3DUPqWB_register_Asm_16: Spacing = 2; return ARM::VLD3DUPq16_UPD;
6603   case ARM::VLD3DUPqWB_register_Asm_32: Spacing = 2; return ARM::VLD3DUPq32_UPD;
6604   case ARM::VLD3DUPdAsm_8:  Spacing = 1; return ARM::VLD3DUPd8;
6605   case ARM::VLD3DUPdAsm_16: Spacing = 1; return ARM::VLD3DUPd16;
6606   case ARM::VLD3DUPdAsm_32: Spacing = 1; return ARM::VLD3DUPd32;
6607   case ARM::VLD3DUPqAsm_8: Spacing = 2; return ARM::VLD3DUPq8;
6608   case ARM::VLD3DUPqAsm_16: Spacing = 2; return ARM::VLD3DUPq16;
6609   case ARM::VLD3DUPqAsm_32: Spacing = 2; return ARM::VLD3DUPq32;
6610 
6611   // VLD3LN
6612   case ARM::VLD3LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3LNd8_UPD;
6613   case ARM::VLD3LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3LNd16_UPD;
6614   case ARM::VLD3LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3LNd32_UPD;
6615   case ARM::VLD3LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3LNq16_UPD;
6616   case ARM::VLD3LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3LNq32_UPD;
6617   case ARM::VLD3LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD3LNd8_UPD;
6618   case ARM::VLD3LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD3LNd16_UPD;
6619   case ARM::VLD3LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD3LNd32_UPD;
6620   case ARM::VLD3LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD3LNq16_UPD;
6621   case ARM::VLD3LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD3LNq32_UPD;
6622   case ARM::VLD3LNdAsm_8:  Spacing = 1; return ARM::VLD3LNd8;
6623   case ARM::VLD3LNdAsm_16: Spacing = 1; return ARM::VLD3LNd16;
6624   case ARM::VLD3LNdAsm_32: Spacing = 1; return ARM::VLD3LNd32;
6625   case ARM::VLD3LNqAsm_16: Spacing = 2; return ARM::VLD3LNq16;
6626   case ARM::VLD3LNqAsm_32: Spacing = 2; return ARM::VLD3LNq32;
6627 
6628   // VLD3
6629   case ARM::VLD3dWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3d8_UPD;
6630   case ARM::VLD3dWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3d16_UPD;
6631   case ARM::VLD3dWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3d32_UPD;
6632   case ARM::VLD3qWB_fixed_Asm_8:  Spacing = 2; return ARM::VLD3q8_UPD;
6633   case ARM::VLD3qWB_fixed_Asm_16: Spacing = 2; return ARM::VLD3q16_UPD;
6634   case ARM::VLD3qWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3q32_UPD;
6635   case ARM::VLD3dWB_register_Asm_8:  Spacing = 1; return ARM::VLD3d8_UPD;
6636   case ARM::VLD3dWB_register_Asm_16: Spacing = 1; return ARM::VLD3d16_UPD;
6637   case ARM::VLD3dWB_register_Asm_32: Spacing = 1; return ARM::VLD3d32_UPD;
6638   case ARM::VLD3qWB_register_Asm_8:  Spacing = 2; return ARM::VLD3q8_UPD;
6639   case ARM::VLD3qWB_register_Asm_16: Spacing = 2; return ARM::VLD3q16_UPD;
6640   case ARM::VLD3qWB_register_Asm_32: Spacing = 2; return ARM::VLD3q32_UPD;
6641   case ARM::VLD3dAsm_8:  Spacing = 1; return ARM::VLD3d8;
6642   case ARM::VLD3dAsm_16: Spacing = 1; return ARM::VLD3d16;
6643   case ARM::VLD3dAsm_32: Spacing = 1; return ARM::VLD3d32;
6644   case ARM::VLD3qAsm_8:  Spacing = 2; return ARM::VLD3q8;
6645   case ARM::VLD3qAsm_16: Spacing = 2; return ARM::VLD3q16;
6646   case ARM::VLD3qAsm_32: Spacing = 2; return ARM::VLD3q32;
6647 
6648   // VLD4LN
6649   case ARM::VLD4LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4LNd8_UPD;
6650   case ARM::VLD4LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4LNd16_UPD;
6651   case ARM::VLD4LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4LNd32_UPD;
6652   case ARM::VLD4LNqWB_fixed_Asm_16: Spacing = 2; return ARM::VLD4LNq16_UPD;
6653   case ARM::VLD4LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4LNq32_UPD;
6654   case ARM::VLD4LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD4LNd8_UPD;
6655   case ARM::VLD4LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD4LNd16_UPD;
6656   case ARM::VLD4LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD4LNd32_UPD;
6657   case ARM::VLD4LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD4LNq16_UPD;
6658   case ARM::VLD4LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD4LNq32_UPD;
6659   case ARM::VLD4LNdAsm_8:  Spacing = 1; return ARM::VLD4LNd8;
6660   case ARM::VLD4LNdAsm_16: Spacing = 1; return ARM::VLD4LNd16;
6661   case ARM::VLD4LNdAsm_32: Spacing = 1; return ARM::VLD4LNd32;
6662   case ARM::VLD4LNqAsm_16: Spacing = 2; return ARM::VLD4LNq16;
6663   case ARM::VLD4LNqAsm_32: Spacing = 2; return ARM::VLD4LNq32;
6664 
6665   // VLD4DUP
6666   case ARM::VLD4DUPdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4DUPd8_UPD;
6667   case ARM::VLD4DUPdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4DUPd16_UPD;
6668   case ARM::VLD4DUPdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4DUPd32_UPD;
6669   case ARM::VLD4DUPqWB_fixed_Asm_8: Spacing = 1; return ARM::VLD4DUPq8_UPD;
6670   case ARM::VLD4DUPqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4DUPq16_UPD;
6671   case ARM::VLD4DUPqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4DUPq32_UPD;
6672   case ARM::VLD4DUPdWB_register_Asm_8:  Spacing = 1; return ARM::VLD4DUPd8_UPD;
6673   case ARM::VLD4DUPdWB_register_Asm_16: Spacing = 1; return ARM::VLD4DUPd16_UPD;
6674   case ARM::VLD4DUPdWB_register_Asm_32: Spacing = 1; return ARM::VLD4DUPd32_UPD;
6675   case ARM::VLD4DUPqWB_register_Asm_8: Spacing = 2; return ARM::VLD4DUPq8_UPD;
6676   case ARM::VLD4DUPqWB_register_Asm_16: Spacing = 2; return ARM::VLD4DUPq16_UPD;
6677   case ARM::VLD4DUPqWB_register_Asm_32: Spacing = 2; return ARM::VLD4DUPq32_UPD;
6678   case ARM::VLD4DUPdAsm_8:  Spacing = 1; return ARM::VLD4DUPd8;
6679   case ARM::VLD4DUPdAsm_16: Spacing = 1; return ARM::VLD4DUPd16;
6680   case ARM::VLD4DUPdAsm_32: Spacing = 1; return ARM::VLD4DUPd32;
6681   case ARM::VLD4DUPqAsm_8: Spacing = 2; return ARM::VLD4DUPq8;
6682   case ARM::VLD4DUPqAsm_16: Spacing = 2; return ARM::VLD4DUPq16;
6683   case ARM::VLD4DUPqAsm_32: Spacing = 2; return ARM::VLD4DUPq32;
6684 
6685   // VLD4
6686   case ARM::VLD4dWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4d8_UPD;
6687   case ARM::VLD4dWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4d16_UPD;
6688   case ARM::VLD4dWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4d32_UPD;
6689   case ARM::VLD4qWB_fixed_Asm_8:  Spacing = 2; return ARM::VLD4q8_UPD;
6690   case ARM::VLD4qWB_fixed_Asm_16: Spacing = 2; return ARM::VLD4q16_UPD;
6691   case ARM::VLD4qWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4q32_UPD;
6692   case ARM::VLD4dWB_register_Asm_8:  Spacing = 1; return ARM::VLD4d8_UPD;
6693   case ARM::VLD4dWB_register_Asm_16: Spacing = 1; return ARM::VLD4d16_UPD;
6694   case ARM::VLD4dWB_register_Asm_32: Spacing = 1; return ARM::VLD4d32_UPD;
6695   case ARM::VLD4qWB_register_Asm_8:  Spacing = 2; return ARM::VLD4q8_UPD;
6696   case ARM::VLD4qWB_register_Asm_16: Spacing = 2; return ARM::VLD4q16_UPD;
6697   case ARM::VLD4qWB_register_Asm_32: Spacing = 2; return ARM::VLD4q32_UPD;
6698   case ARM::VLD4dAsm_8:  Spacing = 1; return ARM::VLD4d8;
6699   case ARM::VLD4dAsm_16: Spacing = 1; return ARM::VLD4d16;
6700   case ARM::VLD4dAsm_32: Spacing = 1; return ARM::VLD4d32;
6701   case ARM::VLD4qAsm_8:  Spacing = 2; return ARM::VLD4q8;
6702   case ARM::VLD4qAsm_16: Spacing = 2; return ARM::VLD4q16;
6703   case ARM::VLD4qAsm_32: Spacing = 2; return ARM::VLD4q32;
6704   }
6705 }
6706 
6707 bool ARMAsmParser::processInstruction(MCInst &Inst,
6708                                       const OperandVector &Operands,
6709                                       MCStreamer &Out) {
6710   // Check if we have the wide qualifier, because if it's present we
6711   // must avoid selecting a 16-bit thumb instruction.
6712   bool HasWideQualifier = false;
6713   for (auto &Op : Operands) {
6714     ARMOperand &ARMOp = static_cast<ARMOperand&>(*Op);
6715     if (ARMOp.isToken() && ARMOp.getToken() == ".w") {
6716       HasWideQualifier = true;
6717       break;
6718     }
6719   }
6720 
6721   switch (Inst.getOpcode()) {
6722   // Alias for alternate form of 'ldr{,b}t Rt, [Rn], #imm' instruction.
6723   case ARM::LDRT_POST:
6724   case ARM::LDRBT_POST: {
6725     const unsigned Opcode =
6726       (Inst.getOpcode() == ARM::LDRT_POST) ? ARM::LDRT_POST_IMM
6727                                            : ARM::LDRBT_POST_IMM;
6728     MCInst TmpInst;
6729     TmpInst.setOpcode(Opcode);
6730     TmpInst.addOperand(Inst.getOperand(0));
6731     TmpInst.addOperand(Inst.getOperand(1));
6732     TmpInst.addOperand(Inst.getOperand(1));
6733     TmpInst.addOperand(MCOperand::createReg(0));
6734     TmpInst.addOperand(MCOperand::createImm(0));
6735     TmpInst.addOperand(Inst.getOperand(2));
6736     TmpInst.addOperand(Inst.getOperand(3));
6737     Inst = TmpInst;
6738     return true;
6739   }
6740   // Alias for alternate form of 'str{,b}t Rt, [Rn], #imm' instruction.
6741   case ARM::STRT_POST:
6742   case ARM::STRBT_POST: {
6743     const unsigned Opcode =
6744       (Inst.getOpcode() == ARM::STRT_POST) ? ARM::STRT_POST_IMM
6745                                            : ARM::STRBT_POST_IMM;
6746     MCInst TmpInst;
6747     TmpInst.setOpcode(Opcode);
6748     TmpInst.addOperand(Inst.getOperand(1));
6749     TmpInst.addOperand(Inst.getOperand(0));
6750     TmpInst.addOperand(Inst.getOperand(1));
6751     TmpInst.addOperand(MCOperand::createReg(0));
6752     TmpInst.addOperand(MCOperand::createImm(0));
6753     TmpInst.addOperand(Inst.getOperand(2));
6754     TmpInst.addOperand(Inst.getOperand(3));
6755     Inst = TmpInst;
6756     return true;
6757   }
6758   // Alias for alternate form of 'ADR Rd, #imm' instruction.
6759   case ARM::ADDri: {
6760     if (Inst.getOperand(1).getReg() != ARM::PC ||
6761         Inst.getOperand(5).getReg() != 0 ||
6762         !(Inst.getOperand(2).isExpr() || Inst.getOperand(2).isImm()))
6763       return false;
6764     MCInst TmpInst;
6765     TmpInst.setOpcode(ARM::ADR);
6766     TmpInst.addOperand(Inst.getOperand(0));
6767     if (Inst.getOperand(2).isImm()) {
6768       // Immediate (mod_imm) will be in its encoded form, we must unencode it
6769       // before passing it to the ADR instruction.
6770       unsigned Enc = Inst.getOperand(2).getImm();
6771       TmpInst.addOperand(MCOperand::createImm(
6772         ARM_AM::rotr32(Enc & 0xFF, (Enc & 0xF00) >> 7)));
6773     } else {
6774       // Turn PC-relative expression into absolute expression.
6775       // Reading PC provides the start of the current instruction + 8 and
6776       // the transform to adr is biased by that.
6777       MCSymbol *Dot = getContext().createTempSymbol();
6778       Out.EmitLabel(Dot);
6779       const MCExpr *OpExpr = Inst.getOperand(2).getExpr();
6780       const MCExpr *InstPC = MCSymbolRefExpr::create(Dot,
6781                                                      MCSymbolRefExpr::VK_None,
6782                                                      getContext());
6783       const MCExpr *Const8 = MCConstantExpr::create(8, getContext());
6784       const MCExpr *ReadPC = MCBinaryExpr::createAdd(InstPC, Const8,
6785                                                      getContext());
6786       const MCExpr *FixupAddr = MCBinaryExpr::createAdd(ReadPC, OpExpr,
6787                                                         getContext());
6788       TmpInst.addOperand(MCOperand::createExpr(FixupAddr));
6789     }
6790     TmpInst.addOperand(Inst.getOperand(3));
6791     TmpInst.addOperand(Inst.getOperand(4));
6792     Inst = TmpInst;
6793     return true;
6794   }
6795   // Aliases for alternate PC+imm syntax of LDR instructions.
6796   case ARM::t2LDRpcrel:
6797     // Select the narrow version if the immediate will fit.
6798     if (Inst.getOperand(1).getImm() > 0 &&
6799         Inst.getOperand(1).getImm() <= 0xff &&
6800         !HasWideQualifier)
6801       Inst.setOpcode(ARM::tLDRpci);
6802     else
6803       Inst.setOpcode(ARM::t2LDRpci);
6804     return true;
6805   case ARM::t2LDRBpcrel:
6806     Inst.setOpcode(ARM::t2LDRBpci);
6807     return true;
6808   case ARM::t2LDRHpcrel:
6809     Inst.setOpcode(ARM::t2LDRHpci);
6810     return true;
6811   case ARM::t2LDRSBpcrel:
6812     Inst.setOpcode(ARM::t2LDRSBpci);
6813     return true;
6814   case ARM::t2LDRSHpcrel:
6815     Inst.setOpcode(ARM::t2LDRSHpci);
6816     return true;
6817   case ARM::LDRConstPool:
6818   case ARM::tLDRConstPool:
6819   case ARM::t2LDRConstPool: {
6820     // Pseudo instruction ldr rt, =immediate is converted to a
6821     // MOV rt, immediate if immediate is known and representable
6822     // otherwise we create a constant pool entry that we load from.
6823     MCInst TmpInst;
6824     if (Inst.getOpcode() == ARM::LDRConstPool)
6825       TmpInst.setOpcode(ARM::LDRi12);
6826     else if (Inst.getOpcode() == ARM::tLDRConstPool)
6827       TmpInst.setOpcode(ARM::tLDRpci);
6828     else if (Inst.getOpcode() == ARM::t2LDRConstPool)
6829       TmpInst.setOpcode(ARM::t2LDRpci);
6830     const ARMOperand &PoolOperand =
6831       (HasWideQualifier ?
6832        static_cast<ARMOperand &>(*Operands[4]) :
6833        static_cast<ARMOperand &>(*Operands[3]));
6834     const MCExpr *SubExprVal = PoolOperand.getConstantPoolImm();
6835     // If SubExprVal is a constant we may be able to use a MOV
6836     if (isa<MCConstantExpr>(SubExprVal) &&
6837         Inst.getOperand(0).getReg() != ARM::PC &&
6838         Inst.getOperand(0).getReg() != ARM::SP) {
6839       int64_t Value =
6840         (int64_t) (cast<MCConstantExpr>(SubExprVal))->getValue();
6841       bool UseMov  = true;
6842       bool MovHasS = true;
6843       if (Inst.getOpcode() == ARM::LDRConstPool) {
6844         // ARM Constant
6845         if (ARM_AM::getSOImmVal(Value) != -1) {
6846           Value = ARM_AM::getSOImmVal(Value);
6847           TmpInst.setOpcode(ARM::MOVi);
6848         }
6849         else if (ARM_AM::getSOImmVal(~Value) != -1) {
6850           Value = ARM_AM::getSOImmVal(~Value);
6851           TmpInst.setOpcode(ARM::MVNi);
6852         }
6853         else if (hasV6T2Ops() &&
6854                  Value >=0 && Value < 65536) {
6855           TmpInst.setOpcode(ARM::MOVi16);
6856           MovHasS = false;
6857         }
6858         else
6859           UseMov = false;
6860       }
6861       else {
6862         // Thumb/Thumb2 Constant
6863         if (hasThumb2() &&
6864             ARM_AM::getT2SOImmVal(Value) != -1)
6865           TmpInst.setOpcode(ARM::t2MOVi);
6866         else if (hasThumb2() &&
6867                  ARM_AM::getT2SOImmVal(~Value) != -1) {
6868           TmpInst.setOpcode(ARM::t2MVNi);
6869           Value = ~Value;
6870         }
6871         else if (hasV8MBaseline() &&
6872                  Value >=0 && Value < 65536) {
6873           TmpInst.setOpcode(ARM::t2MOVi16);
6874           MovHasS = false;
6875         }
6876         else
6877           UseMov = false;
6878       }
6879       if (UseMov) {
6880         TmpInst.addOperand(Inst.getOperand(0));           // Rt
6881         TmpInst.addOperand(MCOperand::createImm(Value));  // Immediate
6882         TmpInst.addOperand(Inst.getOperand(2));           // CondCode
6883         TmpInst.addOperand(Inst.getOperand(3));           // CondCode
6884         if (MovHasS)
6885           TmpInst.addOperand(MCOperand::createReg(0));    // S
6886         Inst = TmpInst;
6887         return true;
6888       }
6889     }
6890     // No opportunity to use MOV/MVN create constant pool
6891     const MCExpr *CPLoc =
6892       getTargetStreamer().addConstantPoolEntry(SubExprVal,
6893                                                PoolOperand.getStartLoc());
6894     TmpInst.addOperand(Inst.getOperand(0));           // Rt
6895     TmpInst.addOperand(MCOperand::createExpr(CPLoc)); // offset to constpool
6896     if (TmpInst.getOpcode() == ARM::LDRi12)
6897       TmpInst.addOperand(MCOperand::createImm(0));    // unused offset
6898     TmpInst.addOperand(Inst.getOperand(2));           // CondCode
6899     TmpInst.addOperand(Inst.getOperand(3));           // CondCode
6900     Inst = TmpInst;
6901     return true;
6902   }
6903   // Handle NEON VST complex aliases.
6904   case ARM::VST1LNdWB_register_Asm_8:
6905   case ARM::VST1LNdWB_register_Asm_16:
6906   case ARM::VST1LNdWB_register_Asm_32: {
6907     MCInst TmpInst;
6908     // Shuffle the operands around so the lane index operand is in the
6909     // right place.
6910     unsigned Spacing;
6911     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
6912     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
6913     TmpInst.addOperand(Inst.getOperand(2)); // Rn
6914     TmpInst.addOperand(Inst.getOperand(3)); // alignment
6915     TmpInst.addOperand(Inst.getOperand(4)); // Rm
6916     TmpInst.addOperand(Inst.getOperand(0)); // Vd
6917     TmpInst.addOperand(Inst.getOperand(1)); // lane
6918     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
6919     TmpInst.addOperand(Inst.getOperand(6));
6920     Inst = TmpInst;
6921     return true;
6922   }
6923 
6924   case ARM::VST2LNdWB_register_Asm_8:
6925   case ARM::VST2LNdWB_register_Asm_16:
6926   case ARM::VST2LNdWB_register_Asm_32:
6927   case ARM::VST2LNqWB_register_Asm_16:
6928   case ARM::VST2LNqWB_register_Asm_32: {
6929     MCInst TmpInst;
6930     // Shuffle the operands around so the lane index operand is in the
6931     // right place.
6932     unsigned Spacing;
6933     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
6934     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
6935     TmpInst.addOperand(Inst.getOperand(2)); // Rn
6936     TmpInst.addOperand(Inst.getOperand(3)); // alignment
6937     TmpInst.addOperand(Inst.getOperand(4)); // Rm
6938     TmpInst.addOperand(Inst.getOperand(0)); // Vd
6939     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6940                                             Spacing));
6941     TmpInst.addOperand(Inst.getOperand(1)); // lane
6942     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
6943     TmpInst.addOperand(Inst.getOperand(6));
6944     Inst = TmpInst;
6945     return true;
6946   }
6947 
6948   case ARM::VST3LNdWB_register_Asm_8:
6949   case ARM::VST3LNdWB_register_Asm_16:
6950   case ARM::VST3LNdWB_register_Asm_32:
6951   case ARM::VST3LNqWB_register_Asm_16:
6952   case ARM::VST3LNqWB_register_Asm_32: {
6953     MCInst TmpInst;
6954     // Shuffle the operands around so the lane index operand is in the
6955     // right place.
6956     unsigned Spacing;
6957     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
6958     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
6959     TmpInst.addOperand(Inst.getOperand(2)); // Rn
6960     TmpInst.addOperand(Inst.getOperand(3)); // alignment
6961     TmpInst.addOperand(Inst.getOperand(4)); // Rm
6962     TmpInst.addOperand(Inst.getOperand(0)); // Vd
6963     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6964                                             Spacing));
6965     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6966                                             Spacing * 2));
6967     TmpInst.addOperand(Inst.getOperand(1)); // lane
6968     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
6969     TmpInst.addOperand(Inst.getOperand(6));
6970     Inst = TmpInst;
6971     return true;
6972   }
6973 
6974   case ARM::VST4LNdWB_register_Asm_8:
6975   case ARM::VST4LNdWB_register_Asm_16:
6976   case ARM::VST4LNdWB_register_Asm_32:
6977   case ARM::VST4LNqWB_register_Asm_16:
6978   case ARM::VST4LNqWB_register_Asm_32: {
6979     MCInst TmpInst;
6980     // Shuffle the operands around so the lane index operand is in the
6981     // right place.
6982     unsigned Spacing;
6983     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
6984     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
6985     TmpInst.addOperand(Inst.getOperand(2)); // Rn
6986     TmpInst.addOperand(Inst.getOperand(3)); // alignment
6987     TmpInst.addOperand(Inst.getOperand(4)); // Rm
6988     TmpInst.addOperand(Inst.getOperand(0)); // Vd
6989     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6990                                             Spacing));
6991     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6992                                             Spacing * 2));
6993     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6994                                             Spacing * 3));
6995     TmpInst.addOperand(Inst.getOperand(1)); // lane
6996     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
6997     TmpInst.addOperand(Inst.getOperand(6));
6998     Inst = TmpInst;
6999     return true;
7000   }
7001 
7002   case ARM::VST1LNdWB_fixed_Asm_8:
7003   case ARM::VST1LNdWB_fixed_Asm_16:
7004   case ARM::VST1LNdWB_fixed_Asm_32: {
7005     MCInst TmpInst;
7006     // Shuffle the operands around so the lane index operand is in the
7007     // right place.
7008     unsigned Spacing;
7009     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7010     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7011     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7012     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7013     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7014     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7015     TmpInst.addOperand(Inst.getOperand(1)); // lane
7016     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7017     TmpInst.addOperand(Inst.getOperand(5));
7018     Inst = TmpInst;
7019     return true;
7020   }
7021 
7022   case ARM::VST2LNdWB_fixed_Asm_8:
7023   case ARM::VST2LNdWB_fixed_Asm_16:
7024   case ARM::VST2LNdWB_fixed_Asm_32:
7025   case ARM::VST2LNqWB_fixed_Asm_16:
7026   case ARM::VST2LNqWB_fixed_Asm_32: {
7027     MCInst TmpInst;
7028     // Shuffle the operands around so the lane index operand is in the
7029     // right place.
7030     unsigned Spacing;
7031     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7032     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7033     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7034     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7035     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7036     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7037     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7038                                             Spacing));
7039     TmpInst.addOperand(Inst.getOperand(1)); // lane
7040     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7041     TmpInst.addOperand(Inst.getOperand(5));
7042     Inst = TmpInst;
7043     return true;
7044   }
7045 
7046   case ARM::VST3LNdWB_fixed_Asm_8:
7047   case ARM::VST3LNdWB_fixed_Asm_16:
7048   case ARM::VST3LNdWB_fixed_Asm_32:
7049   case ARM::VST3LNqWB_fixed_Asm_16:
7050   case ARM::VST3LNqWB_fixed_Asm_32: {
7051     MCInst TmpInst;
7052     // Shuffle the operands around so the lane index operand is in the
7053     // right place.
7054     unsigned Spacing;
7055     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7056     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7057     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7058     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7059     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7060     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7061     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7062                                             Spacing));
7063     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7064                                             Spacing * 2));
7065     TmpInst.addOperand(Inst.getOperand(1)); // lane
7066     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7067     TmpInst.addOperand(Inst.getOperand(5));
7068     Inst = TmpInst;
7069     return true;
7070   }
7071 
7072   case ARM::VST4LNdWB_fixed_Asm_8:
7073   case ARM::VST4LNdWB_fixed_Asm_16:
7074   case ARM::VST4LNdWB_fixed_Asm_32:
7075   case ARM::VST4LNqWB_fixed_Asm_16:
7076   case ARM::VST4LNqWB_fixed_Asm_32: {
7077     MCInst TmpInst;
7078     // Shuffle the operands around so the lane index operand is in the
7079     // right place.
7080     unsigned Spacing;
7081     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7082     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7083     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7084     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7085     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7086     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7087     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7088                                             Spacing));
7089     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7090                                             Spacing * 2));
7091     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7092                                             Spacing * 3));
7093     TmpInst.addOperand(Inst.getOperand(1)); // lane
7094     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7095     TmpInst.addOperand(Inst.getOperand(5));
7096     Inst = TmpInst;
7097     return true;
7098   }
7099 
7100   case ARM::VST1LNdAsm_8:
7101   case ARM::VST1LNdAsm_16:
7102   case ARM::VST1LNdAsm_32: {
7103     MCInst TmpInst;
7104     // Shuffle the operands around so the lane index operand is in the
7105     // right place.
7106     unsigned Spacing;
7107     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7108     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7109     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7110     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7111     TmpInst.addOperand(Inst.getOperand(1)); // lane
7112     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7113     TmpInst.addOperand(Inst.getOperand(5));
7114     Inst = TmpInst;
7115     return true;
7116   }
7117 
7118   case ARM::VST2LNdAsm_8:
7119   case ARM::VST2LNdAsm_16:
7120   case ARM::VST2LNdAsm_32:
7121   case ARM::VST2LNqAsm_16:
7122   case ARM::VST2LNqAsm_32: {
7123     MCInst TmpInst;
7124     // Shuffle the operands around so the lane index operand is in the
7125     // right place.
7126     unsigned Spacing;
7127     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7128     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7129     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7130     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7131     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7132                                             Spacing));
7133     TmpInst.addOperand(Inst.getOperand(1)); // lane
7134     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7135     TmpInst.addOperand(Inst.getOperand(5));
7136     Inst = TmpInst;
7137     return true;
7138   }
7139 
7140   case ARM::VST3LNdAsm_8:
7141   case ARM::VST3LNdAsm_16:
7142   case ARM::VST3LNdAsm_32:
7143   case ARM::VST3LNqAsm_16:
7144   case ARM::VST3LNqAsm_32: {
7145     MCInst TmpInst;
7146     // Shuffle the operands around so the lane index operand is in the
7147     // right place.
7148     unsigned Spacing;
7149     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7150     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7151     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7152     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7153     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7154                                             Spacing));
7155     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7156                                             Spacing * 2));
7157     TmpInst.addOperand(Inst.getOperand(1)); // lane
7158     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7159     TmpInst.addOperand(Inst.getOperand(5));
7160     Inst = TmpInst;
7161     return true;
7162   }
7163 
7164   case ARM::VST4LNdAsm_8:
7165   case ARM::VST4LNdAsm_16:
7166   case ARM::VST4LNdAsm_32:
7167   case ARM::VST4LNqAsm_16:
7168   case ARM::VST4LNqAsm_32: {
7169     MCInst TmpInst;
7170     // Shuffle the operands around so the lane index operand is in the
7171     // right place.
7172     unsigned Spacing;
7173     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7174     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7175     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7176     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7177     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7178                                             Spacing));
7179     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7180                                             Spacing * 2));
7181     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7182                                             Spacing * 3));
7183     TmpInst.addOperand(Inst.getOperand(1)); // lane
7184     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7185     TmpInst.addOperand(Inst.getOperand(5));
7186     Inst = TmpInst;
7187     return true;
7188   }
7189 
7190   // Handle NEON VLD complex aliases.
7191   case ARM::VLD1LNdWB_register_Asm_8:
7192   case ARM::VLD1LNdWB_register_Asm_16:
7193   case ARM::VLD1LNdWB_register_Asm_32: {
7194     MCInst TmpInst;
7195     // Shuffle the operands around so the lane index operand is in the
7196     // right place.
7197     unsigned Spacing;
7198     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7199     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7200     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7201     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7202     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7203     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7204     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7205     TmpInst.addOperand(Inst.getOperand(1)); // lane
7206     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7207     TmpInst.addOperand(Inst.getOperand(6));
7208     Inst = TmpInst;
7209     return true;
7210   }
7211 
7212   case ARM::VLD2LNdWB_register_Asm_8:
7213   case ARM::VLD2LNdWB_register_Asm_16:
7214   case ARM::VLD2LNdWB_register_Asm_32:
7215   case ARM::VLD2LNqWB_register_Asm_16:
7216   case ARM::VLD2LNqWB_register_Asm_32: {
7217     MCInst TmpInst;
7218     // Shuffle the operands around so the lane index operand is in the
7219     // right place.
7220     unsigned Spacing;
7221     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7222     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7223     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7224                                             Spacing));
7225     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7226     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7227     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7228     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7229     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7230     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7231                                             Spacing));
7232     TmpInst.addOperand(Inst.getOperand(1)); // lane
7233     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7234     TmpInst.addOperand(Inst.getOperand(6));
7235     Inst = TmpInst;
7236     return true;
7237   }
7238 
7239   case ARM::VLD3LNdWB_register_Asm_8:
7240   case ARM::VLD3LNdWB_register_Asm_16:
7241   case ARM::VLD3LNdWB_register_Asm_32:
7242   case ARM::VLD3LNqWB_register_Asm_16:
7243   case ARM::VLD3LNqWB_register_Asm_32: {
7244     MCInst TmpInst;
7245     // Shuffle the operands around so the lane index operand is in the
7246     // right place.
7247     unsigned Spacing;
7248     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7249     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7250     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7251                                             Spacing));
7252     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7253                                             Spacing * 2));
7254     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7255     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7256     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7257     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7258     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7259     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7260                                             Spacing));
7261     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7262                                             Spacing * 2));
7263     TmpInst.addOperand(Inst.getOperand(1)); // lane
7264     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7265     TmpInst.addOperand(Inst.getOperand(6));
7266     Inst = TmpInst;
7267     return true;
7268   }
7269 
7270   case ARM::VLD4LNdWB_register_Asm_8:
7271   case ARM::VLD4LNdWB_register_Asm_16:
7272   case ARM::VLD4LNdWB_register_Asm_32:
7273   case ARM::VLD4LNqWB_register_Asm_16:
7274   case ARM::VLD4LNqWB_register_Asm_32: {
7275     MCInst TmpInst;
7276     // Shuffle the operands around so the lane index operand is in the
7277     // right place.
7278     unsigned Spacing;
7279     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7280     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7281     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7282                                             Spacing));
7283     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7284                                             Spacing * 2));
7285     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7286                                             Spacing * 3));
7287     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7288     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7289     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7290     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7291     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7292     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7293                                             Spacing));
7294     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7295                                             Spacing * 2));
7296     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7297                                             Spacing * 3));
7298     TmpInst.addOperand(Inst.getOperand(1)); // lane
7299     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7300     TmpInst.addOperand(Inst.getOperand(6));
7301     Inst = TmpInst;
7302     return true;
7303   }
7304 
7305   case ARM::VLD1LNdWB_fixed_Asm_8:
7306   case ARM::VLD1LNdWB_fixed_Asm_16:
7307   case ARM::VLD1LNdWB_fixed_Asm_32: {
7308     MCInst TmpInst;
7309     // Shuffle the operands around so the lane index operand is in the
7310     // right place.
7311     unsigned Spacing;
7312     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7313     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7314     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7315     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7316     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7317     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7318     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7319     TmpInst.addOperand(Inst.getOperand(1)); // lane
7320     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7321     TmpInst.addOperand(Inst.getOperand(5));
7322     Inst = TmpInst;
7323     return true;
7324   }
7325 
7326   case ARM::VLD2LNdWB_fixed_Asm_8:
7327   case ARM::VLD2LNdWB_fixed_Asm_16:
7328   case ARM::VLD2LNdWB_fixed_Asm_32:
7329   case ARM::VLD2LNqWB_fixed_Asm_16:
7330   case ARM::VLD2LNqWB_fixed_Asm_32: {
7331     MCInst TmpInst;
7332     // Shuffle the operands around so the lane index operand is in the
7333     // right place.
7334     unsigned Spacing;
7335     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7336     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7337     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7338                                             Spacing));
7339     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7340     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7341     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7342     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7343     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7344     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7345                                             Spacing));
7346     TmpInst.addOperand(Inst.getOperand(1)); // lane
7347     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7348     TmpInst.addOperand(Inst.getOperand(5));
7349     Inst = TmpInst;
7350     return true;
7351   }
7352 
7353   case ARM::VLD3LNdWB_fixed_Asm_8:
7354   case ARM::VLD3LNdWB_fixed_Asm_16:
7355   case ARM::VLD3LNdWB_fixed_Asm_32:
7356   case ARM::VLD3LNqWB_fixed_Asm_16:
7357   case ARM::VLD3LNqWB_fixed_Asm_32: {
7358     MCInst TmpInst;
7359     // Shuffle the operands around so the lane index operand is in the
7360     // right place.
7361     unsigned Spacing;
7362     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7363     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7364     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7365                                             Spacing));
7366     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7367                                             Spacing * 2));
7368     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7369     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7370     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7371     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7372     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7373     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7374                                             Spacing));
7375     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7376                                             Spacing * 2));
7377     TmpInst.addOperand(Inst.getOperand(1)); // lane
7378     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7379     TmpInst.addOperand(Inst.getOperand(5));
7380     Inst = TmpInst;
7381     return true;
7382   }
7383 
7384   case ARM::VLD4LNdWB_fixed_Asm_8:
7385   case ARM::VLD4LNdWB_fixed_Asm_16:
7386   case ARM::VLD4LNdWB_fixed_Asm_32:
7387   case ARM::VLD4LNqWB_fixed_Asm_16:
7388   case ARM::VLD4LNqWB_fixed_Asm_32: {
7389     MCInst TmpInst;
7390     // Shuffle the operands around so the lane index operand is in the
7391     // right place.
7392     unsigned Spacing;
7393     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7394     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7395     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7396                                             Spacing));
7397     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7398                                             Spacing * 2));
7399     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7400                                             Spacing * 3));
7401     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7402     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7403     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7404     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7405     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7406     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7407                                             Spacing));
7408     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7409                                             Spacing * 2));
7410     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7411                                             Spacing * 3));
7412     TmpInst.addOperand(Inst.getOperand(1)); // lane
7413     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7414     TmpInst.addOperand(Inst.getOperand(5));
7415     Inst = TmpInst;
7416     return true;
7417   }
7418 
7419   case ARM::VLD1LNdAsm_8:
7420   case ARM::VLD1LNdAsm_16:
7421   case ARM::VLD1LNdAsm_32: {
7422     MCInst TmpInst;
7423     // Shuffle the operands around so the lane index operand is in the
7424     // right place.
7425     unsigned Spacing;
7426     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7427     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7428     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7429     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7430     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7431     TmpInst.addOperand(Inst.getOperand(1)); // lane
7432     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7433     TmpInst.addOperand(Inst.getOperand(5));
7434     Inst = TmpInst;
7435     return true;
7436   }
7437 
7438   case ARM::VLD2LNdAsm_8:
7439   case ARM::VLD2LNdAsm_16:
7440   case ARM::VLD2LNdAsm_32:
7441   case ARM::VLD2LNqAsm_16:
7442   case ARM::VLD2LNqAsm_32: {
7443     MCInst TmpInst;
7444     // Shuffle the operands around so the lane index operand is in the
7445     // right place.
7446     unsigned Spacing;
7447     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7448     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7449     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7450                                             Spacing));
7451     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7452     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7453     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7454     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7455                                             Spacing));
7456     TmpInst.addOperand(Inst.getOperand(1)); // lane
7457     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7458     TmpInst.addOperand(Inst.getOperand(5));
7459     Inst = TmpInst;
7460     return true;
7461   }
7462 
7463   case ARM::VLD3LNdAsm_8:
7464   case ARM::VLD3LNdAsm_16:
7465   case ARM::VLD3LNdAsm_32:
7466   case ARM::VLD3LNqAsm_16:
7467   case ARM::VLD3LNqAsm_32: {
7468     MCInst TmpInst;
7469     // Shuffle the operands around so the lane index operand is in the
7470     // right place.
7471     unsigned Spacing;
7472     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7473     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7474     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7475                                             Spacing));
7476     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7477                                             Spacing * 2));
7478     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7479     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7480     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7481     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7482                                             Spacing));
7483     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7484                                             Spacing * 2));
7485     TmpInst.addOperand(Inst.getOperand(1)); // lane
7486     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7487     TmpInst.addOperand(Inst.getOperand(5));
7488     Inst = TmpInst;
7489     return true;
7490   }
7491 
7492   case ARM::VLD4LNdAsm_8:
7493   case ARM::VLD4LNdAsm_16:
7494   case ARM::VLD4LNdAsm_32:
7495   case ARM::VLD4LNqAsm_16:
7496   case ARM::VLD4LNqAsm_32: {
7497     MCInst TmpInst;
7498     // Shuffle the operands around so the lane index operand is in the
7499     // right place.
7500     unsigned Spacing;
7501     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7502     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7503     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7504                                             Spacing));
7505     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7506                                             Spacing * 2));
7507     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7508                                             Spacing * 3));
7509     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7510     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7511     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7512     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7513                                             Spacing));
7514     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7515                                             Spacing * 2));
7516     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7517                                             Spacing * 3));
7518     TmpInst.addOperand(Inst.getOperand(1)); // lane
7519     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7520     TmpInst.addOperand(Inst.getOperand(5));
7521     Inst = TmpInst;
7522     return true;
7523   }
7524 
7525   // VLD3DUP single 3-element structure to all lanes instructions.
7526   case ARM::VLD3DUPdAsm_8:
7527   case ARM::VLD3DUPdAsm_16:
7528   case ARM::VLD3DUPdAsm_32:
7529   case ARM::VLD3DUPqAsm_8:
7530   case ARM::VLD3DUPqAsm_16:
7531   case ARM::VLD3DUPqAsm_32: {
7532     MCInst TmpInst;
7533     unsigned Spacing;
7534     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7535     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7536     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7537                                             Spacing));
7538     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7539                                             Spacing * 2));
7540     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7541     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7542     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7543     TmpInst.addOperand(Inst.getOperand(4));
7544     Inst = TmpInst;
7545     return true;
7546   }
7547 
7548   case ARM::VLD3DUPdWB_fixed_Asm_8:
7549   case ARM::VLD3DUPdWB_fixed_Asm_16:
7550   case ARM::VLD3DUPdWB_fixed_Asm_32:
7551   case ARM::VLD3DUPqWB_fixed_Asm_8:
7552   case ARM::VLD3DUPqWB_fixed_Asm_16:
7553   case ARM::VLD3DUPqWB_fixed_Asm_32: {
7554     MCInst TmpInst;
7555     unsigned Spacing;
7556     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7557     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7558     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7559                                             Spacing));
7560     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7561                                             Spacing * 2));
7562     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7563     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7564     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7565     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7566     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7567     TmpInst.addOperand(Inst.getOperand(4));
7568     Inst = TmpInst;
7569     return true;
7570   }
7571 
7572   case ARM::VLD3DUPdWB_register_Asm_8:
7573   case ARM::VLD3DUPdWB_register_Asm_16:
7574   case ARM::VLD3DUPdWB_register_Asm_32:
7575   case ARM::VLD3DUPqWB_register_Asm_8:
7576   case ARM::VLD3DUPqWB_register_Asm_16:
7577   case ARM::VLD3DUPqWB_register_Asm_32: {
7578     MCInst TmpInst;
7579     unsigned Spacing;
7580     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7581     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7582     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7583                                             Spacing));
7584     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7585                                             Spacing * 2));
7586     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7587     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7588     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7589     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7590     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7591     TmpInst.addOperand(Inst.getOperand(5));
7592     Inst = TmpInst;
7593     return true;
7594   }
7595 
7596   // VLD3 multiple 3-element structure instructions.
7597   case ARM::VLD3dAsm_8:
7598   case ARM::VLD3dAsm_16:
7599   case ARM::VLD3dAsm_32:
7600   case ARM::VLD3qAsm_8:
7601   case ARM::VLD3qAsm_16:
7602   case ARM::VLD3qAsm_32: {
7603     MCInst TmpInst;
7604     unsigned Spacing;
7605     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7606     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7607     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7608                                             Spacing));
7609     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7610                                             Spacing * 2));
7611     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7612     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7613     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7614     TmpInst.addOperand(Inst.getOperand(4));
7615     Inst = TmpInst;
7616     return true;
7617   }
7618 
7619   case ARM::VLD3dWB_fixed_Asm_8:
7620   case ARM::VLD3dWB_fixed_Asm_16:
7621   case ARM::VLD3dWB_fixed_Asm_32:
7622   case ARM::VLD3qWB_fixed_Asm_8:
7623   case ARM::VLD3qWB_fixed_Asm_16:
7624   case ARM::VLD3qWB_fixed_Asm_32: {
7625     MCInst TmpInst;
7626     unsigned Spacing;
7627     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7628     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7629     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7630                                             Spacing));
7631     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7632                                             Spacing * 2));
7633     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7634     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7635     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7636     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7637     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7638     TmpInst.addOperand(Inst.getOperand(4));
7639     Inst = TmpInst;
7640     return true;
7641   }
7642 
7643   case ARM::VLD3dWB_register_Asm_8:
7644   case ARM::VLD3dWB_register_Asm_16:
7645   case ARM::VLD3dWB_register_Asm_32:
7646   case ARM::VLD3qWB_register_Asm_8:
7647   case ARM::VLD3qWB_register_Asm_16:
7648   case ARM::VLD3qWB_register_Asm_32: {
7649     MCInst TmpInst;
7650     unsigned Spacing;
7651     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7652     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7653     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7654                                             Spacing));
7655     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7656                                             Spacing * 2));
7657     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7658     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7659     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7660     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7661     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7662     TmpInst.addOperand(Inst.getOperand(5));
7663     Inst = TmpInst;
7664     return true;
7665   }
7666 
7667   // VLD4DUP single 3-element structure to all lanes instructions.
7668   case ARM::VLD4DUPdAsm_8:
7669   case ARM::VLD4DUPdAsm_16:
7670   case ARM::VLD4DUPdAsm_32:
7671   case ARM::VLD4DUPqAsm_8:
7672   case ARM::VLD4DUPqAsm_16:
7673   case ARM::VLD4DUPqAsm_32: {
7674     MCInst TmpInst;
7675     unsigned Spacing;
7676     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7677     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7678     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7679                                             Spacing));
7680     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7681                                             Spacing * 2));
7682     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7683                                             Spacing * 3));
7684     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7685     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7686     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7687     TmpInst.addOperand(Inst.getOperand(4));
7688     Inst = TmpInst;
7689     return true;
7690   }
7691 
7692   case ARM::VLD4DUPdWB_fixed_Asm_8:
7693   case ARM::VLD4DUPdWB_fixed_Asm_16:
7694   case ARM::VLD4DUPdWB_fixed_Asm_32:
7695   case ARM::VLD4DUPqWB_fixed_Asm_8:
7696   case ARM::VLD4DUPqWB_fixed_Asm_16:
7697   case ARM::VLD4DUPqWB_fixed_Asm_32: {
7698     MCInst TmpInst;
7699     unsigned Spacing;
7700     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7701     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7702     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7703                                             Spacing));
7704     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7705                                             Spacing * 2));
7706     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7707                                             Spacing * 3));
7708     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7709     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7710     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7711     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7712     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7713     TmpInst.addOperand(Inst.getOperand(4));
7714     Inst = TmpInst;
7715     return true;
7716   }
7717 
7718   case ARM::VLD4DUPdWB_register_Asm_8:
7719   case ARM::VLD4DUPdWB_register_Asm_16:
7720   case ARM::VLD4DUPdWB_register_Asm_32:
7721   case ARM::VLD4DUPqWB_register_Asm_8:
7722   case ARM::VLD4DUPqWB_register_Asm_16:
7723   case ARM::VLD4DUPqWB_register_Asm_32: {
7724     MCInst TmpInst;
7725     unsigned Spacing;
7726     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7727     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7728     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7729                                             Spacing));
7730     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7731                                             Spacing * 2));
7732     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7733                                             Spacing * 3));
7734     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7735     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7736     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7737     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7738     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7739     TmpInst.addOperand(Inst.getOperand(5));
7740     Inst = TmpInst;
7741     return true;
7742   }
7743 
7744   // VLD4 multiple 4-element structure instructions.
7745   case ARM::VLD4dAsm_8:
7746   case ARM::VLD4dAsm_16:
7747   case ARM::VLD4dAsm_32:
7748   case ARM::VLD4qAsm_8:
7749   case ARM::VLD4qAsm_16:
7750   case ARM::VLD4qAsm_32: {
7751     MCInst TmpInst;
7752     unsigned Spacing;
7753     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7754     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7755     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7756                                             Spacing));
7757     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7758                                             Spacing * 2));
7759     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7760                                             Spacing * 3));
7761     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7762     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7763     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7764     TmpInst.addOperand(Inst.getOperand(4));
7765     Inst = TmpInst;
7766     return true;
7767   }
7768 
7769   case ARM::VLD4dWB_fixed_Asm_8:
7770   case ARM::VLD4dWB_fixed_Asm_16:
7771   case ARM::VLD4dWB_fixed_Asm_32:
7772   case ARM::VLD4qWB_fixed_Asm_8:
7773   case ARM::VLD4qWB_fixed_Asm_16:
7774   case ARM::VLD4qWB_fixed_Asm_32: {
7775     MCInst TmpInst;
7776     unsigned Spacing;
7777     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7778     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7779     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7780                                             Spacing));
7781     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7782                                             Spacing * 2));
7783     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7784                                             Spacing * 3));
7785     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7786     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7787     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7788     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7789     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7790     TmpInst.addOperand(Inst.getOperand(4));
7791     Inst = TmpInst;
7792     return true;
7793   }
7794 
7795   case ARM::VLD4dWB_register_Asm_8:
7796   case ARM::VLD4dWB_register_Asm_16:
7797   case ARM::VLD4dWB_register_Asm_32:
7798   case ARM::VLD4qWB_register_Asm_8:
7799   case ARM::VLD4qWB_register_Asm_16:
7800   case ARM::VLD4qWB_register_Asm_32: {
7801     MCInst TmpInst;
7802     unsigned Spacing;
7803     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7804     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7805     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7806                                             Spacing));
7807     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7808                                             Spacing * 2));
7809     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7810                                             Spacing * 3));
7811     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7812     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7813     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7814     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7815     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7816     TmpInst.addOperand(Inst.getOperand(5));
7817     Inst = TmpInst;
7818     return true;
7819   }
7820 
7821   // VST3 multiple 3-element structure instructions.
7822   case ARM::VST3dAsm_8:
7823   case ARM::VST3dAsm_16:
7824   case ARM::VST3dAsm_32:
7825   case ARM::VST3qAsm_8:
7826   case ARM::VST3qAsm_16:
7827   case ARM::VST3qAsm_32: {
7828     MCInst TmpInst;
7829     unsigned Spacing;
7830     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7831     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7832     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7833     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7834     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7835                                             Spacing));
7836     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7837                                             Spacing * 2));
7838     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7839     TmpInst.addOperand(Inst.getOperand(4));
7840     Inst = TmpInst;
7841     return true;
7842   }
7843 
7844   case ARM::VST3dWB_fixed_Asm_8:
7845   case ARM::VST3dWB_fixed_Asm_16:
7846   case ARM::VST3dWB_fixed_Asm_32:
7847   case ARM::VST3qWB_fixed_Asm_8:
7848   case ARM::VST3qWB_fixed_Asm_16:
7849   case ARM::VST3qWB_fixed_Asm_32: {
7850     MCInst TmpInst;
7851     unsigned Spacing;
7852     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7853     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7854     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7855     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7856     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7857     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7858     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7859                                             Spacing));
7860     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7861                                             Spacing * 2));
7862     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7863     TmpInst.addOperand(Inst.getOperand(4));
7864     Inst = TmpInst;
7865     return true;
7866   }
7867 
7868   case ARM::VST3dWB_register_Asm_8:
7869   case ARM::VST3dWB_register_Asm_16:
7870   case ARM::VST3dWB_register_Asm_32:
7871   case ARM::VST3qWB_register_Asm_8:
7872   case ARM::VST3qWB_register_Asm_16:
7873   case ARM::VST3qWB_register_Asm_32: {
7874     MCInst TmpInst;
7875     unsigned Spacing;
7876     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7877     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7878     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7879     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7880     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7881     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7882     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7883                                             Spacing));
7884     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7885                                             Spacing * 2));
7886     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7887     TmpInst.addOperand(Inst.getOperand(5));
7888     Inst = TmpInst;
7889     return true;
7890   }
7891 
7892   // VST4 multiple 3-element structure instructions.
7893   case ARM::VST4dAsm_8:
7894   case ARM::VST4dAsm_16:
7895   case ARM::VST4dAsm_32:
7896   case ARM::VST4qAsm_8:
7897   case ARM::VST4qAsm_16:
7898   case ARM::VST4qAsm_32: {
7899     MCInst TmpInst;
7900     unsigned Spacing;
7901     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7902     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7903     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7904     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7905     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7906                                             Spacing));
7907     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7908                                             Spacing * 2));
7909     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7910                                             Spacing * 3));
7911     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7912     TmpInst.addOperand(Inst.getOperand(4));
7913     Inst = TmpInst;
7914     return true;
7915   }
7916 
7917   case ARM::VST4dWB_fixed_Asm_8:
7918   case ARM::VST4dWB_fixed_Asm_16:
7919   case ARM::VST4dWB_fixed_Asm_32:
7920   case ARM::VST4qWB_fixed_Asm_8:
7921   case ARM::VST4qWB_fixed_Asm_16:
7922   case ARM::VST4qWB_fixed_Asm_32: {
7923     MCInst TmpInst;
7924     unsigned Spacing;
7925     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7926     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7927     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7928     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7929     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7930     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7931     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7932                                             Spacing));
7933     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7934                                             Spacing * 2));
7935     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7936                                             Spacing * 3));
7937     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7938     TmpInst.addOperand(Inst.getOperand(4));
7939     Inst = TmpInst;
7940     return true;
7941   }
7942 
7943   case ARM::VST4dWB_register_Asm_8:
7944   case ARM::VST4dWB_register_Asm_16:
7945   case ARM::VST4dWB_register_Asm_32:
7946   case ARM::VST4qWB_register_Asm_8:
7947   case ARM::VST4qWB_register_Asm_16:
7948   case ARM::VST4qWB_register_Asm_32: {
7949     MCInst TmpInst;
7950     unsigned Spacing;
7951     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7952     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7953     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7954     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7955     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7956     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7957     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7958                                             Spacing));
7959     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7960                                             Spacing * 2));
7961     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7962                                             Spacing * 3));
7963     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7964     TmpInst.addOperand(Inst.getOperand(5));
7965     Inst = TmpInst;
7966     return true;
7967   }
7968 
7969   // Handle encoding choice for the shift-immediate instructions.
7970   case ARM::t2LSLri:
7971   case ARM::t2LSRri:
7972   case ARM::t2ASRri: {
7973     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
7974         isARMLowRegister(Inst.getOperand(1).getReg()) &&
7975         Inst.getOperand(5).getReg() == (inITBlock() ? 0 : ARM::CPSR) &&
7976         !HasWideQualifier) {
7977       unsigned NewOpc;
7978       switch (Inst.getOpcode()) {
7979       default: llvm_unreachable("unexpected opcode");
7980       case ARM::t2LSLri: NewOpc = ARM::tLSLri; break;
7981       case ARM::t2LSRri: NewOpc = ARM::tLSRri; break;
7982       case ARM::t2ASRri: NewOpc = ARM::tASRri; break;
7983       }
7984       // The Thumb1 operands aren't in the same order. Awesome, eh?
7985       MCInst TmpInst;
7986       TmpInst.setOpcode(NewOpc);
7987       TmpInst.addOperand(Inst.getOperand(0));
7988       TmpInst.addOperand(Inst.getOperand(5));
7989       TmpInst.addOperand(Inst.getOperand(1));
7990       TmpInst.addOperand(Inst.getOperand(2));
7991       TmpInst.addOperand(Inst.getOperand(3));
7992       TmpInst.addOperand(Inst.getOperand(4));
7993       Inst = TmpInst;
7994       return true;
7995     }
7996     return false;
7997   }
7998 
7999   // Handle the Thumb2 mode MOV complex aliases.
8000   case ARM::t2MOVsr:
8001   case ARM::t2MOVSsr: {
8002     // Which instruction to expand to depends on the CCOut operand and
8003     // whether we're in an IT block if the register operands are low
8004     // registers.
8005     bool isNarrow = false;
8006     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8007         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8008         isARMLowRegister(Inst.getOperand(2).getReg()) &&
8009         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
8010         inITBlock() == (Inst.getOpcode() == ARM::t2MOVsr) &&
8011         !HasWideQualifier)
8012       isNarrow = true;
8013     MCInst TmpInst;
8014     unsigned newOpc;
8015     switch(ARM_AM::getSORegShOp(Inst.getOperand(3).getImm())) {
8016     default: llvm_unreachable("unexpected opcode!");
8017     case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRrr : ARM::t2ASRrr; break;
8018     case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRrr : ARM::t2LSRrr; break;
8019     case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLrr : ARM::t2LSLrr; break;
8020     case ARM_AM::ror: newOpc = isNarrow ? ARM::tROR   : ARM::t2RORrr; break;
8021     }
8022     TmpInst.setOpcode(newOpc);
8023     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8024     if (isNarrow)
8025       TmpInst.addOperand(MCOperand::createReg(
8026           Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
8027     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8028     TmpInst.addOperand(Inst.getOperand(2)); // Rm
8029     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
8030     TmpInst.addOperand(Inst.getOperand(5));
8031     if (!isNarrow)
8032       TmpInst.addOperand(MCOperand::createReg(
8033           Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
8034     Inst = TmpInst;
8035     return true;
8036   }
8037   case ARM::t2MOVsi:
8038   case ARM::t2MOVSsi: {
8039     // Which instruction to expand to depends on the CCOut operand and
8040     // whether we're in an IT block if the register operands are low
8041     // registers.
8042     bool isNarrow = false;
8043     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8044         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8045         inITBlock() == (Inst.getOpcode() == ARM::t2MOVsi) &&
8046         !HasWideQualifier)
8047       isNarrow = true;
8048     MCInst TmpInst;
8049     unsigned newOpc;
8050     unsigned Shift = ARM_AM::getSORegShOp(Inst.getOperand(2).getImm());
8051     unsigned Amount = ARM_AM::getSORegOffset(Inst.getOperand(2).getImm());
8052     bool isMov = false;
8053     // MOV rd, rm, LSL #0 is actually a MOV instruction
8054     if (Shift == ARM_AM::lsl && Amount == 0) {
8055       isMov = true;
8056       // The 16-bit encoding of MOV rd, rm, LSL #N is explicitly encoding T2 of
8057       // MOV (register) in the ARMv8-A and ARMv8-M manuals, and immediate 0 is
8058       // unpredictable in an IT block so the 32-bit encoding T3 has to be used
8059       // instead.
8060       if (inITBlock()) {
8061         isNarrow = false;
8062       }
8063       newOpc = isNarrow ? ARM::tMOVSr : ARM::t2MOVr;
8064     } else {
8065       switch(Shift) {
8066       default: llvm_unreachable("unexpected opcode!");
8067       case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRri : ARM::t2ASRri; break;
8068       case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRri : ARM::t2LSRri; break;
8069       case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLri : ARM::t2LSLri; break;
8070       case ARM_AM::ror: newOpc = ARM::t2RORri; isNarrow = false; break;
8071       case ARM_AM::rrx: isNarrow = false; newOpc = ARM::t2RRX; break;
8072       }
8073     }
8074     if (Amount == 32) Amount = 0;
8075     TmpInst.setOpcode(newOpc);
8076     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8077     if (isNarrow && !isMov)
8078       TmpInst.addOperand(MCOperand::createReg(
8079           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
8080     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8081     if (newOpc != ARM::t2RRX && !isMov)
8082       TmpInst.addOperand(MCOperand::createImm(Amount));
8083     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8084     TmpInst.addOperand(Inst.getOperand(4));
8085     if (!isNarrow)
8086       TmpInst.addOperand(MCOperand::createReg(
8087           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
8088     Inst = TmpInst;
8089     return true;
8090   }
8091   // Handle the ARM mode MOV complex aliases.
8092   case ARM::ASRr:
8093   case ARM::LSRr:
8094   case ARM::LSLr:
8095   case ARM::RORr: {
8096     ARM_AM::ShiftOpc ShiftTy;
8097     switch(Inst.getOpcode()) {
8098     default: llvm_unreachable("unexpected opcode!");
8099     case ARM::ASRr: ShiftTy = ARM_AM::asr; break;
8100     case ARM::LSRr: ShiftTy = ARM_AM::lsr; break;
8101     case ARM::LSLr: ShiftTy = ARM_AM::lsl; break;
8102     case ARM::RORr: ShiftTy = ARM_AM::ror; break;
8103     }
8104     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, 0);
8105     MCInst TmpInst;
8106     TmpInst.setOpcode(ARM::MOVsr);
8107     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8108     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8109     TmpInst.addOperand(Inst.getOperand(2)); // Rm
8110     TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8111     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8112     TmpInst.addOperand(Inst.getOperand(4));
8113     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
8114     Inst = TmpInst;
8115     return true;
8116   }
8117   case ARM::ASRi:
8118   case ARM::LSRi:
8119   case ARM::LSLi:
8120   case ARM::RORi: {
8121     ARM_AM::ShiftOpc ShiftTy;
8122     switch(Inst.getOpcode()) {
8123     default: llvm_unreachable("unexpected opcode!");
8124     case ARM::ASRi: ShiftTy = ARM_AM::asr; break;
8125     case ARM::LSRi: ShiftTy = ARM_AM::lsr; break;
8126     case ARM::LSLi: ShiftTy = ARM_AM::lsl; break;
8127     case ARM::RORi: ShiftTy = ARM_AM::ror; break;
8128     }
8129     // A shift by zero is a plain MOVr, not a MOVsi.
8130     unsigned Amt = Inst.getOperand(2).getImm();
8131     unsigned Opc = Amt == 0 ? ARM::MOVr : ARM::MOVsi;
8132     // A shift by 32 should be encoded as 0 when permitted
8133     if (Amt == 32 && (ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr))
8134       Amt = 0;
8135     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, Amt);
8136     MCInst TmpInst;
8137     TmpInst.setOpcode(Opc);
8138     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8139     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8140     if (Opc == ARM::MOVsi)
8141       TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8142     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8143     TmpInst.addOperand(Inst.getOperand(4));
8144     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
8145     Inst = TmpInst;
8146     return true;
8147   }
8148   case ARM::RRXi: {
8149     unsigned Shifter = ARM_AM::getSORegOpc(ARM_AM::rrx, 0);
8150     MCInst TmpInst;
8151     TmpInst.setOpcode(ARM::MOVsi);
8152     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8153     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8154     TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8155     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8156     TmpInst.addOperand(Inst.getOperand(3));
8157     TmpInst.addOperand(Inst.getOperand(4)); // cc_out
8158     Inst = TmpInst;
8159     return true;
8160   }
8161   case ARM::t2LDMIA_UPD: {
8162     // If this is a load of a single register, then we should use
8163     // a post-indexed LDR instruction instead, per the ARM ARM.
8164     if (Inst.getNumOperands() != 5)
8165       return false;
8166     MCInst TmpInst;
8167     TmpInst.setOpcode(ARM::t2LDR_POST);
8168     TmpInst.addOperand(Inst.getOperand(4)); // Rt
8169     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8170     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8171     TmpInst.addOperand(MCOperand::createImm(4));
8172     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8173     TmpInst.addOperand(Inst.getOperand(3));
8174     Inst = TmpInst;
8175     return true;
8176   }
8177   case ARM::t2STMDB_UPD: {
8178     // If this is a store of a single register, then we should use
8179     // a pre-indexed STR instruction instead, per the ARM ARM.
8180     if (Inst.getNumOperands() != 5)
8181       return false;
8182     MCInst TmpInst;
8183     TmpInst.setOpcode(ARM::t2STR_PRE);
8184     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8185     TmpInst.addOperand(Inst.getOperand(4)); // Rt
8186     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8187     TmpInst.addOperand(MCOperand::createImm(-4));
8188     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8189     TmpInst.addOperand(Inst.getOperand(3));
8190     Inst = TmpInst;
8191     return true;
8192   }
8193   case ARM::LDMIA_UPD:
8194     // If this is a load of a single register via a 'pop', then we should use
8195     // a post-indexed LDR instruction instead, per the ARM ARM.
8196     if (static_cast<ARMOperand &>(*Operands[0]).getToken() == "pop" &&
8197         Inst.getNumOperands() == 5) {
8198       MCInst TmpInst;
8199       TmpInst.setOpcode(ARM::LDR_POST_IMM);
8200       TmpInst.addOperand(Inst.getOperand(4)); // Rt
8201       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8202       TmpInst.addOperand(Inst.getOperand(1)); // Rn
8203       TmpInst.addOperand(MCOperand::createReg(0));  // am2offset
8204       TmpInst.addOperand(MCOperand::createImm(4));
8205       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8206       TmpInst.addOperand(Inst.getOperand(3));
8207       Inst = TmpInst;
8208       return true;
8209     }
8210     break;
8211   case ARM::STMDB_UPD:
8212     // If this is a store of a single register via a 'push', then we should use
8213     // a pre-indexed STR instruction instead, per the ARM ARM.
8214     if (static_cast<ARMOperand &>(*Operands[0]).getToken() == "push" &&
8215         Inst.getNumOperands() == 5) {
8216       MCInst TmpInst;
8217       TmpInst.setOpcode(ARM::STR_PRE_IMM);
8218       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8219       TmpInst.addOperand(Inst.getOperand(4)); // Rt
8220       TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
8221       TmpInst.addOperand(MCOperand::createImm(-4));
8222       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8223       TmpInst.addOperand(Inst.getOperand(3));
8224       Inst = TmpInst;
8225     }
8226     break;
8227   case ARM::t2ADDri12:
8228     // If the immediate fits for encoding T3 (t2ADDri) and the generic "add"
8229     // mnemonic was used (not "addw"), encoding T3 is preferred.
8230     if (static_cast<ARMOperand &>(*Operands[0]).getToken() != "add" ||
8231         ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
8232       break;
8233     Inst.setOpcode(ARM::t2ADDri);
8234     Inst.addOperand(MCOperand::createReg(0)); // cc_out
8235     break;
8236   case ARM::t2SUBri12:
8237     // If the immediate fits for encoding T3 (t2SUBri) and the generic "sub"
8238     // mnemonic was used (not "subw"), encoding T3 is preferred.
8239     if (static_cast<ARMOperand &>(*Operands[0]).getToken() != "sub" ||
8240         ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
8241       break;
8242     Inst.setOpcode(ARM::t2SUBri);
8243     Inst.addOperand(MCOperand::createReg(0)); // cc_out
8244     break;
8245   case ARM::tADDi8:
8246     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
8247     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
8248     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
8249     // to encoding T1 if <Rd> is omitted."
8250     if ((unsigned)Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
8251       Inst.setOpcode(ARM::tADDi3);
8252       return true;
8253     }
8254     break;
8255   case ARM::tSUBi8:
8256     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
8257     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
8258     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
8259     // to encoding T1 if <Rd> is omitted."
8260     if ((unsigned)Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
8261       Inst.setOpcode(ARM::tSUBi3);
8262       return true;
8263     }
8264     break;
8265   case ARM::t2ADDri:
8266   case ARM::t2SUBri: {
8267     // If the destination and first source operand are the same, and
8268     // the flags are compatible with the current IT status, use encoding T2
8269     // instead of T3. For compatibility with the system 'as'. Make sure the
8270     // wide encoding wasn't explicit.
8271     if (Inst.getOperand(0).getReg() != Inst.getOperand(1).getReg() ||
8272         !isARMLowRegister(Inst.getOperand(0).getReg()) ||
8273         (Inst.getOperand(2).isImm() &&
8274          (unsigned)Inst.getOperand(2).getImm() > 255) ||
8275         Inst.getOperand(5).getReg() != (inITBlock() ? 0 : ARM::CPSR) ||
8276         HasWideQualifier)
8277       break;
8278     MCInst TmpInst;
8279     TmpInst.setOpcode(Inst.getOpcode() == ARM::t2ADDri ?
8280                       ARM::tADDi8 : ARM::tSUBi8);
8281     TmpInst.addOperand(Inst.getOperand(0));
8282     TmpInst.addOperand(Inst.getOperand(5));
8283     TmpInst.addOperand(Inst.getOperand(0));
8284     TmpInst.addOperand(Inst.getOperand(2));
8285     TmpInst.addOperand(Inst.getOperand(3));
8286     TmpInst.addOperand(Inst.getOperand(4));
8287     Inst = TmpInst;
8288     return true;
8289   }
8290   case ARM::t2ADDrr: {
8291     // If the destination and first source operand are the same, and
8292     // there's no setting of the flags, use encoding T2 instead of T3.
8293     // Note that this is only for ADD, not SUB. This mirrors the system
8294     // 'as' behaviour.  Also take advantage of ADD being commutative.
8295     // Make sure the wide encoding wasn't explicit.
8296     bool Swap = false;
8297     auto DestReg = Inst.getOperand(0).getReg();
8298     bool Transform = DestReg == Inst.getOperand(1).getReg();
8299     if (!Transform && DestReg == Inst.getOperand(2).getReg()) {
8300       Transform = true;
8301       Swap = true;
8302     }
8303     if (!Transform ||
8304         Inst.getOperand(5).getReg() != 0 ||
8305         HasWideQualifier)
8306       break;
8307     MCInst TmpInst;
8308     TmpInst.setOpcode(ARM::tADDhirr);
8309     TmpInst.addOperand(Inst.getOperand(0));
8310     TmpInst.addOperand(Inst.getOperand(0));
8311     TmpInst.addOperand(Inst.getOperand(Swap ? 1 : 2));
8312     TmpInst.addOperand(Inst.getOperand(3));
8313     TmpInst.addOperand(Inst.getOperand(4));
8314     Inst = TmpInst;
8315     return true;
8316   }
8317   case ARM::tADDrSP: {
8318     // If the non-SP source operand and the destination operand are not the
8319     // same, we need to use the 32-bit encoding if it's available.
8320     if (Inst.getOperand(0).getReg() != Inst.getOperand(2).getReg()) {
8321       Inst.setOpcode(ARM::t2ADDrr);
8322       Inst.addOperand(MCOperand::createReg(0)); // cc_out
8323       return true;
8324     }
8325     break;
8326   }
8327   case ARM::tB:
8328     // A Thumb conditional branch outside of an IT block is a tBcc.
8329     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()) {
8330       Inst.setOpcode(ARM::tBcc);
8331       return true;
8332     }
8333     break;
8334   case ARM::t2B:
8335     // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
8336     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()){
8337       Inst.setOpcode(ARM::t2Bcc);
8338       return true;
8339     }
8340     break;
8341   case ARM::t2Bcc:
8342     // If the conditional is AL or we're in an IT block, we really want t2B.
8343     if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock()) {
8344       Inst.setOpcode(ARM::t2B);
8345       return true;
8346     }
8347     break;
8348   case ARM::tBcc:
8349     // If the conditional is AL, we really want tB.
8350     if (Inst.getOperand(1).getImm() == ARMCC::AL) {
8351       Inst.setOpcode(ARM::tB);
8352       return true;
8353     }
8354     break;
8355   case ARM::tLDMIA: {
8356     // If the register list contains any high registers, or if the writeback
8357     // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
8358     // instead if we're in Thumb2. Otherwise, this should have generated
8359     // an error in validateInstruction().
8360     unsigned Rn = Inst.getOperand(0).getReg();
8361     bool hasWritebackToken =
8362         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8363          static_cast<ARMOperand &>(*Operands[3]).getToken() == "!");
8364     bool listContainsBase;
8365     if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
8366         (!listContainsBase && !hasWritebackToken) ||
8367         (listContainsBase && hasWritebackToken)) {
8368       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
8369       assert (isThumbTwo());
8370       Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
8371       // If we're switching to the updating version, we need to insert
8372       // the writeback tied operand.
8373       if (hasWritebackToken)
8374         Inst.insert(Inst.begin(),
8375                     MCOperand::createReg(Inst.getOperand(0).getReg()));
8376       return true;
8377     }
8378     break;
8379   }
8380   case ARM::tSTMIA_UPD: {
8381     // If the register list contains any high registers, we need to use
8382     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
8383     // should have generated an error in validateInstruction().
8384     unsigned Rn = Inst.getOperand(0).getReg();
8385     bool listContainsBase;
8386     if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
8387       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
8388       assert (isThumbTwo());
8389       Inst.setOpcode(ARM::t2STMIA_UPD);
8390       return true;
8391     }
8392     break;
8393   }
8394   case ARM::tPOP: {
8395     bool listContainsBase;
8396     // If the register list contains any high registers, we need to use
8397     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
8398     // should have generated an error in validateInstruction().
8399     if (!checkLowRegisterList(Inst, 2, 0, ARM::PC, listContainsBase))
8400       return false;
8401     assert (isThumbTwo());
8402     Inst.setOpcode(ARM::t2LDMIA_UPD);
8403     // Add the base register and writeback operands.
8404     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8405     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8406     return true;
8407   }
8408   case ARM::tPUSH: {
8409     bool listContainsBase;
8410     if (!checkLowRegisterList(Inst, 2, 0, ARM::LR, listContainsBase))
8411       return false;
8412     assert (isThumbTwo());
8413     Inst.setOpcode(ARM::t2STMDB_UPD);
8414     // Add the base register and writeback operands.
8415     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8416     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8417     return true;
8418   }
8419   case ARM::t2MOVi: {
8420     // If we can use the 16-bit encoding and the user didn't explicitly
8421     // request the 32-bit variant, transform it here.
8422     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8423         (Inst.getOperand(1).isImm() &&
8424          (unsigned)Inst.getOperand(1).getImm() <= 255) &&
8425         Inst.getOperand(4).getReg() == (inITBlock() ? 0 : ARM::CPSR) &&
8426         !HasWideQualifier) {
8427       // The operands aren't in the same order for tMOVi8...
8428       MCInst TmpInst;
8429       TmpInst.setOpcode(ARM::tMOVi8);
8430       TmpInst.addOperand(Inst.getOperand(0));
8431       TmpInst.addOperand(Inst.getOperand(4));
8432       TmpInst.addOperand(Inst.getOperand(1));
8433       TmpInst.addOperand(Inst.getOperand(2));
8434       TmpInst.addOperand(Inst.getOperand(3));
8435       Inst = TmpInst;
8436       return true;
8437     }
8438     break;
8439   }
8440   case ARM::t2MOVr: {
8441     // If we can use the 16-bit encoding and the user didn't explicitly
8442     // request the 32-bit variant, transform it here.
8443     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8444         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8445         Inst.getOperand(2).getImm() == ARMCC::AL &&
8446         Inst.getOperand(4).getReg() == ARM::CPSR &&
8447         !HasWideQualifier) {
8448       // The operands aren't the same for tMOV[S]r... (no cc_out)
8449       MCInst TmpInst;
8450       TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
8451       TmpInst.addOperand(Inst.getOperand(0));
8452       TmpInst.addOperand(Inst.getOperand(1));
8453       TmpInst.addOperand(Inst.getOperand(2));
8454       TmpInst.addOperand(Inst.getOperand(3));
8455       Inst = TmpInst;
8456       return true;
8457     }
8458     break;
8459   }
8460   case ARM::t2SXTH:
8461   case ARM::t2SXTB:
8462   case ARM::t2UXTH:
8463   case ARM::t2UXTB: {
8464     // If we can use the 16-bit encoding and the user didn't explicitly
8465     // request the 32-bit variant, transform it here.
8466     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8467         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8468         Inst.getOperand(2).getImm() == 0 &&
8469         !HasWideQualifier) {
8470       unsigned NewOpc;
8471       switch (Inst.getOpcode()) {
8472       default: llvm_unreachable("Illegal opcode!");
8473       case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
8474       case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
8475       case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
8476       case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
8477       }
8478       // The operands aren't the same for thumb1 (no rotate operand).
8479       MCInst TmpInst;
8480       TmpInst.setOpcode(NewOpc);
8481       TmpInst.addOperand(Inst.getOperand(0));
8482       TmpInst.addOperand(Inst.getOperand(1));
8483       TmpInst.addOperand(Inst.getOperand(3));
8484       TmpInst.addOperand(Inst.getOperand(4));
8485       Inst = TmpInst;
8486       return true;
8487     }
8488     break;
8489   }
8490   case ARM::MOVsi: {
8491     ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(2).getImm());
8492     // rrx shifts and asr/lsr of #32 is encoded as 0
8493     if (SOpc == ARM_AM::rrx || SOpc == ARM_AM::asr || SOpc == ARM_AM::lsr)
8494       return false;
8495     if (ARM_AM::getSORegOffset(Inst.getOperand(2).getImm()) == 0) {
8496       // Shifting by zero is accepted as a vanilla 'MOVr'
8497       MCInst TmpInst;
8498       TmpInst.setOpcode(ARM::MOVr);
8499       TmpInst.addOperand(Inst.getOperand(0));
8500       TmpInst.addOperand(Inst.getOperand(1));
8501       TmpInst.addOperand(Inst.getOperand(3));
8502       TmpInst.addOperand(Inst.getOperand(4));
8503       TmpInst.addOperand(Inst.getOperand(5));
8504       Inst = TmpInst;
8505       return true;
8506     }
8507     return false;
8508   }
8509   case ARM::ANDrsi:
8510   case ARM::ORRrsi:
8511   case ARM::EORrsi:
8512   case ARM::BICrsi:
8513   case ARM::SUBrsi:
8514   case ARM::ADDrsi: {
8515     unsigned newOpc;
8516     ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(3).getImm());
8517     if (SOpc == ARM_AM::rrx) return false;
8518     switch (Inst.getOpcode()) {
8519     default: llvm_unreachable("unexpected opcode!");
8520     case ARM::ANDrsi: newOpc = ARM::ANDrr; break;
8521     case ARM::ORRrsi: newOpc = ARM::ORRrr; break;
8522     case ARM::EORrsi: newOpc = ARM::EORrr; break;
8523     case ARM::BICrsi: newOpc = ARM::BICrr; break;
8524     case ARM::SUBrsi: newOpc = ARM::SUBrr; break;
8525     case ARM::ADDrsi: newOpc = ARM::ADDrr; break;
8526     }
8527     // If the shift is by zero, use the non-shifted instruction definition.
8528     // The exception is for right shifts, where 0 == 32
8529     if (ARM_AM::getSORegOffset(Inst.getOperand(3).getImm()) == 0 &&
8530         !(SOpc == ARM_AM::lsr || SOpc == ARM_AM::asr)) {
8531       MCInst TmpInst;
8532       TmpInst.setOpcode(newOpc);
8533       TmpInst.addOperand(Inst.getOperand(0));
8534       TmpInst.addOperand(Inst.getOperand(1));
8535       TmpInst.addOperand(Inst.getOperand(2));
8536       TmpInst.addOperand(Inst.getOperand(4));
8537       TmpInst.addOperand(Inst.getOperand(5));
8538       TmpInst.addOperand(Inst.getOperand(6));
8539       Inst = TmpInst;
8540       return true;
8541     }
8542     return false;
8543   }
8544   case ARM::ITasm:
8545   case ARM::t2IT: {
8546     MCOperand &MO = Inst.getOperand(1);
8547     unsigned Mask = MO.getImm();
8548     ARMCC::CondCodes Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
8549 
8550     // Set up the IT block state according to the IT instruction we just
8551     // matched.
8552     assert(!inITBlock() && "nested IT blocks?!");
8553     startExplicitITBlock(Cond, Mask);
8554     MO.setImm(getITMaskEncoding());
8555     break;
8556   }
8557   case ARM::t2LSLrr:
8558   case ARM::t2LSRrr:
8559   case ARM::t2ASRrr:
8560   case ARM::t2SBCrr:
8561   case ARM::t2RORrr:
8562   case ARM::t2BICrr:
8563   {
8564     // Assemblers should use the narrow encodings of these instructions when permissible.
8565     if ((isARMLowRegister(Inst.getOperand(1).getReg()) &&
8566          isARMLowRegister(Inst.getOperand(2).getReg())) &&
8567         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
8568         Inst.getOperand(5).getReg() == (inITBlock() ? 0 : ARM::CPSR) &&
8569         !HasWideQualifier) {
8570       unsigned NewOpc;
8571       switch (Inst.getOpcode()) {
8572         default: llvm_unreachable("unexpected opcode");
8573         case ARM::t2LSLrr: NewOpc = ARM::tLSLrr; break;
8574         case ARM::t2LSRrr: NewOpc = ARM::tLSRrr; break;
8575         case ARM::t2ASRrr: NewOpc = ARM::tASRrr; break;
8576         case ARM::t2SBCrr: NewOpc = ARM::tSBC; break;
8577         case ARM::t2RORrr: NewOpc = ARM::tROR; break;
8578         case ARM::t2BICrr: NewOpc = ARM::tBIC; break;
8579       }
8580       MCInst TmpInst;
8581       TmpInst.setOpcode(NewOpc);
8582       TmpInst.addOperand(Inst.getOperand(0));
8583       TmpInst.addOperand(Inst.getOperand(5));
8584       TmpInst.addOperand(Inst.getOperand(1));
8585       TmpInst.addOperand(Inst.getOperand(2));
8586       TmpInst.addOperand(Inst.getOperand(3));
8587       TmpInst.addOperand(Inst.getOperand(4));
8588       Inst = TmpInst;
8589       return true;
8590     }
8591     return false;
8592   }
8593   case ARM::t2ANDrr:
8594   case ARM::t2EORrr:
8595   case ARM::t2ADCrr:
8596   case ARM::t2ORRrr:
8597   {
8598     // Assemblers should use the narrow encodings of these instructions when permissible.
8599     // These instructions are special in that they are commutable, so shorter encodings
8600     // are available more often.
8601     if ((isARMLowRegister(Inst.getOperand(1).getReg()) &&
8602          isARMLowRegister(Inst.getOperand(2).getReg())) &&
8603         (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() ||
8604          Inst.getOperand(0).getReg() == Inst.getOperand(2).getReg()) &&
8605         Inst.getOperand(5).getReg() == (inITBlock() ? 0 : ARM::CPSR) &&
8606         !HasWideQualifier) {
8607       unsigned NewOpc;
8608       switch (Inst.getOpcode()) {
8609         default: llvm_unreachable("unexpected opcode");
8610         case ARM::t2ADCrr: NewOpc = ARM::tADC; break;
8611         case ARM::t2ANDrr: NewOpc = ARM::tAND; break;
8612         case ARM::t2EORrr: NewOpc = ARM::tEOR; break;
8613         case ARM::t2ORRrr: NewOpc = ARM::tORR; break;
8614       }
8615       MCInst TmpInst;
8616       TmpInst.setOpcode(NewOpc);
8617       TmpInst.addOperand(Inst.getOperand(0));
8618       TmpInst.addOperand(Inst.getOperand(5));
8619       if (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()) {
8620         TmpInst.addOperand(Inst.getOperand(1));
8621         TmpInst.addOperand(Inst.getOperand(2));
8622       } else {
8623         TmpInst.addOperand(Inst.getOperand(2));
8624         TmpInst.addOperand(Inst.getOperand(1));
8625       }
8626       TmpInst.addOperand(Inst.getOperand(3));
8627       TmpInst.addOperand(Inst.getOperand(4));
8628       Inst = TmpInst;
8629       return true;
8630     }
8631     return false;
8632   }
8633   }
8634   return false;
8635 }
8636 
8637 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
8638   // 16-bit thumb arithmetic instructions either require or preclude the 'S'
8639   // suffix depending on whether they're in an IT block or not.
8640   unsigned Opc = Inst.getOpcode();
8641   const MCInstrDesc &MCID = MII.get(Opc);
8642   if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
8643     assert(MCID.hasOptionalDef() &&
8644            "optionally flag setting instruction missing optional def operand");
8645     assert(MCID.NumOperands == Inst.getNumOperands() &&
8646            "operand count mismatch!");
8647     // Find the optional-def operand (cc_out).
8648     unsigned OpNo;
8649     for (OpNo = 0;
8650          !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
8651          ++OpNo)
8652       ;
8653     // If we're parsing Thumb1, reject it completely.
8654     if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
8655       return Match_RequiresFlagSetting;
8656     // If we're parsing Thumb2, which form is legal depends on whether we're
8657     // in an IT block.
8658     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
8659         !inITBlock())
8660       return Match_RequiresITBlock;
8661     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
8662         inITBlock())
8663       return Match_RequiresNotITBlock;
8664     // LSL with zero immediate is not allowed in an IT block
8665     if (Opc == ARM::tLSLri && Inst.getOperand(3).getImm() == 0 && inITBlock())
8666       return Match_RequiresNotITBlock;
8667   } else if (isThumbOne()) {
8668     // Some high-register supporting Thumb1 encodings only allow both registers
8669     // to be from r0-r7 when in Thumb2.
8670     if (Opc == ARM::tADDhirr && !hasV6MOps() &&
8671         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8672         isARMLowRegister(Inst.getOperand(2).getReg()))
8673       return Match_RequiresThumb2;
8674     // Others only require ARMv6 or later.
8675     else if (Opc == ARM::tMOVr && !hasV6Ops() &&
8676              isARMLowRegister(Inst.getOperand(0).getReg()) &&
8677              isARMLowRegister(Inst.getOperand(1).getReg()))
8678       return Match_RequiresV6;
8679   }
8680 
8681   // Before ARMv8 the rules for when SP is allowed in t2MOVr are more complex
8682   // than the loop below can handle, so it uses the GPRnopc register class and
8683   // we do SP handling here.
8684   if (Opc == ARM::t2MOVr && !hasV8Ops())
8685   {
8686     // SP as both source and destination is not allowed
8687     if (Inst.getOperand(0).getReg() == ARM::SP &&
8688         Inst.getOperand(1).getReg() == ARM::SP)
8689       return Match_RequiresV8;
8690     // When flags-setting SP as either source or destination is not allowed
8691     if (Inst.getOperand(4).getReg() == ARM::CPSR &&
8692         (Inst.getOperand(0).getReg() == ARM::SP ||
8693          Inst.getOperand(1).getReg() == ARM::SP))
8694       return Match_RequiresV8;
8695   }
8696 
8697   for (unsigned I = 0; I < MCID.NumOperands; ++I)
8698     if (MCID.OpInfo[I].RegClass == ARM::rGPRRegClassID) {
8699       // rGPRRegClass excludes PC, and also excluded SP before ARMv8
8700       if ((Inst.getOperand(I).getReg() == ARM::SP) && !hasV8Ops())
8701         return Match_RequiresV8;
8702       else if (Inst.getOperand(I).getReg() == ARM::PC)
8703         return Match_InvalidOperand;
8704     }
8705 
8706   return Match_Success;
8707 }
8708 
8709 namespace llvm {
8710 template <> inline bool IsCPSRDead<MCInst>(const MCInst *Instr) {
8711   return true; // In an assembly source, no need to second-guess
8712 }
8713 }
8714 
8715 // Returns true if Inst is unpredictable if it is in and IT block, but is not
8716 // the last instruction in the block.
8717 bool ARMAsmParser::isITBlockTerminator(MCInst &Inst) const {
8718   const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
8719 
8720   // All branch & call instructions terminate IT blocks.
8721   if (MCID.isTerminator() || MCID.isCall() || MCID.isReturn() ||
8722       MCID.isBranch() || MCID.isIndirectBranch())
8723     return true;
8724 
8725   // Any arithmetic instruction which writes to the PC also terminates the IT
8726   // block.
8727   for (unsigned OpIdx = 0; OpIdx < MCID.getNumDefs(); ++OpIdx) {
8728     MCOperand &Op = Inst.getOperand(OpIdx);
8729     if (Op.isReg() && Op.getReg() == ARM::PC)
8730       return true;
8731   }
8732 
8733   if (MCID.hasImplicitDefOfPhysReg(ARM::PC, MRI))
8734     return true;
8735 
8736   // Instructions with variable operand lists, which write to the variable
8737   // operands. We only care about Thumb instructions here, as ARM instructions
8738   // obviously can't be in an IT block.
8739   switch (Inst.getOpcode()) {
8740   case ARM::tLDMIA:
8741   case ARM::t2LDMIA:
8742   case ARM::t2LDMIA_UPD:
8743   case ARM::t2LDMDB:
8744   case ARM::t2LDMDB_UPD:
8745     if (listContainsReg(Inst, 3, ARM::PC))
8746       return true;
8747     break;
8748   case ARM::tPOP:
8749     if (listContainsReg(Inst, 2, ARM::PC))
8750       return true;
8751     break;
8752   }
8753 
8754   return false;
8755 }
8756 
8757 unsigned ARMAsmParser::MatchInstruction(OperandVector &Operands, MCInst &Inst,
8758                                           uint64_t &ErrorInfo,
8759                                           bool MatchingInlineAsm,
8760                                           bool &EmitInITBlock,
8761                                           MCStreamer &Out) {
8762   // If we can't use an implicit IT block here, just match as normal.
8763   if (inExplicitITBlock() || !isThumbTwo() || !useImplicitITThumb())
8764     return MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
8765 
8766   // Try to match the instruction in an extension of the current IT block (if
8767   // there is one).
8768   if (inImplicitITBlock()) {
8769     extendImplicitITBlock(ITState.Cond);
8770     if (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm) ==
8771             Match_Success) {
8772       // The match succeded, but we still have to check that the instruction is
8773       // valid in this implicit IT block.
8774       const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
8775       if (MCID.isPredicable()) {
8776         ARMCC::CondCodes InstCond =
8777             (ARMCC::CondCodes)Inst.getOperand(MCID.findFirstPredOperandIdx())
8778                 .getImm();
8779         ARMCC::CondCodes ITCond = currentITCond();
8780         if (InstCond == ITCond) {
8781           EmitInITBlock = true;
8782           return Match_Success;
8783         } else if (InstCond == ARMCC::getOppositeCondition(ITCond)) {
8784           invertCurrentITCondition();
8785           EmitInITBlock = true;
8786           return Match_Success;
8787         }
8788       }
8789     }
8790     rewindImplicitITPosition();
8791   }
8792 
8793   // Finish the current IT block, and try to match outside any IT block.
8794   flushPendingInstructions(Out);
8795   unsigned PlainMatchResult =
8796       MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
8797   if (PlainMatchResult == Match_Success) {
8798     const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
8799     if (MCID.isPredicable()) {
8800       ARMCC::CondCodes InstCond =
8801           (ARMCC::CondCodes)Inst.getOperand(MCID.findFirstPredOperandIdx())
8802               .getImm();
8803       // Some forms of the branch instruction have their own condition code
8804       // fields, so can be conditionally executed without an IT block.
8805       if (Inst.getOpcode() == ARM::tBcc || Inst.getOpcode() == ARM::t2Bcc) {
8806         EmitInITBlock = false;
8807         return Match_Success;
8808       }
8809       if (InstCond == ARMCC::AL) {
8810         EmitInITBlock = false;
8811         return Match_Success;
8812       }
8813     } else {
8814       EmitInITBlock = false;
8815       return Match_Success;
8816     }
8817   }
8818 
8819   // Try to match in a new IT block. The matcher doesn't check the actual
8820   // condition, so we create an IT block with a dummy condition, and fix it up
8821   // once we know the actual condition.
8822   startImplicitITBlock();
8823   if (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm) ==
8824       Match_Success) {
8825     const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
8826     if (MCID.isPredicable()) {
8827       ITState.Cond =
8828           (ARMCC::CondCodes)Inst.getOperand(MCID.findFirstPredOperandIdx())
8829               .getImm();
8830       EmitInITBlock = true;
8831       return Match_Success;
8832     }
8833   }
8834   discardImplicitITBlock();
8835 
8836   // If none of these succeed, return the error we got when trying to match
8837   // outside any IT blocks.
8838   EmitInITBlock = false;
8839   return PlainMatchResult;
8840 }
8841 
8842 std::string ARMMnemonicSpellCheck(StringRef S, uint64_t FBS);
8843 
8844 static const char *getSubtargetFeatureName(uint64_t Val);
8845 bool ARMAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
8846                                            OperandVector &Operands,
8847                                            MCStreamer &Out, uint64_t &ErrorInfo,
8848                                            bool MatchingInlineAsm) {
8849   MCInst Inst;
8850   unsigned MatchResult;
8851   bool PendConditionalInstruction = false;
8852 
8853   MatchResult = MatchInstruction(Operands, Inst, ErrorInfo, MatchingInlineAsm,
8854                                  PendConditionalInstruction, Out);
8855 
8856   SMLoc ErrorLoc;
8857   if (ErrorInfo < Operands.size()) {
8858     ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
8859     if (ErrorLoc == SMLoc())
8860       ErrorLoc = IDLoc;
8861   }
8862 
8863   switch (MatchResult) {
8864   case Match_Success:
8865     // Context sensitive operand constraints aren't handled by the matcher,
8866     // so check them here.
8867     if (validateInstruction(Inst, Operands)) {
8868       // Still progress the IT block, otherwise one wrong condition causes
8869       // nasty cascading errors.
8870       forwardITPosition();
8871       return true;
8872     }
8873 
8874     { // processInstruction() updates inITBlock state, we need to save it away
8875       bool wasInITBlock = inITBlock();
8876 
8877       // Some instructions need post-processing to, for example, tweak which
8878       // encoding is selected. Loop on it while changes happen so the
8879       // individual transformations can chain off each other. E.g.,
8880       // tPOP(r8)->t2LDMIA_UPD(sp,r8)->t2STR_POST(sp,r8)
8881       while (processInstruction(Inst, Operands, Out))
8882         ;
8883 
8884       // Only after the instruction is fully processed, we can validate it
8885       if (wasInITBlock && hasV8Ops() && isThumb() &&
8886           !isV8EligibleForIT(&Inst)) {
8887         Warning(IDLoc, "deprecated instruction in IT block");
8888       }
8889     }
8890 
8891     // Only move forward at the very end so that everything in validate
8892     // and process gets a consistent answer about whether we're in an IT
8893     // block.
8894     forwardITPosition();
8895 
8896     // ITasm is an ARM mode pseudo-instruction that just sets the ITblock and
8897     // doesn't actually encode.
8898     if (Inst.getOpcode() == ARM::ITasm)
8899       return false;
8900 
8901     Inst.setLoc(IDLoc);
8902     if (PendConditionalInstruction) {
8903       PendingConditionalInsts.push_back(Inst);
8904       if (isITBlockFull() || isITBlockTerminator(Inst))
8905         flushPendingInstructions(Out);
8906     } else {
8907       Out.EmitInstruction(Inst, getSTI());
8908     }
8909     return false;
8910   case Match_MissingFeature: {
8911     assert(ErrorInfo && "Unknown missing feature!");
8912     // Special case the error message for the very common case where only
8913     // a single subtarget feature is missing (Thumb vs. ARM, e.g.).
8914     std::string Msg = "instruction requires:";
8915     uint64_t Mask = 1;
8916     for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
8917       if (ErrorInfo & Mask) {
8918         Msg += " ";
8919         Msg += getSubtargetFeatureName(ErrorInfo & Mask);
8920       }
8921       Mask <<= 1;
8922     }
8923     return Error(IDLoc, Msg);
8924   }
8925   case Match_InvalidOperand: {
8926     SMLoc ErrorLoc = IDLoc;
8927     if (ErrorInfo != ~0ULL) {
8928       if (ErrorInfo >= Operands.size())
8929         return Error(IDLoc, "too few operands for instruction");
8930 
8931       ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
8932       if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
8933     }
8934 
8935     return Error(ErrorLoc, "invalid operand for instruction");
8936   }
8937   case Match_MnemonicFail: {
8938     uint64_t FBS = ComputeAvailableFeatures(getSTI().getFeatureBits());
8939     std::string Suggestion = ARMMnemonicSpellCheck(
8940       ((ARMOperand &)*Operands[0]).getToken(), FBS);
8941     return Error(IDLoc, "invalid instruction" + Suggestion,
8942                  ((ARMOperand &)*Operands[0]).getLocRange());
8943   }
8944   case Match_RequiresNotITBlock:
8945     return Error(IDLoc, "flag setting instruction only valid outside IT block");
8946   case Match_RequiresITBlock:
8947     return Error(IDLoc, "instruction only valid inside IT block");
8948   case Match_RequiresV6:
8949     return Error(IDLoc, "instruction variant requires ARMv6 or later");
8950   case Match_RequiresThumb2:
8951     return Error(IDLoc, "instruction variant requires Thumb2");
8952   case Match_RequiresV8:
8953     return Error(IDLoc, "instruction variant requires ARMv8 or later");
8954   case Match_RequiresFlagSetting:
8955     return Error(IDLoc, "no flag-preserving variant of this instruction available");
8956   case Match_ImmRange0_1:
8957     return Error(ErrorLoc, "immediate operand must be in the range [0,1]");
8958   case Match_ImmRange0_3:
8959     return Error(ErrorLoc, "immediate operand must be in the range [0,3]");
8960   case Match_ImmRange0_7:
8961     return Error(ErrorLoc, "immediate operand must be in the range [0,7]");
8962   case Match_ImmRange0_15:
8963     return Error(ErrorLoc, "immediate operand must be in the range [0,15]");
8964   case Match_ImmRange0_31:
8965     return Error(ErrorLoc, "immediate operand must be in the range [0,31]");
8966   case Match_ImmRange0_32:
8967     return Error(ErrorLoc, "immediate operand must be in the range [0,32]");
8968   case Match_ImmRange0_63:
8969     return Error(ErrorLoc, "immediate operand must be in the range [0,63]");
8970   case Match_ImmRange0_239:
8971     return Error(ErrorLoc, "immediate operand must be in the range [0,239]");
8972   case Match_ImmRange0_255:
8973     return Error(ErrorLoc, "immediate operand must be in the range [0,255]");
8974   case Match_ImmRange0_4095:
8975     return Error(ErrorLoc, "immediate operand must be in the range [0,4095]");
8976   case Match_ImmRange0_65535:
8977     return Error(ErrorLoc, "immediate operand must be in the range [0,65535]");
8978   case Match_ImmRange1_7:
8979     return Error(ErrorLoc, "immediate operand must be in the range [1,7]");
8980   case Match_ImmRange1_8:
8981     return Error(ErrorLoc, "immediate operand must be in the range [1,8]");
8982   case Match_ImmRange1_15:
8983     return Error(ErrorLoc, "immediate operand must be in the range [1,15]");
8984   case Match_ImmRange1_16:
8985     return Error(ErrorLoc, "immediate operand must be in the range [1,16]");
8986   case Match_ImmRange1_31:
8987     return Error(ErrorLoc, "immediate operand must be in the range [1,31]");
8988   case Match_ImmRange1_32:
8989     return Error(ErrorLoc, "immediate operand must be in the range [1,32]");
8990   case Match_ImmRange1_64:
8991     return Error(ErrorLoc, "immediate operand must be in the range [1,64]");
8992   case Match_ImmRange8_8:
8993     return Error(ErrorLoc, "immediate operand must be 8.");
8994   case Match_ImmRange16_16:
8995     return Error(ErrorLoc, "immediate operand must be 16.");
8996   case Match_ImmRange32_32:
8997     return Error(ErrorLoc, "immediate operand must be 32.");
8998   case Match_ImmRange256_65535:
8999     return Error(ErrorLoc, "immediate operand must be in the range [255,65535]");
9000   case Match_ImmRange0_16777215:
9001     return Error(ErrorLoc, "immediate operand must be in the range [0,0xffffff]");
9002   case Match_AlignedMemoryRequiresNone:
9003   case Match_DupAlignedMemoryRequiresNone:
9004   case Match_AlignedMemoryRequires16:
9005   case Match_DupAlignedMemoryRequires16:
9006   case Match_AlignedMemoryRequires32:
9007   case Match_DupAlignedMemoryRequires32:
9008   case Match_AlignedMemoryRequires64:
9009   case Match_DupAlignedMemoryRequires64:
9010   case Match_AlignedMemoryRequires64or128:
9011   case Match_DupAlignedMemoryRequires64or128:
9012   case Match_AlignedMemoryRequires64or128or256:
9013   {
9014     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getAlignmentLoc();
9015     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
9016     switch (MatchResult) {
9017       default:
9018         llvm_unreachable("Missing Match_Aligned type");
9019       case Match_AlignedMemoryRequiresNone:
9020       case Match_DupAlignedMemoryRequiresNone:
9021         return Error(ErrorLoc, "alignment must be omitted");
9022       case Match_AlignedMemoryRequires16:
9023       case Match_DupAlignedMemoryRequires16:
9024         return Error(ErrorLoc, "alignment must be 16 or omitted");
9025       case Match_AlignedMemoryRequires32:
9026       case Match_DupAlignedMemoryRequires32:
9027         return Error(ErrorLoc, "alignment must be 32 or omitted");
9028       case Match_AlignedMemoryRequires64:
9029       case Match_DupAlignedMemoryRequires64:
9030         return Error(ErrorLoc, "alignment must be 64 or omitted");
9031       case Match_AlignedMemoryRequires64or128:
9032       case Match_DupAlignedMemoryRequires64or128:
9033         return Error(ErrorLoc, "alignment must be 64, 128 or omitted");
9034       case Match_AlignedMemoryRequires64or128or256:
9035         return Error(ErrorLoc, "alignment must be 64, 128, 256 or omitted");
9036     }
9037   }
9038   }
9039 
9040   llvm_unreachable("Implement any new match types added!");
9041 }
9042 
9043 /// parseDirective parses the arm specific directives
9044 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
9045   const MCObjectFileInfo::Environment Format =
9046     getContext().getObjectFileInfo()->getObjectFileType();
9047   bool IsMachO = Format == MCObjectFileInfo::IsMachO;
9048   bool IsCOFF = Format == MCObjectFileInfo::IsCOFF;
9049 
9050   StringRef IDVal = DirectiveID.getIdentifier();
9051   if (IDVal == ".word")
9052     parseLiteralValues(4, DirectiveID.getLoc());
9053   else if (IDVal == ".short" || IDVal == ".hword")
9054     parseLiteralValues(2, DirectiveID.getLoc());
9055   else if (IDVal == ".thumb")
9056     parseDirectiveThumb(DirectiveID.getLoc());
9057   else if (IDVal == ".arm")
9058     parseDirectiveARM(DirectiveID.getLoc());
9059   else if (IDVal == ".thumb_func")
9060     parseDirectiveThumbFunc(DirectiveID.getLoc());
9061   else if (IDVal == ".code")
9062     parseDirectiveCode(DirectiveID.getLoc());
9063   else if (IDVal == ".syntax")
9064     parseDirectiveSyntax(DirectiveID.getLoc());
9065   else if (IDVal == ".unreq")
9066     parseDirectiveUnreq(DirectiveID.getLoc());
9067   else if (IDVal == ".fnend")
9068     parseDirectiveFnEnd(DirectiveID.getLoc());
9069   else if (IDVal == ".cantunwind")
9070     parseDirectiveCantUnwind(DirectiveID.getLoc());
9071   else if (IDVal == ".personality")
9072     parseDirectivePersonality(DirectiveID.getLoc());
9073   else if (IDVal == ".handlerdata")
9074     parseDirectiveHandlerData(DirectiveID.getLoc());
9075   else if (IDVal == ".setfp")
9076     parseDirectiveSetFP(DirectiveID.getLoc());
9077   else if (IDVal == ".pad")
9078     parseDirectivePad(DirectiveID.getLoc());
9079   else if (IDVal == ".save")
9080     parseDirectiveRegSave(DirectiveID.getLoc(), false);
9081   else if (IDVal == ".vsave")
9082     parseDirectiveRegSave(DirectiveID.getLoc(), true);
9083   else if (IDVal == ".ltorg" || IDVal == ".pool")
9084     parseDirectiveLtorg(DirectiveID.getLoc());
9085   else if (IDVal == ".even")
9086     parseDirectiveEven(DirectiveID.getLoc());
9087   else if (IDVal == ".personalityindex")
9088     parseDirectivePersonalityIndex(DirectiveID.getLoc());
9089   else if (IDVal == ".unwind_raw")
9090     parseDirectiveUnwindRaw(DirectiveID.getLoc());
9091   else if (IDVal == ".movsp")
9092     parseDirectiveMovSP(DirectiveID.getLoc());
9093   else if (IDVal == ".arch_extension")
9094     parseDirectiveArchExtension(DirectiveID.getLoc());
9095   else if (IDVal == ".align")
9096     return parseDirectiveAlign(DirectiveID.getLoc()); // Use Generic on failure.
9097   else if (IDVal == ".thumb_set")
9098     parseDirectiveThumbSet(DirectiveID.getLoc());
9099   else if (!IsMachO && !IsCOFF) {
9100     if (IDVal == ".arch")
9101       parseDirectiveArch(DirectiveID.getLoc());
9102     else if (IDVal == ".cpu")
9103       parseDirectiveCPU(DirectiveID.getLoc());
9104     else if (IDVal == ".eabi_attribute")
9105       parseDirectiveEabiAttr(DirectiveID.getLoc());
9106     else if (IDVal == ".fpu")
9107       parseDirectiveFPU(DirectiveID.getLoc());
9108     else if (IDVal == ".fnstart")
9109       parseDirectiveFnStart(DirectiveID.getLoc());
9110     else if (IDVal == ".inst")
9111       parseDirectiveInst(DirectiveID.getLoc());
9112     else if (IDVal == ".inst.n")
9113       parseDirectiveInst(DirectiveID.getLoc(), 'n');
9114     else if (IDVal == ".inst.w")
9115       parseDirectiveInst(DirectiveID.getLoc(), 'w');
9116     else if (IDVal == ".object_arch")
9117       parseDirectiveObjectArch(DirectiveID.getLoc());
9118     else if (IDVal == ".tlsdescseq")
9119       parseDirectiveTLSDescSeq(DirectiveID.getLoc());
9120     else
9121       return true;
9122   } else
9123     return true;
9124   return false;
9125 }
9126 
9127 /// parseLiteralValues
9128 ///  ::= .hword expression [, expression]*
9129 ///  ::= .short expression [, expression]*
9130 ///  ::= .word expression [, expression]*
9131 bool ARMAsmParser::parseLiteralValues(unsigned Size, SMLoc L) {
9132   auto parseOne = [&]() -> bool {
9133     const MCExpr *Value;
9134     if (getParser().parseExpression(Value))
9135       return true;
9136     getParser().getStreamer().EmitValue(Value, Size, L);
9137     return false;
9138   };
9139   return (parseMany(parseOne));
9140 }
9141 
9142 /// parseDirectiveThumb
9143 ///  ::= .thumb
9144 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
9145   if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive") ||
9146       check(!hasThumb(), L, "target does not support Thumb mode"))
9147     return true;
9148 
9149   if (!isThumb())
9150     SwitchMode();
9151 
9152   getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
9153   return false;
9154 }
9155 
9156 /// parseDirectiveARM
9157 ///  ::= .arm
9158 bool ARMAsmParser::parseDirectiveARM(SMLoc L) {
9159   if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive") ||
9160       check(!hasARM(), L, "target does not support ARM mode"))
9161     return true;
9162 
9163   if (isThumb())
9164     SwitchMode();
9165   getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
9166   return false;
9167 }
9168 
9169 void ARMAsmParser::onLabelParsed(MCSymbol *Symbol) {
9170   // We need to flush the current implicit IT block on a label, because it is
9171   // not legal to branch into an IT block.
9172   flushPendingInstructions(getStreamer());
9173   if (NextSymbolIsThumb) {
9174     getParser().getStreamer().EmitThumbFunc(Symbol);
9175     NextSymbolIsThumb = false;
9176   }
9177 }
9178 
9179 /// parseDirectiveThumbFunc
9180 ///  ::= .thumbfunc symbol_name
9181 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
9182   MCAsmParser &Parser = getParser();
9183   const auto Format = getContext().getObjectFileInfo()->getObjectFileType();
9184   bool IsMachO = Format == MCObjectFileInfo::IsMachO;
9185 
9186   // Darwin asm has (optionally) function name after .thumb_func direction
9187   // ELF doesn't
9188 
9189   if (IsMachO) {
9190     if (Parser.getTok().is(AsmToken::Identifier) ||
9191         Parser.getTok().is(AsmToken::String)) {
9192       MCSymbol *Func = getParser().getContext().getOrCreateSymbol(
9193           Parser.getTok().getIdentifier());
9194       getParser().getStreamer().EmitThumbFunc(Func);
9195       Parser.Lex();
9196       if (parseToken(AsmToken::EndOfStatement,
9197                      "unexpected token in '.thumb_func' directive"))
9198         return true;
9199       return false;
9200     }
9201   }
9202 
9203   if (parseToken(AsmToken::EndOfStatement,
9204                  "unexpected token in '.thumb_func' directive"))
9205     return true;
9206 
9207   NextSymbolIsThumb = true;
9208   return false;
9209 }
9210 
9211 /// parseDirectiveSyntax
9212 ///  ::= .syntax unified | divided
9213 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
9214   MCAsmParser &Parser = getParser();
9215   const AsmToken &Tok = Parser.getTok();
9216   if (Tok.isNot(AsmToken::Identifier)) {
9217     Error(L, "unexpected token in .syntax directive");
9218     return false;
9219   }
9220 
9221   StringRef Mode = Tok.getString();
9222   Parser.Lex();
9223   if (check(Mode == "divided" || Mode == "DIVIDED", L,
9224             "'.syntax divided' arm assembly not supported") ||
9225       check(Mode != "unified" && Mode != "UNIFIED", L,
9226             "unrecognized syntax mode in .syntax directive") ||
9227       parseToken(AsmToken::EndOfStatement, "unexpected token in directive"))
9228     return true;
9229 
9230   // TODO tell the MC streamer the mode
9231   // getParser().getStreamer().Emit???();
9232   return false;
9233 }
9234 
9235 /// parseDirectiveCode
9236 ///  ::= .code 16 | 32
9237 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
9238   MCAsmParser &Parser = getParser();
9239   const AsmToken &Tok = Parser.getTok();
9240   if (Tok.isNot(AsmToken::Integer))
9241     return Error(L, "unexpected token in .code directive");
9242   int64_t Val = Parser.getTok().getIntVal();
9243   if (Val != 16 && Val != 32) {
9244     Error(L, "invalid operand to .code directive");
9245     return false;
9246   }
9247   Parser.Lex();
9248 
9249   if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive"))
9250     return true;
9251 
9252   if (Val == 16) {
9253     if (!hasThumb())
9254       return Error(L, "target does not support Thumb mode");
9255 
9256     if (!isThumb())
9257       SwitchMode();
9258     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
9259   } else {
9260     if (!hasARM())
9261       return Error(L, "target does not support ARM mode");
9262 
9263     if (isThumb())
9264       SwitchMode();
9265     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
9266   }
9267 
9268   return false;
9269 }
9270 
9271 /// parseDirectiveReq
9272 ///  ::= name .req registername
9273 bool ARMAsmParser::parseDirectiveReq(StringRef Name, SMLoc L) {
9274   MCAsmParser &Parser = getParser();
9275   Parser.Lex(); // Eat the '.req' token.
9276   unsigned Reg;
9277   SMLoc SRegLoc, ERegLoc;
9278   if (check(ParseRegister(Reg, SRegLoc, ERegLoc), SRegLoc,
9279             "register name expected") ||
9280       parseToken(AsmToken::EndOfStatement,
9281                  "unexpected input in .req directive."))
9282     return true;
9283 
9284   if (RegisterReqs.insert(std::make_pair(Name, Reg)).first->second != Reg)
9285     return Error(SRegLoc,
9286                  "redefinition of '" + Name + "' does not match original.");
9287 
9288   return false;
9289 }
9290 
9291 /// parseDirectiveUneq
9292 ///  ::= .unreq registername
9293 bool ARMAsmParser::parseDirectiveUnreq(SMLoc L) {
9294   MCAsmParser &Parser = getParser();
9295   if (Parser.getTok().isNot(AsmToken::Identifier))
9296     return Error(L, "unexpected input in .unreq directive.");
9297   RegisterReqs.erase(Parser.getTok().getIdentifier().lower());
9298   Parser.Lex(); // Eat the identifier.
9299   if (parseToken(AsmToken::EndOfStatement,
9300                  "unexpected input in '.unreq' directive"))
9301     return true;
9302   return false;
9303 }
9304 
9305 // After changing arch/CPU, try to put the ARM/Thumb mode back to what it was
9306 // before, if supported by the new target, or emit mapping symbols for the mode
9307 // switch.
9308 void ARMAsmParser::FixModeAfterArchChange(bool WasThumb, SMLoc Loc) {
9309   if (WasThumb != isThumb()) {
9310     if (WasThumb && hasThumb()) {
9311       // Stay in Thumb mode
9312       SwitchMode();
9313     } else if (!WasThumb && hasARM()) {
9314       // Stay in ARM mode
9315       SwitchMode();
9316     } else {
9317       // Mode switch forced, because the new arch doesn't support the old mode.
9318       getParser().getStreamer().EmitAssemblerFlag(isThumb() ? MCAF_Code16
9319                                                             : MCAF_Code32);
9320       // Warn about the implcit mode switch. GAS does not switch modes here,
9321       // but instead stays in the old mode, reporting an error on any following
9322       // instructions as the mode does not exist on the target.
9323       Warning(Loc, Twine("new target does not support ") +
9324                        (WasThumb ? "thumb" : "arm") + " mode, switching to " +
9325                        (!WasThumb ? "thumb" : "arm") + " mode");
9326     }
9327   }
9328 }
9329 
9330 /// parseDirectiveArch
9331 ///  ::= .arch token
9332 bool ARMAsmParser::parseDirectiveArch(SMLoc L) {
9333   StringRef Arch = getParser().parseStringToEndOfStatement().trim();
9334   ARM::ArchKind ID = ARM::parseArch(Arch);
9335 
9336   if (ID == ARM::ArchKind::INVALID)
9337     return Error(L, "Unknown arch name");
9338 
9339   bool WasThumb = isThumb();
9340   Triple T;
9341   MCSubtargetInfo &STI = copySTI();
9342   STI.setDefaultFeatures("", ("+" + ARM::getArchName(ID)).str());
9343   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9344   FixModeAfterArchChange(WasThumb, L);
9345 
9346   getTargetStreamer().emitArch(ID);
9347   return false;
9348 }
9349 
9350 /// parseDirectiveEabiAttr
9351 ///  ::= .eabi_attribute int, int [, "str"]
9352 ///  ::= .eabi_attribute Tag_name, int [, "str"]
9353 bool ARMAsmParser::parseDirectiveEabiAttr(SMLoc L) {
9354   MCAsmParser &Parser = getParser();
9355   int64_t Tag;
9356   SMLoc TagLoc;
9357   TagLoc = Parser.getTok().getLoc();
9358   if (Parser.getTok().is(AsmToken::Identifier)) {
9359     StringRef Name = Parser.getTok().getIdentifier();
9360     Tag = ARMBuildAttrs::AttrTypeFromString(Name);
9361     if (Tag == -1) {
9362       Error(TagLoc, "attribute name not recognised: " + Name);
9363       return false;
9364     }
9365     Parser.Lex();
9366   } else {
9367     const MCExpr *AttrExpr;
9368 
9369     TagLoc = Parser.getTok().getLoc();
9370     if (Parser.parseExpression(AttrExpr))
9371       return true;
9372 
9373     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(AttrExpr);
9374     if (check(!CE, TagLoc, "expected numeric constant"))
9375       return true;
9376 
9377     Tag = CE->getValue();
9378   }
9379 
9380   if (Parser.parseToken(AsmToken::Comma, "comma expected"))
9381     return true;
9382 
9383   StringRef StringValue = "";
9384   bool IsStringValue = false;
9385 
9386   int64_t IntegerValue = 0;
9387   bool IsIntegerValue = false;
9388 
9389   if (Tag == ARMBuildAttrs::CPU_raw_name || Tag == ARMBuildAttrs::CPU_name)
9390     IsStringValue = true;
9391   else if (Tag == ARMBuildAttrs::compatibility) {
9392     IsStringValue = true;
9393     IsIntegerValue = true;
9394   } else if (Tag < 32 || Tag % 2 == 0)
9395     IsIntegerValue = true;
9396   else if (Tag % 2 == 1)
9397     IsStringValue = true;
9398   else
9399     llvm_unreachable("invalid tag type");
9400 
9401   if (IsIntegerValue) {
9402     const MCExpr *ValueExpr;
9403     SMLoc ValueExprLoc = Parser.getTok().getLoc();
9404     if (Parser.parseExpression(ValueExpr))
9405       return true;
9406 
9407     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ValueExpr);
9408     if (!CE)
9409       return Error(ValueExprLoc, "expected numeric constant");
9410     IntegerValue = CE->getValue();
9411   }
9412 
9413   if (Tag == ARMBuildAttrs::compatibility) {
9414     if (Parser.parseToken(AsmToken::Comma, "comma expected"))
9415       return true;
9416   }
9417 
9418   if (IsStringValue) {
9419     if (Parser.getTok().isNot(AsmToken::String))
9420       return Error(Parser.getTok().getLoc(), "bad string constant");
9421 
9422     StringValue = Parser.getTok().getStringContents();
9423     Parser.Lex();
9424   }
9425 
9426   if (Parser.parseToken(AsmToken::EndOfStatement,
9427                         "unexpected token in '.eabi_attribute' directive"))
9428     return true;
9429 
9430   if (IsIntegerValue && IsStringValue) {
9431     assert(Tag == ARMBuildAttrs::compatibility);
9432     getTargetStreamer().emitIntTextAttribute(Tag, IntegerValue, StringValue);
9433   } else if (IsIntegerValue)
9434     getTargetStreamer().emitAttribute(Tag, IntegerValue);
9435   else if (IsStringValue)
9436     getTargetStreamer().emitTextAttribute(Tag, StringValue);
9437   return false;
9438 }
9439 
9440 /// parseDirectiveCPU
9441 ///  ::= .cpu str
9442 bool ARMAsmParser::parseDirectiveCPU(SMLoc L) {
9443   StringRef CPU = getParser().parseStringToEndOfStatement().trim();
9444   getTargetStreamer().emitTextAttribute(ARMBuildAttrs::CPU_name, CPU);
9445 
9446   // FIXME: This is using table-gen data, but should be moved to
9447   // ARMTargetParser once that is table-gen'd.
9448   if (!getSTI().isCPUStringValid(CPU))
9449     return Error(L, "Unknown CPU name");
9450 
9451   bool WasThumb = isThumb();
9452   MCSubtargetInfo &STI = copySTI();
9453   STI.setDefaultFeatures(CPU, "");
9454   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9455   FixModeAfterArchChange(WasThumb, L);
9456 
9457   return false;
9458 }
9459 /// parseDirectiveFPU
9460 ///  ::= .fpu str
9461 bool ARMAsmParser::parseDirectiveFPU(SMLoc L) {
9462   SMLoc FPUNameLoc = getTok().getLoc();
9463   StringRef FPU = getParser().parseStringToEndOfStatement().trim();
9464 
9465   unsigned ID = ARM::parseFPU(FPU);
9466   std::vector<StringRef> Features;
9467   if (!ARM::getFPUFeatures(ID, Features))
9468     return Error(FPUNameLoc, "Unknown FPU name");
9469 
9470   MCSubtargetInfo &STI = copySTI();
9471   for (auto Feature : Features)
9472     STI.ApplyFeatureFlag(Feature);
9473   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9474 
9475   getTargetStreamer().emitFPU(ID);
9476   return false;
9477 }
9478 
9479 /// parseDirectiveFnStart
9480 ///  ::= .fnstart
9481 bool ARMAsmParser::parseDirectiveFnStart(SMLoc L) {
9482   if (parseToken(AsmToken::EndOfStatement,
9483                  "unexpected token in '.fnstart' directive"))
9484     return true;
9485 
9486   if (UC.hasFnStart()) {
9487     Error(L, ".fnstart starts before the end of previous one");
9488     UC.emitFnStartLocNotes();
9489     return true;
9490   }
9491 
9492   // Reset the unwind directives parser state
9493   UC.reset();
9494 
9495   getTargetStreamer().emitFnStart();
9496 
9497   UC.recordFnStart(L);
9498   return false;
9499 }
9500 
9501 /// parseDirectiveFnEnd
9502 ///  ::= .fnend
9503 bool ARMAsmParser::parseDirectiveFnEnd(SMLoc L) {
9504   if (parseToken(AsmToken::EndOfStatement,
9505                  "unexpected token in '.fnend' directive"))
9506     return true;
9507   // Check the ordering of unwind directives
9508   if (!UC.hasFnStart())
9509     return Error(L, ".fnstart must precede .fnend directive");
9510 
9511   // Reset the unwind directives parser state
9512   getTargetStreamer().emitFnEnd();
9513 
9514   UC.reset();
9515   return false;
9516 }
9517 
9518 /// parseDirectiveCantUnwind
9519 ///  ::= .cantunwind
9520 bool ARMAsmParser::parseDirectiveCantUnwind(SMLoc L) {
9521   if (parseToken(AsmToken::EndOfStatement,
9522                  "unexpected token in '.cantunwind' directive"))
9523     return true;
9524 
9525   UC.recordCantUnwind(L);
9526   // Check the ordering of unwind directives
9527   if (check(!UC.hasFnStart(), L, ".fnstart must precede .cantunwind directive"))
9528     return true;
9529 
9530   if (UC.hasHandlerData()) {
9531     Error(L, ".cantunwind can't be used with .handlerdata directive");
9532     UC.emitHandlerDataLocNotes();
9533     return true;
9534   }
9535   if (UC.hasPersonality()) {
9536     Error(L, ".cantunwind can't be used with .personality directive");
9537     UC.emitPersonalityLocNotes();
9538     return true;
9539   }
9540 
9541   getTargetStreamer().emitCantUnwind();
9542   return false;
9543 }
9544 
9545 /// parseDirectivePersonality
9546 ///  ::= .personality name
9547 bool ARMAsmParser::parseDirectivePersonality(SMLoc L) {
9548   MCAsmParser &Parser = getParser();
9549   bool HasExistingPersonality = UC.hasPersonality();
9550 
9551   // Parse the name of the personality routine
9552   if (Parser.getTok().isNot(AsmToken::Identifier))
9553     return Error(L, "unexpected input in .personality directive.");
9554   StringRef Name(Parser.getTok().getIdentifier());
9555   Parser.Lex();
9556 
9557   if (parseToken(AsmToken::EndOfStatement,
9558                  "unexpected token in '.personality' directive"))
9559     return true;
9560 
9561   UC.recordPersonality(L);
9562 
9563   // Check the ordering of unwind directives
9564   if (!UC.hasFnStart())
9565     return Error(L, ".fnstart must precede .personality directive");
9566   if (UC.cantUnwind()) {
9567     Error(L, ".personality can't be used with .cantunwind directive");
9568     UC.emitCantUnwindLocNotes();
9569     return true;
9570   }
9571   if (UC.hasHandlerData()) {
9572     Error(L, ".personality must precede .handlerdata directive");
9573     UC.emitHandlerDataLocNotes();
9574     return true;
9575   }
9576   if (HasExistingPersonality) {
9577     Error(L, "multiple personality directives");
9578     UC.emitPersonalityLocNotes();
9579     return true;
9580   }
9581 
9582   MCSymbol *PR = getParser().getContext().getOrCreateSymbol(Name);
9583   getTargetStreamer().emitPersonality(PR);
9584   return false;
9585 }
9586 
9587 /// parseDirectiveHandlerData
9588 ///  ::= .handlerdata
9589 bool ARMAsmParser::parseDirectiveHandlerData(SMLoc L) {
9590   if (parseToken(AsmToken::EndOfStatement,
9591                  "unexpected token in '.handlerdata' directive"))
9592     return true;
9593 
9594   UC.recordHandlerData(L);
9595   // Check the ordering of unwind directives
9596   if (!UC.hasFnStart())
9597     return Error(L, ".fnstart must precede .personality directive");
9598   if (UC.cantUnwind()) {
9599     Error(L, ".handlerdata can't be used with .cantunwind directive");
9600     UC.emitCantUnwindLocNotes();
9601     return true;
9602   }
9603 
9604   getTargetStreamer().emitHandlerData();
9605   return false;
9606 }
9607 
9608 /// parseDirectiveSetFP
9609 ///  ::= .setfp fpreg, spreg [, offset]
9610 bool ARMAsmParser::parseDirectiveSetFP(SMLoc L) {
9611   MCAsmParser &Parser = getParser();
9612   // Check the ordering of unwind directives
9613   if (check(!UC.hasFnStart(), L, ".fnstart must precede .setfp directive") ||
9614       check(UC.hasHandlerData(), L,
9615             ".setfp must precede .handlerdata directive"))
9616     return true;
9617 
9618   // Parse fpreg
9619   SMLoc FPRegLoc = Parser.getTok().getLoc();
9620   int FPReg = tryParseRegister();
9621 
9622   if (check(FPReg == -1, FPRegLoc, "frame pointer register expected") ||
9623       Parser.parseToken(AsmToken::Comma, "comma expected"))
9624     return true;
9625 
9626   // Parse spreg
9627   SMLoc SPRegLoc = Parser.getTok().getLoc();
9628   int SPReg = tryParseRegister();
9629   if (check(SPReg == -1, SPRegLoc, "stack pointer register expected") ||
9630       check(SPReg != ARM::SP && SPReg != UC.getFPReg(), SPRegLoc,
9631             "register should be either $sp or the latest fp register"))
9632     return true;
9633 
9634   // Update the frame pointer register
9635   UC.saveFPReg(FPReg);
9636 
9637   // Parse offset
9638   int64_t Offset = 0;
9639   if (Parser.parseOptionalToken(AsmToken::Comma)) {
9640     if (Parser.getTok().isNot(AsmToken::Hash) &&
9641         Parser.getTok().isNot(AsmToken::Dollar))
9642       return Error(Parser.getTok().getLoc(), "'#' expected");
9643     Parser.Lex(); // skip hash token.
9644 
9645     const MCExpr *OffsetExpr;
9646     SMLoc ExLoc = Parser.getTok().getLoc();
9647     SMLoc EndLoc;
9648     if (getParser().parseExpression(OffsetExpr, EndLoc))
9649       return Error(ExLoc, "malformed setfp offset");
9650     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9651     if (check(!CE, ExLoc, "setfp offset must be an immediate"))
9652       return true;
9653     Offset = CE->getValue();
9654   }
9655 
9656   if (Parser.parseToken(AsmToken::EndOfStatement))
9657     return true;
9658 
9659   getTargetStreamer().emitSetFP(static_cast<unsigned>(FPReg),
9660                                 static_cast<unsigned>(SPReg), Offset);
9661   return false;
9662 }
9663 
9664 /// parseDirective
9665 ///  ::= .pad offset
9666 bool ARMAsmParser::parseDirectivePad(SMLoc L) {
9667   MCAsmParser &Parser = getParser();
9668   // Check the ordering of unwind directives
9669   if (!UC.hasFnStart())
9670     return Error(L, ".fnstart must precede .pad directive");
9671   if (UC.hasHandlerData())
9672     return Error(L, ".pad must precede .handlerdata directive");
9673 
9674   // Parse the offset
9675   if (Parser.getTok().isNot(AsmToken::Hash) &&
9676       Parser.getTok().isNot(AsmToken::Dollar))
9677     return Error(Parser.getTok().getLoc(), "'#' expected");
9678   Parser.Lex(); // skip hash token.
9679 
9680   const MCExpr *OffsetExpr;
9681   SMLoc ExLoc = Parser.getTok().getLoc();
9682   SMLoc EndLoc;
9683   if (getParser().parseExpression(OffsetExpr, EndLoc))
9684     return Error(ExLoc, "malformed pad offset");
9685   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9686   if (!CE)
9687     return Error(ExLoc, "pad offset must be an immediate");
9688 
9689   if (parseToken(AsmToken::EndOfStatement,
9690                  "unexpected token in '.pad' directive"))
9691     return true;
9692 
9693   getTargetStreamer().emitPad(CE->getValue());
9694   return false;
9695 }
9696 
9697 /// parseDirectiveRegSave
9698 ///  ::= .save  { registers }
9699 ///  ::= .vsave { registers }
9700 bool ARMAsmParser::parseDirectiveRegSave(SMLoc L, bool IsVector) {
9701   // Check the ordering of unwind directives
9702   if (!UC.hasFnStart())
9703     return Error(L, ".fnstart must precede .save or .vsave directives");
9704   if (UC.hasHandlerData())
9705     return Error(L, ".save or .vsave must precede .handlerdata directive");
9706 
9707   // RAII object to make sure parsed operands are deleted.
9708   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Operands;
9709 
9710   // Parse the register list
9711   if (parseRegisterList(Operands) ||
9712       parseToken(AsmToken::EndOfStatement, "unexpected token in directive"))
9713     return true;
9714   ARMOperand &Op = (ARMOperand &)*Operands[0];
9715   if (!IsVector && !Op.isRegList())
9716     return Error(L, ".save expects GPR registers");
9717   if (IsVector && !Op.isDPRRegList())
9718     return Error(L, ".vsave expects DPR registers");
9719 
9720   getTargetStreamer().emitRegSave(Op.getRegList(), IsVector);
9721   return false;
9722 }
9723 
9724 /// parseDirectiveInst
9725 ///  ::= .inst opcode [, ...]
9726 ///  ::= .inst.n opcode [, ...]
9727 ///  ::= .inst.w opcode [, ...]
9728 bool ARMAsmParser::parseDirectiveInst(SMLoc Loc, char Suffix) {
9729   int Width = 4;
9730 
9731   if (isThumb()) {
9732     switch (Suffix) {
9733     case 'n':
9734       Width = 2;
9735       break;
9736     case 'w':
9737       break;
9738     default:
9739       return Error(Loc, "cannot determine Thumb instruction size, "
9740                         "use inst.n/inst.w instead");
9741     }
9742   } else {
9743     if (Suffix)
9744       return Error(Loc, "width suffixes are invalid in ARM mode");
9745   }
9746 
9747   auto parseOne = [&]() -> bool {
9748     const MCExpr *Expr;
9749     if (getParser().parseExpression(Expr))
9750       return true;
9751     const MCConstantExpr *Value = dyn_cast_or_null<MCConstantExpr>(Expr);
9752     if (!Value) {
9753       return Error(Loc, "expected constant expression");
9754     }
9755 
9756     switch (Width) {
9757     case 2:
9758       if (Value->getValue() > 0xffff)
9759         return Error(Loc, "inst.n operand is too big, use inst.w instead");
9760       break;
9761     case 4:
9762       if (Value->getValue() > 0xffffffff)
9763         return Error(Loc, StringRef(Suffix ? "inst.w" : "inst") +
9764                               " operand is too big");
9765       break;
9766     default:
9767       llvm_unreachable("only supported widths are 2 and 4");
9768     }
9769 
9770     getTargetStreamer().emitInst(Value->getValue(), Suffix);
9771     return false;
9772   };
9773 
9774   if (parseOptionalToken(AsmToken::EndOfStatement))
9775     return Error(Loc, "expected expression following directive");
9776   if (parseMany(parseOne))
9777     return true;
9778   return false;
9779 }
9780 
9781 /// parseDirectiveLtorg
9782 ///  ::= .ltorg | .pool
9783 bool ARMAsmParser::parseDirectiveLtorg(SMLoc L) {
9784   if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive"))
9785     return true;
9786   getTargetStreamer().emitCurrentConstantPool();
9787   return false;
9788 }
9789 
9790 bool ARMAsmParser::parseDirectiveEven(SMLoc L) {
9791   const MCSection *Section = getStreamer().getCurrentSectionOnly();
9792 
9793   if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive"))
9794     return true;
9795 
9796   if (!Section) {
9797     getStreamer().InitSections(false);
9798     Section = getStreamer().getCurrentSectionOnly();
9799   }
9800 
9801   assert(Section && "must have section to emit alignment");
9802   if (Section->UseCodeAlign())
9803     getStreamer().EmitCodeAlignment(2);
9804   else
9805     getStreamer().EmitValueToAlignment(2);
9806 
9807   return false;
9808 }
9809 
9810 /// parseDirectivePersonalityIndex
9811 ///   ::= .personalityindex index
9812 bool ARMAsmParser::parseDirectivePersonalityIndex(SMLoc L) {
9813   MCAsmParser &Parser = getParser();
9814   bool HasExistingPersonality = UC.hasPersonality();
9815 
9816   const MCExpr *IndexExpression;
9817   SMLoc IndexLoc = Parser.getTok().getLoc();
9818   if (Parser.parseExpression(IndexExpression) ||
9819       parseToken(AsmToken::EndOfStatement,
9820                  "unexpected token in '.personalityindex' directive")) {
9821     return true;
9822   }
9823 
9824   UC.recordPersonalityIndex(L);
9825 
9826   if (!UC.hasFnStart()) {
9827     return Error(L, ".fnstart must precede .personalityindex directive");
9828   }
9829   if (UC.cantUnwind()) {
9830     Error(L, ".personalityindex cannot be used with .cantunwind");
9831     UC.emitCantUnwindLocNotes();
9832     return true;
9833   }
9834   if (UC.hasHandlerData()) {
9835     Error(L, ".personalityindex must precede .handlerdata directive");
9836     UC.emitHandlerDataLocNotes();
9837     return true;
9838   }
9839   if (HasExistingPersonality) {
9840     Error(L, "multiple personality directives");
9841     UC.emitPersonalityLocNotes();
9842     return true;
9843   }
9844 
9845   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(IndexExpression);
9846   if (!CE)
9847     return Error(IndexLoc, "index must be a constant number");
9848   if (CE->getValue() < 0 || CE->getValue() >= ARM::EHABI::NUM_PERSONALITY_INDEX)
9849     return Error(IndexLoc,
9850                  "personality routine index should be in range [0-3]");
9851 
9852   getTargetStreamer().emitPersonalityIndex(CE->getValue());
9853   return false;
9854 }
9855 
9856 /// parseDirectiveUnwindRaw
9857 ///   ::= .unwind_raw offset, opcode [, opcode...]
9858 bool ARMAsmParser::parseDirectiveUnwindRaw(SMLoc L) {
9859   MCAsmParser &Parser = getParser();
9860   int64_t StackOffset;
9861   const MCExpr *OffsetExpr;
9862   SMLoc OffsetLoc = getLexer().getLoc();
9863 
9864   if (!UC.hasFnStart())
9865     return Error(L, ".fnstart must precede .unwind_raw directives");
9866   if (getParser().parseExpression(OffsetExpr))
9867     return Error(OffsetLoc, "expected expression");
9868 
9869   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9870   if (!CE)
9871     return Error(OffsetLoc, "offset must be a constant");
9872 
9873   StackOffset = CE->getValue();
9874 
9875   if (Parser.parseToken(AsmToken::Comma, "expected comma"))
9876     return true;
9877 
9878   SmallVector<uint8_t, 16> Opcodes;
9879 
9880   auto parseOne = [&]() -> bool {
9881     const MCExpr *OE;
9882     SMLoc OpcodeLoc = getLexer().getLoc();
9883     if (check(getLexer().is(AsmToken::EndOfStatement) ||
9884                   Parser.parseExpression(OE),
9885               OpcodeLoc, "expected opcode expression"))
9886       return true;
9887     const MCConstantExpr *OC = dyn_cast<MCConstantExpr>(OE);
9888     if (!OC)
9889       return Error(OpcodeLoc, "opcode value must be a constant");
9890     const int64_t Opcode = OC->getValue();
9891     if (Opcode & ~0xff)
9892       return Error(OpcodeLoc, "invalid opcode");
9893     Opcodes.push_back(uint8_t(Opcode));
9894     return false;
9895   };
9896 
9897   // Must have at least 1 element
9898   SMLoc OpcodeLoc = getLexer().getLoc();
9899   if (parseOptionalToken(AsmToken::EndOfStatement))
9900     return Error(OpcodeLoc, "expected opcode expression");
9901   if (parseMany(parseOne))
9902     return true;
9903 
9904   getTargetStreamer().emitUnwindRaw(StackOffset, Opcodes);
9905   return false;
9906 }
9907 
9908 /// parseDirectiveTLSDescSeq
9909 ///   ::= .tlsdescseq tls-variable
9910 bool ARMAsmParser::parseDirectiveTLSDescSeq(SMLoc L) {
9911   MCAsmParser &Parser = getParser();
9912 
9913   if (getLexer().isNot(AsmToken::Identifier))
9914     return TokError("expected variable after '.tlsdescseq' directive");
9915 
9916   const MCSymbolRefExpr *SRE =
9917     MCSymbolRefExpr::create(Parser.getTok().getIdentifier(),
9918                             MCSymbolRefExpr::VK_ARM_TLSDESCSEQ, getContext());
9919   Lex();
9920 
9921   if (parseToken(AsmToken::EndOfStatement,
9922                  "unexpected token in '.tlsdescseq' directive"))
9923     return true;
9924 
9925   getTargetStreamer().AnnotateTLSDescriptorSequence(SRE);
9926   return false;
9927 }
9928 
9929 /// parseDirectiveMovSP
9930 ///  ::= .movsp reg [, #offset]
9931 bool ARMAsmParser::parseDirectiveMovSP(SMLoc L) {
9932   MCAsmParser &Parser = getParser();
9933   if (!UC.hasFnStart())
9934     return Error(L, ".fnstart must precede .movsp directives");
9935   if (UC.getFPReg() != ARM::SP)
9936     return Error(L, "unexpected .movsp directive");
9937 
9938   SMLoc SPRegLoc = Parser.getTok().getLoc();
9939   int SPReg = tryParseRegister();
9940   if (SPReg == -1)
9941     return Error(SPRegLoc, "register expected");
9942   if (SPReg == ARM::SP || SPReg == ARM::PC)
9943     return Error(SPRegLoc, "sp and pc are not permitted in .movsp directive");
9944 
9945   int64_t Offset = 0;
9946   if (Parser.parseOptionalToken(AsmToken::Comma)) {
9947     if (Parser.parseToken(AsmToken::Hash, "expected #constant"))
9948       return true;
9949 
9950     const MCExpr *OffsetExpr;
9951     SMLoc OffsetLoc = Parser.getTok().getLoc();
9952 
9953     if (Parser.parseExpression(OffsetExpr))
9954       return Error(OffsetLoc, "malformed offset expression");
9955 
9956     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9957     if (!CE)
9958       return Error(OffsetLoc, "offset must be an immediate constant");
9959 
9960     Offset = CE->getValue();
9961   }
9962 
9963   if (parseToken(AsmToken::EndOfStatement,
9964                  "unexpected token in '.movsp' directive"))
9965     return true;
9966 
9967   getTargetStreamer().emitMovSP(SPReg, Offset);
9968   UC.saveFPReg(SPReg);
9969 
9970   return false;
9971 }
9972 
9973 /// parseDirectiveObjectArch
9974 ///   ::= .object_arch name
9975 bool ARMAsmParser::parseDirectiveObjectArch(SMLoc L) {
9976   MCAsmParser &Parser = getParser();
9977   if (getLexer().isNot(AsmToken::Identifier))
9978     return Error(getLexer().getLoc(), "unexpected token");
9979 
9980   StringRef Arch = Parser.getTok().getString();
9981   SMLoc ArchLoc = Parser.getTok().getLoc();
9982   Lex();
9983 
9984   ARM::ArchKind ID = ARM::parseArch(Arch);
9985 
9986   if (ID == ARM::ArchKind::INVALID)
9987     return Error(ArchLoc, "unknown architecture '" + Arch + "'");
9988   if (parseToken(AsmToken::EndOfStatement))
9989     return true;
9990 
9991   getTargetStreamer().emitObjectArch(ID);
9992   return false;
9993 }
9994 
9995 /// parseDirectiveAlign
9996 ///   ::= .align
9997 bool ARMAsmParser::parseDirectiveAlign(SMLoc L) {
9998   // NOTE: if this is not the end of the statement, fall back to the target
9999   // agnostic handling for this directive which will correctly handle this.
10000   if (parseOptionalToken(AsmToken::EndOfStatement)) {
10001     // '.align' is target specifically handled to mean 2**2 byte alignment.
10002     const MCSection *Section = getStreamer().getCurrentSectionOnly();
10003     assert(Section && "must have section to emit alignment");
10004     if (Section->UseCodeAlign())
10005       getStreamer().EmitCodeAlignment(4, 0);
10006     else
10007       getStreamer().EmitValueToAlignment(4, 0, 1, 0);
10008     return false;
10009   }
10010   return true;
10011 }
10012 
10013 /// parseDirectiveThumbSet
10014 ///  ::= .thumb_set name, value
10015 bool ARMAsmParser::parseDirectiveThumbSet(SMLoc L) {
10016   MCAsmParser &Parser = getParser();
10017 
10018   StringRef Name;
10019   if (check(Parser.parseIdentifier(Name),
10020             "expected identifier after '.thumb_set'") ||
10021       parseToken(AsmToken::Comma, "expected comma after name '" + Name + "'"))
10022     return true;
10023 
10024   MCSymbol *Sym;
10025   const MCExpr *Value;
10026   if (MCParserUtils::parseAssignmentExpression(Name, /* allow_redef */ true,
10027                                                Parser, Sym, Value))
10028     return true;
10029 
10030   getTargetStreamer().emitThumbSet(Sym, Value);
10031   return false;
10032 }
10033 
10034 /// Force static initialization.
10035 extern "C" void LLVMInitializeARMAsmParser() {
10036   RegisterMCAsmParser<ARMAsmParser> X(getTheARMLETarget());
10037   RegisterMCAsmParser<ARMAsmParser> Y(getTheARMBETarget());
10038   RegisterMCAsmParser<ARMAsmParser> A(getTheThumbLETarget());
10039   RegisterMCAsmParser<ARMAsmParser> B(getTheThumbBETarget());
10040 }
10041 
10042 #define GET_REGISTER_MATCHER
10043 #define GET_SUBTARGET_FEATURE_NAME
10044 #define GET_MATCHER_IMPLEMENTATION
10045 #include "ARMGenAsmMatcher.inc"
10046 
10047 // FIXME: This structure should be moved inside ARMTargetParser
10048 // when we start to table-generate them, and we can use the ARM
10049 // flags below, that were generated by table-gen.
10050 static const struct {
10051   const unsigned Kind;
10052   const uint64_t ArchCheck;
10053   const FeatureBitset Features;
10054 } Extensions[] = {
10055   { ARM::AEK_CRC, Feature_HasV8, {ARM::FeatureCRC} },
10056   { ARM::AEK_CRYPTO,  Feature_HasV8,
10057     {ARM::FeatureCrypto, ARM::FeatureNEON, ARM::FeatureFPARMv8} },
10058   { ARM::AEK_FP, Feature_HasV8, {ARM::FeatureFPARMv8} },
10059   { (ARM::AEK_HWDIVTHUMB | ARM::AEK_HWDIVARM), Feature_HasV7 | Feature_IsNotMClass,
10060     {ARM::FeatureHWDivThumb, ARM::FeatureHWDivARM} },
10061   { ARM::AEK_MP, Feature_HasV7 | Feature_IsNotMClass, {ARM::FeatureMP} },
10062   { ARM::AEK_SIMD, Feature_HasV8, {ARM::FeatureNEON, ARM::FeatureFPARMv8} },
10063   { ARM::AEK_SEC, Feature_HasV6K, {ARM::FeatureTrustZone} },
10064   // FIXME: Only available in A-class, isel not predicated
10065   { ARM::AEK_VIRT, Feature_HasV7, {ARM::FeatureVirtualization} },
10066   { ARM::AEK_FP16, Feature_HasV8_2a, {ARM::FeatureFPARMv8, ARM::FeatureFullFP16} },
10067   { ARM::AEK_RAS, Feature_HasV8, {ARM::FeatureRAS} },
10068   // FIXME: Unsupported extensions.
10069   { ARM::AEK_OS, Feature_None, {} },
10070   { ARM::AEK_IWMMXT, Feature_None, {} },
10071   { ARM::AEK_IWMMXT2, Feature_None, {} },
10072   { ARM::AEK_MAVERICK, Feature_None, {} },
10073   { ARM::AEK_XSCALE, Feature_None, {} },
10074 };
10075 
10076 /// parseDirectiveArchExtension
10077 ///   ::= .arch_extension [no]feature
10078 bool ARMAsmParser::parseDirectiveArchExtension(SMLoc L) {
10079   MCAsmParser &Parser = getParser();
10080 
10081   if (getLexer().isNot(AsmToken::Identifier))
10082     return Error(getLexer().getLoc(), "expected architecture extension name");
10083 
10084   StringRef Name = Parser.getTok().getString();
10085   SMLoc ExtLoc = Parser.getTok().getLoc();
10086   Lex();
10087 
10088   if (parseToken(AsmToken::EndOfStatement,
10089                  "unexpected token in '.arch_extension' directive"))
10090     return true;
10091 
10092   bool EnableFeature = true;
10093   if (Name.startswith_lower("no")) {
10094     EnableFeature = false;
10095     Name = Name.substr(2);
10096   }
10097   unsigned FeatureKind = ARM::parseArchExt(Name);
10098   if (FeatureKind == ARM::AEK_INVALID)
10099     return Error(ExtLoc, "unknown architectural extension: " + Name);
10100 
10101   for (const auto &Extension : Extensions) {
10102     if (Extension.Kind != FeatureKind)
10103       continue;
10104 
10105     if (Extension.Features.none())
10106       return Error(ExtLoc, "unsupported architectural extension: " + Name);
10107 
10108     if ((getAvailableFeatures() & Extension.ArchCheck) != Extension.ArchCheck)
10109       return Error(ExtLoc, "architectural extension '" + Name +
10110                                "' is not "
10111                                "allowed for the current base architecture");
10112 
10113     MCSubtargetInfo &STI = copySTI();
10114     FeatureBitset ToggleFeatures = EnableFeature
10115       ? (~STI.getFeatureBits() & Extension.Features)
10116       : ( STI.getFeatureBits() & Extension.Features);
10117 
10118     uint64_t Features =
10119         ComputeAvailableFeatures(STI.ToggleFeature(ToggleFeatures));
10120     setAvailableFeatures(Features);
10121     return false;
10122   }
10123 
10124   return Error(ExtLoc, "unknown architectural extension: " + Name);
10125 }
10126 
10127 // Define this matcher function after the auto-generated include so we
10128 // have the match class enum definitions.
10129 unsigned ARMAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
10130                                                   unsigned Kind) {
10131   ARMOperand &Op = static_cast<ARMOperand &>(AsmOp);
10132   // If the kind is a token for a literal immediate, check if our asm
10133   // operand matches. This is for InstAliases which have a fixed-value
10134   // immediate in the syntax.
10135   switch (Kind) {
10136   default: break;
10137   case MCK__35_0:
10138     if (Op.isImm())
10139       if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm()))
10140         if (CE->getValue() == 0)
10141           return Match_Success;
10142     break;
10143   case MCK_ModImm:
10144     if (Op.isImm()) {
10145       const MCExpr *SOExpr = Op.getImm();
10146       int64_t Value;
10147       if (!SOExpr->evaluateAsAbsolute(Value))
10148         return Match_Success;
10149       assert((Value >= INT32_MIN && Value <= UINT32_MAX) &&
10150              "expression value must be representable in 32 bits");
10151     }
10152     break;
10153   case MCK_rGPR:
10154     if (hasV8Ops() && Op.isReg() && Op.getReg() == ARM::SP)
10155       return Match_Success;
10156     break;
10157   case MCK_GPRPair:
10158     if (Op.isReg() &&
10159         MRI->getRegClass(ARM::GPRRegClassID).contains(Op.getReg()))
10160       return Match_Success;
10161     break;
10162   }
10163   return Match_InvalidOperand;
10164 }
10165