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 "MCTargetDesc/ARMAddressingModes.h"
12 #include "MCTargetDesc/ARMBaseInfo.h"
13 #include "MCTargetDesc/ARMMCExpr.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCAssembler.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
24 #include "llvm/MC/MCELFStreamer.h"
25 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCInstrDesc.h"
28 #include "llvm/MC/MCInstrInfo.h"
29 #include "llvm/MC/MCObjectFileInfo.h"
30 #include "llvm/MC/MCParser/MCAsmLexer.h"
31 #include "llvm/MC/MCParser/MCAsmParser.h"
32 #include "llvm/MC/MCParser/MCAsmParserUtils.h"
33 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
34 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
35 #include "llvm/MC/MCRegisterInfo.h"
36 #include "llvm/MC/MCSection.h"
37 #include "llvm/MC/MCStreamer.h"
38 #include "llvm/MC/MCSubtargetInfo.h"
39 #include "llvm/MC/MCSymbol.h"
40 #include "llvm/Support/ARMBuildAttributes.h"
41 #include "llvm/Support/ARMEHABI.h"
42 #include "llvm/Support/COFF.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/ELF.h"
45 #include "llvm/Support/MathExtras.h"
46 #include "llvm/Support/SourceMgr.h"
47 #include "llvm/Support/TargetParser.h"
48 #include "llvm/Support/TargetRegistry.h"
49 #include "llvm/Support/raw_ostream.h"
50 
51 using namespace llvm;
52 
53 namespace {
54 
55 class ARMOperand;
56 
57 enum VectorLaneTy { NoLanes, AllLanes, IndexedLane };
58 
59 class UnwindContext {
60   MCAsmParser &Parser;
61 
62   typedef SmallVector<SMLoc, 4> Locs;
63 
64   Locs FnStartLocs;
65   Locs CantUnwindLocs;
66   Locs PersonalityLocs;
67   Locs PersonalityIndexLocs;
68   Locs HandlerDataLocs;
69   int FPReg;
70 
71 public:
72   UnwindContext(MCAsmParser &P) : Parser(P), FPReg(ARM::SP) {}
73 
74   bool hasFnStart() const { return !FnStartLocs.empty(); }
75   bool cantUnwind() const { return !CantUnwindLocs.empty(); }
76   bool hasHandlerData() const { return !HandlerDataLocs.empty(); }
77   bool hasPersonality() const {
78     return !(PersonalityLocs.empty() && PersonalityIndexLocs.empty());
79   }
80 
81   void recordFnStart(SMLoc L) { FnStartLocs.push_back(L); }
82   void recordCantUnwind(SMLoc L) { CantUnwindLocs.push_back(L); }
83   void recordPersonality(SMLoc L) { PersonalityLocs.push_back(L); }
84   void recordHandlerData(SMLoc L) { HandlerDataLocs.push_back(L); }
85   void recordPersonalityIndex(SMLoc L) { PersonalityIndexLocs.push_back(L); }
86 
87   void saveFPReg(int Reg) { FPReg = Reg; }
88   int getFPReg() const { return FPReg; }
89 
90   void emitFnStartLocNotes() const {
91     for (Locs::const_iterator FI = FnStartLocs.begin(), FE = FnStartLocs.end();
92          FI != FE; ++FI)
93       Parser.Note(*FI, ".fnstart was specified here");
94   }
95   void emitCantUnwindLocNotes() const {
96     for (Locs::const_iterator UI = CantUnwindLocs.begin(),
97                               UE = CantUnwindLocs.end(); UI != UE; ++UI)
98       Parser.Note(*UI, ".cantunwind was specified here");
99   }
100   void emitHandlerDataLocNotes() const {
101     for (Locs::const_iterator HI = HandlerDataLocs.begin(),
102                               HE = HandlerDataLocs.end(); HI != HE; ++HI)
103       Parser.Note(*HI, ".handlerdata was specified here");
104   }
105   void emitPersonalityLocNotes() const {
106     for (Locs::const_iterator PI = PersonalityLocs.begin(),
107                               PE = PersonalityLocs.end(),
108                               PII = PersonalityIndexLocs.begin(),
109                               PIE = PersonalityIndexLocs.end();
110          PI != PE || PII != PIE;) {
111       if (PI != PE && (PII == PIE || PI->getPointer() < PII->getPointer()))
112         Parser.Note(*PI++, ".personality was specified here");
113       else if (PII != PIE && (PI == PE || PII->getPointer() < PI->getPointer()))
114         Parser.Note(*PII++, ".personalityindex was specified here");
115       else
116         llvm_unreachable(".personality and .personalityindex cannot be "
117                          "at the same location");
118     }
119   }
120 
121   void reset() {
122     FnStartLocs = Locs();
123     CantUnwindLocs = Locs();
124     PersonalityLocs = Locs();
125     HandlerDataLocs = Locs();
126     PersonalityIndexLocs = Locs();
127     FPReg = ARM::SP;
128   }
129 };
130 
131 class ARMAsmParser : public MCTargetAsmParser {
132   const MCInstrInfo &MII;
133   const MCRegisterInfo *MRI;
134   UnwindContext UC;
135 
136   ARMTargetStreamer &getTargetStreamer() {
137     assert(getParser().getStreamer().getTargetStreamer() &&
138            "do not have a target streamer");
139     MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
140     return static_cast<ARMTargetStreamer &>(TS);
141   }
142 
143   // Map of register aliases registers via the .req directive.
144   StringMap<unsigned> RegisterReqs;
145 
146   bool NextSymbolIsThumb;
147 
148   struct {
149     ARMCC::CondCodes Cond;    // Condition for IT block.
150     unsigned Mask:4;          // Condition mask for instructions.
151                               // Starting at first 1 (from lsb).
152                               //   '1'  condition as indicated in IT.
153                               //   '0'  inverse of condition (else).
154                               // Count of instructions in IT block is
155                               // 4 - trailingzeroes(mask)
156 
157     bool FirstCond;           // Explicit flag for when we're parsing the
158                               // First instruction in the IT block. It's
159                               // implied in the mask, so needs special
160                               // handling.
161 
162     unsigned CurPosition;     // Current position in parsing of IT
163                               // block. In range [0,3]. Initialized
164                               // according to count of instructions in block.
165                               // ~0U if no active IT block.
166   } ITState;
167   bool inITBlock() { return ITState.CurPosition != ~0U; }
168   bool lastInITBlock() {
169     return ITState.CurPosition == 4 - countTrailingZeros(ITState.Mask);
170   }
171   void forwardITPosition() {
172     if (!inITBlock()) return;
173     // Move to the next instruction in the IT block, if there is one. If not,
174     // mark the block as done.
175     unsigned TZ = countTrailingZeros(ITState.Mask);
176     if (++ITState.CurPosition == 5 - TZ)
177       ITState.CurPosition = ~0U; // Done with the IT block after this.
178   }
179 
180   void Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges = None) {
181     return getParser().Note(L, Msg, Ranges);
182   }
183   bool Warning(SMLoc L, const Twine &Msg,
184                ArrayRef<SMRange> Ranges = None) {
185     return getParser().Warning(L, Msg, Ranges);
186   }
187   bool Error(SMLoc L, const Twine &Msg,
188              ArrayRef<SMRange> Ranges = None) {
189     return getParser().Error(L, Msg, Ranges);
190   }
191 
192   bool validatetLDMRegList(const MCInst &Inst, const OperandVector &Operands,
193                            unsigned ListNo, bool IsARPop = false);
194   bool validatetSTMRegList(const MCInst &Inst, const OperandVector &Operands,
195                            unsigned ListNo);
196 
197   int tryParseRegister();
198   bool tryParseRegisterWithWriteBack(OperandVector &);
199   int tryParseShiftRegister(OperandVector &);
200   bool parseRegisterList(OperandVector &);
201   bool parseMemory(OperandVector &);
202   bool parseOperand(OperandVector &, StringRef Mnemonic);
203   bool parsePrefix(ARMMCExpr::VariantKind &RefKind);
204   bool parseMemRegOffsetShift(ARM_AM::ShiftOpc &ShiftType,
205                               unsigned &ShiftAmount);
206   bool parseLiteralValues(unsigned Size, SMLoc L);
207   bool parseDirectiveThumb(SMLoc L);
208   bool parseDirectiveARM(SMLoc L);
209   bool parseDirectiveThumbFunc(SMLoc L);
210   bool parseDirectiveCode(SMLoc L);
211   bool parseDirectiveSyntax(SMLoc L);
212   bool parseDirectiveReq(StringRef Name, SMLoc L);
213   bool parseDirectiveUnreq(SMLoc L);
214   bool parseDirectiveArch(SMLoc L);
215   bool parseDirectiveEabiAttr(SMLoc L);
216   bool parseDirectiveCPU(SMLoc L);
217   bool parseDirectiveFPU(SMLoc L);
218   bool parseDirectiveFnStart(SMLoc L);
219   bool parseDirectiveFnEnd(SMLoc L);
220   bool parseDirectiveCantUnwind(SMLoc L);
221   bool parseDirectivePersonality(SMLoc L);
222   bool parseDirectiveHandlerData(SMLoc L);
223   bool parseDirectiveSetFP(SMLoc L);
224   bool parseDirectivePad(SMLoc L);
225   bool parseDirectiveRegSave(SMLoc L, bool IsVector);
226   bool parseDirectiveInst(SMLoc L, char Suffix = '\0');
227   bool parseDirectiveLtorg(SMLoc L);
228   bool parseDirectiveEven(SMLoc L);
229   bool parseDirectivePersonalityIndex(SMLoc L);
230   bool parseDirectiveUnwindRaw(SMLoc L);
231   bool parseDirectiveTLSDescSeq(SMLoc L);
232   bool parseDirectiveMovSP(SMLoc L);
233   bool parseDirectiveObjectArch(SMLoc L);
234   bool parseDirectiveArchExtension(SMLoc L);
235   bool parseDirectiveAlign(SMLoc L);
236   bool parseDirectiveThumbSet(SMLoc L);
237 
238   StringRef splitMnemonic(StringRef Mnemonic, unsigned &PredicationCode,
239                           bool &CarrySetting, unsigned &ProcessorIMod,
240                           StringRef &ITMask);
241   void getMnemonicAcceptInfo(StringRef Mnemonic, StringRef FullInst,
242                              bool &CanAcceptCarrySet,
243                              bool &CanAcceptPredicationCode);
244 
245   void tryConvertingToTwoOperandForm(StringRef Mnemonic, bool CarrySetting,
246                                      OperandVector &Operands);
247   bool isThumb() const {
248     // FIXME: Can tablegen auto-generate this?
249     return getSTI().getFeatureBits()[ARM::ModeThumb];
250   }
251   bool isThumbOne() const {
252     return isThumb() && !getSTI().getFeatureBits()[ARM::FeatureThumb2];
253   }
254   bool isThumbTwo() const {
255     return isThumb() && getSTI().getFeatureBits()[ARM::FeatureThumb2];
256   }
257   bool hasThumb() const {
258     return getSTI().getFeatureBits()[ARM::HasV4TOps];
259   }
260   bool hasThumb2() const {
261     return getSTI().getFeatureBits()[ARM::FeatureThumb2];
262   }
263   bool hasV6Ops() const {
264     return getSTI().getFeatureBits()[ARM::HasV6Ops];
265   }
266   bool hasV6T2Ops() const {
267     return getSTI().getFeatureBits()[ARM::HasV6T2Ops];
268   }
269   bool hasV6MOps() const {
270     return getSTI().getFeatureBits()[ARM::HasV6MOps];
271   }
272   bool hasV7Ops() const {
273     return getSTI().getFeatureBits()[ARM::HasV7Ops];
274   }
275   bool hasV8Ops() const {
276     return getSTI().getFeatureBits()[ARM::HasV8Ops];
277   }
278   bool hasV8MBaseline() const {
279     return getSTI().getFeatureBits()[ARM::HasV8MBaselineOps];
280   }
281   bool hasV8MMainline() const {
282     return getSTI().getFeatureBits()[ARM::HasV8MMainlineOps];
283   }
284   bool has8MSecExt() const {
285     return getSTI().getFeatureBits()[ARM::Feature8MSecExt];
286   }
287   bool hasARM() const {
288     return !getSTI().getFeatureBits()[ARM::FeatureNoARM];
289   }
290   bool hasDSP() const {
291     return getSTI().getFeatureBits()[ARM::FeatureDSP];
292   }
293   bool hasD16() const {
294     return getSTI().getFeatureBits()[ARM::FeatureD16];
295   }
296   bool hasV8_1aOps() const {
297     return getSTI().getFeatureBits()[ARM::HasV8_1aOps];
298   }
299 
300   void SwitchMode() {
301     MCSubtargetInfo &STI = copySTI();
302     uint64_t FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
303     setAvailableFeatures(FB);
304   }
305   void FixModeAfterArchChange(bool WasThumb, SMLoc Loc);
306   bool isMClass() const {
307     return getSTI().getFeatureBits()[ARM::FeatureMClass];
308   }
309 
310   /// @name Auto-generated Match Functions
311   /// {
312 
313 #define GET_ASSEMBLER_HEADER
314 #include "ARMGenAsmMatcher.inc"
315 
316   /// }
317 
318   OperandMatchResultTy parseITCondCode(OperandVector &);
319   OperandMatchResultTy parseCoprocNumOperand(OperandVector &);
320   OperandMatchResultTy parseCoprocRegOperand(OperandVector &);
321   OperandMatchResultTy parseCoprocOptionOperand(OperandVector &);
322   OperandMatchResultTy parseMemBarrierOptOperand(OperandVector &);
323   OperandMatchResultTy parseInstSyncBarrierOptOperand(OperandVector &);
324   OperandMatchResultTy parseProcIFlagsOperand(OperandVector &);
325   OperandMatchResultTy parseMSRMaskOperand(OperandVector &);
326   OperandMatchResultTy parseBankedRegOperand(OperandVector &);
327   OperandMatchResultTy parsePKHImm(OperandVector &O, StringRef Op, int Low,
328                                    int High);
329   OperandMatchResultTy parsePKHLSLImm(OperandVector &O) {
330     return parsePKHImm(O, "lsl", 0, 31);
331   }
332   OperandMatchResultTy parsePKHASRImm(OperandVector &O) {
333     return parsePKHImm(O, "asr", 1, 32);
334   }
335   OperandMatchResultTy parseSetEndImm(OperandVector &);
336   OperandMatchResultTy parseShifterImm(OperandVector &);
337   OperandMatchResultTy parseRotImm(OperandVector &);
338   OperandMatchResultTy parseModImm(OperandVector &);
339   OperandMatchResultTy parseBitfield(OperandVector &);
340   OperandMatchResultTy parsePostIdxReg(OperandVector &);
341   OperandMatchResultTy parseAM3Offset(OperandVector &);
342   OperandMatchResultTy parseFPImm(OperandVector &);
343   OperandMatchResultTy parseVectorList(OperandVector &);
344   OperandMatchResultTy parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index,
345                                        SMLoc &EndLoc);
346 
347   // Asm Match Converter Methods
348   void cvtThumbMultiply(MCInst &Inst, const OperandVector &);
349   void cvtThumbBranches(MCInst &Inst, const OperandVector &);
350 
351   bool validateInstruction(MCInst &Inst, const OperandVector &Ops);
352   bool processInstruction(MCInst &Inst, const OperandVector &Ops, MCStreamer &Out);
353   bool shouldOmitCCOutOperand(StringRef Mnemonic, OperandVector &Operands);
354   bool shouldOmitPredicateOperand(StringRef Mnemonic, OperandVector &Operands);
355 
356 public:
357   enum ARMMatchResultTy {
358     Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
359     Match_RequiresNotITBlock,
360     Match_RequiresV6,
361     Match_RequiresThumb2,
362     Match_RequiresV8,
363 #define GET_OPERAND_DIAGNOSTIC_TYPES
364 #include "ARMGenAsmMatcher.inc"
365 
366   };
367 
368   ARMAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
369                const MCInstrInfo &MII, const MCTargetOptions &Options)
370     : MCTargetAsmParser(Options, STI), MII(MII), UC(Parser) {
371     MCAsmParserExtension::Initialize(Parser);
372 
373     // Cache the MCRegisterInfo.
374     MRI = getContext().getRegisterInfo();
375 
376     // Initialize the set of available features.
377     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
378 
379     // Not in an ITBlock to start with.
380     ITState.CurPosition = ~0U;
381 
382     NextSymbolIsThumb = false;
383   }
384 
385   // Implementation of the MCTargetAsmParser interface:
386   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
387   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
388                         SMLoc NameLoc, OperandVector &Operands) override;
389   bool ParseDirective(AsmToken DirectiveID) override;
390 
391   unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
392                                       unsigned Kind) override;
393   unsigned checkTargetMatchPredicate(MCInst &Inst) override;
394 
395   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
396                                OperandVector &Operands, MCStreamer &Out,
397                                uint64_t &ErrorInfo,
398                                bool MatchingInlineAsm) override;
399   void onLabelParsed(MCSymbol *Symbol) override;
400 };
401 } // end anonymous namespace
402 
403 namespace {
404 
405 /// ARMOperand - Instances of this class represent a parsed ARM machine
406 /// operand.
407 class ARMOperand : public MCParsedAsmOperand {
408   enum KindTy {
409     k_CondCode,
410     k_CCOut,
411     k_ITCondMask,
412     k_CoprocNum,
413     k_CoprocReg,
414     k_CoprocOption,
415     k_Immediate,
416     k_MemBarrierOpt,
417     k_InstSyncBarrierOpt,
418     k_Memory,
419     k_PostIndexRegister,
420     k_MSRMask,
421     k_BankedReg,
422     k_ProcIFlags,
423     k_VectorIndex,
424     k_Register,
425     k_RegisterList,
426     k_DPRRegisterList,
427     k_SPRRegisterList,
428     k_VectorList,
429     k_VectorListAllLanes,
430     k_VectorListIndexed,
431     k_ShiftedRegister,
432     k_ShiftedImmediate,
433     k_ShifterImmediate,
434     k_RotateImmediate,
435     k_ModifiedImmediate,
436     k_ConstantPoolImmediate,
437     k_BitfieldDescriptor,
438     k_Token,
439   } Kind;
440 
441   SMLoc StartLoc, EndLoc, AlignmentLoc;
442   SmallVector<unsigned, 8> Registers;
443 
444   struct CCOp {
445     ARMCC::CondCodes Val;
446   };
447 
448   struct CopOp {
449     unsigned Val;
450   };
451 
452   struct CoprocOptionOp {
453     unsigned Val;
454   };
455 
456   struct ITMaskOp {
457     unsigned Mask:4;
458   };
459 
460   struct MBOptOp {
461     ARM_MB::MemBOpt Val;
462   };
463 
464   struct ISBOptOp {
465     ARM_ISB::InstSyncBOpt Val;
466   };
467 
468   struct IFlagsOp {
469     ARM_PROC::IFlags Val;
470   };
471 
472   struct MMaskOp {
473     unsigned Val;
474   };
475 
476   struct BankedRegOp {
477     unsigned Val;
478   };
479 
480   struct TokOp {
481     const char *Data;
482     unsigned Length;
483   };
484 
485   struct RegOp {
486     unsigned RegNum;
487   };
488 
489   // A vector register list is a sequential list of 1 to 4 registers.
490   struct VectorListOp {
491     unsigned RegNum;
492     unsigned Count;
493     unsigned LaneIndex;
494     bool isDoubleSpaced;
495   };
496 
497   struct VectorIndexOp {
498     unsigned Val;
499   };
500 
501   struct ImmOp {
502     const MCExpr *Val;
503   };
504 
505   /// Combined record for all forms of ARM address expressions.
506   struct MemoryOp {
507     unsigned BaseRegNum;
508     // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
509     // was specified.
510     const MCConstantExpr *OffsetImm;  // Offset immediate value
511     unsigned OffsetRegNum;    // Offset register num, when OffsetImm == NULL
512     ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
513     unsigned ShiftImm;        // shift for OffsetReg.
514     unsigned Alignment;       // 0 = no alignment specified
515     // n = alignment in bytes (2, 4, 8, 16, or 32)
516     unsigned isNegative : 1;  // Negated OffsetReg? (~'U' bit)
517   };
518 
519   struct PostIdxRegOp {
520     unsigned RegNum;
521     bool isAdd;
522     ARM_AM::ShiftOpc ShiftTy;
523     unsigned ShiftImm;
524   };
525 
526   struct ShifterImmOp {
527     bool isASR;
528     unsigned Imm;
529   };
530 
531   struct RegShiftedRegOp {
532     ARM_AM::ShiftOpc ShiftTy;
533     unsigned SrcReg;
534     unsigned ShiftReg;
535     unsigned ShiftImm;
536   };
537 
538   struct RegShiftedImmOp {
539     ARM_AM::ShiftOpc ShiftTy;
540     unsigned SrcReg;
541     unsigned ShiftImm;
542   };
543 
544   struct RotImmOp {
545     unsigned Imm;
546   };
547 
548   struct ModImmOp {
549     unsigned Bits;
550     unsigned Rot;
551   };
552 
553   struct BitfieldOp {
554     unsigned LSB;
555     unsigned Width;
556   };
557 
558   union {
559     struct CCOp CC;
560     struct CopOp Cop;
561     struct CoprocOptionOp CoprocOption;
562     struct MBOptOp MBOpt;
563     struct ISBOptOp ISBOpt;
564     struct ITMaskOp ITMask;
565     struct IFlagsOp IFlags;
566     struct MMaskOp MMask;
567     struct BankedRegOp BankedReg;
568     struct TokOp Tok;
569     struct RegOp Reg;
570     struct VectorListOp VectorList;
571     struct VectorIndexOp VectorIndex;
572     struct ImmOp Imm;
573     struct MemoryOp Memory;
574     struct PostIdxRegOp PostIdxReg;
575     struct ShifterImmOp ShifterImm;
576     struct RegShiftedRegOp RegShiftedReg;
577     struct RegShiftedImmOp RegShiftedImm;
578     struct RotImmOp RotImm;
579     struct ModImmOp ModImm;
580     struct BitfieldOp Bitfield;
581   };
582 
583 public:
584   ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
585 
586   /// getStartLoc - Get the location of the first token of this operand.
587   SMLoc getStartLoc() const override { return StartLoc; }
588   /// getEndLoc - Get the location of the last token of this operand.
589   SMLoc getEndLoc() const override { return EndLoc; }
590   /// getLocRange - Get the range between the first and last token of this
591   /// operand.
592   SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
593 
594   /// getAlignmentLoc - Get the location of the Alignment token of this operand.
595   SMLoc getAlignmentLoc() const {
596     assert(Kind == k_Memory && "Invalid access!");
597     return AlignmentLoc;
598   }
599 
600   ARMCC::CondCodes getCondCode() const {
601     assert(Kind == k_CondCode && "Invalid access!");
602     return CC.Val;
603   }
604 
605   unsigned getCoproc() const {
606     assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
607     return Cop.Val;
608   }
609 
610   StringRef getToken() const {
611     assert(Kind == k_Token && "Invalid access!");
612     return StringRef(Tok.Data, Tok.Length);
613   }
614 
615   unsigned getReg() const override {
616     assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
617     return Reg.RegNum;
618   }
619 
620   const SmallVectorImpl<unsigned> &getRegList() const {
621     assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
622             Kind == k_SPRRegisterList) && "Invalid access!");
623     return Registers;
624   }
625 
626   const MCExpr *getImm() const {
627     assert(isImm() && "Invalid access!");
628     return Imm.Val;
629   }
630 
631   const MCExpr *getConstantPoolImm() const {
632     assert(isConstantPoolImm() && "Invalid access!");
633     return Imm.Val;
634   }
635 
636   unsigned getVectorIndex() const {
637     assert(Kind == k_VectorIndex && "Invalid access!");
638     return VectorIndex.Val;
639   }
640 
641   ARM_MB::MemBOpt getMemBarrierOpt() const {
642     assert(Kind == k_MemBarrierOpt && "Invalid access!");
643     return MBOpt.Val;
644   }
645 
646   ARM_ISB::InstSyncBOpt getInstSyncBarrierOpt() const {
647     assert(Kind == k_InstSyncBarrierOpt && "Invalid access!");
648     return ISBOpt.Val;
649   }
650 
651   ARM_PROC::IFlags getProcIFlags() const {
652     assert(Kind == k_ProcIFlags && "Invalid access!");
653     return IFlags.Val;
654   }
655 
656   unsigned getMSRMask() const {
657     assert(Kind == k_MSRMask && "Invalid access!");
658     return MMask.Val;
659   }
660 
661   unsigned getBankedReg() const {
662     assert(Kind == k_BankedReg && "Invalid access!");
663     return BankedReg.Val;
664   }
665 
666   bool isCoprocNum() const { return Kind == k_CoprocNum; }
667   bool isCoprocReg() const { return Kind == k_CoprocReg; }
668   bool isCoprocOption() const { return Kind == k_CoprocOption; }
669   bool isCondCode() const { return Kind == k_CondCode; }
670   bool isCCOut() const { return Kind == k_CCOut; }
671   bool isITMask() const { return Kind == k_ITCondMask; }
672   bool isITCondCode() const { return Kind == k_CondCode; }
673   bool isImm() const override {
674     return Kind == k_Immediate;
675   }
676   // checks whether this operand is an unsigned offset which fits is a field
677   // of specified width and scaled by a specific number of bits
678   template<unsigned width, unsigned scale>
679   bool isUnsignedOffset() const {
680     if (!isImm()) return false;
681     if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
682     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
683       int64_t Val = CE->getValue();
684       int64_t Align = 1LL << scale;
685       int64_t Max = Align * ((1LL << width) - 1);
686       return ((Val % Align) == 0) && (Val >= 0) && (Val <= Max);
687     }
688     return false;
689   }
690   // checks whether this operand is an signed offset which fits is a field
691   // of specified width and scaled by a specific number of bits
692   template<unsigned width, unsigned scale>
693   bool isSignedOffset() const {
694     if (!isImm()) return false;
695     if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
696     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
697       int64_t Val = CE->getValue();
698       int64_t Align = 1LL << scale;
699       int64_t Max = Align * ((1LL << (width-1)) - 1);
700       int64_t Min = -Align * (1LL << (width-1));
701       return ((Val % Align) == 0) && (Val >= Min) && (Val <= Max);
702     }
703     return false;
704   }
705 
706   // checks whether this operand is a memory operand computed as an offset
707   // applied to PC. the offset may have 8 bits of magnitude and is represented
708   // with two bits of shift. textually it may be either [pc, #imm], #imm or
709   // relocable expression...
710   bool isThumbMemPC() const {
711     int64_t Val = 0;
712     if (isImm()) {
713       if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
714       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val);
715       if (!CE) return false;
716       Val = CE->getValue();
717     }
718     else if (isMem()) {
719       if(!Memory.OffsetImm || Memory.OffsetRegNum) return false;
720       if(Memory.BaseRegNum != ARM::PC) return false;
721       Val = Memory.OffsetImm->getValue();
722     }
723     else return false;
724     return ((Val % 4) == 0) && (Val >= 0) && (Val <= 1020);
725   }
726   bool isFPImm() const {
727     if (!isImm()) return false;
728     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
729     if (!CE) return false;
730     int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
731     return Val != -1;
732   }
733   bool isFBits16() const {
734     if (!isImm()) return false;
735     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
736     if (!CE) return false;
737     int64_t Value = CE->getValue();
738     return Value >= 0 && Value <= 16;
739   }
740   bool isFBits32() const {
741     if (!isImm()) return false;
742     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
743     if (!CE) return false;
744     int64_t Value = CE->getValue();
745     return Value >= 1 && Value <= 32;
746   }
747   bool isImm8s4() const {
748     if (!isImm()) return false;
749     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
750     if (!CE) return false;
751     int64_t Value = CE->getValue();
752     return ((Value & 3) == 0) && Value >= -1020 && Value <= 1020;
753   }
754   bool isImm0_1020s4() const {
755     if (!isImm()) return false;
756     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
757     if (!CE) return false;
758     int64_t Value = CE->getValue();
759     return ((Value & 3) == 0) && Value >= 0 && Value <= 1020;
760   }
761   bool isImm0_508s4() const {
762     if (!isImm()) return false;
763     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
764     if (!CE) return false;
765     int64_t Value = CE->getValue();
766     return ((Value & 3) == 0) && Value >= 0 && Value <= 508;
767   }
768   bool isImm0_508s4Neg() const {
769     if (!isImm()) return false;
770     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
771     if (!CE) return false;
772     int64_t Value = -CE->getValue();
773     // explicitly exclude zero. we want that to use the normal 0_508 version.
774     return ((Value & 3) == 0) && Value > 0 && Value <= 508;
775   }
776   bool isImm0_239() const {
777     if (!isImm()) return false;
778     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
779     if (!CE) return false;
780     int64_t Value = CE->getValue();
781     return Value >= 0 && Value < 240;
782   }
783   bool isImm0_255() const {
784     if (!isImm()) return false;
785     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
786     if (!CE) return false;
787     int64_t Value = CE->getValue();
788     return Value >= 0 && Value < 256;
789   }
790   bool isImm0_4095() const {
791     if (!isImm()) return false;
792     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
793     if (!CE) return false;
794     int64_t Value = CE->getValue();
795     return Value >= 0 && Value < 4096;
796   }
797   bool isImm0_4095Neg() const {
798     if (!isImm()) return false;
799     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
800     if (!CE) return false;
801     int64_t Value = -CE->getValue();
802     return Value > 0 && Value < 4096;
803   }
804   bool isImm0_1() const {
805     if (!isImm()) return false;
806     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
807     if (!CE) return false;
808     int64_t Value = CE->getValue();
809     return Value >= 0 && Value < 2;
810   }
811   bool isImm0_3() const {
812     if (!isImm()) return false;
813     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
814     if (!CE) return false;
815     int64_t Value = CE->getValue();
816     return Value >= 0 && Value < 4;
817   }
818   bool isImm0_7() const {
819     if (!isImm()) return false;
820     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
821     if (!CE) return false;
822     int64_t Value = CE->getValue();
823     return Value >= 0 && Value < 8;
824   }
825   bool isImm0_15() const {
826     if (!isImm()) return false;
827     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
828     if (!CE) return false;
829     int64_t Value = CE->getValue();
830     return Value >= 0 && Value < 16;
831   }
832   bool isImm0_31() const {
833     if (!isImm()) return false;
834     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
835     if (!CE) return false;
836     int64_t Value = CE->getValue();
837     return Value >= 0 && Value < 32;
838   }
839   bool isImm0_63() const {
840     if (!isImm()) return false;
841     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
842     if (!CE) return false;
843     int64_t Value = CE->getValue();
844     return Value >= 0 && Value < 64;
845   }
846   bool isImm8() const {
847     if (!isImm()) return false;
848     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
849     if (!CE) return false;
850     int64_t Value = CE->getValue();
851     return Value == 8;
852   }
853   bool isImm16() const {
854     if (!isImm()) return false;
855     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
856     if (!CE) return false;
857     int64_t Value = CE->getValue();
858     return Value == 16;
859   }
860   bool isImm32() const {
861     if (!isImm()) return false;
862     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
863     if (!CE) return false;
864     int64_t Value = CE->getValue();
865     return Value == 32;
866   }
867   bool isShrImm8() const {
868     if (!isImm()) return false;
869     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
870     if (!CE) return false;
871     int64_t Value = CE->getValue();
872     return Value > 0 && Value <= 8;
873   }
874   bool isShrImm16() const {
875     if (!isImm()) return false;
876     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
877     if (!CE) return false;
878     int64_t Value = CE->getValue();
879     return Value > 0 && Value <= 16;
880   }
881   bool isShrImm32() const {
882     if (!isImm()) return false;
883     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
884     if (!CE) return false;
885     int64_t Value = CE->getValue();
886     return Value > 0 && Value <= 32;
887   }
888   bool isShrImm64() const {
889     if (!isImm()) return false;
890     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
891     if (!CE) return false;
892     int64_t Value = CE->getValue();
893     return Value > 0 && Value <= 64;
894   }
895   bool isImm1_7() const {
896     if (!isImm()) return false;
897     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
898     if (!CE) return false;
899     int64_t Value = CE->getValue();
900     return Value > 0 && Value < 8;
901   }
902   bool isImm1_15() const {
903     if (!isImm()) return false;
904     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
905     if (!CE) return false;
906     int64_t Value = CE->getValue();
907     return Value > 0 && Value < 16;
908   }
909   bool isImm1_31() const {
910     if (!isImm()) return false;
911     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
912     if (!CE) return false;
913     int64_t Value = CE->getValue();
914     return Value > 0 && Value < 32;
915   }
916   bool isImm1_16() const {
917     if (!isImm()) return false;
918     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
919     if (!CE) return false;
920     int64_t Value = CE->getValue();
921     return Value > 0 && Value < 17;
922   }
923   bool isImm1_32() const {
924     if (!isImm()) return false;
925     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
926     if (!CE) return false;
927     int64_t Value = CE->getValue();
928     return Value > 0 && Value < 33;
929   }
930   bool isImm0_32() const {
931     if (!isImm()) return false;
932     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
933     if (!CE) return false;
934     int64_t Value = CE->getValue();
935     return Value >= 0 && Value < 33;
936   }
937   bool isImm0_65535() const {
938     if (!isImm()) return false;
939     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
940     if (!CE) return false;
941     int64_t Value = CE->getValue();
942     return Value >= 0 && Value < 65536;
943   }
944   bool isImm256_65535Expr() const {
945     if (!isImm()) return false;
946     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
947     // If it's not a constant expression, it'll generate a fixup and be
948     // handled later.
949     if (!CE) return true;
950     int64_t Value = CE->getValue();
951     return Value >= 256 && Value < 65536;
952   }
953   bool isImm0_65535Expr() const {
954     if (!isImm()) return false;
955     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
956     // If it's not a constant expression, it'll generate a fixup and be
957     // handled later.
958     if (!CE) return true;
959     int64_t Value = CE->getValue();
960     return Value >= 0 && Value < 65536;
961   }
962   bool isImm24bit() const {
963     if (!isImm()) return false;
964     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
965     if (!CE) return false;
966     int64_t Value = CE->getValue();
967     return Value >= 0 && Value <= 0xffffff;
968   }
969   bool isImmThumbSR() const {
970     if (!isImm()) return false;
971     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
972     if (!CE) return false;
973     int64_t Value = CE->getValue();
974     return Value > 0 && Value < 33;
975   }
976   bool isPKHLSLImm() const {
977     if (!isImm()) return false;
978     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
979     if (!CE) return false;
980     int64_t Value = CE->getValue();
981     return Value >= 0 && Value < 32;
982   }
983   bool isPKHASRImm() const {
984     if (!isImm()) return false;
985     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
986     if (!CE) return false;
987     int64_t Value = CE->getValue();
988     return Value > 0 && Value <= 32;
989   }
990   bool isAdrLabel() const {
991     // If we have an immediate that's not a constant, treat it as a label
992     // reference needing a fixup.
993     if (isImm() && !isa<MCConstantExpr>(getImm()))
994       return true;
995 
996     // If it is a constant, it must fit into a modified immediate encoding.
997     if (!isImm()) return false;
998     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
999     if (!CE) return false;
1000     int64_t Value = CE->getValue();
1001     return (ARM_AM::getSOImmVal(Value) != -1 ||
1002             ARM_AM::getSOImmVal(-Value) != -1);
1003   }
1004   bool isT2SOImm() const {
1005     if (!isImm()) return false;
1006     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1007     if (!CE) return false;
1008     int64_t Value = CE->getValue();
1009     return ARM_AM::getT2SOImmVal(Value) != -1;
1010   }
1011   bool isT2SOImmNot() const {
1012     if (!isImm()) return false;
1013     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1014     if (!CE) return false;
1015     int64_t Value = CE->getValue();
1016     return ARM_AM::getT2SOImmVal(Value) == -1 &&
1017       ARM_AM::getT2SOImmVal(~Value) != -1;
1018   }
1019   bool isT2SOImmNeg() const {
1020     if (!isImm()) return false;
1021     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1022     if (!CE) return false;
1023     int64_t Value = CE->getValue();
1024     // Only use this when not representable as a plain so_imm.
1025     return ARM_AM::getT2SOImmVal(Value) == -1 &&
1026       ARM_AM::getT2SOImmVal(-Value) != -1;
1027   }
1028   bool isSetEndImm() const {
1029     if (!isImm()) return false;
1030     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1031     if (!CE) return false;
1032     int64_t Value = CE->getValue();
1033     return Value == 1 || Value == 0;
1034   }
1035   bool isReg() const override { return Kind == k_Register; }
1036   bool isRegList() const { return Kind == k_RegisterList; }
1037   bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
1038   bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
1039   bool isToken() const override { return Kind == k_Token; }
1040   bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
1041   bool isInstSyncBarrierOpt() const { return Kind == k_InstSyncBarrierOpt; }
1042   bool isMem() const override { return Kind == k_Memory; }
1043   bool isShifterImm() const { return Kind == k_ShifterImmediate; }
1044   bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
1045   bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
1046   bool isRotImm() const { return Kind == k_RotateImmediate; }
1047   bool isModImm() const { return Kind == k_ModifiedImmediate; }
1048   bool isModImmNot() const {
1049     if (!isImm()) return false;
1050     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1051     if (!CE) return false;
1052     int64_t Value = CE->getValue();
1053     return ARM_AM::getSOImmVal(~Value) != -1;
1054   }
1055   bool isModImmNeg() const {
1056     if (!isImm()) return false;
1057     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1058     if (!CE) return false;
1059     int64_t Value = CE->getValue();
1060     return ARM_AM::getSOImmVal(Value) == -1 &&
1061       ARM_AM::getSOImmVal(-Value) != -1;
1062   }
1063   bool isConstantPoolImm() const { return Kind == k_ConstantPoolImmediate; }
1064   bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
1065   bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
1066   bool isPostIdxReg() const {
1067     return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy ==ARM_AM::no_shift;
1068   }
1069   bool isMemNoOffset(bool alignOK = false, unsigned Alignment = 0) const {
1070     if (!isMem())
1071       return false;
1072     // No offset of any kind.
1073     return Memory.OffsetRegNum == 0 && Memory.OffsetImm == nullptr &&
1074      (alignOK || Memory.Alignment == Alignment);
1075   }
1076   bool isMemPCRelImm12() const {
1077     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1078       return false;
1079     // Base register must be PC.
1080     if (Memory.BaseRegNum != ARM::PC)
1081       return false;
1082     // Immediate offset in range [-4095, 4095].
1083     if (!Memory.OffsetImm) return true;
1084     int64_t Val = Memory.OffsetImm->getValue();
1085     return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
1086   }
1087   bool isAlignedMemory() const {
1088     return isMemNoOffset(true);
1089   }
1090   bool isAlignedMemoryNone() const {
1091     return isMemNoOffset(false, 0);
1092   }
1093   bool isDupAlignedMemoryNone() const {
1094     return isMemNoOffset(false, 0);
1095   }
1096   bool isAlignedMemory16() const {
1097     if (isMemNoOffset(false, 2)) // alignment in bytes for 16-bits is 2.
1098       return true;
1099     return isMemNoOffset(false, 0);
1100   }
1101   bool isDupAlignedMemory16() const {
1102     if (isMemNoOffset(false, 2)) // alignment in bytes for 16-bits is 2.
1103       return true;
1104     return isMemNoOffset(false, 0);
1105   }
1106   bool isAlignedMemory32() const {
1107     if (isMemNoOffset(false, 4)) // alignment in bytes for 32-bits is 4.
1108       return true;
1109     return isMemNoOffset(false, 0);
1110   }
1111   bool isDupAlignedMemory32() const {
1112     if (isMemNoOffset(false, 4)) // alignment in bytes for 32-bits is 4.
1113       return true;
1114     return isMemNoOffset(false, 0);
1115   }
1116   bool isAlignedMemory64() const {
1117     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1118       return true;
1119     return isMemNoOffset(false, 0);
1120   }
1121   bool isDupAlignedMemory64() const {
1122     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1123       return true;
1124     return isMemNoOffset(false, 0);
1125   }
1126   bool isAlignedMemory64or128() const {
1127     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1128       return true;
1129     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1130       return true;
1131     return isMemNoOffset(false, 0);
1132   }
1133   bool isDupAlignedMemory64or128() const {
1134     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1135       return true;
1136     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1137       return true;
1138     return isMemNoOffset(false, 0);
1139   }
1140   bool isAlignedMemory64or128or256() const {
1141     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1142       return true;
1143     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1144       return true;
1145     if (isMemNoOffset(false, 32)) // alignment in bytes for 256-bits is 32.
1146       return true;
1147     return isMemNoOffset(false, 0);
1148   }
1149   bool isAddrMode2() const {
1150     if (!isMem() || Memory.Alignment != 0) return false;
1151     // Check for register offset.
1152     if (Memory.OffsetRegNum) return true;
1153     // Immediate offset in range [-4095, 4095].
1154     if (!Memory.OffsetImm) return true;
1155     int64_t Val = Memory.OffsetImm->getValue();
1156     return Val > -4096 && Val < 4096;
1157   }
1158   bool isAM2OffsetImm() const {
1159     if (!isImm()) return false;
1160     // Immediate offset in range [-4095, 4095].
1161     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1162     if (!CE) return false;
1163     int64_t Val = CE->getValue();
1164     return (Val == INT32_MIN) || (Val > -4096 && Val < 4096);
1165   }
1166   bool isAddrMode3() const {
1167     // If we have an immediate that's not a constant, treat it as a label
1168     // reference needing a fixup. If it is a constant, it's something else
1169     // and we reject it.
1170     if (isImm() && !isa<MCConstantExpr>(getImm()))
1171       return true;
1172     if (!isMem() || Memory.Alignment != 0) return false;
1173     // No shifts are legal for AM3.
1174     if (Memory.ShiftType != ARM_AM::no_shift) return false;
1175     // Check for register offset.
1176     if (Memory.OffsetRegNum) return true;
1177     // Immediate offset in range [-255, 255].
1178     if (!Memory.OffsetImm) return true;
1179     int64_t Val = Memory.OffsetImm->getValue();
1180     // The #-0 offset is encoded as INT32_MIN, and we have to check
1181     // for this too.
1182     return (Val > -256 && Val < 256) || Val == INT32_MIN;
1183   }
1184   bool isAM3Offset() const {
1185     if (Kind != k_Immediate && Kind != k_PostIndexRegister)
1186       return false;
1187     if (Kind == k_PostIndexRegister)
1188       return PostIdxReg.ShiftTy == ARM_AM::no_shift;
1189     // Immediate offset in range [-255, 255].
1190     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1191     if (!CE) return false;
1192     int64_t Val = CE->getValue();
1193     // Special case, #-0 is INT32_MIN.
1194     return (Val > -256 && Val < 256) || Val == INT32_MIN;
1195   }
1196   bool isAddrMode5() const {
1197     // If we have an immediate that's not a constant, treat it as a label
1198     // reference needing a fixup. If it is a constant, it's something else
1199     // and we reject it.
1200     if (isImm() && !isa<MCConstantExpr>(getImm()))
1201       return true;
1202     if (!isMem() || Memory.Alignment != 0) return false;
1203     // Check for register offset.
1204     if (Memory.OffsetRegNum) return false;
1205     // Immediate offset in range [-1020, 1020] and a multiple of 4.
1206     if (!Memory.OffsetImm) return true;
1207     int64_t Val = Memory.OffsetImm->getValue();
1208     return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
1209       Val == INT32_MIN;
1210   }
1211   bool isAddrMode5FP16() const {
1212     // If we have an immediate that's not a constant, treat it as a label
1213     // reference needing a fixup. If it is a constant, it's something else
1214     // and we reject it.
1215     if (isImm() && !isa<MCConstantExpr>(getImm()))
1216       return true;
1217     if (!isMem() || Memory.Alignment != 0) return false;
1218     // Check for register offset.
1219     if (Memory.OffsetRegNum) return false;
1220     // Immediate offset in range [-510, 510] and a multiple of 2.
1221     if (!Memory.OffsetImm) return true;
1222     int64_t Val = Memory.OffsetImm->getValue();
1223     return (Val >= -510 && Val <= 510 && ((Val & 1) == 0)) || Val == INT32_MIN;
1224   }
1225   bool isMemTBB() const {
1226     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1227         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
1228       return false;
1229     return true;
1230   }
1231   bool isMemTBH() const {
1232     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1233         Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
1234         Memory.Alignment != 0 )
1235       return false;
1236     return true;
1237   }
1238   bool isMemRegOffset() const {
1239     if (!isMem() || !Memory.OffsetRegNum || Memory.Alignment != 0)
1240       return false;
1241     return true;
1242   }
1243   bool isT2MemRegOffset() const {
1244     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1245         Memory.Alignment != 0 || Memory.BaseRegNum == ARM::PC)
1246       return false;
1247     // Only lsl #{0, 1, 2, 3} allowed.
1248     if (Memory.ShiftType == ARM_AM::no_shift)
1249       return true;
1250     if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
1251       return false;
1252     return true;
1253   }
1254   bool isMemThumbRR() const {
1255     // Thumb reg+reg addressing is simple. Just two registers, a base and
1256     // an offset. No shifts, negations or any other complicating factors.
1257     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1258         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
1259       return false;
1260     return isARMLowRegister(Memory.BaseRegNum) &&
1261       (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
1262   }
1263   bool isMemThumbRIs4() const {
1264     if (!isMem() || Memory.OffsetRegNum != 0 ||
1265         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1266       return false;
1267     // Immediate offset, multiple of 4 in range [0, 124].
1268     if (!Memory.OffsetImm) return true;
1269     int64_t Val = Memory.OffsetImm->getValue();
1270     return Val >= 0 && Val <= 124 && (Val % 4) == 0;
1271   }
1272   bool isMemThumbRIs2() const {
1273     if (!isMem() || Memory.OffsetRegNum != 0 ||
1274         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1275       return false;
1276     // Immediate offset, multiple of 4 in range [0, 62].
1277     if (!Memory.OffsetImm) return true;
1278     int64_t Val = Memory.OffsetImm->getValue();
1279     return Val >= 0 && Val <= 62 && (Val % 2) == 0;
1280   }
1281   bool isMemThumbRIs1() const {
1282     if (!isMem() || Memory.OffsetRegNum != 0 ||
1283         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1284       return false;
1285     // Immediate offset in range [0, 31].
1286     if (!Memory.OffsetImm) return true;
1287     int64_t Val = Memory.OffsetImm->getValue();
1288     return Val >= 0 && Val <= 31;
1289   }
1290   bool isMemThumbSPI() const {
1291     if (!isMem() || Memory.OffsetRegNum != 0 ||
1292         Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
1293       return false;
1294     // Immediate offset, multiple of 4 in range [0, 1020].
1295     if (!Memory.OffsetImm) return true;
1296     int64_t Val = Memory.OffsetImm->getValue();
1297     return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
1298   }
1299   bool isMemImm8s4Offset() const {
1300     // If we have an immediate that's not a constant, treat it as a label
1301     // reference needing a fixup. If it is a constant, it's something else
1302     // and we reject it.
1303     if (isImm() && !isa<MCConstantExpr>(getImm()))
1304       return true;
1305     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1306       return false;
1307     // Immediate offset a multiple of 4 in range [-1020, 1020].
1308     if (!Memory.OffsetImm) return true;
1309     int64_t Val = Memory.OffsetImm->getValue();
1310     // Special case, #-0 is INT32_MIN.
1311     return (Val >= -1020 && Val <= 1020 && (Val & 3) == 0) || Val == INT32_MIN;
1312   }
1313   bool isMemImm0_1020s4Offset() const {
1314     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1315       return false;
1316     // Immediate offset a multiple of 4 in range [0, 1020].
1317     if (!Memory.OffsetImm) return true;
1318     int64_t Val = Memory.OffsetImm->getValue();
1319     return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
1320   }
1321   bool isMemImm8Offset() const {
1322     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1323       return false;
1324     // Base reg of PC isn't allowed for these encodings.
1325     if (Memory.BaseRegNum == ARM::PC) return false;
1326     // Immediate offset in range [-255, 255].
1327     if (!Memory.OffsetImm) return true;
1328     int64_t Val = Memory.OffsetImm->getValue();
1329     return (Val == INT32_MIN) || (Val > -256 && Val < 256);
1330   }
1331   bool isMemPosImm8Offset() const {
1332     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1333       return false;
1334     // Immediate offset in range [0, 255].
1335     if (!Memory.OffsetImm) return true;
1336     int64_t Val = Memory.OffsetImm->getValue();
1337     return Val >= 0 && Val < 256;
1338   }
1339   bool isMemNegImm8Offset() const {
1340     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1341       return false;
1342     // Base reg of PC isn't allowed for these encodings.
1343     if (Memory.BaseRegNum == ARM::PC) return false;
1344     // Immediate offset in range [-255, -1].
1345     if (!Memory.OffsetImm) return false;
1346     int64_t Val = Memory.OffsetImm->getValue();
1347     return (Val == INT32_MIN) || (Val > -256 && Val < 0);
1348   }
1349   bool isMemUImm12Offset() const {
1350     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1351       return false;
1352     // Immediate offset in range [0, 4095].
1353     if (!Memory.OffsetImm) return true;
1354     int64_t Val = Memory.OffsetImm->getValue();
1355     return (Val >= 0 && Val < 4096);
1356   }
1357   bool isMemImm12Offset() const {
1358     // If we have an immediate that's not a constant, treat it as a label
1359     // reference needing a fixup. If it is a constant, it's something else
1360     // and we reject it.
1361 
1362     if (isImm() && !isa<MCConstantExpr>(getImm()))
1363       return true;
1364 
1365     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1366       return false;
1367     // Immediate offset in range [-4095, 4095].
1368     if (!Memory.OffsetImm) return true;
1369     int64_t Val = Memory.OffsetImm->getValue();
1370     return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
1371   }
1372   bool isConstPoolAsmImm() const {
1373     // Delay processing of Constant Pool Immediate, this will turn into
1374     // a constant. Match no other operand
1375     return (isConstantPoolImm());
1376   }
1377   bool isPostIdxImm8() const {
1378     if (!isImm()) return false;
1379     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1380     if (!CE) return false;
1381     int64_t Val = CE->getValue();
1382     return (Val > -256 && Val < 256) || (Val == INT32_MIN);
1383   }
1384   bool isPostIdxImm8s4() const {
1385     if (!isImm()) return false;
1386     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1387     if (!CE) return false;
1388     int64_t Val = CE->getValue();
1389     return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
1390       (Val == INT32_MIN);
1391   }
1392 
1393   bool isMSRMask() const { return Kind == k_MSRMask; }
1394   bool isBankedReg() const { return Kind == k_BankedReg; }
1395   bool isProcIFlags() const { return Kind == k_ProcIFlags; }
1396 
1397   // NEON operands.
1398   bool isSingleSpacedVectorList() const {
1399     return Kind == k_VectorList && !VectorList.isDoubleSpaced;
1400   }
1401   bool isDoubleSpacedVectorList() const {
1402     return Kind == k_VectorList && VectorList.isDoubleSpaced;
1403   }
1404   bool isVecListOneD() const {
1405     if (!isSingleSpacedVectorList()) return false;
1406     return VectorList.Count == 1;
1407   }
1408 
1409   bool isVecListDPair() const {
1410     if (!isSingleSpacedVectorList()) return false;
1411     return (ARMMCRegisterClasses[ARM::DPairRegClassID]
1412               .contains(VectorList.RegNum));
1413   }
1414 
1415   bool isVecListThreeD() const {
1416     if (!isSingleSpacedVectorList()) return false;
1417     return VectorList.Count == 3;
1418   }
1419 
1420   bool isVecListFourD() const {
1421     if (!isSingleSpacedVectorList()) return false;
1422     return VectorList.Count == 4;
1423   }
1424 
1425   bool isVecListDPairSpaced() const {
1426     if (Kind != k_VectorList) return false;
1427     if (isSingleSpacedVectorList()) return false;
1428     return (ARMMCRegisterClasses[ARM::DPairSpcRegClassID]
1429               .contains(VectorList.RegNum));
1430   }
1431 
1432   bool isVecListThreeQ() const {
1433     if (!isDoubleSpacedVectorList()) return false;
1434     return VectorList.Count == 3;
1435   }
1436 
1437   bool isVecListFourQ() const {
1438     if (!isDoubleSpacedVectorList()) return false;
1439     return VectorList.Count == 4;
1440   }
1441 
1442   bool isSingleSpacedVectorAllLanes() const {
1443     return Kind == k_VectorListAllLanes && !VectorList.isDoubleSpaced;
1444   }
1445   bool isDoubleSpacedVectorAllLanes() const {
1446     return Kind == k_VectorListAllLanes && VectorList.isDoubleSpaced;
1447   }
1448   bool isVecListOneDAllLanes() const {
1449     if (!isSingleSpacedVectorAllLanes()) return false;
1450     return VectorList.Count == 1;
1451   }
1452 
1453   bool isVecListDPairAllLanes() const {
1454     if (!isSingleSpacedVectorAllLanes()) return false;
1455     return (ARMMCRegisterClasses[ARM::DPairRegClassID]
1456               .contains(VectorList.RegNum));
1457   }
1458 
1459   bool isVecListDPairSpacedAllLanes() const {
1460     if (!isDoubleSpacedVectorAllLanes()) return false;
1461     return VectorList.Count == 2;
1462   }
1463 
1464   bool isVecListThreeDAllLanes() const {
1465     if (!isSingleSpacedVectorAllLanes()) return false;
1466     return VectorList.Count == 3;
1467   }
1468 
1469   bool isVecListThreeQAllLanes() const {
1470     if (!isDoubleSpacedVectorAllLanes()) return false;
1471     return VectorList.Count == 3;
1472   }
1473 
1474   bool isVecListFourDAllLanes() const {
1475     if (!isSingleSpacedVectorAllLanes()) return false;
1476     return VectorList.Count == 4;
1477   }
1478 
1479   bool isVecListFourQAllLanes() const {
1480     if (!isDoubleSpacedVectorAllLanes()) return false;
1481     return VectorList.Count == 4;
1482   }
1483 
1484   bool isSingleSpacedVectorIndexed() const {
1485     return Kind == k_VectorListIndexed && !VectorList.isDoubleSpaced;
1486   }
1487   bool isDoubleSpacedVectorIndexed() const {
1488     return Kind == k_VectorListIndexed && VectorList.isDoubleSpaced;
1489   }
1490   bool isVecListOneDByteIndexed() const {
1491     if (!isSingleSpacedVectorIndexed()) return false;
1492     return VectorList.Count == 1 && VectorList.LaneIndex <= 7;
1493   }
1494 
1495   bool isVecListOneDHWordIndexed() const {
1496     if (!isSingleSpacedVectorIndexed()) return false;
1497     return VectorList.Count == 1 && VectorList.LaneIndex <= 3;
1498   }
1499 
1500   bool isVecListOneDWordIndexed() const {
1501     if (!isSingleSpacedVectorIndexed()) return false;
1502     return VectorList.Count == 1 && VectorList.LaneIndex <= 1;
1503   }
1504 
1505   bool isVecListTwoDByteIndexed() const {
1506     if (!isSingleSpacedVectorIndexed()) return false;
1507     return VectorList.Count == 2 && VectorList.LaneIndex <= 7;
1508   }
1509 
1510   bool isVecListTwoDHWordIndexed() const {
1511     if (!isSingleSpacedVectorIndexed()) return false;
1512     return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
1513   }
1514 
1515   bool isVecListTwoQWordIndexed() const {
1516     if (!isDoubleSpacedVectorIndexed()) return false;
1517     return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
1518   }
1519 
1520   bool isVecListTwoQHWordIndexed() const {
1521     if (!isDoubleSpacedVectorIndexed()) return false;
1522     return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
1523   }
1524 
1525   bool isVecListTwoDWordIndexed() const {
1526     if (!isSingleSpacedVectorIndexed()) return false;
1527     return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
1528   }
1529 
1530   bool isVecListThreeDByteIndexed() const {
1531     if (!isSingleSpacedVectorIndexed()) return false;
1532     return VectorList.Count == 3 && VectorList.LaneIndex <= 7;
1533   }
1534 
1535   bool isVecListThreeDHWordIndexed() const {
1536     if (!isSingleSpacedVectorIndexed()) return false;
1537     return VectorList.Count == 3 && VectorList.LaneIndex <= 3;
1538   }
1539 
1540   bool isVecListThreeQWordIndexed() const {
1541     if (!isDoubleSpacedVectorIndexed()) return false;
1542     return VectorList.Count == 3 && VectorList.LaneIndex <= 1;
1543   }
1544 
1545   bool isVecListThreeQHWordIndexed() const {
1546     if (!isDoubleSpacedVectorIndexed()) return false;
1547     return VectorList.Count == 3 && VectorList.LaneIndex <= 3;
1548   }
1549 
1550   bool isVecListThreeDWordIndexed() const {
1551     if (!isSingleSpacedVectorIndexed()) return false;
1552     return VectorList.Count == 3 && VectorList.LaneIndex <= 1;
1553   }
1554 
1555   bool isVecListFourDByteIndexed() const {
1556     if (!isSingleSpacedVectorIndexed()) return false;
1557     return VectorList.Count == 4 && VectorList.LaneIndex <= 7;
1558   }
1559 
1560   bool isVecListFourDHWordIndexed() const {
1561     if (!isSingleSpacedVectorIndexed()) return false;
1562     return VectorList.Count == 4 && VectorList.LaneIndex <= 3;
1563   }
1564 
1565   bool isVecListFourQWordIndexed() const {
1566     if (!isDoubleSpacedVectorIndexed()) return false;
1567     return VectorList.Count == 4 && VectorList.LaneIndex <= 1;
1568   }
1569 
1570   bool isVecListFourQHWordIndexed() const {
1571     if (!isDoubleSpacedVectorIndexed()) return false;
1572     return VectorList.Count == 4 && VectorList.LaneIndex <= 3;
1573   }
1574 
1575   bool isVecListFourDWordIndexed() const {
1576     if (!isSingleSpacedVectorIndexed()) return false;
1577     return VectorList.Count == 4 && VectorList.LaneIndex <= 1;
1578   }
1579 
1580   bool isVectorIndex8() const {
1581     if (Kind != k_VectorIndex) return false;
1582     return VectorIndex.Val < 8;
1583   }
1584   bool isVectorIndex16() const {
1585     if (Kind != k_VectorIndex) return false;
1586     return VectorIndex.Val < 4;
1587   }
1588   bool isVectorIndex32() const {
1589     if (Kind != k_VectorIndex) return false;
1590     return VectorIndex.Val < 2;
1591   }
1592 
1593   bool isNEONi8splat() const {
1594     if (!isImm()) return false;
1595     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1596     // Must be a constant.
1597     if (!CE) return false;
1598     int64_t Value = CE->getValue();
1599     // i8 value splatted across 8 bytes. The immediate is just the 8 byte
1600     // value.
1601     return Value >= 0 && Value < 256;
1602   }
1603 
1604   bool isNEONi16splat() const {
1605     if (isNEONByteReplicate(2))
1606       return false; // Leave that for bytes replication and forbid by default.
1607     if (!isImm())
1608       return false;
1609     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1610     // Must be a constant.
1611     if (!CE) return false;
1612     unsigned Value = CE->getValue();
1613     return ARM_AM::isNEONi16splat(Value);
1614   }
1615 
1616   bool isNEONi16splatNot() const {
1617     if (!isImm())
1618       return false;
1619     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1620     // Must be a constant.
1621     if (!CE) return false;
1622     unsigned Value = CE->getValue();
1623     return ARM_AM::isNEONi16splat(~Value & 0xffff);
1624   }
1625 
1626   bool isNEONi32splat() const {
1627     if (isNEONByteReplicate(4))
1628       return false; // Leave that for bytes replication and forbid by default.
1629     if (!isImm())
1630       return false;
1631     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1632     // Must be a constant.
1633     if (!CE) return false;
1634     unsigned Value = CE->getValue();
1635     return ARM_AM::isNEONi32splat(Value);
1636   }
1637 
1638   bool isNEONi32splatNot() const {
1639     if (!isImm())
1640       return false;
1641     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1642     // Must be a constant.
1643     if (!CE) return false;
1644     unsigned Value = CE->getValue();
1645     return ARM_AM::isNEONi32splat(~Value);
1646   }
1647 
1648   bool isNEONByteReplicate(unsigned NumBytes) const {
1649     if (!isImm())
1650       return false;
1651     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1652     // Must be a constant.
1653     if (!CE)
1654       return false;
1655     int64_t Value = CE->getValue();
1656     if (!Value)
1657       return false; // Don't bother with zero.
1658 
1659     unsigned char B = Value & 0xff;
1660     for (unsigned i = 1; i < NumBytes; ++i) {
1661       Value >>= 8;
1662       if ((Value & 0xff) != B)
1663         return false;
1664     }
1665     return true;
1666   }
1667   bool isNEONi16ByteReplicate() const { return isNEONByteReplicate(2); }
1668   bool isNEONi32ByteReplicate() const { return isNEONByteReplicate(4); }
1669   bool isNEONi32vmov() const {
1670     if (isNEONByteReplicate(4))
1671       return false; // Let it to be classified as byte-replicate case.
1672     if (!isImm())
1673       return false;
1674     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1675     // Must be a constant.
1676     if (!CE)
1677       return false;
1678     int64_t Value = CE->getValue();
1679     // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1680     // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1681     // FIXME: This is probably wrong and a copy and paste from previous example
1682     return (Value >= 0 && Value < 256) ||
1683       (Value >= 0x0100 && Value <= 0xff00) ||
1684       (Value >= 0x010000 && Value <= 0xff0000) ||
1685       (Value >= 0x01000000 && Value <= 0xff000000) ||
1686       (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1687       (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1688   }
1689   bool isNEONi32vmovNeg() const {
1690     if (!isImm()) return false;
1691     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1692     // Must be a constant.
1693     if (!CE) return false;
1694     int64_t Value = ~CE->getValue();
1695     // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1696     // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1697     // FIXME: This is probably wrong and a copy and paste from previous example
1698     return (Value >= 0 && Value < 256) ||
1699       (Value >= 0x0100 && Value <= 0xff00) ||
1700       (Value >= 0x010000 && Value <= 0xff0000) ||
1701       (Value >= 0x01000000 && Value <= 0xff000000) ||
1702       (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1703       (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1704   }
1705 
1706   bool isNEONi64splat() const {
1707     if (!isImm()) return false;
1708     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1709     // Must be a constant.
1710     if (!CE) return false;
1711     uint64_t Value = CE->getValue();
1712     // i64 value with each byte being either 0 or 0xff.
1713     for (unsigned i = 0; i < 8; ++i)
1714       if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
1715     return true;
1716   }
1717 
1718   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
1719     // Add as immediates when possible.  Null MCExpr = 0.
1720     if (!Expr)
1721       Inst.addOperand(MCOperand::createImm(0));
1722     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
1723       Inst.addOperand(MCOperand::createImm(CE->getValue()));
1724     else
1725       Inst.addOperand(MCOperand::createExpr(Expr));
1726   }
1727 
1728   void addCondCodeOperands(MCInst &Inst, unsigned N) const {
1729     assert(N == 2 && "Invalid number of operands!");
1730     Inst.addOperand(MCOperand::createImm(unsigned(getCondCode())));
1731     unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
1732     Inst.addOperand(MCOperand::createReg(RegNum));
1733   }
1734 
1735   void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
1736     assert(N == 1 && "Invalid number of operands!");
1737     Inst.addOperand(MCOperand::createImm(getCoproc()));
1738   }
1739 
1740   void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
1741     assert(N == 1 && "Invalid number of operands!");
1742     Inst.addOperand(MCOperand::createImm(getCoproc()));
1743   }
1744 
1745   void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
1746     assert(N == 1 && "Invalid number of operands!");
1747     Inst.addOperand(MCOperand::createImm(CoprocOption.Val));
1748   }
1749 
1750   void addITMaskOperands(MCInst &Inst, unsigned N) const {
1751     assert(N == 1 && "Invalid number of operands!");
1752     Inst.addOperand(MCOperand::createImm(ITMask.Mask));
1753   }
1754 
1755   void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
1756     assert(N == 1 && "Invalid number of operands!");
1757     Inst.addOperand(MCOperand::createImm(unsigned(getCondCode())));
1758   }
1759 
1760   void addCCOutOperands(MCInst &Inst, unsigned N) const {
1761     assert(N == 1 && "Invalid number of operands!");
1762     Inst.addOperand(MCOperand::createReg(getReg()));
1763   }
1764 
1765   void addRegOperands(MCInst &Inst, unsigned N) const {
1766     assert(N == 1 && "Invalid number of operands!");
1767     Inst.addOperand(MCOperand::createReg(getReg()));
1768   }
1769 
1770   void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
1771     assert(N == 3 && "Invalid number of operands!");
1772     assert(isRegShiftedReg() &&
1773            "addRegShiftedRegOperands() on non-RegShiftedReg!");
1774     Inst.addOperand(MCOperand::createReg(RegShiftedReg.SrcReg));
1775     Inst.addOperand(MCOperand::createReg(RegShiftedReg.ShiftReg));
1776     Inst.addOperand(MCOperand::createImm(
1777       ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
1778   }
1779 
1780   void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
1781     assert(N == 2 && "Invalid number of operands!");
1782     assert(isRegShiftedImm() &&
1783            "addRegShiftedImmOperands() on non-RegShiftedImm!");
1784     Inst.addOperand(MCOperand::createReg(RegShiftedImm.SrcReg));
1785     // Shift of #32 is encoded as 0 where permitted
1786     unsigned Imm = (RegShiftedImm.ShiftImm == 32 ? 0 : RegShiftedImm.ShiftImm);
1787     Inst.addOperand(MCOperand::createImm(
1788       ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, Imm)));
1789   }
1790 
1791   void addShifterImmOperands(MCInst &Inst, unsigned N) const {
1792     assert(N == 1 && "Invalid number of operands!");
1793     Inst.addOperand(MCOperand::createImm((ShifterImm.isASR << 5) |
1794                                          ShifterImm.Imm));
1795   }
1796 
1797   void addRegListOperands(MCInst &Inst, unsigned N) const {
1798     assert(N == 1 && "Invalid number of operands!");
1799     const SmallVectorImpl<unsigned> &RegList = getRegList();
1800     for (SmallVectorImpl<unsigned>::const_iterator
1801            I = RegList.begin(), E = RegList.end(); I != E; ++I)
1802       Inst.addOperand(MCOperand::createReg(*I));
1803   }
1804 
1805   void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
1806     addRegListOperands(Inst, N);
1807   }
1808 
1809   void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
1810     addRegListOperands(Inst, N);
1811   }
1812 
1813   void addRotImmOperands(MCInst &Inst, unsigned N) const {
1814     assert(N == 1 && "Invalid number of operands!");
1815     // Encoded as val>>3. The printer handles display as 8, 16, 24.
1816     Inst.addOperand(MCOperand::createImm(RotImm.Imm >> 3));
1817   }
1818 
1819   void addModImmOperands(MCInst &Inst, unsigned N) const {
1820     assert(N == 1 && "Invalid number of operands!");
1821 
1822     // Support for fixups (MCFixup)
1823     if (isImm())
1824       return addImmOperands(Inst, N);
1825 
1826     Inst.addOperand(MCOperand::createImm(ModImm.Bits | (ModImm.Rot << 7)));
1827   }
1828 
1829   void addModImmNotOperands(MCInst &Inst, unsigned N) const {
1830     assert(N == 1 && "Invalid number of operands!");
1831     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1832     uint32_t Enc = ARM_AM::getSOImmVal(~CE->getValue());
1833     Inst.addOperand(MCOperand::createImm(Enc));
1834   }
1835 
1836   void addModImmNegOperands(MCInst &Inst, unsigned N) const {
1837     assert(N == 1 && "Invalid number of operands!");
1838     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1839     uint32_t Enc = ARM_AM::getSOImmVal(-CE->getValue());
1840     Inst.addOperand(MCOperand::createImm(Enc));
1841   }
1842 
1843   void addBitfieldOperands(MCInst &Inst, unsigned N) const {
1844     assert(N == 1 && "Invalid number of operands!");
1845     // Munge the lsb/width into a bitfield mask.
1846     unsigned lsb = Bitfield.LSB;
1847     unsigned width = Bitfield.Width;
1848     // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
1849     uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
1850                       (32 - (lsb + width)));
1851     Inst.addOperand(MCOperand::createImm(Mask));
1852   }
1853 
1854   void addImmOperands(MCInst &Inst, unsigned N) const {
1855     assert(N == 1 && "Invalid number of operands!");
1856     addExpr(Inst, getImm());
1857   }
1858 
1859   void addFBits16Operands(MCInst &Inst, unsigned N) const {
1860     assert(N == 1 && "Invalid number of operands!");
1861     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1862     Inst.addOperand(MCOperand::createImm(16 - CE->getValue()));
1863   }
1864 
1865   void addFBits32Operands(MCInst &Inst, unsigned N) const {
1866     assert(N == 1 && "Invalid number of operands!");
1867     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1868     Inst.addOperand(MCOperand::createImm(32 - CE->getValue()));
1869   }
1870 
1871   void addFPImmOperands(MCInst &Inst, unsigned N) const {
1872     assert(N == 1 && "Invalid number of operands!");
1873     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1874     int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
1875     Inst.addOperand(MCOperand::createImm(Val));
1876   }
1877 
1878   void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1879     assert(N == 1 && "Invalid number of operands!");
1880     // FIXME: We really want to scale the value here, but the LDRD/STRD
1881     // instruction don't encode operands that way yet.
1882     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1883     Inst.addOperand(MCOperand::createImm(CE->getValue()));
1884   }
1885 
1886   void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1887     assert(N == 1 && "Invalid number of operands!");
1888     // The immediate is scaled by four in the encoding and is stored
1889     // in the MCInst as such. Lop off the low two bits here.
1890     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1891     Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
1892   }
1893 
1894   void addImm0_508s4NegOperands(MCInst &Inst, unsigned N) const {
1895     assert(N == 1 && "Invalid number of operands!");
1896     // The immediate is scaled by four in the encoding and is stored
1897     // in the MCInst as such. Lop off the low two bits here.
1898     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1899     Inst.addOperand(MCOperand::createImm(-(CE->getValue() / 4)));
1900   }
1901 
1902   void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1903     assert(N == 1 && "Invalid number of operands!");
1904     // The immediate is scaled by four in the encoding and is stored
1905     // in the MCInst as such. Lop off the low two bits here.
1906     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1907     Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
1908   }
1909 
1910   void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1911     assert(N == 1 && "Invalid number of operands!");
1912     // The constant encodes as the immediate-1, and we store in the instruction
1913     // the bits as encoded, so subtract off one here.
1914     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1915     Inst.addOperand(MCOperand::createImm(CE->getValue() - 1));
1916   }
1917 
1918   void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1919     assert(N == 1 && "Invalid number of operands!");
1920     // The constant encodes as the immediate-1, and we store in the instruction
1921     // the bits as encoded, so subtract off one here.
1922     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1923     Inst.addOperand(MCOperand::createImm(CE->getValue() - 1));
1924   }
1925 
1926   void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
1927     assert(N == 1 && "Invalid number of operands!");
1928     // The constant encodes as the immediate, except for 32, which encodes as
1929     // zero.
1930     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1931     unsigned Imm = CE->getValue();
1932     Inst.addOperand(MCOperand::createImm((Imm == 32 ? 0 : Imm)));
1933   }
1934 
1935   void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
1936     assert(N == 1 && "Invalid number of operands!");
1937     // An ASR value of 32 encodes as 0, so that's how we want to add it to
1938     // the instruction as well.
1939     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1940     int Val = CE->getValue();
1941     Inst.addOperand(MCOperand::createImm(Val == 32 ? 0 : Val));
1942   }
1943 
1944   void addT2SOImmNotOperands(MCInst &Inst, unsigned N) const {
1945     assert(N == 1 && "Invalid number of operands!");
1946     // The operand is actually a t2_so_imm, but we have its bitwise
1947     // negation in the assembly source, so twiddle it here.
1948     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1949     Inst.addOperand(MCOperand::createImm(~CE->getValue()));
1950   }
1951 
1952   void addT2SOImmNegOperands(MCInst &Inst, unsigned N) const {
1953     assert(N == 1 && "Invalid number of operands!");
1954     // The operand is actually a t2_so_imm, but we have its
1955     // negation in the assembly source, so twiddle it here.
1956     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1957     Inst.addOperand(MCOperand::createImm(-CE->getValue()));
1958   }
1959 
1960   void addImm0_4095NegOperands(MCInst &Inst, unsigned N) const {
1961     assert(N == 1 && "Invalid number of operands!");
1962     // The operand is actually an imm0_4095, but we have its
1963     // negation in the assembly source, so twiddle it here.
1964     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1965     Inst.addOperand(MCOperand::createImm(-CE->getValue()));
1966   }
1967 
1968   void addUnsignedOffset_b8s2Operands(MCInst &Inst, unsigned N) const {
1969     if(const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
1970       Inst.addOperand(MCOperand::createImm(CE->getValue() >> 2));
1971       return;
1972     }
1973 
1974     const MCSymbolRefExpr *SR = dyn_cast<MCSymbolRefExpr>(Imm.Val);
1975     assert(SR && "Unknown value type!");
1976     Inst.addOperand(MCOperand::createExpr(SR));
1977   }
1978 
1979   void addThumbMemPCOperands(MCInst &Inst, unsigned N) const {
1980     assert(N == 1 && "Invalid number of operands!");
1981     if (isImm()) {
1982       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1983       if (CE) {
1984         Inst.addOperand(MCOperand::createImm(CE->getValue()));
1985         return;
1986       }
1987 
1988       const MCSymbolRefExpr *SR = dyn_cast<MCSymbolRefExpr>(Imm.Val);
1989 
1990       assert(SR && "Unknown value type!");
1991       Inst.addOperand(MCOperand::createExpr(SR));
1992       return;
1993     }
1994 
1995     assert(isMem()  && "Unknown value type!");
1996     assert(isa<MCConstantExpr>(Memory.OffsetImm) && "Unknown value type!");
1997     Inst.addOperand(MCOperand::createImm(Memory.OffsetImm->getValue()));
1998   }
1999 
2000   void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
2001     assert(N == 1 && "Invalid number of operands!");
2002     Inst.addOperand(MCOperand::createImm(unsigned(getMemBarrierOpt())));
2003   }
2004 
2005   void addInstSyncBarrierOptOperands(MCInst &Inst, unsigned N) const {
2006     assert(N == 1 && "Invalid number of operands!");
2007     Inst.addOperand(MCOperand::createImm(unsigned(getInstSyncBarrierOpt())));
2008   }
2009 
2010   void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
2011     assert(N == 1 && "Invalid number of operands!");
2012     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2013   }
2014 
2015   void addMemPCRelImm12Operands(MCInst &Inst, unsigned N) const {
2016     assert(N == 1 && "Invalid number of operands!");
2017     int32_t Imm = Memory.OffsetImm->getValue();
2018     Inst.addOperand(MCOperand::createImm(Imm));
2019   }
2020 
2021   void addAdrLabelOperands(MCInst &Inst, unsigned N) const {
2022     assert(N == 1 && "Invalid number of operands!");
2023     assert(isImm() && "Not an immediate!");
2024 
2025     // If we have an immediate that's not a constant, treat it as a label
2026     // reference needing a fixup.
2027     if (!isa<MCConstantExpr>(getImm())) {
2028       Inst.addOperand(MCOperand::createExpr(getImm()));
2029       return;
2030     }
2031 
2032     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2033     int Val = CE->getValue();
2034     Inst.addOperand(MCOperand::createImm(Val));
2035   }
2036 
2037   void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
2038     assert(N == 2 && "Invalid number of operands!");
2039     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2040     Inst.addOperand(MCOperand::createImm(Memory.Alignment));
2041   }
2042 
2043   void addDupAlignedMemoryNoneOperands(MCInst &Inst, unsigned N) const {
2044     addAlignedMemoryOperands(Inst, N);
2045   }
2046 
2047   void addAlignedMemoryNoneOperands(MCInst &Inst, unsigned N) const {
2048     addAlignedMemoryOperands(Inst, N);
2049   }
2050 
2051   void addAlignedMemory16Operands(MCInst &Inst, unsigned N) const {
2052     addAlignedMemoryOperands(Inst, N);
2053   }
2054 
2055   void addDupAlignedMemory16Operands(MCInst &Inst, unsigned N) const {
2056     addAlignedMemoryOperands(Inst, N);
2057   }
2058 
2059   void addAlignedMemory32Operands(MCInst &Inst, unsigned N) const {
2060     addAlignedMemoryOperands(Inst, N);
2061   }
2062 
2063   void addDupAlignedMemory32Operands(MCInst &Inst, unsigned N) const {
2064     addAlignedMemoryOperands(Inst, N);
2065   }
2066 
2067   void addAlignedMemory64Operands(MCInst &Inst, unsigned N) const {
2068     addAlignedMemoryOperands(Inst, N);
2069   }
2070 
2071   void addDupAlignedMemory64Operands(MCInst &Inst, unsigned N) const {
2072     addAlignedMemoryOperands(Inst, N);
2073   }
2074 
2075   void addAlignedMemory64or128Operands(MCInst &Inst, unsigned N) const {
2076     addAlignedMemoryOperands(Inst, N);
2077   }
2078 
2079   void addDupAlignedMemory64or128Operands(MCInst &Inst, unsigned N) const {
2080     addAlignedMemoryOperands(Inst, N);
2081   }
2082 
2083   void addAlignedMemory64or128or256Operands(MCInst &Inst, unsigned N) const {
2084     addAlignedMemoryOperands(Inst, N);
2085   }
2086 
2087   void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
2088     assert(N == 3 && "Invalid number of operands!");
2089     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2090     if (!Memory.OffsetRegNum) {
2091       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2092       // Special case for #-0
2093       if (Val == INT32_MIN) Val = 0;
2094       if (Val < 0) Val = -Val;
2095       Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
2096     } else {
2097       // For register offset, we encode the shift type and negation flag
2098       // here.
2099       Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
2100                               Memory.ShiftImm, Memory.ShiftType);
2101     }
2102     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2103     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2104     Inst.addOperand(MCOperand::createImm(Val));
2105   }
2106 
2107   void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
2108     assert(N == 2 && "Invalid number of operands!");
2109     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2110     assert(CE && "non-constant AM2OffsetImm operand!");
2111     int32_t Val = CE->getValue();
2112     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2113     // Special case for #-0
2114     if (Val == INT32_MIN) Val = 0;
2115     if (Val < 0) Val = -Val;
2116     Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
2117     Inst.addOperand(MCOperand::createReg(0));
2118     Inst.addOperand(MCOperand::createImm(Val));
2119   }
2120 
2121   void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
2122     assert(N == 3 && "Invalid number of operands!");
2123     // If we have an immediate that's not a constant, treat it as a label
2124     // reference needing a fixup. If it is a constant, it's something else
2125     // and we reject it.
2126     if (isImm()) {
2127       Inst.addOperand(MCOperand::createExpr(getImm()));
2128       Inst.addOperand(MCOperand::createReg(0));
2129       Inst.addOperand(MCOperand::createImm(0));
2130       return;
2131     }
2132 
2133     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2134     if (!Memory.OffsetRegNum) {
2135       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2136       // Special case for #-0
2137       if (Val == INT32_MIN) Val = 0;
2138       if (Val < 0) Val = -Val;
2139       Val = ARM_AM::getAM3Opc(AddSub, Val);
2140     } else {
2141       // For register offset, we encode the shift type and negation flag
2142       // here.
2143       Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
2144     }
2145     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2146     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2147     Inst.addOperand(MCOperand::createImm(Val));
2148   }
2149 
2150   void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
2151     assert(N == 2 && "Invalid number of operands!");
2152     if (Kind == k_PostIndexRegister) {
2153       int32_t Val =
2154         ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
2155       Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
2156       Inst.addOperand(MCOperand::createImm(Val));
2157       return;
2158     }
2159 
2160     // Constant offset.
2161     const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
2162     int32_t Val = CE->getValue();
2163     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2164     // Special case for #-0
2165     if (Val == INT32_MIN) Val = 0;
2166     if (Val < 0) Val = -Val;
2167     Val = ARM_AM::getAM3Opc(AddSub, Val);
2168     Inst.addOperand(MCOperand::createReg(0));
2169     Inst.addOperand(MCOperand::createImm(Val));
2170   }
2171 
2172   void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
2173     assert(N == 2 && "Invalid number of operands!");
2174     // If we have an immediate that's not a constant, treat it as a label
2175     // reference needing a fixup. If it is a constant, it's something else
2176     // and we reject it.
2177     if (isImm()) {
2178       Inst.addOperand(MCOperand::createExpr(getImm()));
2179       Inst.addOperand(MCOperand::createImm(0));
2180       return;
2181     }
2182 
2183     // The lower two bits are always zero and as such are not encoded.
2184     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
2185     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2186     // Special case for #-0
2187     if (Val == INT32_MIN) Val = 0;
2188     if (Val < 0) Val = -Val;
2189     Val = ARM_AM::getAM5Opc(AddSub, Val);
2190     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2191     Inst.addOperand(MCOperand::createImm(Val));
2192   }
2193 
2194   void addAddrMode5FP16Operands(MCInst &Inst, unsigned N) const {
2195     assert(N == 2 && "Invalid number of operands!");
2196     // If we have an immediate that's not a constant, treat it as a label
2197     // reference needing a fixup. If it is a constant, it's something else
2198     // and we reject it.
2199     if (isImm()) {
2200       Inst.addOperand(MCOperand::createExpr(getImm()));
2201       Inst.addOperand(MCOperand::createImm(0));
2202       return;
2203     }
2204 
2205     // The lower bit is always zero and as such is not encoded.
2206     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 2 : 0;
2207     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2208     // Special case for #-0
2209     if (Val == INT32_MIN) Val = 0;
2210     if (Val < 0) Val = -Val;
2211     Val = ARM_AM::getAM5FP16Opc(AddSub, Val);
2212     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2213     Inst.addOperand(MCOperand::createImm(Val));
2214   }
2215 
2216   void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
2217     assert(N == 2 && "Invalid number of operands!");
2218     // If we have an immediate that's not a constant, treat it as a label
2219     // reference needing a fixup. If it is a constant, it's something else
2220     // and we reject it.
2221     if (isImm()) {
2222       Inst.addOperand(MCOperand::createExpr(getImm()));
2223       Inst.addOperand(MCOperand::createImm(0));
2224       return;
2225     }
2226 
2227     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2228     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2229     Inst.addOperand(MCOperand::createImm(Val));
2230   }
2231 
2232   void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
2233     assert(N == 2 && "Invalid number of operands!");
2234     // The lower two bits are always zero and as such are not encoded.
2235     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
2236     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2237     Inst.addOperand(MCOperand::createImm(Val));
2238   }
2239 
2240   void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
2241     assert(N == 2 && "Invalid number of operands!");
2242     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2243     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2244     Inst.addOperand(MCOperand::createImm(Val));
2245   }
2246 
2247   void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
2248     addMemImm8OffsetOperands(Inst, N);
2249   }
2250 
2251   void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
2252     addMemImm8OffsetOperands(Inst, N);
2253   }
2254 
2255   void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
2256     assert(N == 2 && "Invalid number of operands!");
2257     // If this is an immediate, it's a label reference.
2258     if (isImm()) {
2259       addExpr(Inst, getImm());
2260       Inst.addOperand(MCOperand::createImm(0));
2261       return;
2262     }
2263 
2264     // Otherwise, it's a normal memory reg+offset.
2265     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2266     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2267     Inst.addOperand(MCOperand::createImm(Val));
2268   }
2269 
2270   void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
2271     assert(N == 2 && "Invalid number of operands!");
2272     // If this is an immediate, it's a label reference.
2273     if (isImm()) {
2274       addExpr(Inst, getImm());
2275       Inst.addOperand(MCOperand::createImm(0));
2276       return;
2277     }
2278 
2279     // Otherwise, it's a normal memory reg+offset.
2280     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2281     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2282     Inst.addOperand(MCOperand::createImm(Val));
2283   }
2284 
2285   void addConstPoolAsmImmOperands(MCInst &Inst, unsigned N) const {
2286     assert(N == 1 && "Invalid number of operands!");
2287     // This is container for the immediate that we will create the constant
2288     // pool from
2289     addExpr(Inst, getConstantPoolImm());
2290     return;
2291   }
2292 
2293   void addMemTBBOperands(MCInst &Inst, unsigned N) const {
2294     assert(N == 2 && "Invalid number of operands!");
2295     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2296     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2297   }
2298 
2299   void addMemTBHOperands(MCInst &Inst, unsigned N) const {
2300     assert(N == 2 && "Invalid number of operands!");
2301     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2302     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2303   }
2304 
2305   void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
2306     assert(N == 3 && "Invalid number of operands!");
2307     unsigned Val =
2308       ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
2309                         Memory.ShiftImm, Memory.ShiftType);
2310     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2311     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2312     Inst.addOperand(MCOperand::createImm(Val));
2313   }
2314 
2315   void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
2316     assert(N == 3 && "Invalid number of operands!");
2317     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2318     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2319     Inst.addOperand(MCOperand::createImm(Memory.ShiftImm));
2320   }
2321 
2322   void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
2323     assert(N == 2 && "Invalid number of operands!");
2324     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2325     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2326   }
2327 
2328   void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
2329     assert(N == 2 && "Invalid number of operands!");
2330     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
2331     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2332     Inst.addOperand(MCOperand::createImm(Val));
2333   }
2334 
2335   void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
2336     assert(N == 2 && "Invalid number of operands!");
2337     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
2338     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2339     Inst.addOperand(MCOperand::createImm(Val));
2340   }
2341 
2342   void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
2343     assert(N == 2 && "Invalid number of operands!");
2344     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
2345     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2346     Inst.addOperand(MCOperand::createImm(Val));
2347   }
2348 
2349   void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
2350     assert(N == 2 && "Invalid number of operands!");
2351     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
2352     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2353     Inst.addOperand(MCOperand::createImm(Val));
2354   }
2355 
2356   void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
2357     assert(N == 1 && "Invalid number of operands!");
2358     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2359     assert(CE && "non-constant post-idx-imm8 operand!");
2360     int Imm = CE->getValue();
2361     bool isAdd = Imm >= 0;
2362     if (Imm == INT32_MIN) Imm = 0;
2363     Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
2364     Inst.addOperand(MCOperand::createImm(Imm));
2365   }
2366 
2367   void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
2368     assert(N == 1 && "Invalid number of operands!");
2369     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2370     assert(CE && "non-constant post-idx-imm8s4 operand!");
2371     int Imm = CE->getValue();
2372     bool isAdd = Imm >= 0;
2373     if (Imm == INT32_MIN) Imm = 0;
2374     // Immediate is scaled by 4.
2375     Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
2376     Inst.addOperand(MCOperand::createImm(Imm));
2377   }
2378 
2379   void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
2380     assert(N == 2 && "Invalid number of operands!");
2381     Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
2382     Inst.addOperand(MCOperand::createImm(PostIdxReg.isAdd));
2383   }
2384 
2385   void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
2386     assert(N == 2 && "Invalid number of operands!");
2387     Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
2388     // The sign, shift type, and shift amount are encoded in a single operand
2389     // using the AM2 encoding helpers.
2390     ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
2391     unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
2392                                      PostIdxReg.ShiftTy);
2393     Inst.addOperand(MCOperand::createImm(Imm));
2394   }
2395 
2396   void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
2397     assert(N == 1 && "Invalid number of operands!");
2398     Inst.addOperand(MCOperand::createImm(unsigned(getMSRMask())));
2399   }
2400 
2401   void addBankedRegOperands(MCInst &Inst, unsigned N) const {
2402     assert(N == 1 && "Invalid number of operands!");
2403     Inst.addOperand(MCOperand::createImm(unsigned(getBankedReg())));
2404   }
2405 
2406   void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
2407     assert(N == 1 && "Invalid number of operands!");
2408     Inst.addOperand(MCOperand::createImm(unsigned(getProcIFlags())));
2409   }
2410 
2411   void addVecListOperands(MCInst &Inst, unsigned N) const {
2412     assert(N == 1 && "Invalid number of operands!");
2413     Inst.addOperand(MCOperand::createReg(VectorList.RegNum));
2414   }
2415 
2416   void addVecListIndexedOperands(MCInst &Inst, unsigned N) const {
2417     assert(N == 2 && "Invalid number of operands!");
2418     Inst.addOperand(MCOperand::createReg(VectorList.RegNum));
2419     Inst.addOperand(MCOperand::createImm(VectorList.LaneIndex));
2420   }
2421 
2422   void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
2423     assert(N == 1 && "Invalid number of operands!");
2424     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
2425   }
2426 
2427   void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
2428     assert(N == 1 && "Invalid number of operands!");
2429     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
2430   }
2431 
2432   void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
2433     assert(N == 1 && "Invalid number of operands!");
2434     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
2435   }
2436 
2437   void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
2438     assert(N == 1 && "Invalid number of operands!");
2439     // The immediate encodes the type of constant as well as the value.
2440     // Mask in that this is an i8 splat.
2441     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2442     Inst.addOperand(MCOperand::createImm(CE->getValue() | 0xe00));
2443   }
2444 
2445   void addNEONi16splatOperands(MCInst &Inst, unsigned N) const {
2446     assert(N == 1 && "Invalid number of operands!");
2447     // The immediate encodes the type of constant as well as the value.
2448     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2449     unsigned Value = CE->getValue();
2450     Value = ARM_AM::encodeNEONi16splat(Value);
2451     Inst.addOperand(MCOperand::createImm(Value));
2452   }
2453 
2454   void addNEONi16splatNotOperands(MCInst &Inst, unsigned N) const {
2455     assert(N == 1 && "Invalid number of operands!");
2456     // The immediate encodes the type of constant as well as the value.
2457     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2458     unsigned Value = CE->getValue();
2459     Value = ARM_AM::encodeNEONi16splat(~Value & 0xffff);
2460     Inst.addOperand(MCOperand::createImm(Value));
2461   }
2462 
2463   void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
2464     assert(N == 1 && "Invalid number of operands!");
2465     // The immediate encodes the type of constant as well as the value.
2466     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2467     unsigned Value = CE->getValue();
2468     Value = ARM_AM::encodeNEONi32splat(Value);
2469     Inst.addOperand(MCOperand::createImm(Value));
2470   }
2471 
2472   void addNEONi32splatNotOperands(MCInst &Inst, unsigned N) const {
2473     assert(N == 1 && "Invalid number of operands!");
2474     // The immediate encodes the type of constant as well as the value.
2475     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2476     unsigned Value = CE->getValue();
2477     Value = ARM_AM::encodeNEONi32splat(~Value);
2478     Inst.addOperand(MCOperand::createImm(Value));
2479   }
2480 
2481   void addNEONinvByteReplicateOperands(MCInst &Inst, unsigned N) const {
2482     assert(N == 1 && "Invalid number of operands!");
2483     // The immediate encodes the type of constant as well as the value.
2484     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2485     unsigned Value = CE->getValue();
2486     assert((Inst.getOpcode() == ARM::VMOVv8i8 ||
2487             Inst.getOpcode() == ARM::VMOVv16i8) &&
2488            "All vmvn instructions that wants to replicate non-zero byte "
2489            "always must be replaced with VMOVv8i8 or VMOVv16i8.");
2490     unsigned B = ((~Value) & 0xff);
2491     B |= 0xe00; // cmode = 0b1110
2492     Inst.addOperand(MCOperand::createImm(B));
2493   }
2494   void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
2495     assert(N == 1 && "Invalid number of operands!");
2496     // The immediate encodes the type of constant as well as the value.
2497     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2498     unsigned Value = CE->getValue();
2499     if (Value >= 256 && Value <= 0xffff)
2500       Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
2501     else if (Value > 0xffff && Value <= 0xffffff)
2502       Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
2503     else if (Value > 0xffffff)
2504       Value = (Value >> 24) | 0x600;
2505     Inst.addOperand(MCOperand::createImm(Value));
2506   }
2507 
2508   void addNEONvmovByteReplicateOperands(MCInst &Inst, unsigned N) const {
2509     assert(N == 1 && "Invalid number of operands!");
2510     // The immediate encodes the type of constant as well as the value.
2511     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2512     unsigned Value = CE->getValue();
2513     assert((Inst.getOpcode() == ARM::VMOVv8i8 ||
2514             Inst.getOpcode() == ARM::VMOVv16i8) &&
2515            "All instructions that wants to replicate non-zero byte "
2516            "always must be replaced with VMOVv8i8 or VMOVv16i8.");
2517     unsigned B = Value & 0xff;
2518     B |= 0xe00; // cmode = 0b1110
2519     Inst.addOperand(MCOperand::createImm(B));
2520   }
2521   void addNEONi32vmovNegOperands(MCInst &Inst, unsigned N) const {
2522     assert(N == 1 && "Invalid number of operands!");
2523     // The immediate encodes the type of constant as well as the value.
2524     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2525     unsigned Value = ~CE->getValue();
2526     if (Value >= 256 && Value <= 0xffff)
2527       Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
2528     else if (Value > 0xffff && Value <= 0xffffff)
2529       Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
2530     else if (Value > 0xffffff)
2531       Value = (Value >> 24) | 0x600;
2532     Inst.addOperand(MCOperand::createImm(Value));
2533   }
2534 
2535   void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
2536     assert(N == 1 && "Invalid number of operands!");
2537     // The immediate encodes the type of constant as well as the value.
2538     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2539     uint64_t Value = CE->getValue();
2540     unsigned Imm = 0;
2541     for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
2542       Imm |= (Value & 1) << i;
2543     }
2544     Inst.addOperand(MCOperand::createImm(Imm | 0x1e00));
2545   }
2546 
2547   void print(raw_ostream &OS) const override;
2548 
2549   static std::unique_ptr<ARMOperand> CreateITMask(unsigned Mask, SMLoc S) {
2550     auto Op = make_unique<ARMOperand>(k_ITCondMask);
2551     Op->ITMask.Mask = Mask;
2552     Op->StartLoc = S;
2553     Op->EndLoc = S;
2554     return Op;
2555   }
2556 
2557   static std::unique_ptr<ARMOperand> CreateCondCode(ARMCC::CondCodes CC,
2558                                                     SMLoc S) {
2559     auto Op = make_unique<ARMOperand>(k_CondCode);
2560     Op->CC.Val = CC;
2561     Op->StartLoc = S;
2562     Op->EndLoc = S;
2563     return Op;
2564   }
2565 
2566   static std::unique_ptr<ARMOperand> CreateCoprocNum(unsigned CopVal, SMLoc S) {
2567     auto Op = make_unique<ARMOperand>(k_CoprocNum);
2568     Op->Cop.Val = CopVal;
2569     Op->StartLoc = S;
2570     Op->EndLoc = S;
2571     return Op;
2572   }
2573 
2574   static std::unique_ptr<ARMOperand> CreateCoprocReg(unsigned CopVal, SMLoc S) {
2575     auto Op = make_unique<ARMOperand>(k_CoprocReg);
2576     Op->Cop.Val = CopVal;
2577     Op->StartLoc = S;
2578     Op->EndLoc = S;
2579     return Op;
2580   }
2581 
2582   static std::unique_ptr<ARMOperand> CreateCoprocOption(unsigned Val, SMLoc S,
2583                                                         SMLoc E) {
2584     auto Op = make_unique<ARMOperand>(k_CoprocOption);
2585     Op->Cop.Val = Val;
2586     Op->StartLoc = S;
2587     Op->EndLoc = E;
2588     return Op;
2589   }
2590 
2591   static std::unique_ptr<ARMOperand> CreateCCOut(unsigned RegNum, SMLoc S) {
2592     auto Op = make_unique<ARMOperand>(k_CCOut);
2593     Op->Reg.RegNum = RegNum;
2594     Op->StartLoc = S;
2595     Op->EndLoc = S;
2596     return Op;
2597   }
2598 
2599   static std::unique_ptr<ARMOperand> CreateToken(StringRef Str, SMLoc S) {
2600     auto Op = make_unique<ARMOperand>(k_Token);
2601     Op->Tok.Data = Str.data();
2602     Op->Tok.Length = Str.size();
2603     Op->StartLoc = S;
2604     Op->EndLoc = S;
2605     return Op;
2606   }
2607 
2608   static std::unique_ptr<ARMOperand> CreateReg(unsigned RegNum, SMLoc S,
2609                                                SMLoc E) {
2610     auto Op = make_unique<ARMOperand>(k_Register);
2611     Op->Reg.RegNum = RegNum;
2612     Op->StartLoc = S;
2613     Op->EndLoc = E;
2614     return Op;
2615   }
2616 
2617   static std::unique_ptr<ARMOperand>
2618   CreateShiftedRegister(ARM_AM::ShiftOpc ShTy, unsigned SrcReg,
2619                         unsigned ShiftReg, unsigned ShiftImm, SMLoc S,
2620                         SMLoc E) {
2621     auto Op = make_unique<ARMOperand>(k_ShiftedRegister);
2622     Op->RegShiftedReg.ShiftTy = ShTy;
2623     Op->RegShiftedReg.SrcReg = SrcReg;
2624     Op->RegShiftedReg.ShiftReg = ShiftReg;
2625     Op->RegShiftedReg.ShiftImm = ShiftImm;
2626     Op->StartLoc = S;
2627     Op->EndLoc = E;
2628     return Op;
2629   }
2630 
2631   static std::unique_ptr<ARMOperand>
2632   CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy, unsigned SrcReg,
2633                          unsigned ShiftImm, SMLoc S, SMLoc E) {
2634     auto Op = make_unique<ARMOperand>(k_ShiftedImmediate);
2635     Op->RegShiftedImm.ShiftTy = ShTy;
2636     Op->RegShiftedImm.SrcReg = SrcReg;
2637     Op->RegShiftedImm.ShiftImm = ShiftImm;
2638     Op->StartLoc = S;
2639     Op->EndLoc = E;
2640     return Op;
2641   }
2642 
2643   static std::unique_ptr<ARMOperand> CreateShifterImm(bool isASR, unsigned Imm,
2644                                                       SMLoc S, SMLoc E) {
2645     auto Op = make_unique<ARMOperand>(k_ShifterImmediate);
2646     Op->ShifterImm.isASR = isASR;
2647     Op->ShifterImm.Imm = Imm;
2648     Op->StartLoc = S;
2649     Op->EndLoc = E;
2650     return Op;
2651   }
2652 
2653   static std::unique_ptr<ARMOperand> CreateRotImm(unsigned Imm, SMLoc S,
2654                                                   SMLoc E) {
2655     auto Op = make_unique<ARMOperand>(k_RotateImmediate);
2656     Op->RotImm.Imm = Imm;
2657     Op->StartLoc = S;
2658     Op->EndLoc = E;
2659     return Op;
2660   }
2661 
2662   static std::unique_ptr<ARMOperand> CreateModImm(unsigned Bits, unsigned Rot,
2663                                                   SMLoc S, SMLoc E) {
2664     auto Op = make_unique<ARMOperand>(k_ModifiedImmediate);
2665     Op->ModImm.Bits = Bits;
2666     Op->ModImm.Rot = Rot;
2667     Op->StartLoc = S;
2668     Op->EndLoc = E;
2669     return Op;
2670   }
2671 
2672   static std::unique_ptr<ARMOperand>
2673   CreateConstantPoolImm(const MCExpr *Val, SMLoc S, SMLoc E) {
2674     auto Op = make_unique<ARMOperand>(k_ConstantPoolImmediate);
2675     Op->Imm.Val = Val;
2676     Op->StartLoc = S;
2677     Op->EndLoc = E;
2678     return Op;
2679   }
2680 
2681   static std::unique_ptr<ARMOperand>
2682   CreateBitfield(unsigned LSB, unsigned Width, SMLoc S, SMLoc E) {
2683     auto Op = make_unique<ARMOperand>(k_BitfieldDescriptor);
2684     Op->Bitfield.LSB = LSB;
2685     Op->Bitfield.Width = Width;
2686     Op->StartLoc = S;
2687     Op->EndLoc = E;
2688     return Op;
2689   }
2690 
2691   static std::unique_ptr<ARMOperand>
2692   CreateRegList(SmallVectorImpl<std::pair<unsigned, unsigned>> &Regs,
2693                 SMLoc StartLoc, SMLoc EndLoc) {
2694     assert (Regs.size() > 0 && "RegList contains no registers?");
2695     KindTy Kind = k_RegisterList;
2696 
2697     if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().second))
2698       Kind = k_DPRRegisterList;
2699     else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
2700              contains(Regs.front().second))
2701       Kind = k_SPRRegisterList;
2702 
2703     // Sort based on the register encoding values.
2704     array_pod_sort(Regs.begin(), Regs.end());
2705 
2706     auto Op = make_unique<ARMOperand>(Kind);
2707     for (SmallVectorImpl<std::pair<unsigned, unsigned> >::const_iterator
2708            I = Regs.begin(), E = Regs.end(); I != E; ++I)
2709       Op->Registers.push_back(I->second);
2710     Op->StartLoc = StartLoc;
2711     Op->EndLoc = EndLoc;
2712     return Op;
2713   }
2714 
2715   static std::unique_ptr<ARMOperand> CreateVectorList(unsigned RegNum,
2716                                                       unsigned Count,
2717                                                       bool isDoubleSpaced,
2718                                                       SMLoc S, SMLoc E) {
2719     auto Op = make_unique<ARMOperand>(k_VectorList);
2720     Op->VectorList.RegNum = RegNum;
2721     Op->VectorList.Count = Count;
2722     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2723     Op->StartLoc = S;
2724     Op->EndLoc = E;
2725     return Op;
2726   }
2727 
2728   static std::unique_ptr<ARMOperand>
2729   CreateVectorListAllLanes(unsigned RegNum, unsigned Count, bool isDoubleSpaced,
2730                            SMLoc S, SMLoc E) {
2731     auto Op = make_unique<ARMOperand>(k_VectorListAllLanes);
2732     Op->VectorList.RegNum = RegNum;
2733     Op->VectorList.Count = Count;
2734     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2735     Op->StartLoc = S;
2736     Op->EndLoc = E;
2737     return Op;
2738   }
2739 
2740   static std::unique_ptr<ARMOperand>
2741   CreateVectorListIndexed(unsigned RegNum, unsigned Count, unsigned Index,
2742                           bool isDoubleSpaced, SMLoc S, SMLoc E) {
2743     auto Op = make_unique<ARMOperand>(k_VectorListIndexed);
2744     Op->VectorList.RegNum = RegNum;
2745     Op->VectorList.Count = Count;
2746     Op->VectorList.LaneIndex = Index;
2747     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2748     Op->StartLoc = S;
2749     Op->EndLoc = E;
2750     return Op;
2751   }
2752 
2753   static std::unique_ptr<ARMOperand>
2754   CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E, MCContext &Ctx) {
2755     auto Op = make_unique<ARMOperand>(k_VectorIndex);
2756     Op->VectorIndex.Val = Idx;
2757     Op->StartLoc = S;
2758     Op->EndLoc = E;
2759     return Op;
2760   }
2761 
2762   static std::unique_ptr<ARMOperand> CreateImm(const MCExpr *Val, SMLoc S,
2763                                                SMLoc E) {
2764     auto Op = make_unique<ARMOperand>(k_Immediate);
2765     Op->Imm.Val = Val;
2766     Op->StartLoc = S;
2767     Op->EndLoc = E;
2768     return Op;
2769   }
2770 
2771   static std::unique_ptr<ARMOperand>
2772   CreateMem(unsigned BaseRegNum, const MCConstantExpr *OffsetImm,
2773             unsigned OffsetRegNum, ARM_AM::ShiftOpc ShiftType,
2774             unsigned ShiftImm, unsigned Alignment, bool isNegative, SMLoc S,
2775             SMLoc E, SMLoc AlignmentLoc = SMLoc()) {
2776     auto Op = make_unique<ARMOperand>(k_Memory);
2777     Op->Memory.BaseRegNum = BaseRegNum;
2778     Op->Memory.OffsetImm = OffsetImm;
2779     Op->Memory.OffsetRegNum = OffsetRegNum;
2780     Op->Memory.ShiftType = ShiftType;
2781     Op->Memory.ShiftImm = ShiftImm;
2782     Op->Memory.Alignment = Alignment;
2783     Op->Memory.isNegative = isNegative;
2784     Op->StartLoc = S;
2785     Op->EndLoc = E;
2786     Op->AlignmentLoc = AlignmentLoc;
2787     return Op;
2788   }
2789 
2790   static std::unique_ptr<ARMOperand>
2791   CreatePostIdxReg(unsigned RegNum, bool isAdd, ARM_AM::ShiftOpc ShiftTy,
2792                    unsigned ShiftImm, SMLoc S, SMLoc E) {
2793     auto Op = make_unique<ARMOperand>(k_PostIndexRegister);
2794     Op->PostIdxReg.RegNum = RegNum;
2795     Op->PostIdxReg.isAdd = isAdd;
2796     Op->PostIdxReg.ShiftTy = ShiftTy;
2797     Op->PostIdxReg.ShiftImm = ShiftImm;
2798     Op->StartLoc = S;
2799     Op->EndLoc = E;
2800     return Op;
2801   }
2802 
2803   static std::unique_ptr<ARMOperand> CreateMemBarrierOpt(ARM_MB::MemBOpt Opt,
2804                                                          SMLoc S) {
2805     auto Op = make_unique<ARMOperand>(k_MemBarrierOpt);
2806     Op->MBOpt.Val = Opt;
2807     Op->StartLoc = S;
2808     Op->EndLoc = S;
2809     return Op;
2810   }
2811 
2812   static std::unique_ptr<ARMOperand>
2813   CreateInstSyncBarrierOpt(ARM_ISB::InstSyncBOpt Opt, SMLoc S) {
2814     auto Op = make_unique<ARMOperand>(k_InstSyncBarrierOpt);
2815     Op->ISBOpt.Val = Opt;
2816     Op->StartLoc = S;
2817     Op->EndLoc = S;
2818     return Op;
2819   }
2820 
2821   static std::unique_ptr<ARMOperand> CreateProcIFlags(ARM_PROC::IFlags IFlags,
2822                                                       SMLoc S) {
2823     auto Op = make_unique<ARMOperand>(k_ProcIFlags);
2824     Op->IFlags.Val = IFlags;
2825     Op->StartLoc = S;
2826     Op->EndLoc = S;
2827     return Op;
2828   }
2829 
2830   static std::unique_ptr<ARMOperand> CreateMSRMask(unsigned MMask, SMLoc S) {
2831     auto Op = make_unique<ARMOperand>(k_MSRMask);
2832     Op->MMask.Val = MMask;
2833     Op->StartLoc = S;
2834     Op->EndLoc = S;
2835     return Op;
2836   }
2837 
2838   static std::unique_ptr<ARMOperand> CreateBankedReg(unsigned Reg, SMLoc S) {
2839     auto Op = make_unique<ARMOperand>(k_BankedReg);
2840     Op->BankedReg.Val = Reg;
2841     Op->StartLoc = S;
2842     Op->EndLoc = S;
2843     return Op;
2844   }
2845 };
2846 
2847 } // end anonymous namespace.
2848 
2849 void ARMOperand::print(raw_ostream &OS) const {
2850   switch (Kind) {
2851   case k_CondCode:
2852     OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
2853     break;
2854   case k_CCOut:
2855     OS << "<ccout " << getReg() << ">";
2856     break;
2857   case k_ITCondMask: {
2858     static const char *const MaskStr[] = {
2859       "()", "(t)", "(e)", "(tt)", "(et)", "(te)", "(ee)", "(ttt)", "(ett)",
2860       "(tet)", "(eet)", "(tte)", "(ete)", "(tee)", "(eee)"
2861     };
2862     assert((ITMask.Mask & 0xf) == ITMask.Mask);
2863     OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
2864     break;
2865   }
2866   case k_CoprocNum:
2867     OS << "<coprocessor number: " << getCoproc() << ">";
2868     break;
2869   case k_CoprocReg:
2870     OS << "<coprocessor register: " << getCoproc() << ">";
2871     break;
2872   case k_CoprocOption:
2873     OS << "<coprocessor option: " << CoprocOption.Val << ">";
2874     break;
2875   case k_MSRMask:
2876     OS << "<mask: " << getMSRMask() << ">";
2877     break;
2878   case k_BankedReg:
2879     OS << "<banked reg: " << getBankedReg() << ">";
2880     break;
2881   case k_Immediate:
2882     OS << *getImm();
2883     break;
2884   case k_MemBarrierOpt:
2885     OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt(), false) << ">";
2886     break;
2887   case k_InstSyncBarrierOpt:
2888     OS << "<ARM_ISB::" << InstSyncBOptToString(getInstSyncBarrierOpt()) << ">";
2889     break;
2890   case k_Memory:
2891     OS << "<memory "
2892        << " base:" << Memory.BaseRegNum;
2893     OS << ">";
2894     break;
2895   case k_PostIndexRegister:
2896     OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
2897        << PostIdxReg.RegNum;
2898     if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
2899       OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
2900          << PostIdxReg.ShiftImm;
2901     OS << ">";
2902     break;
2903   case k_ProcIFlags: {
2904     OS << "<ARM_PROC::";
2905     unsigned IFlags = getProcIFlags();
2906     for (int i=2; i >= 0; --i)
2907       if (IFlags & (1 << i))
2908         OS << ARM_PROC::IFlagsToString(1 << i);
2909     OS << ">";
2910     break;
2911   }
2912   case k_Register:
2913     OS << "<register " << getReg() << ">";
2914     break;
2915   case k_ShifterImmediate:
2916     OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
2917        << " #" << ShifterImm.Imm << ">";
2918     break;
2919   case k_ShiftedRegister:
2920     OS << "<so_reg_reg "
2921        << RegShiftedReg.SrcReg << " "
2922        << ARM_AM::getShiftOpcStr(RegShiftedReg.ShiftTy)
2923        << " " << RegShiftedReg.ShiftReg << ">";
2924     break;
2925   case k_ShiftedImmediate:
2926     OS << "<so_reg_imm "
2927        << RegShiftedImm.SrcReg << " "
2928        << ARM_AM::getShiftOpcStr(RegShiftedImm.ShiftTy)
2929        << " #" << RegShiftedImm.ShiftImm << ">";
2930     break;
2931   case k_RotateImmediate:
2932     OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
2933     break;
2934   case k_ModifiedImmediate:
2935     OS << "<mod_imm #" << ModImm.Bits << ", #"
2936        <<  ModImm.Rot << ")>";
2937     break;
2938   case k_ConstantPoolImmediate:
2939     OS << "<constant_pool_imm #" << *getConstantPoolImm();
2940     break;
2941   case k_BitfieldDescriptor:
2942     OS << "<bitfield " << "lsb: " << Bitfield.LSB
2943        << ", width: " << Bitfield.Width << ">";
2944     break;
2945   case k_RegisterList:
2946   case k_DPRRegisterList:
2947   case k_SPRRegisterList: {
2948     OS << "<register_list ";
2949 
2950     const SmallVectorImpl<unsigned> &RegList = getRegList();
2951     for (SmallVectorImpl<unsigned>::const_iterator
2952            I = RegList.begin(), E = RegList.end(); I != E; ) {
2953       OS << *I;
2954       if (++I < E) OS << ", ";
2955     }
2956 
2957     OS << ">";
2958     break;
2959   }
2960   case k_VectorList:
2961     OS << "<vector_list " << VectorList.Count << " * "
2962        << VectorList.RegNum << ">";
2963     break;
2964   case k_VectorListAllLanes:
2965     OS << "<vector_list(all lanes) " << VectorList.Count << " * "
2966        << VectorList.RegNum << ">";
2967     break;
2968   case k_VectorListIndexed:
2969     OS << "<vector_list(lane " << VectorList.LaneIndex << ") "
2970        << VectorList.Count << " * " << VectorList.RegNum << ">";
2971     break;
2972   case k_Token:
2973     OS << "'" << getToken() << "'";
2974     break;
2975   case k_VectorIndex:
2976     OS << "<vectorindex " << getVectorIndex() << ">";
2977     break;
2978   }
2979 }
2980 
2981 /// @name Auto-generated Match Functions
2982 /// {
2983 
2984 static unsigned MatchRegisterName(StringRef Name);
2985 
2986 /// }
2987 
2988 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
2989                                  SMLoc &StartLoc, SMLoc &EndLoc) {
2990   const AsmToken &Tok = getParser().getTok();
2991   StartLoc = Tok.getLoc();
2992   EndLoc = Tok.getEndLoc();
2993   RegNo = tryParseRegister();
2994 
2995   return (RegNo == (unsigned)-1);
2996 }
2997 
2998 /// Try to parse a register name.  The token must be an Identifier when called,
2999 /// and if it is a register name the token is eaten and the register number is
3000 /// returned.  Otherwise return -1.
3001 ///
3002 int ARMAsmParser::tryParseRegister() {
3003   MCAsmParser &Parser = getParser();
3004   const AsmToken &Tok = Parser.getTok();
3005   if (Tok.isNot(AsmToken::Identifier)) return -1;
3006 
3007   std::string lowerCase = Tok.getString().lower();
3008   unsigned RegNum = MatchRegisterName(lowerCase);
3009   if (!RegNum) {
3010     RegNum = StringSwitch<unsigned>(lowerCase)
3011       .Case("r13", ARM::SP)
3012       .Case("r14", ARM::LR)
3013       .Case("r15", ARM::PC)
3014       .Case("ip", ARM::R12)
3015       // Additional register name aliases for 'gas' compatibility.
3016       .Case("a1", ARM::R0)
3017       .Case("a2", ARM::R1)
3018       .Case("a3", ARM::R2)
3019       .Case("a4", ARM::R3)
3020       .Case("v1", ARM::R4)
3021       .Case("v2", ARM::R5)
3022       .Case("v3", ARM::R6)
3023       .Case("v4", ARM::R7)
3024       .Case("v5", ARM::R8)
3025       .Case("v6", ARM::R9)
3026       .Case("v7", ARM::R10)
3027       .Case("v8", ARM::R11)
3028       .Case("sb", ARM::R9)
3029       .Case("sl", ARM::R10)
3030       .Case("fp", ARM::R11)
3031       .Default(0);
3032   }
3033   if (!RegNum) {
3034     // Check for aliases registered via .req. Canonicalize to lower case.
3035     // That's more consistent since register names are case insensitive, and
3036     // it's how the original entry was passed in from MC/MCParser/AsmParser.
3037     StringMap<unsigned>::const_iterator Entry = RegisterReqs.find(lowerCase);
3038     // If no match, return failure.
3039     if (Entry == RegisterReqs.end())
3040       return -1;
3041     Parser.Lex(); // Eat identifier token.
3042     return Entry->getValue();
3043   }
3044 
3045   // Some FPUs only have 16 D registers, so D16-D31 are invalid
3046   if (hasD16() && RegNum >= ARM::D16 && RegNum <= ARM::D31)
3047     return -1;
3048 
3049   Parser.Lex(); // Eat identifier token.
3050 
3051   return RegNum;
3052 }
3053 
3054 // Try to parse a shifter  (e.g., "lsl <amt>"). On success, return 0.
3055 // If a recoverable error occurs, return 1. If an irrecoverable error
3056 // occurs, return -1. An irrecoverable error is one where tokens have been
3057 // consumed in the process of trying to parse the shifter (i.e., when it is
3058 // indeed a shifter operand, but malformed).
3059 int ARMAsmParser::tryParseShiftRegister(OperandVector &Operands) {
3060   MCAsmParser &Parser = getParser();
3061   SMLoc S = Parser.getTok().getLoc();
3062   const AsmToken &Tok = Parser.getTok();
3063   if (Tok.isNot(AsmToken::Identifier))
3064     return -1;
3065 
3066   std::string lowerCase = Tok.getString().lower();
3067   ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
3068       .Case("asl", ARM_AM::lsl)
3069       .Case("lsl", ARM_AM::lsl)
3070       .Case("lsr", ARM_AM::lsr)
3071       .Case("asr", ARM_AM::asr)
3072       .Case("ror", ARM_AM::ror)
3073       .Case("rrx", ARM_AM::rrx)
3074       .Default(ARM_AM::no_shift);
3075 
3076   if (ShiftTy == ARM_AM::no_shift)
3077     return 1;
3078 
3079   Parser.Lex(); // Eat the operator.
3080 
3081   // The source register for the shift has already been added to the
3082   // operand list, so we need to pop it off and combine it into the shifted
3083   // register operand instead.
3084   std::unique_ptr<ARMOperand> PrevOp(
3085       (ARMOperand *)Operands.pop_back_val().release());
3086   if (!PrevOp->isReg())
3087     return Error(PrevOp->getStartLoc(), "shift must be of a register");
3088   int SrcReg = PrevOp->getReg();
3089 
3090   SMLoc EndLoc;
3091   int64_t Imm = 0;
3092   int ShiftReg = 0;
3093   if (ShiftTy == ARM_AM::rrx) {
3094     // RRX Doesn't have an explicit shift amount. The encoder expects
3095     // the shift register to be the same as the source register. Seems odd,
3096     // but OK.
3097     ShiftReg = SrcReg;
3098   } else {
3099     // Figure out if this is shifted by a constant or a register (for non-RRX).
3100     if (Parser.getTok().is(AsmToken::Hash) ||
3101         Parser.getTok().is(AsmToken::Dollar)) {
3102       Parser.Lex(); // Eat hash.
3103       SMLoc ImmLoc = Parser.getTok().getLoc();
3104       const MCExpr *ShiftExpr = nullptr;
3105       if (getParser().parseExpression(ShiftExpr, EndLoc)) {
3106         Error(ImmLoc, "invalid immediate shift value");
3107         return -1;
3108       }
3109       // The expression must be evaluatable as an immediate.
3110       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
3111       if (!CE) {
3112         Error(ImmLoc, "invalid immediate shift value");
3113         return -1;
3114       }
3115       // Range check the immediate.
3116       // lsl, ror: 0 <= imm <= 31
3117       // lsr, asr: 0 <= imm <= 32
3118       Imm = CE->getValue();
3119       if (Imm < 0 ||
3120           ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
3121           ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
3122         Error(ImmLoc, "immediate shift value out of range");
3123         return -1;
3124       }
3125       // shift by zero is a nop. Always send it through as lsl.
3126       // ('as' compatibility)
3127       if (Imm == 0)
3128         ShiftTy = ARM_AM::lsl;
3129     } else if (Parser.getTok().is(AsmToken::Identifier)) {
3130       SMLoc L = Parser.getTok().getLoc();
3131       EndLoc = Parser.getTok().getEndLoc();
3132       ShiftReg = tryParseRegister();
3133       if (ShiftReg == -1) {
3134         Error(L, "expected immediate or register in shift operand");
3135         return -1;
3136       }
3137     } else {
3138       Error(Parser.getTok().getLoc(),
3139             "expected immediate or register in shift operand");
3140       return -1;
3141     }
3142   }
3143 
3144   if (ShiftReg && ShiftTy != ARM_AM::rrx)
3145     Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
3146                                                          ShiftReg, Imm,
3147                                                          S, EndLoc));
3148   else
3149     Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
3150                                                           S, EndLoc));
3151 
3152   return 0;
3153 }
3154 
3155 
3156 /// Try to parse a register name.  The token must be an Identifier when called.
3157 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
3158 /// if there is a "writeback". 'true' if it's not a register.
3159 ///
3160 /// TODO this is likely to change to allow different register types and or to
3161 /// parse for a specific register type.
3162 bool ARMAsmParser::tryParseRegisterWithWriteBack(OperandVector &Operands) {
3163   MCAsmParser &Parser = getParser();
3164   const AsmToken &RegTok = Parser.getTok();
3165   int RegNo = tryParseRegister();
3166   if (RegNo == -1)
3167     return true;
3168 
3169   Operands.push_back(ARMOperand::CreateReg(RegNo, RegTok.getLoc(),
3170                                            RegTok.getEndLoc()));
3171 
3172   const AsmToken &ExclaimTok = Parser.getTok();
3173   if (ExclaimTok.is(AsmToken::Exclaim)) {
3174     Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
3175                                                ExclaimTok.getLoc()));
3176     Parser.Lex(); // Eat exclaim token
3177     return false;
3178   }
3179 
3180   // Also check for an index operand. This is only legal for vector registers,
3181   // but that'll get caught OK in operand matching, so we don't need to
3182   // explicitly filter everything else out here.
3183   if (Parser.getTok().is(AsmToken::LBrac)) {
3184     SMLoc SIdx = Parser.getTok().getLoc();
3185     Parser.Lex(); // Eat left bracket token.
3186 
3187     const MCExpr *ImmVal;
3188     if (getParser().parseExpression(ImmVal))
3189       return true;
3190     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
3191     if (!MCE)
3192       return TokError("immediate value expected for vector index");
3193 
3194     if (Parser.getTok().isNot(AsmToken::RBrac))
3195       return Error(Parser.getTok().getLoc(), "']' expected");
3196 
3197     SMLoc E = Parser.getTok().getEndLoc();
3198     Parser.Lex(); // Eat right bracket token.
3199 
3200     Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
3201                                                      SIdx, E,
3202                                                      getContext()));
3203   }
3204 
3205   return false;
3206 }
3207 
3208 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
3209 /// instruction with a symbolic operand name.
3210 /// We accept "crN" syntax for GAS compatibility.
3211 /// <operand-name> ::= <prefix><number>
3212 /// If CoprocOp is 'c', then:
3213 ///   <prefix> ::= c | cr
3214 /// If CoprocOp is 'p', then :
3215 ///   <prefix> ::= p
3216 /// <number> ::= integer in range [0, 15]
3217 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
3218   // Use the same layout as the tablegen'erated register name matcher. Ugly,
3219   // but efficient.
3220   if (Name.size() < 2 || Name[0] != CoprocOp)
3221     return -1;
3222   Name = (Name[1] == 'r') ? Name.drop_front(2) : Name.drop_front();
3223 
3224   switch (Name.size()) {
3225   default: return -1;
3226   case 1:
3227     switch (Name[0]) {
3228     default:  return -1;
3229     case '0': return 0;
3230     case '1': return 1;
3231     case '2': return 2;
3232     case '3': return 3;
3233     case '4': return 4;
3234     case '5': return 5;
3235     case '6': return 6;
3236     case '7': return 7;
3237     case '8': return 8;
3238     case '9': return 9;
3239     }
3240   case 2:
3241     if (Name[0] != '1')
3242       return -1;
3243     switch (Name[1]) {
3244     default:  return -1;
3245     // CP10 and CP11 are VFP/NEON and so vector instructions should be used.
3246     // However, old cores (v5/v6) did use them in that way.
3247     case '0': return 10;
3248     case '1': return 11;
3249     case '2': return 12;
3250     case '3': return 13;
3251     case '4': return 14;
3252     case '5': return 15;
3253     }
3254   }
3255 }
3256 
3257 /// parseITCondCode - Try to parse a condition code for an IT instruction.
3258 ARMAsmParser::OperandMatchResultTy
3259 ARMAsmParser::parseITCondCode(OperandVector &Operands) {
3260   MCAsmParser &Parser = getParser();
3261   SMLoc S = Parser.getTok().getLoc();
3262   const AsmToken &Tok = Parser.getTok();
3263   if (!Tok.is(AsmToken::Identifier))
3264     return MatchOperand_NoMatch;
3265   unsigned CC = StringSwitch<unsigned>(Tok.getString().lower())
3266     .Case("eq", ARMCC::EQ)
3267     .Case("ne", ARMCC::NE)
3268     .Case("hs", ARMCC::HS)
3269     .Case("cs", ARMCC::HS)
3270     .Case("lo", ARMCC::LO)
3271     .Case("cc", ARMCC::LO)
3272     .Case("mi", ARMCC::MI)
3273     .Case("pl", ARMCC::PL)
3274     .Case("vs", ARMCC::VS)
3275     .Case("vc", ARMCC::VC)
3276     .Case("hi", ARMCC::HI)
3277     .Case("ls", ARMCC::LS)
3278     .Case("ge", ARMCC::GE)
3279     .Case("lt", ARMCC::LT)
3280     .Case("gt", ARMCC::GT)
3281     .Case("le", ARMCC::LE)
3282     .Case("al", ARMCC::AL)
3283     .Default(~0U);
3284   if (CC == ~0U)
3285     return MatchOperand_NoMatch;
3286   Parser.Lex(); // Eat the token.
3287 
3288   Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
3289 
3290   return MatchOperand_Success;
3291 }
3292 
3293 /// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
3294 /// token must be an Identifier when called, and if it is a coprocessor
3295 /// number, the token is eaten and the operand is added to the operand list.
3296 ARMAsmParser::OperandMatchResultTy
3297 ARMAsmParser::parseCoprocNumOperand(OperandVector &Operands) {
3298   MCAsmParser &Parser = getParser();
3299   SMLoc S = Parser.getTok().getLoc();
3300   const AsmToken &Tok = Parser.getTok();
3301   if (Tok.isNot(AsmToken::Identifier))
3302     return MatchOperand_NoMatch;
3303 
3304   int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
3305   if (Num == -1)
3306     return MatchOperand_NoMatch;
3307   // ARMv7 and v8 don't allow cp10/cp11 due to VFP/NEON specific instructions
3308   if ((hasV7Ops() || hasV8Ops()) && (Num == 10 || Num == 11))
3309     return MatchOperand_NoMatch;
3310 
3311   Parser.Lex(); // Eat identifier token.
3312   Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
3313   return MatchOperand_Success;
3314 }
3315 
3316 /// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
3317 /// token must be an Identifier when called, and if it is a coprocessor
3318 /// number, the token is eaten and the operand is added to the operand list.
3319 ARMAsmParser::OperandMatchResultTy
3320 ARMAsmParser::parseCoprocRegOperand(OperandVector &Operands) {
3321   MCAsmParser &Parser = getParser();
3322   SMLoc S = Parser.getTok().getLoc();
3323   const AsmToken &Tok = Parser.getTok();
3324   if (Tok.isNot(AsmToken::Identifier))
3325     return MatchOperand_NoMatch;
3326 
3327   int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
3328   if (Reg == -1)
3329     return MatchOperand_NoMatch;
3330 
3331   Parser.Lex(); // Eat identifier token.
3332   Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
3333   return MatchOperand_Success;
3334 }
3335 
3336 /// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
3337 /// coproc_option : '{' imm0_255 '}'
3338 ARMAsmParser::OperandMatchResultTy
3339 ARMAsmParser::parseCoprocOptionOperand(OperandVector &Operands) {
3340   MCAsmParser &Parser = getParser();
3341   SMLoc S = Parser.getTok().getLoc();
3342 
3343   // If this isn't a '{', this isn't a coprocessor immediate operand.
3344   if (Parser.getTok().isNot(AsmToken::LCurly))
3345     return MatchOperand_NoMatch;
3346   Parser.Lex(); // Eat the '{'
3347 
3348   const MCExpr *Expr;
3349   SMLoc Loc = Parser.getTok().getLoc();
3350   if (getParser().parseExpression(Expr)) {
3351     Error(Loc, "illegal expression");
3352     return MatchOperand_ParseFail;
3353   }
3354   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3355   if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
3356     Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
3357     return MatchOperand_ParseFail;
3358   }
3359   int Val = CE->getValue();
3360 
3361   // Check for and consume the closing '}'
3362   if (Parser.getTok().isNot(AsmToken::RCurly))
3363     return MatchOperand_ParseFail;
3364   SMLoc E = Parser.getTok().getEndLoc();
3365   Parser.Lex(); // Eat the '}'
3366 
3367   Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
3368   return MatchOperand_Success;
3369 }
3370 
3371 // For register list parsing, we need to map from raw GPR register numbering
3372 // to the enumeration values. The enumeration values aren't sorted by
3373 // register number due to our using "sp", "lr" and "pc" as canonical names.
3374 static unsigned getNextRegister(unsigned Reg) {
3375   // If this is a GPR, we need to do it manually, otherwise we can rely
3376   // on the sort ordering of the enumeration since the other reg-classes
3377   // are sane.
3378   if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
3379     return Reg + 1;
3380   switch(Reg) {
3381   default: llvm_unreachable("Invalid GPR number!");
3382   case ARM::R0:  return ARM::R1;  case ARM::R1:  return ARM::R2;
3383   case ARM::R2:  return ARM::R3;  case ARM::R3:  return ARM::R4;
3384   case ARM::R4:  return ARM::R5;  case ARM::R5:  return ARM::R6;
3385   case ARM::R6:  return ARM::R7;  case ARM::R7:  return ARM::R8;
3386   case ARM::R8:  return ARM::R9;  case ARM::R9:  return ARM::R10;
3387   case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
3388   case ARM::R12: return ARM::SP;  case ARM::SP:  return ARM::LR;
3389   case ARM::LR:  return ARM::PC;  case ARM::PC:  return ARM::R0;
3390   }
3391 }
3392 
3393 // Return the low-subreg of a given Q register.
3394 static unsigned getDRegFromQReg(unsigned QReg) {
3395   switch (QReg) {
3396   default: llvm_unreachable("expected a Q register!");
3397   case ARM::Q0:  return ARM::D0;
3398   case ARM::Q1:  return ARM::D2;
3399   case ARM::Q2:  return ARM::D4;
3400   case ARM::Q3:  return ARM::D6;
3401   case ARM::Q4:  return ARM::D8;
3402   case ARM::Q5:  return ARM::D10;
3403   case ARM::Q6:  return ARM::D12;
3404   case ARM::Q7:  return ARM::D14;
3405   case ARM::Q8:  return ARM::D16;
3406   case ARM::Q9:  return ARM::D18;
3407   case ARM::Q10: return ARM::D20;
3408   case ARM::Q11: return ARM::D22;
3409   case ARM::Q12: return ARM::D24;
3410   case ARM::Q13: return ARM::D26;
3411   case ARM::Q14: return ARM::D28;
3412   case ARM::Q15: return ARM::D30;
3413   }
3414 }
3415 
3416 /// Parse a register list.
3417 bool ARMAsmParser::parseRegisterList(OperandVector &Operands) {
3418   MCAsmParser &Parser = getParser();
3419   assert(Parser.getTok().is(AsmToken::LCurly) &&
3420          "Token is not a Left Curly Brace");
3421   SMLoc S = Parser.getTok().getLoc();
3422   Parser.Lex(); // Eat '{' token.
3423   SMLoc RegLoc = Parser.getTok().getLoc();
3424 
3425   // Check the first register in the list to see what register class
3426   // this is a list of.
3427   int Reg = tryParseRegister();
3428   if (Reg == -1)
3429     return Error(RegLoc, "register expected");
3430 
3431   // The reglist instructions have at most 16 registers, so reserve
3432   // space for that many.
3433   int EReg = 0;
3434   SmallVector<std::pair<unsigned, unsigned>, 16> Registers;
3435 
3436   // Allow Q regs and just interpret them as the two D sub-registers.
3437   if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3438     Reg = getDRegFromQReg(Reg);
3439     EReg = MRI->getEncodingValue(Reg);
3440     Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3441     ++Reg;
3442   }
3443   const MCRegisterClass *RC;
3444   if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
3445     RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
3446   else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
3447     RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
3448   else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
3449     RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
3450   else
3451     return Error(RegLoc, "invalid register in register list");
3452 
3453   // Store the register.
3454   EReg = MRI->getEncodingValue(Reg);
3455   Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3456 
3457   // This starts immediately after the first register token in the list,
3458   // so we can see either a comma or a minus (range separator) as a legal
3459   // next token.
3460   while (Parser.getTok().is(AsmToken::Comma) ||
3461          Parser.getTok().is(AsmToken::Minus)) {
3462     if (Parser.getTok().is(AsmToken::Minus)) {
3463       Parser.Lex(); // Eat the minus.
3464       SMLoc AfterMinusLoc = Parser.getTok().getLoc();
3465       int EndReg = tryParseRegister();
3466       if (EndReg == -1)
3467         return Error(AfterMinusLoc, "register expected");
3468       // Allow Q regs and just interpret them as the two D sub-registers.
3469       if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
3470         EndReg = getDRegFromQReg(EndReg) + 1;
3471       // If the register is the same as the start reg, there's nothing
3472       // more to do.
3473       if (Reg == EndReg)
3474         continue;
3475       // The register must be in the same register class as the first.
3476       if (!RC->contains(EndReg))
3477         return Error(AfterMinusLoc, "invalid register in register list");
3478       // Ranges must go from low to high.
3479       if (MRI->getEncodingValue(Reg) > MRI->getEncodingValue(EndReg))
3480         return Error(AfterMinusLoc, "bad range in register list");
3481 
3482       // Add all the registers in the range to the register list.
3483       while (Reg != EndReg) {
3484         Reg = getNextRegister(Reg);
3485         EReg = MRI->getEncodingValue(Reg);
3486         Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3487       }
3488       continue;
3489     }
3490     Parser.Lex(); // Eat the comma.
3491     RegLoc = Parser.getTok().getLoc();
3492     int OldReg = Reg;
3493     const AsmToken RegTok = Parser.getTok();
3494     Reg = tryParseRegister();
3495     if (Reg == -1)
3496       return Error(RegLoc, "register expected");
3497     // Allow Q regs and just interpret them as the two D sub-registers.
3498     bool isQReg = false;
3499     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3500       Reg = getDRegFromQReg(Reg);
3501       isQReg = true;
3502     }
3503     // The register must be in the same register class as the first.
3504     if (!RC->contains(Reg))
3505       return Error(RegLoc, "invalid register in register list");
3506     // List must be monotonically increasing.
3507     if (MRI->getEncodingValue(Reg) < MRI->getEncodingValue(OldReg)) {
3508       if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
3509         Warning(RegLoc, "register list not in ascending order");
3510       else
3511         return Error(RegLoc, "register list not in ascending order");
3512     }
3513     if (MRI->getEncodingValue(Reg) == MRI->getEncodingValue(OldReg)) {
3514       Warning(RegLoc, "duplicated register (" + RegTok.getString() +
3515               ") in register list");
3516       continue;
3517     }
3518     // VFP register lists must also be contiguous.
3519     if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
3520         Reg != OldReg + 1)
3521       return Error(RegLoc, "non-contiguous register range");
3522     EReg = MRI->getEncodingValue(Reg);
3523     Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3524     if (isQReg) {
3525       EReg = MRI->getEncodingValue(++Reg);
3526       Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3527     }
3528   }
3529 
3530   if (Parser.getTok().isNot(AsmToken::RCurly))
3531     return Error(Parser.getTok().getLoc(), "'}' expected");
3532   SMLoc E = Parser.getTok().getEndLoc();
3533   Parser.Lex(); // Eat '}' token.
3534 
3535   // Push the register list operand.
3536   Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
3537 
3538   // The ARM system instruction variants for LDM/STM have a '^' token here.
3539   if (Parser.getTok().is(AsmToken::Caret)) {
3540     Operands.push_back(ARMOperand::CreateToken("^",Parser.getTok().getLoc()));
3541     Parser.Lex(); // Eat '^' token.
3542   }
3543 
3544   return false;
3545 }
3546 
3547 // Helper function to parse the lane index for vector lists.
3548 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3549 parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index, SMLoc &EndLoc) {
3550   MCAsmParser &Parser = getParser();
3551   Index = 0; // Always return a defined index value.
3552   if (Parser.getTok().is(AsmToken::LBrac)) {
3553     Parser.Lex(); // Eat the '['.
3554     if (Parser.getTok().is(AsmToken::RBrac)) {
3555       // "Dn[]" is the 'all lanes' syntax.
3556       LaneKind = AllLanes;
3557       EndLoc = Parser.getTok().getEndLoc();
3558       Parser.Lex(); // Eat the ']'.
3559       return MatchOperand_Success;
3560     }
3561 
3562     // There's an optional '#' token here. Normally there wouldn't be, but
3563     // inline assemble puts one in, and it's friendly to accept that.
3564     if (Parser.getTok().is(AsmToken::Hash))
3565       Parser.Lex(); // Eat '#' or '$'.
3566 
3567     const MCExpr *LaneIndex;
3568     SMLoc Loc = Parser.getTok().getLoc();
3569     if (getParser().parseExpression(LaneIndex)) {
3570       Error(Loc, "illegal expression");
3571       return MatchOperand_ParseFail;
3572     }
3573     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LaneIndex);
3574     if (!CE) {
3575       Error(Loc, "lane index must be empty or an integer");
3576       return MatchOperand_ParseFail;
3577     }
3578     if (Parser.getTok().isNot(AsmToken::RBrac)) {
3579       Error(Parser.getTok().getLoc(), "']' expected");
3580       return MatchOperand_ParseFail;
3581     }
3582     EndLoc = Parser.getTok().getEndLoc();
3583     Parser.Lex(); // Eat the ']'.
3584     int64_t Val = CE->getValue();
3585 
3586     // FIXME: Make this range check context sensitive for .8, .16, .32.
3587     if (Val < 0 || Val > 7) {
3588       Error(Parser.getTok().getLoc(), "lane index out of range");
3589       return MatchOperand_ParseFail;
3590     }
3591     Index = Val;
3592     LaneKind = IndexedLane;
3593     return MatchOperand_Success;
3594   }
3595   LaneKind = NoLanes;
3596   return MatchOperand_Success;
3597 }
3598 
3599 // parse a vector register list
3600 ARMAsmParser::OperandMatchResultTy
3601 ARMAsmParser::parseVectorList(OperandVector &Operands) {
3602   MCAsmParser &Parser = getParser();
3603   VectorLaneTy LaneKind;
3604   unsigned LaneIndex;
3605   SMLoc S = Parser.getTok().getLoc();
3606   // As an extension (to match gas), support a plain D register or Q register
3607   // (without encosing curly braces) as a single or double entry list,
3608   // respectively.
3609   if (Parser.getTok().is(AsmToken::Identifier)) {
3610     SMLoc E = Parser.getTok().getEndLoc();
3611     int Reg = tryParseRegister();
3612     if (Reg == -1)
3613       return MatchOperand_NoMatch;
3614     if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg)) {
3615       OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex, E);
3616       if (Res != MatchOperand_Success)
3617         return Res;
3618       switch (LaneKind) {
3619       case NoLanes:
3620         Operands.push_back(ARMOperand::CreateVectorList(Reg, 1, false, S, E));
3621         break;
3622       case AllLanes:
3623         Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 1, false,
3624                                                                 S, E));
3625         break;
3626       case IndexedLane:
3627         Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 1,
3628                                                                LaneIndex,
3629                                                                false, S, E));
3630         break;
3631       }
3632       return MatchOperand_Success;
3633     }
3634     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3635       Reg = getDRegFromQReg(Reg);
3636       OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex, E);
3637       if (Res != MatchOperand_Success)
3638         return Res;
3639       switch (LaneKind) {
3640       case NoLanes:
3641         Reg = MRI->getMatchingSuperReg(Reg, ARM::dsub_0,
3642                                    &ARMMCRegisterClasses[ARM::DPairRegClassID]);
3643         Operands.push_back(ARMOperand::CreateVectorList(Reg, 2, false, S, E));
3644         break;
3645       case AllLanes:
3646         Reg = MRI->getMatchingSuperReg(Reg, ARM::dsub_0,
3647                                    &ARMMCRegisterClasses[ARM::DPairRegClassID]);
3648         Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 2, false,
3649                                                                 S, E));
3650         break;
3651       case IndexedLane:
3652         Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 2,
3653                                                                LaneIndex,
3654                                                                false, S, E));
3655         break;
3656       }
3657       return MatchOperand_Success;
3658     }
3659     Error(S, "vector register expected");
3660     return MatchOperand_ParseFail;
3661   }
3662 
3663   if (Parser.getTok().isNot(AsmToken::LCurly))
3664     return MatchOperand_NoMatch;
3665 
3666   Parser.Lex(); // Eat '{' token.
3667   SMLoc RegLoc = Parser.getTok().getLoc();
3668 
3669   int Reg = tryParseRegister();
3670   if (Reg == -1) {
3671     Error(RegLoc, "register expected");
3672     return MatchOperand_ParseFail;
3673   }
3674   unsigned Count = 1;
3675   int Spacing = 0;
3676   unsigned FirstReg = Reg;
3677   // The list is of D registers, but we also allow Q regs and just interpret
3678   // them as the two D sub-registers.
3679   if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3680     FirstReg = Reg = getDRegFromQReg(Reg);
3681     Spacing = 1; // double-spacing requires explicit D registers, otherwise
3682                  // it's ambiguous with four-register single spaced.
3683     ++Reg;
3684     ++Count;
3685   }
3686 
3687   SMLoc E;
3688   if (parseVectorLane(LaneKind, LaneIndex, E) != MatchOperand_Success)
3689     return MatchOperand_ParseFail;
3690 
3691   while (Parser.getTok().is(AsmToken::Comma) ||
3692          Parser.getTok().is(AsmToken::Minus)) {
3693     if (Parser.getTok().is(AsmToken::Minus)) {
3694       if (!Spacing)
3695         Spacing = 1; // Register range implies a single spaced list.
3696       else if (Spacing == 2) {
3697         Error(Parser.getTok().getLoc(),
3698               "sequential registers in double spaced list");
3699         return MatchOperand_ParseFail;
3700       }
3701       Parser.Lex(); // Eat the minus.
3702       SMLoc AfterMinusLoc = Parser.getTok().getLoc();
3703       int EndReg = tryParseRegister();
3704       if (EndReg == -1) {
3705         Error(AfterMinusLoc, "register expected");
3706         return MatchOperand_ParseFail;
3707       }
3708       // Allow Q regs and just interpret them as the two D sub-registers.
3709       if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
3710         EndReg = getDRegFromQReg(EndReg) + 1;
3711       // If the register is the same as the start reg, there's nothing
3712       // more to do.
3713       if (Reg == EndReg)
3714         continue;
3715       // The register must be in the same register class as the first.
3716       if (!ARMMCRegisterClasses[ARM::DPRRegClassID].contains(EndReg)) {
3717         Error(AfterMinusLoc, "invalid register in register list");
3718         return MatchOperand_ParseFail;
3719       }
3720       // Ranges must go from low to high.
3721       if (Reg > EndReg) {
3722         Error(AfterMinusLoc, "bad range in register list");
3723         return MatchOperand_ParseFail;
3724       }
3725       // Parse the lane specifier if present.
3726       VectorLaneTy NextLaneKind;
3727       unsigned NextLaneIndex;
3728       if (parseVectorLane(NextLaneKind, NextLaneIndex, E) !=
3729           MatchOperand_Success)
3730         return MatchOperand_ParseFail;
3731       if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3732         Error(AfterMinusLoc, "mismatched lane index in register list");
3733         return MatchOperand_ParseFail;
3734       }
3735 
3736       // Add all the registers in the range to the register list.
3737       Count += EndReg - Reg;
3738       Reg = EndReg;
3739       continue;
3740     }
3741     Parser.Lex(); // Eat the comma.
3742     RegLoc = Parser.getTok().getLoc();
3743     int OldReg = Reg;
3744     Reg = tryParseRegister();
3745     if (Reg == -1) {
3746       Error(RegLoc, "register expected");
3747       return MatchOperand_ParseFail;
3748     }
3749     // vector register lists must be contiguous.
3750     // It's OK to use the enumeration values directly here rather, as the
3751     // VFP register classes have the enum sorted properly.
3752     //
3753     // The list is of D registers, but we also allow Q regs and just interpret
3754     // them as the two D sub-registers.
3755     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3756       if (!Spacing)
3757         Spacing = 1; // Register range implies a single spaced list.
3758       else if (Spacing == 2) {
3759         Error(RegLoc,
3760               "invalid register in double-spaced list (must be 'D' register')");
3761         return MatchOperand_ParseFail;
3762       }
3763       Reg = getDRegFromQReg(Reg);
3764       if (Reg != OldReg + 1) {
3765         Error(RegLoc, "non-contiguous register range");
3766         return MatchOperand_ParseFail;
3767       }
3768       ++Reg;
3769       Count += 2;
3770       // Parse the lane specifier if present.
3771       VectorLaneTy NextLaneKind;
3772       unsigned NextLaneIndex;
3773       SMLoc LaneLoc = Parser.getTok().getLoc();
3774       if (parseVectorLane(NextLaneKind, NextLaneIndex, E) !=
3775           MatchOperand_Success)
3776         return MatchOperand_ParseFail;
3777       if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3778         Error(LaneLoc, "mismatched lane index in register list");
3779         return MatchOperand_ParseFail;
3780       }
3781       continue;
3782     }
3783     // Normal D register.
3784     // Figure out the register spacing (single or double) of the list if
3785     // we don't know it already.
3786     if (!Spacing)
3787       Spacing = 1 + (Reg == OldReg + 2);
3788 
3789     // Just check that it's contiguous and keep going.
3790     if (Reg != OldReg + Spacing) {
3791       Error(RegLoc, "non-contiguous register range");
3792       return MatchOperand_ParseFail;
3793     }
3794     ++Count;
3795     // Parse the lane specifier if present.
3796     VectorLaneTy NextLaneKind;
3797     unsigned NextLaneIndex;
3798     SMLoc EndLoc = Parser.getTok().getLoc();
3799     if (parseVectorLane(NextLaneKind, NextLaneIndex, E) != MatchOperand_Success)
3800       return MatchOperand_ParseFail;
3801     if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3802       Error(EndLoc, "mismatched lane index in register list");
3803       return MatchOperand_ParseFail;
3804     }
3805   }
3806 
3807   if (Parser.getTok().isNot(AsmToken::RCurly)) {
3808     Error(Parser.getTok().getLoc(), "'}' expected");
3809     return MatchOperand_ParseFail;
3810   }
3811   E = Parser.getTok().getEndLoc();
3812   Parser.Lex(); // Eat '}' token.
3813 
3814   switch (LaneKind) {
3815   case NoLanes:
3816     // Two-register operands have been converted to the
3817     // composite register classes.
3818     if (Count == 2) {
3819       const MCRegisterClass *RC = (Spacing == 1) ?
3820         &ARMMCRegisterClasses[ARM::DPairRegClassID] :
3821         &ARMMCRegisterClasses[ARM::DPairSpcRegClassID];
3822       FirstReg = MRI->getMatchingSuperReg(FirstReg, ARM::dsub_0, RC);
3823     }
3824 
3825     Operands.push_back(ARMOperand::CreateVectorList(FirstReg, Count,
3826                                                     (Spacing == 2), S, E));
3827     break;
3828   case AllLanes:
3829     // Two-register operands have been converted to the
3830     // composite register classes.
3831     if (Count == 2) {
3832       const MCRegisterClass *RC = (Spacing == 1) ?
3833         &ARMMCRegisterClasses[ARM::DPairRegClassID] :
3834         &ARMMCRegisterClasses[ARM::DPairSpcRegClassID];
3835       FirstReg = MRI->getMatchingSuperReg(FirstReg, ARM::dsub_0, RC);
3836     }
3837     Operands.push_back(ARMOperand::CreateVectorListAllLanes(FirstReg, Count,
3838                                                             (Spacing == 2),
3839                                                             S, E));
3840     break;
3841   case IndexedLane:
3842     Operands.push_back(ARMOperand::CreateVectorListIndexed(FirstReg, Count,
3843                                                            LaneIndex,
3844                                                            (Spacing == 2),
3845                                                            S, E));
3846     break;
3847   }
3848   return MatchOperand_Success;
3849 }
3850 
3851 /// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
3852 ARMAsmParser::OperandMatchResultTy
3853 ARMAsmParser::parseMemBarrierOptOperand(OperandVector &Operands) {
3854   MCAsmParser &Parser = getParser();
3855   SMLoc S = Parser.getTok().getLoc();
3856   const AsmToken &Tok = Parser.getTok();
3857   unsigned Opt;
3858 
3859   if (Tok.is(AsmToken::Identifier)) {
3860     StringRef OptStr = Tok.getString();
3861 
3862     Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()).lower())
3863       .Case("sy",    ARM_MB::SY)
3864       .Case("st",    ARM_MB::ST)
3865       .Case("ld",    ARM_MB::LD)
3866       .Case("sh",    ARM_MB::ISH)
3867       .Case("ish",   ARM_MB::ISH)
3868       .Case("shst",  ARM_MB::ISHST)
3869       .Case("ishst", ARM_MB::ISHST)
3870       .Case("ishld", ARM_MB::ISHLD)
3871       .Case("nsh",   ARM_MB::NSH)
3872       .Case("un",    ARM_MB::NSH)
3873       .Case("nshst", ARM_MB::NSHST)
3874       .Case("nshld", ARM_MB::NSHLD)
3875       .Case("unst",  ARM_MB::NSHST)
3876       .Case("osh",   ARM_MB::OSH)
3877       .Case("oshst", ARM_MB::OSHST)
3878       .Case("oshld", ARM_MB::OSHLD)
3879       .Default(~0U);
3880 
3881     // ishld, oshld, nshld and ld are only available from ARMv8.
3882     if (!hasV8Ops() && (Opt == ARM_MB::ISHLD || Opt == ARM_MB::OSHLD ||
3883                         Opt == ARM_MB::NSHLD || Opt == ARM_MB::LD))
3884       Opt = ~0U;
3885 
3886     if (Opt == ~0U)
3887       return MatchOperand_NoMatch;
3888 
3889     Parser.Lex(); // Eat identifier token.
3890   } else if (Tok.is(AsmToken::Hash) ||
3891              Tok.is(AsmToken::Dollar) ||
3892              Tok.is(AsmToken::Integer)) {
3893     if (Parser.getTok().isNot(AsmToken::Integer))
3894       Parser.Lex(); // Eat '#' or '$'.
3895     SMLoc Loc = Parser.getTok().getLoc();
3896 
3897     const MCExpr *MemBarrierID;
3898     if (getParser().parseExpression(MemBarrierID)) {
3899       Error(Loc, "illegal expression");
3900       return MatchOperand_ParseFail;
3901     }
3902 
3903     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(MemBarrierID);
3904     if (!CE) {
3905       Error(Loc, "constant expression expected");
3906       return MatchOperand_ParseFail;
3907     }
3908 
3909     int Val = CE->getValue();
3910     if (Val & ~0xf) {
3911       Error(Loc, "immediate value out of range");
3912       return MatchOperand_ParseFail;
3913     }
3914 
3915     Opt = ARM_MB::RESERVED_0 + Val;
3916   } else
3917     return MatchOperand_ParseFail;
3918 
3919   Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
3920   return MatchOperand_Success;
3921 }
3922 
3923 /// parseInstSyncBarrierOptOperand - Try to parse ISB inst sync barrier options.
3924 ARMAsmParser::OperandMatchResultTy
3925 ARMAsmParser::parseInstSyncBarrierOptOperand(OperandVector &Operands) {
3926   MCAsmParser &Parser = getParser();
3927   SMLoc S = Parser.getTok().getLoc();
3928   const AsmToken &Tok = Parser.getTok();
3929   unsigned Opt;
3930 
3931   if (Tok.is(AsmToken::Identifier)) {
3932     StringRef OptStr = Tok.getString();
3933 
3934     if (OptStr.equals_lower("sy"))
3935       Opt = ARM_ISB::SY;
3936     else
3937       return MatchOperand_NoMatch;
3938 
3939     Parser.Lex(); // Eat identifier token.
3940   } else if (Tok.is(AsmToken::Hash) ||
3941              Tok.is(AsmToken::Dollar) ||
3942              Tok.is(AsmToken::Integer)) {
3943     if (Parser.getTok().isNot(AsmToken::Integer))
3944       Parser.Lex(); // Eat '#' or '$'.
3945     SMLoc Loc = Parser.getTok().getLoc();
3946 
3947     const MCExpr *ISBarrierID;
3948     if (getParser().parseExpression(ISBarrierID)) {
3949       Error(Loc, "illegal expression");
3950       return MatchOperand_ParseFail;
3951     }
3952 
3953     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ISBarrierID);
3954     if (!CE) {
3955       Error(Loc, "constant expression expected");
3956       return MatchOperand_ParseFail;
3957     }
3958 
3959     int Val = CE->getValue();
3960     if (Val & ~0xf) {
3961       Error(Loc, "immediate value out of range");
3962       return MatchOperand_ParseFail;
3963     }
3964 
3965     Opt = ARM_ISB::RESERVED_0 + Val;
3966   } else
3967     return MatchOperand_ParseFail;
3968 
3969   Operands.push_back(ARMOperand::CreateInstSyncBarrierOpt(
3970           (ARM_ISB::InstSyncBOpt)Opt, S));
3971   return MatchOperand_Success;
3972 }
3973 
3974 
3975 /// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
3976 ARMAsmParser::OperandMatchResultTy
3977 ARMAsmParser::parseProcIFlagsOperand(OperandVector &Operands) {
3978   MCAsmParser &Parser = getParser();
3979   SMLoc S = Parser.getTok().getLoc();
3980   const AsmToken &Tok = Parser.getTok();
3981   if (!Tok.is(AsmToken::Identifier))
3982     return MatchOperand_NoMatch;
3983   StringRef IFlagsStr = Tok.getString();
3984 
3985   // An iflags string of "none" is interpreted to mean that none of the AIF
3986   // bits are set.  Not a terribly useful instruction, but a valid encoding.
3987   unsigned IFlags = 0;
3988   if (IFlagsStr != "none") {
3989         for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
3990       unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
3991         .Case("a", ARM_PROC::A)
3992         .Case("i", ARM_PROC::I)
3993         .Case("f", ARM_PROC::F)
3994         .Default(~0U);
3995 
3996       // If some specific iflag is already set, it means that some letter is
3997       // present more than once, this is not acceptable.
3998       if (Flag == ~0U || (IFlags & Flag))
3999         return MatchOperand_NoMatch;
4000 
4001       IFlags |= Flag;
4002     }
4003   }
4004 
4005   Parser.Lex(); // Eat identifier token.
4006   Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
4007   return MatchOperand_Success;
4008 }
4009 
4010 /// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
4011 ARMAsmParser::OperandMatchResultTy
4012 ARMAsmParser::parseMSRMaskOperand(OperandVector &Operands) {
4013   MCAsmParser &Parser = getParser();
4014   SMLoc S = Parser.getTok().getLoc();
4015   const AsmToken &Tok = Parser.getTok();
4016   if (!Tok.is(AsmToken::Identifier))
4017     return MatchOperand_NoMatch;
4018   StringRef Mask = Tok.getString();
4019 
4020   if (isMClass()) {
4021     // See ARMv6-M 10.1.1
4022     std::string Name = Mask.lower();
4023     unsigned FlagsVal = StringSwitch<unsigned>(Name)
4024       // Note: in the documentation:
4025       //  ARM deprecates using MSR APSR without a _<bits> qualifier as an alias
4026       //  for MSR APSR_nzcvq.
4027       // but we do make it an alias here.  This is so to get the "mask encoding"
4028       // bits correct on MSR APSR writes.
4029       //
4030       // FIXME: Note the 0xc00 "mask encoding" bits version of the registers
4031       // should really only be allowed when writing a special register.  Note
4032       // they get dropped in the MRS instruction reading a special register as
4033       // the SYSm field is only 8 bits.
4034       .Case("apsr", 0x800)
4035       .Case("apsr_nzcvq", 0x800)
4036       .Case("apsr_g", 0x400)
4037       .Case("apsr_nzcvqg", 0xc00)
4038       .Case("iapsr", 0x801)
4039       .Case("iapsr_nzcvq", 0x801)
4040       .Case("iapsr_g", 0x401)
4041       .Case("iapsr_nzcvqg", 0xc01)
4042       .Case("eapsr", 0x802)
4043       .Case("eapsr_nzcvq", 0x802)
4044       .Case("eapsr_g", 0x402)
4045       .Case("eapsr_nzcvqg", 0xc02)
4046       .Case("xpsr", 0x803)
4047       .Case("xpsr_nzcvq", 0x803)
4048       .Case("xpsr_g", 0x403)
4049       .Case("xpsr_nzcvqg", 0xc03)
4050       .Case("ipsr", 0x805)
4051       .Case("epsr", 0x806)
4052       .Case("iepsr", 0x807)
4053       .Case("msp", 0x808)
4054       .Case("psp", 0x809)
4055       .Case("primask", 0x810)
4056       .Case("basepri", 0x811)
4057       .Case("basepri_max", 0x812)
4058       .Case("faultmask", 0x813)
4059       .Case("control", 0x814)
4060       .Case("msplim", 0x80a)
4061       .Case("psplim", 0x80b)
4062       .Case("msp_ns", 0x888)
4063       .Case("psp_ns", 0x889)
4064       .Case("msplim_ns", 0x88a)
4065       .Case("psplim_ns", 0x88b)
4066       .Case("primask_ns", 0x890)
4067       .Case("basepri_ns", 0x891)
4068       .Case("basepri_max_ns", 0x892)
4069       .Case("faultmask_ns", 0x893)
4070       .Case("control_ns", 0x894)
4071       .Case("sp_ns", 0x898)
4072       .Default(~0U);
4073 
4074     if (FlagsVal == ~0U)
4075       return MatchOperand_NoMatch;
4076 
4077     if (!hasDSP() && (FlagsVal & 0x400))
4078       // The _g and _nzcvqg versions are only valid if the DSP extension is
4079       // available.
4080       return MatchOperand_NoMatch;
4081 
4082     if (!hasV7Ops() && FlagsVal >= 0x811 && FlagsVal <= 0x813)
4083       // basepri, basepri_max and faultmask only valid for V7m.
4084       return MatchOperand_NoMatch;
4085 
4086     if (!has8MSecExt() && (FlagsVal == 0x80a || FlagsVal == 0x80b ||
4087                              (FlagsVal > 0x814 && FlagsVal < 0xc00)))
4088       return MatchOperand_NoMatch;
4089 
4090     if (!hasV8MMainline() && (FlagsVal == 0x88a || FlagsVal == 0x88b ||
4091                               (FlagsVal > 0x890 && FlagsVal <= 0x893)))
4092       return MatchOperand_NoMatch;
4093 
4094     Parser.Lex(); // Eat identifier token.
4095     Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
4096     return MatchOperand_Success;
4097   }
4098 
4099   // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
4100   size_t Start = 0, Next = Mask.find('_');
4101   StringRef Flags = "";
4102   std::string SpecReg = Mask.slice(Start, Next).lower();
4103   if (Next != StringRef::npos)
4104     Flags = Mask.slice(Next+1, Mask.size());
4105 
4106   // FlagsVal contains the complete mask:
4107   // 3-0: Mask
4108   // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
4109   unsigned FlagsVal = 0;
4110 
4111   if (SpecReg == "apsr") {
4112     FlagsVal = StringSwitch<unsigned>(Flags)
4113     .Case("nzcvq",  0x8) // same as CPSR_f
4114     .Case("g",      0x4) // same as CPSR_s
4115     .Case("nzcvqg", 0xc) // same as CPSR_fs
4116     .Default(~0U);
4117 
4118     if (FlagsVal == ~0U) {
4119       if (!Flags.empty())
4120         return MatchOperand_NoMatch;
4121       else
4122         FlagsVal = 8; // No flag
4123     }
4124   } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
4125     // cpsr_all is an alias for cpsr_fc, as is plain cpsr.
4126     if (Flags == "all" || Flags == "")
4127       Flags = "fc";
4128     for (int i = 0, e = Flags.size(); i != e; ++i) {
4129       unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
4130       .Case("c", 1)
4131       .Case("x", 2)
4132       .Case("s", 4)
4133       .Case("f", 8)
4134       .Default(~0U);
4135 
4136       // If some specific flag is already set, it means that some letter is
4137       // present more than once, this is not acceptable.
4138       if (FlagsVal == ~0U || (FlagsVal & Flag))
4139         return MatchOperand_NoMatch;
4140       FlagsVal |= Flag;
4141     }
4142   } else // No match for special register.
4143     return MatchOperand_NoMatch;
4144 
4145   // Special register without flags is NOT equivalent to "fc" flags.
4146   // NOTE: This is a divergence from gas' behavior.  Uncommenting the following
4147   // two lines would enable gas compatibility at the expense of breaking
4148   // round-tripping.
4149   //
4150   // if (!FlagsVal)
4151   //  FlagsVal = 0x9;
4152 
4153   // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
4154   if (SpecReg == "spsr")
4155     FlagsVal |= 16;
4156 
4157   Parser.Lex(); // Eat identifier token.
4158   Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
4159   return MatchOperand_Success;
4160 }
4161 
4162 /// parseBankedRegOperand - Try to parse a banked register (e.g. "lr_irq") for
4163 /// use in the MRS/MSR instructions added to support virtualization.
4164 ARMAsmParser::OperandMatchResultTy
4165 ARMAsmParser::parseBankedRegOperand(OperandVector &Operands) {
4166   MCAsmParser &Parser = getParser();
4167   SMLoc S = Parser.getTok().getLoc();
4168   const AsmToken &Tok = Parser.getTok();
4169   if (!Tok.is(AsmToken::Identifier))
4170     return MatchOperand_NoMatch;
4171   StringRef RegName = Tok.getString();
4172 
4173   // The values here come from B9.2.3 of the ARM ARM, where bits 4-0 are SysM
4174   // and bit 5 is R.
4175   unsigned Encoding = StringSwitch<unsigned>(RegName.lower())
4176                           .Case("r8_usr", 0x00)
4177                           .Case("r9_usr", 0x01)
4178                           .Case("r10_usr", 0x02)
4179                           .Case("r11_usr", 0x03)
4180                           .Case("r12_usr", 0x04)
4181                           .Case("sp_usr", 0x05)
4182                           .Case("lr_usr", 0x06)
4183                           .Case("r8_fiq", 0x08)
4184                           .Case("r9_fiq", 0x09)
4185                           .Case("r10_fiq", 0x0a)
4186                           .Case("r11_fiq", 0x0b)
4187                           .Case("r12_fiq", 0x0c)
4188                           .Case("sp_fiq", 0x0d)
4189                           .Case("lr_fiq", 0x0e)
4190                           .Case("lr_irq", 0x10)
4191                           .Case("sp_irq", 0x11)
4192                           .Case("lr_svc", 0x12)
4193                           .Case("sp_svc", 0x13)
4194                           .Case("lr_abt", 0x14)
4195                           .Case("sp_abt", 0x15)
4196                           .Case("lr_und", 0x16)
4197                           .Case("sp_und", 0x17)
4198                           .Case("lr_mon", 0x1c)
4199                           .Case("sp_mon", 0x1d)
4200                           .Case("elr_hyp", 0x1e)
4201                           .Case("sp_hyp", 0x1f)
4202                           .Case("spsr_fiq", 0x2e)
4203                           .Case("spsr_irq", 0x30)
4204                           .Case("spsr_svc", 0x32)
4205                           .Case("spsr_abt", 0x34)
4206                           .Case("spsr_und", 0x36)
4207                           .Case("spsr_mon", 0x3c)
4208                           .Case("spsr_hyp", 0x3e)
4209                           .Default(~0U);
4210 
4211   if (Encoding == ~0U)
4212     return MatchOperand_NoMatch;
4213 
4214   Parser.Lex(); // Eat identifier token.
4215   Operands.push_back(ARMOperand::CreateBankedReg(Encoding, S));
4216   return MatchOperand_Success;
4217 }
4218 
4219 ARMAsmParser::OperandMatchResultTy
4220 ARMAsmParser::parsePKHImm(OperandVector &Operands, StringRef Op, int Low,
4221                           int High) {
4222   MCAsmParser &Parser = getParser();
4223   const AsmToken &Tok = Parser.getTok();
4224   if (Tok.isNot(AsmToken::Identifier)) {
4225     Error(Parser.getTok().getLoc(), Op + " operand expected.");
4226     return MatchOperand_ParseFail;
4227   }
4228   StringRef ShiftName = Tok.getString();
4229   std::string LowerOp = Op.lower();
4230   std::string UpperOp = Op.upper();
4231   if (ShiftName != LowerOp && ShiftName != UpperOp) {
4232     Error(Parser.getTok().getLoc(), Op + " operand expected.");
4233     return MatchOperand_ParseFail;
4234   }
4235   Parser.Lex(); // Eat shift type token.
4236 
4237   // There must be a '#' and a shift amount.
4238   if (Parser.getTok().isNot(AsmToken::Hash) &&
4239       Parser.getTok().isNot(AsmToken::Dollar)) {
4240     Error(Parser.getTok().getLoc(), "'#' expected");
4241     return MatchOperand_ParseFail;
4242   }
4243   Parser.Lex(); // Eat hash token.
4244 
4245   const MCExpr *ShiftAmount;
4246   SMLoc Loc = Parser.getTok().getLoc();
4247   SMLoc EndLoc;
4248   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
4249     Error(Loc, "illegal expression");
4250     return MatchOperand_ParseFail;
4251   }
4252   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
4253   if (!CE) {
4254     Error(Loc, "constant expression expected");
4255     return MatchOperand_ParseFail;
4256   }
4257   int Val = CE->getValue();
4258   if (Val < Low || Val > High) {
4259     Error(Loc, "immediate value out of range");
4260     return MatchOperand_ParseFail;
4261   }
4262 
4263   Operands.push_back(ARMOperand::CreateImm(CE, Loc, EndLoc));
4264 
4265   return MatchOperand_Success;
4266 }
4267 
4268 ARMAsmParser::OperandMatchResultTy
4269 ARMAsmParser::parseSetEndImm(OperandVector &Operands) {
4270   MCAsmParser &Parser = getParser();
4271   const AsmToken &Tok = Parser.getTok();
4272   SMLoc S = Tok.getLoc();
4273   if (Tok.isNot(AsmToken::Identifier)) {
4274     Error(S, "'be' or 'le' operand expected");
4275     return MatchOperand_ParseFail;
4276   }
4277   int Val = StringSwitch<int>(Tok.getString().lower())
4278     .Case("be", 1)
4279     .Case("le", 0)
4280     .Default(-1);
4281   Parser.Lex(); // Eat the token.
4282 
4283   if (Val == -1) {
4284     Error(S, "'be' or 'le' operand expected");
4285     return MatchOperand_ParseFail;
4286   }
4287   Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::create(Val,
4288                                                                   getContext()),
4289                                            S, Tok.getEndLoc()));
4290   return MatchOperand_Success;
4291 }
4292 
4293 /// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
4294 /// instructions. Legal values are:
4295 ///     lsl #n  'n' in [0,31]
4296 ///     asr #n  'n' in [1,32]
4297 ///             n == 32 encoded as n == 0.
4298 ARMAsmParser::OperandMatchResultTy
4299 ARMAsmParser::parseShifterImm(OperandVector &Operands) {
4300   MCAsmParser &Parser = getParser();
4301   const AsmToken &Tok = Parser.getTok();
4302   SMLoc S = Tok.getLoc();
4303   if (Tok.isNot(AsmToken::Identifier)) {
4304     Error(S, "shift operator 'asr' or 'lsl' expected");
4305     return MatchOperand_ParseFail;
4306   }
4307   StringRef ShiftName = Tok.getString();
4308   bool isASR;
4309   if (ShiftName == "lsl" || ShiftName == "LSL")
4310     isASR = false;
4311   else if (ShiftName == "asr" || ShiftName == "ASR")
4312     isASR = true;
4313   else {
4314     Error(S, "shift operator 'asr' or 'lsl' expected");
4315     return MatchOperand_ParseFail;
4316   }
4317   Parser.Lex(); // Eat the operator.
4318 
4319   // A '#' and a shift amount.
4320   if (Parser.getTok().isNot(AsmToken::Hash) &&
4321       Parser.getTok().isNot(AsmToken::Dollar)) {
4322     Error(Parser.getTok().getLoc(), "'#' expected");
4323     return MatchOperand_ParseFail;
4324   }
4325   Parser.Lex(); // Eat hash token.
4326   SMLoc ExLoc = Parser.getTok().getLoc();
4327 
4328   const MCExpr *ShiftAmount;
4329   SMLoc EndLoc;
4330   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
4331     Error(ExLoc, "malformed shift expression");
4332     return MatchOperand_ParseFail;
4333   }
4334   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
4335   if (!CE) {
4336     Error(ExLoc, "shift amount must be an immediate");
4337     return MatchOperand_ParseFail;
4338   }
4339 
4340   int64_t Val = CE->getValue();
4341   if (isASR) {
4342     // Shift amount must be in [1,32]
4343     if (Val < 1 || Val > 32) {
4344       Error(ExLoc, "'asr' shift amount must be in range [1,32]");
4345       return MatchOperand_ParseFail;
4346     }
4347     // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
4348     if (isThumb() && Val == 32) {
4349       Error(ExLoc, "'asr #32' shift amount not allowed in Thumb mode");
4350       return MatchOperand_ParseFail;
4351     }
4352     if (Val == 32) Val = 0;
4353   } else {
4354     // Shift amount must be in [1,32]
4355     if (Val < 0 || Val > 31) {
4356       Error(ExLoc, "'lsr' shift amount must be in range [0,31]");
4357       return MatchOperand_ParseFail;
4358     }
4359   }
4360 
4361   Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, EndLoc));
4362 
4363   return MatchOperand_Success;
4364 }
4365 
4366 /// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
4367 /// of instructions. Legal values are:
4368 ///     ror #n  'n' in {0, 8, 16, 24}
4369 ARMAsmParser::OperandMatchResultTy
4370 ARMAsmParser::parseRotImm(OperandVector &Operands) {
4371   MCAsmParser &Parser = getParser();
4372   const AsmToken &Tok = Parser.getTok();
4373   SMLoc S = Tok.getLoc();
4374   if (Tok.isNot(AsmToken::Identifier))
4375     return MatchOperand_NoMatch;
4376   StringRef ShiftName = Tok.getString();
4377   if (ShiftName != "ror" && ShiftName != "ROR")
4378     return MatchOperand_NoMatch;
4379   Parser.Lex(); // Eat the operator.
4380 
4381   // A '#' and a rotate amount.
4382   if (Parser.getTok().isNot(AsmToken::Hash) &&
4383       Parser.getTok().isNot(AsmToken::Dollar)) {
4384     Error(Parser.getTok().getLoc(), "'#' expected");
4385     return MatchOperand_ParseFail;
4386   }
4387   Parser.Lex(); // Eat hash token.
4388   SMLoc ExLoc = Parser.getTok().getLoc();
4389 
4390   const MCExpr *ShiftAmount;
4391   SMLoc EndLoc;
4392   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
4393     Error(ExLoc, "malformed rotate expression");
4394     return MatchOperand_ParseFail;
4395   }
4396   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
4397   if (!CE) {
4398     Error(ExLoc, "rotate amount must be an immediate");
4399     return MatchOperand_ParseFail;
4400   }
4401 
4402   int64_t Val = CE->getValue();
4403   // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
4404   // normally, zero is represented in asm by omitting the rotate operand
4405   // entirely.
4406   if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
4407     Error(ExLoc, "'ror' rotate amount must be 8, 16, or 24");
4408     return MatchOperand_ParseFail;
4409   }
4410 
4411   Operands.push_back(ARMOperand::CreateRotImm(Val, S, EndLoc));
4412 
4413   return MatchOperand_Success;
4414 }
4415 
4416 ARMAsmParser::OperandMatchResultTy
4417 ARMAsmParser::parseModImm(OperandVector &Operands) {
4418   MCAsmParser &Parser = getParser();
4419   MCAsmLexer &Lexer = getLexer();
4420   int64_t Imm1, Imm2;
4421 
4422   SMLoc S = Parser.getTok().getLoc();
4423 
4424   // 1) A mod_imm operand can appear in the place of a register name:
4425   //   add r0, #mod_imm
4426   //   add r0, r0, #mod_imm
4427   // to correctly handle the latter, we bail out as soon as we see an
4428   // identifier.
4429   //
4430   // 2) Similarly, we do not want to parse into complex operands:
4431   //   mov r0, #mod_imm
4432   //   mov r0, :lower16:(_foo)
4433   if (Parser.getTok().is(AsmToken::Identifier) ||
4434       Parser.getTok().is(AsmToken::Colon))
4435     return MatchOperand_NoMatch;
4436 
4437   // Hash (dollar) is optional as per the ARMARM
4438   if (Parser.getTok().is(AsmToken::Hash) ||
4439       Parser.getTok().is(AsmToken::Dollar)) {
4440     // Avoid parsing into complex operands (#:)
4441     if (Lexer.peekTok().is(AsmToken::Colon))
4442       return MatchOperand_NoMatch;
4443 
4444     // Eat the hash (dollar)
4445     Parser.Lex();
4446   }
4447 
4448   SMLoc Sx1, Ex1;
4449   Sx1 = Parser.getTok().getLoc();
4450   const MCExpr *Imm1Exp;
4451   if (getParser().parseExpression(Imm1Exp, Ex1)) {
4452     Error(Sx1, "malformed expression");
4453     return MatchOperand_ParseFail;
4454   }
4455 
4456   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm1Exp);
4457 
4458   if (CE) {
4459     // Immediate must fit within 32-bits
4460     Imm1 = CE->getValue();
4461     int Enc = ARM_AM::getSOImmVal(Imm1);
4462     if (Enc != -1 && Parser.getTok().is(AsmToken::EndOfStatement)) {
4463       // We have a match!
4464       Operands.push_back(ARMOperand::CreateModImm((Enc & 0xFF),
4465                                                   (Enc & 0xF00) >> 7,
4466                                                   Sx1, Ex1));
4467       return MatchOperand_Success;
4468     }
4469 
4470     // We have parsed an immediate which is not for us, fallback to a plain
4471     // immediate. This can happen for instruction aliases. For an example,
4472     // ARMInstrInfo.td defines the alias [mov <-> mvn] which can transform
4473     // a mov (mvn) with a mod_imm_neg/mod_imm_not operand into the opposite
4474     // instruction with a mod_imm operand. The alias is defined such that the
4475     // parser method is shared, that's why we have to do this here.
4476     if (Parser.getTok().is(AsmToken::EndOfStatement)) {
4477       Operands.push_back(ARMOperand::CreateImm(Imm1Exp, Sx1, Ex1));
4478       return MatchOperand_Success;
4479     }
4480   } else {
4481     // Operands like #(l1 - l2) can only be evaluated at a later stage (via an
4482     // MCFixup). Fallback to a plain immediate.
4483     Operands.push_back(ARMOperand::CreateImm(Imm1Exp, Sx1, Ex1));
4484     return MatchOperand_Success;
4485   }
4486 
4487   // From this point onward, we expect the input to be a (#bits, #rot) pair
4488   if (Parser.getTok().isNot(AsmToken::Comma)) {
4489     Error(Sx1, "expected modified immediate operand: #[0, 255], #even[0-30]");
4490     return MatchOperand_ParseFail;
4491   }
4492 
4493   if (Imm1 & ~0xFF) {
4494     Error(Sx1, "immediate operand must a number in the range [0, 255]");
4495     return MatchOperand_ParseFail;
4496   }
4497 
4498   // Eat the comma
4499   Parser.Lex();
4500 
4501   // Repeat for #rot
4502   SMLoc Sx2, Ex2;
4503   Sx2 = Parser.getTok().getLoc();
4504 
4505   // Eat the optional hash (dollar)
4506   if (Parser.getTok().is(AsmToken::Hash) ||
4507       Parser.getTok().is(AsmToken::Dollar))
4508     Parser.Lex();
4509 
4510   const MCExpr *Imm2Exp;
4511   if (getParser().parseExpression(Imm2Exp, Ex2)) {
4512     Error(Sx2, "malformed expression");
4513     return MatchOperand_ParseFail;
4514   }
4515 
4516   CE = dyn_cast<MCConstantExpr>(Imm2Exp);
4517 
4518   if (CE) {
4519     Imm2 = CE->getValue();
4520     if (!(Imm2 & ~0x1E)) {
4521       // We have a match!
4522       Operands.push_back(ARMOperand::CreateModImm(Imm1, Imm2, S, Ex2));
4523       return MatchOperand_Success;
4524     }
4525     Error(Sx2, "immediate operand must an even number in the range [0, 30]");
4526     return MatchOperand_ParseFail;
4527   } else {
4528     Error(Sx2, "constant expression expected");
4529     return MatchOperand_ParseFail;
4530   }
4531 }
4532 
4533 ARMAsmParser::OperandMatchResultTy
4534 ARMAsmParser::parseBitfield(OperandVector &Operands) {
4535   MCAsmParser &Parser = getParser();
4536   SMLoc S = Parser.getTok().getLoc();
4537   // The bitfield descriptor is really two operands, the LSB and the width.
4538   if (Parser.getTok().isNot(AsmToken::Hash) &&
4539       Parser.getTok().isNot(AsmToken::Dollar)) {
4540     Error(Parser.getTok().getLoc(), "'#' expected");
4541     return MatchOperand_ParseFail;
4542   }
4543   Parser.Lex(); // Eat hash token.
4544 
4545   const MCExpr *LSBExpr;
4546   SMLoc E = Parser.getTok().getLoc();
4547   if (getParser().parseExpression(LSBExpr)) {
4548     Error(E, "malformed immediate expression");
4549     return MatchOperand_ParseFail;
4550   }
4551   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
4552   if (!CE) {
4553     Error(E, "'lsb' operand must be an immediate");
4554     return MatchOperand_ParseFail;
4555   }
4556 
4557   int64_t LSB = CE->getValue();
4558   // The LSB must be in the range [0,31]
4559   if (LSB < 0 || LSB > 31) {
4560     Error(E, "'lsb' operand must be in the range [0,31]");
4561     return MatchOperand_ParseFail;
4562   }
4563   E = Parser.getTok().getLoc();
4564 
4565   // Expect another immediate operand.
4566   if (Parser.getTok().isNot(AsmToken::Comma)) {
4567     Error(Parser.getTok().getLoc(), "too few operands");
4568     return MatchOperand_ParseFail;
4569   }
4570   Parser.Lex(); // Eat hash token.
4571   if (Parser.getTok().isNot(AsmToken::Hash) &&
4572       Parser.getTok().isNot(AsmToken::Dollar)) {
4573     Error(Parser.getTok().getLoc(), "'#' expected");
4574     return MatchOperand_ParseFail;
4575   }
4576   Parser.Lex(); // Eat hash token.
4577 
4578   const MCExpr *WidthExpr;
4579   SMLoc EndLoc;
4580   if (getParser().parseExpression(WidthExpr, EndLoc)) {
4581     Error(E, "malformed immediate expression");
4582     return MatchOperand_ParseFail;
4583   }
4584   CE = dyn_cast<MCConstantExpr>(WidthExpr);
4585   if (!CE) {
4586     Error(E, "'width' operand must be an immediate");
4587     return MatchOperand_ParseFail;
4588   }
4589 
4590   int64_t Width = CE->getValue();
4591   // The LSB must be in the range [1,32-lsb]
4592   if (Width < 1 || Width > 32 - LSB) {
4593     Error(E, "'width' operand must be in the range [1,32-lsb]");
4594     return MatchOperand_ParseFail;
4595   }
4596 
4597   Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, EndLoc));
4598 
4599   return MatchOperand_Success;
4600 }
4601 
4602 ARMAsmParser::OperandMatchResultTy
4603 ARMAsmParser::parsePostIdxReg(OperandVector &Operands) {
4604   // Check for a post-index addressing register operand. Specifically:
4605   // postidx_reg := '+' register {, shift}
4606   //              | '-' register {, shift}
4607   //              | register {, shift}
4608 
4609   // This method must return MatchOperand_NoMatch without consuming any tokens
4610   // in the case where there is no match, as other alternatives take other
4611   // parse methods.
4612   MCAsmParser &Parser = getParser();
4613   AsmToken Tok = Parser.getTok();
4614   SMLoc S = Tok.getLoc();
4615   bool haveEaten = false;
4616   bool isAdd = true;
4617   if (Tok.is(AsmToken::Plus)) {
4618     Parser.Lex(); // Eat the '+' token.
4619     haveEaten = true;
4620   } else if (Tok.is(AsmToken::Minus)) {
4621     Parser.Lex(); // Eat the '-' token.
4622     isAdd = false;
4623     haveEaten = true;
4624   }
4625 
4626   SMLoc E = Parser.getTok().getEndLoc();
4627   int Reg = tryParseRegister();
4628   if (Reg == -1) {
4629     if (!haveEaten)
4630       return MatchOperand_NoMatch;
4631     Error(Parser.getTok().getLoc(), "register expected");
4632     return MatchOperand_ParseFail;
4633   }
4634 
4635   ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
4636   unsigned ShiftImm = 0;
4637   if (Parser.getTok().is(AsmToken::Comma)) {
4638     Parser.Lex(); // Eat the ','.
4639     if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
4640       return MatchOperand_ParseFail;
4641 
4642     // FIXME: Only approximates end...may include intervening whitespace.
4643     E = Parser.getTok().getLoc();
4644   }
4645 
4646   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
4647                                                   ShiftImm, S, E));
4648 
4649   return MatchOperand_Success;
4650 }
4651 
4652 ARMAsmParser::OperandMatchResultTy
4653 ARMAsmParser::parseAM3Offset(OperandVector &Operands) {
4654   // Check for a post-index addressing register operand. Specifically:
4655   // am3offset := '+' register
4656   //              | '-' register
4657   //              | register
4658   //              | # imm
4659   //              | # + imm
4660   //              | # - imm
4661 
4662   // This method must return MatchOperand_NoMatch without consuming any tokens
4663   // in the case where there is no match, as other alternatives take other
4664   // parse methods.
4665   MCAsmParser &Parser = getParser();
4666   AsmToken Tok = Parser.getTok();
4667   SMLoc S = Tok.getLoc();
4668 
4669   // Do immediates first, as we always parse those if we have a '#'.
4670   if (Parser.getTok().is(AsmToken::Hash) ||
4671       Parser.getTok().is(AsmToken::Dollar)) {
4672     Parser.Lex(); // Eat '#' or '$'.
4673     // Explicitly look for a '-', as we need to encode negative zero
4674     // differently.
4675     bool isNegative = Parser.getTok().is(AsmToken::Minus);
4676     const MCExpr *Offset;
4677     SMLoc E;
4678     if (getParser().parseExpression(Offset, E))
4679       return MatchOperand_ParseFail;
4680     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
4681     if (!CE) {
4682       Error(S, "constant expression expected");
4683       return MatchOperand_ParseFail;
4684     }
4685     // Negative zero is encoded as the flag value INT32_MIN.
4686     int32_t Val = CE->getValue();
4687     if (isNegative && Val == 0)
4688       Val = INT32_MIN;
4689 
4690     Operands.push_back(
4691       ARMOperand::CreateImm(MCConstantExpr::create(Val, getContext()), S, E));
4692 
4693     return MatchOperand_Success;
4694   }
4695 
4696 
4697   bool haveEaten = false;
4698   bool isAdd = true;
4699   if (Tok.is(AsmToken::Plus)) {
4700     Parser.Lex(); // Eat the '+' token.
4701     haveEaten = true;
4702   } else if (Tok.is(AsmToken::Minus)) {
4703     Parser.Lex(); // Eat the '-' token.
4704     isAdd = false;
4705     haveEaten = true;
4706   }
4707 
4708   Tok = Parser.getTok();
4709   int Reg = tryParseRegister();
4710   if (Reg == -1) {
4711     if (!haveEaten)
4712       return MatchOperand_NoMatch;
4713     Error(Tok.getLoc(), "register expected");
4714     return MatchOperand_ParseFail;
4715   }
4716 
4717   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
4718                                                   0, S, Tok.getEndLoc()));
4719 
4720   return MatchOperand_Success;
4721 }
4722 
4723 /// Convert parsed operands to MCInst.  Needed here because this instruction
4724 /// only has two register operands, but multiplication is commutative so
4725 /// assemblers should accept both "mul rD, rN, rD" and "mul rD, rD, rN".
4726 void ARMAsmParser::cvtThumbMultiply(MCInst &Inst,
4727                                     const OperandVector &Operands) {
4728   ((ARMOperand &)*Operands[3]).addRegOperands(Inst, 1);
4729   ((ARMOperand &)*Operands[1]).addCCOutOperands(Inst, 1);
4730   // If we have a three-operand form, make sure to set Rn to be the operand
4731   // that isn't the same as Rd.
4732   unsigned RegOp = 4;
4733   if (Operands.size() == 6 &&
4734       ((ARMOperand &)*Operands[4]).getReg() ==
4735           ((ARMOperand &)*Operands[3]).getReg())
4736     RegOp = 5;
4737   ((ARMOperand &)*Operands[RegOp]).addRegOperands(Inst, 1);
4738   Inst.addOperand(Inst.getOperand(0));
4739   ((ARMOperand &)*Operands[2]).addCondCodeOperands(Inst, 2);
4740 }
4741 
4742 void ARMAsmParser::cvtThumbBranches(MCInst &Inst,
4743                                     const OperandVector &Operands) {
4744   int CondOp = -1, ImmOp = -1;
4745   switch(Inst.getOpcode()) {
4746     case ARM::tB:
4747     case ARM::tBcc:  CondOp = 1; ImmOp = 2; break;
4748 
4749     case ARM::t2B:
4750     case ARM::t2Bcc: CondOp = 1; ImmOp = 3; break;
4751 
4752     default: llvm_unreachable("Unexpected instruction in cvtThumbBranches");
4753   }
4754   // first decide whether or not the branch should be conditional
4755   // by looking at it's location relative to an IT block
4756   if(inITBlock()) {
4757     // inside an IT block we cannot have any conditional branches. any
4758     // such instructions needs to be converted to unconditional form
4759     switch(Inst.getOpcode()) {
4760       case ARM::tBcc: Inst.setOpcode(ARM::tB); break;
4761       case ARM::t2Bcc: Inst.setOpcode(ARM::t2B); break;
4762     }
4763   } else {
4764     // outside IT blocks we can only have unconditional branches with AL
4765     // condition code or conditional branches with non-AL condition code
4766     unsigned Cond = static_cast<ARMOperand &>(*Operands[CondOp]).getCondCode();
4767     switch(Inst.getOpcode()) {
4768       case ARM::tB:
4769       case ARM::tBcc:
4770         Inst.setOpcode(Cond == ARMCC::AL ? ARM::tB : ARM::tBcc);
4771         break;
4772       case ARM::t2B:
4773       case ARM::t2Bcc:
4774         Inst.setOpcode(Cond == ARMCC::AL ? ARM::t2B : ARM::t2Bcc);
4775         break;
4776     }
4777   }
4778 
4779   // now decide on encoding size based on branch target range
4780   switch(Inst.getOpcode()) {
4781     // classify tB as either t2B or t1B based on range of immediate operand
4782     case ARM::tB: {
4783       ARMOperand &op = static_cast<ARMOperand &>(*Operands[ImmOp]);
4784       if (!op.isSignedOffset<11, 1>() && isThumb() && hasV8MBaseline())
4785         Inst.setOpcode(ARM::t2B);
4786       break;
4787     }
4788     // classify tBcc as either t2Bcc or t1Bcc based on range of immediate operand
4789     case ARM::tBcc: {
4790       ARMOperand &op = static_cast<ARMOperand &>(*Operands[ImmOp]);
4791       if (!op.isSignedOffset<8, 1>() && isThumb() && hasV8MBaseline())
4792         Inst.setOpcode(ARM::t2Bcc);
4793       break;
4794     }
4795   }
4796   ((ARMOperand &)*Operands[ImmOp]).addImmOperands(Inst, 1);
4797   ((ARMOperand &)*Operands[CondOp]).addCondCodeOperands(Inst, 2);
4798 }
4799 
4800 /// Parse an ARM memory expression, return false if successful else return true
4801 /// or an error.  The first token must be a '[' when called.
4802 bool ARMAsmParser::parseMemory(OperandVector &Operands) {
4803   MCAsmParser &Parser = getParser();
4804   SMLoc S, E;
4805   assert(Parser.getTok().is(AsmToken::LBrac) &&
4806          "Token is not a Left Bracket");
4807   S = Parser.getTok().getLoc();
4808   Parser.Lex(); // Eat left bracket token.
4809 
4810   const AsmToken &BaseRegTok = Parser.getTok();
4811   int BaseRegNum = tryParseRegister();
4812   if (BaseRegNum == -1)
4813     return Error(BaseRegTok.getLoc(), "register expected");
4814 
4815   // The next token must either be a comma, a colon or a closing bracket.
4816   const AsmToken &Tok = Parser.getTok();
4817   if (!Tok.is(AsmToken::Colon) && !Tok.is(AsmToken::Comma) &&
4818       !Tok.is(AsmToken::RBrac))
4819     return Error(Tok.getLoc(), "malformed memory operand");
4820 
4821   if (Tok.is(AsmToken::RBrac)) {
4822     E = Tok.getEndLoc();
4823     Parser.Lex(); // Eat right bracket token.
4824 
4825     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, 0,
4826                                              ARM_AM::no_shift, 0, 0, false,
4827                                              S, E));
4828 
4829     // If there's a pre-indexing writeback marker, '!', just add it as a token
4830     // operand. It's rather odd, but syntactically valid.
4831     if (Parser.getTok().is(AsmToken::Exclaim)) {
4832       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4833       Parser.Lex(); // Eat the '!'.
4834     }
4835 
4836     return false;
4837   }
4838 
4839   assert((Tok.is(AsmToken::Colon) || Tok.is(AsmToken::Comma)) &&
4840          "Lost colon or comma in memory operand?!");
4841   if (Tok.is(AsmToken::Comma)) {
4842     Parser.Lex(); // Eat the comma.
4843   }
4844 
4845   // If we have a ':', it's an alignment specifier.
4846   if (Parser.getTok().is(AsmToken::Colon)) {
4847     Parser.Lex(); // Eat the ':'.
4848     E = Parser.getTok().getLoc();
4849     SMLoc AlignmentLoc = Tok.getLoc();
4850 
4851     const MCExpr *Expr;
4852     if (getParser().parseExpression(Expr))
4853      return true;
4854 
4855     // The expression has to be a constant. Memory references with relocations
4856     // don't come through here, as they use the <label> forms of the relevant
4857     // instructions.
4858     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
4859     if (!CE)
4860       return Error (E, "constant expression expected");
4861 
4862     unsigned Align = 0;
4863     switch (CE->getValue()) {
4864     default:
4865       return Error(E,
4866                    "alignment specifier must be 16, 32, 64, 128, or 256 bits");
4867     case 16:  Align = 2; break;
4868     case 32:  Align = 4; break;
4869     case 64:  Align = 8; break;
4870     case 128: Align = 16; break;
4871     case 256: Align = 32; break;
4872     }
4873 
4874     // Now we should have the closing ']'
4875     if (Parser.getTok().isNot(AsmToken::RBrac))
4876       return Error(Parser.getTok().getLoc(), "']' expected");
4877     E = Parser.getTok().getEndLoc();
4878     Parser.Lex(); // Eat right bracket token.
4879 
4880     // Don't worry about range checking the value here. That's handled by
4881     // the is*() predicates.
4882     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, 0,
4883                                              ARM_AM::no_shift, 0, Align,
4884                                              false, S, E, AlignmentLoc));
4885 
4886     // If there's a pre-indexing writeback marker, '!', just add it as a token
4887     // operand.
4888     if (Parser.getTok().is(AsmToken::Exclaim)) {
4889       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4890       Parser.Lex(); // Eat the '!'.
4891     }
4892 
4893     return false;
4894   }
4895 
4896   // If we have a '#', it's an immediate offset, else assume it's a register
4897   // offset. Be friendly and also accept a plain integer (without a leading
4898   // hash) for gas compatibility.
4899   if (Parser.getTok().is(AsmToken::Hash) ||
4900       Parser.getTok().is(AsmToken::Dollar) ||
4901       Parser.getTok().is(AsmToken::Integer)) {
4902     if (Parser.getTok().isNot(AsmToken::Integer))
4903       Parser.Lex(); // Eat '#' or '$'.
4904     E = Parser.getTok().getLoc();
4905 
4906     bool isNegative = getParser().getTok().is(AsmToken::Minus);
4907     const MCExpr *Offset;
4908     if (getParser().parseExpression(Offset))
4909      return true;
4910 
4911     // The expression has to be a constant. Memory references with relocations
4912     // don't come through here, as they use the <label> forms of the relevant
4913     // instructions.
4914     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
4915     if (!CE)
4916       return Error (E, "constant expression expected");
4917 
4918     // If the constant was #-0, represent it as INT32_MIN.
4919     int32_t Val = CE->getValue();
4920     if (isNegative && Val == 0)
4921       CE = MCConstantExpr::create(INT32_MIN, getContext());
4922 
4923     // Now we should have the closing ']'
4924     if (Parser.getTok().isNot(AsmToken::RBrac))
4925       return Error(Parser.getTok().getLoc(), "']' expected");
4926     E = Parser.getTok().getEndLoc();
4927     Parser.Lex(); // Eat right bracket token.
4928 
4929     // Don't worry about range checking the value here. That's handled by
4930     // the is*() predicates.
4931     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
4932                                              ARM_AM::no_shift, 0, 0,
4933                                              false, S, E));
4934 
4935     // If there's a pre-indexing writeback marker, '!', just add it as a token
4936     // operand.
4937     if (Parser.getTok().is(AsmToken::Exclaim)) {
4938       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4939       Parser.Lex(); // Eat the '!'.
4940     }
4941 
4942     return false;
4943   }
4944 
4945   // The register offset is optionally preceded by a '+' or '-'
4946   bool isNegative = false;
4947   if (Parser.getTok().is(AsmToken::Minus)) {
4948     isNegative = true;
4949     Parser.Lex(); // Eat the '-'.
4950   } else if (Parser.getTok().is(AsmToken::Plus)) {
4951     // Nothing to do.
4952     Parser.Lex(); // Eat the '+'.
4953   }
4954 
4955   E = Parser.getTok().getLoc();
4956   int OffsetRegNum = tryParseRegister();
4957   if (OffsetRegNum == -1)
4958     return Error(E, "register expected");
4959 
4960   // If there's a shift operator, handle it.
4961   ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
4962   unsigned ShiftImm = 0;
4963   if (Parser.getTok().is(AsmToken::Comma)) {
4964     Parser.Lex(); // Eat the ','.
4965     if (parseMemRegOffsetShift(ShiftType, ShiftImm))
4966       return true;
4967   }
4968 
4969   // Now we should have the closing ']'
4970   if (Parser.getTok().isNot(AsmToken::RBrac))
4971     return Error(Parser.getTok().getLoc(), "']' expected");
4972   E = Parser.getTok().getEndLoc();
4973   Parser.Lex(); // Eat right bracket token.
4974 
4975   Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, OffsetRegNum,
4976                                            ShiftType, ShiftImm, 0, isNegative,
4977                                            S, E));
4978 
4979   // If there's a pre-indexing writeback marker, '!', just add it as a token
4980   // operand.
4981   if (Parser.getTok().is(AsmToken::Exclaim)) {
4982     Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4983     Parser.Lex(); // Eat the '!'.
4984   }
4985 
4986   return false;
4987 }
4988 
4989 /// parseMemRegOffsetShift - one of these two:
4990 ///   ( lsl | lsr | asr | ror ) , # shift_amount
4991 ///   rrx
4992 /// return true if it parses a shift otherwise it returns false.
4993 bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
4994                                           unsigned &Amount) {
4995   MCAsmParser &Parser = getParser();
4996   SMLoc Loc = Parser.getTok().getLoc();
4997   const AsmToken &Tok = Parser.getTok();
4998   if (Tok.isNot(AsmToken::Identifier))
4999     return true;
5000   StringRef ShiftName = Tok.getString();
5001   if (ShiftName == "lsl" || ShiftName == "LSL" ||
5002       ShiftName == "asl" || ShiftName == "ASL")
5003     St = ARM_AM::lsl;
5004   else if (ShiftName == "lsr" || ShiftName == "LSR")
5005     St = ARM_AM::lsr;
5006   else if (ShiftName == "asr" || ShiftName == "ASR")
5007     St = ARM_AM::asr;
5008   else if (ShiftName == "ror" || ShiftName == "ROR")
5009     St = ARM_AM::ror;
5010   else if (ShiftName == "rrx" || ShiftName == "RRX")
5011     St = ARM_AM::rrx;
5012   else
5013     return Error(Loc, "illegal shift operator");
5014   Parser.Lex(); // Eat shift type token.
5015 
5016   // rrx stands alone.
5017   Amount = 0;
5018   if (St != ARM_AM::rrx) {
5019     Loc = Parser.getTok().getLoc();
5020     // A '#' and a shift amount.
5021     const AsmToken &HashTok = Parser.getTok();
5022     if (HashTok.isNot(AsmToken::Hash) &&
5023         HashTok.isNot(AsmToken::Dollar))
5024       return Error(HashTok.getLoc(), "'#' expected");
5025     Parser.Lex(); // Eat hash token.
5026 
5027     const MCExpr *Expr;
5028     if (getParser().parseExpression(Expr))
5029       return true;
5030     // Range check the immediate.
5031     // lsl, ror: 0 <= imm <= 31
5032     // lsr, asr: 0 <= imm <= 32
5033     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
5034     if (!CE)
5035       return Error(Loc, "shift amount must be an immediate");
5036     int64_t Imm = CE->getValue();
5037     if (Imm < 0 ||
5038         ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
5039         ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
5040       return Error(Loc, "immediate shift value out of range");
5041     // If <ShiftTy> #0, turn it into a no_shift.
5042     if (Imm == 0)
5043       St = ARM_AM::lsl;
5044     // For consistency, treat lsr #32 and asr #32 as having immediate value 0.
5045     if (Imm == 32)
5046       Imm = 0;
5047     Amount = Imm;
5048   }
5049 
5050   return false;
5051 }
5052 
5053 /// parseFPImm - A floating point immediate expression operand.
5054 ARMAsmParser::OperandMatchResultTy
5055 ARMAsmParser::parseFPImm(OperandVector &Operands) {
5056   MCAsmParser &Parser = getParser();
5057   // Anything that can accept a floating point constant as an operand
5058   // needs to go through here, as the regular parseExpression is
5059   // integer only.
5060   //
5061   // This routine still creates a generic Immediate operand, containing
5062   // a bitcast of the 64-bit floating point value. The various operands
5063   // that accept floats can check whether the value is valid for them
5064   // via the standard is*() predicates.
5065 
5066   SMLoc S = Parser.getTok().getLoc();
5067 
5068   if (Parser.getTok().isNot(AsmToken::Hash) &&
5069       Parser.getTok().isNot(AsmToken::Dollar))
5070     return MatchOperand_NoMatch;
5071 
5072   // Disambiguate the VMOV forms that can accept an FP immediate.
5073   // vmov.f32 <sreg>, #imm
5074   // vmov.f64 <dreg>, #imm
5075   // vmov.f32 <dreg>, #imm  @ vector f32x2
5076   // vmov.f32 <qreg>, #imm  @ vector f32x4
5077   //
5078   // There are also the NEON VMOV instructions which expect an
5079   // integer constant. Make sure we don't try to parse an FPImm
5080   // for these:
5081   // vmov.i{8|16|32|64} <dreg|qreg>, #imm
5082   ARMOperand &TyOp = static_cast<ARMOperand &>(*Operands[2]);
5083   bool isVmovf = TyOp.isToken() &&
5084                  (TyOp.getToken() == ".f32" || TyOp.getToken() == ".f64" ||
5085                   TyOp.getToken() == ".f16");
5086   ARMOperand &Mnemonic = static_cast<ARMOperand &>(*Operands[0]);
5087   bool isFconst = Mnemonic.isToken() && (Mnemonic.getToken() == "fconstd" ||
5088                                          Mnemonic.getToken() == "fconsts");
5089   if (!(isVmovf || isFconst))
5090     return MatchOperand_NoMatch;
5091 
5092   Parser.Lex(); // Eat '#' or '$'.
5093 
5094   // Handle negation, as that still comes through as a separate token.
5095   bool isNegative = false;
5096   if (Parser.getTok().is(AsmToken::Minus)) {
5097     isNegative = true;
5098     Parser.Lex();
5099   }
5100   const AsmToken &Tok = Parser.getTok();
5101   SMLoc Loc = Tok.getLoc();
5102   if (Tok.is(AsmToken::Real) && isVmovf) {
5103     APFloat RealVal(APFloat::IEEEsingle, Tok.getString());
5104     uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
5105     // If we had a '-' in front, toggle the sign bit.
5106     IntVal ^= (uint64_t)isNegative << 31;
5107     Parser.Lex(); // Eat the token.
5108     Operands.push_back(ARMOperand::CreateImm(
5109           MCConstantExpr::create(IntVal, getContext()),
5110           S, Parser.getTok().getLoc()));
5111     return MatchOperand_Success;
5112   }
5113   // Also handle plain integers. Instructions which allow floating point
5114   // immediates also allow a raw encoded 8-bit value.
5115   if (Tok.is(AsmToken::Integer) && isFconst) {
5116     int64_t Val = Tok.getIntVal();
5117     Parser.Lex(); // Eat the token.
5118     if (Val > 255 || Val < 0) {
5119       Error(Loc, "encoded floating point value out of range");
5120       return MatchOperand_ParseFail;
5121     }
5122     float RealVal = ARM_AM::getFPImmFloat(Val);
5123     Val = APFloat(RealVal).bitcastToAPInt().getZExtValue();
5124 
5125     Operands.push_back(ARMOperand::CreateImm(
5126         MCConstantExpr::create(Val, getContext()), S,
5127         Parser.getTok().getLoc()));
5128     return MatchOperand_Success;
5129   }
5130 
5131   Error(Loc, "invalid floating point immediate");
5132   return MatchOperand_ParseFail;
5133 }
5134 
5135 /// Parse a arm instruction operand.  For now this parses the operand regardless
5136 /// of the mnemonic.
5137 bool ARMAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
5138   MCAsmParser &Parser = getParser();
5139   SMLoc S, E;
5140 
5141   // Check if the current operand has a custom associated parser, if so, try to
5142   // custom parse the operand, or fallback to the general approach.
5143   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
5144   if (ResTy == MatchOperand_Success)
5145     return false;
5146   // If there wasn't a custom match, try the generic matcher below. Otherwise,
5147   // there was a match, but an error occurred, in which case, just return that
5148   // the operand parsing failed.
5149   if (ResTy == MatchOperand_ParseFail)
5150     return true;
5151 
5152   switch (getLexer().getKind()) {
5153   default:
5154     Error(Parser.getTok().getLoc(), "unexpected token in operand");
5155     return true;
5156   case AsmToken::Identifier: {
5157     // If we've seen a branch mnemonic, the next operand must be a label.  This
5158     // is true even if the label is a register name.  So "br r1" means branch to
5159     // label "r1".
5160     bool ExpectLabel = Mnemonic == "b" || Mnemonic == "bl";
5161     if (!ExpectLabel) {
5162       if (!tryParseRegisterWithWriteBack(Operands))
5163         return false;
5164       int Res = tryParseShiftRegister(Operands);
5165       if (Res == 0) // success
5166         return false;
5167       else if (Res == -1) // irrecoverable error
5168         return true;
5169       // If this is VMRS, check for the apsr_nzcv operand.
5170       if (Mnemonic == "vmrs" &&
5171           Parser.getTok().getString().equals_lower("apsr_nzcv")) {
5172         S = Parser.getTok().getLoc();
5173         Parser.Lex();
5174         Operands.push_back(ARMOperand::CreateToken("APSR_nzcv", S));
5175         return false;
5176       }
5177     }
5178 
5179     // Fall though for the Identifier case that is not a register or a
5180     // special name.
5181   }
5182   case AsmToken::LParen:  // parenthesized expressions like (_strcmp-4)
5183   case AsmToken::Integer: // things like 1f and 2b as a branch targets
5184   case AsmToken::String:  // quoted label names.
5185   case AsmToken::Dot: {   // . as a branch target
5186     // This was not a register so parse other operands that start with an
5187     // identifier (like labels) as expressions and create them as immediates.
5188     const MCExpr *IdVal;
5189     S = Parser.getTok().getLoc();
5190     if (getParser().parseExpression(IdVal))
5191       return true;
5192     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5193     Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
5194     return false;
5195   }
5196   case AsmToken::LBrac:
5197     return parseMemory(Operands);
5198   case AsmToken::LCurly:
5199     return parseRegisterList(Operands);
5200   case AsmToken::Dollar:
5201   case AsmToken::Hash: {
5202     // #42 -> immediate.
5203     S = Parser.getTok().getLoc();
5204     Parser.Lex();
5205 
5206     if (Parser.getTok().isNot(AsmToken::Colon)) {
5207       bool isNegative = Parser.getTok().is(AsmToken::Minus);
5208       const MCExpr *ImmVal;
5209       if (getParser().parseExpression(ImmVal))
5210         return true;
5211       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
5212       if (CE) {
5213         int32_t Val = CE->getValue();
5214         if (isNegative && Val == 0)
5215           ImmVal = MCConstantExpr::create(INT32_MIN, getContext());
5216       }
5217       E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5218       Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
5219 
5220       // There can be a trailing '!' on operands that we want as a separate
5221       // '!' Token operand. Handle that here. For example, the compatibility
5222       // alias for 'srsdb sp!, #imm' is 'srsdb #imm!'.
5223       if (Parser.getTok().is(AsmToken::Exclaim)) {
5224         Operands.push_back(ARMOperand::CreateToken(Parser.getTok().getString(),
5225                                                    Parser.getTok().getLoc()));
5226         Parser.Lex(); // Eat exclaim token
5227       }
5228       return false;
5229     }
5230     // w/ a ':' after the '#', it's just like a plain ':'.
5231     // FALLTHROUGH
5232   }
5233   case AsmToken::Colon: {
5234     S = Parser.getTok().getLoc();
5235     // ":lower16:" and ":upper16:" expression prefixes
5236     // FIXME: Check it's an expression prefix,
5237     // e.g. (FOO - :lower16:BAR) isn't legal.
5238     ARMMCExpr::VariantKind RefKind;
5239     if (parsePrefix(RefKind))
5240       return true;
5241 
5242     const MCExpr *SubExprVal;
5243     if (getParser().parseExpression(SubExprVal))
5244       return true;
5245 
5246     const MCExpr *ExprVal = ARMMCExpr::create(RefKind, SubExprVal,
5247                                               getContext());
5248     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5249     Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
5250     return false;
5251   }
5252   case AsmToken::Equal: {
5253     S = Parser.getTok().getLoc();
5254     if (Mnemonic != "ldr") // only parse for ldr pseudo (e.g. ldr r0, =val)
5255       return Error(S, "unexpected token in operand");
5256     Parser.Lex(); // Eat '='
5257     const MCExpr *SubExprVal;
5258     if (getParser().parseExpression(SubExprVal))
5259       return true;
5260     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5261     Operands.push_back(ARMOperand::CreateConstantPoolImm(SubExprVal, S, E));
5262     return false;
5263   }
5264   }
5265 }
5266 
5267 // parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
5268 //  :lower16: and :upper16:.
5269 bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
5270   MCAsmParser &Parser = getParser();
5271   RefKind = ARMMCExpr::VK_ARM_None;
5272 
5273   // consume an optional '#' (GNU compatibility)
5274   if (getLexer().is(AsmToken::Hash))
5275     Parser.Lex();
5276 
5277   // :lower16: and :upper16: modifiers
5278   assert(getLexer().is(AsmToken::Colon) && "expected a :");
5279   Parser.Lex(); // Eat ':'
5280 
5281   if (getLexer().isNot(AsmToken::Identifier)) {
5282     Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
5283     return true;
5284   }
5285 
5286   enum {
5287     COFF = (1 << MCObjectFileInfo::IsCOFF),
5288     ELF = (1 << MCObjectFileInfo::IsELF),
5289     MACHO = (1 << MCObjectFileInfo::IsMachO)
5290   };
5291   static const struct PrefixEntry {
5292     const char *Spelling;
5293     ARMMCExpr::VariantKind VariantKind;
5294     uint8_t SupportedFormats;
5295   } PrefixEntries[] = {
5296     { "lower16", ARMMCExpr::VK_ARM_LO16, COFF | ELF | MACHO },
5297     { "upper16", ARMMCExpr::VK_ARM_HI16, COFF | ELF | MACHO },
5298   };
5299 
5300   StringRef IDVal = Parser.getTok().getIdentifier();
5301 
5302   const auto &Prefix =
5303       std::find_if(std::begin(PrefixEntries), std::end(PrefixEntries),
5304                    [&IDVal](const PrefixEntry &PE) {
5305                       return PE.Spelling == IDVal;
5306                    });
5307   if (Prefix == std::end(PrefixEntries)) {
5308     Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
5309     return true;
5310   }
5311 
5312   uint8_t CurrentFormat;
5313   switch (getContext().getObjectFileInfo()->getObjectFileType()) {
5314   case MCObjectFileInfo::IsMachO:
5315     CurrentFormat = MACHO;
5316     break;
5317   case MCObjectFileInfo::IsELF:
5318     CurrentFormat = ELF;
5319     break;
5320   case MCObjectFileInfo::IsCOFF:
5321     CurrentFormat = COFF;
5322     break;
5323   }
5324 
5325   if (~Prefix->SupportedFormats & CurrentFormat) {
5326     Error(Parser.getTok().getLoc(),
5327           "cannot represent relocation in the current file format");
5328     return true;
5329   }
5330 
5331   RefKind = Prefix->VariantKind;
5332   Parser.Lex();
5333 
5334   if (getLexer().isNot(AsmToken::Colon)) {
5335     Error(Parser.getTok().getLoc(), "unexpected token after prefix");
5336     return true;
5337   }
5338   Parser.Lex(); // Eat the last ':'
5339 
5340   return false;
5341 }
5342 
5343 /// \brief Given a mnemonic, split out possible predication code and carry
5344 /// setting letters to form a canonical mnemonic and flags.
5345 //
5346 // FIXME: Would be nice to autogen this.
5347 // FIXME: This is a bit of a maze of special cases.
5348 StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
5349                                       unsigned &PredicationCode,
5350                                       bool &CarrySetting,
5351                                       unsigned &ProcessorIMod,
5352                                       StringRef &ITMask) {
5353   PredicationCode = ARMCC::AL;
5354   CarrySetting = false;
5355   ProcessorIMod = 0;
5356 
5357   // Ignore some mnemonics we know aren't predicated forms.
5358   //
5359   // FIXME: Would be nice to autogen this.
5360   if ((Mnemonic == "movs" && isThumb()) ||
5361       Mnemonic == "teq"   || Mnemonic == "vceq"   || Mnemonic == "svc"   ||
5362       Mnemonic == "mls"   || Mnemonic == "smmls"  || Mnemonic == "vcls"  ||
5363       Mnemonic == "vmls"  || Mnemonic == "vnmls"  || Mnemonic == "vacge" ||
5364       Mnemonic == "vcge"  || Mnemonic == "vclt"   || Mnemonic == "vacgt" ||
5365       Mnemonic == "vaclt" || Mnemonic == "vacle"  || Mnemonic == "hlt" ||
5366       Mnemonic == "vcgt"  || Mnemonic == "vcle"   || Mnemonic == "smlal" ||
5367       Mnemonic == "umaal" || Mnemonic == "umlal"  || Mnemonic == "vabal" ||
5368       Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal" ||
5369       Mnemonic == "fmuls" || Mnemonic == "vmaxnm" || Mnemonic == "vminnm" ||
5370       Mnemonic == "vcvta" || Mnemonic == "vcvtn"  || Mnemonic == "vcvtp" ||
5371       Mnemonic == "vcvtm" || Mnemonic == "vrinta" || Mnemonic == "vrintn" ||
5372       Mnemonic == "vrintp" || Mnemonic == "vrintm" || Mnemonic == "hvc" ||
5373       Mnemonic.startswith("vsel") || Mnemonic == "vins" || Mnemonic == "vmovx" ||
5374       Mnemonic == "bxns"  || Mnemonic == "blxns")
5375     return Mnemonic;
5376 
5377   // First, split out any predication code. Ignore mnemonics we know aren't
5378   // predicated but do have a carry-set and so weren't caught above.
5379   if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
5380       Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
5381       Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
5382       Mnemonic != "sbcs" && Mnemonic != "rscs") {
5383     unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
5384       .Case("eq", ARMCC::EQ)
5385       .Case("ne", ARMCC::NE)
5386       .Case("hs", ARMCC::HS)
5387       .Case("cs", ARMCC::HS)
5388       .Case("lo", ARMCC::LO)
5389       .Case("cc", ARMCC::LO)
5390       .Case("mi", ARMCC::MI)
5391       .Case("pl", ARMCC::PL)
5392       .Case("vs", ARMCC::VS)
5393       .Case("vc", ARMCC::VC)
5394       .Case("hi", ARMCC::HI)
5395       .Case("ls", ARMCC::LS)
5396       .Case("ge", ARMCC::GE)
5397       .Case("lt", ARMCC::LT)
5398       .Case("gt", ARMCC::GT)
5399       .Case("le", ARMCC::LE)
5400       .Case("al", ARMCC::AL)
5401       .Default(~0U);
5402     if (CC != ~0U) {
5403       Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
5404       PredicationCode = CC;
5405     }
5406   }
5407 
5408   // Next, determine if we have a carry setting bit. We explicitly ignore all
5409   // the instructions we know end in 's'.
5410   if (Mnemonic.endswith("s") &&
5411       !(Mnemonic == "cps" || Mnemonic == "mls" ||
5412         Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
5413         Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
5414         Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
5415         Mnemonic == "vrsqrts" || Mnemonic == "srs" || Mnemonic == "flds" ||
5416         Mnemonic == "fmrs" || Mnemonic == "fsqrts" || Mnemonic == "fsubs" ||
5417         Mnemonic == "fsts" || Mnemonic == "fcpys" || Mnemonic == "fdivs" ||
5418         Mnemonic == "fmuls" || Mnemonic == "fcmps" || Mnemonic == "fcmpzs" ||
5419         Mnemonic == "vfms" || Mnemonic == "vfnms" || Mnemonic == "fconsts" ||
5420         (Mnemonic == "movs" && isThumb()))) {
5421     Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
5422     CarrySetting = true;
5423   }
5424 
5425   // The "cps" instruction can have a interrupt mode operand which is glued into
5426   // the mnemonic. Check if this is the case, split it and parse the imod op
5427   if (Mnemonic.startswith("cps")) {
5428     // Split out any imod code.
5429     unsigned IMod =
5430       StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
5431       .Case("ie", ARM_PROC::IE)
5432       .Case("id", ARM_PROC::ID)
5433       .Default(~0U);
5434     if (IMod != ~0U) {
5435       Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
5436       ProcessorIMod = IMod;
5437     }
5438   }
5439 
5440   // The "it" instruction has the condition mask on the end of the mnemonic.
5441   if (Mnemonic.startswith("it")) {
5442     ITMask = Mnemonic.slice(2, Mnemonic.size());
5443     Mnemonic = Mnemonic.slice(0, 2);
5444   }
5445 
5446   return Mnemonic;
5447 }
5448 
5449 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
5450 /// inclusion of carry set or predication code operands.
5451 //
5452 // FIXME: It would be nice to autogen this.
5453 void ARMAsmParser::getMnemonicAcceptInfo(StringRef Mnemonic, StringRef FullInst,
5454                                          bool &CanAcceptCarrySet,
5455                                          bool &CanAcceptPredicationCode) {
5456   CanAcceptCarrySet =
5457       Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
5458       Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
5459       Mnemonic == "add" || Mnemonic == "adc" || Mnemonic == "mul" ||
5460       Mnemonic == "bic" || Mnemonic == "asr" || Mnemonic == "orr" ||
5461       Mnemonic == "mvn" || Mnemonic == "rsb" || Mnemonic == "rsc" ||
5462       Mnemonic == "orn" || Mnemonic == "sbc" || Mnemonic == "eor" ||
5463       Mnemonic == "neg" || Mnemonic == "vfm" || Mnemonic == "vfnm" ||
5464       (!isThumb() &&
5465        (Mnemonic == "smull" || Mnemonic == "mov" || Mnemonic == "mla" ||
5466         Mnemonic == "smlal" || Mnemonic == "umlal" || Mnemonic == "umull"));
5467 
5468   if (Mnemonic == "bkpt" || Mnemonic == "cbnz" || Mnemonic == "setend" ||
5469       Mnemonic == "cps" || Mnemonic == "it" || Mnemonic == "cbz" ||
5470       Mnemonic == "trap" || Mnemonic == "hlt" || Mnemonic == "udf" ||
5471       Mnemonic.startswith("crc32") || Mnemonic.startswith("cps") ||
5472       Mnemonic.startswith("vsel") || Mnemonic == "vmaxnm" ||
5473       Mnemonic == "vminnm" || Mnemonic == "vcvta" || Mnemonic == "vcvtn" ||
5474       Mnemonic == "vcvtp" || Mnemonic == "vcvtm" || Mnemonic == "vrinta" ||
5475       Mnemonic == "vrintn" || Mnemonic == "vrintp" || Mnemonic == "vrintm" ||
5476       Mnemonic.startswith("aes") || Mnemonic == "hvc" || Mnemonic == "setpan" ||
5477       Mnemonic.startswith("sha1") || Mnemonic.startswith("sha256") ||
5478       (FullInst.startswith("vmull") && FullInst.endswith(".p64")) ||
5479       Mnemonic == "vmovx" || Mnemonic == "vins") {
5480     // These mnemonics are never predicable
5481     CanAcceptPredicationCode = false;
5482   } else if (!isThumb()) {
5483     // Some instructions are only predicable in Thumb mode
5484     CanAcceptPredicationCode =
5485         Mnemonic != "cdp2" && Mnemonic != "clrex" && Mnemonic != "mcr2" &&
5486         Mnemonic != "mcrr2" && Mnemonic != "mrc2" && Mnemonic != "mrrc2" &&
5487         Mnemonic != "dmb" && Mnemonic != "dsb" && Mnemonic != "isb" &&
5488         Mnemonic != "pld" && Mnemonic != "pli" && Mnemonic != "pldw" &&
5489         Mnemonic != "ldc2" && Mnemonic != "ldc2l" && Mnemonic != "stc2" &&
5490         Mnemonic != "stc2l" && !Mnemonic.startswith("rfe") &&
5491         !Mnemonic.startswith("srs");
5492   } else if (isThumbOne()) {
5493     if (hasV6MOps())
5494       CanAcceptPredicationCode = Mnemonic != "movs";
5495     else
5496       CanAcceptPredicationCode = Mnemonic != "nop" && Mnemonic != "movs";
5497   } else
5498     CanAcceptPredicationCode = true;
5499 }
5500 
5501 // \brief Some Thumb instructions have two operand forms that are not
5502 // available as three operand, convert to two operand form if possible.
5503 //
5504 // FIXME: We would really like to be able to tablegen'erate this.
5505 void ARMAsmParser::tryConvertingToTwoOperandForm(StringRef Mnemonic,
5506                                                  bool CarrySetting,
5507                                                  OperandVector &Operands) {
5508   if (Operands.size() != 6)
5509     return;
5510 
5511   const auto &Op3 = static_cast<ARMOperand &>(*Operands[3]);
5512         auto &Op4 = static_cast<ARMOperand &>(*Operands[4]);
5513   if (!Op3.isReg() || !Op4.isReg())
5514     return;
5515 
5516   auto Op3Reg = Op3.getReg();
5517   auto Op4Reg = Op4.getReg();
5518 
5519   // For most Thumb2 cases we just generate the 3 operand form and reduce
5520   // it in processInstruction(), but the 3 operand form of ADD (t2ADDrr)
5521   // won't accept SP or PC so we do the transformation here taking care
5522   // with immediate range in the 'add sp, sp #imm' case.
5523   auto &Op5 = static_cast<ARMOperand &>(*Operands[5]);
5524   if (isThumbTwo()) {
5525     if (Mnemonic != "add")
5526       return;
5527     bool TryTransform = Op3Reg == ARM::PC || Op4Reg == ARM::PC ||
5528                         (Op5.isReg() && Op5.getReg() == ARM::PC);
5529     if (!TryTransform) {
5530       TryTransform = (Op3Reg == ARM::SP || Op4Reg == ARM::SP ||
5531                       (Op5.isReg() && Op5.getReg() == ARM::SP)) &&
5532                      !(Op3Reg == ARM::SP && Op4Reg == ARM::SP &&
5533                        Op5.isImm() && !Op5.isImm0_508s4());
5534     }
5535     if (!TryTransform)
5536       return;
5537   } else if (!isThumbOne())
5538     return;
5539 
5540   if (!(Mnemonic == "add" || Mnemonic == "sub" || Mnemonic == "and" ||
5541         Mnemonic == "eor" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
5542         Mnemonic == "asr" || Mnemonic == "adc" || Mnemonic == "sbc" ||
5543         Mnemonic == "ror" || Mnemonic == "orr" || Mnemonic == "bic"))
5544     return;
5545 
5546   // If first 2 operands of a 3 operand instruction are the same
5547   // then transform to 2 operand version of the same instruction
5548   // e.g. 'adds r0, r0, #1' transforms to 'adds r0, #1'
5549   bool Transform = Op3Reg == Op4Reg;
5550 
5551   // For communtative operations, we might be able to transform if we swap
5552   // Op4 and Op5.  The 'ADD Rdm, SP, Rdm' form is already handled specially
5553   // as tADDrsp.
5554   const ARMOperand *LastOp = &Op5;
5555   bool Swap = false;
5556   if (!Transform && Op5.isReg() && Op3Reg == Op5.getReg() &&
5557       ((Mnemonic == "add" && Op4Reg != ARM::SP) ||
5558        Mnemonic == "and" || Mnemonic == "eor" ||
5559        Mnemonic == "adc" || Mnemonic == "orr")) {
5560     Swap = true;
5561     LastOp = &Op4;
5562     Transform = true;
5563   }
5564 
5565   // If both registers are the same then remove one of them from
5566   // the operand list, with certain exceptions.
5567   if (Transform) {
5568     // Don't transform 'adds Rd, Rd, Rm' or 'sub{s} Rd, Rd, Rm' because the
5569     // 2 operand forms don't exist.
5570     if (((Mnemonic == "add" && CarrySetting) || Mnemonic == "sub") &&
5571         LastOp->isReg())
5572       Transform = false;
5573 
5574     // Don't transform 'add/sub{s} Rd, Rd, #imm' if the immediate fits into
5575     // 3-bits because the ARMARM says not to.
5576     if ((Mnemonic == "add" || Mnemonic == "sub") && LastOp->isImm0_7())
5577       Transform = false;
5578   }
5579 
5580   if (Transform) {
5581     if (Swap)
5582       std::swap(Op4, Op5);
5583     Operands.erase(Operands.begin() + 3);
5584   }
5585 }
5586 
5587 bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
5588                                           OperandVector &Operands) {
5589   // FIXME: This is all horribly hacky. We really need a better way to deal
5590   // with optional operands like this in the matcher table.
5591 
5592   // The 'mov' mnemonic is special. One variant has a cc_out operand, while
5593   // another does not. Specifically, the MOVW instruction does not. So we
5594   // special case it here and remove the defaulted (non-setting) cc_out
5595   // operand if that's the instruction we're trying to match.
5596   //
5597   // We do this as post-processing of the explicit operands rather than just
5598   // conditionally adding the cc_out in the first place because we need
5599   // to check the type of the parsed immediate operand.
5600   if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
5601       !static_cast<ARMOperand &>(*Operands[4]).isModImm() &&
5602       static_cast<ARMOperand &>(*Operands[4]).isImm0_65535Expr() &&
5603       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0)
5604     return true;
5605 
5606   // Register-register 'add' for thumb does not have a cc_out operand
5607   // when there are only two register operands.
5608   if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
5609       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5610       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5611       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0)
5612     return true;
5613   // Register-register 'add' for thumb does not have a cc_out operand
5614   // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
5615   // have to check the immediate range here since Thumb2 has a variant
5616   // that can handle a different range and has a cc_out operand.
5617   if (((isThumb() && Mnemonic == "add") ||
5618        (isThumbTwo() && Mnemonic == "sub")) &&
5619       Operands.size() == 6 && static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5620       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5621       static_cast<ARMOperand &>(*Operands[4]).getReg() == ARM::SP &&
5622       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5623       ((Mnemonic == "add" && static_cast<ARMOperand &>(*Operands[5]).isReg()) ||
5624        static_cast<ARMOperand &>(*Operands[5]).isImm0_1020s4()))
5625     return true;
5626   // For Thumb2, add/sub immediate does not have a cc_out operand for the
5627   // imm0_4095 variant. That's the least-preferred variant when
5628   // selecting via the generic "add" mnemonic, so to know that we
5629   // should remove the cc_out operand, we have to explicitly check that
5630   // it's not one of the other variants. Ugh.
5631   if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
5632       Operands.size() == 6 && static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5633       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5634       static_cast<ARMOperand &>(*Operands[5]).isImm()) {
5635     // Nest conditions rather than one big 'if' statement for readability.
5636     //
5637     // If both registers are low, we're in an IT block, and the immediate is
5638     // in range, we should use encoding T1 instead, which has a cc_out.
5639     if (inITBlock() &&
5640         isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) &&
5641         isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) &&
5642         static_cast<ARMOperand &>(*Operands[5]).isImm0_7())
5643       return false;
5644     // Check against T3. If the second register is the PC, this is an
5645     // alternate form of ADR, which uses encoding T4, so check for that too.
5646     if (static_cast<ARMOperand &>(*Operands[4]).getReg() != ARM::PC &&
5647         static_cast<ARMOperand &>(*Operands[5]).isT2SOImm())
5648       return false;
5649 
5650     // Otherwise, we use encoding T4, which does not have a cc_out
5651     // operand.
5652     return true;
5653   }
5654 
5655   // The thumb2 multiply instruction doesn't have a CCOut register, so
5656   // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
5657   // use the 16-bit encoding or not.
5658   if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
5659       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5660       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5661       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5662       static_cast<ARMOperand &>(*Operands[5]).isReg() &&
5663       // If the registers aren't low regs, the destination reg isn't the
5664       // same as one of the source regs, or the cc_out operand is zero
5665       // outside of an IT block, we have to use the 32-bit encoding, so
5666       // remove the cc_out operand.
5667       (!isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) ||
5668        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) ||
5669        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[5]).getReg()) ||
5670        !inITBlock() || (static_cast<ARMOperand &>(*Operands[3]).getReg() !=
5671                             static_cast<ARMOperand &>(*Operands[5]).getReg() &&
5672                         static_cast<ARMOperand &>(*Operands[3]).getReg() !=
5673                             static_cast<ARMOperand &>(*Operands[4]).getReg())))
5674     return true;
5675 
5676   // Also check the 'mul' syntax variant that doesn't specify an explicit
5677   // destination register.
5678   if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 5 &&
5679       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5680       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5681       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5682       // If the registers aren't low regs  or the cc_out operand is zero
5683       // outside of an IT block, we have to use the 32-bit encoding, so
5684       // remove the cc_out operand.
5685       (!isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) ||
5686        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) ||
5687        !inITBlock()))
5688     return true;
5689 
5690 
5691 
5692   // Register-register 'add/sub' for thumb does not have a cc_out operand
5693   // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
5694   // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
5695   // right, this will result in better diagnostics (which operand is off)
5696   // anyway.
5697   if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
5698       (Operands.size() == 5 || Operands.size() == 6) &&
5699       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5700       static_cast<ARMOperand &>(*Operands[3]).getReg() == ARM::SP &&
5701       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5702       (static_cast<ARMOperand &>(*Operands[4]).isImm() ||
5703        (Operands.size() == 6 &&
5704         static_cast<ARMOperand &>(*Operands[5]).isImm())))
5705     return true;
5706 
5707   return false;
5708 }
5709 
5710 bool ARMAsmParser::shouldOmitPredicateOperand(StringRef Mnemonic,
5711                                               OperandVector &Operands) {
5712   // VRINT{Z, R, X} have a predicate operand in VFP, but not in NEON
5713   unsigned RegIdx = 3;
5714   if ((Mnemonic == "vrintz" || Mnemonic == "vrintx" || Mnemonic == "vrintr") &&
5715       (static_cast<ARMOperand &>(*Operands[2]).getToken() == ".f32" ||
5716        static_cast<ARMOperand &>(*Operands[2]).getToken() == ".f16")) {
5717     if (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
5718         (static_cast<ARMOperand &>(*Operands[3]).getToken() == ".f32" ||
5719          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".f16"))
5720       RegIdx = 4;
5721 
5722     if (static_cast<ARMOperand &>(*Operands[RegIdx]).isReg() &&
5723         (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(
5724              static_cast<ARMOperand &>(*Operands[RegIdx]).getReg()) ||
5725          ARMMCRegisterClasses[ARM::QPRRegClassID].contains(
5726              static_cast<ARMOperand &>(*Operands[RegIdx]).getReg())))
5727       return true;
5728   }
5729   return false;
5730 }
5731 
5732 static bool isDataTypeToken(StringRef Tok) {
5733   return Tok == ".8" || Tok == ".16" || Tok == ".32" || Tok == ".64" ||
5734     Tok == ".i8" || Tok == ".i16" || Tok == ".i32" || Tok == ".i64" ||
5735     Tok == ".u8" || Tok == ".u16" || Tok == ".u32" || Tok == ".u64" ||
5736     Tok == ".s8" || Tok == ".s16" || Tok == ".s32" || Tok == ".s64" ||
5737     Tok == ".p8" || Tok == ".p16" || Tok == ".f32" || Tok == ".f64" ||
5738     Tok == ".f" || Tok == ".d";
5739 }
5740 
5741 // FIXME: This bit should probably be handled via an explicit match class
5742 // in the .td files that matches the suffix instead of having it be
5743 // a literal string token the way it is now.
5744 static bool doesIgnoreDataTypeSuffix(StringRef Mnemonic, StringRef DT) {
5745   return Mnemonic.startswith("vldm") || Mnemonic.startswith("vstm");
5746 }
5747 static void applyMnemonicAliases(StringRef &Mnemonic, uint64_t Features,
5748                                  unsigned VariantID);
5749 
5750 static bool RequiresVFPRegListValidation(StringRef Inst,
5751                                          bool &AcceptSinglePrecisionOnly,
5752                                          bool &AcceptDoublePrecisionOnly) {
5753   if (Inst.size() < 7)
5754     return false;
5755 
5756   if (Inst.startswith("fldm") || Inst.startswith("fstm")) {
5757     StringRef AddressingMode = Inst.substr(4, 2);
5758     if (AddressingMode == "ia" || AddressingMode == "db" ||
5759         AddressingMode == "ea" || AddressingMode == "fd") {
5760       AcceptSinglePrecisionOnly = Inst[6] == 's';
5761       AcceptDoublePrecisionOnly = Inst[6] == 'd' || Inst[6] == 'x';
5762       return true;
5763     }
5764   }
5765 
5766   return false;
5767 }
5768 
5769 /// Parse an arm instruction mnemonic followed by its operands.
5770 bool ARMAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
5771                                     SMLoc NameLoc, OperandVector &Operands) {
5772   MCAsmParser &Parser = getParser();
5773   // FIXME: Can this be done via tablegen in some fashion?
5774   bool RequireVFPRegisterListCheck;
5775   bool AcceptSinglePrecisionOnly;
5776   bool AcceptDoublePrecisionOnly;
5777   RequireVFPRegisterListCheck =
5778     RequiresVFPRegListValidation(Name, AcceptSinglePrecisionOnly,
5779                                  AcceptDoublePrecisionOnly);
5780 
5781   // Apply mnemonic aliases before doing anything else, as the destination
5782   // mnemonic may include suffices and we want to handle them normally.
5783   // The generic tblgen'erated code does this later, at the start of
5784   // MatchInstructionImpl(), but that's too late for aliases that include
5785   // any sort of suffix.
5786   uint64_t AvailableFeatures = getAvailableFeatures();
5787   unsigned AssemblerDialect = getParser().getAssemblerDialect();
5788   applyMnemonicAliases(Name, AvailableFeatures, AssemblerDialect);
5789 
5790   // First check for the ARM-specific .req directive.
5791   if (Parser.getTok().is(AsmToken::Identifier) &&
5792       Parser.getTok().getIdentifier() == ".req") {
5793     parseDirectiveReq(Name, NameLoc);
5794     // We always return 'error' for this, as we're done with this
5795     // statement and don't need to match the 'instruction."
5796     return true;
5797   }
5798 
5799   // Create the leading tokens for the mnemonic, split by '.' characters.
5800   size_t Start = 0, Next = Name.find('.');
5801   StringRef Mnemonic = Name.slice(Start, Next);
5802 
5803   // Split out the predication code and carry setting flag from the mnemonic.
5804   unsigned PredicationCode;
5805   unsigned ProcessorIMod;
5806   bool CarrySetting;
5807   StringRef ITMask;
5808   Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
5809                            ProcessorIMod, ITMask);
5810 
5811   // In Thumb1, only the branch (B) instruction can be predicated.
5812   if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
5813     Parser.eatToEndOfStatement();
5814     return Error(NameLoc, "conditional execution not supported in Thumb1");
5815   }
5816 
5817   Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
5818 
5819   // Handle the IT instruction ITMask. Convert it to a bitmask. This
5820   // is the mask as it will be for the IT encoding if the conditional
5821   // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
5822   // where the conditional bit0 is zero, the instruction post-processing
5823   // will adjust the mask accordingly.
5824   if (Mnemonic == "it") {
5825     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
5826     if (ITMask.size() > 3) {
5827       Parser.eatToEndOfStatement();
5828       return Error(Loc, "too many conditions on IT instruction");
5829     }
5830     unsigned Mask = 8;
5831     for (unsigned i = ITMask.size(); i != 0; --i) {
5832       char pos = ITMask[i - 1];
5833       if (pos != 't' && pos != 'e') {
5834         Parser.eatToEndOfStatement();
5835         return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
5836       }
5837       Mask >>= 1;
5838       if (ITMask[i - 1] == 't')
5839         Mask |= 8;
5840     }
5841     Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
5842   }
5843 
5844   // FIXME: This is all a pretty gross hack. We should automatically handle
5845   // optional operands like this via tblgen.
5846 
5847   // Next, add the CCOut and ConditionCode operands, if needed.
5848   //
5849   // For mnemonics which can ever incorporate a carry setting bit or predication
5850   // code, our matching model involves us always generating CCOut and
5851   // ConditionCode operands to match the mnemonic "as written" and then we let
5852   // the matcher deal with finding the right instruction or generating an
5853   // appropriate error.
5854   bool CanAcceptCarrySet, CanAcceptPredicationCode;
5855   getMnemonicAcceptInfo(Mnemonic, Name, CanAcceptCarrySet, CanAcceptPredicationCode);
5856 
5857   // If we had a carry-set on an instruction that can't do that, issue an
5858   // error.
5859   if (!CanAcceptCarrySet && CarrySetting) {
5860     Parser.eatToEndOfStatement();
5861     return Error(NameLoc, "instruction '" + Mnemonic +
5862                  "' can not set flags, but 's' suffix specified");
5863   }
5864   // If we had a predication code on an instruction that can't do that, issue an
5865   // error.
5866   if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
5867     Parser.eatToEndOfStatement();
5868     return Error(NameLoc, "instruction '" + Mnemonic +
5869                  "' is not predicable, but condition code specified");
5870   }
5871 
5872   // Add the carry setting operand, if necessary.
5873   if (CanAcceptCarrySet) {
5874     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
5875     Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
5876                                                Loc));
5877   }
5878 
5879   // Add the predication code operand, if necessary.
5880   if (CanAcceptPredicationCode) {
5881     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
5882                                       CarrySetting);
5883     Operands.push_back(ARMOperand::CreateCondCode(
5884                          ARMCC::CondCodes(PredicationCode), Loc));
5885   }
5886 
5887   // Add the processor imod operand, if necessary.
5888   if (ProcessorIMod) {
5889     Operands.push_back(ARMOperand::CreateImm(
5890           MCConstantExpr::create(ProcessorIMod, getContext()),
5891                                  NameLoc, NameLoc));
5892   } else if (Mnemonic == "cps" && isMClass()) {
5893     return Error(NameLoc, "instruction 'cps' requires effect for M-class");
5894   }
5895 
5896   // Add the remaining tokens in the mnemonic.
5897   while (Next != StringRef::npos) {
5898     Start = Next;
5899     Next = Name.find('.', Start + 1);
5900     StringRef ExtraToken = Name.slice(Start, Next);
5901 
5902     // Some NEON instructions have an optional datatype suffix that is
5903     // completely ignored. Check for that.
5904     if (isDataTypeToken(ExtraToken) &&
5905         doesIgnoreDataTypeSuffix(Mnemonic, ExtraToken))
5906       continue;
5907 
5908     // For for ARM mode generate an error if the .n qualifier is used.
5909     if (ExtraToken == ".n" && !isThumb()) {
5910       SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
5911       Parser.eatToEndOfStatement();
5912       return Error(Loc, "instruction with .n (narrow) qualifier not allowed in "
5913                    "arm mode");
5914     }
5915 
5916     // The .n qualifier is always discarded as that is what the tables
5917     // and matcher expect.  In ARM mode the .w qualifier has no effect,
5918     // so discard it to avoid errors that can be caused by the matcher.
5919     if (ExtraToken != ".n" && (isThumb() || ExtraToken != ".w")) {
5920       SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
5921       Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
5922     }
5923   }
5924 
5925   // Read the remaining operands.
5926   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5927     // Read the first operand.
5928     if (parseOperand(Operands, Mnemonic)) {
5929       Parser.eatToEndOfStatement();
5930       return true;
5931     }
5932 
5933     while (getLexer().is(AsmToken::Comma)) {
5934       Parser.Lex();  // Eat the comma.
5935 
5936       // Parse and remember the operand.
5937       if (parseOperand(Operands, Mnemonic)) {
5938         Parser.eatToEndOfStatement();
5939         return true;
5940       }
5941     }
5942   }
5943 
5944   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5945     SMLoc Loc = getLexer().getLoc();
5946     Parser.eatToEndOfStatement();
5947     return Error(Loc, "unexpected token in argument list");
5948   }
5949 
5950   Parser.Lex(); // Consume the EndOfStatement
5951 
5952   if (RequireVFPRegisterListCheck) {
5953     ARMOperand &Op = static_cast<ARMOperand &>(*Operands.back());
5954     if (AcceptSinglePrecisionOnly && !Op.isSPRRegList())
5955       return Error(Op.getStartLoc(),
5956                    "VFP/Neon single precision register expected");
5957     if (AcceptDoublePrecisionOnly && !Op.isDPRRegList())
5958       return Error(Op.getStartLoc(),
5959                    "VFP/Neon double precision register expected");
5960   }
5961 
5962   tryConvertingToTwoOperandForm(Mnemonic, CarrySetting, Operands);
5963 
5964   // Some instructions, mostly Thumb, have forms for the same mnemonic that
5965   // do and don't have a cc_out optional-def operand. With some spot-checks
5966   // of the operand list, we can figure out which variant we're trying to
5967   // parse and adjust accordingly before actually matching. We shouldn't ever
5968   // try to remove a cc_out operand that was explicitly set on the
5969   // mnemonic, of course (CarrySetting == true). Reason number #317 the
5970   // table driven matcher doesn't fit well with the ARM instruction set.
5971   if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands))
5972     Operands.erase(Operands.begin() + 1);
5973 
5974   // Some instructions have the same mnemonic, but don't always
5975   // have a predicate. Distinguish them here and delete the
5976   // predicate if needed.
5977   if (shouldOmitPredicateOperand(Mnemonic, Operands))
5978     Operands.erase(Operands.begin() + 1);
5979 
5980   // ARM mode 'blx' need special handling, as the register operand version
5981   // is predicable, but the label operand version is not. So, we can't rely
5982   // on the Mnemonic based checking to correctly figure out when to put
5983   // a k_CondCode operand in the list. If we're trying to match the label
5984   // version, remove the k_CondCode operand here.
5985   if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
5986       static_cast<ARMOperand &>(*Operands[2]).isImm())
5987     Operands.erase(Operands.begin() + 1);
5988 
5989   // Adjust operands of ldrexd/strexd to MCK_GPRPair.
5990   // ldrexd/strexd require even/odd GPR pair. To enforce this constraint,
5991   // a single GPRPair reg operand is used in the .td file to replace the two
5992   // GPRs. However, when parsing from asm, the two GRPs cannot be automatically
5993   // expressed as a GPRPair, so we have to manually merge them.
5994   // FIXME: We would really like to be able to tablegen'erate this.
5995   if (!isThumb() && Operands.size() > 4 &&
5996       (Mnemonic == "ldrexd" || Mnemonic == "strexd" || Mnemonic == "ldaexd" ||
5997        Mnemonic == "stlexd")) {
5998     bool isLoad = (Mnemonic == "ldrexd" || Mnemonic == "ldaexd");
5999     unsigned Idx = isLoad ? 2 : 3;
6000     ARMOperand &Op1 = static_cast<ARMOperand &>(*Operands[Idx]);
6001     ARMOperand &Op2 = static_cast<ARMOperand &>(*Operands[Idx + 1]);
6002 
6003     const MCRegisterClass& MRC = MRI->getRegClass(ARM::GPRRegClassID);
6004     // Adjust only if Op1 and Op2 are GPRs.
6005     if (Op1.isReg() && Op2.isReg() && MRC.contains(Op1.getReg()) &&
6006         MRC.contains(Op2.getReg())) {
6007       unsigned Reg1 = Op1.getReg();
6008       unsigned Reg2 = Op2.getReg();
6009       unsigned Rt = MRI->getEncodingValue(Reg1);
6010       unsigned Rt2 = MRI->getEncodingValue(Reg2);
6011 
6012       // Rt2 must be Rt + 1 and Rt must be even.
6013       if (Rt + 1 != Rt2 || (Rt & 1)) {
6014         Error(Op2.getStartLoc(), isLoad
6015                                      ? "destination operands must be sequential"
6016                                      : "source operands must be sequential");
6017         return true;
6018       }
6019       unsigned NewReg = MRI->getMatchingSuperReg(Reg1, ARM::gsub_0,
6020           &(MRI->getRegClass(ARM::GPRPairRegClassID)));
6021       Operands[Idx] =
6022           ARMOperand::CreateReg(NewReg, Op1.getStartLoc(), Op2.getEndLoc());
6023       Operands.erase(Operands.begin() + Idx + 1);
6024     }
6025   }
6026 
6027   // GNU Assembler extension (compatibility)
6028   if ((Mnemonic == "ldrd" || Mnemonic == "strd")) {
6029     ARMOperand &Op2 = static_cast<ARMOperand &>(*Operands[2]);
6030     ARMOperand &Op3 = static_cast<ARMOperand &>(*Operands[3]);
6031     if (Op3.isMem()) {
6032       assert(Op2.isReg() && "expected register argument");
6033 
6034       unsigned SuperReg = MRI->getMatchingSuperReg(
6035           Op2.getReg(), ARM::gsub_0, &MRI->getRegClass(ARM::GPRPairRegClassID));
6036 
6037       assert(SuperReg && "expected register pair");
6038 
6039       unsigned PairedReg = MRI->getSubReg(SuperReg, ARM::gsub_1);
6040 
6041       Operands.insert(
6042           Operands.begin() + 3,
6043           ARMOperand::CreateReg(PairedReg, Op2.getStartLoc(), Op2.getEndLoc()));
6044     }
6045   }
6046 
6047   // FIXME: As said above, this is all a pretty gross hack.  This instruction
6048   // does not fit with other "subs" and tblgen.
6049   // Adjust operands of B9.3.19 SUBS PC, LR, #imm (Thumb2) system instruction
6050   // so the Mnemonic is the original name "subs" and delete the predicate
6051   // operand so it will match the table entry.
6052   if (isThumbTwo() && Mnemonic == "sub" && Operands.size() == 6 &&
6053       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
6054       static_cast<ARMOperand &>(*Operands[3]).getReg() == ARM::PC &&
6055       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
6056       static_cast<ARMOperand &>(*Operands[4]).getReg() == ARM::LR &&
6057       static_cast<ARMOperand &>(*Operands[5]).isImm()) {
6058     Operands.front() = ARMOperand::CreateToken(Name, NameLoc);
6059     Operands.erase(Operands.begin() + 1);
6060   }
6061   return false;
6062 }
6063 
6064 // Validate context-sensitive operand constraints.
6065 
6066 // return 'true' if register list contains non-low GPR registers,
6067 // 'false' otherwise. If Reg is in the register list or is HiReg, set
6068 // 'containsReg' to true.
6069 static bool checkLowRegisterList(const MCInst &Inst, unsigned OpNo,
6070                                  unsigned Reg, unsigned HiReg,
6071                                  bool &containsReg) {
6072   containsReg = false;
6073   for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
6074     unsigned OpReg = Inst.getOperand(i).getReg();
6075     if (OpReg == Reg)
6076       containsReg = true;
6077     // Anything other than a low register isn't legal here.
6078     if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
6079       return true;
6080   }
6081   return false;
6082 }
6083 
6084 // Check if the specified regisgter is in the register list of the inst,
6085 // starting at the indicated operand number.
6086 static bool listContainsReg(const MCInst &Inst, unsigned OpNo, unsigned Reg) {
6087   for (unsigned i = OpNo, e = Inst.getNumOperands(); i < e; ++i) {
6088     unsigned OpReg = Inst.getOperand(i).getReg();
6089     if (OpReg == Reg)
6090       return true;
6091   }
6092   return false;
6093 }
6094 
6095 // Return true if instruction has the interesting property of being
6096 // allowed in IT blocks, but not being predicable.
6097 static bool instIsBreakpoint(const MCInst &Inst) {
6098     return Inst.getOpcode() == ARM::tBKPT ||
6099            Inst.getOpcode() == ARM::BKPT ||
6100            Inst.getOpcode() == ARM::tHLT ||
6101            Inst.getOpcode() == ARM::HLT;
6102 
6103 }
6104 
6105 bool ARMAsmParser::validatetLDMRegList(const MCInst &Inst,
6106                                        const OperandVector &Operands,
6107                                        unsigned ListNo, bool IsARPop) {
6108   const ARMOperand &Op = static_cast<const ARMOperand &>(*Operands[ListNo]);
6109   bool HasWritebackToken = Op.isToken() && Op.getToken() == "!";
6110 
6111   bool ListContainsSP = listContainsReg(Inst, ListNo, ARM::SP);
6112   bool ListContainsLR = listContainsReg(Inst, ListNo, ARM::LR);
6113   bool ListContainsPC = listContainsReg(Inst, ListNo, ARM::PC);
6114 
6115   if (!IsARPop && ListContainsSP)
6116     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6117                  "SP may not be in the register list");
6118   else if (ListContainsPC && ListContainsLR)
6119     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6120                  "PC and LR may not be in the register list simultaneously");
6121   else if (inITBlock() && !lastInITBlock() && ListContainsPC)
6122     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6123                  "instruction must be outside of IT block or the last "
6124                  "instruction in an IT block");
6125   return false;
6126 }
6127 
6128 bool ARMAsmParser::validatetSTMRegList(const MCInst &Inst,
6129                                        const OperandVector &Operands,
6130                                        unsigned ListNo) {
6131   const ARMOperand &Op = static_cast<const ARMOperand &>(*Operands[ListNo]);
6132   bool HasWritebackToken = Op.isToken() && Op.getToken() == "!";
6133 
6134   bool ListContainsSP = listContainsReg(Inst, ListNo, ARM::SP);
6135   bool ListContainsPC = listContainsReg(Inst, ListNo, ARM::PC);
6136 
6137   if (ListContainsSP && ListContainsPC)
6138     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6139                  "SP and PC may not be in the register list");
6140   else if (ListContainsSP)
6141     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6142                  "SP may not be in the register list");
6143   else if (ListContainsPC)
6144     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6145                  "PC may not be in the register list");
6146   return false;
6147 }
6148 
6149 // FIXME: We would really like to be able to tablegen'erate this.
6150 bool ARMAsmParser::validateInstruction(MCInst &Inst,
6151                                        const OperandVector &Operands) {
6152   const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
6153   SMLoc Loc = Operands[0]->getStartLoc();
6154 
6155   // Check the IT block state first.
6156   // NOTE: BKPT and HLT instructions have the interesting property of being
6157   // allowed in IT blocks, but not being predicable. They just always execute.
6158   if (inITBlock() && !instIsBreakpoint(Inst)) {
6159     unsigned Bit = 1;
6160     if (ITState.FirstCond)
6161       ITState.FirstCond = false;
6162     else
6163       Bit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
6164     // The instruction must be predicable.
6165     if (!MCID.isPredicable())
6166       return Error(Loc, "instructions in IT block must be predicable");
6167     unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
6168     unsigned ITCond = Bit ? ITState.Cond :
6169       ARMCC::getOppositeCondition(ITState.Cond);
6170     if (Cond != ITCond) {
6171       // Find the condition code Operand to get its SMLoc information.
6172       SMLoc CondLoc;
6173       for (unsigned I = 1; I < Operands.size(); ++I)
6174         if (static_cast<ARMOperand &>(*Operands[I]).isCondCode())
6175           CondLoc = Operands[I]->getStartLoc();
6176       return Error(CondLoc, "incorrect condition in IT block; got '" +
6177                    StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
6178                    "', but expected '" +
6179                    ARMCondCodeToString(ARMCC::CondCodes(ITCond)) + "'");
6180     }
6181   // Check for non-'al' condition codes outside of the IT block.
6182   } else if (isThumbTwo() && MCID.isPredicable() &&
6183              Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
6184              ARMCC::AL && Inst.getOpcode() != ARM::tBcc &&
6185              Inst.getOpcode() != ARM::t2Bcc)
6186     return Error(Loc, "predicated instructions must be in IT block");
6187 
6188   const unsigned Opcode = Inst.getOpcode();
6189   switch (Opcode) {
6190   case ARM::LDRD:
6191   case ARM::LDRD_PRE:
6192   case ARM::LDRD_POST: {
6193     const unsigned RtReg = Inst.getOperand(0).getReg();
6194 
6195     // Rt can't be R14.
6196     if (RtReg == ARM::LR)
6197       return Error(Operands[3]->getStartLoc(),
6198                    "Rt can't be R14");
6199 
6200     const unsigned Rt = MRI->getEncodingValue(RtReg);
6201     // Rt must be even-numbered.
6202     if ((Rt & 1) == 1)
6203       return Error(Operands[3]->getStartLoc(),
6204                    "Rt must be even-numbered");
6205 
6206     // Rt2 must be Rt + 1.
6207     const unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6208     if (Rt2 != Rt + 1)
6209       return Error(Operands[3]->getStartLoc(),
6210                    "destination operands must be sequential");
6211 
6212     if (Opcode == ARM::LDRD_PRE || Opcode == ARM::LDRD_POST) {
6213       const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(3).getReg());
6214       // For addressing modes with writeback, the base register needs to be
6215       // different from the destination registers.
6216       if (Rn == Rt || Rn == Rt2)
6217         return Error(Operands[3]->getStartLoc(),
6218                      "base register needs to be different from destination "
6219                      "registers");
6220     }
6221 
6222     return false;
6223   }
6224   case ARM::t2LDRDi8:
6225   case ARM::t2LDRD_PRE:
6226   case ARM::t2LDRD_POST: {
6227     // Rt2 must be different from Rt.
6228     unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
6229     unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6230     if (Rt2 == Rt)
6231       return Error(Operands[3]->getStartLoc(),
6232                    "destination operands can't be identical");
6233     return false;
6234   }
6235   case ARM::t2BXJ: {
6236     const unsigned RmReg = Inst.getOperand(0).getReg();
6237     // Rm = SP is no longer unpredictable in v8-A
6238     if (RmReg == ARM::SP && !hasV8Ops())
6239       return Error(Operands[2]->getStartLoc(),
6240                    "r13 (SP) is an unpredictable operand to BXJ");
6241     return false;
6242   }
6243   case ARM::STRD: {
6244     // Rt2 must be Rt + 1.
6245     unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
6246     unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6247     if (Rt2 != Rt + 1)
6248       return Error(Operands[3]->getStartLoc(),
6249                    "source operands must be sequential");
6250     return false;
6251   }
6252   case ARM::STRD_PRE:
6253   case ARM::STRD_POST: {
6254     // Rt2 must be Rt + 1.
6255     unsigned Rt = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6256     unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(2).getReg());
6257     if (Rt2 != Rt + 1)
6258       return Error(Operands[3]->getStartLoc(),
6259                    "source operands must be sequential");
6260     return false;
6261   }
6262   case ARM::STR_PRE_IMM:
6263   case ARM::STR_PRE_REG:
6264   case ARM::STR_POST_IMM:
6265   case ARM::STR_POST_REG:
6266   case ARM::STRH_PRE:
6267   case ARM::STRH_POST:
6268   case ARM::STRB_PRE_IMM:
6269   case ARM::STRB_PRE_REG:
6270   case ARM::STRB_POST_IMM:
6271   case ARM::STRB_POST_REG: {
6272     // Rt must be different from Rn.
6273     const unsigned Rt = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6274     const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(2).getReg());
6275 
6276     if (Rt == Rn)
6277       return Error(Operands[3]->getStartLoc(),
6278                    "source register and base register can't be identical");
6279     return false;
6280   }
6281   case ARM::LDR_PRE_IMM:
6282   case ARM::LDR_PRE_REG:
6283   case ARM::LDR_POST_IMM:
6284   case ARM::LDR_POST_REG:
6285   case ARM::LDRH_PRE:
6286   case ARM::LDRH_POST:
6287   case ARM::LDRSH_PRE:
6288   case ARM::LDRSH_POST:
6289   case ARM::LDRB_PRE_IMM:
6290   case ARM::LDRB_PRE_REG:
6291   case ARM::LDRB_POST_IMM:
6292   case ARM::LDRB_POST_REG:
6293   case ARM::LDRSB_PRE:
6294   case ARM::LDRSB_POST: {
6295     // Rt must be different from Rn.
6296     const unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
6297     const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(2).getReg());
6298 
6299     if (Rt == Rn)
6300       return Error(Operands[3]->getStartLoc(),
6301                    "destination register and base register can't be identical");
6302     return false;
6303   }
6304   case ARM::SBFX:
6305   case ARM::UBFX: {
6306     // Width must be in range [1, 32-lsb].
6307     unsigned LSB = Inst.getOperand(2).getImm();
6308     unsigned Widthm1 = Inst.getOperand(3).getImm();
6309     if (Widthm1 >= 32 - LSB)
6310       return Error(Operands[5]->getStartLoc(),
6311                    "bitfield width must be in range [1,32-lsb]");
6312     return false;
6313   }
6314   // Notionally handles ARM::tLDMIA_UPD too.
6315   case ARM::tLDMIA: {
6316     // If we're parsing Thumb2, the .w variant is available and handles
6317     // most cases that are normally illegal for a Thumb1 LDM instruction.
6318     // We'll make the transformation in processInstruction() if necessary.
6319     //
6320     // Thumb LDM instructions are writeback iff the base register is not
6321     // in the register list.
6322     unsigned Rn = Inst.getOperand(0).getReg();
6323     bool HasWritebackToken =
6324         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
6325          static_cast<ARMOperand &>(*Operands[3]).getToken() == "!");
6326     bool ListContainsBase;
6327     if (checkLowRegisterList(Inst, 3, Rn, 0, ListContainsBase) && !isThumbTwo())
6328       return Error(Operands[3 + HasWritebackToken]->getStartLoc(),
6329                    "registers must be in range r0-r7");
6330     // If we should have writeback, then there should be a '!' token.
6331     if (!ListContainsBase && !HasWritebackToken && !isThumbTwo())
6332       return Error(Operands[2]->getStartLoc(),
6333                    "writeback operator '!' expected");
6334     // If we should not have writeback, there must not be a '!'. This is
6335     // true even for the 32-bit wide encodings.
6336     if (ListContainsBase && HasWritebackToken)
6337       return Error(Operands[3]->getStartLoc(),
6338                    "writeback operator '!' not allowed when base register "
6339                    "in register list");
6340 
6341     if (validatetLDMRegList(Inst, Operands, 3))
6342       return true;
6343     break;
6344   }
6345   case ARM::LDMIA_UPD:
6346   case ARM::LDMDB_UPD:
6347   case ARM::LDMIB_UPD:
6348   case ARM::LDMDA_UPD:
6349     // ARM variants loading and updating the same register are only officially
6350     // UNPREDICTABLE on v7 upwards. Goodness knows what they did before.
6351     if (!hasV7Ops())
6352       break;
6353     if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
6354       return Error(Operands.back()->getStartLoc(),
6355                    "writeback register not allowed in register list");
6356     break;
6357   case ARM::t2LDMIA:
6358   case ARM::t2LDMDB:
6359     if (validatetLDMRegList(Inst, Operands, 3))
6360       return true;
6361     break;
6362   case ARM::t2STMIA:
6363   case ARM::t2STMDB:
6364     if (validatetSTMRegList(Inst, Operands, 3))
6365       return true;
6366     break;
6367   case ARM::t2LDMIA_UPD:
6368   case ARM::t2LDMDB_UPD:
6369   case ARM::t2STMIA_UPD:
6370   case ARM::t2STMDB_UPD: {
6371     if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
6372       return Error(Operands.back()->getStartLoc(),
6373                    "writeback register not allowed in register list");
6374 
6375     if (Opcode == ARM::t2LDMIA_UPD || Opcode == ARM::t2LDMDB_UPD) {
6376       if (validatetLDMRegList(Inst, Operands, 3))
6377         return true;
6378     } else {
6379       if (validatetSTMRegList(Inst, Operands, 3))
6380         return true;
6381     }
6382     break;
6383   }
6384   case ARM::sysLDMIA_UPD:
6385   case ARM::sysLDMDA_UPD:
6386   case ARM::sysLDMDB_UPD:
6387   case ARM::sysLDMIB_UPD:
6388     if (!listContainsReg(Inst, 3, ARM::PC))
6389       return Error(Operands[4]->getStartLoc(),
6390                    "writeback register only allowed on system LDM "
6391                    "if PC in register-list");
6392     break;
6393   case ARM::sysSTMIA_UPD:
6394   case ARM::sysSTMDA_UPD:
6395   case ARM::sysSTMDB_UPD:
6396   case ARM::sysSTMIB_UPD:
6397     return Error(Operands[2]->getStartLoc(),
6398                  "system STM cannot have writeback register");
6399   case ARM::tMUL: {
6400     // The second source operand must be the same register as the destination
6401     // operand.
6402     //
6403     // In this case, we must directly check the parsed operands because the
6404     // cvtThumbMultiply() function is written in such a way that it guarantees
6405     // this first statement is always true for the new Inst.  Essentially, the
6406     // destination is unconditionally copied into the second source operand
6407     // without checking to see if it matches what we actually parsed.
6408     if (Operands.size() == 6 && (((ARMOperand &)*Operands[3]).getReg() !=
6409                                  ((ARMOperand &)*Operands[5]).getReg()) &&
6410         (((ARMOperand &)*Operands[3]).getReg() !=
6411          ((ARMOperand &)*Operands[4]).getReg())) {
6412       return Error(Operands[3]->getStartLoc(),
6413                    "destination register must match source register");
6414     }
6415     break;
6416   }
6417   // Like for ldm/stm, push and pop have hi-reg handling version in Thumb2,
6418   // so only issue a diagnostic for thumb1. The instructions will be
6419   // switched to the t2 encodings in processInstruction() if necessary.
6420   case ARM::tPOP: {
6421     bool ListContainsBase;
6422     if (checkLowRegisterList(Inst, 2, 0, ARM::PC, ListContainsBase) &&
6423         !isThumbTwo())
6424       return Error(Operands[2]->getStartLoc(),
6425                    "registers must be in range r0-r7 or pc");
6426     if (validatetLDMRegList(Inst, Operands, 2, !isMClass()))
6427       return true;
6428     break;
6429   }
6430   case ARM::tPUSH: {
6431     bool ListContainsBase;
6432     if (checkLowRegisterList(Inst, 2, 0, ARM::LR, ListContainsBase) &&
6433         !isThumbTwo())
6434       return Error(Operands[2]->getStartLoc(),
6435                    "registers must be in range r0-r7 or lr");
6436     if (validatetSTMRegList(Inst, Operands, 2))
6437       return true;
6438     break;
6439   }
6440   case ARM::tSTMIA_UPD: {
6441     bool ListContainsBase, InvalidLowList;
6442     InvalidLowList = checkLowRegisterList(Inst, 4, Inst.getOperand(0).getReg(),
6443                                           0, ListContainsBase);
6444     if (InvalidLowList && !isThumbTwo())
6445       return Error(Operands[4]->getStartLoc(),
6446                    "registers must be in range r0-r7");
6447 
6448     // This would be converted to a 32-bit stm, but that's not valid if the
6449     // writeback register is in the list.
6450     if (InvalidLowList && ListContainsBase)
6451       return Error(Operands[4]->getStartLoc(),
6452                    "writeback operator '!' not allowed when base register "
6453                    "in register list");
6454 
6455     if (validatetSTMRegList(Inst, Operands, 4))
6456       return true;
6457     break;
6458   }
6459   case ARM::tADDrSP: {
6460     // If the non-SP source operand and the destination operand are not the
6461     // same, we need thumb2 (for the wide encoding), or we have an error.
6462     if (!isThumbTwo() &&
6463         Inst.getOperand(0).getReg() != Inst.getOperand(2).getReg()) {
6464       return Error(Operands[4]->getStartLoc(),
6465                    "source register must be the same as destination");
6466     }
6467     break;
6468   }
6469   // Final range checking for Thumb unconditional branch instructions.
6470   case ARM::tB:
6471     if (!(static_cast<ARMOperand &>(*Operands[2])).isSignedOffset<11, 1>())
6472       return Error(Operands[2]->getStartLoc(), "branch target out of range");
6473     break;
6474   case ARM::t2B: {
6475     int op = (Operands[2]->isImm()) ? 2 : 3;
6476     if (!static_cast<ARMOperand &>(*Operands[op]).isSignedOffset<24, 1>())
6477       return Error(Operands[op]->getStartLoc(), "branch target out of range");
6478     break;
6479   }
6480   // Final range checking for Thumb conditional branch instructions.
6481   case ARM::tBcc:
6482     if (!static_cast<ARMOperand &>(*Operands[2]).isSignedOffset<8, 1>())
6483       return Error(Operands[2]->getStartLoc(), "branch target out of range");
6484     break;
6485   case ARM::t2Bcc: {
6486     int Op = (Operands[2]->isImm()) ? 2 : 3;
6487     if (!static_cast<ARMOperand &>(*Operands[Op]).isSignedOffset<20, 1>())
6488       return Error(Operands[Op]->getStartLoc(), "branch target out of range");
6489     break;
6490   }
6491   case ARM::MOVi16:
6492   case ARM::t2MOVi16:
6493   case ARM::t2MOVTi16:
6494     {
6495     // We want to avoid misleadingly allowing something like "mov r0, <symbol>"
6496     // especially when we turn it into a movw and the expression <symbol> does
6497     // not have a :lower16: or :upper16 as part of the expression.  We don't
6498     // want the behavior of silently truncating, which can be unexpected and
6499     // lead to bugs that are difficult to find since this is an easy mistake
6500     // to make.
6501     int i = (Operands[3]->isImm()) ? 3 : 4;
6502     ARMOperand &Op = static_cast<ARMOperand &>(*Operands[i]);
6503     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm());
6504     if (CE) break;
6505     const MCExpr *E = dyn_cast<MCExpr>(Op.getImm());
6506     if (!E) break;
6507     const ARMMCExpr *ARM16Expr = dyn_cast<ARMMCExpr>(E);
6508     if (!ARM16Expr || (ARM16Expr->getKind() != ARMMCExpr::VK_ARM_HI16 &&
6509                        ARM16Expr->getKind() != ARMMCExpr::VK_ARM_LO16))
6510       return Error(
6511           Op.getStartLoc(),
6512           "immediate expression for mov requires :lower16: or :upper16");
6513     break;
6514   }
6515   }
6516 
6517   return false;
6518 }
6519 
6520 static unsigned getRealVSTOpcode(unsigned Opc, unsigned &Spacing) {
6521   switch(Opc) {
6522   default: llvm_unreachable("unexpected opcode!");
6523   // VST1LN
6524   case ARM::VST1LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST1LNd8_UPD;
6525   case ARM::VST1LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST1LNd16_UPD;
6526   case ARM::VST1LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST1LNd32_UPD;
6527   case ARM::VST1LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST1LNd8_UPD;
6528   case ARM::VST1LNdWB_register_Asm_16: Spacing = 1; return ARM::VST1LNd16_UPD;
6529   case ARM::VST1LNdWB_register_Asm_32: Spacing = 1; return ARM::VST1LNd32_UPD;
6530   case ARM::VST1LNdAsm_8:  Spacing = 1; return ARM::VST1LNd8;
6531   case ARM::VST1LNdAsm_16: Spacing = 1; return ARM::VST1LNd16;
6532   case ARM::VST1LNdAsm_32: Spacing = 1; return ARM::VST1LNd32;
6533 
6534   // VST2LN
6535   case ARM::VST2LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST2LNd8_UPD;
6536   case ARM::VST2LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST2LNd16_UPD;
6537   case ARM::VST2LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST2LNd32_UPD;
6538   case ARM::VST2LNqWB_fixed_Asm_16: Spacing = 2; return ARM::VST2LNq16_UPD;
6539   case ARM::VST2LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST2LNq32_UPD;
6540 
6541   case ARM::VST2LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST2LNd8_UPD;
6542   case ARM::VST2LNdWB_register_Asm_16: Spacing = 1; return ARM::VST2LNd16_UPD;
6543   case ARM::VST2LNdWB_register_Asm_32: Spacing = 1; return ARM::VST2LNd32_UPD;
6544   case ARM::VST2LNqWB_register_Asm_16: Spacing = 2; return ARM::VST2LNq16_UPD;
6545   case ARM::VST2LNqWB_register_Asm_32: Spacing = 2; return ARM::VST2LNq32_UPD;
6546 
6547   case ARM::VST2LNdAsm_8:  Spacing = 1; return ARM::VST2LNd8;
6548   case ARM::VST2LNdAsm_16: Spacing = 1; return ARM::VST2LNd16;
6549   case ARM::VST2LNdAsm_32: Spacing = 1; return ARM::VST2LNd32;
6550   case ARM::VST2LNqAsm_16: Spacing = 2; return ARM::VST2LNq16;
6551   case ARM::VST2LNqAsm_32: Spacing = 2; return ARM::VST2LNq32;
6552 
6553   // VST3LN
6554   case ARM::VST3LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST3LNd8_UPD;
6555   case ARM::VST3LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST3LNd16_UPD;
6556   case ARM::VST3LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST3LNd32_UPD;
6557   case ARM::VST3LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VST3LNq16_UPD;
6558   case ARM::VST3LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST3LNq32_UPD;
6559   case ARM::VST3LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST3LNd8_UPD;
6560   case ARM::VST3LNdWB_register_Asm_16: Spacing = 1; return ARM::VST3LNd16_UPD;
6561   case ARM::VST3LNdWB_register_Asm_32: Spacing = 1; return ARM::VST3LNd32_UPD;
6562   case ARM::VST3LNqWB_register_Asm_16: Spacing = 2; return ARM::VST3LNq16_UPD;
6563   case ARM::VST3LNqWB_register_Asm_32: Spacing = 2; return ARM::VST3LNq32_UPD;
6564   case ARM::VST3LNdAsm_8:  Spacing = 1; return ARM::VST3LNd8;
6565   case ARM::VST3LNdAsm_16: Spacing = 1; return ARM::VST3LNd16;
6566   case ARM::VST3LNdAsm_32: Spacing = 1; return ARM::VST3LNd32;
6567   case ARM::VST3LNqAsm_16: Spacing = 2; return ARM::VST3LNq16;
6568   case ARM::VST3LNqAsm_32: Spacing = 2; return ARM::VST3LNq32;
6569 
6570   // VST3
6571   case ARM::VST3dWB_fixed_Asm_8:  Spacing = 1; return ARM::VST3d8_UPD;
6572   case ARM::VST3dWB_fixed_Asm_16: Spacing = 1; return ARM::VST3d16_UPD;
6573   case ARM::VST3dWB_fixed_Asm_32: Spacing = 1; return ARM::VST3d32_UPD;
6574   case ARM::VST3qWB_fixed_Asm_8:  Spacing = 2; return ARM::VST3q8_UPD;
6575   case ARM::VST3qWB_fixed_Asm_16: Spacing = 2; return ARM::VST3q16_UPD;
6576   case ARM::VST3qWB_fixed_Asm_32: Spacing = 2; return ARM::VST3q32_UPD;
6577   case ARM::VST3dWB_register_Asm_8:  Spacing = 1; return ARM::VST3d8_UPD;
6578   case ARM::VST3dWB_register_Asm_16: Spacing = 1; return ARM::VST3d16_UPD;
6579   case ARM::VST3dWB_register_Asm_32: Spacing = 1; return ARM::VST3d32_UPD;
6580   case ARM::VST3qWB_register_Asm_8:  Spacing = 2; return ARM::VST3q8_UPD;
6581   case ARM::VST3qWB_register_Asm_16: Spacing = 2; return ARM::VST3q16_UPD;
6582   case ARM::VST3qWB_register_Asm_32: Spacing = 2; return ARM::VST3q32_UPD;
6583   case ARM::VST3dAsm_8:  Spacing = 1; return ARM::VST3d8;
6584   case ARM::VST3dAsm_16: Spacing = 1; return ARM::VST3d16;
6585   case ARM::VST3dAsm_32: Spacing = 1; return ARM::VST3d32;
6586   case ARM::VST3qAsm_8:  Spacing = 2; return ARM::VST3q8;
6587   case ARM::VST3qAsm_16: Spacing = 2; return ARM::VST3q16;
6588   case ARM::VST3qAsm_32: Spacing = 2; return ARM::VST3q32;
6589 
6590   // VST4LN
6591   case ARM::VST4LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST4LNd8_UPD;
6592   case ARM::VST4LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST4LNd16_UPD;
6593   case ARM::VST4LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST4LNd32_UPD;
6594   case ARM::VST4LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VST4LNq16_UPD;
6595   case ARM::VST4LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST4LNq32_UPD;
6596   case ARM::VST4LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST4LNd8_UPD;
6597   case ARM::VST4LNdWB_register_Asm_16: Spacing = 1; return ARM::VST4LNd16_UPD;
6598   case ARM::VST4LNdWB_register_Asm_32: Spacing = 1; return ARM::VST4LNd32_UPD;
6599   case ARM::VST4LNqWB_register_Asm_16: Spacing = 2; return ARM::VST4LNq16_UPD;
6600   case ARM::VST4LNqWB_register_Asm_32: Spacing = 2; return ARM::VST4LNq32_UPD;
6601   case ARM::VST4LNdAsm_8:  Spacing = 1; return ARM::VST4LNd8;
6602   case ARM::VST4LNdAsm_16: Spacing = 1; return ARM::VST4LNd16;
6603   case ARM::VST4LNdAsm_32: Spacing = 1; return ARM::VST4LNd32;
6604   case ARM::VST4LNqAsm_16: Spacing = 2; return ARM::VST4LNq16;
6605   case ARM::VST4LNqAsm_32: Spacing = 2; return ARM::VST4LNq32;
6606 
6607   // VST4
6608   case ARM::VST4dWB_fixed_Asm_8:  Spacing = 1; return ARM::VST4d8_UPD;
6609   case ARM::VST4dWB_fixed_Asm_16: Spacing = 1; return ARM::VST4d16_UPD;
6610   case ARM::VST4dWB_fixed_Asm_32: Spacing = 1; return ARM::VST4d32_UPD;
6611   case ARM::VST4qWB_fixed_Asm_8:  Spacing = 2; return ARM::VST4q8_UPD;
6612   case ARM::VST4qWB_fixed_Asm_16: Spacing = 2; return ARM::VST4q16_UPD;
6613   case ARM::VST4qWB_fixed_Asm_32: Spacing = 2; return ARM::VST4q32_UPD;
6614   case ARM::VST4dWB_register_Asm_8:  Spacing = 1; return ARM::VST4d8_UPD;
6615   case ARM::VST4dWB_register_Asm_16: Spacing = 1; return ARM::VST4d16_UPD;
6616   case ARM::VST4dWB_register_Asm_32: Spacing = 1; return ARM::VST4d32_UPD;
6617   case ARM::VST4qWB_register_Asm_8:  Spacing = 2; return ARM::VST4q8_UPD;
6618   case ARM::VST4qWB_register_Asm_16: Spacing = 2; return ARM::VST4q16_UPD;
6619   case ARM::VST4qWB_register_Asm_32: Spacing = 2; return ARM::VST4q32_UPD;
6620   case ARM::VST4dAsm_8:  Spacing = 1; return ARM::VST4d8;
6621   case ARM::VST4dAsm_16: Spacing = 1; return ARM::VST4d16;
6622   case ARM::VST4dAsm_32: Spacing = 1; return ARM::VST4d32;
6623   case ARM::VST4qAsm_8:  Spacing = 2; return ARM::VST4q8;
6624   case ARM::VST4qAsm_16: Spacing = 2; return ARM::VST4q16;
6625   case ARM::VST4qAsm_32: Spacing = 2; return ARM::VST4q32;
6626   }
6627 }
6628 
6629 static unsigned getRealVLDOpcode(unsigned Opc, unsigned &Spacing) {
6630   switch(Opc) {
6631   default: llvm_unreachable("unexpected opcode!");
6632   // VLD1LN
6633   case ARM::VLD1LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD1LNd8_UPD;
6634   case ARM::VLD1LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD1LNd16_UPD;
6635   case ARM::VLD1LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD1LNd32_UPD;
6636   case ARM::VLD1LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD1LNd8_UPD;
6637   case ARM::VLD1LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD1LNd16_UPD;
6638   case ARM::VLD1LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD1LNd32_UPD;
6639   case ARM::VLD1LNdAsm_8:  Spacing = 1; return ARM::VLD1LNd8;
6640   case ARM::VLD1LNdAsm_16: Spacing = 1; return ARM::VLD1LNd16;
6641   case ARM::VLD1LNdAsm_32: Spacing = 1; return ARM::VLD1LNd32;
6642 
6643   // VLD2LN
6644   case ARM::VLD2LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD2LNd8_UPD;
6645   case ARM::VLD2LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD2LNd16_UPD;
6646   case ARM::VLD2LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD2LNd32_UPD;
6647   case ARM::VLD2LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD2LNq16_UPD;
6648   case ARM::VLD2LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD2LNq32_UPD;
6649   case ARM::VLD2LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD2LNd8_UPD;
6650   case ARM::VLD2LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD2LNd16_UPD;
6651   case ARM::VLD2LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD2LNd32_UPD;
6652   case ARM::VLD2LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD2LNq16_UPD;
6653   case ARM::VLD2LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD2LNq32_UPD;
6654   case ARM::VLD2LNdAsm_8:  Spacing = 1; return ARM::VLD2LNd8;
6655   case ARM::VLD2LNdAsm_16: Spacing = 1; return ARM::VLD2LNd16;
6656   case ARM::VLD2LNdAsm_32: Spacing = 1; return ARM::VLD2LNd32;
6657   case ARM::VLD2LNqAsm_16: Spacing = 2; return ARM::VLD2LNq16;
6658   case ARM::VLD2LNqAsm_32: Spacing = 2; return ARM::VLD2LNq32;
6659 
6660   // VLD3DUP
6661   case ARM::VLD3DUPdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3DUPd8_UPD;
6662   case ARM::VLD3DUPdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3DUPd16_UPD;
6663   case ARM::VLD3DUPdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3DUPd32_UPD;
6664   case ARM::VLD3DUPqWB_fixed_Asm_8: Spacing = 1; return ARM::VLD3DUPq8_UPD;
6665   case ARM::VLD3DUPqWB_fixed_Asm_16: Spacing = 2; return ARM::VLD3DUPq16_UPD;
6666   case ARM::VLD3DUPqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3DUPq32_UPD;
6667   case ARM::VLD3DUPdWB_register_Asm_8:  Spacing = 1; return ARM::VLD3DUPd8_UPD;
6668   case ARM::VLD3DUPdWB_register_Asm_16: Spacing = 1; return ARM::VLD3DUPd16_UPD;
6669   case ARM::VLD3DUPdWB_register_Asm_32: Spacing = 1; return ARM::VLD3DUPd32_UPD;
6670   case ARM::VLD3DUPqWB_register_Asm_8: Spacing = 2; return ARM::VLD3DUPq8_UPD;
6671   case ARM::VLD3DUPqWB_register_Asm_16: Spacing = 2; return ARM::VLD3DUPq16_UPD;
6672   case ARM::VLD3DUPqWB_register_Asm_32: Spacing = 2; return ARM::VLD3DUPq32_UPD;
6673   case ARM::VLD3DUPdAsm_8:  Spacing = 1; return ARM::VLD3DUPd8;
6674   case ARM::VLD3DUPdAsm_16: Spacing = 1; return ARM::VLD3DUPd16;
6675   case ARM::VLD3DUPdAsm_32: Spacing = 1; return ARM::VLD3DUPd32;
6676   case ARM::VLD3DUPqAsm_8: Spacing = 2; return ARM::VLD3DUPq8;
6677   case ARM::VLD3DUPqAsm_16: Spacing = 2; return ARM::VLD3DUPq16;
6678   case ARM::VLD3DUPqAsm_32: Spacing = 2; return ARM::VLD3DUPq32;
6679 
6680   // VLD3LN
6681   case ARM::VLD3LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3LNd8_UPD;
6682   case ARM::VLD3LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3LNd16_UPD;
6683   case ARM::VLD3LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3LNd32_UPD;
6684   case ARM::VLD3LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3LNq16_UPD;
6685   case ARM::VLD3LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3LNq32_UPD;
6686   case ARM::VLD3LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD3LNd8_UPD;
6687   case ARM::VLD3LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD3LNd16_UPD;
6688   case ARM::VLD3LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD3LNd32_UPD;
6689   case ARM::VLD3LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD3LNq16_UPD;
6690   case ARM::VLD3LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD3LNq32_UPD;
6691   case ARM::VLD3LNdAsm_8:  Spacing = 1; return ARM::VLD3LNd8;
6692   case ARM::VLD3LNdAsm_16: Spacing = 1; return ARM::VLD3LNd16;
6693   case ARM::VLD3LNdAsm_32: Spacing = 1; return ARM::VLD3LNd32;
6694   case ARM::VLD3LNqAsm_16: Spacing = 2; return ARM::VLD3LNq16;
6695   case ARM::VLD3LNqAsm_32: Spacing = 2; return ARM::VLD3LNq32;
6696 
6697   // VLD3
6698   case ARM::VLD3dWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3d8_UPD;
6699   case ARM::VLD3dWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3d16_UPD;
6700   case ARM::VLD3dWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3d32_UPD;
6701   case ARM::VLD3qWB_fixed_Asm_8:  Spacing = 2; return ARM::VLD3q8_UPD;
6702   case ARM::VLD3qWB_fixed_Asm_16: Spacing = 2; return ARM::VLD3q16_UPD;
6703   case ARM::VLD3qWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3q32_UPD;
6704   case ARM::VLD3dWB_register_Asm_8:  Spacing = 1; return ARM::VLD3d8_UPD;
6705   case ARM::VLD3dWB_register_Asm_16: Spacing = 1; return ARM::VLD3d16_UPD;
6706   case ARM::VLD3dWB_register_Asm_32: Spacing = 1; return ARM::VLD3d32_UPD;
6707   case ARM::VLD3qWB_register_Asm_8:  Spacing = 2; return ARM::VLD3q8_UPD;
6708   case ARM::VLD3qWB_register_Asm_16: Spacing = 2; return ARM::VLD3q16_UPD;
6709   case ARM::VLD3qWB_register_Asm_32: Spacing = 2; return ARM::VLD3q32_UPD;
6710   case ARM::VLD3dAsm_8:  Spacing = 1; return ARM::VLD3d8;
6711   case ARM::VLD3dAsm_16: Spacing = 1; return ARM::VLD3d16;
6712   case ARM::VLD3dAsm_32: Spacing = 1; return ARM::VLD3d32;
6713   case ARM::VLD3qAsm_8:  Spacing = 2; return ARM::VLD3q8;
6714   case ARM::VLD3qAsm_16: Spacing = 2; return ARM::VLD3q16;
6715   case ARM::VLD3qAsm_32: Spacing = 2; return ARM::VLD3q32;
6716 
6717   // VLD4LN
6718   case ARM::VLD4LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4LNd8_UPD;
6719   case ARM::VLD4LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4LNd16_UPD;
6720   case ARM::VLD4LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4LNd32_UPD;
6721   case ARM::VLD4LNqWB_fixed_Asm_16: Spacing = 2; return ARM::VLD4LNq16_UPD;
6722   case ARM::VLD4LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4LNq32_UPD;
6723   case ARM::VLD4LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD4LNd8_UPD;
6724   case ARM::VLD4LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD4LNd16_UPD;
6725   case ARM::VLD4LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD4LNd32_UPD;
6726   case ARM::VLD4LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD4LNq16_UPD;
6727   case ARM::VLD4LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD4LNq32_UPD;
6728   case ARM::VLD4LNdAsm_8:  Spacing = 1; return ARM::VLD4LNd8;
6729   case ARM::VLD4LNdAsm_16: Spacing = 1; return ARM::VLD4LNd16;
6730   case ARM::VLD4LNdAsm_32: Spacing = 1; return ARM::VLD4LNd32;
6731   case ARM::VLD4LNqAsm_16: Spacing = 2; return ARM::VLD4LNq16;
6732   case ARM::VLD4LNqAsm_32: Spacing = 2; return ARM::VLD4LNq32;
6733 
6734   // VLD4DUP
6735   case ARM::VLD4DUPdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4DUPd8_UPD;
6736   case ARM::VLD4DUPdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4DUPd16_UPD;
6737   case ARM::VLD4DUPdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4DUPd32_UPD;
6738   case ARM::VLD4DUPqWB_fixed_Asm_8: Spacing = 1; return ARM::VLD4DUPq8_UPD;
6739   case ARM::VLD4DUPqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4DUPq16_UPD;
6740   case ARM::VLD4DUPqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4DUPq32_UPD;
6741   case ARM::VLD4DUPdWB_register_Asm_8:  Spacing = 1; return ARM::VLD4DUPd8_UPD;
6742   case ARM::VLD4DUPdWB_register_Asm_16: Spacing = 1; return ARM::VLD4DUPd16_UPD;
6743   case ARM::VLD4DUPdWB_register_Asm_32: Spacing = 1; return ARM::VLD4DUPd32_UPD;
6744   case ARM::VLD4DUPqWB_register_Asm_8: Spacing = 2; return ARM::VLD4DUPq8_UPD;
6745   case ARM::VLD4DUPqWB_register_Asm_16: Spacing = 2; return ARM::VLD4DUPq16_UPD;
6746   case ARM::VLD4DUPqWB_register_Asm_32: Spacing = 2; return ARM::VLD4DUPq32_UPD;
6747   case ARM::VLD4DUPdAsm_8:  Spacing = 1; return ARM::VLD4DUPd8;
6748   case ARM::VLD4DUPdAsm_16: Spacing = 1; return ARM::VLD4DUPd16;
6749   case ARM::VLD4DUPdAsm_32: Spacing = 1; return ARM::VLD4DUPd32;
6750   case ARM::VLD4DUPqAsm_8: Spacing = 2; return ARM::VLD4DUPq8;
6751   case ARM::VLD4DUPqAsm_16: Spacing = 2; return ARM::VLD4DUPq16;
6752   case ARM::VLD4DUPqAsm_32: Spacing = 2; return ARM::VLD4DUPq32;
6753 
6754   // VLD4
6755   case ARM::VLD4dWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4d8_UPD;
6756   case ARM::VLD4dWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4d16_UPD;
6757   case ARM::VLD4dWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4d32_UPD;
6758   case ARM::VLD4qWB_fixed_Asm_8:  Spacing = 2; return ARM::VLD4q8_UPD;
6759   case ARM::VLD4qWB_fixed_Asm_16: Spacing = 2; return ARM::VLD4q16_UPD;
6760   case ARM::VLD4qWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4q32_UPD;
6761   case ARM::VLD4dWB_register_Asm_8:  Spacing = 1; return ARM::VLD4d8_UPD;
6762   case ARM::VLD4dWB_register_Asm_16: Spacing = 1; return ARM::VLD4d16_UPD;
6763   case ARM::VLD4dWB_register_Asm_32: Spacing = 1; return ARM::VLD4d32_UPD;
6764   case ARM::VLD4qWB_register_Asm_8:  Spacing = 2; return ARM::VLD4q8_UPD;
6765   case ARM::VLD4qWB_register_Asm_16: Spacing = 2; return ARM::VLD4q16_UPD;
6766   case ARM::VLD4qWB_register_Asm_32: Spacing = 2; return ARM::VLD4q32_UPD;
6767   case ARM::VLD4dAsm_8:  Spacing = 1; return ARM::VLD4d8;
6768   case ARM::VLD4dAsm_16: Spacing = 1; return ARM::VLD4d16;
6769   case ARM::VLD4dAsm_32: Spacing = 1; return ARM::VLD4d32;
6770   case ARM::VLD4qAsm_8:  Spacing = 2; return ARM::VLD4q8;
6771   case ARM::VLD4qAsm_16: Spacing = 2; return ARM::VLD4q16;
6772   case ARM::VLD4qAsm_32: Spacing = 2; return ARM::VLD4q32;
6773   }
6774 }
6775 
6776 bool ARMAsmParser::processInstruction(MCInst &Inst,
6777                                       const OperandVector &Operands,
6778                                       MCStreamer &Out) {
6779   switch (Inst.getOpcode()) {
6780   // Alias for alternate form of 'ldr{,b}t Rt, [Rn], #imm' instruction.
6781   case ARM::LDRT_POST:
6782   case ARM::LDRBT_POST: {
6783     const unsigned Opcode =
6784       (Inst.getOpcode() == ARM::LDRT_POST) ? ARM::LDRT_POST_IMM
6785                                            : ARM::LDRBT_POST_IMM;
6786     MCInst TmpInst;
6787     TmpInst.setOpcode(Opcode);
6788     TmpInst.addOperand(Inst.getOperand(0));
6789     TmpInst.addOperand(Inst.getOperand(1));
6790     TmpInst.addOperand(Inst.getOperand(1));
6791     TmpInst.addOperand(MCOperand::createReg(0));
6792     TmpInst.addOperand(MCOperand::createImm(0));
6793     TmpInst.addOperand(Inst.getOperand(2));
6794     TmpInst.addOperand(Inst.getOperand(3));
6795     Inst = TmpInst;
6796     return true;
6797   }
6798   // Alias for alternate form of 'str{,b}t Rt, [Rn], #imm' instruction.
6799   case ARM::STRT_POST:
6800   case ARM::STRBT_POST: {
6801     const unsigned Opcode =
6802       (Inst.getOpcode() == ARM::STRT_POST) ? ARM::STRT_POST_IMM
6803                                            : ARM::STRBT_POST_IMM;
6804     MCInst TmpInst;
6805     TmpInst.setOpcode(Opcode);
6806     TmpInst.addOperand(Inst.getOperand(1));
6807     TmpInst.addOperand(Inst.getOperand(0));
6808     TmpInst.addOperand(Inst.getOperand(1));
6809     TmpInst.addOperand(MCOperand::createReg(0));
6810     TmpInst.addOperand(MCOperand::createImm(0));
6811     TmpInst.addOperand(Inst.getOperand(2));
6812     TmpInst.addOperand(Inst.getOperand(3));
6813     Inst = TmpInst;
6814     return true;
6815   }
6816   // Alias for alternate form of 'ADR Rd, #imm' instruction.
6817   case ARM::ADDri: {
6818     if (Inst.getOperand(1).getReg() != ARM::PC ||
6819         Inst.getOperand(5).getReg() != 0 ||
6820         !(Inst.getOperand(2).isExpr() || Inst.getOperand(2).isImm()))
6821       return false;
6822     MCInst TmpInst;
6823     TmpInst.setOpcode(ARM::ADR);
6824     TmpInst.addOperand(Inst.getOperand(0));
6825     if (Inst.getOperand(2).isImm()) {
6826       // Immediate (mod_imm) will be in its encoded form, we must unencode it
6827       // before passing it to the ADR instruction.
6828       unsigned Enc = Inst.getOperand(2).getImm();
6829       TmpInst.addOperand(MCOperand::createImm(
6830         ARM_AM::rotr32(Enc & 0xFF, (Enc & 0xF00) >> 7)));
6831     } else {
6832       // Turn PC-relative expression into absolute expression.
6833       // Reading PC provides the start of the current instruction + 8 and
6834       // the transform to adr is biased by that.
6835       MCSymbol *Dot = getContext().createTempSymbol();
6836       Out.EmitLabel(Dot);
6837       const MCExpr *OpExpr = Inst.getOperand(2).getExpr();
6838       const MCExpr *InstPC = MCSymbolRefExpr::create(Dot,
6839                                                      MCSymbolRefExpr::VK_None,
6840                                                      getContext());
6841       const MCExpr *Const8 = MCConstantExpr::create(8, getContext());
6842       const MCExpr *ReadPC = MCBinaryExpr::createAdd(InstPC, Const8,
6843                                                      getContext());
6844       const MCExpr *FixupAddr = MCBinaryExpr::createAdd(ReadPC, OpExpr,
6845                                                         getContext());
6846       TmpInst.addOperand(MCOperand::createExpr(FixupAddr));
6847     }
6848     TmpInst.addOperand(Inst.getOperand(3));
6849     TmpInst.addOperand(Inst.getOperand(4));
6850     Inst = TmpInst;
6851     return true;
6852   }
6853   // Aliases for alternate PC+imm syntax of LDR instructions.
6854   case ARM::t2LDRpcrel:
6855     // Select the narrow version if the immediate will fit.
6856     if (Inst.getOperand(1).getImm() > 0 &&
6857         Inst.getOperand(1).getImm() <= 0xff &&
6858         !(static_cast<ARMOperand &>(*Operands[2]).isToken() &&
6859           static_cast<ARMOperand &>(*Operands[2]).getToken() == ".w"))
6860       Inst.setOpcode(ARM::tLDRpci);
6861     else
6862       Inst.setOpcode(ARM::t2LDRpci);
6863     return true;
6864   case ARM::t2LDRBpcrel:
6865     Inst.setOpcode(ARM::t2LDRBpci);
6866     return true;
6867   case ARM::t2LDRHpcrel:
6868     Inst.setOpcode(ARM::t2LDRHpci);
6869     return true;
6870   case ARM::t2LDRSBpcrel:
6871     Inst.setOpcode(ARM::t2LDRSBpci);
6872     return true;
6873   case ARM::t2LDRSHpcrel:
6874     Inst.setOpcode(ARM::t2LDRSHpci);
6875     return true;
6876   case ARM::LDRConstPool:
6877   case ARM::tLDRConstPool:
6878   case ARM::t2LDRConstPool: {
6879     // Pseudo instruction ldr rt, =immediate is converted to a
6880     // MOV rt, immediate if immediate is known and representable
6881     // otherwise we create a constant pool entry that we load from.
6882     MCInst TmpInst;
6883     if (Inst.getOpcode() == ARM::LDRConstPool)
6884       TmpInst.setOpcode(ARM::LDRi12);
6885     else if (Inst.getOpcode() == ARM::tLDRConstPool)
6886       TmpInst.setOpcode(ARM::tLDRpci);
6887     else if (Inst.getOpcode() == ARM::t2LDRConstPool)
6888       TmpInst.setOpcode(ARM::t2LDRpci);
6889     const ARMOperand &PoolOperand =
6890       static_cast<ARMOperand &>(*Operands[3]);
6891     const MCExpr *SubExprVal = PoolOperand.getConstantPoolImm();
6892     // If SubExprVal is a constant we may be able to use a MOV
6893     if (isa<MCConstantExpr>(SubExprVal) &&
6894         Inst.getOperand(0).getReg() != ARM::PC &&
6895         Inst.getOperand(0).getReg() != ARM::SP) {
6896       int64_t Value =
6897         (int64_t) (cast<MCConstantExpr>(SubExprVal))->getValue();
6898       bool UseMov  = true;
6899       bool MovHasS = true;
6900       if (Inst.getOpcode() == ARM::LDRConstPool) {
6901         // ARM Constant
6902         if (ARM_AM::getSOImmVal(Value) != -1) {
6903           Value = ARM_AM::getSOImmVal(Value);
6904           TmpInst.setOpcode(ARM::MOVi);
6905         }
6906         else if (ARM_AM::getSOImmVal(~Value) != -1) {
6907           Value = ARM_AM::getSOImmVal(~Value);
6908           TmpInst.setOpcode(ARM::MVNi);
6909         }
6910         else if (hasV6T2Ops() &&
6911                  Value >=0 && Value < 65536) {
6912           TmpInst.setOpcode(ARM::MOVi16);
6913           MovHasS = false;
6914         }
6915         else
6916           UseMov = false;
6917       }
6918       else {
6919         // Thumb/Thumb2 Constant
6920         if (hasThumb2() &&
6921             ARM_AM::getT2SOImmVal(Value) != -1)
6922           TmpInst.setOpcode(ARM::t2MOVi);
6923         else if (hasThumb2() &&
6924                  ARM_AM::getT2SOImmVal(~Value) != -1) {
6925           TmpInst.setOpcode(ARM::t2MVNi);
6926           Value = ~Value;
6927         }
6928         else if (hasV8MBaseline() &&
6929                  Value >=0 && Value < 65536) {
6930           TmpInst.setOpcode(ARM::t2MOVi16);
6931           MovHasS = false;
6932         }
6933         else
6934           UseMov = false;
6935       }
6936       if (UseMov) {
6937         TmpInst.addOperand(Inst.getOperand(0));           // Rt
6938         TmpInst.addOperand(MCOperand::createImm(Value));  // Immediate
6939         TmpInst.addOperand(Inst.getOperand(2));           // CondCode
6940         TmpInst.addOperand(Inst.getOperand(3));           // CondCode
6941         if (MovHasS)
6942           TmpInst.addOperand(MCOperand::createReg(0));    // S
6943         Inst = TmpInst;
6944         return true;
6945       }
6946     }
6947     // No opportunity to use MOV/MVN create constant pool
6948     const MCExpr *CPLoc =
6949       getTargetStreamer().addConstantPoolEntry(SubExprVal,
6950                                                PoolOperand.getStartLoc());
6951     TmpInst.addOperand(Inst.getOperand(0));           // Rt
6952     TmpInst.addOperand(MCOperand::createExpr(CPLoc)); // offset to constpool
6953     if (TmpInst.getOpcode() == ARM::LDRi12)
6954       TmpInst.addOperand(MCOperand::createImm(0));    // unused offset
6955     TmpInst.addOperand(Inst.getOperand(2));           // CondCode
6956     TmpInst.addOperand(Inst.getOperand(3));           // CondCode
6957     Inst = TmpInst;
6958     return true;
6959   }
6960   // Handle NEON VST complex aliases.
6961   case ARM::VST1LNdWB_register_Asm_8:
6962   case ARM::VST1LNdWB_register_Asm_16:
6963   case ARM::VST1LNdWB_register_Asm_32: {
6964     MCInst TmpInst;
6965     // Shuffle the operands around so the lane index operand is in the
6966     // right place.
6967     unsigned Spacing;
6968     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
6969     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
6970     TmpInst.addOperand(Inst.getOperand(2)); // Rn
6971     TmpInst.addOperand(Inst.getOperand(3)); // alignment
6972     TmpInst.addOperand(Inst.getOperand(4)); // Rm
6973     TmpInst.addOperand(Inst.getOperand(0)); // Vd
6974     TmpInst.addOperand(Inst.getOperand(1)); // lane
6975     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
6976     TmpInst.addOperand(Inst.getOperand(6));
6977     Inst = TmpInst;
6978     return true;
6979   }
6980 
6981   case ARM::VST2LNdWB_register_Asm_8:
6982   case ARM::VST2LNdWB_register_Asm_16:
6983   case ARM::VST2LNdWB_register_Asm_32:
6984   case ARM::VST2LNqWB_register_Asm_16:
6985   case ARM::VST2LNqWB_register_Asm_32: {
6986     MCInst TmpInst;
6987     // Shuffle the operands around so the lane index operand is in the
6988     // right place.
6989     unsigned Spacing;
6990     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
6991     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
6992     TmpInst.addOperand(Inst.getOperand(2)); // Rn
6993     TmpInst.addOperand(Inst.getOperand(3)); // alignment
6994     TmpInst.addOperand(Inst.getOperand(4)); // Rm
6995     TmpInst.addOperand(Inst.getOperand(0)); // Vd
6996     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6997                                             Spacing));
6998     TmpInst.addOperand(Inst.getOperand(1)); // lane
6999     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7000     TmpInst.addOperand(Inst.getOperand(6));
7001     Inst = TmpInst;
7002     return true;
7003   }
7004 
7005   case ARM::VST3LNdWB_register_Asm_8:
7006   case ARM::VST3LNdWB_register_Asm_16:
7007   case ARM::VST3LNdWB_register_Asm_32:
7008   case ARM::VST3LNqWB_register_Asm_16:
7009   case ARM::VST3LNqWB_register_Asm_32: {
7010     MCInst TmpInst;
7011     // Shuffle the operands around so the lane index operand is in the
7012     // right place.
7013     unsigned Spacing;
7014     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7015     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7016     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7017     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7018     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7019     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7020     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7021                                             Spacing));
7022     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7023                                             Spacing * 2));
7024     TmpInst.addOperand(Inst.getOperand(1)); // lane
7025     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7026     TmpInst.addOperand(Inst.getOperand(6));
7027     Inst = TmpInst;
7028     return true;
7029   }
7030 
7031   case ARM::VST4LNdWB_register_Asm_8:
7032   case ARM::VST4LNdWB_register_Asm_16:
7033   case ARM::VST4LNdWB_register_Asm_32:
7034   case ARM::VST4LNqWB_register_Asm_16:
7035   case ARM::VST4LNqWB_register_Asm_32: {
7036     MCInst TmpInst;
7037     // Shuffle the operands around so the lane index operand is in the
7038     // right place.
7039     unsigned Spacing;
7040     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7041     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7042     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7043     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7044     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7045     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7046     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7047                                             Spacing));
7048     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7049                                             Spacing * 2));
7050     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7051                                             Spacing * 3));
7052     TmpInst.addOperand(Inst.getOperand(1)); // lane
7053     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7054     TmpInst.addOperand(Inst.getOperand(6));
7055     Inst = TmpInst;
7056     return true;
7057   }
7058 
7059   case ARM::VST1LNdWB_fixed_Asm_8:
7060   case ARM::VST1LNdWB_fixed_Asm_16:
7061   case ARM::VST1LNdWB_fixed_Asm_32: {
7062     MCInst TmpInst;
7063     // Shuffle the operands around so the lane index operand is in the
7064     // right place.
7065     unsigned Spacing;
7066     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7067     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7068     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7069     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7070     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7071     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7072     TmpInst.addOperand(Inst.getOperand(1)); // lane
7073     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7074     TmpInst.addOperand(Inst.getOperand(5));
7075     Inst = TmpInst;
7076     return true;
7077   }
7078 
7079   case ARM::VST2LNdWB_fixed_Asm_8:
7080   case ARM::VST2LNdWB_fixed_Asm_16:
7081   case ARM::VST2LNdWB_fixed_Asm_32:
7082   case ARM::VST2LNqWB_fixed_Asm_16:
7083   case ARM::VST2LNqWB_fixed_Asm_32: {
7084     MCInst TmpInst;
7085     // Shuffle the operands around so the lane index operand is in the
7086     // right place.
7087     unsigned Spacing;
7088     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7089     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7090     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7091     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7092     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7093     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7094     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7095                                             Spacing));
7096     TmpInst.addOperand(Inst.getOperand(1)); // lane
7097     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7098     TmpInst.addOperand(Inst.getOperand(5));
7099     Inst = TmpInst;
7100     return true;
7101   }
7102 
7103   case ARM::VST3LNdWB_fixed_Asm_8:
7104   case ARM::VST3LNdWB_fixed_Asm_16:
7105   case ARM::VST3LNdWB_fixed_Asm_32:
7106   case ARM::VST3LNqWB_fixed_Asm_16:
7107   case ARM::VST3LNqWB_fixed_Asm_32: {
7108     MCInst TmpInst;
7109     // Shuffle the operands around so the lane index operand is in the
7110     // right place.
7111     unsigned Spacing;
7112     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7113     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7114     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7115     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7116     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7117     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7118     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7119                                             Spacing));
7120     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7121                                             Spacing * 2));
7122     TmpInst.addOperand(Inst.getOperand(1)); // lane
7123     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7124     TmpInst.addOperand(Inst.getOperand(5));
7125     Inst = TmpInst;
7126     return true;
7127   }
7128 
7129   case ARM::VST4LNdWB_fixed_Asm_8:
7130   case ARM::VST4LNdWB_fixed_Asm_16:
7131   case ARM::VST4LNdWB_fixed_Asm_32:
7132   case ARM::VST4LNqWB_fixed_Asm_16:
7133   case ARM::VST4LNqWB_fixed_Asm_32: {
7134     MCInst TmpInst;
7135     // Shuffle the operands around so the lane index operand is in the
7136     // right place.
7137     unsigned Spacing;
7138     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7139     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7140     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7141     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7142     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7143     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7144     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7145                                             Spacing));
7146     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7147                                             Spacing * 2));
7148     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7149                                             Spacing * 3));
7150     TmpInst.addOperand(Inst.getOperand(1)); // lane
7151     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7152     TmpInst.addOperand(Inst.getOperand(5));
7153     Inst = TmpInst;
7154     return true;
7155   }
7156 
7157   case ARM::VST1LNdAsm_8:
7158   case ARM::VST1LNdAsm_16:
7159   case ARM::VST1LNdAsm_32: {
7160     MCInst TmpInst;
7161     // Shuffle the operands around so the lane index operand is in the
7162     // right place.
7163     unsigned Spacing;
7164     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7165     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7166     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7167     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7168     TmpInst.addOperand(Inst.getOperand(1)); // lane
7169     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7170     TmpInst.addOperand(Inst.getOperand(5));
7171     Inst = TmpInst;
7172     return true;
7173   }
7174 
7175   case ARM::VST2LNdAsm_8:
7176   case ARM::VST2LNdAsm_16:
7177   case ARM::VST2LNdAsm_32:
7178   case ARM::VST2LNqAsm_16:
7179   case ARM::VST2LNqAsm_32: {
7180     MCInst TmpInst;
7181     // Shuffle the operands around so the lane index operand is in the
7182     // right place.
7183     unsigned Spacing;
7184     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7185     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7186     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7187     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7188     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7189                                             Spacing));
7190     TmpInst.addOperand(Inst.getOperand(1)); // lane
7191     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7192     TmpInst.addOperand(Inst.getOperand(5));
7193     Inst = TmpInst;
7194     return true;
7195   }
7196 
7197   case ARM::VST3LNdAsm_8:
7198   case ARM::VST3LNdAsm_16:
7199   case ARM::VST3LNdAsm_32:
7200   case ARM::VST3LNqAsm_16:
7201   case ARM::VST3LNqAsm_32: {
7202     MCInst TmpInst;
7203     // Shuffle the operands around so the lane index operand is in the
7204     // right place.
7205     unsigned Spacing;
7206     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7207     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7208     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7209     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7210     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7211                                             Spacing));
7212     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7213                                             Spacing * 2));
7214     TmpInst.addOperand(Inst.getOperand(1)); // lane
7215     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7216     TmpInst.addOperand(Inst.getOperand(5));
7217     Inst = TmpInst;
7218     return true;
7219   }
7220 
7221   case ARM::VST4LNdAsm_8:
7222   case ARM::VST4LNdAsm_16:
7223   case ARM::VST4LNdAsm_32:
7224   case ARM::VST4LNqAsm_16:
7225   case ARM::VST4LNqAsm_32: {
7226     MCInst TmpInst;
7227     // Shuffle the operands around so the lane index operand is in the
7228     // right place.
7229     unsigned Spacing;
7230     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7231     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7232     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7233     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7234     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7235                                             Spacing));
7236     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7237                                             Spacing * 2));
7238     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7239                                             Spacing * 3));
7240     TmpInst.addOperand(Inst.getOperand(1)); // lane
7241     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7242     TmpInst.addOperand(Inst.getOperand(5));
7243     Inst = TmpInst;
7244     return true;
7245   }
7246 
7247   // Handle NEON VLD complex aliases.
7248   case ARM::VLD1LNdWB_register_Asm_8:
7249   case ARM::VLD1LNdWB_register_Asm_16:
7250   case ARM::VLD1LNdWB_register_Asm_32: {
7251     MCInst TmpInst;
7252     // Shuffle the operands around so the lane index operand is in the
7253     // right place.
7254     unsigned Spacing;
7255     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7256     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7257     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7258     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7259     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7260     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7261     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7262     TmpInst.addOperand(Inst.getOperand(1)); // lane
7263     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7264     TmpInst.addOperand(Inst.getOperand(6));
7265     Inst = TmpInst;
7266     return true;
7267   }
7268 
7269   case ARM::VLD2LNdWB_register_Asm_8:
7270   case ARM::VLD2LNdWB_register_Asm_16:
7271   case ARM::VLD2LNdWB_register_Asm_32:
7272   case ARM::VLD2LNqWB_register_Asm_16:
7273   case ARM::VLD2LNqWB_register_Asm_32: {
7274     MCInst TmpInst;
7275     // Shuffle the operands around so the lane index operand is in the
7276     // right place.
7277     unsigned Spacing;
7278     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7279     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7280     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7281                                             Spacing));
7282     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7283     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7284     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7285     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7286     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7287     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7288                                             Spacing));
7289     TmpInst.addOperand(Inst.getOperand(1)); // lane
7290     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7291     TmpInst.addOperand(Inst.getOperand(6));
7292     Inst = TmpInst;
7293     return true;
7294   }
7295 
7296   case ARM::VLD3LNdWB_register_Asm_8:
7297   case ARM::VLD3LNdWB_register_Asm_16:
7298   case ARM::VLD3LNdWB_register_Asm_32:
7299   case ARM::VLD3LNqWB_register_Asm_16:
7300   case ARM::VLD3LNqWB_register_Asm_32: {
7301     MCInst TmpInst;
7302     // Shuffle the operands around so the lane index operand is in the
7303     // right place.
7304     unsigned Spacing;
7305     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7306     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7307     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7308                                             Spacing));
7309     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7310                                             Spacing * 2));
7311     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7312     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7313     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7314     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7315     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7316     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7317                                             Spacing));
7318     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7319                                             Spacing * 2));
7320     TmpInst.addOperand(Inst.getOperand(1)); // lane
7321     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7322     TmpInst.addOperand(Inst.getOperand(6));
7323     Inst = TmpInst;
7324     return true;
7325   }
7326 
7327   case ARM::VLD4LNdWB_register_Asm_8:
7328   case ARM::VLD4LNdWB_register_Asm_16:
7329   case ARM::VLD4LNdWB_register_Asm_32:
7330   case ARM::VLD4LNqWB_register_Asm_16:
7331   case ARM::VLD4LNqWB_register_Asm_32: {
7332     MCInst TmpInst;
7333     // Shuffle the operands around so the lane index operand is in the
7334     // right place.
7335     unsigned Spacing;
7336     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7337     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7338     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7339                                             Spacing));
7340     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7341                                             Spacing * 2));
7342     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7343                                             Spacing * 3));
7344     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7345     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7346     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7347     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7348     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7349     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7350                                             Spacing));
7351     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7352                                             Spacing * 2));
7353     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7354                                             Spacing * 3));
7355     TmpInst.addOperand(Inst.getOperand(1)); // lane
7356     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7357     TmpInst.addOperand(Inst.getOperand(6));
7358     Inst = TmpInst;
7359     return true;
7360   }
7361 
7362   case ARM::VLD1LNdWB_fixed_Asm_8:
7363   case ARM::VLD1LNdWB_fixed_Asm_16:
7364   case ARM::VLD1LNdWB_fixed_Asm_32: {
7365     MCInst TmpInst;
7366     // Shuffle the operands around so the lane index operand is in the
7367     // right place.
7368     unsigned Spacing;
7369     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7370     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7371     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7372     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7373     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7374     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7375     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7376     TmpInst.addOperand(Inst.getOperand(1)); // lane
7377     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7378     TmpInst.addOperand(Inst.getOperand(5));
7379     Inst = TmpInst;
7380     return true;
7381   }
7382 
7383   case ARM::VLD2LNdWB_fixed_Asm_8:
7384   case ARM::VLD2LNdWB_fixed_Asm_16:
7385   case ARM::VLD2LNdWB_fixed_Asm_32:
7386   case ARM::VLD2LNqWB_fixed_Asm_16:
7387   case ARM::VLD2LNqWB_fixed_Asm_32: {
7388     MCInst TmpInst;
7389     // Shuffle the operands around so the lane index operand is in the
7390     // right place.
7391     unsigned Spacing;
7392     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7393     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7394     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7395                                             Spacing));
7396     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7397     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7398     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7399     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7400     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7401     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7402                                             Spacing));
7403     TmpInst.addOperand(Inst.getOperand(1)); // lane
7404     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7405     TmpInst.addOperand(Inst.getOperand(5));
7406     Inst = TmpInst;
7407     return true;
7408   }
7409 
7410   case ARM::VLD3LNdWB_fixed_Asm_8:
7411   case ARM::VLD3LNdWB_fixed_Asm_16:
7412   case ARM::VLD3LNdWB_fixed_Asm_32:
7413   case ARM::VLD3LNqWB_fixed_Asm_16:
7414   case ARM::VLD3LNqWB_fixed_Asm_32: {
7415     MCInst TmpInst;
7416     // Shuffle the operands around so the lane index operand is in the
7417     // right place.
7418     unsigned Spacing;
7419     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7420     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7421     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7422                                             Spacing));
7423     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7424                                             Spacing * 2));
7425     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7426     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7427     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7428     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7429     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7430     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7431                                             Spacing));
7432     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7433                                             Spacing * 2));
7434     TmpInst.addOperand(Inst.getOperand(1)); // lane
7435     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7436     TmpInst.addOperand(Inst.getOperand(5));
7437     Inst = TmpInst;
7438     return true;
7439   }
7440 
7441   case ARM::VLD4LNdWB_fixed_Asm_8:
7442   case ARM::VLD4LNdWB_fixed_Asm_16:
7443   case ARM::VLD4LNdWB_fixed_Asm_32:
7444   case ARM::VLD4LNqWB_fixed_Asm_16:
7445   case ARM::VLD4LNqWB_fixed_Asm_32: {
7446     MCInst TmpInst;
7447     // Shuffle the operands around so the lane index operand is in the
7448     // right place.
7449     unsigned Spacing;
7450     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7451     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7452     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7453                                             Spacing));
7454     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7455                                             Spacing * 2));
7456     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7457                                             Spacing * 3));
7458     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7459     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7460     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7461     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7462     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7463     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7464                                             Spacing));
7465     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7466                                             Spacing * 2));
7467     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7468                                             Spacing * 3));
7469     TmpInst.addOperand(Inst.getOperand(1)); // lane
7470     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7471     TmpInst.addOperand(Inst.getOperand(5));
7472     Inst = TmpInst;
7473     return true;
7474   }
7475 
7476   case ARM::VLD1LNdAsm_8:
7477   case ARM::VLD1LNdAsm_16:
7478   case ARM::VLD1LNdAsm_32: {
7479     MCInst TmpInst;
7480     // Shuffle the operands around so the lane index operand is in the
7481     // right place.
7482     unsigned Spacing;
7483     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7484     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7485     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7486     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7487     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7488     TmpInst.addOperand(Inst.getOperand(1)); // lane
7489     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7490     TmpInst.addOperand(Inst.getOperand(5));
7491     Inst = TmpInst;
7492     return true;
7493   }
7494 
7495   case ARM::VLD2LNdAsm_8:
7496   case ARM::VLD2LNdAsm_16:
7497   case ARM::VLD2LNdAsm_32:
7498   case ARM::VLD2LNqAsm_16:
7499   case ARM::VLD2LNqAsm_32: {
7500     MCInst TmpInst;
7501     // Shuffle the operands around so the lane index operand is in the
7502     // right place.
7503     unsigned Spacing;
7504     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7505     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7506     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7507                                             Spacing));
7508     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7509     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7510     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7511     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7512                                             Spacing));
7513     TmpInst.addOperand(Inst.getOperand(1)); // lane
7514     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7515     TmpInst.addOperand(Inst.getOperand(5));
7516     Inst = TmpInst;
7517     return true;
7518   }
7519 
7520   case ARM::VLD3LNdAsm_8:
7521   case ARM::VLD3LNdAsm_16:
7522   case ARM::VLD3LNdAsm_32:
7523   case ARM::VLD3LNqAsm_16:
7524   case ARM::VLD3LNqAsm_32: {
7525     MCInst TmpInst;
7526     // Shuffle the operands around so the lane index operand is in the
7527     // right place.
7528     unsigned Spacing;
7529     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7530     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7531     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7532                                             Spacing));
7533     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7534                                             Spacing * 2));
7535     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7536     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7537     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7538     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7539                                             Spacing));
7540     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7541                                             Spacing * 2));
7542     TmpInst.addOperand(Inst.getOperand(1)); // lane
7543     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7544     TmpInst.addOperand(Inst.getOperand(5));
7545     Inst = TmpInst;
7546     return true;
7547   }
7548 
7549   case ARM::VLD4LNdAsm_8:
7550   case ARM::VLD4LNdAsm_16:
7551   case ARM::VLD4LNdAsm_32:
7552   case ARM::VLD4LNqAsm_16:
7553   case ARM::VLD4LNqAsm_32: {
7554     MCInst TmpInst;
7555     // Shuffle the operands around so the lane index operand is in the
7556     // right place.
7557     unsigned Spacing;
7558     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7559     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7560     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7561                                             Spacing));
7562     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7563                                             Spacing * 2));
7564     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7565                                             Spacing * 3));
7566     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7567     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7568     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7569     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7570                                             Spacing));
7571     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7572                                             Spacing * 2));
7573     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7574                                             Spacing * 3));
7575     TmpInst.addOperand(Inst.getOperand(1)); // lane
7576     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7577     TmpInst.addOperand(Inst.getOperand(5));
7578     Inst = TmpInst;
7579     return true;
7580   }
7581 
7582   // VLD3DUP single 3-element structure to all lanes instructions.
7583   case ARM::VLD3DUPdAsm_8:
7584   case ARM::VLD3DUPdAsm_16:
7585   case ARM::VLD3DUPdAsm_32:
7586   case ARM::VLD3DUPqAsm_8:
7587   case ARM::VLD3DUPqAsm_16:
7588   case ARM::VLD3DUPqAsm_32: {
7589     MCInst TmpInst;
7590     unsigned Spacing;
7591     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7592     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7593     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7594                                             Spacing));
7595     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7596                                             Spacing * 2));
7597     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7598     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7599     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7600     TmpInst.addOperand(Inst.getOperand(4));
7601     Inst = TmpInst;
7602     return true;
7603   }
7604 
7605   case ARM::VLD3DUPdWB_fixed_Asm_8:
7606   case ARM::VLD3DUPdWB_fixed_Asm_16:
7607   case ARM::VLD3DUPdWB_fixed_Asm_32:
7608   case ARM::VLD3DUPqWB_fixed_Asm_8:
7609   case ARM::VLD3DUPqWB_fixed_Asm_16:
7610   case ARM::VLD3DUPqWB_fixed_Asm_32: {
7611     MCInst TmpInst;
7612     unsigned Spacing;
7613     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7614     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7615     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7616                                             Spacing));
7617     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7618                                             Spacing * 2));
7619     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7620     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7621     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7622     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7623     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7624     TmpInst.addOperand(Inst.getOperand(4));
7625     Inst = TmpInst;
7626     return true;
7627   }
7628 
7629   case ARM::VLD3DUPdWB_register_Asm_8:
7630   case ARM::VLD3DUPdWB_register_Asm_16:
7631   case ARM::VLD3DUPdWB_register_Asm_32:
7632   case ARM::VLD3DUPqWB_register_Asm_8:
7633   case ARM::VLD3DUPqWB_register_Asm_16:
7634   case ARM::VLD3DUPqWB_register_Asm_32: {
7635     MCInst TmpInst;
7636     unsigned Spacing;
7637     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7638     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7639     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7640                                             Spacing));
7641     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7642                                             Spacing * 2));
7643     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7644     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7645     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7646     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7647     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7648     TmpInst.addOperand(Inst.getOperand(5));
7649     Inst = TmpInst;
7650     return true;
7651   }
7652 
7653   // VLD3 multiple 3-element structure instructions.
7654   case ARM::VLD3dAsm_8:
7655   case ARM::VLD3dAsm_16:
7656   case ARM::VLD3dAsm_32:
7657   case ARM::VLD3qAsm_8:
7658   case ARM::VLD3qAsm_16:
7659   case ARM::VLD3qAsm_32: {
7660     MCInst TmpInst;
7661     unsigned Spacing;
7662     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7663     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7664     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7665                                             Spacing));
7666     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7667                                             Spacing * 2));
7668     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7669     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7670     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7671     TmpInst.addOperand(Inst.getOperand(4));
7672     Inst = TmpInst;
7673     return true;
7674   }
7675 
7676   case ARM::VLD3dWB_fixed_Asm_8:
7677   case ARM::VLD3dWB_fixed_Asm_16:
7678   case ARM::VLD3dWB_fixed_Asm_32:
7679   case ARM::VLD3qWB_fixed_Asm_8:
7680   case ARM::VLD3qWB_fixed_Asm_16:
7681   case ARM::VLD3qWB_fixed_Asm_32: {
7682     MCInst TmpInst;
7683     unsigned Spacing;
7684     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7685     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7686     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7687                                             Spacing));
7688     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7689                                             Spacing * 2));
7690     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7691     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7692     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7693     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7694     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7695     TmpInst.addOperand(Inst.getOperand(4));
7696     Inst = TmpInst;
7697     return true;
7698   }
7699 
7700   case ARM::VLD3dWB_register_Asm_8:
7701   case ARM::VLD3dWB_register_Asm_16:
7702   case ARM::VLD3dWB_register_Asm_32:
7703   case ARM::VLD3qWB_register_Asm_8:
7704   case ARM::VLD3qWB_register_Asm_16:
7705   case ARM::VLD3qWB_register_Asm_32: {
7706     MCInst TmpInst;
7707     unsigned Spacing;
7708     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7709     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7710     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7711                                             Spacing));
7712     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7713                                             Spacing * 2));
7714     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7715     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7716     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7717     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7718     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7719     TmpInst.addOperand(Inst.getOperand(5));
7720     Inst = TmpInst;
7721     return true;
7722   }
7723 
7724   // VLD4DUP single 3-element structure to all lanes instructions.
7725   case ARM::VLD4DUPdAsm_8:
7726   case ARM::VLD4DUPdAsm_16:
7727   case ARM::VLD4DUPdAsm_32:
7728   case ARM::VLD4DUPqAsm_8:
7729   case ARM::VLD4DUPqAsm_16:
7730   case ARM::VLD4DUPqAsm_32: {
7731     MCInst TmpInst;
7732     unsigned Spacing;
7733     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7734     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7735     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7736                                             Spacing));
7737     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7738                                             Spacing * 2));
7739     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7740                                             Spacing * 3));
7741     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7742     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7743     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7744     TmpInst.addOperand(Inst.getOperand(4));
7745     Inst = TmpInst;
7746     return true;
7747   }
7748 
7749   case ARM::VLD4DUPdWB_fixed_Asm_8:
7750   case ARM::VLD4DUPdWB_fixed_Asm_16:
7751   case ARM::VLD4DUPdWB_fixed_Asm_32:
7752   case ARM::VLD4DUPqWB_fixed_Asm_8:
7753   case ARM::VLD4DUPqWB_fixed_Asm_16:
7754   case ARM::VLD4DUPqWB_fixed_Asm_32: {
7755     MCInst TmpInst;
7756     unsigned Spacing;
7757     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7758     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7759     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7760                                             Spacing));
7761     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7762                                             Spacing * 2));
7763     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7764                                             Spacing * 3));
7765     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7766     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7767     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7768     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7769     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7770     TmpInst.addOperand(Inst.getOperand(4));
7771     Inst = TmpInst;
7772     return true;
7773   }
7774 
7775   case ARM::VLD4DUPdWB_register_Asm_8:
7776   case ARM::VLD4DUPdWB_register_Asm_16:
7777   case ARM::VLD4DUPdWB_register_Asm_32:
7778   case ARM::VLD4DUPqWB_register_Asm_8:
7779   case ARM::VLD4DUPqWB_register_Asm_16:
7780   case ARM::VLD4DUPqWB_register_Asm_32: {
7781     MCInst TmpInst;
7782     unsigned Spacing;
7783     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7784     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7785     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7786                                             Spacing));
7787     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7788                                             Spacing * 2));
7789     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7790                                             Spacing * 3));
7791     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7792     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7793     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7794     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7795     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7796     TmpInst.addOperand(Inst.getOperand(5));
7797     Inst = TmpInst;
7798     return true;
7799   }
7800 
7801   // VLD4 multiple 4-element structure instructions.
7802   case ARM::VLD4dAsm_8:
7803   case ARM::VLD4dAsm_16:
7804   case ARM::VLD4dAsm_32:
7805   case ARM::VLD4qAsm_8:
7806   case ARM::VLD4qAsm_16:
7807   case ARM::VLD4qAsm_32: {
7808     MCInst TmpInst;
7809     unsigned Spacing;
7810     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7811     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7812     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7813                                             Spacing));
7814     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7815                                             Spacing * 2));
7816     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7817                                             Spacing * 3));
7818     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7819     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7820     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7821     TmpInst.addOperand(Inst.getOperand(4));
7822     Inst = TmpInst;
7823     return true;
7824   }
7825 
7826   case ARM::VLD4dWB_fixed_Asm_8:
7827   case ARM::VLD4dWB_fixed_Asm_16:
7828   case ARM::VLD4dWB_fixed_Asm_32:
7829   case ARM::VLD4qWB_fixed_Asm_8:
7830   case ARM::VLD4qWB_fixed_Asm_16:
7831   case ARM::VLD4qWB_fixed_Asm_32: {
7832     MCInst TmpInst;
7833     unsigned Spacing;
7834     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7835     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7836     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7837                                             Spacing));
7838     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7839                                             Spacing * 2));
7840     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7841                                             Spacing * 3));
7842     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7843     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7844     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7845     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7846     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7847     TmpInst.addOperand(Inst.getOperand(4));
7848     Inst = TmpInst;
7849     return true;
7850   }
7851 
7852   case ARM::VLD4dWB_register_Asm_8:
7853   case ARM::VLD4dWB_register_Asm_16:
7854   case ARM::VLD4dWB_register_Asm_32:
7855   case ARM::VLD4qWB_register_Asm_8:
7856   case ARM::VLD4qWB_register_Asm_16:
7857   case ARM::VLD4qWB_register_Asm_32: {
7858     MCInst TmpInst;
7859     unsigned Spacing;
7860     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7861     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7862     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7863                                             Spacing));
7864     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7865                                             Spacing * 2));
7866     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7867                                             Spacing * 3));
7868     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7869     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7870     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7871     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7872     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7873     TmpInst.addOperand(Inst.getOperand(5));
7874     Inst = TmpInst;
7875     return true;
7876   }
7877 
7878   // VST3 multiple 3-element structure instructions.
7879   case ARM::VST3dAsm_8:
7880   case ARM::VST3dAsm_16:
7881   case ARM::VST3dAsm_32:
7882   case ARM::VST3qAsm_8:
7883   case ARM::VST3qAsm_16:
7884   case ARM::VST3qAsm_32: {
7885     MCInst TmpInst;
7886     unsigned Spacing;
7887     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7888     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7889     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7890     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7891     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7892                                             Spacing));
7893     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7894                                             Spacing * 2));
7895     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7896     TmpInst.addOperand(Inst.getOperand(4));
7897     Inst = TmpInst;
7898     return true;
7899   }
7900 
7901   case ARM::VST3dWB_fixed_Asm_8:
7902   case ARM::VST3dWB_fixed_Asm_16:
7903   case ARM::VST3dWB_fixed_Asm_32:
7904   case ARM::VST3qWB_fixed_Asm_8:
7905   case ARM::VST3qWB_fixed_Asm_16:
7906   case ARM::VST3qWB_fixed_Asm_32: {
7907     MCInst TmpInst;
7908     unsigned Spacing;
7909     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7910     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7911     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7912     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7913     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7914     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7915     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7916                                             Spacing));
7917     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7918                                             Spacing * 2));
7919     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7920     TmpInst.addOperand(Inst.getOperand(4));
7921     Inst = TmpInst;
7922     return true;
7923   }
7924 
7925   case ARM::VST3dWB_register_Asm_8:
7926   case ARM::VST3dWB_register_Asm_16:
7927   case ARM::VST3dWB_register_Asm_32:
7928   case ARM::VST3qWB_register_Asm_8:
7929   case ARM::VST3qWB_register_Asm_16:
7930   case ARM::VST3qWB_register_Asm_32: {
7931     MCInst TmpInst;
7932     unsigned Spacing;
7933     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7934     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7935     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7936     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7937     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7938     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7939     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7940                                             Spacing));
7941     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7942                                             Spacing * 2));
7943     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7944     TmpInst.addOperand(Inst.getOperand(5));
7945     Inst = TmpInst;
7946     return true;
7947   }
7948 
7949   // VST4 multiple 3-element structure instructions.
7950   case ARM::VST4dAsm_8:
7951   case ARM::VST4dAsm_16:
7952   case ARM::VST4dAsm_32:
7953   case ARM::VST4qAsm_8:
7954   case ARM::VST4qAsm_16:
7955   case ARM::VST4qAsm_32: {
7956     MCInst TmpInst;
7957     unsigned Spacing;
7958     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7959     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7960     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7961     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7962     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7963                                             Spacing));
7964     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7965                                             Spacing * 2));
7966     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7967                                             Spacing * 3));
7968     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7969     TmpInst.addOperand(Inst.getOperand(4));
7970     Inst = TmpInst;
7971     return true;
7972   }
7973 
7974   case ARM::VST4dWB_fixed_Asm_8:
7975   case ARM::VST4dWB_fixed_Asm_16:
7976   case ARM::VST4dWB_fixed_Asm_32:
7977   case ARM::VST4qWB_fixed_Asm_8:
7978   case ARM::VST4qWB_fixed_Asm_16:
7979   case ARM::VST4qWB_fixed_Asm_32: {
7980     MCInst TmpInst;
7981     unsigned Spacing;
7982     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7983     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7984     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7985     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7986     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7987     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7988     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7989                                             Spacing));
7990     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7991                                             Spacing * 2));
7992     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7993                                             Spacing * 3));
7994     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7995     TmpInst.addOperand(Inst.getOperand(4));
7996     Inst = TmpInst;
7997     return true;
7998   }
7999 
8000   case ARM::VST4dWB_register_Asm_8:
8001   case ARM::VST4dWB_register_Asm_16:
8002   case ARM::VST4dWB_register_Asm_32:
8003   case ARM::VST4qWB_register_Asm_8:
8004   case ARM::VST4qWB_register_Asm_16:
8005   case ARM::VST4qWB_register_Asm_32: {
8006     MCInst TmpInst;
8007     unsigned Spacing;
8008     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8009     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8010     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8011     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8012     TmpInst.addOperand(Inst.getOperand(3)); // Rm
8013     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8014     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8015                                             Spacing));
8016     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8017                                             Spacing * 2));
8018     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8019                                             Spacing * 3));
8020     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
8021     TmpInst.addOperand(Inst.getOperand(5));
8022     Inst = TmpInst;
8023     return true;
8024   }
8025 
8026   // Handle encoding choice for the shift-immediate instructions.
8027   case ARM::t2LSLri:
8028   case ARM::t2LSRri:
8029   case ARM::t2ASRri: {
8030     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8031         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
8032         Inst.getOperand(5).getReg() == (inITBlock() ? 0 : ARM::CPSR) &&
8033         !(static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8034           static_cast<ARMOperand &>(*Operands[3]).getToken() == ".w")) {
8035       unsigned NewOpc;
8036       switch (Inst.getOpcode()) {
8037       default: llvm_unreachable("unexpected opcode");
8038       case ARM::t2LSLri: NewOpc = ARM::tLSLri; break;
8039       case ARM::t2LSRri: NewOpc = ARM::tLSRri; break;
8040       case ARM::t2ASRri: NewOpc = ARM::tASRri; break;
8041       }
8042       // The Thumb1 operands aren't in the same order. Awesome, eh?
8043       MCInst TmpInst;
8044       TmpInst.setOpcode(NewOpc);
8045       TmpInst.addOperand(Inst.getOperand(0));
8046       TmpInst.addOperand(Inst.getOperand(5));
8047       TmpInst.addOperand(Inst.getOperand(1));
8048       TmpInst.addOperand(Inst.getOperand(2));
8049       TmpInst.addOperand(Inst.getOperand(3));
8050       TmpInst.addOperand(Inst.getOperand(4));
8051       Inst = TmpInst;
8052       return true;
8053     }
8054     return false;
8055   }
8056 
8057   // Handle the Thumb2 mode MOV complex aliases.
8058   case ARM::t2MOVsr:
8059   case ARM::t2MOVSsr: {
8060     // Which instruction to expand to depends on the CCOut operand and
8061     // whether we're in an IT block if the register operands are low
8062     // registers.
8063     bool isNarrow = false;
8064     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8065         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8066         isARMLowRegister(Inst.getOperand(2).getReg()) &&
8067         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
8068         inITBlock() == (Inst.getOpcode() == ARM::t2MOVsr))
8069       isNarrow = true;
8070     MCInst TmpInst;
8071     unsigned newOpc;
8072     switch(ARM_AM::getSORegShOp(Inst.getOperand(3).getImm())) {
8073     default: llvm_unreachable("unexpected opcode!");
8074     case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRrr : ARM::t2ASRrr; break;
8075     case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRrr : ARM::t2LSRrr; break;
8076     case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLrr : ARM::t2LSLrr; break;
8077     case ARM_AM::ror: newOpc = isNarrow ? ARM::tROR   : ARM::t2RORrr; break;
8078     }
8079     TmpInst.setOpcode(newOpc);
8080     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8081     if (isNarrow)
8082       TmpInst.addOperand(MCOperand::createReg(
8083           Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
8084     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8085     TmpInst.addOperand(Inst.getOperand(2)); // Rm
8086     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
8087     TmpInst.addOperand(Inst.getOperand(5));
8088     if (!isNarrow)
8089       TmpInst.addOperand(MCOperand::createReg(
8090           Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
8091     Inst = TmpInst;
8092     return true;
8093   }
8094   case ARM::t2MOVsi:
8095   case ARM::t2MOVSsi: {
8096     // Which instruction to expand to depends on the CCOut operand and
8097     // whether we're in an IT block if the register operands are low
8098     // registers.
8099     bool isNarrow = false;
8100     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8101         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8102         inITBlock() == (Inst.getOpcode() == ARM::t2MOVsi))
8103       isNarrow = true;
8104     MCInst TmpInst;
8105     unsigned newOpc;
8106     switch(ARM_AM::getSORegShOp(Inst.getOperand(2).getImm())) {
8107     default: llvm_unreachable("unexpected opcode!");
8108     case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRri : ARM::t2ASRri; break;
8109     case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRri : ARM::t2LSRri; break;
8110     case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLri : ARM::t2LSLri; break;
8111     case ARM_AM::ror: newOpc = ARM::t2RORri; isNarrow = false; break;
8112     case ARM_AM::rrx: isNarrow = false; newOpc = ARM::t2RRX; break;
8113     }
8114     unsigned Amount = ARM_AM::getSORegOffset(Inst.getOperand(2).getImm());
8115     if (Amount == 32) Amount = 0;
8116     TmpInst.setOpcode(newOpc);
8117     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8118     if (isNarrow)
8119       TmpInst.addOperand(MCOperand::createReg(
8120           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
8121     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8122     if (newOpc != ARM::t2RRX)
8123       TmpInst.addOperand(MCOperand::createImm(Amount));
8124     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8125     TmpInst.addOperand(Inst.getOperand(4));
8126     if (!isNarrow)
8127       TmpInst.addOperand(MCOperand::createReg(
8128           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
8129     Inst = TmpInst;
8130     return true;
8131   }
8132   // Handle the ARM mode MOV complex aliases.
8133   case ARM::ASRr:
8134   case ARM::LSRr:
8135   case ARM::LSLr:
8136   case ARM::RORr: {
8137     ARM_AM::ShiftOpc ShiftTy;
8138     switch(Inst.getOpcode()) {
8139     default: llvm_unreachable("unexpected opcode!");
8140     case ARM::ASRr: ShiftTy = ARM_AM::asr; break;
8141     case ARM::LSRr: ShiftTy = ARM_AM::lsr; break;
8142     case ARM::LSLr: ShiftTy = ARM_AM::lsl; break;
8143     case ARM::RORr: ShiftTy = ARM_AM::ror; break;
8144     }
8145     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, 0);
8146     MCInst TmpInst;
8147     TmpInst.setOpcode(ARM::MOVsr);
8148     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8149     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8150     TmpInst.addOperand(Inst.getOperand(2)); // Rm
8151     TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8152     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8153     TmpInst.addOperand(Inst.getOperand(4));
8154     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
8155     Inst = TmpInst;
8156     return true;
8157   }
8158   case ARM::ASRi:
8159   case ARM::LSRi:
8160   case ARM::LSLi:
8161   case ARM::RORi: {
8162     ARM_AM::ShiftOpc ShiftTy;
8163     switch(Inst.getOpcode()) {
8164     default: llvm_unreachable("unexpected opcode!");
8165     case ARM::ASRi: ShiftTy = ARM_AM::asr; break;
8166     case ARM::LSRi: ShiftTy = ARM_AM::lsr; break;
8167     case ARM::LSLi: ShiftTy = ARM_AM::lsl; break;
8168     case ARM::RORi: ShiftTy = ARM_AM::ror; break;
8169     }
8170     // A shift by zero is a plain MOVr, not a MOVsi.
8171     unsigned Amt = Inst.getOperand(2).getImm();
8172     unsigned Opc = Amt == 0 ? ARM::MOVr : ARM::MOVsi;
8173     // A shift by 32 should be encoded as 0 when permitted
8174     if (Amt == 32 && (ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr))
8175       Amt = 0;
8176     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, Amt);
8177     MCInst TmpInst;
8178     TmpInst.setOpcode(Opc);
8179     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8180     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8181     if (Opc == ARM::MOVsi)
8182       TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8183     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8184     TmpInst.addOperand(Inst.getOperand(4));
8185     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
8186     Inst = TmpInst;
8187     return true;
8188   }
8189   case ARM::RRXi: {
8190     unsigned Shifter = ARM_AM::getSORegOpc(ARM_AM::rrx, 0);
8191     MCInst TmpInst;
8192     TmpInst.setOpcode(ARM::MOVsi);
8193     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8194     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8195     TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8196     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8197     TmpInst.addOperand(Inst.getOperand(3));
8198     TmpInst.addOperand(Inst.getOperand(4)); // cc_out
8199     Inst = TmpInst;
8200     return true;
8201   }
8202   case ARM::t2LDMIA_UPD: {
8203     // If this is a load of a single register, then we should use
8204     // a post-indexed LDR instruction instead, per the ARM ARM.
8205     if (Inst.getNumOperands() != 5)
8206       return false;
8207     MCInst TmpInst;
8208     TmpInst.setOpcode(ARM::t2LDR_POST);
8209     TmpInst.addOperand(Inst.getOperand(4)); // Rt
8210     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8211     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8212     TmpInst.addOperand(MCOperand::createImm(4));
8213     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8214     TmpInst.addOperand(Inst.getOperand(3));
8215     Inst = TmpInst;
8216     return true;
8217   }
8218   case ARM::t2STMDB_UPD: {
8219     // If this is a store of a single register, then we should use
8220     // a pre-indexed STR instruction instead, per the ARM ARM.
8221     if (Inst.getNumOperands() != 5)
8222       return false;
8223     MCInst TmpInst;
8224     TmpInst.setOpcode(ARM::t2STR_PRE);
8225     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8226     TmpInst.addOperand(Inst.getOperand(4)); // Rt
8227     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8228     TmpInst.addOperand(MCOperand::createImm(-4));
8229     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8230     TmpInst.addOperand(Inst.getOperand(3));
8231     Inst = TmpInst;
8232     return true;
8233   }
8234   case ARM::LDMIA_UPD:
8235     // If this is a load of a single register via a 'pop', then we should use
8236     // a post-indexed LDR instruction instead, per the ARM ARM.
8237     if (static_cast<ARMOperand &>(*Operands[0]).getToken() == "pop" &&
8238         Inst.getNumOperands() == 5) {
8239       MCInst TmpInst;
8240       TmpInst.setOpcode(ARM::LDR_POST_IMM);
8241       TmpInst.addOperand(Inst.getOperand(4)); // Rt
8242       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8243       TmpInst.addOperand(Inst.getOperand(1)); // Rn
8244       TmpInst.addOperand(MCOperand::createReg(0));  // am2offset
8245       TmpInst.addOperand(MCOperand::createImm(4));
8246       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8247       TmpInst.addOperand(Inst.getOperand(3));
8248       Inst = TmpInst;
8249       return true;
8250     }
8251     break;
8252   case ARM::STMDB_UPD:
8253     // If this is a store of a single register via a 'push', then we should use
8254     // a pre-indexed STR instruction instead, per the ARM ARM.
8255     if (static_cast<ARMOperand &>(*Operands[0]).getToken() == "push" &&
8256         Inst.getNumOperands() == 5) {
8257       MCInst TmpInst;
8258       TmpInst.setOpcode(ARM::STR_PRE_IMM);
8259       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8260       TmpInst.addOperand(Inst.getOperand(4)); // Rt
8261       TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
8262       TmpInst.addOperand(MCOperand::createImm(-4));
8263       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8264       TmpInst.addOperand(Inst.getOperand(3));
8265       Inst = TmpInst;
8266     }
8267     break;
8268   case ARM::t2ADDri12:
8269     // If the immediate fits for encoding T3 (t2ADDri) and the generic "add"
8270     // mnemonic was used (not "addw"), encoding T3 is preferred.
8271     if (static_cast<ARMOperand &>(*Operands[0]).getToken() != "add" ||
8272         ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
8273       break;
8274     Inst.setOpcode(ARM::t2ADDri);
8275     Inst.addOperand(MCOperand::createReg(0)); // cc_out
8276     break;
8277   case ARM::t2SUBri12:
8278     // If the immediate fits for encoding T3 (t2SUBri) and the generic "sub"
8279     // mnemonic was used (not "subw"), encoding T3 is preferred.
8280     if (static_cast<ARMOperand &>(*Operands[0]).getToken() != "sub" ||
8281         ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
8282       break;
8283     Inst.setOpcode(ARM::t2SUBri);
8284     Inst.addOperand(MCOperand::createReg(0)); // cc_out
8285     break;
8286   case ARM::tADDi8:
8287     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
8288     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
8289     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
8290     // to encoding T1 if <Rd> is omitted."
8291     if ((unsigned)Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
8292       Inst.setOpcode(ARM::tADDi3);
8293       return true;
8294     }
8295     break;
8296   case ARM::tSUBi8:
8297     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
8298     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
8299     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
8300     // to encoding T1 if <Rd> is omitted."
8301     if ((unsigned)Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
8302       Inst.setOpcode(ARM::tSUBi3);
8303       return true;
8304     }
8305     break;
8306   case ARM::t2ADDri:
8307   case ARM::t2SUBri: {
8308     // If the destination and first source operand are the same, and
8309     // the flags are compatible with the current IT status, use encoding T2
8310     // instead of T3. For compatibility with the system 'as'. Make sure the
8311     // wide encoding wasn't explicit.
8312     if (Inst.getOperand(0).getReg() != Inst.getOperand(1).getReg() ||
8313         !isARMLowRegister(Inst.getOperand(0).getReg()) ||
8314         (unsigned)Inst.getOperand(2).getImm() > 255 ||
8315         ((!inITBlock() && Inst.getOperand(5).getReg() != ARM::CPSR) ||
8316          (inITBlock() && Inst.getOperand(5).getReg() != 0)) ||
8317         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8318          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".w"))
8319       break;
8320     MCInst TmpInst;
8321     TmpInst.setOpcode(Inst.getOpcode() == ARM::t2ADDri ?
8322                       ARM::tADDi8 : ARM::tSUBi8);
8323     TmpInst.addOperand(Inst.getOperand(0));
8324     TmpInst.addOperand(Inst.getOperand(5));
8325     TmpInst.addOperand(Inst.getOperand(0));
8326     TmpInst.addOperand(Inst.getOperand(2));
8327     TmpInst.addOperand(Inst.getOperand(3));
8328     TmpInst.addOperand(Inst.getOperand(4));
8329     Inst = TmpInst;
8330     return true;
8331   }
8332   case ARM::t2ADDrr: {
8333     // If the destination and first source operand are the same, and
8334     // there's no setting of the flags, use encoding T2 instead of T3.
8335     // Note that this is only for ADD, not SUB. This mirrors the system
8336     // 'as' behaviour.  Also take advantage of ADD being commutative.
8337     // Make sure the wide encoding wasn't explicit.
8338     bool Swap = false;
8339     auto DestReg = Inst.getOperand(0).getReg();
8340     bool Transform = DestReg == Inst.getOperand(1).getReg();
8341     if (!Transform && DestReg == Inst.getOperand(2).getReg()) {
8342       Transform = true;
8343       Swap = true;
8344     }
8345     if (!Transform ||
8346         Inst.getOperand(5).getReg() != 0 ||
8347         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8348          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".w"))
8349       break;
8350     MCInst TmpInst;
8351     TmpInst.setOpcode(ARM::tADDhirr);
8352     TmpInst.addOperand(Inst.getOperand(0));
8353     TmpInst.addOperand(Inst.getOperand(0));
8354     TmpInst.addOperand(Inst.getOperand(Swap ? 1 : 2));
8355     TmpInst.addOperand(Inst.getOperand(3));
8356     TmpInst.addOperand(Inst.getOperand(4));
8357     Inst = TmpInst;
8358     return true;
8359   }
8360   case ARM::tADDrSP: {
8361     // If the non-SP source operand and the destination operand are not the
8362     // same, we need to use the 32-bit encoding if it's available.
8363     if (Inst.getOperand(0).getReg() != Inst.getOperand(2).getReg()) {
8364       Inst.setOpcode(ARM::t2ADDrr);
8365       Inst.addOperand(MCOperand::createReg(0)); // cc_out
8366       return true;
8367     }
8368     break;
8369   }
8370   case ARM::tB:
8371     // A Thumb conditional branch outside of an IT block is a tBcc.
8372     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()) {
8373       Inst.setOpcode(ARM::tBcc);
8374       return true;
8375     }
8376     break;
8377   case ARM::t2B:
8378     // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
8379     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()){
8380       Inst.setOpcode(ARM::t2Bcc);
8381       return true;
8382     }
8383     break;
8384   case ARM::t2Bcc:
8385     // If the conditional is AL or we're in an IT block, we really want t2B.
8386     if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock()) {
8387       Inst.setOpcode(ARM::t2B);
8388       return true;
8389     }
8390     break;
8391   case ARM::tBcc:
8392     // If the conditional is AL, we really want tB.
8393     if (Inst.getOperand(1).getImm() == ARMCC::AL) {
8394       Inst.setOpcode(ARM::tB);
8395       return true;
8396     }
8397     break;
8398   case ARM::tLDMIA: {
8399     // If the register list contains any high registers, or if the writeback
8400     // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
8401     // instead if we're in Thumb2. Otherwise, this should have generated
8402     // an error in validateInstruction().
8403     unsigned Rn = Inst.getOperand(0).getReg();
8404     bool hasWritebackToken =
8405         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8406          static_cast<ARMOperand &>(*Operands[3]).getToken() == "!");
8407     bool listContainsBase;
8408     if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
8409         (!listContainsBase && !hasWritebackToken) ||
8410         (listContainsBase && hasWritebackToken)) {
8411       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
8412       assert (isThumbTwo());
8413       Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
8414       // If we're switching to the updating version, we need to insert
8415       // the writeback tied operand.
8416       if (hasWritebackToken)
8417         Inst.insert(Inst.begin(),
8418                     MCOperand::createReg(Inst.getOperand(0).getReg()));
8419       return true;
8420     }
8421     break;
8422   }
8423   case ARM::tSTMIA_UPD: {
8424     // If the register list contains any high registers, we need to use
8425     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
8426     // should have generated an error in validateInstruction().
8427     unsigned Rn = Inst.getOperand(0).getReg();
8428     bool listContainsBase;
8429     if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
8430       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
8431       assert (isThumbTwo());
8432       Inst.setOpcode(ARM::t2STMIA_UPD);
8433       return true;
8434     }
8435     break;
8436   }
8437   case ARM::tPOP: {
8438     bool listContainsBase;
8439     // If the register list contains any high registers, we need to use
8440     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
8441     // should have generated an error in validateInstruction().
8442     if (!checkLowRegisterList(Inst, 2, 0, ARM::PC, listContainsBase))
8443       return false;
8444     assert (isThumbTwo());
8445     Inst.setOpcode(ARM::t2LDMIA_UPD);
8446     // Add the base register and writeback operands.
8447     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8448     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8449     return true;
8450   }
8451   case ARM::tPUSH: {
8452     bool listContainsBase;
8453     if (!checkLowRegisterList(Inst, 2, 0, ARM::LR, listContainsBase))
8454       return false;
8455     assert (isThumbTwo());
8456     Inst.setOpcode(ARM::t2STMDB_UPD);
8457     // Add the base register and writeback operands.
8458     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8459     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8460     return true;
8461   }
8462   case ARM::t2MOVi: {
8463     // If we can use the 16-bit encoding and the user didn't explicitly
8464     // request the 32-bit variant, transform it here.
8465     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8466         (unsigned)Inst.getOperand(1).getImm() <= 255 &&
8467         ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
8468           Inst.getOperand(4).getReg() == ARM::CPSR) ||
8469          (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
8470         (!static_cast<ARMOperand &>(*Operands[2]).isToken() ||
8471          static_cast<ARMOperand &>(*Operands[2]).getToken() != ".w")) {
8472       // The operands aren't in the same order for tMOVi8...
8473       MCInst TmpInst;
8474       TmpInst.setOpcode(ARM::tMOVi8);
8475       TmpInst.addOperand(Inst.getOperand(0));
8476       TmpInst.addOperand(Inst.getOperand(4));
8477       TmpInst.addOperand(Inst.getOperand(1));
8478       TmpInst.addOperand(Inst.getOperand(2));
8479       TmpInst.addOperand(Inst.getOperand(3));
8480       Inst = TmpInst;
8481       return true;
8482     }
8483     break;
8484   }
8485   case ARM::t2MOVr: {
8486     // If we can use the 16-bit encoding and the user didn't explicitly
8487     // request the 32-bit variant, transform it here.
8488     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8489         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8490         Inst.getOperand(2).getImm() == ARMCC::AL &&
8491         Inst.getOperand(4).getReg() == ARM::CPSR &&
8492         (!static_cast<ARMOperand &>(*Operands[2]).isToken() ||
8493          static_cast<ARMOperand &>(*Operands[2]).getToken() != ".w")) {
8494       // The operands aren't the same for tMOV[S]r... (no cc_out)
8495       MCInst TmpInst;
8496       TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
8497       TmpInst.addOperand(Inst.getOperand(0));
8498       TmpInst.addOperand(Inst.getOperand(1));
8499       TmpInst.addOperand(Inst.getOperand(2));
8500       TmpInst.addOperand(Inst.getOperand(3));
8501       Inst = TmpInst;
8502       return true;
8503     }
8504     break;
8505   }
8506   case ARM::t2SXTH:
8507   case ARM::t2SXTB:
8508   case ARM::t2UXTH:
8509   case ARM::t2UXTB: {
8510     // If we can use the 16-bit encoding and the user didn't explicitly
8511     // request the 32-bit variant, transform it here.
8512     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8513         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8514         Inst.getOperand(2).getImm() == 0 &&
8515         (!static_cast<ARMOperand &>(*Operands[2]).isToken() ||
8516          static_cast<ARMOperand &>(*Operands[2]).getToken() != ".w")) {
8517       unsigned NewOpc;
8518       switch (Inst.getOpcode()) {
8519       default: llvm_unreachable("Illegal opcode!");
8520       case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
8521       case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
8522       case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
8523       case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
8524       }
8525       // The operands aren't the same for thumb1 (no rotate operand).
8526       MCInst TmpInst;
8527       TmpInst.setOpcode(NewOpc);
8528       TmpInst.addOperand(Inst.getOperand(0));
8529       TmpInst.addOperand(Inst.getOperand(1));
8530       TmpInst.addOperand(Inst.getOperand(3));
8531       TmpInst.addOperand(Inst.getOperand(4));
8532       Inst = TmpInst;
8533       return true;
8534     }
8535     break;
8536   }
8537   case ARM::MOVsi: {
8538     ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(2).getImm());
8539     // rrx shifts and asr/lsr of #32 is encoded as 0
8540     if (SOpc == ARM_AM::rrx || SOpc == ARM_AM::asr || SOpc == ARM_AM::lsr)
8541       return false;
8542     if (ARM_AM::getSORegOffset(Inst.getOperand(2).getImm()) == 0) {
8543       // Shifting by zero is accepted as a vanilla 'MOVr'
8544       MCInst TmpInst;
8545       TmpInst.setOpcode(ARM::MOVr);
8546       TmpInst.addOperand(Inst.getOperand(0));
8547       TmpInst.addOperand(Inst.getOperand(1));
8548       TmpInst.addOperand(Inst.getOperand(3));
8549       TmpInst.addOperand(Inst.getOperand(4));
8550       TmpInst.addOperand(Inst.getOperand(5));
8551       Inst = TmpInst;
8552       return true;
8553     }
8554     return false;
8555   }
8556   case ARM::ANDrsi:
8557   case ARM::ORRrsi:
8558   case ARM::EORrsi:
8559   case ARM::BICrsi:
8560   case ARM::SUBrsi:
8561   case ARM::ADDrsi: {
8562     unsigned newOpc;
8563     ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(3).getImm());
8564     if (SOpc == ARM_AM::rrx) return false;
8565     switch (Inst.getOpcode()) {
8566     default: llvm_unreachable("unexpected opcode!");
8567     case ARM::ANDrsi: newOpc = ARM::ANDrr; break;
8568     case ARM::ORRrsi: newOpc = ARM::ORRrr; break;
8569     case ARM::EORrsi: newOpc = ARM::EORrr; break;
8570     case ARM::BICrsi: newOpc = ARM::BICrr; break;
8571     case ARM::SUBrsi: newOpc = ARM::SUBrr; break;
8572     case ARM::ADDrsi: newOpc = ARM::ADDrr; break;
8573     }
8574     // If the shift is by zero, use the non-shifted instruction definition.
8575     // The exception is for right shifts, where 0 == 32
8576     if (ARM_AM::getSORegOffset(Inst.getOperand(3).getImm()) == 0 &&
8577         !(SOpc == ARM_AM::lsr || SOpc == ARM_AM::asr)) {
8578       MCInst TmpInst;
8579       TmpInst.setOpcode(newOpc);
8580       TmpInst.addOperand(Inst.getOperand(0));
8581       TmpInst.addOperand(Inst.getOperand(1));
8582       TmpInst.addOperand(Inst.getOperand(2));
8583       TmpInst.addOperand(Inst.getOperand(4));
8584       TmpInst.addOperand(Inst.getOperand(5));
8585       TmpInst.addOperand(Inst.getOperand(6));
8586       Inst = TmpInst;
8587       return true;
8588     }
8589     return false;
8590   }
8591   case ARM::ITasm:
8592   case ARM::t2IT: {
8593     // The mask bits for all but the first condition are represented as
8594     // the low bit of the condition code value implies 't'. We currently
8595     // always have 1 implies 't', so XOR toggle the bits if the low bit
8596     // of the condition code is zero.
8597     MCOperand &MO = Inst.getOperand(1);
8598     unsigned Mask = MO.getImm();
8599     unsigned OrigMask = Mask;
8600     unsigned TZ = countTrailingZeros(Mask);
8601     if ((Inst.getOperand(0).getImm() & 1) == 0) {
8602       assert(Mask && TZ <= 3 && "illegal IT mask value!");
8603       Mask ^= (0xE << TZ) & 0xF;
8604     }
8605     MO.setImm(Mask);
8606 
8607     // Set up the IT block state according to the IT instruction we just
8608     // matched.
8609     assert(!inITBlock() && "nested IT blocks?!");
8610     ITState.Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
8611     ITState.Mask = OrigMask; // Use the original mask, not the updated one.
8612     ITState.CurPosition = 0;
8613     ITState.FirstCond = true;
8614     break;
8615   }
8616   case ARM::t2LSLrr:
8617   case ARM::t2LSRrr:
8618   case ARM::t2ASRrr:
8619   case ARM::t2SBCrr:
8620   case ARM::t2RORrr:
8621   case ARM::t2BICrr:
8622   {
8623     // Assemblers should use the narrow encodings of these instructions when permissible.
8624     if ((isARMLowRegister(Inst.getOperand(1).getReg()) &&
8625          isARMLowRegister(Inst.getOperand(2).getReg())) &&
8626         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
8627         ((!inITBlock() && Inst.getOperand(5).getReg() == ARM::CPSR) ||
8628          (inITBlock() && Inst.getOperand(5).getReg() != ARM::CPSR)) &&
8629         (!static_cast<ARMOperand &>(*Operands[3]).isToken() ||
8630          !static_cast<ARMOperand &>(*Operands[3]).getToken().equals_lower(
8631              ".w"))) {
8632       unsigned NewOpc;
8633       switch (Inst.getOpcode()) {
8634         default: llvm_unreachable("unexpected opcode");
8635         case ARM::t2LSLrr: NewOpc = ARM::tLSLrr; break;
8636         case ARM::t2LSRrr: NewOpc = ARM::tLSRrr; break;
8637         case ARM::t2ASRrr: NewOpc = ARM::tASRrr; break;
8638         case ARM::t2SBCrr: NewOpc = ARM::tSBC; break;
8639         case ARM::t2RORrr: NewOpc = ARM::tROR; break;
8640         case ARM::t2BICrr: NewOpc = ARM::tBIC; break;
8641       }
8642       MCInst TmpInst;
8643       TmpInst.setOpcode(NewOpc);
8644       TmpInst.addOperand(Inst.getOperand(0));
8645       TmpInst.addOperand(Inst.getOperand(5));
8646       TmpInst.addOperand(Inst.getOperand(1));
8647       TmpInst.addOperand(Inst.getOperand(2));
8648       TmpInst.addOperand(Inst.getOperand(3));
8649       TmpInst.addOperand(Inst.getOperand(4));
8650       Inst = TmpInst;
8651       return true;
8652     }
8653     return false;
8654   }
8655   case ARM::t2ANDrr:
8656   case ARM::t2EORrr:
8657   case ARM::t2ADCrr:
8658   case ARM::t2ORRrr:
8659   {
8660     // Assemblers should use the narrow encodings of these instructions when permissible.
8661     // These instructions are special in that they are commutable, so shorter encodings
8662     // are available more often.
8663     if ((isARMLowRegister(Inst.getOperand(1).getReg()) &&
8664          isARMLowRegister(Inst.getOperand(2).getReg())) &&
8665         (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() ||
8666          Inst.getOperand(0).getReg() == Inst.getOperand(2).getReg()) &&
8667         ((!inITBlock() && Inst.getOperand(5).getReg() == ARM::CPSR) ||
8668          (inITBlock() && Inst.getOperand(5).getReg() != ARM::CPSR)) &&
8669         (!static_cast<ARMOperand &>(*Operands[3]).isToken() ||
8670          !static_cast<ARMOperand &>(*Operands[3]).getToken().equals_lower(
8671              ".w"))) {
8672       unsigned NewOpc;
8673       switch (Inst.getOpcode()) {
8674         default: llvm_unreachable("unexpected opcode");
8675         case ARM::t2ADCrr: NewOpc = ARM::tADC; break;
8676         case ARM::t2ANDrr: NewOpc = ARM::tAND; break;
8677         case ARM::t2EORrr: NewOpc = ARM::tEOR; break;
8678         case ARM::t2ORRrr: NewOpc = ARM::tORR; break;
8679       }
8680       MCInst TmpInst;
8681       TmpInst.setOpcode(NewOpc);
8682       TmpInst.addOperand(Inst.getOperand(0));
8683       TmpInst.addOperand(Inst.getOperand(5));
8684       if (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()) {
8685         TmpInst.addOperand(Inst.getOperand(1));
8686         TmpInst.addOperand(Inst.getOperand(2));
8687       } else {
8688         TmpInst.addOperand(Inst.getOperand(2));
8689         TmpInst.addOperand(Inst.getOperand(1));
8690       }
8691       TmpInst.addOperand(Inst.getOperand(3));
8692       TmpInst.addOperand(Inst.getOperand(4));
8693       Inst = TmpInst;
8694       return true;
8695     }
8696     return false;
8697   }
8698   }
8699   return false;
8700 }
8701 
8702 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
8703   // 16-bit thumb arithmetic instructions either require or preclude the 'S'
8704   // suffix depending on whether they're in an IT block or not.
8705   unsigned Opc = Inst.getOpcode();
8706   const MCInstrDesc &MCID = MII.get(Opc);
8707   if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
8708     assert(MCID.hasOptionalDef() &&
8709            "optionally flag setting instruction missing optional def operand");
8710     assert(MCID.NumOperands == Inst.getNumOperands() &&
8711            "operand count mismatch!");
8712     // Find the optional-def operand (cc_out).
8713     unsigned OpNo;
8714     for (OpNo = 0;
8715          !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
8716          ++OpNo)
8717       ;
8718     // If we're parsing Thumb1, reject it completely.
8719     if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
8720       return Match_MnemonicFail;
8721     // If we're parsing Thumb2, which form is legal depends on whether we're
8722     // in an IT block.
8723     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
8724         !inITBlock())
8725       return Match_RequiresITBlock;
8726     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
8727         inITBlock())
8728       return Match_RequiresNotITBlock;
8729   } else if (isThumbOne()) {
8730     // Some high-register supporting Thumb1 encodings only allow both registers
8731     // to be from r0-r7 when in Thumb2.
8732     if (Opc == ARM::tADDhirr && !hasV6MOps() &&
8733         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8734         isARMLowRegister(Inst.getOperand(2).getReg()))
8735       return Match_RequiresThumb2;
8736     // Others only require ARMv6 or later.
8737     else if (Opc == ARM::tMOVr && !hasV6Ops() &&
8738              isARMLowRegister(Inst.getOperand(0).getReg()) &&
8739              isARMLowRegister(Inst.getOperand(1).getReg()))
8740       return Match_RequiresV6;
8741   }
8742 
8743   for (unsigned I = 0; I < MCID.NumOperands; ++I)
8744     if (MCID.OpInfo[I].RegClass == ARM::rGPRRegClassID) {
8745       // rGPRRegClass excludes PC, and also excluded SP before ARMv8
8746       if ((Inst.getOperand(I).getReg() == ARM::SP) && !hasV8Ops())
8747         return Match_RequiresV8;
8748       else if (Inst.getOperand(I).getReg() == ARM::PC)
8749         return Match_InvalidOperand;
8750     }
8751 
8752   return Match_Success;
8753 }
8754 
8755 namespace llvm {
8756 template <> inline bool IsCPSRDead<MCInst>(MCInst *Instr) {
8757   return true; // In an assembly source, no need to second-guess
8758 }
8759 }
8760 
8761 static const char *getSubtargetFeatureName(uint64_t Val);
8762 bool ARMAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
8763                                            OperandVector &Operands,
8764                                            MCStreamer &Out, uint64_t &ErrorInfo,
8765                                            bool MatchingInlineAsm) {
8766   MCInst Inst;
8767   unsigned MatchResult;
8768 
8769   MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
8770                                      MatchingInlineAsm);
8771   switch (MatchResult) {
8772   case Match_Success:
8773     // Context sensitive operand constraints aren't handled by the matcher,
8774     // so check them here.
8775     if (validateInstruction(Inst, Operands)) {
8776       // Still progress the IT block, otherwise one wrong condition causes
8777       // nasty cascading errors.
8778       forwardITPosition();
8779       return true;
8780     }
8781 
8782     { // processInstruction() updates inITBlock state, we need to save it away
8783       bool wasInITBlock = inITBlock();
8784 
8785       // Some instructions need post-processing to, for example, tweak which
8786       // encoding is selected. Loop on it while changes happen so the
8787       // individual transformations can chain off each other. E.g.,
8788       // tPOP(r8)->t2LDMIA_UPD(sp,r8)->t2STR_POST(sp,r8)
8789       while (processInstruction(Inst, Operands, Out))
8790         ;
8791 
8792       // Only after the instruction is fully processed, we can validate it
8793       if (wasInITBlock && hasV8Ops() && isThumb() &&
8794           !isV8EligibleForIT(&Inst)) {
8795         Warning(IDLoc, "deprecated instruction in IT block");
8796       }
8797     }
8798 
8799     // Only move forward at the very end so that everything in validate
8800     // and process gets a consistent answer about whether we're in an IT
8801     // block.
8802     forwardITPosition();
8803 
8804     // ITasm is an ARM mode pseudo-instruction that just sets the ITblock and
8805     // doesn't actually encode.
8806     if (Inst.getOpcode() == ARM::ITasm)
8807       return false;
8808 
8809     Inst.setLoc(IDLoc);
8810     Out.EmitInstruction(Inst, getSTI());
8811     return false;
8812   case Match_MissingFeature: {
8813     assert(ErrorInfo && "Unknown missing feature!");
8814     // Special case the error message for the very common case where only
8815     // a single subtarget feature is missing (Thumb vs. ARM, e.g.).
8816     std::string Msg = "instruction requires:";
8817     uint64_t Mask = 1;
8818     for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
8819       if (ErrorInfo & Mask) {
8820         Msg += " ";
8821         Msg += getSubtargetFeatureName(ErrorInfo & Mask);
8822       }
8823       Mask <<= 1;
8824     }
8825     return Error(IDLoc, Msg);
8826   }
8827   case Match_InvalidOperand: {
8828     SMLoc ErrorLoc = IDLoc;
8829     if (ErrorInfo != ~0ULL) {
8830       if (ErrorInfo >= Operands.size())
8831         return Error(IDLoc, "too few operands for instruction");
8832 
8833       ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
8834       if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
8835     }
8836 
8837     return Error(ErrorLoc, "invalid operand for instruction");
8838   }
8839   case Match_MnemonicFail:
8840     return Error(IDLoc, "invalid instruction",
8841                  ((ARMOperand &)*Operands[0]).getLocRange());
8842   case Match_RequiresNotITBlock:
8843     return Error(IDLoc, "flag setting instruction only valid outside IT block");
8844   case Match_RequiresITBlock:
8845     return Error(IDLoc, "instruction only valid inside IT block");
8846   case Match_RequiresV6:
8847     return Error(IDLoc, "instruction variant requires ARMv6 or later");
8848   case Match_RequiresThumb2:
8849     return Error(IDLoc, "instruction variant requires Thumb2");
8850   case Match_RequiresV8:
8851     return Error(IDLoc, "instruction variant requires ARMv8 or later");
8852   case Match_ImmRange0_15: {
8853     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
8854     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
8855     return Error(ErrorLoc, "immediate operand must be in the range [0,15]");
8856   }
8857   case Match_ImmRange0_239: {
8858     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
8859     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
8860     return Error(ErrorLoc, "immediate operand must be in the range [0,239]");
8861   }
8862   case Match_AlignedMemoryRequiresNone:
8863   case Match_DupAlignedMemoryRequiresNone:
8864   case Match_AlignedMemoryRequires16:
8865   case Match_DupAlignedMemoryRequires16:
8866   case Match_AlignedMemoryRequires32:
8867   case Match_DupAlignedMemoryRequires32:
8868   case Match_AlignedMemoryRequires64:
8869   case Match_DupAlignedMemoryRequires64:
8870   case Match_AlignedMemoryRequires64or128:
8871   case Match_DupAlignedMemoryRequires64or128:
8872   case Match_AlignedMemoryRequires64or128or256:
8873   {
8874     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getAlignmentLoc();
8875     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
8876     switch (MatchResult) {
8877       default:
8878         llvm_unreachable("Missing Match_Aligned type");
8879       case Match_AlignedMemoryRequiresNone:
8880       case Match_DupAlignedMemoryRequiresNone:
8881         return Error(ErrorLoc, "alignment must be omitted");
8882       case Match_AlignedMemoryRequires16:
8883       case Match_DupAlignedMemoryRequires16:
8884         return Error(ErrorLoc, "alignment must be 16 or omitted");
8885       case Match_AlignedMemoryRequires32:
8886       case Match_DupAlignedMemoryRequires32:
8887         return Error(ErrorLoc, "alignment must be 32 or omitted");
8888       case Match_AlignedMemoryRequires64:
8889       case Match_DupAlignedMemoryRequires64:
8890         return Error(ErrorLoc, "alignment must be 64 or omitted");
8891       case Match_AlignedMemoryRequires64or128:
8892       case Match_DupAlignedMemoryRequires64or128:
8893         return Error(ErrorLoc, "alignment must be 64, 128 or omitted");
8894       case Match_AlignedMemoryRequires64or128or256:
8895         return Error(ErrorLoc, "alignment must be 64, 128, 256 or omitted");
8896     }
8897   }
8898   }
8899 
8900   llvm_unreachable("Implement any new match types added!");
8901 }
8902 
8903 /// parseDirective parses the arm specific directives
8904 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
8905   const MCObjectFileInfo::Environment Format =
8906     getContext().getObjectFileInfo()->getObjectFileType();
8907   bool IsMachO = Format == MCObjectFileInfo::IsMachO;
8908   bool IsCOFF = Format == MCObjectFileInfo::IsCOFF;
8909 
8910   StringRef IDVal = DirectiveID.getIdentifier();
8911   if (IDVal == ".word")
8912     return parseLiteralValues(4, DirectiveID.getLoc());
8913   else if (IDVal == ".short" || IDVal == ".hword")
8914     return parseLiteralValues(2, DirectiveID.getLoc());
8915   else if (IDVal == ".thumb")
8916     return parseDirectiveThumb(DirectiveID.getLoc());
8917   else if (IDVal == ".arm")
8918     return parseDirectiveARM(DirectiveID.getLoc());
8919   else if (IDVal == ".thumb_func")
8920     return parseDirectiveThumbFunc(DirectiveID.getLoc());
8921   else if (IDVal == ".code")
8922     return parseDirectiveCode(DirectiveID.getLoc());
8923   else if (IDVal == ".syntax")
8924     return parseDirectiveSyntax(DirectiveID.getLoc());
8925   else if (IDVal == ".unreq")
8926     return parseDirectiveUnreq(DirectiveID.getLoc());
8927   else if (IDVal == ".fnend")
8928     return parseDirectiveFnEnd(DirectiveID.getLoc());
8929   else if (IDVal == ".cantunwind")
8930     return parseDirectiveCantUnwind(DirectiveID.getLoc());
8931   else if (IDVal == ".personality")
8932     return parseDirectivePersonality(DirectiveID.getLoc());
8933   else if (IDVal == ".handlerdata")
8934     return parseDirectiveHandlerData(DirectiveID.getLoc());
8935   else if (IDVal == ".setfp")
8936     return parseDirectiveSetFP(DirectiveID.getLoc());
8937   else if (IDVal == ".pad")
8938     return parseDirectivePad(DirectiveID.getLoc());
8939   else if (IDVal == ".save")
8940     return parseDirectiveRegSave(DirectiveID.getLoc(), false);
8941   else if (IDVal == ".vsave")
8942     return parseDirectiveRegSave(DirectiveID.getLoc(), true);
8943   else if (IDVal == ".ltorg" || IDVal == ".pool")
8944     return parseDirectiveLtorg(DirectiveID.getLoc());
8945   else if (IDVal == ".even")
8946     return parseDirectiveEven(DirectiveID.getLoc());
8947   else if (IDVal == ".personalityindex")
8948     return parseDirectivePersonalityIndex(DirectiveID.getLoc());
8949   else if (IDVal == ".unwind_raw")
8950     return parseDirectiveUnwindRaw(DirectiveID.getLoc());
8951   else if (IDVal == ".movsp")
8952     return parseDirectiveMovSP(DirectiveID.getLoc());
8953   else if (IDVal == ".arch_extension")
8954     return parseDirectiveArchExtension(DirectiveID.getLoc());
8955   else if (IDVal == ".align")
8956     return parseDirectiveAlign(DirectiveID.getLoc());
8957   else if (IDVal == ".thumb_set")
8958     return parseDirectiveThumbSet(DirectiveID.getLoc());
8959 
8960   if (!IsMachO && !IsCOFF) {
8961     if (IDVal == ".arch")
8962       return parseDirectiveArch(DirectiveID.getLoc());
8963     else if (IDVal == ".cpu")
8964       return parseDirectiveCPU(DirectiveID.getLoc());
8965     else if (IDVal == ".eabi_attribute")
8966       return parseDirectiveEabiAttr(DirectiveID.getLoc());
8967     else if (IDVal == ".fpu")
8968       return parseDirectiveFPU(DirectiveID.getLoc());
8969     else if (IDVal == ".fnstart")
8970       return parseDirectiveFnStart(DirectiveID.getLoc());
8971     else if (IDVal == ".inst")
8972       return parseDirectiveInst(DirectiveID.getLoc());
8973     else if (IDVal == ".inst.n")
8974       return parseDirectiveInst(DirectiveID.getLoc(), 'n');
8975     else if (IDVal == ".inst.w")
8976       return parseDirectiveInst(DirectiveID.getLoc(), 'w');
8977     else if (IDVal == ".object_arch")
8978       return parseDirectiveObjectArch(DirectiveID.getLoc());
8979     else if (IDVal == ".tlsdescseq")
8980       return parseDirectiveTLSDescSeq(DirectiveID.getLoc());
8981   }
8982 
8983   return true;
8984 }
8985 
8986 /// parseLiteralValues
8987 ///  ::= .hword expression [, expression]*
8988 ///  ::= .short expression [, expression]*
8989 ///  ::= .word expression [, expression]*
8990 bool ARMAsmParser::parseLiteralValues(unsigned Size, SMLoc L) {
8991   MCAsmParser &Parser = getParser();
8992   if (getLexer().isNot(AsmToken::EndOfStatement)) {
8993     for (;;) {
8994       const MCExpr *Value;
8995       if (getParser().parseExpression(Value)) {
8996         Parser.eatToEndOfStatement();
8997         return false;
8998       }
8999 
9000       getParser().getStreamer().EmitValue(Value, Size, L);
9001 
9002       if (getLexer().is(AsmToken::EndOfStatement))
9003         break;
9004 
9005       // FIXME: Improve diagnostic.
9006       if (getLexer().isNot(AsmToken::Comma)) {
9007         Error(L, "unexpected token in directive");
9008         return false;
9009       }
9010       Parser.Lex();
9011     }
9012   }
9013 
9014   Parser.Lex();
9015   return false;
9016 }
9017 
9018 /// parseDirectiveThumb
9019 ///  ::= .thumb
9020 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
9021   MCAsmParser &Parser = getParser();
9022   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9023     Error(L, "unexpected token in directive");
9024     return false;
9025   }
9026   Parser.Lex();
9027 
9028   if (!hasThumb()) {
9029     Error(L, "target does not support Thumb mode");
9030     return false;
9031   }
9032 
9033   if (!isThumb())
9034     SwitchMode();
9035 
9036   getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
9037   return false;
9038 }
9039 
9040 /// parseDirectiveARM
9041 ///  ::= .arm
9042 bool ARMAsmParser::parseDirectiveARM(SMLoc L) {
9043   MCAsmParser &Parser = getParser();
9044   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9045     Error(L, "unexpected token in directive");
9046     return false;
9047   }
9048   Parser.Lex();
9049 
9050   if (!hasARM()) {
9051     Error(L, "target does not support ARM mode");
9052     return false;
9053   }
9054 
9055   if (isThumb())
9056     SwitchMode();
9057 
9058   getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
9059   return false;
9060 }
9061 
9062 void ARMAsmParser::onLabelParsed(MCSymbol *Symbol) {
9063   if (NextSymbolIsThumb) {
9064     getParser().getStreamer().EmitThumbFunc(Symbol);
9065     NextSymbolIsThumb = false;
9066   }
9067 }
9068 
9069 /// parseDirectiveThumbFunc
9070 ///  ::= .thumbfunc symbol_name
9071 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
9072   MCAsmParser &Parser = getParser();
9073   const auto Format = getContext().getObjectFileInfo()->getObjectFileType();
9074   bool IsMachO = Format == MCObjectFileInfo::IsMachO;
9075 
9076   // Darwin asm has (optionally) function name after .thumb_func direction
9077   // ELF doesn't
9078   if (IsMachO) {
9079     const AsmToken &Tok = Parser.getTok();
9080     if (Tok.isNot(AsmToken::EndOfStatement)) {
9081       if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String)) {
9082         Error(L, "unexpected token in .thumb_func directive");
9083         return false;
9084       }
9085 
9086       MCSymbol *Func =
9087           getParser().getContext().getOrCreateSymbol(Tok.getIdentifier());
9088       getParser().getStreamer().EmitThumbFunc(Func);
9089       Parser.Lex(); // Consume the identifier token.
9090       return false;
9091     }
9092   }
9093 
9094   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9095     Error(Parser.getTok().getLoc(), "unexpected token in directive");
9096     Parser.eatToEndOfStatement();
9097     return false;
9098   }
9099 
9100   NextSymbolIsThumb = true;
9101   return false;
9102 }
9103 
9104 /// parseDirectiveSyntax
9105 ///  ::= .syntax unified | divided
9106 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
9107   MCAsmParser &Parser = getParser();
9108   const AsmToken &Tok = Parser.getTok();
9109   if (Tok.isNot(AsmToken::Identifier)) {
9110     Error(L, "unexpected token in .syntax directive");
9111     return false;
9112   }
9113 
9114   StringRef Mode = Tok.getString();
9115   if (Mode == "unified" || Mode == "UNIFIED") {
9116     Parser.Lex();
9117   } else if (Mode == "divided" || Mode == "DIVIDED") {
9118     Error(L, "'.syntax divided' arm asssembly not supported");
9119     return false;
9120   } else {
9121     Error(L, "unrecognized syntax mode in .syntax directive");
9122     return false;
9123   }
9124 
9125   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9126     Error(Parser.getTok().getLoc(), "unexpected token in directive");
9127     return false;
9128   }
9129   Parser.Lex();
9130 
9131   // TODO tell the MC streamer the mode
9132   // getParser().getStreamer().Emit???();
9133   return false;
9134 }
9135 
9136 /// parseDirectiveCode
9137 ///  ::= .code 16 | 32
9138 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
9139   MCAsmParser &Parser = getParser();
9140   const AsmToken &Tok = Parser.getTok();
9141   if (Tok.isNot(AsmToken::Integer)) {
9142     Error(L, "unexpected token in .code directive");
9143     return false;
9144   }
9145   int64_t Val = Parser.getTok().getIntVal();
9146   if (Val != 16 && Val != 32) {
9147     Error(L, "invalid operand to .code directive");
9148     return false;
9149   }
9150   Parser.Lex();
9151 
9152   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9153     Error(Parser.getTok().getLoc(), "unexpected token in directive");
9154     return false;
9155   }
9156   Parser.Lex();
9157 
9158   if (Val == 16) {
9159     if (!hasThumb()) {
9160       Error(L, "target does not support Thumb mode");
9161       return false;
9162     }
9163 
9164     if (!isThumb())
9165       SwitchMode();
9166     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
9167   } else {
9168     if (!hasARM()) {
9169       Error(L, "target does not support ARM mode");
9170       return false;
9171     }
9172 
9173     if (isThumb())
9174       SwitchMode();
9175     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
9176   }
9177 
9178   return false;
9179 }
9180 
9181 /// parseDirectiveReq
9182 ///  ::= name .req registername
9183 bool ARMAsmParser::parseDirectiveReq(StringRef Name, SMLoc L) {
9184   MCAsmParser &Parser = getParser();
9185   Parser.Lex(); // Eat the '.req' token.
9186   unsigned Reg;
9187   SMLoc SRegLoc, ERegLoc;
9188   if (ParseRegister(Reg, SRegLoc, ERegLoc)) {
9189     Parser.eatToEndOfStatement();
9190     Error(SRegLoc, "register name expected");
9191     return false;
9192   }
9193 
9194   // Shouldn't be anything else.
9195   if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
9196     Parser.eatToEndOfStatement();
9197     Error(Parser.getTok().getLoc(), "unexpected input in .req directive.");
9198     return false;
9199   }
9200 
9201   Parser.Lex(); // Consume the EndOfStatement
9202 
9203   if (RegisterReqs.insert(std::make_pair(Name, Reg)).first->second != Reg) {
9204     Error(SRegLoc, "redefinition of '" + Name + "' does not match original.");
9205     return false;
9206   }
9207 
9208   return false;
9209 }
9210 
9211 /// parseDirectiveUneq
9212 ///  ::= .unreq registername
9213 bool ARMAsmParser::parseDirectiveUnreq(SMLoc L) {
9214   MCAsmParser &Parser = getParser();
9215   if (Parser.getTok().isNot(AsmToken::Identifier)) {
9216     Parser.eatToEndOfStatement();
9217     Error(L, "unexpected input in .unreq directive.");
9218     return false;
9219   }
9220   RegisterReqs.erase(Parser.getTok().getIdentifier().lower());
9221   Parser.Lex(); // Eat the identifier.
9222   return false;
9223 }
9224 
9225 // After changing arch/CPU, try to put the ARM/Thumb mode back to what it was
9226 // before, if supported by the new target, or emit mapping symbols for the mode
9227 // switch.
9228 void ARMAsmParser::FixModeAfterArchChange(bool WasThumb, SMLoc Loc) {
9229   if (WasThumb != isThumb()) {
9230     if (WasThumb && hasThumb()) {
9231       // Stay in Thumb mode
9232       SwitchMode();
9233     } else if (!WasThumb && hasARM()) {
9234       // Stay in ARM mode
9235       SwitchMode();
9236     } else {
9237       // Mode switch forced, because the new arch doesn't support the old mode.
9238       getParser().getStreamer().EmitAssemblerFlag(isThumb() ? MCAF_Code16
9239                                                             : MCAF_Code32);
9240       // Warn about the implcit mode switch. GAS does not switch modes here,
9241       // but instead stays in the old mode, reporting an error on any following
9242       // instructions as the mode does not exist on the target.
9243       Warning(Loc, Twine("new target does not support ") +
9244                        (WasThumb ? "thumb" : "arm") + " mode, switching to " +
9245                        (!WasThumb ? "thumb" : "arm") + " mode");
9246     }
9247   }
9248 }
9249 
9250 /// parseDirectiveArch
9251 ///  ::= .arch token
9252 bool ARMAsmParser::parseDirectiveArch(SMLoc L) {
9253   StringRef Arch = getParser().parseStringToEndOfStatement().trim();
9254 
9255   unsigned ID = ARM::parseArch(Arch);
9256 
9257   if (ID == ARM::AK_INVALID) {
9258     Error(L, "Unknown arch name");
9259     return false;
9260   }
9261 
9262   bool WasThumb = isThumb();
9263   Triple T;
9264   MCSubtargetInfo &STI = copySTI();
9265   STI.setDefaultFeatures("", ("+" + ARM::getArchName(ID)).str());
9266   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9267   FixModeAfterArchChange(WasThumb, L);
9268 
9269   getTargetStreamer().emitArch(ID);
9270   return false;
9271 }
9272 
9273 /// parseDirectiveEabiAttr
9274 ///  ::= .eabi_attribute int, int [, "str"]
9275 ///  ::= .eabi_attribute Tag_name, int [, "str"]
9276 bool ARMAsmParser::parseDirectiveEabiAttr(SMLoc L) {
9277   MCAsmParser &Parser = getParser();
9278   int64_t Tag;
9279   SMLoc TagLoc;
9280   TagLoc = Parser.getTok().getLoc();
9281   if (Parser.getTok().is(AsmToken::Identifier)) {
9282     StringRef Name = Parser.getTok().getIdentifier();
9283     Tag = ARMBuildAttrs::AttrTypeFromString(Name);
9284     if (Tag == -1) {
9285       Error(TagLoc, "attribute name not recognised: " + Name);
9286       Parser.eatToEndOfStatement();
9287       return false;
9288     }
9289     Parser.Lex();
9290   } else {
9291     const MCExpr *AttrExpr;
9292 
9293     TagLoc = Parser.getTok().getLoc();
9294     if (Parser.parseExpression(AttrExpr)) {
9295       Parser.eatToEndOfStatement();
9296       return false;
9297     }
9298 
9299     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(AttrExpr);
9300     if (!CE) {
9301       Error(TagLoc, "expected numeric constant");
9302       Parser.eatToEndOfStatement();
9303       return false;
9304     }
9305 
9306     Tag = CE->getValue();
9307   }
9308 
9309   if (Parser.getTok().isNot(AsmToken::Comma)) {
9310     Error(Parser.getTok().getLoc(), "comma expected");
9311     Parser.eatToEndOfStatement();
9312     return false;
9313   }
9314   Parser.Lex(); // skip comma
9315 
9316   StringRef StringValue = "";
9317   bool IsStringValue = false;
9318 
9319   int64_t IntegerValue = 0;
9320   bool IsIntegerValue = false;
9321 
9322   if (Tag == ARMBuildAttrs::CPU_raw_name || Tag == ARMBuildAttrs::CPU_name)
9323     IsStringValue = true;
9324   else if (Tag == ARMBuildAttrs::compatibility) {
9325     IsStringValue = true;
9326     IsIntegerValue = true;
9327   } else if (Tag < 32 || Tag % 2 == 0)
9328     IsIntegerValue = true;
9329   else if (Tag % 2 == 1)
9330     IsStringValue = true;
9331   else
9332     llvm_unreachable("invalid tag type");
9333 
9334   if (IsIntegerValue) {
9335     const MCExpr *ValueExpr;
9336     SMLoc ValueExprLoc = Parser.getTok().getLoc();
9337     if (Parser.parseExpression(ValueExpr)) {
9338       Parser.eatToEndOfStatement();
9339       return false;
9340     }
9341 
9342     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ValueExpr);
9343     if (!CE) {
9344       Error(ValueExprLoc, "expected numeric constant");
9345       Parser.eatToEndOfStatement();
9346       return false;
9347     }
9348 
9349     IntegerValue = CE->getValue();
9350   }
9351 
9352   if (Tag == ARMBuildAttrs::compatibility) {
9353     if (Parser.getTok().isNot(AsmToken::Comma))
9354       IsStringValue = false;
9355     if (Parser.getTok().isNot(AsmToken::Comma)) {
9356       Error(Parser.getTok().getLoc(), "comma expected");
9357       Parser.eatToEndOfStatement();
9358       return false;
9359     } else {
9360        Parser.Lex();
9361     }
9362   }
9363 
9364   if (IsStringValue) {
9365     if (Parser.getTok().isNot(AsmToken::String)) {
9366       Error(Parser.getTok().getLoc(), "bad string constant");
9367       Parser.eatToEndOfStatement();
9368       return false;
9369     }
9370 
9371     StringValue = Parser.getTok().getStringContents();
9372     Parser.Lex();
9373   }
9374 
9375   if (IsIntegerValue && IsStringValue) {
9376     assert(Tag == ARMBuildAttrs::compatibility);
9377     getTargetStreamer().emitIntTextAttribute(Tag, IntegerValue, StringValue);
9378   } else if (IsIntegerValue)
9379     getTargetStreamer().emitAttribute(Tag, IntegerValue);
9380   else if (IsStringValue)
9381     getTargetStreamer().emitTextAttribute(Tag, StringValue);
9382   return false;
9383 }
9384 
9385 /// parseDirectiveCPU
9386 ///  ::= .cpu str
9387 bool ARMAsmParser::parseDirectiveCPU(SMLoc L) {
9388   StringRef CPU = getParser().parseStringToEndOfStatement().trim();
9389   getTargetStreamer().emitTextAttribute(ARMBuildAttrs::CPU_name, CPU);
9390 
9391   // FIXME: This is using table-gen data, but should be moved to
9392   // ARMTargetParser once that is table-gen'd.
9393   if (!getSTI().isCPUStringValid(CPU)) {
9394     Error(L, "Unknown CPU name");
9395     return false;
9396   }
9397 
9398   bool WasThumb = isThumb();
9399   MCSubtargetInfo &STI = copySTI();
9400   STI.setDefaultFeatures(CPU, "");
9401   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9402   FixModeAfterArchChange(WasThumb, L);
9403 
9404   return false;
9405 }
9406 /// parseDirectiveFPU
9407 ///  ::= .fpu str
9408 bool ARMAsmParser::parseDirectiveFPU(SMLoc L) {
9409   SMLoc FPUNameLoc = getTok().getLoc();
9410   StringRef FPU = getParser().parseStringToEndOfStatement().trim();
9411 
9412   unsigned ID = ARM::parseFPU(FPU);
9413   std::vector<const char *> Features;
9414   if (!ARM::getFPUFeatures(ID, Features)) {
9415     Error(FPUNameLoc, "Unknown FPU name");
9416     return false;
9417   }
9418 
9419   MCSubtargetInfo &STI = copySTI();
9420   for (auto Feature : Features)
9421     STI.ApplyFeatureFlag(Feature);
9422   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9423 
9424   getTargetStreamer().emitFPU(ID);
9425   return false;
9426 }
9427 
9428 /// parseDirectiveFnStart
9429 ///  ::= .fnstart
9430 bool ARMAsmParser::parseDirectiveFnStart(SMLoc L) {
9431   if (UC.hasFnStart()) {
9432     Error(L, ".fnstart starts before the end of previous one");
9433     UC.emitFnStartLocNotes();
9434     return false;
9435   }
9436 
9437   // Reset the unwind directives parser state
9438   UC.reset();
9439 
9440   getTargetStreamer().emitFnStart();
9441 
9442   UC.recordFnStart(L);
9443   return false;
9444 }
9445 
9446 /// parseDirectiveFnEnd
9447 ///  ::= .fnend
9448 bool ARMAsmParser::parseDirectiveFnEnd(SMLoc L) {
9449   // Check the ordering of unwind directives
9450   if (!UC.hasFnStart()) {
9451     Error(L, ".fnstart must precede .fnend directive");
9452     return false;
9453   }
9454 
9455   // Reset the unwind directives parser state
9456   getTargetStreamer().emitFnEnd();
9457 
9458   UC.reset();
9459   return false;
9460 }
9461 
9462 /// parseDirectiveCantUnwind
9463 ///  ::= .cantunwind
9464 bool ARMAsmParser::parseDirectiveCantUnwind(SMLoc L) {
9465   UC.recordCantUnwind(L);
9466 
9467   // Check the ordering of unwind directives
9468   if (!UC.hasFnStart()) {
9469     Error(L, ".fnstart must precede .cantunwind directive");
9470     return false;
9471   }
9472   if (UC.hasHandlerData()) {
9473     Error(L, ".cantunwind can't be used with .handlerdata directive");
9474     UC.emitHandlerDataLocNotes();
9475     return false;
9476   }
9477   if (UC.hasPersonality()) {
9478     Error(L, ".cantunwind can't be used with .personality directive");
9479     UC.emitPersonalityLocNotes();
9480     return false;
9481   }
9482 
9483   getTargetStreamer().emitCantUnwind();
9484   return false;
9485 }
9486 
9487 /// parseDirectivePersonality
9488 ///  ::= .personality name
9489 bool ARMAsmParser::parseDirectivePersonality(SMLoc L) {
9490   MCAsmParser &Parser = getParser();
9491   bool HasExistingPersonality = UC.hasPersonality();
9492 
9493   UC.recordPersonality(L);
9494 
9495   // Check the ordering of unwind directives
9496   if (!UC.hasFnStart()) {
9497     Error(L, ".fnstart must precede .personality directive");
9498     return false;
9499   }
9500   if (UC.cantUnwind()) {
9501     Error(L, ".personality can't be used with .cantunwind directive");
9502     UC.emitCantUnwindLocNotes();
9503     return false;
9504   }
9505   if (UC.hasHandlerData()) {
9506     Error(L, ".personality must precede .handlerdata directive");
9507     UC.emitHandlerDataLocNotes();
9508     return false;
9509   }
9510   if (HasExistingPersonality) {
9511     Parser.eatToEndOfStatement();
9512     Error(L, "multiple personality directives");
9513     UC.emitPersonalityLocNotes();
9514     return false;
9515   }
9516 
9517   // Parse the name of the personality routine
9518   if (Parser.getTok().isNot(AsmToken::Identifier)) {
9519     Parser.eatToEndOfStatement();
9520     Error(L, "unexpected input in .personality directive.");
9521     return false;
9522   }
9523   StringRef Name(Parser.getTok().getIdentifier());
9524   Parser.Lex();
9525 
9526   MCSymbol *PR = getParser().getContext().getOrCreateSymbol(Name);
9527   getTargetStreamer().emitPersonality(PR);
9528   return false;
9529 }
9530 
9531 /// parseDirectiveHandlerData
9532 ///  ::= .handlerdata
9533 bool ARMAsmParser::parseDirectiveHandlerData(SMLoc L) {
9534   UC.recordHandlerData(L);
9535 
9536   // Check the ordering of unwind directives
9537   if (!UC.hasFnStart()) {
9538     Error(L, ".fnstart must precede .personality directive");
9539     return false;
9540   }
9541   if (UC.cantUnwind()) {
9542     Error(L, ".handlerdata can't be used with .cantunwind directive");
9543     UC.emitCantUnwindLocNotes();
9544     return false;
9545   }
9546 
9547   getTargetStreamer().emitHandlerData();
9548   return false;
9549 }
9550 
9551 /// parseDirectiveSetFP
9552 ///  ::= .setfp fpreg, spreg [, offset]
9553 bool ARMAsmParser::parseDirectiveSetFP(SMLoc L) {
9554   MCAsmParser &Parser = getParser();
9555   // Check the ordering of unwind directives
9556   if (!UC.hasFnStart()) {
9557     Error(L, ".fnstart must precede .setfp directive");
9558     return false;
9559   }
9560   if (UC.hasHandlerData()) {
9561     Error(L, ".setfp must precede .handlerdata directive");
9562     return false;
9563   }
9564 
9565   // Parse fpreg
9566   SMLoc FPRegLoc = Parser.getTok().getLoc();
9567   int FPReg = tryParseRegister();
9568   if (FPReg == -1) {
9569     Error(FPRegLoc, "frame pointer register expected");
9570     return false;
9571   }
9572 
9573   // Consume comma
9574   if (Parser.getTok().isNot(AsmToken::Comma)) {
9575     Error(Parser.getTok().getLoc(), "comma expected");
9576     return false;
9577   }
9578   Parser.Lex(); // skip comma
9579 
9580   // Parse spreg
9581   SMLoc SPRegLoc = Parser.getTok().getLoc();
9582   int SPReg = tryParseRegister();
9583   if (SPReg == -1) {
9584     Error(SPRegLoc, "stack pointer register expected");
9585     return false;
9586   }
9587 
9588   if (SPReg != ARM::SP && SPReg != UC.getFPReg()) {
9589     Error(SPRegLoc, "register should be either $sp or the latest fp register");
9590     return false;
9591   }
9592 
9593   // Update the frame pointer register
9594   UC.saveFPReg(FPReg);
9595 
9596   // Parse offset
9597   int64_t Offset = 0;
9598   if (Parser.getTok().is(AsmToken::Comma)) {
9599     Parser.Lex(); // skip comma
9600 
9601     if (Parser.getTok().isNot(AsmToken::Hash) &&
9602         Parser.getTok().isNot(AsmToken::Dollar)) {
9603       Error(Parser.getTok().getLoc(), "'#' expected");
9604       return false;
9605     }
9606     Parser.Lex(); // skip hash token.
9607 
9608     const MCExpr *OffsetExpr;
9609     SMLoc ExLoc = Parser.getTok().getLoc();
9610     SMLoc EndLoc;
9611     if (getParser().parseExpression(OffsetExpr, EndLoc)) {
9612       Error(ExLoc, "malformed setfp offset");
9613       return false;
9614     }
9615     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9616     if (!CE) {
9617       Error(ExLoc, "setfp offset must be an immediate");
9618       return false;
9619     }
9620 
9621     Offset = CE->getValue();
9622   }
9623 
9624   getTargetStreamer().emitSetFP(static_cast<unsigned>(FPReg),
9625                                 static_cast<unsigned>(SPReg), Offset);
9626   return false;
9627 }
9628 
9629 /// parseDirective
9630 ///  ::= .pad offset
9631 bool ARMAsmParser::parseDirectivePad(SMLoc L) {
9632   MCAsmParser &Parser = getParser();
9633   // Check the ordering of unwind directives
9634   if (!UC.hasFnStart()) {
9635     Error(L, ".fnstart must precede .pad directive");
9636     return false;
9637   }
9638   if (UC.hasHandlerData()) {
9639     Error(L, ".pad must precede .handlerdata directive");
9640     return false;
9641   }
9642 
9643   // Parse the offset
9644   if (Parser.getTok().isNot(AsmToken::Hash) &&
9645       Parser.getTok().isNot(AsmToken::Dollar)) {
9646     Error(Parser.getTok().getLoc(), "'#' expected");
9647     return false;
9648   }
9649   Parser.Lex(); // skip hash token.
9650 
9651   const MCExpr *OffsetExpr;
9652   SMLoc ExLoc = Parser.getTok().getLoc();
9653   SMLoc EndLoc;
9654   if (getParser().parseExpression(OffsetExpr, EndLoc)) {
9655     Error(ExLoc, "malformed pad offset");
9656     return false;
9657   }
9658   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9659   if (!CE) {
9660     Error(ExLoc, "pad offset must be an immediate");
9661     return false;
9662   }
9663 
9664   getTargetStreamer().emitPad(CE->getValue());
9665   return false;
9666 }
9667 
9668 /// parseDirectiveRegSave
9669 ///  ::= .save  { registers }
9670 ///  ::= .vsave { registers }
9671 bool ARMAsmParser::parseDirectiveRegSave(SMLoc L, bool IsVector) {
9672   // Check the ordering of unwind directives
9673   if (!UC.hasFnStart()) {
9674     Error(L, ".fnstart must precede .save or .vsave directives");
9675     return false;
9676   }
9677   if (UC.hasHandlerData()) {
9678     Error(L, ".save or .vsave must precede .handlerdata directive");
9679     return false;
9680   }
9681 
9682   // RAII object to make sure parsed operands are deleted.
9683   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Operands;
9684 
9685   // Parse the register list
9686   if (parseRegisterList(Operands))
9687     return false;
9688   ARMOperand &Op = (ARMOperand &)*Operands[0];
9689   if (!IsVector && !Op.isRegList()) {
9690     Error(L, ".save expects GPR registers");
9691     return false;
9692   }
9693   if (IsVector && !Op.isDPRRegList()) {
9694     Error(L, ".vsave expects DPR registers");
9695     return false;
9696   }
9697 
9698   getTargetStreamer().emitRegSave(Op.getRegList(), IsVector);
9699   return false;
9700 }
9701 
9702 /// parseDirectiveInst
9703 ///  ::= .inst opcode [, ...]
9704 ///  ::= .inst.n opcode [, ...]
9705 ///  ::= .inst.w opcode [, ...]
9706 bool ARMAsmParser::parseDirectiveInst(SMLoc Loc, char Suffix) {
9707   MCAsmParser &Parser = getParser();
9708   int Width;
9709 
9710   if (isThumb()) {
9711     switch (Suffix) {
9712     case 'n':
9713       Width = 2;
9714       break;
9715     case 'w':
9716       Width = 4;
9717       break;
9718     default:
9719       Parser.eatToEndOfStatement();
9720       Error(Loc, "cannot determine Thumb instruction size, "
9721                  "use inst.n/inst.w instead");
9722       return false;
9723     }
9724   } else {
9725     if (Suffix) {
9726       Parser.eatToEndOfStatement();
9727       Error(Loc, "width suffixes are invalid in ARM mode");
9728       return false;
9729     }
9730     Width = 4;
9731   }
9732 
9733   if (getLexer().is(AsmToken::EndOfStatement)) {
9734     Parser.eatToEndOfStatement();
9735     Error(Loc, "expected expression following directive");
9736     return false;
9737   }
9738 
9739   for (;;) {
9740     const MCExpr *Expr;
9741 
9742     if (getParser().parseExpression(Expr)) {
9743       Error(Loc, "expected expression");
9744       return false;
9745     }
9746 
9747     const MCConstantExpr *Value = dyn_cast_or_null<MCConstantExpr>(Expr);
9748     if (!Value) {
9749       Error(Loc, "expected constant expression");
9750       return false;
9751     }
9752 
9753     switch (Width) {
9754     case 2:
9755       if (Value->getValue() > 0xffff) {
9756         Error(Loc, "inst.n operand is too big, use inst.w instead");
9757         return false;
9758       }
9759       break;
9760     case 4:
9761       if (Value->getValue() > 0xffffffff) {
9762         Error(Loc,
9763               StringRef(Suffix ? "inst.w" : "inst") + " operand is too big");
9764         return false;
9765       }
9766       break;
9767     default:
9768       llvm_unreachable("only supported widths are 2 and 4");
9769     }
9770 
9771     getTargetStreamer().emitInst(Value->getValue(), Suffix);
9772 
9773     if (getLexer().is(AsmToken::EndOfStatement))
9774       break;
9775 
9776     if (getLexer().isNot(AsmToken::Comma)) {
9777       Error(Loc, "unexpected token in directive");
9778       return false;
9779     }
9780 
9781     Parser.Lex();
9782   }
9783 
9784   Parser.Lex();
9785   return false;
9786 }
9787 
9788 /// parseDirectiveLtorg
9789 ///  ::= .ltorg | .pool
9790 bool ARMAsmParser::parseDirectiveLtorg(SMLoc L) {
9791   getTargetStreamer().emitCurrentConstantPool();
9792   return false;
9793 }
9794 
9795 bool ARMAsmParser::parseDirectiveEven(SMLoc L) {
9796   const MCSection *Section = getStreamer().getCurrentSection().first;
9797 
9798   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9799     TokError("unexpected token in directive");
9800     return false;
9801   }
9802 
9803   if (!Section) {
9804     getStreamer().InitSections(false);
9805     Section = getStreamer().getCurrentSection().first;
9806   }
9807 
9808   assert(Section && "must have section to emit alignment");
9809   if (Section->UseCodeAlign())
9810     getStreamer().EmitCodeAlignment(2);
9811   else
9812     getStreamer().EmitValueToAlignment(2);
9813 
9814   return false;
9815 }
9816 
9817 /// parseDirectivePersonalityIndex
9818 ///   ::= .personalityindex index
9819 bool ARMAsmParser::parseDirectivePersonalityIndex(SMLoc L) {
9820   MCAsmParser &Parser = getParser();
9821   bool HasExistingPersonality = UC.hasPersonality();
9822 
9823   UC.recordPersonalityIndex(L);
9824 
9825   if (!UC.hasFnStart()) {
9826     Parser.eatToEndOfStatement();
9827     Error(L, ".fnstart must precede .personalityindex directive");
9828     return false;
9829   }
9830   if (UC.cantUnwind()) {
9831     Parser.eatToEndOfStatement();
9832     Error(L, ".personalityindex cannot be used with .cantunwind");
9833     UC.emitCantUnwindLocNotes();
9834     return false;
9835   }
9836   if (UC.hasHandlerData()) {
9837     Parser.eatToEndOfStatement();
9838     Error(L, ".personalityindex must precede .handlerdata directive");
9839     UC.emitHandlerDataLocNotes();
9840     return false;
9841   }
9842   if (HasExistingPersonality) {
9843     Parser.eatToEndOfStatement();
9844     Error(L, "multiple personality directives");
9845     UC.emitPersonalityLocNotes();
9846     return false;
9847   }
9848 
9849   const MCExpr *IndexExpression;
9850   SMLoc IndexLoc = Parser.getTok().getLoc();
9851   if (Parser.parseExpression(IndexExpression)) {
9852     Parser.eatToEndOfStatement();
9853     return false;
9854   }
9855 
9856   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(IndexExpression);
9857   if (!CE) {
9858     Parser.eatToEndOfStatement();
9859     Error(IndexLoc, "index must be a constant number");
9860     return false;
9861   }
9862   if (CE->getValue() < 0 ||
9863       CE->getValue() >= ARM::EHABI::NUM_PERSONALITY_INDEX) {
9864     Parser.eatToEndOfStatement();
9865     Error(IndexLoc, "personality routine index should be in range [0-3]");
9866     return false;
9867   }
9868 
9869   getTargetStreamer().emitPersonalityIndex(CE->getValue());
9870   return false;
9871 }
9872 
9873 /// parseDirectiveUnwindRaw
9874 ///   ::= .unwind_raw offset, opcode [, opcode...]
9875 bool ARMAsmParser::parseDirectiveUnwindRaw(SMLoc L) {
9876   MCAsmParser &Parser = getParser();
9877   if (!UC.hasFnStart()) {
9878     Parser.eatToEndOfStatement();
9879     Error(L, ".fnstart must precede .unwind_raw directives");
9880     return false;
9881   }
9882 
9883   int64_t StackOffset;
9884 
9885   const MCExpr *OffsetExpr;
9886   SMLoc OffsetLoc = getLexer().getLoc();
9887   if (getLexer().is(AsmToken::EndOfStatement) ||
9888       getParser().parseExpression(OffsetExpr)) {
9889     Error(OffsetLoc, "expected expression");
9890     Parser.eatToEndOfStatement();
9891     return false;
9892   }
9893 
9894   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9895   if (!CE) {
9896     Error(OffsetLoc, "offset must be a constant");
9897     Parser.eatToEndOfStatement();
9898     return false;
9899   }
9900 
9901   StackOffset = CE->getValue();
9902 
9903   if (getLexer().isNot(AsmToken::Comma)) {
9904     Error(getLexer().getLoc(), "expected comma");
9905     Parser.eatToEndOfStatement();
9906     return false;
9907   }
9908   Parser.Lex();
9909 
9910   SmallVector<uint8_t, 16> Opcodes;
9911   for (;;) {
9912     const MCExpr *OE;
9913 
9914     SMLoc OpcodeLoc = getLexer().getLoc();
9915     if (getLexer().is(AsmToken::EndOfStatement) || Parser.parseExpression(OE)) {
9916       Error(OpcodeLoc, "expected opcode expression");
9917       Parser.eatToEndOfStatement();
9918       return false;
9919     }
9920 
9921     const MCConstantExpr *OC = dyn_cast<MCConstantExpr>(OE);
9922     if (!OC) {
9923       Error(OpcodeLoc, "opcode value must be a constant");
9924       Parser.eatToEndOfStatement();
9925       return false;
9926     }
9927 
9928     const int64_t Opcode = OC->getValue();
9929     if (Opcode & ~0xff) {
9930       Error(OpcodeLoc, "invalid opcode");
9931       Parser.eatToEndOfStatement();
9932       return false;
9933     }
9934 
9935     Opcodes.push_back(uint8_t(Opcode));
9936 
9937     if (getLexer().is(AsmToken::EndOfStatement))
9938       break;
9939 
9940     if (getLexer().isNot(AsmToken::Comma)) {
9941       Error(getLexer().getLoc(), "unexpected token in directive");
9942       Parser.eatToEndOfStatement();
9943       return false;
9944     }
9945 
9946     Parser.Lex();
9947   }
9948 
9949   getTargetStreamer().emitUnwindRaw(StackOffset, Opcodes);
9950 
9951   Parser.Lex();
9952   return false;
9953 }
9954 
9955 /// parseDirectiveTLSDescSeq
9956 ///   ::= .tlsdescseq tls-variable
9957 bool ARMAsmParser::parseDirectiveTLSDescSeq(SMLoc L) {
9958   MCAsmParser &Parser = getParser();
9959 
9960   if (getLexer().isNot(AsmToken::Identifier)) {
9961     TokError("expected variable after '.tlsdescseq' directive");
9962     Parser.eatToEndOfStatement();
9963     return false;
9964   }
9965 
9966   const MCSymbolRefExpr *SRE =
9967     MCSymbolRefExpr::create(Parser.getTok().getIdentifier(),
9968                             MCSymbolRefExpr::VK_ARM_TLSDESCSEQ, getContext());
9969   Lex();
9970 
9971   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9972     Error(Parser.getTok().getLoc(), "unexpected token");
9973     Parser.eatToEndOfStatement();
9974     return false;
9975   }
9976 
9977   getTargetStreamer().AnnotateTLSDescriptorSequence(SRE);
9978   return false;
9979 }
9980 
9981 /// parseDirectiveMovSP
9982 ///  ::= .movsp reg [, #offset]
9983 bool ARMAsmParser::parseDirectiveMovSP(SMLoc L) {
9984   MCAsmParser &Parser = getParser();
9985   if (!UC.hasFnStart()) {
9986     Parser.eatToEndOfStatement();
9987     Error(L, ".fnstart must precede .movsp directives");
9988     return false;
9989   }
9990   if (UC.getFPReg() != ARM::SP) {
9991     Parser.eatToEndOfStatement();
9992     Error(L, "unexpected .movsp directive");
9993     return false;
9994   }
9995 
9996   SMLoc SPRegLoc = Parser.getTok().getLoc();
9997   int SPReg = tryParseRegister();
9998   if (SPReg == -1) {
9999     Parser.eatToEndOfStatement();
10000     Error(SPRegLoc, "register expected");
10001     return false;
10002   }
10003 
10004   if (SPReg == ARM::SP || SPReg == ARM::PC) {
10005     Parser.eatToEndOfStatement();
10006     Error(SPRegLoc, "sp and pc are not permitted in .movsp directive");
10007     return false;
10008   }
10009 
10010   int64_t Offset = 0;
10011   if (Parser.getTok().is(AsmToken::Comma)) {
10012     Parser.Lex();
10013 
10014     if (Parser.getTok().isNot(AsmToken::Hash)) {
10015       Error(Parser.getTok().getLoc(), "expected #constant");
10016       Parser.eatToEndOfStatement();
10017       return false;
10018     }
10019     Parser.Lex();
10020 
10021     const MCExpr *OffsetExpr;
10022     SMLoc OffsetLoc = Parser.getTok().getLoc();
10023     if (Parser.parseExpression(OffsetExpr)) {
10024       Parser.eatToEndOfStatement();
10025       Error(OffsetLoc, "malformed offset expression");
10026       return false;
10027     }
10028 
10029     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
10030     if (!CE) {
10031       Parser.eatToEndOfStatement();
10032       Error(OffsetLoc, "offset must be an immediate constant");
10033       return false;
10034     }
10035 
10036     Offset = CE->getValue();
10037   }
10038 
10039   getTargetStreamer().emitMovSP(SPReg, Offset);
10040   UC.saveFPReg(SPReg);
10041 
10042   return false;
10043 }
10044 
10045 /// parseDirectiveObjectArch
10046 ///   ::= .object_arch name
10047 bool ARMAsmParser::parseDirectiveObjectArch(SMLoc L) {
10048   MCAsmParser &Parser = getParser();
10049   if (getLexer().isNot(AsmToken::Identifier)) {
10050     Error(getLexer().getLoc(), "unexpected token");
10051     Parser.eatToEndOfStatement();
10052     return false;
10053   }
10054 
10055   StringRef Arch = Parser.getTok().getString();
10056   SMLoc ArchLoc = Parser.getTok().getLoc();
10057   getLexer().Lex();
10058 
10059   unsigned ID = ARM::parseArch(Arch);
10060 
10061   if (ID == ARM::AK_INVALID) {
10062     Error(ArchLoc, "unknown architecture '" + Arch + "'");
10063     Parser.eatToEndOfStatement();
10064     return false;
10065   }
10066 
10067   getTargetStreamer().emitObjectArch(ID);
10068 
10069   if (getLexer().isNot(AsmToken::EndOfStatement)) {
10070     Error(getLexer().getLoc(), "unexpected token");
10071     Parser.eatToEndOfStatement();
10072   }
10073 
10074   return false;
10075 }
10076 
10077 /// parseDirectiveAlign
10078 ///   ::= .align
10079 bool ARMAsmParser::parseDirectiveAlign(SMLoc L) {
10080   // NOTE: if this is not the end of the statement, fall back to the target
10081   // agnostic handling for this directive which will correctly handle this.
10082   if (getLexer().isNot(AsmToken::EndOfStatement))
10083     return true;
10084 
10085   // '.align' is target specifically handled to mean 2**2 byte alignment.
10086   const MCSection *Section = getStreamer().getCurrentSection().first;
10087   assert(Section && "must have section to emit alignment");
10088   if (Section->UseCodeAlign())
10089     getStreamer().EmitCodeAlignment(4, 0);
10090   else
10091     getStreamer().EmitValueToAlignment(4, 0, 1, 0);
10092 
10093   return false;
10094 }
10095 
10096 /// parseDirectiveThumbSet
10097 ///  ::= .thumb_set name, value
10098 bool ARMAsmParser::parseDirectiveThumbSet(SMLoc L) {
10099   MCAsmParser &Parser = getParser();
10100 
10101   StringRef Name;
10102   if (Parser.parseIdentifier(Name)) {
10103     TokError("expected identifier after '.thumb_set'");
10104     Parser.eatToEndOfStatement();
10105     return false;
10106   }
10107 
10108   if (getLexer().isNot(AsmToken::Comma)) {
10109     TokError("expected comma after name '" + Name + "'");
10110     Parser.eatToEndOfStatement();
10111     return false;
10112   }
10113   Lex();
10114 
10115   MCSymbol *Sym;
10116   const MCExpr *Value;
10117   if (MCParserUtils::parseAssignmentExpression(Name, /* allow_redef */ true,
10118                                                Parser, Sym, Value))
10119     return true;
10120 
10121   getTargetStreamer().emitThumbSet(Sym, Value);
10122   return false;
10123 }
10124 
10125 /// Force static initialization.
10126 extern "C" void LLVMInitializeARMAsmParser() {
10127   RegisterMCAsmParser<ARMAsmParser> X(TheARMLETarget);
10128   RegisterMCAsmParser<ARMAsmParser> Y(TheARMBETarget);
10129   RegisterMCAsmParser<ARMAsmParser> A(TheThumbLETarget);
10130   RegisterMCAsmParser<ARMAsmParser> B(TheThumbBETarget);
10131 }
10132 
10133 #define GET_REGISTER_MATCHER
10134 #define GET_SUBTARGET_FEATURE_NAME
10135 #define GET_MATCHER_IMPLEMENTATION
10136 #include "ARMGenAsmMatcher.inc"
10137 
10138 // FIXME: This structure should be moved inside ARMTargetParser
10139 // when we start to table-generate them, and we can use the ARM
10140 // flags below, that were generated by table-gen.
10141 static const struct {
10142   const unsigned Kind;
10143   const uint64_t ArchCheck;
10144   const FeatureBitset Features;
10145 } Extensions[] = {
10146   { ARM::AEK_CRC, Feature_HasV8, {ARM::FeatureCRC} },
10147   { ARM::AEK_CRYPTO,  Feature_HasV8,
10148     {ARM::FeatureCrypto, ARM::FeatureNEON, ARM::FeatureFPARMv8} },
10149   { ARM::AEK_FP, Feature_HasV8, {ARM::FeatureFPARMv8} },
10150   { (ARM::AEK_HWDIV | ARM::AEK_HWDIVARM), Feature_HasV7 | Feature_IsNotMClass,
10151     {ARM::FeatureHWDiv, ARM::FeatureHWDivARM} },
10152   { ARM::AEK_MP, Feature_HasV7 | Feature_IsNotMClass, {ARM::FeatureMP} },
10153   { ARM::AEK_SIMD, Feature_HasV8, {ARM::FeatureNEON, ARM::FeatureFPARMv8} },
10154   { ARM::AEK_SEC, Feature_HasV6K, {ARM::FeatureTrustZone} },
10155   // FIXME: Only available in A-class, isel not predicated
10156   { ARM::AEK_VIRT, Feature_HasV7, {ARM::FeatureVirtualization} },
10157   { ARM::AEK_FP16, Feature_HasV8_2a, {ARM::FeatureFPARMv8, ARM::FeatureFullFP16} },
10158   // FIXME: Unsupported extensions.
10159   { ARM::AEK_OS, Feature_None, {} },
10160   { ARM::AEK_IWMMXT, Feature_None, {} },
10161   { ARM::AEK_IWMMXT2, Feature_None, {} },
10162   { ARM::AEK_MAVERICK, Feature_None, {} },
10163   { ARM::AEK_XSCALE, Feature_None, {} },
10164 };
10165 
10166 /// parseDirectiveArchExtension
10167 ///   ::= .arch_extension [no]feature
10168 bool ARMAsmParser::parseDirectiveArchExtension(SMLoc L) {
10169   MCAsmParser &Parser = getParser();
10170 
10171   if (getLexer().isNot(AsmToken::Identifier)) {
10172     Error(getLexer().getLoc(), "unexpected token");
10173     Parser.eatToEndOfStatement();
10174     return false;
10175   }
10176 
10177   StringRef Name = Parser.getTok().getString();
10178   SMLoc ExtLoc = Parser.getTok().getLoc();
10179   getLexer().Lex();
10180 
10181   bool EnableFeature = true;
10182   if (Name.startswith_lower("no")) {
10183     EnableFeature = false;
10184     Name = Name.substr(2);
10185   }
10186   unsigned FeatureKind = ARM::parseArchExt(Name);
10187   if (FeatureKind == ARM::AEK_INVALID)
10188     Error(ExtLoc, "unknown architectural extension: " + Name);
10189 
10190   for (const auto &Extension : Extensions) {
10191     if (Extension.Kind != FeatureKind)
10192       continue;
10193 
10194     if (Extension.Features.none())
10195       report_fatal_error("unsupported architectural extension: " + Name);
10196 
10197     if ((getAvailableFeatures() & Extension.ArchCheck) != Extension.ArchCheck) {
10198       Error(ExtLoc, "architectural extension '" + Name + "' is not "
10199             "allowed for the current base architecture");
10200       return false;
10201     }
10202 
10203     MCSubtargetInfo &STI = copySTI();
10204     FeatureBitset ToggleFeatures = EnableFeature
10205       ? (~STI.getFeatureBits() & Extension.Features)
10206       : ( STI.getFeatureBits() & Extension.Features);
10207 
10208     uint64_t Features =
10209         ComputeAvailableFeatures(STI.ToggleFeature(ToggleFeatures));
10210     setAvailableFeatures(Features);
10211     return false;
10212   }
10213 
10214   Error(ExtLoc, "unknown architectural extension: " + Name);
10215   Parser.eatToEndOfStatement();
10216   return false;
10217 }
10218 
10219 // Define this matcher function after the auto-generated include so we
10220 // have the match class enum definitions.
10221 unsigned ARMAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
10222                                                   unsigned Kind) {
10223   ARMOperand &Op = static_cast<ARMOperand &>(AsmOp);
10224   // If the kind is a token for a literal immediate, check if our asm
10225   // operand matches. This is for InstAliases which have a fixed-value
10226   // immediate in the syntax.
10227   switch (Kind) {
10228   default: break;
10229   case MCK__35_0:
10230     if (Op.isImm())
10231       if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm()))
10232         if (CE->getValue() == 0)
10233           return Match_Success;
10234     break;
10235   case MCK_ModImm:
10236     if (Op.isImm()) {
10237       const MCExpr *SOExpr = Op.getImm();
10238       int64_t Value;
10239       if (!SOExpr->evaluateAsAbsolute(Value))
10240         return Match_Success;
10241       assert((Value >= INT32_MIN && Value <= UINT32_MAX) &&
10242              "expression value must be representable in 32 bits");
10243     }
10244     break;
10245   case MCK_rGPR:
10246     if (hasV8Ops() && Op.isReg() && Op.getReg() == ARM::SP)
10247       return Match_Success;
10248     break;
10249   case MCK_GPRPair:
10250     if (Op.isReg() &&
10251         MRI->getRegClass(ARM::GPRRegClassID).contains(Op.getReg()))
10252       return Match_Success;
10253     break;
10254   }
10255   return Match_InvalidOperand;
10256 }
10257