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   bool hasRAS() const {
300     return getSTI().getFeatureBits()[ARM::FeatureRAS];
301   }
302 
303   void SwitchMode() {
304     MCSubtargetInfo &STI = copySTI();
305     uint64_t FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
306     setAvailableFeatures(FB);
307   }
308   void FixModeAfterArchChange(bool WasThumb, SMLoc Loc);
309   bool isMClass() const {
310     return getSTI().getFeatureBits()[ARM::FeatureMClass];
311   }
312 
313   /// @name Auto-generated Match Functions
314   /// {
315 
316 #define GET_ASSEMBLER_HEADER
317 #include "ARMGenAsmMatcher.inc"
318 
319   /// }
320 
321   OperandMatchResultTy parseITCondCode(OperandVector &);
322   OperandMatchResultTy parseCoprocNumOperand(OperandVector &);
323   OperandMatchResultTy parseCoprocRegOperand(OperandVector &);
324   OperandMatchResultTy parseCoprocOptionOperand(OperandVector &);
325   OperandMatchResultTy parseMemBarrierOptOperand(OperandVector &);
326   OperandMatchResultTy parseInstSyncBarrierOptOperand(OperandVector &);
327   OperandMatchResultTy parseProcIFlagsOperand(OperandVector &);
328   OperandMatchResultTy parseMSRMaskOperand(OperandVector &);
329   OperandMatchResultTy parseBankedRegOperand(OperandVector &);
330   OperandMatchResultTy parsePKHImm(OperandVector &O, StringRef Op, int Low,
331                                    int High);
332   OperandMatchResultTy parsePKHLSLImm(OperandVector &O) {
333     return parsePKHImm(O, "lsl", 0, 31);
334   }
335   OperandMatchResultTy parsePKHASRImm(OperandVector &O) {
336     return parsePKHImm(O, "asr", 1, 32);
337   }
338   OperandMatchResultTy parseSetEndImm(OperandVector &);
339   OperandMatchResultTy parseShifterImm(OperandVector &);
340   OperandMatchResultTy parseRotImm(OperandVector &);
341   OperandMatchResultTy parseModImm(OperandVector &);
342   OperandMatchResultTy parseBitfield(OperandVector &);
343   OperandMatchResultTy parsePostIdxReg(OperandVector &);
344   OperandMatchResultTy parseAM3Offset(OperandVector &);
345   OperandMatchResultTy parseFPImm(OperandVector &);
346   OperandMatchResultTy parseVectorList(OperandVector &);
347   OperandMatchResultTy parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index,
348                                        SMLoc &EndLoc);
349 
350   // Asm Match Converter Methods
351   void cvtThumbMultiply(MCInst &Inst, const OperandVector &);
352   void cvtThumbBranches(MCInst &Inst, const OperandVector &);
353 
354   bool validateInstruction(MCInst &Inst, const OperandVector &Ops);
355   bool processInstruction(MCInst &Inst, const OperandVector &Ops, MCStreamer &Out);
356   bool shouldOmitCCOutOperand(StringRef Mnemonic, OperandVector &Operands);
357   bool shouldOmitPredicateOperand(StringRef Mnemonic, OperandVector &Operands);
358 
359 public:
360   enum ARMMatchResultTy {
361     Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
362     Match_RequiresNotITBlock,
363     Match_RequiresV6,
364     Match_RequiresThumb2,
365     Match_RequiresV8,
366 #define GET_OPERAND_DIAGNOSTIC_TYPES
367 #include "ARMGenAsmMatcher.inc"
368 
369   };
370 
371   ARMAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
372                const MCInstrInfo &MII, const MCTargetOptions &Options)
373     : MCTargetAsmParser(Options, STI), MII(MII), UC(Parser) {
374     MCAsmParserExtension::Initialize(Parser);
375 
376     // Cache the MCRegisterInfo.
377     MRI = getContext().getRegisterInfo();
378 
379     // Initialize the set of available features.
380     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
381 
382     // Not in an ITBlock to start with.
383     ITState.CurPosition = ~0U;
384 
385     NextSymbolIsThumb = false;
386   }
387 
388   // Implementation of the MCTargetAsmParser interface:
389   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
390   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
391                         SMLoc NameLoc, OperandVector &Operands) override;
392   bool ParseDirective(AsmToken DirectiveID) override;
393 
394   unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
395                                       unsigned Kind) override;
396   unsigned checkTargetMatchPredicate(MCInst &Inst) override;
397 
398   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
399                                OperandVector &Operands, MCStreamer &Out,
400                                uint64_t &ErrorInfo,
401                                bool MatchingInlineAsm) override;
402   void onLabelParsed(MCSymbol *Symbol) override;
403 };
404 } // end anonymous namespace
405 
406 namespace {
407 
408 /// ARMOperand - Instances of this class represent a parsed ARM machine
409 /// operand.
410 class ARMOperand : public MCParsedAsmOperand {
411   enum KindTy {
412     k_CondCode,
413     k_CCOut,
414     k_ITCondMask,
415     k_CoprocNum,
416     k_CoprocReg,
417     k_CoprocOption,
418     k_Immediate,
419     k_MemBarrierOpt,
420     k_InstSyncBarrierOpt,
421     k_Memory,
422     k_PostIndexRegister,
423     k_MSRMask,
424     k_BankedReg,
425     k_ProcIFlags,
426     k_VectorIndex,
427     k_Register,
428     k_RegisterList,
429     k_DPRRegisterList,
430     k_SPRRegisterList,
431     k_VectorList,
432     k_VectorListAllLanes,
433     k_VectorListIndexed,
434     k_ShiftedRegister,
435     k_ShiftedImmediate,
436     k_ShifterImmediate,
437     k_RotateImmediate,
438     k_ModifiedImmediate,
439     k_ConstantPoolImmediate,
440     k_BitfieldDescriptor,
441     k_Token,
442   } Kind;
443 
444   SMLoc StartLoc, EndLoc, AlignmentLoc;
445   SmallVector<unsigned, 8> Registers;
446 
447   struct CCOp {
448     ARMCC::CondCodes Val;
449   };
450 
451   struct CopOp {
452     unsigned Val;
453   };
454 
455   struct CoprocOptionOp {
456     unsigned Val;
457   };
458 
459   struct ITMaskOp {
460     unsigned Mask:4;
461   };
462 
463   struct MBOptOp {
464     ARM_MB::MemBOpt Val;
465   };
466 
467   struct ISBOptOp {
468     ARM_ISB::InstSyncBOpt Val;
469   };
470 
471   struct IFlagsOp {
472     ARM_PROC::IFlags Val;
473   };
474 
475   struct MMaskOp {
476     unsigned Val;
477   };
478 
479   struct BankedRegOp {
480     unsigned Val;
481   };
482 
483   struct TokOp {
484     const char *Data;
485     unsigned Length;
486   };
487 
488   struct RegOp {
489     unsigned RegNum;
490   };
491 
492   // A vector register list is a sequential list of 1 to 4 registers.
493   struct VectorListOp {
494     unsigned RegNum;
495     unsigned Count;
496     unsigned LaneIndex;
497     bool isDoubleSpaced;
498   };
499 
500   struct VectorIndexOp {
501     unsigned Val;
502   };
503 
504   struct ImmOp {
505     const MCExpr *Val;
506   };
507 
508   /// Combined record for all forms of ARM address expressions.
509   struct MemoryOp {
510     unsigned BaseRegNum;
511     // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
512     // was specified.
513     const MCConstantExpr *OffsetImm;  // Offset immediate value
514     unsigned OffsetRegNum;    // Offset register num, when OffsetImm == NULL
515     ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
516     unsigned ShiftImm;        // shift for OffsetReg.
517     unsigned Alignment;       // 0 = no alignment specified
518     // n = alignment in bytes (2, 4, 8, 16, or 32)
519     unsigned isNegative : 1;  // Negated OffsetReg? (~'U' bit)
520   };
521 
522   struct PostIdxRegOp {
523     unsigned RegNum;
524     bool isAdd;
525     ARM_AM::ShiftOpc ShiftTy;
526     unsigned ShiftImm;
527   };
528 
529   struct ShifterImmOp {
530     bool isASR;
531     unsigned Imm;
532   };
533 
534   struct RegShiftedRegOp {
535     ARM_AM::ShiftOpc ShiftTy;
536     unsigned SrcReg;
537     unsigned ShiftReg;
538     unsigned ShiftImm;
539   };
540 
541   struct RegShiftedImmOp {
542     ARM_AM::ShiftOpc ShiftTy;
543     unsigned SrcReg;
544     unsigned ShiftImm;
545   };
546 
547   struct RotImmOp {
548     unsigned Imm;
549   };
550 
551   struct ModImmOp {
552     unsigned Bits;
553     unsigned Rot;
554   };
555 
556   struct BitfieldOp {
557     unsigned LSB;
558     unsigned Width;
559   };
560 
561   union {
562     struct CCOp CC;
563     struct CopOp Cop;
564     struct CoprocOptionOp CoprocOption;
565     struct MBOptOp MBOpt;
566     struct ISBOptOp ISBOpt;
567     struct ITMaskOp ITMask;
568     struct IFlagsOp IFlags;
569     struct MMaskOp MMask;
570     struct BankedRegOp BankedReg;
571     struct TokOp Tok;
572     struct RegOp Reg;
573     struct VectorListOp VectorList;
574     struct VectorIndexOp VectorIndex;
575     struct ImmOp Imm;
576     struct MemoryOp Memory;
577     struct PostIdxRegOp PostIdxReg;
578     struct ShifterImmOp ShifterImm;
579     struct RegShiftedRegOp RegShiftedReg;
580     struct RegShiftedImmOp RegShiftedImm;
581     struct RotImmOp RotImm;
582     struct ModImmOp ModImm;
583     struct BitfieldOp Bitfield;
584   };
585 
586 public:
587   ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
588 
589   /// getStartLoc - Get the location of the first token of this operand.
590   SMLoc getStartLoc() const override { return StartLoc; }
591   /// getEndLoc - Get the location of the last token of this operand.
592   SMLoc getEndLoc() const override { return EndLoc; }
593   /// getLocRange - Get the range between the first and last token of this
594   /// operand.
595   SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
596 
597   /// getAlignmentLoc - Get the location of the Alignment token of this operand.
598   SMLoc getAlignmentLoc() const {
599     assert(Kind == k_Memory && "Invalid access!");
600     return AlignmentLoc;
601   }
602 
603   ARMCC::CondCodes getCondCode() const {
604     assert(Kind == k_CondCode && "Invalid access!");
605     return CC.Val;
606   }
607 
608   unsigned getCoproc() const {
609     assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
610     return Cop.Val;
611   }
612 
613   StringRef getToken() const {
614     assert(Kind == k_Token && "Invalid access!");
615     return StringRef(Tok.Data, Tok.Length);
616   }
617 
618   unsigned getReg() const override {
619     assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
620     return Reg.RegNum;
621   }
622 
623   const SmallVectorImpl<unsigned> &getRegList() const {
624     assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
625             Kind == k_SPRRegisterList) && "Invalid access!");
626     return Registers;
627   }
628 
629   const MCExpr *getImm() const {
630     assert(isImm() && "Invalid access!");
631     return Imm.Val;
632   }
633 
634   const MCExpr *getConstantPoolImm() const {
635     assert(isConstantPoolImm() && "Invalid access!");
636     return Imm.Val;
637   }
638 
639   unsigned getVectorIndex() const {
640     assert(Kind == k_VectorIndex && "Invalid access!");
641     return VectorIndex.Val;
642   }
643 
644   ARM_MB::MemBOpt getMemBarrierOpt() const {
645     assert(Kind == k_MemBarrierOpt && "Invalid access!");
646     return MBOpt.Val;
647   }
648 
649   ARM_ISB::InstSyncBOpt getInstSyncBarrierOpt() const {
650     assert(Kind == k_InstSyncBarrierOpt && "Invalid access!");
651     return ISBOpt.Val;
652   }
653 
654   ARM_PROC::IFlags getProcIFlags() const {
655     assert(Kind == k_ProcIFlags && "Invalid access!");
656     return IFlags.Val;
657   }
658 
659   unsigned getMSRMask() const {
660     assert(Kind == k_MSRMask && "Invalid access!");
661     return MMask.Val;
662   }
663 
664   unsigned getBankedReg() const {
665     assert(Kind == k_BankedReg && "Invalid access!");
666     return BankedReg.Val;
667   }
668 
669   bool isCoprocNum() const { return Kind == k_CoprocNum; }
670   bool isCoprocReg() const { return Kind == k_CoprocReg; }
671   bool isCoprocOption() const { return Kind == k_CoprocOption; }
672   bool isCondCode() const { return Kind == k_CondCode; }
673   bool isCCOut() const { return Kind == k_CCOut; }
674   bool isITMask() const { return Kind == k_ITCondMask; }
675   bool isITCondCode() const { return Kind == k_CondCode; }
676   bool isImm() const override {
677     return Kind == k_Immediate;
678   }
679   // checks whether this operand is an unsigned offset which fits is a field
680   // of specified width and scaled by a specific number of bits
681   template<unsigned width, unsigned scale>
682   bool isUnsignedOffset() const {
683     if (!isImm()) return false;
684     if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
685     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
686       int64_t Val = CE->getValue();
687       int64_t Align = 1LL << scale;
688       int64_t Max = Align * ((1LL << width) - 1);
689       return ((Val % Align) == 0) && (Val >= 0) && (Val <= Max);
690     }
691     return false;
692   }
693   // checks whether this operand is an signed offset which fits is a field
694   // of specified width and scaled by a specific number of bits
695   template<unsigned width, unsigned scale>
696   bool isSignedOffset() const {
697     if (!isImm()) return false;
698     if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
699     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
700       int64_t Val = CE->getValue();
701       int64_t Align = 1LL << scale;
702       int64_t Max = Align * ((1LL << (width-1)) - 1);
703       int64_t Min = -Align * (1LL << (width-1));
704       return ((Val % Align) == 0) && (Val >= Min) && (Val <= Max);
705     }
706     return false;
707   }
708 
709   // checks whether this operand is a memory operand computed as an offset
710   // applied to PC. the offset may have 8 bits of magnitude and is represented
711   // with two bits of shift. textually it may be either [pc, #imm], #imm or
712   // relocable expression...
713   bool isThumbMemPC() const {
714     int64_t Val = 0;
715     if (isImm()) {
716       if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
717       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val);
718       if (!CE) return false;
719       Val = CE->getValue();
720     }
721     else if (isMem()) {
722       if(!Memory.OffsetImm || Memory.OffsetRegNum) return false;
723       if(Memory.BaseRegNum != ARM::PC) return false;
724       Val = Memory.OffsetImm->getValue();
725     }
726     else return false;
727     return ((Val % 4) == 0) && (Val >= 0) && (Val <= 1020);
728   }
729   bool isFPImm() const {
730     if (!isImm()) return false;
731     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
732     if (!CE) return false;
733     int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
734     return Val != -1;
735   }
736   bool isFBits16() const {
737     if (!isImm()) return false;
738     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
739     if (!CE) return false;
740     int64_t Value = CE->getValue();
741     return Value >= 0 && Value <= 16;
742   }
743   bool isFBits32() const {
744     if (!isImm()) return false;
745     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
746     if (!CE) return false;
747     int64_t Value = CE->getValue();
748     return Value >= 1 && Value <= 32;
749   }
750   bool isImm8s4() const {
751     if (!isImm()) return false;
752     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
753     if (!CE) return false;
754     int64_t Value = CE->getValue();
755     return ((Value & 3) == 0) && Value >= -1020 && Value <= 1020;
756   }
757   bool isImm0_1020s4() const {
758     if (!isImm()) return false;
759     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
760     if (!CE) return false;
761     int64_t Value = CE->getValue();
762     return ((Value & 3) == 0) && Value >= 0 && Value <= 1020;
763   }
764   bool isImm0_508s4() const {
765     if (!isImm()) return false;
766     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
767     if (!CE) return false;
768     int64_t Value = CE->getValue();
769     return ((Value & 3) == 0) && Value >= 0 && Value <= 508;
770   }
771   bool isImm0_508s4Neg() const {
772     if (!isImm()) return false;
773     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
774     if (!CE) return false;
775     int64_t Value = -CE->getValue();
776     // explicitly exclude zero. we want that to use the normal 0_508 version.
777     return ((Value & 3) == 0) && Value > 0 && Value <= 508;
778   }
779   bool isImm0_239() const {
780     if (!isImm()) return false;
781     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
782     if (!CE) return false;
783     int64_t Value = CE->getValue();
784     return Value >= 0 && Value < 240;
785   }
786   bool isImm0_255() const {
787     if (!isImm()) return false;
788     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
789     if (!CE) return false;
790     int64_t Value = CE->getValue();
791     return Value >= 0 && Value < 256;
792   }
793   bool isImm0_4095() const {
794     if (!isImm()) return false;
795     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
796     if (!CE) return false;
797     int64_t Value = CE->getValue();
798     return Value >= 0 && Value < 4096;
799   }
800   bool isImm0_4095Neg() const {
801     if (!isImm()) return false;
802     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
803     if (!CE) return false;
804     int64_t Value = -CE->getValue();
805     return Value > 0 && Value < 4096;
806   }
807   bool isImm0_1() const {
808     if (!isImm()) return false;
809     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
810     if (!CE) return false;
811     int64_t Value = CE->getValue();
812     return Value >= 0 && Value < 2;
813   }
814   bool isImm0_3() const {
815     if (!isImm()) return false;
816     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
817     if (!CE) return false;
818     int64_t Value = CE->getValue();
819     return Value >= 0 && Value < 4;
820   }
821   bool isImm0_7() const {
822     if (!isImm()) return false;
823     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
824     if (!CE) return false;
825     int64_t Value = CE->getValue();
826     return Value >= 0 && Value < 8;
827   }
828   bool isImm0_15() const {
829     if (!isImm()) return false;
830     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
831     if (!CE) return false;
832     int64_t Value = CE->getValue();
833     return Value >= 0 && Value < 16;
834   }
835   bool isImm0_31() const {
836     if (!isImm()) return false;
837     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
838     if (!CE) return false;
839     int64_t Value = CE->getValue();
840     return Value >= 0 && Value < 32;
841   }
842   bool isImm0_63() const {
843     if (!isImm()) return false;
844     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
845     if (!CE) return false;
846     int64_t Value = CE->getValue();
847     return Value >= 0 && Value < 64;
848   }
849   bool isImm8() const {
850     if (!isImm()) return false;
851     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
852     if (!CE) return false;
853     int64_t Value = CE->getValue();
854     return Value == 8;
855   }
856   bool isImm16() const {
857     if (!isImm()) return false;
858     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
859     if (!CE) return false;
860     int64_t Value = CE->getValue();
861     return Value == 16;
862   }
863   bool isImm32() const {
864     if (!isImm()) return false;
865     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
866     if (!CE) return false;
867     int64_t Value = CE->getValue();
868     return Value == 32;
869   }
870   bool isShrImm8() const {
871     if (!isImm()) return false;
872     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
873     if (!CE) return false;
874     int64_t Value = CE->getValue();
875     return Value > 0 && Value <= 8;
876   }
877   bool isShrImm16() const {
878     if (!isImm()) return false;
879     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
880     if (!CE) return false;
881     int64_t Value = CE->getValue();
882     return Value > 0 && Value <= 16;
883   }
884   bool isShrImm32() const {
885     if (!isImm()) return false;
886     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
887     if (!CE) return false;
888     int64_t Value = CE->getValue();
889     return Value > 0 && Value <= 32;
890   }
891   bool isShrImm64() const {
892     if (!isImm()) return false;
893     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
894     if (!CE) return false;
895     int64_t Value = CE->getValue();
896     return Value > 0 && Value <= 64;
897   }
898   bool isImm1_7() const {
899     if (!isImm()) return false;
900     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
901     if (!CE) return false;
902     int64_t Value = CE->getValue();
903     return Value > 0 && Value < 8;
904   }
905   bool isImm1_15() const {
906     if (!isImm()) return false;
907     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
908     if (!CE) return false;
909     int64_t Value = CE->getValue();
910     return Value > 0 && Value < 16;
911   }
912   bool isImm1_31() const {
913     if (!isImm()) return false;
914     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
915     if (!CE) return false;
916     int64_t Value = CE->getValue();
917     return Value > 0 && Value < 32;
918   }
919   bool isImm1_16() const {
920     if (!isImm()) return false;
921     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
922     if (!CE) return false;
923     int64_t Value = CE->getValue();
924     return Value > 0 && Value < 17;
925   }
926   bool isImm1_32() const {
927     if (!isImm()) return false;
928     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
929     if (!CE) return false;
930     int64_t Value = CE->getValue();
931     return Value > 0 && Value < 33;
932   }
933   bool isImm0_32() const {
934     if (!isImm()) return false;
935     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
936     if (!CE) return false;
937     int64_t Value = CE->getValue();
938     return Value >= 0 && Value < 33;
939   }
940   bool isImm0_65535() const {
941     if (!isImm()) return false;
942     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
943     if (!CE) return false;
944     int64_t Value = CE->getValue();
945     return Value >= 0 && Value < 65536;
946   }
947   bool isImm256_65535Expr() const {
948     if (!isImm()) return false;
949     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
950     // If it's not a constant expression, it'll generate a fixup and be
951     // handled later.
952     if (!CE) return true;
953     int64_t Value = CE->getValue();
954     return Value >= 256 && Value < 65536;
955   }
956   bool isImm0_65535Expr() const {
957     if (!isImm()) return false;
958     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
959     // If it's not a constant expression, it'll generate a fixup and be
960     // handled later.
961     if (!CE) return true;
962     int64_t Value = CE->getValue();
963     return Value >= 0 && Value < 65536;
964   }
965   bool isImm24bit() const {
966     if (!isImm()) return false;
967     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
968     if (!CE) return false;
969     int64_t Value = CE->getValue();
970     return Value >= 0 && Value <= 0xffffff;
971   }
972   bool isImmThumbSR() const {
973     if (!isImm()) return false;
974     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
975     if (!CE) return false;
976     int64_t Value = CE->getValue();
977     return Value > 0 && Value < 33;
978   }
979   bool isPKHLSLImm() const {
980     if (!isImm()) return false;
981     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
982     if (!CE) return false;
983     int64_t Value = CE->getValue();
984     return Value >= 0 && Value < 32;
985   }
986   bool isPKHASRImm() const {
987     if (!isImm()) return false;
988     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
989     if (!CE) return false;
990     int64_t Value = CE->getValue();
991     return Value > 0 && Value <= 32;
992   }
993   bool isAdrLabel() const {
994     // If we have an immediate that's not a constant, treat it as a label
995     // reference needing a fixup.
996     if (isImm() && !isa<MCConstantExpr>(getImm()))
997       return true;
998 
999     // If it is a constant, it must fit into a modified immediate encoding.
1000     if (!isImm()) return false;
1001     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1002     if (!CE) return false;
1003     int64_t Value = CE->getValue();
1004     return (ARM_AM::getSOImmVal(Value) != -1 ||
1005             ARM_AM::getSOImmVal(-Value) != -1);
1006   }
1007   bool isT2SOImm() const {
1008     if (!isImm()) return false;
1009     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1010     if (!CE) return false;
1011     int64_t Value = CE->getValue();
1012     return ARM_AM::getT2SOImmVal(Value) != -1;
1013   }
1014   bool isT2SOImmNot() const {
1015     if (!isImm()) return false;
1016     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1017     if (!CE) return false;
1018     int64_t Value = CE->getValue();
1019     return ARM_AM::getT2SOImmVal(Value) == -1 &&
1020       ARM_AM::getT2SOImmVal(~Value) != -1;
1021   }
1022   bool isT2SOImmNeg() const {
1023     if (!isImm()) return false;
1024     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1025     if (!CE) return false;
1026     int64_t Value = CE->getValue();
1027     // Only use this when not representable as a plain so_imm.
1028     return ARM_AM::getT2SOImmVal(Value) == -1 &&
1029       ARM_AM::getT2SOImmVal(-Value) != -1;
1030   }
1031   bool isSetEndImm() const {
1032     if (!isImm()) return false;
1033     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1034     if (!CE) return false;
1035     int64_t Value = CE->getValue();
1036     return Value == 1 || Value == 0;
1037   }
1038   bool isReg() const override { return Kind == k_Register; }
1039   bool isRegList() const { return Kind == k_RegisterList; }
1040   bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
1041   bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
1042   bool isToken() const override { return Kind == k_Token; }
1043   bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
1044   bool isInstSyncBarrierOpt() const { return Kind == k_InstSyncBarrierOpt; }
1045   bool isMem() const override { return Kind == k_Memory; }
1046   bool isShifterImm() const { return Kind == k_ShifterImmediate; }
1047   bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
1048   bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
1049   bool isRotImm() const { return Kind == k_RotateImmediate; }
1050   bool isModImm() const { return Kind == k_ModifiedImmediate; }
1051   bool isModImmNot() const {
1052     if (!isImm()) return false;
1053     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1054     if (!CE) return false;
1055     int64_t Value = CE->getValue();
1056     return ARM_AM::getSOImmVal(~Value) != -1;
1057   }
1058   bool isModImmNeg() const {
1059     if (!isImm()) return false;
1060     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1061     if (!CE) return false;
1062     int64_t Value = CE->getValue();
1063     return ARM_AM::getSOImmVal(Value) == -1 &&
1064       ARM_AM::getSOImmVal(-Value) != -1;
1065   }
1066   bool isConstantPoolImm() const { return Kind == k_ConstantPoolImmediate; }
1067   bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
1068   bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
1069   bool isPostIdxReg() const {
1070     return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy ==ARM_AM::no_shift;
1071   }
1072   bool isMemNoOffset(bool alignOK = false, unsigned Alignment = 0) const {
1073     if (!isMem())
1074       return false;
1075     // No offset of any kind.
1076     return Memory.OffsetRegNum == 0 && Memory.OffsetImm == nullptr &&
1077      (alignOK || Memory.Alignment == Alignment);
1078   }
1079   bool isMemPCRelImm12() const {
1080     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1081       return false;
1082     // Base register must be PC.
1083     if (Memory.BaseRegNum != ARM::PC)
1084       return false;
1085     // Immediate offset in range [-4095, 4095].
1086     if (!Memory.OffsetImm) return true;
1087     int64_t Val = Memory.OffsetImm->getValue();
1088     return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
1089   }
1090   bool isAlignedMemory() const {
1091     return isMemNoOffset(true);
1092   }
1093   bool isAlignedMemoryNone() const {
1094     return isMemNoOffset(false, 0);
1095   }
1096   bool isDupAlignedMemoryNone() const {
1097     return isMemNoOffset(false, 0);
1098   }
1099   bool isAlignedMemory16() const {
1100     if (isMemNoOffset(false, 2)) // alignment in bytes for 16-bits is 2.
1101       return true;
1102     return isMemNoOffset(false, 0);
1103   }
1104   bool isDupAlignedMemory16() const {
1105     if (isMemNoOffset(false, 2)) // alignment in bytes for 16-bits is 2.
1106       return true;
1107     return isMemNoOffset(false, 0);
1108   }
1109   bool isAlignedMemory32() const {
1110     if (isMemNoOffset(false, 4)) // alignment in bytes for 32-bits is 4.
1111       return true;
1112     return isMemNoOffset(false, 0);
1113   }
1114   bool isDupAlignedMemory32() const {
1115     if (isMemNoOffset(false, 4)) // alignment in bytes for 32-bits is 4.
1116       return true;
1117     return isMemNoOffset(false, 0);
1118   }
1119   bool isAlignedMemory64() const {
1120     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1121       return true;
1122     return isMemNoOffset(false, 0);
1123   }
1124   bool isDupAlignedMemory64() const {
1125     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1126       return true;
1127     return isMemNoOffset(false, 0);
1128   }
1129   bool isAlignedMemory64or128() const {
1130     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1131       return true;
1132     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1133       return true;
1134     return isMemNoOffset(false, 0);
1135   }
1136   bool isDupAlignedMemory64or128() const {
1137     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1138       return true;
1139     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1140       return true;
1141     return isMemNoOffset(false, 0);
1142   }
1143   bool isAlignedMemory64or128or256() const {
1144     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1145       return true;
1146     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1147       return true;
1148     if (isMemNoOffset(false, 32)) // alignment in bytes for 256-bits is 32.
1149       return true;
1150     return isMemNoOffset(false, 0);
1151   }
1152   bool isAddrMode2() const {
1153     if (!isMem() || Memory.Alignment != 0) return false;
1154     // Check for register offset.
1155     if (Memory.OffsetRegNum) return true;
1156     // Immediate offset in range [-4095, 4095].
1157     if (!Memory.OffsetImm) return true;
1158     int64_t Val = Memory.OffsetImm->getValue();
1159     return Val > -4096 && Val < 4096;
1160   }
1161   bool isAM2OffsetImm() const {
1162     if (!isImm()) return false;
1163     // Immediate offset in range [-4095, 4095].
1164     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1165     if (!CE) return false;
1166     int64_t Val = CE->getValue();
1167     return (Val == INT32_MIN) || (Val > -4096 && Val < 4096);
1168   }
1169   bool isAddrMode3() const {
1170     // If we have an immediate that's not a constant, treat it as a label
1171     // reference needing a fixup. If it is a constant, it's something else
1172     // and we reject it.
1173     if (isImm() && !isa<MCConstantExpr>(getImm()))
1174       return true;
1175     if (!isMem() || Memory.Alignment != 0) return false;
1176     // No shifts are legal for AM3.
1177     if (Memory.ShiftType != ARM_AM::no_shift) return false;
1178     // Check for register offset.
1179     if (Memory.OffsetRegNum) return true;
1180     // Immediate offset in range [-255, 255].
1181     if (!Memory.OffsetImm) return true;
1182     int64_t Val = Memory.OffsetImm->getValue();
1183     // The #-0 offset is encoded as INT32_MIN, and we have to check
1184     // for this too.
1185     return (Val > -256 && Val < 256) || Val == INT32_MIN;
1186   }
1187   bool isAM3Offset() const {
1188     if (Kind != k_Immediate && Kind != k_PostIndexRegister)
1189       return false;
1190     if (Kind == k_PostIndexRegister)
1191       return PostIdxReg.ShiftTy == ARM_AM::no_shift;
1192     // Immediate offset in range [-255, 255].
1193     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1194     if (!CE) return false;
1195     int64_t Val = CE->getValue();
1196     // Special case, #-0 is INT32_MIN.
1197     return (Val > -256 && Val < 256) || Val == INT32_MIN;
1198   }
1199   bool isAddrMode5() const {
1200     // If we have an immediate that's not a constant, treat it as a label
1201     // reference needing a fixup. If it is a constant, it's something else
1202     // and we reject it.
1203     if (isImm() && !isa<MCConstantExpr>(getImm()))
1204       return true;
1205     if (!isMem() || Memory.Alignment != 0) return false;
1206     // Check for register offset.
1207     if (Memory.OffsetRegNum) return false;
1208     // Immediate offset in range [-1020, 1020] and a multiple of 4.
1209     if (!Memory.OffsetImm) return true;
1210     int64_t Val = Memory.OffsetImm->getValue();
1211     return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
1212       Val == INT32_MIN;
1213   }
1214   bool isAddrMode5FP16() const {
1215     // If we have an immediate that's not a constant, treat it as a label
1216     // reference needing a fixup. If it is a constant, it's something else
1217     // and we reject it.
1218     if (isImm() && !isa<MCConstantExpr>(getImm()))
1219       return true;
1220     if (!isMem() || Memory.Alignment != 0) return false;
1221     // Check for register offset.
1222     if (Memory.OffsetRegNum) return false;
1223     // Immediate offset in range [-510, 510] and a multiple of 2.
1224     if (!Memory.OffsetImm) return true;
1225     int64_t Val = Memory.OffsetImm->getValue();
1226     return (Val >= -510 && Val <= 510 && ((Val & 1) == 0)) || Val == INT32_MIN;
1227   }
1228   bool isMemTBB() const {
1229     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1230         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
1231       return false;
1232     return true;
1233   }
1234   bool isMemTBH() const {
1235     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1236         Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
1237         Memory.Alignment != 0 )
1238       return false;
1239     return true;
1240   }
1241   bool isMemRegOffset() const {
1242     if (!isMem() || !Memory.OffsetRegNum || Memory.Alignment != 0)
1243       return false;
1244     return true;
1245   }
1246   bool isT2MemRegOffset() const {
1247     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1248         Memory.Alignment != 0 || Memory.BaseRegNum == ARM::PC)
1249       return false;
1250     // Only lsl #{0, 1, 2, 3} allowed.
1251     if (Memory.ShiftType == ARM_AM::no_shift)
1252       return true;
1253     if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
1254       return false;
1255     return true;
1256   }
1257   bool isMemThumbRR() const {
1258     // Thumb reg+reg addressing is simple. Just two registers, a base and
1259     // an offset. No shifts, negations or any other complicating factors.
1260     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1261         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
1262       return false;
1263     return isARMLowRegister(Memory.BaseRegNum) &&
1264       (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
1265   }
1266   bool isMemThumbRIs4() const {
1267     if (!isMem() || Memory.OffsetRegNum != 0 ||
1268         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1269       return false;
1270     // Immediate offset, multiple of 4 in range [0, 124].
1271     if (!Memory.OffsetImm) return true;
1272     int64_t Val = Memory.OffsetImm->getValue();
1273     return Val >= 0 && Val <= 124 && (Val % 4) == 0;
1274   }
1275   bool isMemThumbRIs2() const {
1276     if (!isMem() || Memory.OffsetRegNum != 0 ||
1277         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1278       return false;
1279     // Immediate offset, multiple of 4 in range [0, 62].
1280     if (!Memory.OffsetImm) return true;
1281     int64_t Val = Memory.OffsetImm->getValue();
1282     return Val >= 0 && Val <= 62 && (Val % 2) == 0;
1283   }
1284   bool isMemThumbRIs1() const {
1285     if (!isMem() || Memory.OffsetRegNum != 0 ||
1286         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1287       return false;
1288     // Immediate offset in range [0, 31].
1289     if (!Memory.OffsetImm) return true;
1290     int64_t Val = Memory.OffsetImm->getValue();
1291     return Val >= 0 && Val <= 31;
1292   }
1293   bool isMemThumbSPI() const {
1294     if (!isMem() || Memory.OffsetRegNum != 0 ||
1295         Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
1296       return false;
1297     // Immediate offset, multiple of 4 in range [0, 1020].
1298     if (!Memory.OffsetImm) return true;
1299     int64_t Val = Memory.OffsetImm->getValue();
1300     return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
1301   }
1302   bool isMemImm8s4Offset() const {
1303     // If we have an immediate that's not a constant, treat it as a label
1304     // reference needing a fixup. If it is a constant, it's something else
1305     // and we reject it.
1306     if (isImm() && !isa<MCConstantExpr>(getImm()))
1307       return true;
1308     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1309       return false;
1310     // Immediate offset a multiple of 4 in range [-1020, 1020].
1311     if (!Memory.OffsetImm) return true;
1312     int64_t Val = Memory.OffsetImm->getValue();
1313     // Special case, #-0 is INT32_MIN.
1314     return (Val >= -1020 && Val <= 1020 && (Val & 3) == 0) || Val == INT32_MIN;
1315   }
1316   bool isMemImm0_1020s4Offset() const {
1317     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1318       return false;
1319     // Immediate offset a multiple of 4 in range [0, 1020].
1320     if (!Memory.OffsetImm) return true;
1321     int64_t Val = Memory.OffsetImm->getValue();
1322     return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
1323   }
1324   bool isMemImm8Offset() const {
1325     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1326       return false;
1327     // Base reg of PC isn't allowed for these encodings.
1328     if (Memory.BaseRegNum == ARM::PC) return false;
1329     // Immediate offset in range [-255, 255].
1330     if (!Memory.OffsetImm) return true;
1331     int64_t Val = Memory.OffsetImm->getValue();
1332     return (Val == INT32_MIN) || (Val > -256 && Val < 256);
1333   }
1334   bool isMemPosImm8Offset() const {
1335     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1336       return false;
1337     // Immediate offset in range [0, 255].
1338     if (!Memory.OffsetImm) return true;
1339     int64_t Val = Memory.OffsetImm->getValue();
1340     return Val >= 0 && Val < 256;
1341   }
1342   bool isMemNegImm8Offset() const {
1343     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1344       return false;
1345     // Base reg of PC isn't allowed for these encodings.
1346     if (Memory.BaseRegNum == ARM::PC) return false;
1347     // Immediate offset in range [-255, -1].
1348     if (!Memory.OffsetImm) return false;
1349     int64_t Val = Memory.OffsetImm->getValue();
1350     return (Val == INT32_MIN) || (Val > -256 && Val < 0);
1351   }
1352   bool isMemUImm12Offset() const {
1353     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1354       return false;
1355     // Immediate offset in range [0, 4095].
1356     if (!Memory.OffsetImm) return true;
1357     int64_t Val = Memory.OffsetImm->getValue();
1358     return (Val >= 0 && Val < 4096);
1359   }
1360   bool isMemImm12Offset() const {
1361     // If we have an immediate that's not a constant, treat it as a label
1362     // reference needing a fixup. If it is a constant, it's something else
1363     // and we reject it.
1364 
1365     if (isImm() && !isa<MCConstantExpr>(getImm()))
1366       return true;
1367 
1368     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1369       return false;
1370     // Immediate offset in range [-4095, 4095].
1371     if (!Memory.OffsetImm) return true;
1372     int64_t Val = Memory.OffsetImm->getValue();
1373     return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
1374   }
1375   bool isConstPoolAsmImm() const {
1376     // Delay processing of Constant Pool Immediate, this will turn into
1377     // a constant. Match no other operand
1378     return (isConstantPoolImm());
1379   }
1380   bool isPostIdxImm8() const {
1381     if (!isImm()) return false;
1382     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1383     if (!CE) return false;
1384     int64_t Val = CE->getValue();
1385     return (Val > -256 && Val < 256) || (Val == INT32_MIN);
1386   }
1387   bool isPostIdxImm8s4() const {
1388     if (!isImm()) return false;
1389     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1390     if (!CE) return false;
1391     int64_t Val = CE->getValue();
1392     return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
1393       (Val == INT32_MIN);
1394   }
1395 
1396   bool isMSRMask() const { return Kind == k_MSRMask; }
1397   bool isBankedReg() const { return Kind == k_BankedReg; }
1398   bool isProcIFlags() const { return Kind == k_ProcIFlags; }
1399 
1400   // NEON operands.
1401   bool isSingleSpacedVectorList() const {
1402     return Kind == k_VectorList && !VectorList.isDoubleSpaced;
1403   }
1404   bool isDoubleSpacedVectorList() const {
1405     return Kind == k_VectorList && VectorList.isDoubleSpaced;
1406   }
1407   bool isVecListOneD() const {
1408     if (!isSingleSpacedVectorList()) return false;
1409     return VectorList.Count == 1;
1410   }
1411 
1412   bool isVecListDPair() const {
1413     if (!isSingleSpacedVectorList()) return false;
1414     return (ARMMCRegisterClasses[ARM::DPairRegClassID]
1415               .contains(VectorList.RegNum));
1416   }
1417 
1418   bool isVecListThreeD() const {
1419     if (!isSingleSpacedVectorList()) return false;
1420     return VectorList.Count == 3;
1421   }
1422 
1423   bool isVecListFourD() const {
1424     if (!isSingleSpacedVectorList()) return false;
1425     return VectorList.Count == 4;
1426   }
1427 
1428   bool isVecListDPairSpaced() const {
1429     if (Kind != k_VectorList) return false;
1430     if (isSingleSpacedVectorList()) return false;
1431     return (ARMMCRegisterClasses[ARM::DPairSpcRegClassID]
1432               .contains(VectorList.RegNum));
1433   }
1434 
1435   bool isVecListThreeQ() const {
1436     if (!isDoubleSpacedVectorList()) return false;
1437     return VectorList.Count == 3;
1438   }
1439 
1440   bool isVecListFourQ() const {
1441     if (!isDoubleSpacedVectorList()) return false;
1442     return VectorList.Count == 4;
1443   }
1444 
1445   bool isSingleSpacedVectorAllLanes() const {
1446     return Kind == k_VectorListAllLanes && !VectorList.isDoubleSpaced;
1447   }
1448   bool isDoubleSpacedVectorAllLanes() const {
1449     return Kind == k_VectorListAllLanes && VectorList.isDoubleSpaced;
1450   }
1451   bool isVecListOneDAllLanes() const {
1452     if (!isSingleSpacedVectorAllLanes()) return false;
1453     return VectorList.Count == 1;
1454   }
1455 
1456   bool isVecListDPairAllLanes() const {
1457     if (!isSingleSpacedVectorAllLanes()) return false;
1458     return (ARMMCRegisterClasses[ARM::DPairRegClassID]
1459               .contains(VectorList.RegNum));
1460   }
1461 
1462   bool isVecListDPairSpacedAllLanes() const {
1463     if (!isDoubleSpacedVectorAllLanes()) return false;
1464     return VectorList.Count == 2;
1465   }
1466 
1467   bool isVecListThreeDAllLanes() const {
1468     if (!isSingleSpacedVectorAllLanes()) return false;
1469     return VectorList.Count == 3;
1470   }
1471 
1472   bool isVecListThreeQAllLanes() const {
1473     if (!isDoubleSpacedVectorAllLanes()) return false;
1474     return VectorList.Count == 3;
1475   }
1476 
1477   bool isVecListFourDAllLanes() const {
1478     if (!isSingleSpacedVectorAllLanes()) return false;
1479     return VectorList.Count == 4;
1480   }
1481 
1482   bool isVecListFourQAllLanes() const {
1483     if (!isDoubleSpacedVectorAllLanes()) return false;
1484     return VectorList.Count == 4;
1485   }
1486 
1487   bool isSingleSpacedVectorIndexed() const {
1488     return Kind == k_VectorListIndexed && !VectorList.isDoubleSpaced;
1489   }
1490   bool isDoubleSpacedVectorIndexed() const {
1491     return Kind == k_VectorListIndexed && VectorList.isDoubleSpaced;
1492   }
1493   bool isVecListOneDByteIndexed() const {
1494     if (!isSingleSpacedVectorIndexed()) return false;
1495     return VectorList.Count == 1 && VectorList.LaneIndex <= 7;
1496   }
1497 
1498   bool isVecListOneDHWordIndexed() const {
1499     if (!isSingleSpacedVectorIndexed()) return false;
1500     return VectorList.Count == 1 && VectorList.LaneIndex <= 3;
1501   }
1502 
1503   bool isVecListOneDWordIndexed() const {
1504     if (!isSingleSpacedVectorIndexed()) return false;
1505     return VectorList.Count == 1 && VectorList.LaneIndex <= 1;
1506   }
1507 
1508   bool isVecListTwoDByteIndexed() const {
1509     if (!isSingleSpacedVectorIndexed()) return false;
1510     return VectorList.Count == 2 && VectorList.LaneIndex <= 7;
1511   }
1512 
1513   bool isVecListTwoDHWordIndexed() const {
1514     if (!isSingleSpacedVectorIndexed()) return false;
1515     return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
1516   }
1517 
1518   bool isVecListTwoQWordIndexed() const {
1519     if (!isDoubleSpacedVectorIndexed()) return false;
1520     return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
1521   }
1522 
1523   bool isVecListTwoQHWordIndexed() const {
1524     if (!isDoubleSpacedVectorIndexed()) return false;
1525     return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
1526   }
1527 
1528   bool isVecListTwoDWordIndexed() const {
1529     if (!isSingleSpacedVectorIndexed()) return false;
1530     return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
1531   }
1532 
1533   bool isVecListThreeDByteIndexed() const {
1534     if (!isSingleSpacedVectorIndexed()) return false;
1535     return VectorList.Count == 3 && VectorList.LaneIndex <= 7;
1536   }
1537 
1538   bool isVecListThreeDHWordIndexed() const {
1539     if (!isSingleSpacedVectorIndexed()) return false;
1540     return VectorList.Count == 3 && VectorList.LaneIndex <= 3;
1541   }
1542 
1543   bool isVecListThreeQWordIndexed() const {
1544     if (!isDoubleSpacedVectorIndexed()) return false;
1545     return VectorList.Count == 3 && VectorList.LaneIndex <= 1;
1546   }
1547 
1548   bool isVecListThreeQHWordIndexed() const {
1549     if (!isDoubleSpacedVectorIndexed()) return false;
1550     return VectorList.Count == 3 && VectorList.LaneIndex <= 3;
1551   }
1552 
1553   bool isVecListThreeDWordIndexed() const {
1554     if (!isSingleSpacedVectorIndexed()) return false;
1555     return VectorList.Count == 3 && VectorList.LaneIndex <= 1;
1556   }
1557 
1558   bool isVecListFourDByteIndexed() const {
1559     if (!isSingleSpacedVectorIndexed()) return false;
1560     return VectorList.Count == 4 && VectorList.LaneIndex <= 7;
1561   }
1562 
1563   bool isVecListFourDHWordIndexed() const {
1564     if (!isSingleSpacedVectorIndexed()) return false;
1565     return VectorList.Count == 4 && VectorList.LaneIndex <= 3;
1566   }
1567 
1568   bool isVecListFourQWordIndexed() const {
1569     if (!isDoubleSpacedVectorIndexed()) return false;
1570     return VectorList.Count == 4 && VectorList.LaneIndex <= 1;
1571   }
1572 
1573   bool isVecListFourQHWordIndexed() const {
1574     if (!isDoubleSpacedVectorIndexed()) return false;
1575     return VectorList.Count == 4 && VectorList.LaneIndex <= 3;
1576   }
1577 
1578   bool isVecListFourDWordIndexed() const {
1579     if (!isSingleSpacedVectorIndexed()) return false;
1580     return VectorList.Count == 4 && VectorList.LaneIndex <= 1;
1581   }
1582 
1583   bool isVectorIndex8() const {
1584     if (Kind != k_VectorIndex) return false;
1585     return VectorIndex.Val < 8;
1586   }
1587   bool isVectorIndex16() const {
1588     if (Kind != k_VectorIndex) return false;
1589     return VectorIndex.Val < 4;
1590   }
1591   bool isVectorIndex32() const {
1592     if (Kind != k_VectorIndex) return false;
1593     return VectorIndex.Val < 2;
1594   }
1595 
1596   bool isNEONi8splat() const {
1597     if (!isImm()) return false;
1598     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1599     // Must be a constant.
1600     if (!CE) return false;
1601     int64_t Value = CE->getValue();
1602     // i8 value splatted across 8 bytes. The immediate is just the 8 byte
1603     // value.
1604     return Value >= 0 && Value < 256;
1605   }
1606 
1607   bool isNEONi16splat() const {
1608     if (isNEONByteReplicate(2))
1609       return false; // Leave that for bytes replication and forbid by default.
1610     if (!isImm())
1611       return false;
1612     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1613     // Must be a constant.
1614     if (!CE) return false;
1615     unsigned Value = CE->getValue();
1616     return ARM_AM::isNEONi16splat(Value);
1617   }
1618 
1619   bool isNEONi16splatNot() const {
1620     if (!isImm())
1621       return false;
1622     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1623     // Must be a constant.
1624     if (!CE) return false;
1625     unsigned Value = CE->getValue();
1626     return ARM_AM::isNEONi16splat(~Value & 0xffff);
1627   }
1628 
1629   bool isNEONi32splat() const {
1630     if (isNEONByteReplicate(4))
1631       return false; // Leave that for bytes replication and forbid by default.
1632     if (!isImm())
1633       return false;
1634     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1635     // Must be a constant.
1636     if (!CE) return false;
1637     unsigned Value = CE->getValue();
1638     return ARM_AM::isNEONi32splat(Value);
1639   }
1640 
1641   bool isNEONi32splatNot() const {
1642     if (!isImm())
1643       return false;
1644     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1645     // Must be a constant.
1646     if (!CE) return false;
1647     unsigned Value = CE->getValue();
1648     return ARM_AM::isNEONi32splat(~Value);
1649   }
1650 
1651   bool isNEONByteReplicate(unsigned NumBytes) const {
1652     if (!isImm())
1653       return false;
1654     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1655     // Must be a constant.
1656     if (!CE)
1657       return false;
1658     int64_t Value = CE->getValue();
1659     if (!Value)
1660       return false; // Don't bother with zero.
1661 
1662     unsigned char B = Value & 0xff;
1663     for (unsigned i = 1; i < NumBytes; ++i) {
1664       Value >>= 8;
1665       if ((Value & 0xff) != B)
1666         return false;
1667     }
1668     return true;
1669   }
1670   bool isNEONi16ByteReplicate() const { return isNEONByteReplicate(2); }
1671   bool isNEONi32ByteReplicate() const { return isNEONByteReplicate(4); }
1672   bool isNEONi32vmov() const {
1673     if (isNEONByteReplicate(4))
1674       return false; // Let it to be classified as byte-replicate case.
1675     if (!isImm())
1676       return false;
1677     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1678     // Must be a constant.
1679     if (!CE)
1680       return false;
1681     int64_t Value = CE->getValue();
1682     // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1683     // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1684     // FIXME: This is probably wrong and a copy and paste from previous example
1685     return (Value >= 0 && Value < 256) ||
1686       (Value >= 0x0100 && Value <= 0xff00) ||
1687       (Value >= 0x010000 && Value <= 0xff0000) ||
1688       (Value >= 0x01000000 && Value <= 0xff000000) ||
1689       (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1690       (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1691   }
1692   bool isNEONi32vmovNeg() const {
1693     if (!isImm()) return false;
1694     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1695     // Must be a constant.
1696     if (!CE) return false;
1697     int64_t Value = ~CE->getValue();
1698     // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1699     // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1700     // FIXME: This is probably wrong and a copy and paste from previous example
1701     return (Value >= 0 && Value < 256) ||
1702       (Value >= 0x0100 && Value <= 0xff00) ||
1703       (Value >= 0x010000 && Value <= 0xff0000) ||
1704       (Value >= 0x01000000 && Value <= 0xff000000) ||
1705       (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1706       (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1707   }
1708 
1709   bool isNEONi64splat() const {
1710     if (!isImm()) return false;
1711     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1712     // Must be a constant.
1713     if (!CE) return false;
1714     uint64_t Value = CE->getValue();
1715     // i64 value with each byte being either 0 or 0xff.
1716     for (unsigned i = 0; i < 8; ++i)
1717       if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
1718     return true;
1719   }
1720 
1721   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
1722     // Add as immediates when possible.  Null MCExpr = 0.
1723     if (!Expr)
1724       Inst.addOperand(MCOperand::createImm(0));
1725     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
1726       Inst.addOperand(MCOperand::createImm(CE->getValue()));
1727     else
1728       Inst.addOperand(MCOperand::createExpr(Expr));
1729   }
1730 
1731   void addCondCodeOperands(MCInst &Inst, unsigned N) const {
1732     assert(N == 2 && "Invalid number of operands!");
1733     Inst.addOperand(MCOperand::createImm(unsigned(getCondCode())));
1734     unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
1735     Inst.addOperand(MCOperand::createReg(RegNum));
1736   }
1737 
1738   void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
1739     assert(N == 1 && "Invalid number of operands!");
1740     Inst.addOperand(MCOperand::createImm(getCoproc()));
1741   }
1742 
1743   void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
1744     assert(N == 1 && "Invalid number of operands!");
1745     Inst.addOperand(MCOperand::createImm(getCoproc()));
1746   }
1747 
1748   void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
1749     assert(N == 1 && "Invalid number of operands!");
1750     Inst.addOperand(MCOperand::createImm(CoprocOption.Val));
1751   }
1752 
1753   void addITMaskOperands(MCInst &Inst, unsigned N) const {
1754     assert(N == 1 && "Invalid number of operands!");
1755     Inst.addOperand(MCOperand::createImm(ITMask.Mask));
1756   }
1757 
1758   void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
1759     assert(N == 1 && "Invalid number of operands!");
1760     Inst.addOperand(MCOperand::createImm(unsigned(getCondCode())));
1761   }
1762 
1763   void addCCOutOperands(MCInst &Inst, unsigned N) const {
1764     assert(N == 1 && "Invalid number of operands!");
1765     Inst.addOperand(MCOperand::createReg(getReg()));
1766   }
1767 
1768   void addRegOperands(MCInst &Inst, unsigned N) const {
1769     assert(N == 1 && "Invalid number of operands!");
1770     Inst.addOperand(MCOperand::createReg(getReg()));
1771   }
1772 
1773   void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
1774     assert(N == 3 && "Invalid number of operands!");
1775     assert(isRegShiftedReg() &&
1776            "addRegShiftedRegOperands() on non-RegShiftedReg!");
1777     Inst.addOperand(MCOperand::createReg(RegShiftedReg.SrcReg));
1778     Inst.addOperand(MCOperand::createReg(RegShiftedReg.ShiftReg));
1779     Inst.addOperand(MCOperand::createImm(
1780       ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
1781   }
1782 
1783   void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
1784     assert(N == 2 && "Invalid number of operands!");
1785     assert(isRegShiftedImm() &&
1786            "addRegShiftedImmOperands() on non-RegShiftedImm!");
1787     Inst.addOperand(MCOperand::createReg(RegShiftedImm.SrcReg));
1788     // Shift of #32 is encoded as 0 where permitted
1789     unsigned Imm = (RegShiftedImm.ShiftImm == 32 ? 0 : RegShiftedImm.ShiftImm);
1790     Inst.addOperand(MCOperand::createImm(
1791       ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, Imm)));
1792   }
1793 
1794   void addShifterImmOperands(MCInst &Inst, unsigned N) const {
1795     assert(N == 1 && "Invalid number of operands!");
1796     Inst.addOperand(MCOperand::createImm((ShifterImm.isASR << 5) |
1797                                          ShifterImm.Imm));
1798   }
1799 
1800   void addRegListOperands(MCInst &Inst, unsigned N) const {
1801     assert(N == 1 && "Invalid number of operands!");
1802     const SmallVectorImpl<unsigned> &RegList = getRegList();
1803     for (SmallVectorImpl<unsigned>::const_iterator
1804            I = RegList.begin(), E = RegList.end(); I != E; ++I)
1805       Inst.addOperand(MCOperand::createReg(*I));
1806   }
1807 
1808   void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
1809     addRegListOperands(Inst, N);
1810   }
1811 
1812   void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
1813     addRegListOperands(Inst, N);
1814   }
1815 
1816   void addRotImmOperands(MCInst &Inst, unsigned N) const {
1817     assert(N == 1 && "Invalid number of operands!");
1818     // Encoded as val>>3. The printer handles display as 8, 16, 24.
1819     Inst.addOperand(MCOperand::createImm(RotImm.Imm >> 3));
1820   }
1821 
1822   void addModImmOperands(MCInst &Inst, unsigned N) const {
1823     assert(N == 1 && "Invalid number of operands!");
1824 
1825     // Support for fixups (MCFixup)
1826     if (isImm())
1827       return addImmOperands(Inst, N);
1828 
1829     Inst.addOperand(MCOperand::createImm(ModImm.Bits | (ModImm.Rot << 7)));
1830   }
1831 
1832   void addModImmNotOperands(MCInst &Inst, unsigned N) const {
1833     assert(N == 1 && "Invalid number of operands!");
1834     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1835     uint32_t Enc = ARM_AM::getSOImmVal(~CE->getValue());
1836     Inst.addOperand(MCOperand::createImm(Enc));
1837   }
1838 
1839   void addModImmNegOperands(MCInst &Inst, unsigned N) const {
1840     assert(N == 1 && "Invalid number of operands!");
1841     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1842     uint32_t Enc = ARM_AM::getSOImmVal(-CE->getValue());
1843     Inst.addOperand(MCOperand::createImm(Enc));
1844   }
1845 
1846   void addBitfieldOperands(MCInst &Inst, unsigned N) const {
1847     assert(N == 1 && "Invalid number of operands!");
1848     // Munge the lsb/width into a bitfield mask.
1849     unsigned lsb = Bitfield.LSB;
1850     unsigned width = Bitfield.Width;
1851     // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
1852     uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
1853                       (32 - (lsb + width)));
1854     Inst.addOperand(MCOperand::createImm(Mask));
1855   }
1856 
1857   void addImmOperands(MCInst &Inst, unsigned N) const {
1858     assert(N == 1 && "Invalid number of operands!");
1859     addExpr(Inst, getImm());
1860   }
1861 
1862   void addFBits16Operands(MCInst &Inst, unsigned N) const {
1863     assert(N == 1 && "Invalid number of operands!");
1864     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1865     Inst.addOperand(MCOperand::createImm(16 - CE->getValue()));
1866   }
1867 
1868   void addFBits32Operands(MCInst &Inst, unsigned N) const {
1869     assert(N == 1 && "Invalid number of operands!");
1870     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1871     Inst.addOperand(MCOperand::createImm(32 - CE->getValue()));
1872   }
1873 
1874   void addFPImmOperands(MCInst &Inst, unsigned N) const {
1875     assert(N == 1 && "Invalid number of operands!");
1876     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1877     int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
1878     Inst.addOperand(MCOperand::createImm(Val));
1879   }
1880 
1881   void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1882     assert(N == 1 && "Invalid number of operands!");
1883     // FIXME: We really want to scale the value here, but the LDRD/STRD
1884     // instruction don't encode operands that way yet.
1885     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1886     Inst.addOperand(MCOperand::createImm(CE->getValue()));
1887   }
1888 
1889   void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1890     assert(N == 1 && "Invalid number of operands!");
1891     // The immediate is scaled by four in the encoding and is stored
1892     // in the MCInst as such. Lop off the low two bits here.
1893     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1894     Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
1895   }
1896 
1897   void addImm0_508s4NegOperands(MCInst &Inst, unsigned N) const {
1898     assert(N == 1 && "Invalid number of operands!");
1899     // The immediate is scaled by four in the encoding and is stored
1900     // in the MCInst as such. Lop off the low two bits here.
1901     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1902     Inst.addOperand(MCOperand::createImm(-(CE->getValue() / 4)));
1903   }
1904 
1905   void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1906     assert(N == 1 && "Invalid number of operands!");
1907     // The immediate is scaled by four in the encoding and is stored
1908     // in the MCInst as such. Lop off the low two bits here.
1909     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1910     Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
1911   }
1912 
1913   void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1914     assert(N == 1 && "Invalid number of operands!");
1915     // The constant encodes as the immediate-1, and we store in the instruction
1916     // the bits as encoded, so subtract off one here.
1917     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1918     Inst.addOperand(MCOperand::createImm(CE->getValue() - 1));
1919   }
1920 
1921   void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1922     assert(N == 1 && "Invalid number of operands!");
1923     // The constant encodes as the immediate-1, and we store in the instruction
1924     // the bits as encoded, so subtract off one here.
1925     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1926     Inst.addOperand(MCOperand::createImm(CE->getValue() - 1));
1927   }
1928 
1929   void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
1930     assert(N == 1 && "Invalid number of operands!");
1931     // The constant encodes as the immediate, except for 32, which encodes as
1932     // zero.
1933     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1934     unsigned Imm = CE->getValue();
1935     Inst.addOperand(MCOperand::createImm((Imm == 32 ? 0 : Imm)));
1936   }
1937 
1938   void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
1939     assert(N == 1 && "Invalid number of operands!");
1940     // An ASR value of 32 encodes as 0, so that's how we want to add it to
1941     // the instruction as well.
1942     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1943     int Val = CE->getValue();
1944     Inst.addOperand(MCOperand::createImm(Val == 32 ? 0 : Val));
1945   }
1946 
1947   void addT2SOImmNotOperands(MCInst &Inst, unsigned N) const {
1948     assert(N == 1 && "Invalid number of operands!");
1949     // The operand is actually a t2_so_imm, but we have its bitwise
1950     // negation in the assembly source, so twiddle it here.
1951     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1952     Inst.addOperand(MCOperand::createImm(~CE->getValue()));
1953   }
1954 
1955   void addT2SOImmNegOperands(MCInst &Inst, unsigned N) const {
1956     assert(N == 1 && "Invalid number of operands!");
1957     // The operand is actually a t2_so_imm, but we have its
1958     // negation in the assembly source, so twiddle it here.
1959     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1960     Inst.addOperand(MCOperand::createImm(-CE->getValue()));
1961   }
1962 
1963   void addImm0_4095NegOperands(MCInst &Inst, unsigned N) const {
1964     assert(N == 1 && "Invalid number of operands!");
1965     // The operand is actually an imm0_4095, but we have its
1966     // negation in the assembly source, so twiddle it here.
1967     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1968     Inst.addOperand(MCOperand::createImm(-CE->getValue()));
1969   }
1970 
1971   void addUnsignedOffset_b8s2Operands(MCInst &Inst, unsigned N) const {
1972     if(const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
1973       Inst.addOperand(MCOperand::createImm(CE->getValue() >> 2));
1974       return;
1975     }
1976 
1977     const MCSymbolRefExpr *SR = dyn_cast<MCSymbolRefExpr>(Imm.Val);
1978     assert(SR && "Unknown value type!");
1979     Inst.addOperand(MCOperand::createExpr(SR));
1980   }
1981 
1982   void addThumbMemPCOperands(MCInst &Inst, unsigned N) const {
1983     assert(N == 1 && "Invalid number of operands!");
1984     if (isImm()) {
1985       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1986       if (CE) {
1987         Inst.addOperand(MCOperand::createImm(CE->getValue()));
1988         return;
1989       }
1990 
1991       const MCSymbolRefExpr *SR = dyn_cast<MCSymbolRefExpr>(Imm.Val);
1992 
1993       assert(SR && "Unknown value type!");
1994       Inst.addOperand(MCOperand::createExpr(SR));
1995       return;
1996     }
1997 
1998     assert(isMem()  && "Unknown value type!");
1999     assert(isa<MCConstantExpr>(Memory.OffsetImm) && "Unknown value type!");
2000     Inst.addOperand(MCOperand::createImm(Memory.OffsetImm->getValue()));
2001   }
2002 
2003   void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
2004     assert(N == 1 && "Invalid number of operands!");
2005     Inst.addOperand(MCOperand::createImm(unsigned(getMemBarrierOpt())));
2006   }
2007 
2008   void addInstSyncBarrierOptOperands(MCInst &Inst, unsigned N) const {
2009     assert(N == 1 && "Invalid number of operands!");
2010     Inst.addOperand(MCOperand::createImm(unsigned(getInstSyncBarrierOpt())));
2011   }
2012 
2013   void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
2014     assert(N == 1 && "Invalid number of operands!");
2015     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2016   }
2017 
2018   void addMemPCRelImm12Operands(MCInst &Inst, unsigned N) const {
2019     assert(N == 1 && "Invalid number of operands!");
2020     int32_t Imm = Memory.OffsetImm->getValue();
2021     Inst.addOperand(MCOperand::createImm(Imm));
2022   }
2023 
2024   void addAdrLabelOperands(MCInst &Inst, unsigned N) const {
2025     assert(N == 1 && "Invalid number of operands!");
2026     assert(isImm() && "Not an immediate!");
2027 
2028     // If we have an immediate that's not a constant, treat it as a label
2029     // reference needing a fixup.
2030     if (!isa<MCConstantExpr>(getImm())) {
2031       Inst.addOperand(MCOperand::createExpr(getImm()));
2032       return;
2033     }
2034 
2035     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2036     int Val = CE->getValue();
2037     Inst.addOperand(MCOperand::createImm(Val));
2038   }
2039 
2040   void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
2041     assert(N == 2 && "Invalid number of operands!");
2042     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2043     Inst.addOperand(MCOperand::createImm(Memory.Alignment));
2044   }
2045 
2046   void addDupAlignedMemoryNoneOperands(MCInst &Inst, unsigned N) const {
2047     addAlignedMemoryOperands(Inst, N);
2048   }
2049 
2050   void addAlignedMemoryNoneOperands(MCInst &Inst, unsigned N) const {
2051     addAlignedMemoryOperands(Inst, N);
2052   }
2053 
2054   void addAlignedMemory16Operands(MCInst &Inst, unsigned N) const {
2055     addAlignedMemoryOperands(Inst, N);
2056   }
2057 
2058   void addDupAlignedMemory16Operands(MCInst &Inst, unsigned N) const {
2059     addAlignedMemoryOperands(Inst, N);
2060   }
2061 
2062   void addAlignedMemory32Operands(MCInst &Inst, unsigned N) const {
2063     addAlignedMemoryOperands(Inst, N);
2064   }
2065 
2066   void addDupAlignedMemory32Operands(MCInst &Inst, unsigned N) const {
2067     addAlignedMemoryOperands(Inst, N);
2068   }
2069 
2070   void addAlignedMemory64Operands(MCInst &Inst, unsigned N) const {
2071     addAlignedMemoryOperands(Inst, N);
2072   }
2073 
2074   void addDupAlignedMemory64Operands(MCInst &Inst, unsigned N) const {
2075     addAlignedMemoryOperands(Inst, N);
2076   }
2077 
2078   void addAlignedMemory64or128Operands(MCInst &Inst, unsigned N) const {
2079     addAlignedMemoryOperands(Inst, N);
2080   }
2081 
2082   void addDupAlignedMemory64or128Operands(MCInst &Inst, unsigned N) const {
2083     addAlignedMemoryOperands(Inst, N);
2084   }
2085 
2086   void addAlignedMemory64or128or256Operands(MCInst &Inst, unsigned N) const {
2087     addAlignedMemoryOperands(Inst, N);
2088   }
2089 
2090   void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
2091     assert(N == 3 && "Invalid number of operands!");
2092     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2093     if (!Memory.OffsetRegNum) {
2094       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2095       // Special case for #-0
2096       if (Val == INT32_MIN) Val = 0;
2097       if (Val < 0) Val = -Val;
2098       Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
2099     } else {
2100       // For register offset, we encode the shift type and negation flag
2101       // here.
2102       Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
2103                               Memory.ShiftImm, Memory.ShiftType);
2104     }
2105     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2106     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2107     Inst.addOperand(MCOperand::createImm(Val));
2108   }
2109 
2110   void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
2111     assert(N == 2 && "Invalid number of operands!");
2112     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2113     assert(CE && "non-constant AM2OffsetImm operand!");
2114     int32_t Val = CE->getValue();
2115     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2116     // Special case for #-0
2117     if (Val == INT32_MIN) Val = 0;
2118     if (Val < 0) Val = -Val;
2119     Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
2120     Inst.addOperand(MCOperand::createReg(0));
2121     Inst.addOperand(MCOperand::createImm(Val));
2122   }
2123 
2124   void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
2125     assert(N == 3 && "Invalid number of operands!");
2126     // If we have an immediate that's not a constant, treat it as a label
2127     // reference needing a fixup. If it is a constant, it's something else
2128     // and we reject it.
2129     if (isImm()) {
2130       Inst.addOperand(MCOperand::createExpr(getImm()));
2131       Inst.addOperand(MCOperand::createReg(0));
2132       Inst.addOperand(MCOperand::createImm(0));
2133       return;
2134     }
2135 
2136     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2137     if (!Memory.OffsetRegNum) {
2138       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2139       // Special case for #-0
2140       if (Val == INT32_MIN) Val = 0;
2141       if (Val < 0) Val = -Val;
2142       Val = ARM_AM::getAM3Opc(AddSub, Val);
2143     } else {
2144       // For register offset, we encode the shift type and negation flag
2145       // here.
2146       Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
2147     }
2148     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2149     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2150     Inst.addOperand(MCOperand::createImm(Val));
2151   }
2152 
2153   void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
2154     assert(N == 2 && "Invalid number of operands!");
2155     if (Kind == k_PostIndexRegister) {
2156       int32_t Val =
2157         ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
2158       Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
2159       Inst.addOperand(MCOperand::createImm(Val));
2160       return;
2161     }
2162 
2163     // Constant offset.
2164     const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
2165     int32_t Val = CE->getValue();
2166     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2167     // Special case for #-0
2168     if (Val == INT32_MIN) Val = 0;
2169     if (Val < 0) Val = -Val;
2170     Val = ARM_AM::getAM3Opc(AddSub, Val);
2171     Inst.addOperand(MCOperand::createReg(0));
2172     Inst.addOperand(MCOperand::createImm(Val));
2173   }
2174 
2175   void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
2176     assert(N == 2 && "Invalid number of operands!");
2177     // If we have an immediate that's not a constant, treat it as a label
2178     // reference needing a fixup. If it is a constant, it's something else
2179     // and we reject it.
2180     if (isImm()) {
2181       Inst.addOperand(MCOperand::createExpr(getImm()));
2182       Inst.addOperand(MCOperand::createImm(0));
2183       return;
2184     }
2185 
2186     // The lower two bits are always zero and as such are not encoded.
2187     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
2188     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2189     // Special case for #-0
2190     if (Val == INT32_MIN) Val = 0;
2191     if (Val < 0) Val = -Val;
2192     Val = ARM_AM::getAM5Opc(AddSub, Val);
2193     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2194     Inst.addOperand(MCOperand::createImm(Val));
2195   }
2196 
2197   void addAddrMode5FP16Operands(MCInst &Inst, unsigned N) const {
2198     assert(N == 2 && "Invalid number of operands!");
2199     // If we have an immediate that's not a constant, treat it as a label
2200     // reference needing a fixup. If it is a constant, it's something else
2201     // and we reject it.
2202     if (isImm()) {
2203       Inst.addOperand(MCOperand::createExpr(getImm()));
2204       Inst.addOperand(MCOperand::createImm(0));
2205       return;
2206     }
2207 
2208     // The lower bit is always zero and as such is not encoded.
2209     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 2 : 0;
2210     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2211     // Special case for #-0
2212     if (Val == INT32_MIN) Val = 0;
2213     if (Val < 0) Val = -Val;
2214     Val = ARM_AM::getAM5FP16Opc(AddSub, Val);
2215     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2216     Inst.addOperand(MCOperand::createImm(Val));
2217   }
2218 
2219   void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
2220     assert(N == 2 && "Invalid number of operands!");
2221     // If we have an immediate that's not a constant, treat it as a label
2222     // reference needing a fixup. If it is a constant, it's something else
2223     // and we reject it.
2224     if (isImm()) {
2225       Inst.addOperand(MCOperand::createExpr(getImm()));
2226       Inst.addOperand(MCOperand::createImm(0));
2227       return;
2228     }
2229 
2230     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2231     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2232     Inst.addOperand(MCOperand::createImm(Val));
2233   }
2234 
2235   void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
2236     assert(N == 2 && "Invalid number of operands!");
2237     // The lower two bits are always zero and as such are not encoded.
2238     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
2239     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2240     Inst.addOperand(MCOperand::createImm(Val));
2241   }
2242 
2243   void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
2244     assert(N == 2 && "Invalid number of operands!");
2245     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2246     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2247     Inst.addOperand(MCOperand::createImm(Val));
2248   }
2249 
2250   void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
2251     addMemImm8OffsetOperands(Inst, N);
2252   }
2253 
2254   void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
2255     addMemImm8OffsetOperands(Inst, N);
2256   }
2257 
2258   void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
2259     assert(N == 2 && "Invalid number of operands!");
2260     // If this is an immediate, it's a label reference.
2261     if (isImm()) {
2262       addExpr(Inst, getImm());
2263       Inst.addOperand(MCOperand::createImm(0));
2264       return;
2265     }
2266 
2267     // Otherwise, it's a normal memory reg+offset.
2268     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2269     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2270     Inst.addOperand(MCOperand::createImm(Val));
2271   }
2272 
2273   void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
2274     assert(N == 2 && "Invalid number of operands!");
2275     // If this is an immediate, it's a label reference.
2276     if (isImm()) {
2277       addExpr(Inst, getImm());
2278       Inst.addOperand(MCOperand::createImm(0));
2279       return;
2280     }
2281 
2282     // Otherwise, it's a normal memory reg+offset.
2283     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2284     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2285     Inst.addOperand(MCOperand::createImm(Val));
2286   }
2287 
2288   void addConstPoolAsmImmOperands(MCInst &Inst, unsigned N) const {
2289     assert(N == 1 && "Invalid number of operands!");
2290     // This is container for the immediate that we will create the constant
2291     // pool from
2292     addExpr(Inst, getConstantPoolImm());
2293     return;
2294   }
2295 
2296   void addMemTBBOperands(MCInst &Inst, unsigned N) const {
2297     assert(N == 2 && "Invalid number of operands!");
2298     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2299     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2300   }
2301 
2302   void addMemTBHOperands(MCInst &Inst, unsigned N) const {
2303     assert(N == 2 && "Invalid number of operands!");
2304     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2305     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2306   }
2307 
2308   void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
2309     assert(N == 3 && "Invalid number of operands!");
2310     unsigned Val =
2311       ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
2312                         Memory.ShiftImm, Memory.ShiftType);
2313     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2314     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2315     Inst.addOperand(MCOperand::createImm(Val));
2316   }
2317 
2318   void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
2319     assert(N == 3 && "Invalid number of operands!");
2320     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2321     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2322     Inst.addOperand(MCOperand::createImm(Memory.ShiftImm));
2323   }
2324 
2325   void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
2326     assert(N == 2 && "Invalid number of operands!");
2327     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2328     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2329   }
2330 
2331   void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
2332     assert(N == 2 && "Invalid number of operands!");
2333     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
2334     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2335     Inst.addOperand(MCOperand::createImm(Val));
2336   }
2337 
2338   void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
2339     assert(N == 2 && "Invalid number of operands!");
2340     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
2341     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2342     Inst.addOperand(MCOperand::createImm(Val));
2343   }
2344 
2345   void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
2346     assert(N == 2 && "Invalid number of operands!");
2347     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
2348     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2349     Inst.addOperand(MCOperand::createImm(Val));
2350   }
2351 
2352   void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
2353     assert(N == 2 && "Invalid number of operands!");
2354     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
2355     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2356     Inst.addOperand(MCOperand::createImm(Val));
2357   }
2358 
2359   void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
2360     assert(N == 1 && "Invalid number of operands!");
2361     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2362     assert(CE && "non-constant post-idx-imm8 operand!");
2363     int Imm = CE->getValue();
2364     bool isAdd = Imm >= 0;
2365     if (Imm == INT32_MIN) Imm = 0;
2366     Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
2367     Inst.addOperand(MCOperand::createImm(Imm));
2368   }
2369 
2370   void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
2371     assert(N == 1 && "Invalid number of operands!");
2372     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2373     assert(CE && "non-constant post-idx-imm8s4 operand!");
2374     int Imm = CE->getValue();
2375     bool isAdd = Imm >= 0;
2376     if (Imm == INT32_MIN) Imm = 0;
2377     // Immediate is scaled by 4.
2378     Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
2379     Inst.addOperand(MCOperand::createImm(Imm));
2380   }
2381 
2382   void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
2383     assert(N == 2 && "Invalid number of operands!");
2384     Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
2385     Inst.addOperand(MCOperand::createImm(PostIdxReg.isAdd));
2386   }
2387 
2388   void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
2389     assert(N == 2 && "Invalid number of operands!");
2390     Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
2391     // The sign, shift type, and shift amount are encoded in a single operand
2392     // using the AM2 encoding helpers.
2393     ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
2394     unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
2395                                      PostIdxReg.ShiftTy);
2396     Inst.addOperand(MCOperand::createImm(Imm));
2397   }
2398 
2399   void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
2400     assert(N == 1 && "Invalid number of operands!");
2401     Inst.addOperand(MCOperand::createImm(unsigned(getMSRMask())));
2402   }
2403 
2404   void addBankedRegOperands(MCInst &Inst, unsigned N) const {
2405     assert(N == 1 && "Invalid number of operands!");
2406     Inst.addOperand(MCOperand::createImm(unsigned(getBankedReg())));
2407   }
2408 
2409   void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
2410     assert(N == 1 && "Invalid number of operands!");
2411     Inst.addOperand(MCOperand::createImm(unsigned(getProcIFlags())));
2412   }
2413 
2414   void addVecListOperands(MCInst &Inst, unsigned N) const {
2415     assert(N == 1 && "Invalid number of operands!");
2416     Inst.addOperand(MCOperand::createReg(VectorList.RegNum));
2417   }
2418 
2419   void addVecListIndexedOperands(MCInst &Inst, unsigned N) const {
2420     assert(N == 2 && "Invalid number of operands!");
2421     Inst.addOperand(MCOperand::createReg(VectorList.RegNum));
2422     Inst.addOperand(MCOperand::createImm(VectorList.LaneIndex));
2423   }
2424 
2425   void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
2426     assert(N == 1 && "Invalid number of operands!");
2427     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
2428   }
2429 
2430   void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
2431     assert(N == 1 && "Invalid number of operands!");
2432     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
2433   }
2434 
2435   void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
2436     assert(N == 1 && "Invalid number of operands!");
2437     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
2438   }
2439 
2440   void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
2441     assert(N == 1 && "Invalid number of operands!");
2442     // The immediate encodes the type of constant as well as the value.
2443     // Mask in that this is an i8 splat.
2444     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2445     Inst.addOperand(MCOperand::createImm(CE->getValue() | 0xe00));
2446   }
2447 
2448   void addNEONi16splatOperands(MCInst &Inst, unsigned N) const {
2449     assert(N == 1 && "Invalid number of operands!");
2450     // The immediate encodes the type of constant as well as the value.
2451     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2452     unsigned Value = CE->getValue();
2453     Value = ARM_AM::encodeNEONi16splat(Value);
2454     Inst.addOperand(MCOperand::createImm(Value));
2455   }
2456 
2457   void addNEONi16splatNotOperands(MCInst &Inst, unsigned N) const {
2458     assert(N == 1 && "Invalid number of operands!");
2459     // The immediate encodes the type of constant as well as the value.
2460     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2461     unsigned Value = CE->getValue();
2462     Value = ARM_AM::encodeNEONi16splat(~Value & 0xffff);
2463     Inst.addOperand(MCOperand::createImm(Value));
2464   }
2465 
2466   void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
2467     assert(N == 1 && "Invalid number of operands!");
2468     // The immediate encodes the type of constant as well as the value.
2469     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2470     unsigned Value = CE->getValue();
2471     Value = ARM_AM::encodeNEONi32splat(Value);
2472     Inst.addOperand(MCOperand::createImm(Value));
2473   }
2474 
2475   void addNEONi32splatNotOperands(MCInst &Inst, unsigned N) const {
2476     assert(N == 1 && "Invalid number of operands!");
2477     // The immediate encodes the type of constant as well as the value.
2478     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2479     unsigned Value = CE->getValue();
2480     Value = ARM_AM::encodeNEONi32splat(~Value);
2481     Inst.addOperand(MCOperand::createImm(Value));
2482   }
2483 
2484   void addNEONinvByteReplicateOperands(MCInst &Inst, unsigned N) const {
2485     assert(N == 1 && "Invalid number of operands!");
2486     // The immediate encodes the type of constant as well as the value.
2487     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2488     unsigned Value = CE->getValue();
2489     assert((Inst.getOpcode() == ARM::VMOVv8i8 ||
2490             Inst.getOpcode() == ARM::VMOVv16i8) &&
2491            "All vmvn instructions that wants to replicate non-zero byte "
2492            "always must be replaced with VMOVv8i8 or VMOVv16i8.");
2493     unsigned B = ((~Value) & 0xff);
2494     B |= 0xe00; // cmode = 0b1110
2495     Inst.addOperand(MCOperand::createImm(B));
2496   }
2497   void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
2498     assert(N == 1 && "Invalid number of operands!");
2499     // The immediate encodes the type of constant as well as the value.
2500     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2501     unsigned Value = CE->getValue();
2502     if (Value >= 256 && Value <= 0xffff)
2503       Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
2504     else if (Value > 0xffff && Value <= 0xffffff)
2505       Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
2506     else if (Value > 0xffffff)
2507       Value = (Value >> 24) | 0x600;
2508     Inst.addOperand(MCOperand::createImm(Value));
2509   }
2510 
2511   void addNEONvmovByteReplicateOperands(MCInst &Inst, unsigned N) const {
2512     assert(N == 1 && "Invalid number of operands!");
2513     // The immediate encodes the type of constant as well as the value.
2514     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2515     unsigned Value = CE->getValue();
2516     assert((Inst.getOpcode() == ARM::VMOVv8i8 ||
2517             Inst.getOpcode() == ARM::VMOVv16i8) &&
2518            "All instructions that wants to replicate non-zero byte "
2519            "always must be replaced with VMOVv8i8 or VMOVv16i8.");
2520     unsigned B = Value & 0xff;
2521     B |= 0xe00; // cmode = 0b1110
2522     Inst.addOperand(MCOperand::createImm(B));
2523   }
2524   void addNEONi32vmovNegOperands(MCInst &Inst, unsigned N) const {
2525     assert(N == 1 && "Invalid number of operands!");
2526     // The immediate encodes the type of constant as well as the value.
2527     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2528     unsigned Value = ~CE->getValue();
2529     if (Value >= 256 && Value <= 0xffff)
2530       Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
2531     else if (Value > 0xffff && Value <= 0xffffff)
2532       Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
2533     else if (Value > 0xffffff)
2534       Value = (Value >> 24) | 0x600;
2535     Inst.addOperand(MCOperand::createImm(Value));
2536   }
2537 
2538   void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
2539     assert(N == 1 && "Invalid number of operands!");
2540     // The immediate encodes the type of constant as well as the value.
2541     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2542     uint64_t Value = CE->getValue();
2543     unsigned Imm = 0;
2544     for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
2545       Imm |= (Value & 1) << i;
2546     }
2547     Inst.addOperand(MCOperand::createImm(Imm | 0x1e00));
2548   }
2549 
2550   void print(raw_ostream &OS) const override;
2551 
2552   static std::unique_ptr<ARMOperand> CreateITMask(unsigned Mask, SMLoc S) {
2553     auto Op = make_unique<ARMOperand>(k_ITCondMask);
2554     Op->ITMask.Mask = Mask;
2555     Op->StartLoc = S;
2556     Op->EndLoc = S;
2557     return Op;
2558   }
2559 
2560   static std::unique_ptr<ARMOperand> CreateCondCode(ARMCC::CondCodes CC,
2561                                                     SMLoc S) {
2562     auto Op = make_unique<ARMOperand>(k_CondCode);
2563     Op->CC.Val = CC;
2564     Op->StartLoc = S;
2565     Op->EndLoc = S;
2566     return Op;
2567   }
2568 
2569   static std::unique_ptr<ARMOperand> CreateCoprocNum(unsigned CopVal, SMLoc S) {
2570     auto Op = make_unique<ARMOperand>(k_CoprocNum);
2571     Op->Cop.Val = CopVal;
2572     Op->StartLoc = S;
2573     Op->EndLoc = S;
2574     return Op;
2575   }
2576 
2577   static std::unique_ptr<ARMOperand> CreateCoprocReg(unsigned CopVal, SMLoc S) {
2578     auto Op = make_unique<ARMOperand>(k_CoprocReg);
2579     Op->Cop.Val = CopVal;
2580     Op->StartLoc = S;
2581     Op->EndLoc = S;
2582     return Op;
2583   }
2584 
2585   static std::unique_ptr<ARMOperand> CreateCoprocOption(unsigned Val, SMLoc S,
2586                                                         SMLoc E) {
2587     auto Op = make_unique<ARMOperand>(k_CoprocOption);
2588     Op->Cop.Val = Val;
2589     Op->StartLoc = S;
2590     Op->EndLoc = E;
2591     return Op;
2592   }
2593 
2594   static std::unique_ptr<ARMOperand> CreateCCOut(unsigned RegNum, SMLoc S) {
2595     auto Op = make_unique<ARMOperand>(k_CCOut);
2596     Op->Reg.RegNum = RegNum;
2597     Op->StartLoc = S;
2598     Op->EndLoc = S;
2599     return Op;
2600   }
2601 
2602   static std::unique_ptr<ARMOperand> CreateToken(StringRef Str, SMLoc S) {
2603     auto Op = make_unique<ARMOperand>(k_Token);
2604     Op->Tok.Data = Str.data();
2605     Op->Tok.Length = Str.size();
2606     Op->StartLoc = S;
2607     Op->EndLoc = S;
2608     return Op;
2609   }
2610 
2611   static std::unique_ptr<ARMOperand> CreateReg(unsigned RegNum, SMLoc S,
2612                                                SMLoc E) {
2613     auto Op = make_unique<ARMOperand>(k_Register);
2614     Op->Reg.RegNum = RegNum;
2615     Op->StartLoc = S;
2616     Op->EndLoc = E;
2617     return Op;
2618   }
2619 
2620   static std::unique_ptr<ARMOperand>
2621   CreateShiftedRegister(ARM_AM::ShiftOpc ShTy, unsigned SrcReg,
2622                         unsigned ShiftReg, unsigned ShiftImm, SMLoc S,
2623                         SMLoc E) {
2624     auto Op = make_unique<ARMOperand>(k_ShiftedRegister);
2625     Op->RegShiftedReg.ShiftTy = ShTy;
2626     Op->RegShiftedReg.SrcReg = SrcReg;
2627     Op->RegShiftedReg.ShiftReg = ShiftReg;
2628     Op->RegShiftedReg.ShiftImm = ShiftImm;
2629     Op->StartLoc = S;
2630     Op->EndLoc = E;
2631     return Op;
2632   }
2633 
2634   static std::unique_ptr<ARMOperand>
2635   CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy, unsigned SrcReg,
2636                          unsigned ShiftImm, SMLoc S, SMLoc E) {
2637     auto Op = make_unique<ARMOperand>(k_ShiftedImmediate);
2638     Op->RegShiftedImm.ShiftTy = ShTy;
2639     Op->RegShiftedImm.SrcReg = SrcReg;
2640     Op->RegShiftedImm.ShiftImm = ShiftImm;
2641     Op->StartLoc = S;
2642     Op->EndLoc = E;
2643     return Op;
2644   }
2645 
2646   static std::unique_ptr<ARMOperand> CreateShifterImm(bool isASR, unsigned Imm,
2647                                                       SMLoc S, SMLoc E) {
2648     auto Op = make_unique<ARMOperand>(k_ShifterImmediate);
2649     Op->ShifterImm.isASR = isASR;
2650     Op->ShifterImm.Imm = Imm;
2651     Op->StartLoc = S;
2652     Op->EndLoc = E;
2653     return Op;
2654   }
2655 
2656   static std::unique_ptr<ARMOperand> CreateRotImm(unsigned Imm, SMLoc S,
2657                                                   SMLoc E) {
2658     auto Op = make_unique<ARMOperand>(k_RotateImmediate);
2659     Op->RotImm.Imm = Imm;
2660     Op->StartLoc = S;
2661     Op->EndLoc = E;
2662     return Op;
2663   }
2664 
2665   static std::unique_ptr<ARMOperand> CreateModImm(unsigned Bits, unsigned Rot,
2666                                                   SMLoc S, SMLoc E) {
2667     auto Op = make_unique<ARMOperand>(k_ModifiedImmediate);
2668     Op->ModImm.Bits = Bits;
2669     Op->ModImm.Rot = Rot;
2670     Op->StartLoc = S;
2671     Op->EndLoc = E;
2672     return Op;
2673   }
2674 
2675   static std::unique_ptr<ARMOperand>
2676   CreateConstantPoolImm(const MCExpr *Val, SMLoc S, SMLoc E) {
2677     auto Op = make_unique<ARMOperand>(k_ConstantPoolImmediate);
2678     Op->Imm.Val = Val;
2679     Op->StartLoc = S;
2680     Op->EndLoc = E;
2681     return Op;
2682   }
2683 
2684   static std::unique_ptr<ARMOperand>
2685   CreateBitfield(unsigned LSB, unsigned Width, SMLoc S, SMLoc E) {
2686     auto Op = make_unique<ARMOperand>(k_BitfieldDescriptor);
2687     Op->Bitfield.LSB = LSB;
2688     Op->Bitfield.Width = Width;
2689     Op->StartLoc = S;
2690     Op->EndLoc = E;
2691     return Op;
2692   }
2693 
2694   static std::unique_ptr<ARMOperand>
2695   CreateRegList(SmallVectorImpl<std::pair<unsigned, unsigned>> &Regs,
2696                 SMLoc StartLoc, SMLoc EndLoc) {
2697     assert (Regs.size() > 0 && "RegList contains no registers?");
2698     KindTy Kind = k_RegisterList;
2699 
2700     if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().second))
2701       Kind = k_DPRRegisterList;
2702     else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
2703              contains(Regs.front().second))
2704       Kind = k_SPRRegisterList;
2705 
2706     // Sort based on the register encoding values.
2707     array_pod_sort(Regs.begin(), Regs.end());
2708 
2709     auto Op = make_unique<ARMOperand>(Kind);
2710     for (SmallVectorImpl<std::pair<unsigned, unsigned> >::const_iterator
2711            I = Regs.begin(), E = Regs.end(); I != E; ++I)
2712       Op->Registers.push_back(I->second);
2713     Op->StartLoc = StartLoc;
2714     Op->EndLoc = EndLoc;
2715     return Op;
2716   }
2717 
2718   static std::unique_ptr<ARMOperand> CreateVectorList(unsigned RegNum,
2719                                                       unsigned Count,
2720                                                       bool isDoubleSpaced,
2721                                                       SMLoc S, SMLoc E) {
2722     auto Op = make_unique<ARMOperand>(k_VectorList);
2723     Op->VectorList.RegNum = RegNum;
2724     Op->VectorList.Count = Count;
2725     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2726     Op->StartLoc = S;
2727     Op->EndLoc = E;
2728     return Op;
2729   }
2730 
2731   static std::unique_ptr<ARMOperand>
2732   CreateVectorListAllLanes(unsigned RegNum, unsigned Count, bool isDoubleSpaced,
2733                            SMLoc S, SMLoc E) {
2734     auto Op = make_unique<ARMOperand>(k_VectorListAllLanes);
2735     Op->VectorList.RegNum = RegNum;
2736     Op->VectorList.Count = Count;
2737     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2738     Op->StartLoc = S;
2739     Op->EndLoc = E;
2740     return Op;
2741   }
2742 
2743   static std::unique_ptr<ARMOperand>
2744   CreateVectorListIndexed(unsigned RegNum, unsigned Count, unsigned Index,
2745                           bool isDoubleSpaced, SMLoc S, SMLoc E) {
2746     auto Op = make_unique<ARMOperand>(k_VectorListIndexed);
2747     Op->VectorList.RegNum = RegNum;
2748     Op->VectorList.Count = Count;
2749     Op->VectorList.LaneIndex = Index;
2750     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2751     Op->StartLoc = S;
2752     Op->EndLoc = E;
2753     return Op;
2754   }
2755 
2756   static std::unique_ptr<ARMOperand>
2757   CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E, MCContext &Ctx) {
2758     auto Op = make_unique<ARMOperand>(k_VectorIndex);
2759     Op->VectorIndex.Val = Idx;
2760     Op->StartLoc = S;
2761     Op->EndLoc = E;
2762     return Op;
2763   }
2764 
2765   static std::unique_ptr<ARMOperand> CreateImm(const MCExpr *Val, SMLoc S,
2766                                                SMLoc E) {
2767     auto Op = make_unique<ARMOperand>(k_Immediate);
2768     Op->Imm.Val = Val;
2769     Op->StartLoc = S;
2770     Op->EndLoc = E;
2771     return Op;
2772   }
2773 
2774   static std::unique_ptr<ARMOperand>
2775   CreateMem(unsigned BaseRegNum, const MCConstantExpr *OffsetImm,
2776             unsigned OffsetRegNum, ARM_AM::ShiftOpc ShiftType,
2777             unsigned ShiftImm, unsigned Alignment, bool isNegative, SMLoc S,
2778             SMLoc E, SMLoc AlignmentLoc = SMLoc()) {
2779     auto Op = make_unique<ARMOperand>(k_Memory);
2780     Op->Memory.BaseRegNum = BaseRegNum;
2781     Op->Memory.OffsetImm = OffsetImm;
2782     Op->Memory.OffsetRegNum = OffsetRegNum;
2783     Op->Memory.ShiftType = ShiftType;
2784     Op->Memory.ShiftImm = ShiftImm;
2785     Op->Memory.Alignment = Alignment;
2786     Op->Memory.isNegative = isNegative;
2787     Op->StartLoc = S;
2788     Op->EndLoc = E;
2789     Op->AlignmentLoc = AlignmentLoc;
2790     return Op;
2791   }
2792 
2793   static std::unique_ptr<ARMOperand>
2794   CreatePostIdxReg(unsigned RegNum, bool isAdd, ARM_AM::ShiftOpc ShiftTy,
2795                    unsigned ShiftImm, SMLoc S, SMLoc E) {
2796     auto Op = make_unique<ARMOperand>(k_PostIndexRegister);
2797     Op->PostIdxReg.RegNum = RegNum;
2798     Op->PostIdxReg.isAdd = isAdd;
2799     Op->PostIdxReg.ShiftTy = ShiftTy;
2800     Op->PostIdxReg.ShiftImm = ShiftImm;
2801     Op->StartLoc = S;
2802     Op->EndLoc = E;
2803     return Op;
2804   }
2805 
2806   static std::unique_ptr<ARMOperand> CreateMemBarrierOpt(ARM_MB::MemBOpt Opt,
2807                                                          SMLoc S) {
2808     auto Op = make_unique<ARMOperand>(k_MemBarrierOpt);
2809     Op->MBOpt.Val = Opt;
2810     Op->StartLoc = S;
2811     Op->EndLoc = S;
2812     return Op;
2813   }
2814 
2815   static std::unique_ptr<ARMOperand>
2816   CreateInstSyncBarrierOpt(ARM_ISB::InstSyncBOpt Opt, SMLoc S) {
2817     auto Op = make_unique<ARMOperand>(k_InstSyncBarrierOpt);
2818     Op->ISBOpt.Val = Opt;
2819     Op->StartLoc = S;
2820     Op->EndLoc = S;
2821     return Op;
2822   }
2823 
2824   static std::unique_ptr<ARMOperand> CreateProcIFlags(ARM_PROC::IFlags IFlags,
2825                                                       SMLoc S) {
2826     auto Op = make_unique<ARMOperand>(k_ProcIFlags);
2827     Op->IFlags.Val = IFlags;
2828     Op->StartLoc = S;
2829     Op->EndLoc = S;
2830     return Op;
2831   }
2832 
2833   static std::unique_ptr<ARMOperand> CreateMSRMask(unsigned MMask, SMLoc S) {
2834     auto Op = make_unique<ARMOperand>(k_MSRMask);
2835     Op->MMask.Val = MMask;
2836     Op->StartLoc = S;
2837     Op->EndLoc = S;
2838     return Op;
2839   }
2840 
2841   static std::unique_ptr<ARMOperand> CreateBankedReg(unsigned Reg, SMLoc S) {
2842     auto Op = make_unique<ARMOperand>(k_BankedReg);
2843     Op->BankedReg.Val = Reg;
2844     Op->StartLoc = S;
2845     Op->EndLoc = S;
2846     return Op;
2847   }
2848 };
2849 
2850 } // end anonymous namespace.
2851 
2852 void ARMOperand::print(raw_ostream &OS) const {
2853   switch (Kind) {
2854   case k_CondCode:
2855     OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
2856     break;
2857   case k_CCOut:
2858     OS << "<ccout " << getReg() << ">";
2859     break;
2860   case k_ITCondMask: {
2861     static const char *const MaskStr[] = {
2862       "()", "(t)", "(e)", "(tt)", "(et)", "(te)", "(ee)", "(ttt)", "(ett)",
2863       "(tet)", "(eet)", "(tte)", "(ete)", "(tee)", "(eee)"
2864     };
2865     assert((ITMask.Mask & 0xf) == ITMask.Mask);
2866     OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
2867     break;
2868   }
2869   case k_CoprocNum:
2870     OS << "<coprocessor number: " << getCoproc() << ">";
2871     break;
2872   case k_CoprocReg:
2873     OS << "<coprocessor register: " << getCoproc() << ">";
2874     break;
2875   case k_CoprocOption:
2876     OS << "<coprocessor option: " << CoprocOption.Val << ">";
2877     break;
2878   case k_MSRMask:
2879     OS << "<mask: " << getMSRMask() << ">";
2880     break;
2881   case k_BankedReg:
2882     OS << "<banked reg: " << getBankedReg() << ">";
2883     break;
2884   case k_Immediate:
2885     OS << *getImm();
2886     break;
2887   case k_MemBarrierOpt:
2888     OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt(), false) << ">";
2889     break;
2890   case k_InstSyncBarrierOpt:
2891     OS << "<ARM_ISB::" << InstSyncBOptToString(getInstSyncBarrierOpt()) << ">";
2892     break;
2893   case k_Memory:
2894     OS << "<memory "
2895        << " base:" << Memory.BaseRegNum;
2896     OS << ">";
2897     break;
2898   case k_PostIndexRegister:
2899     OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
2900        << PostIdxReg.RegNum;
2901     if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
2902       OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
2903          << PostIdxReg.ShiftImm;
2904     OS << ">";
2905     break;
2906   case k_ProcIFlags: {
2907     OS << "<ARM_PROC::";
2908     unsigned IFlags = getProcIFlags();
2909     for (int i=2; i >= 0; --i)
2910       if (IFlags & (1 << i))
2911         OS << ARM_PROC::IFlagsToString(1 << i);
2912     OS << ">";
2913     break;
2914   }
2915   case k_Register:
2916     OS << "<register " << getReg() << ">";
2917     break;
2918   case k_ShifterImmediate:
2919     OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
2920        << " #" << ShifterImm.Imm << ">";
2921     break;
2922   case k_ShiftedRegister:
2923     OS << "<so_reg_reg "
2924        << RegShiftedReg.SrcReg << " "
2925        << ARM_AM::getShiftOpcStr(RegShiftedReg.ShiftTy)
2926        << " " << RegShiftedReg.ShiftReg << ">";
2927     break;
2928   case k_ShiftedImmediate:
2929     OS << "<so_reg_imm "
2930        << RegShiftedImm.SrcReg << " "
2931        << ARM_AM::getShiftOpcStr(RegShiftedImm.ShiftTy)
2932        << " #" << RegShiftedImm.ShiftImm << ">";
2933     break;
2934   case k_RotateImmediate:
2935     OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
2936     break;
2937   case k_ModifiedImmediate:
2938     OS << "<mod_imm #" << ModImm.Bits << ", #"
2939        <<  ModImm.Rot << ")>";
2940     break;
2941   case k_ConstantPoolImmediate:
2942     OS << "<constant_pool_imm #" << *getConstantPoolImm();
2943     break;
2944   case k_BitfieldDescriptor:
2945     OS << "<bitfield " << "lsb: " << Bitfield.LSB
2946        << ", width: " << Bitfield.Width << ">";
2947     break;
2948   case k_RegisterList:
2949   case k_DPRRegisterList:
2950   case k_SPRRegisterList: {
2951     OS << "<register_list ";
2952 
2953     const SmallVectorImpl<unsigned> &RegList = getRegList();
2954     for (SmallVectorImpl<unsigned>::const_iterator
2955            I = RegList.begin(), E = RegList.end(); I != E; ) {
2956       OS << *I;
2957       if (++I < E) OS << ", ";
2958     }
2959 
2960     OS << ">";
2961     break;
2962   }
2963   case k_VectorList:
2964     OS << "<vector_list " << VectorList.Count << " * "
2965        << VectorList.RegNum << ">";
2966     break;
2967   case k_VectorListAllLanes:
2968     OS << "<vector_list(all lanes) " << VectorList.Count << " * "
2969        << VectorList.RegNum << ">";
2970     break;
2971   case k_VectorListIndexed:
2972     OS << "<vector_list(lane " << VectorList.LaneIndex << ") "
2973        << VectorList.Count << " * " << VectorList.RegNum << ">";
2974     break;
2975   case k_Token:
2976     OS << "'" << getToken() << "'";
2977     break;
2978   case k_VectorIndex:
2979     OS << "<vectorindex " << getVectorIndex() << ">";
2980     break;
2981   }
2982 }
2983 
2984 /// @name Auto-generated Match Functions
2985 /// {
2986 
2987 static unsigned MatchRegisterName(StringRef Name);
2988 
2989 /// }
2990 
2991 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
2992                                  SMLoc &StartLoc, SMLoc &EndLoc) {
2993   const AsmToken &Tok = getParser().getTok();
2994   StartLoc = Tok.getLoc();
2995   EndLoc = Tok.getEndLoc();
2996   RegNo = tryParseRegister();
2997 
2998   return (RegNo == (unsigned)-1);
2999 }
3000 
3001 /// Try to parse a register name.  The token must be an Identifier when called,
3002 /// and if it is a register name the token is eaten and the register number is
3003 /// returned.  Otherwise return -1.
3004 ///
3005 int ARMAsmParser::tryParseRegister() {
3006   MCAsmParser &Parser = getParser();
3007   const AsmToken &Tok = Parser.getTok();
3008   if (Tok.isNot(AsmToken::Identifier)) return -1;
3009 
3010   std::string lowerCase = Tok.getString().lower();
3011   unsigned RegNum = MatchRegisterName(lowerCase);
3012   if (!RegNum) {
3013     RegNum = StringSwitch<unsigned>(lowerCase)
3014       .Case("r13", ARM::SP)
3015       .Case("r14", ARM::LR)
3016       .Case("r15", ARM::PC)
3017       .Case("ip", ARM::R12)
3018       // Additional register name aliases for 'gas' compatibility.
3019       .Case("a1", ARM::R0)
3020       .Case("a2", ARM::R1)
3021       .Case("a3", ARM::R2)
3022       .Case("a4", ARM::R3)
3023       .Case("v1", ARM::R4)
3024       .Case("v2", ARM::R5)
3025       .Case("v3", ARM::R6)
3026       .Case("v4", ARM::R7)
3027       .Case("v5", ARM::R8)
3028       .Case("v6", ARM::R9)
3029       .Case("v7", ARM::R10)
3030       .Case("v8", ARM::R11)
3031       .Case("sb", ARM::R9)
3032       .Case("sl", ARM::R10)
3033       .Case("fp", ARM::R11)
3034       .Default(0);
3035   }
3036   if (!RegNum) {
3037     // Check for aliases registered via .req. Canonicalize to lower case.
3038     // That's more consistent since register names are case insensitive, and
3039     // it's how the original entry was passed in from MC/MCParser/AsmParser.
3040     StringMap<unsigned>::const_iterator Entry = RegisterReqs.find(lowerCase);
3041     // If no match, return failure.
3042     if (Entry == RegisterReqs.end())
3043       return -1;
3044     Parser.Lex(); // Eat identifier token.
3045     return Entry->getValue();
3046   }
3047 
3048   // Some FPUs only have 16 D registers, so D16-D31 are invalid
3049   if (hasD16() && RegNum >= ARM::D16 && RegNum <= ARM::D31)
3050     return -1;
3051 
3052   Parser.Lex(); // Eat identifier token.
3053 
3054   return RegNum;
3055 }
3056 
3057 // Try to parse a shifter  (e.g., "lsl <amt>"). On success, return 0.
3058 // If a recoverable error occurs, return 1. If an irrecoverable error
3059 // occurs, return -1. An irrecoverable error is one where tokens have been
3060 // consumed in the process of trying to parse the shifter (i.e., when it is
3061 // indeed a shifter operand, but malformed).
3062 int ARMAsmParser::tryParseShiftRegister(OperandVector &Operands) {
3063   MCAsmParser &Parser = getParser();
3064   SMLoc S = Parser.getTok().getLoc();
3065   const AsmToken &Tok = Parser.getTok();
3066   if (Tok.isNot(AsmToken::Identifier))
3067     return -1;
3068 
3069   std::string lowerCase = Tok.getString().lower();
3070   ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
3071       .Case("asl", ARM_AM::lsl)
3072       .Case("lsl", ARM_AM::lsl)
3073       .Case("lsr", ARM_AM::lsr)
3074       .Case("asr", ARM_AM::asr)
3075       .Case("ror", ARM_AM::ror)
3076       .Case("rrx", ARM_AM::rrx)
3077       .Default(ARM_AM::no_shift);
3078 
3079   if (ShiftTy == ARM_AM::no_shift)
3080     return 1;
3081 
3082   Parser.Lex(); // Eat the operator.
3083 
3084   // The source register for the shift has already been added to the
3085   // operand list, so we need to pop it off and combine it into the shifted
3086   // register operand instead.
3087   std::unique_ptr<ARMOperand> PrevOp(
3088       (ARMOperand *)Operands.pop_back_val().release());
3089   if (!PrevOp->isReg())
3090     return Error(PrevOp->getStartLoc(), "shift must be of a register");
3091   int SrcReg = PrevOp->getReg();
3092 
3093   SMLoc EndLoc;
3094   int64_t Imm = 0;
3095   int ShiftReg = 0;
3096   if (ShiftTy == ARM_AM::rrx) {
3097     // RRX Doesn't have an explicit shift amount. The encoder expects
3098     // the shift register to be the same as the source register. Seems odd,
3099     // but OK.
3100     ShiftReg = SrcReg;
3101   } else {
3102     // Figure out if this is shifted by a constant or a register (for non-RRX).
3103     if (Parser.getTok().is(AsmToken::Hash) ||
3104         Parser.getTok().is(AsmToken::Dollar)) {
3105       Parser.Lex(); // Eat hash.
3106       SMLoc ImmLoc = Parser.getTok().getLoc();
3107       const MCExpr *ShiftExpr = nullptr;
3108       if (getParser().parseExpression(ShiftExpr, EndLoc)) {
3109         Error(ImmLoc, "invalid immediate shift value");
3110         return -1;
3111       }
3112       // The expression must be evaluatable as an immediate.
3113       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
3114       if (!CE) {
3115         Error(ImmLoc, "invalid immediate shift value");
3116         return -1;
3117       }
3118       // Range check the immediate.
3119       // lsl, ror: 0 <= imm <= 31
3120       // lsr, asr: 0 <= imm <= 32
3121       Imm = CE->getValue();
3122       if (Imm < 0 ||
3123           ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
3124           ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
3125         Error(ImmLoc, "immediate shift value out of range");
3126         return -1;
3127       }
3128       // shift by zero is a nop. Always send it through as lsl.
3129       // ('as' compatibility)
3130       if (Imm == 0)
3131         ShiftTy = ARM_AM::lsl;
3132     } else if (Parser.getTok().is(AsmToken::Identifier)) {
3133       SMLoc L = Parser.getTok().getLoc();
3134       EndLoc = Parser.getTok().getEndLoc();
3135       ShiftReg = tryParseRegister();
3136       if (ShiftReg == -1) {
3137         Error(L, "expected immediate or register in shift operand");
3138         return -1;
3139       }
3140     } else {
3141       Error(Parser.getTok().getLoc(),
3142             "expected immediate or register in shift operand");
3143       return -1;
3144     }
3145   }
3146 
3147   if (ShiftReg && ShiftTy != ARM_AM::rrx)
3148     Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
3149                                                          ShiftReg, Imm,
3150                                                          S, EndLoc));
3151   else
3152     Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
3153                                                           S, EndLoc));
3154 
3155   return 0;
3156 }
3157 
3158 
3159 /// Try to parse a register name.  The token must be an Identifier when called.
3160 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
3161 /// if there is a "writeback". 'true' if it's not a register.
3162 ///
3163 /// TODO this is likely to change to allow different register types and or to
3164 /// parse for a specific register type.
3165 bool ARMAsmParser::tryParseRegisterWithWriteBack(OperandVector &Operands) {
3166   MCAsmParser &Parser = getParser();
3167   const AsmToken &RegTok = Parser.getTok();
3168   int RegNo = tryParseRegister();
3169   if (RegNo == -1)
3170     return true;
3171 
3172   Operands.push_back(ARMOperand::CreateReg(RegNo, RegTok.getLoc(),
3173                                            RegTok.getEndLoc()));
3174 
3175   const AsmToken &ExclaimTok = Parser.getTok();
3176   if (ExclaimTok.is(AsmToken::Exclaim)) {
3177     Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
3178                                                ExclaimTok.getLoc()));
3179     Parser.Lex(); // Eat exclaim token
3180     return false;
3181   }
3182 
3183   // Also check for an index operand. This is only legal for vector registers,
3184   // but that'll get caught OK in operand matching, so we don't need to
3185   // explicitly filter everything else out here.
3186   if (Parser.getTok().is(AsmToken::LBrac)) {
3187     SMLoc SIdx = Parser.getTok().getLoc();
3188     Parser.Lex(); // Eat left bracket token.
3189 
3190     const MCExpr *ImmVal;
3191     if (getParser().parseExpression(ImmVal))
3192       return true;
3193     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
3194     if (!MCE)
3195       return TokError("immediate value expected for vector index");
3196 
3197     if (Parser.getTok().isNot(AsmToken::RBrac))
3198       return Error(Parser.getTok().getLoc(), "']' expected");
3199 
3200     SMLoc E = Parser.getTok().getEndLoc();
3201     Parser.Lex(); // Eat right bracket token.
3202 
3203     Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
3204                                                      SIdx, E,
3205                                                      getContext()));
3206   }
3207 
3208   return false;
3209 }
3210 
3211 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
3212 /// instruction with a symbolic operand name.
3213 /// We accept "crN" syntax for GAS compatibility.
3214 /// <operand-name> ::= <prefix><number>
3215 /// If CoprocOp is 'c', then:
3216 ///   <prefix> ::= c | cr
3217 /// If CoprocOp is 'p', then :
3218 ///   <prefix> ::= p
3219 /// <number> ::= integer in range [0, 15]
3220 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
3221   // Use the same layout as the tablegen'erated register name matcher. Ugly,
3222   // but efficient.
3223   if (Name.size() < 2 || Name[0] != CoprocOp)
3224     return -1;
3225   Name = (Name[1] == 'r') ? Name.drop_front(2) : Name.drop_front();
3226 
3227   switch (Name.size()) {
3228   default: return -1;
3229   case 1:
3230     switch (Name[0]) {
3231     default:  return -1;
3232     case '0': return 0;
3233     case '1': return 1;
3234     case '2': return 2;
3235     case '3': return 3;
3236     case '4': return 4;
3237     case '5': return 5;
3238     case '6': return 6;
3239     case '7': return 7;
3240     case '8': return 8;
3241     case '9': return 9;
3242     }
3243   case 2:
3244     if (Name[0] != '1')
3245       return -1;
3246     switch (Name[1]) {
3247     default:  return -1;
3248     // CP10 and CP11 are VFP/NEON and so vector instructions should be used.
3249     // However, old cores (v5/v6) did use them in that way.
3250     case '0': return 10;
3251     case '1': return 11;
3252     case '2': return 12;
3253     case '3': return 13;
3254     case '4': return 14;
3255     case '5': return 15;
3256     }
3257   }
3258 }
3259 
3260 /// parseITCondCode - Try to parse a condition code for an IT instruction.
3261 ARMAsmParser::OperandMatchResultTy
3262 ARMAsmParser::parseITCondCode(OperandVector &Operands) {
3263   MCAsmParser &Parser = getParser();
3264   SMLoc S = Parser.getTok().getLoc();
3265   const AsmToken &Tok = Parser.getTok();
3266   if (!Tok.is(AsmToken::Identifier))
3267     return MatchOperand_NoMatch;
3268   unsigned CC = StringSwitch<unsigned>(Tok.getString().lower())
3269     .Case("eq", ARMCC::EQ)
3270     .Case("ne", ARMCC::NE)
3271     .Case("hs", ARMCC::HS)
3272     .Case("cs", ARMCC::HS)
3273     .Case("lo", ARMCC::LO)
3274     .Case("cc", ARMCC::LO)
3275     .Case("mi", ARMCC::MI)
3276     .Case("pl", ARMCC::PL)
3277     .Case("vs", ARMCC::VS)
3278     .Case("vc", ARMCC::VC)
3279     .Case("hi", ARMCC::HI)
3280     .Case("ls", ARMCC::LS)
3281     .Case("ge", ARMCC::GE)
3282     .Case("lt", ARMCC::LT)
3283     .Case("gt", ARMCC::GT)
3284     .Case("le", ARMCC::LE)
3285     .Case("al", ARMCC::AL)
3286     .Default(~0U);
3287   if (CC == ~0U)
3288     return MatchOperand_NoMatch;
3289   Parser.Lex(); // Eat the token.
3290 
3291   Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
3292 
3293   return MatchOperand_Success;
3294 }
3295 
3296 /// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
3297 /// token must be an Identifier when called, and if it is a coprocessor
3298 /// number, the token is eaten and the operand is added to the operand list.
3299 ARMAsmParser::OperandMatchResultTy
3300 ARMAsmParser::parseCoprocNumOperand(OperandVector &Operands) {
3301   MCAsmParser &Parser = getParser();
3302   SMLoc S = Parser.getTok().getLoc();
3303   const AsmToken &Tok = Parser.getTok();
3304   if (Tok.isNot(AsmToken::Identifier))
3305     return MatchOperand_NoMatch;
3306 
3307   int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
3308   if (Num == -1)
3309     return MatchOperand_NoMatch;
3310   // ARMv7 and v8 don't allow cp10/cp11 due to VFP/NEON specific instructions
3311   if ((hasV7Ops() || hasV8Ops()) && (Num == 10 || Num == 11))
3312     return MatchOperand_NoMatch;
3313 
3314   Parser.Lex(); // Eat identifier token.
3315   Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
3316   return MatchOperand_Success;
3317 }
3318 
3319 /// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
3320 /// token must be an Identifier when called, and if it is a coprocessor
3321 /// number, the token is eaten and the operand is added to the operand list.
3322 ARMAsmParser::OperandMatchResultTy
3323 ARMAsmParser::parseCoprocRegOperand(OperandVector &Operands) {
3324   MCAsmParser &Parser = getParser();
3325   SMLoc S = Parser.getTok().getLoc();
3326   const AsmToken &Tok = Parser.getTok();
3327   if (Tok.isNot(AsmToken::Identifier))
3328     return MatchOperand_NoMatch;
3329 
3330   int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
3331   if (Reg == -1)
3332     return MatchOperand_NoMatch;
3333 
3334   Parser.Lex(); // Eat identifier token.
3335   Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
3336   return MatchOperand_Success;
3337 }
3338 
3339 /// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
3340 /// coproc_option : '{' imm0_255 '}'
3341 ARMAsmParser::OperandMatchResultTy
3342 ARMAsmParser::parseCoprocOptionOperand(OperandVector &Operands) {
3343   MCAsmParser &Parser = getParser();
3344   SMLoc S = Parser.getTok().getLoc();
3345 
3346   // If this isn't a '{', this isn't a coprocessor immediate operand.
3347   if (Parser.getTok().isNot(AsmToken::LCurly))
3348     return MatchOperand_NoMatch;
3349   Parser.Lex(); // Eat the '{'
3350 
3351   const MCExpr *Expr;
3352   SMLoc Loc = Parser.getTok().getLoc();
3353   if (getParser().parseExpression(Expr)) {
3354     Error(Loc, "illegal expression");
3355     return MatchOperand_ParseFail;
3356   }
3357   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3358   if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
3359     Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
3360     return MatchOperand_ParseFail;
3361   }
3362   int Val = CE->getValue();
3363 
3364   // Check for and consume the closing '}'
3365   if (Parser.getTok().isNot(AsmToken::RCurly))
3366     return MatchOperand_ParseFail;
3367   SMLoc E = Parser.getTok().getEndLoc();
3368   Parser.Lex(); // Eat the '}'
3369 
3370   Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
3371   return MatchOperand_Success;
3372 }
3373 
3374 // For register list parsing, we need to map from raw GPR register numbering
3375 // to the enumeration values. The enumeration values aren't sorted by
3376 // register number due to our using "sp", "lr" and "pc" as canonical names.
3377 static unsigned getNextRegister(unsigned Reg) {
3378   // If this is a GPR, we need to do it manually, otherwise we can rely
3379   // on the sort ordering of the enumeration since the other reg-classes
3380   // are sane.
3381   if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
3382     return Reg + 1;
3383   switch(Reg) {
3384   default: llvm_unreachable("Invalid GPR number!");
3385   case ARM::R0:  return ARM::R1;  case ARM::R1:  return ARM::R2;
3386   case ARM::R2:  return ARM::R3;  case ARM::R3:  return ARM::R4;
3387   case ARM::R4:  return ARM::R5;  case ARM::R5:  return ARM::R6;
3388   case ARM::R6:  return ARM::R7;  case ARM::R7:  return ARM::R8;
3389   case ARM::R8:  return ARM::R9;  case ARM::R9:  return ARM::R10;
3390   case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
3391   case ARM::R12: return ARM::SP;  case ARM::SP:  return ARM::LR;
3392   case ARM::LR:  return ARM::PC;  case ARM::PC:  return ARM::R0;
3393   }
3394 }
3395 
3396 // Return the low-subreg of a given Q register.
3397 static unsigned getDRegFromQReg(unsigned QReg) {
3398   switch (QReg) {
3399   default: llvm_unreachable("expected a Q register!");
3400   case ARM::Q0:  return ARM::D0;
3401   case ARM::Q1:  return ARM::D2;
3402   case ARM::Q2:  return ARM::D4;
3403   case ARM::Q3:  return ARM::D6;
3404   case ARM::Q4:  return ARM::D8;
3405   case ARM::Q5:  return ARM::D10;
3406   case ARM::Q6:  return ARM::D12;
3407   case ARM::Q7:  return ARM::D14;
3408   case ARM::Q8:  return ARM::D16;
3409   case ARM::Q9:  return ARM::D18;
3410   case ARM::Q10: return ARM::D20;
3411   case ARM::Q11: return ARM::D22;
3412   case ARM::Q12: return ARM::D24;
3413   case ARM::Q13: return ARM::D26;
3414   case ARM::Q14: return ARM::D28;
3415   case ARM::Q15: return ARM::D30;
3416   }
3417 }
3418 
3419 /// Parse a register list.
3420 bool ARMAsmParser::parseRegisterList(OperandVector &Operands) {
3421   MCAsmParser &Parser = getParser();
3422   assert(Parser.getTok().is(AsmToken::LCurly) &&
3423          "Token is not a Left Curly Brace");
3424   SMLoc S = Parser.getTok().getLoc();
3425   Parser.Lex(); // Eat '{' token.
3426   SMLoc RegLoc = Parser.getTok().getLoc();
3427 
3428   // Check the first register in the list to see what register class
3429   // this is a list of.
3430   int Reg = tryParseRegister();
3431   if (Reg == -1)
3432     return Error(RegLoc, "register expected");
3433 
3434   // The reglist instructions have at most 16 registers, so reserve
3435   // space for that many.
3436   int EReg = 0;
3437   SmallVector<std::pair<unsigned, unsigned>, 16> Registers;
3438 
3439   // Allow Q regs and just interpret them as the two D sub-registers.
3440   if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3441     Reg = getDRegFromQReg(Reg);
3442     EReg = MRI->getEncodingValue(Reg);
3443     Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3444     ++Reg;
3445   }
3446   const MCRegisterClass *RC;
3447   if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
3448     RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
3449   else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
3450     RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
3451   else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
3452     RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
3453   else
3454     return Error(RegLoc, "invalid register in register list");
3455 
3456   // Store the register.
3457   EReg = MRI->getEncodingValue(Reg);
3458   Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3459 
3460   // This starts immediately after the first register token in the list,
3461   // so we can see either a comma or a minus (range separator) as a legal
3462   // next token.
3463   while (Parser.getTok().is(AsmToken::Comma) ||
3464          Parser.getTok().is(AsmToken::Minus)) {
3465     if (Parser.getTok().is(AsmToken::Minus)) {
3466       Parser.Lex(); // Eat the minus.
3467       SMLoc AfterMinusLoc = Parser.getTok().getLoc();
3468       int EndReg = tryParseRegister();
3469       if (EndReg == -1)
3470         return Error(AfterMinusLoc, "register expected");
3471       // Allow Q regs and just interpret them as the two D sub-registers.
3472       if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
3473         EndReg = getDRegFromQReg(EndReg) + 1;
3474       // If the register is the same as the start reg, there's nothing
3475       // more to do.
3476       if (Reg == EndReg)
3477         continue;
3478       // The register must be in the same register class as the first.
3479       if (!RC->contains(EndReg))
3480         return Error(AfterMinusLoc, "invalid register in register list");
3481       // Ranges must go from low to high.
3482       if (MRI->getEncodingValue(Reg) > MRI->getEncodingValue(EndReg))
3483         return Error(AfterMinusLoc, "bad range in register list");
3484 
3485       // Add all the registers in the range to the register list.
3486       while (Reg != EndReg) {
3487         Reg = getNextRegister(Reg);
3488         EReg = MRI->getEncodingValue(Reg);
3489         Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3490       }
3491       continue;
3492     }
3493     Parser.Lex(); // Eat the comma.
3494     RegLoc = Parser.getTok().getLoc();
3495     int OldReg = Reg;
3496     const AsmToken RegTok = Parser.getTok();
3497     Reg = tryParseRegister();
3498     if (Reg == -1)
3499       return Error(RegLoc, "register expected");
3500     // Allow Q regs and just interpret them as the two D sub-registers.
3501     bool isQReg = false;
3502     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3503       Reg = getDRegFromQReg(Reg);
3504       isQReg = true;
3505     }
3506     // The register must be in the same register class as the first.
3507     if (!RC->contains(Reg))
3508       return Error(RegLoc, "invalid register in register list");
3509     // List must be monotonically increasing.
3510     if (MRI->getEncodingValue(Reg) < MRI->getEncodingValue(OldReg)) {
3511       if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
3512         Warning(RegLoc, "register list not in ascending order");
3513       else
3514         return Error(RegLoc, "register list not in ascending order");
3515     }
3516     if (MRI->getEncodingValue(Reg) == MRI->getEncodingValue(OldReg)) {
3517       Warning(RegLoc, "duplicated register (" + RegTok.getString() +
3518               ") in register list");
3519       continue;
3520     }
3521     // VFP register lists must also be contiguous.
3522     if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
3523         Reg != OldReg + 1)
3524       return Error(RegLoc, "non-contiguous register range");
3525     EReg = MRI->getEncodingValue(Reg);
3526     Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3527     if (isQReg) {
3528       EReg = MRI->getEncodingValue(++Reg);
3529       Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3530     }
3531   }
3532 
3533   if (Parser.getTok().isNot(AsmToken::RCurly))
3534     return Error(Parser.getTok().getLoc(), "'}' expected");
3535   SMLoc E = Parser.getTok().getEndLoc();
3536   Parser.Lex(); // Eat '}' token.
3537 
3538   // Push the register list operand.
3539   Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
3540 
3541   // The ARM system instruction variants for LDM/STM have a '^' token here.
3542   if (Parser.getTok().is(AsmToken::Caret)) {
3543     Operands.push_back(ARMOperand::CreateToken("^",Parser.getTok().getLoc()));
3544     Parser.Lex(); // Eat '^' token.
3545   }
3546 
3547   return false;
3548 }
3549 
3550 // Helper function to parse the lane index for vector lists.
3551 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3552 parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index, SMLoc &EndLoc) {
3553   MCAsmParser &Parser = getParser();
3554   Index = 0; // Always return a defined index value.
3555   if (Parser.getTok().is(AsmToken::LBrac)) {
3556     Parser.Lex(); // Eat the '['.
3557     if (Parser.getTok().is(AsmToken::RBrac)) {
3558       // "Dn[]" is the 'all lanes' syntax.
3559       LaneKind = AllLanes;
3560       EndLoc = Parser.getTok().getEndLoc();
3561       Parser.Lex(); // Eat the ']'.
3562       return MatchOperand_Success;
3563     }
3564 
3565     // There's an optional '#' token here. Normally there wouldn't be, but
3566     // inline assemble puts one in, and it's friendly to accept that.
3567     if (Parser.getTok().is(AsmToken::Hash))
3568       Parser.Lex(); // Eat '#' or '$'.
3569 
3570     const MCExpr *LaneIndex;
3571     SMLoc Loc = Parser.getTok().getLoc();
3572     if (getParser().parseExpression(LaneIndex)) {
3573       Error(Loc, "illegal expression");
3574       return MatchOperand_ParseFail;
3575     }
3576     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LaneIndex);
3577     if (!CE) {
3578       Error(Loc, "lane index must be empty or an integer");
3579       return MatchOperand_ParseFail;
3580     }
3581     if (Parser.getTok().isNot(AsmToken::RBrac)) {
3582       Error(Parser.getTok().getLoc(), "']' expected");
3583       return MatchOperand_ParseFail;
3584     }
3585     EndLoc = Parser.getTok().getEndLoc();
3586     Parser.Lex(); // Eat the ']'.
3587     int64_t Val = CE->getValue();
3588 
3589     // FIXME: Make this range check context sensitive for .8, .16, .32.
3590     if (Val < 0 || Val > 7) {
3591       Error(Parser.getTok().getLoc(), "lane index out of range");
3592       return MatchOperand_ParseFail;
3593     }
3594     Index = Val;
3595     LaneKind = IndexedLane;
3596     return MatchOperand_Success;
3597   }
3598   LaneKind = NoLanes;
3599   return MatchOperand_Success;
3600 }
3601 
3602 // parse a vector register list
3603 ARMAsmParser::OperandMatchResultTy
3604 ARMAsmParser::parseVectorList(OperandVector &Operands) {
3605   MCAsmParser &Parser = getParser();
3606   VectorLaneTy LaneKind;
3607   unsigned LaneIndex;
3608   SMLoc S = Parser.getTok().getLoc();
3609   // As an extension (to match gas), support a plain D register or Q register
3610   // (without encosing curly braces) as a single or double entry list,
3611   // respectively.
3612   if (Parser.getTok().is(AsmToken::Identifier)) {
3613     SMLoc E = Parser.getTok().getEndLoc();
3614     int Reg = tryParseRegister();
3615     if (Reg == -1)
3616       return MatchOperand_NoMatch;
3617     if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg)) {
3618       OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex, E);
3619       if (Res != MatchOperand_Success)
3620         return Res;
3621       switch (LaneKind) {
3622       case NoLanes:
3623         Operands.push_back(ARMOperand::CreateVectorList(Reg, 1, false, S, E));
3624         break;
3625       case AllLanes:
3626         Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 1, false,
3627                                                                 S, E));
3628         break;
3629       case IndexedLane:
3630         Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 1,
3631                                                                LaneIndex,
3632                                                                false, S, E));
3633         break;
3634       }
3635       return MatchOperand_Success;
3636     }
3637     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3638       Reg = getDRegFromQReg(Reg);
3639       OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex, E);
3640       if (Res != MatchOperand_Success)
3641         return Res;
3642       switch (LaneKind) {
3643       case NoLanes:
3644         Reg = MRI->getMatchingSuperReg(Reg, ARM::dsub_0,
3645                                    &ARMMCRegisterClasses[ARM::DPairRegClassID]);
3646         Operands.push_back(ARMOperand::CreateVectorList(Reg, 2, false, S, E));
3647         break;
3648       case AllLanes:
3649         Reg = MRI->getMatchingSuperReg(Reg, ARM::dsub_0,
3650                                    &ARMMCRegisterClasses[ARM::DPairRegClassID]);
3651         Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 2, false,
3652                                                                 S, E));
3653         break;
3654       case IndexedLane:
3655         Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 2,
3656                                                                LaneIndex,
3657                                                                false, S, E));
3658         break;
3659       }
3660       return MatchOperand_Success;
3661     }
3662     Error(S, "vector register expected");
3663     return MatchOperand_ParseFail;
3664   }
3665 
3666   if (Parser.getTok().isNot(AsmToken::LCurly))
3667     return MatchOperand_NoMatch;
3668 
3669   Parser.Lex(); // Eat '{' token.
3670   SMLoc RegLoc = Parser.getTok().getLoc();
3671 
3672   int Reg = tryParseRegister();
3673   if (Reg == -1) {
3674     Error(RegLoc, "register expected");
3675     return MatchOperand_ParseFail;
3676   }
3677   unsigned Count = 1;
3678   int Spacing = 0;
3679   unsigned FirstReg = Reg;
3680   // The list is of D registers, but we also allow Q regs and just interpret
3681   // them as the two D sub-registers.
3682   if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3683     FirstReg = Reg = getDRegFromQReg(Reg);
3684     Spacing = 1; // double-spacing requires explicit D registers, otherwise
3685                  // it's ambiguous with four-register single spaced.
3686     ++Reg;
3687     ++Count;
3688   }
3689 
3690   SMLoc E;
3691   if (parseVectorLane(LaneKind, LaneIndex, E) != MatchOperand_Success)
3692     return MatchOperand_ParseFail;
3693 
3694   while (Parser.getTok().is(AsmToken::Comma) ||
3695          Parser.getTok().is(AsmToken::Minus)) {
3696     if (Parser.getTok().is(AsmToken::Minus)) {
3697       if (!Spacing)
3698         Spacing = 1; // Register range implies a single spaced list.
3699       else if (Spacing == 2) {
3700         Error(Parser.getTok().getLoc(),
3701               "sequential registers in double spaced list");
3702         return MatchOperand_ParseFail;
3703       }
3704       Parser.Lex(); // Eat the minus.
3705       SMLoc AfterMinusLoc = Parser.getTok().getLoc();
3706       int EndReg = tryParseRegister();
3707       if (EndReg == -1) {
3708         Error(AfterMinusLoc, "register expected");
3709         return MatchOperand_ParseFail;
3710       }
3711       // Allow Q regs and just interpret them as the two D sub-registers.
3712       if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
3713         EndReg = getDRegFromQReg(EndReg) + 1;
3714       // If the register is the same as the start reg, there's nothing
3715       // more to do.
3716       if (Reg == EndReg)
3717         continue;
3718       // The register must be in the same register class as the first.
3719       if (!ARMMCRegisterClasses[ARM::DPRRegClassID].contains(EndReg)) {
3720         Error(AfterMinusLoc, "invalid register in register list");
3721         return MatchOperand_ParseFail;
3722       }
3723       // Ranges must go from low to high.
3724       if (Reg > EndReg) {
3725         Error(AfterMinusLoc, "bad range in register list");
3726         return MatchOperand_ParseFail;
3727       }
3728       // Parse the lane specifier if present.
3729       VectorLaneTy NextLaneKind;
3730       unsigned NextLaneIndex;
3731       if (parseVectorLane(NextLaneKind, NextLaneIndex, E) !=
3732           MatchOperand_Success)
3733         return MatchOperand_ParseFail;
3734       if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3735         Error(AfterMinusLoc, "mismatched lane index in register list");
3736         return MatchOperand_ParseFail;
3737       }
3738 
3739       // Add all the registers in the range to the register list.
3740       Count += EndReg - Reg;
3741       Reg = EndReg;
3742       continue;
3743     }
3744     Parser.Lex(); // Eat the comma.
3745     RegLoc = Parser.getTok().getLoc();
3746     int OldReg = Reg;
3747     Reg = tryParseRegister();
3748     if (Reg == -1) {
3749       Error(RegLoc, "register expected");
3750       return MatchOperand_ParseFail;
3751     }
3752     // vector register lists must be contiguous.
3753     // It's OK to use the enumeration values directly here rather, as the
3754     // VFP register classes have the enum sorted properly.
3755     //
3756     // The list is of D registers, but we also allow Q regs and just interpret
3757     // them as the two D sub-registers.
3758     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3759       if (!Spacing)
3760         Spacing = 1; // Register range implies a single spaced list.
3761       else if (Spacing == 2) {
3762         Error(RegLoc,
3763               "invalid register in double-spaced list (must be 'D' register')");
3764         return MatchOperand_ParseFail;
3765       }
3766       Reg = getDRegFromQReg(Reg);
3767       if (Reg != OldReg + 1) {
3768         Error(RegLoc, "non-contiguous register range");
3769         return MatchOperand_ParseFail;
3770       }
3771       ++Reg;
3772       Count += 2;
3773       // Parse the lane specifier if present.
3774       VectorLaneTy NextLaneKind;
3775       unsigned NextLaneIndex;
3776       SMLoc LaneLoc = Parser.getTok().getLoc();
3777       if (parseVectorLane(NextLaneKind, NextLaneIndex, E) !=
3778           MatchOperand_Success)
3779         return MatchOperand_ParseFail;
3780       if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3781         Error(LaneLoc, "mismatched lane index in register list");
3782         return MatchOperand_ParseFail;
3783       }
3784       continue;
3785     }
3786     // Normal D register.
3787     // Figure out the register spacing (single or double) of the list if
3788     // we don't know it already.
3789     if (!Spacing)
3790       Spacing = 1 + (Reg == OldReg + 2);
3791 
3792     // Just check that it's contiguous and keep going.
3793     if (Reg != OldReg + Spacing) {
3794       Error(RegLoc, "non-contiguous register range");
3795       return MatchOperand_ParseFail;
3796     }
3797     ++Count;
3798     // Parse the lane specifier if present.
3799     VectorLaneTy NextLaneKind;
3800     unsigned NextLaneIndex;
3801     SMLoc EndLoc = Parser.getTok().getLoc();
3802     if (parseVectorLane(NextLaneKind, NextLaneIndex, E) != MatchOperand_Success)
3803       return MatchOperand_ParseFail;
3804     if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3805       Error(EndLoc, "mismatched lane index in register list");
3806       return MatchOperand_ParseFail;
3807     }
3808   }
3809 
3810   if (Parser.getTok().isNot(AsmToken::RCurly)) {
3811     Error(Parser.getTok().getLoc(), "'}' expected");
3812     return MatchOperand_ParseFail;
3813   }
3814   E = Parser.getTok().getEndLoc();
3815   Parser.Lex(); // Eat '}' token.
3816 
3817   switch (LaneKind) {
3818   case NoLanes:
3819     // Two-register operands have been converted to the
3820     // composite register classes.
3821     if (Count == 2) {
3822       const MCRegisterClass *RC = (Spacing == 1) ?
3823         &ARMMCRegisterClasses[ARM::DPairRegClassID] :
3824         &ARMMCRegisterClasses[ARM::DPairSpcRegClassID];
3825       FirstReg = MRI->getMatchingSuperReg(FirstReg, ARM::dsub_0, RC);
3826     }
3827 
3828     Operands.push_back(ARMOperand::CreateVectorList(FirstReg, Count,
3829                                                     (Spacing == 2), S, E));
3830     break;
3831   case AllLanes:
3832     // Two-register operands have been converted to the
3833     // composite register classes.
3834     if (Count == 2) {
3835       const MCRegisterClass *RC = (Spacing == 1) ?
3836         &ARMMCRegisterClasses[ARM::DPairRegClassID] :
3837         &ARMMCRegisterClasses[ARM::DPairSpcRegClassID];
3838       FirstReg = MRI->getMatchingSuperReg(FirstReg, ARM::dsub_0, RC);
3839     }
3840     Operands.push_back(ARMOperand::CreateVectorListAllLanes(FirstReg, Count,
3841                                                             (Spacing == 2),
3842                                                             S, E));
3843     break;
3844   case IndexedLane:
3845     Operands.push_back(ARMOperand::CreateVectorListIndexed(FirstReg, Count,
3846                                                            LaneIndex,
3847                                                            (Spacing == 2),
3848                                                            S, E));
3849     break;
3850   }
3851   return MatchOperand_Success;
3852 }
3853 
3854 /// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
3855 ARMAsmParser::OperandMatchResultTy
3856 ARMAsmParser::parseMemBarrierOptOperand(OperandVector &Operands) {
3857   MCAsmParser &Parser = getParser();
3858   SMLoc S = Parser.getTok().getLoc();
3859   const AsmToken &Tok = Parser.getTok();
3860   unsigned Opt;
3861 
3862   if (Tok.is(AsmToken::Identifier)) {
3863     StringRef OptStr = Tok.getString();
3864 
3865     Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()).lower())
3866       .Case("sy",    ARM_MB::SY)
3867       .Case("st",    ARM_MB::ST)
3868       .Case("ld",    ARM_MB::LD)
3869       .Case("sh",    ARM_MB::ISH)
3870       .Case("ish",   ARM_MB::ISH)
3871       .Case("shst",  ARM_MB::ISHST)
3872       .Case("ishst", ARM_MB::ISHST)
3873       .Case("ishld", ARM_MB::ISHLD)
3874       .Case("nsh",   ARM_MB::NSH)
3875       .Case("un",    ARM_MB::NSH)
3876       .Case("nshst", ARM_MB::NSHST)
3877       .Case("nshld", ARM_MB::NSHLD)
3878       .Case("unst",  ARM_MB::NSHST)
3879       .Case("osh",   ARM_MB::OSH)
3880       .Case("oshst", ARM_MB::OSHST)
3881       .Case("oshld", ARM_MB::OSHLD)
3882       .Default(~0U);
3883 
3884     // ishld, oshld, nshld and ld are only available from ARMv8.
3885     if (!hasV8Ops() && (Opt == ARM_MB::ISHLD || Opt == ARM_MB::OSHLD ||
3886                         Opt == ARM_MB::NSHLD || Opt == ARM_MB::LD))
3887       Opt = ~0U;
3888 
3889     if (Opt == ~0U)
3890       return MatchOperand_NoMatch;
3891 
3892     Parser.Lex(); // Eat identifier token.
3893   } else if (Tok.is(AsmToken::Hash) ||
3894              Tok.is(AsmToken::Dollar) ||
3895              Tok.is(AsmToken::Integer)) {
3896     if (Parser.getTok().isNot(AsmToken::Integer))
3897       Parser.Lex(); // Eat '#' or '$'.
3898     SMLoc Loc = Parser.getTok().getLoc();
3899 
3900     const MCExpr *MemBarrierID;
3901     if (getParser().parseExpression(MemBarrierID)) {
3902       Error(Loc, "illegal expression");
3903       return MatchOperand_ParseFail;
3904     }
3905 
3906     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(MemBarrierID);
3907     if (!CE) {
3908       Error(Loc, "constant expression expected");
3909       return MatchOperand_ParseFail;
3910     }
3911 
3912     int Val = CE->getValue();
3913     if (Val & ~0xf) {
3914       Error(Loc, "immediate value out of range");
3915       return MatchOperand_ParseFail;
3916     }
3917 
3918     Opt = ARM_MB::RESERVED_0 + Val;
3919   } else
3920     return MatchOperand_ParseFail;
3921 
3922   Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
3923   return MatchOperand_Success;
3924 }
3925 
3926 /// parseInstSyncBarrierOptOperand - Try to parse ISB inst sync barrier options.
3927 ARMAsmParser::OperandMatchResultTy
3928 ARMAsmParser::parseInstSyncBarrierOptOperand(OperandVector &Operands) {
3929   MCAsmParser &Parser = getParser();
3930   SMLoc S = Parser.getTok().getLoc();
3931   const AsmToken &Tok = Parser.getTok();
3932   unsigned Opt;
3933 
3934   if (Tok.is(AsmToken::Identifier)) {
3935     StringRef OptStr = Tok.getString();
3936 
3937     if (OptStr.equals_lower("sy"))
3938       Opt = ARM_ISB::SY;
3939     else
3940       return MatchOperand_NoMatch;
3941 
3942     Parser.Lex(); // Eat identifier token.
3943   } else if (Tok.is(AsmToken::Hash) ||
3944              Tok.is(AsmToken::Dollar) ||
3945              Tok.is(AsmToken::Integer)) {
3946     if (Parser.getTok().isNot(AsmToken::Integer))
3947       Parser.Lex(); // Eat '#' or '$'.
3948     SMLoc Loc = Parser.getTok().getLoc();
3949 
3950     const MCExpr *ISBarrierID;
3951     if (getParser().parseExpression(ISBarrierID)) {
3952       Error(Loc, "illegal expression");
3953       return MatchOperand_ParseFail;
3954     }
3955 
3956     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ISBarrierID);
3957     if (!CE) {
3958       Error(Loc, "constant expression expected");
3959       return MatchOperand_ParseFail;
3960     }
3961 
3962     int Val = CE->getValue();
3963     if (Val & ~0xf) {
3964       Error(Loc, "immediate value out of range");
3965       return MatchOperand_ParseFail;
3966     }
3967 
3968     Opt = ARM_ISB::RESERVED_0 + Val;
3969   } else
3970     return MatchOperand_ParseFail;
3971 
3972   Operands.push_back(ARMOperand::CreateInstSyncBarrierOpt(
3973           (ARM_ISB::InstSyncBOpt)Opt, S));
3974   return MatchOperand_Success;
3975 }
3976 
3977 
3978 /// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
3979 ARMAsmParser::OperandMatchResultTy
3980 ARMAsmParser::parseProcIFlagsOperand(OperandVector &Operands) {
3981   MCAsmParser &Parser = getParser();
3982   SMLoc S = Parser.getTok().getLoc();
3983   const AsmToken &Tok = Parser.getTok();
3984   if (!Tok.is(AsmToken::Identifier))
3985     return MatchOperand_NoMatch;
3986   StringRef IFlagsStr = Tok.getString();
3987 
3988   // An iflags string of "none" is interpreted to mean that none of the AIF
3989   // bits are set.  Not a terribly useful instruction, but a valid encoding.
3990   unsigned IFlags = 0;
3991   if (IFlagsStr != "none") {
3992         for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
3993       unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
3994         .Case("a", ARM_PROC::A)
3995         .Case("i", ARM_PROC::I)
3996         .Case("f", ARM_PROC::F)
3997         .Default(~0U);
3998 
3999       // If some specific iflag is already set, it means that some letter is
4000       // present more than once, this is not acceptable.
4001       if (Flag == ~0U || (IFlags & Flag))
4002         return MatchOperand_NoMatch;
4003 
4004       IFlags |= Flag;
4005     }
4006   }
4007 
4008   Parser.Lex(); // Eat identifier token.
4009   Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
4010   return MatchOperand_Success;
4011 }
4012 
4013 /// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
4014 ARMAsmParser::OperandMatchResultTy
4015 ARMAsmParser::parseMSRMaskOperand(OperandVector &Operands) {
4016   MCAsmParser &Parser = getParser();
4017   SMLoc S = Parser.getTok().getLoc();
4018   const AsmToken &Tok = Parser.getTok();
4019   if (!Tok.is(AsmToken::Identifier))
4020     return MatchOperand_NoMatch;
4021   StringRef Mask = Tok.getString();
4022 
4023   if (isMClass()) {
4024     // See ARMv6-M 10.1.1
4025     std::string Name = Mask.lower();
4026     unsigned FlagsVal = StringSwitch<unsigned>(Name)
4027       // Note: in the documentation:
4028       //  ARM deprecates using MSR APSR without a _<bits> qualifier as an alias
4029       //  for MSR APSR_nzcvq.
4030       // but we do make it an alias here.  This is so to get the "mask encoding"
4031       // bits correct on MSR APSR writes.
4032       //
4033       // FIXME: Note the 0xc00 "mask encoding" bits version of the registers
4034       // should really only be allowed when writing a special register.  Note
4035       // they get dropped in the MRS instruction reading a special register as
4036       // the SYSm field is only 8 bits.
4037       .Case("apsr", 0x800)
4038       .Case("apsr_nzcvq", 0x800)
4039       .Case("apsr_g", 0x400)
4040       .Case("apsr_nzcvqg", 0xc00)
4041       .Case("iapsr", 0x801)
4042       .Case("iapsr_nzcvq", 0x801)
4043       .Case("iapsr_g", 0x401)
4044       .Case("iapsr_nzcvqg", 0xc01)
4045       .Case("eapsr", 0x802)
4046       .Case("eapsr_nzcvq", 0x802)
4047       .Case("eapsr_g", 0x402)
4048       .Case("eapsr_nzcvqg", 0xc02)
4049       .Case("xpsr", 0x803)
4050       .Case("xpsr_nzcvq", 0x803)
4051       .Case("xpsr_g", 0x403)
4052       .Case("xpsr_nzcvqg", 0xc03)
4053       .Case("ipsr", 0x805)
4054       .Case("epsr", 0x806)
4055       .Case("iepsr", 0x807)
4056       .Case("msp", 0x808)
4057       .Case("psp", 0x809)
4058       .Case("primask", 0x810)
4059       .Case("basepri", 0x811)
4060       .Case("basepri_max", 0x812)
4061       .Case("faultmask", 0x813)
4062       .Case("control", 0x814)
4063       .Case("msplim", 0x80a)
4064       .Case("psplim", 0x80b)
4065       .Case("msp_ns", 0x888)
4066       .Case("psp_ns", 0x889)
4067       .Case("msplim_ns", 0x88a)
4068       .Case("psplim_ns", 0x88b)
4069       .Case("primask_ns", 0x890)
4070       .Case("basepri_ns", 0x891)
4071       .Case("basepri_max_ns", 0x892)
4072       .Case("faultmask_ns", 0x893)
4073       .Case("control_ns", 0x894)
4074       .Case("sp_ns", 0x898)
4075       .Default(~0U);
4076 
4077     if (FlagsVal == ~0U)
4078       return MatchOperand_NoMatch;
4079 
4080     if (!hasDSP() && (FlagsVal & 0x400))
4081       // The _g and _nzcvqg versions are only valid if the DSP extension is
4082       // available.
4083       return MatchOperand_NoMatch;
4084 
4085     if (!hasV7Ops() && FlagsVal >= 0x811 && FlagsVal <= 0x813)
4086       // basepri, basepri_max and faultmask only valid for V7m.
4087       return MatchOperand_NoMatch;
4088 
4089     if (!has8MSecExt() && (FlagsVal == 0x80a || FlagsVal == 0x80b ||
4090                              (FlagsVal > 0x814 && FlagsVal < 0xc00)))
4091       return MatchOperand_NoMatch;
4092 
4093     if (!hasV8MMainline() && (FlagsVal == 0x88a || FlagsVal == 0x88b ||
4094                               (FlagsVal > 0x890 && FlagsVal <= 0x893)))
4095       return MatchOperand_NoMatch;
4096 
4097     Parser.Lex(); // Eat identifier token.
4098     Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
4099     return MatchOperand_Success;
4100   }
4101 
4102   // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
4103   size_t Start = 0, Next = Mask.find('_');
4104   StringRef Flags = "";
4105   std::string SpecReg = Mask.slice(Start, Next).lower();
4106   if (Next != StringRef::npos)
4107     Flags = Mask.slice(Next+1, Mask.size());
4108 
4109   // FlagsVal contains the complete mask:
4110   // 3-0: Mask
4111   // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
4112   unsigned FlagsVal = 0;
4113 
4114   if (SpecReg == "apsr") {
4115     FlagsVal = StringSwitch<unsigned>(Flags)
4116     .Case("nzcvq",  0x8) // same as CPSR_f
4117     .Case("g",      0x4) // same as CPSR_s
4118     .Case("nzcvqg", 0xc) // same as CPSR_fs
4119     .Default(~0U);
4120 
4121     if (FlagsVal == ~0U) {
4122       if (!Flags.empty())
4123         return MatchOperand_NoMatch;
4124       else
4125         FlagsVal = 8; // No flag
4126     }
4127   } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
4128     // cpsr_all is an alias for cpsr_fc, as is plain cpsr.
4129     if (Flags == "all" || Flags == "")
4130       Flags = "fc";
4131     for (int i = 0, e = Flags.size(); i != e; ++i) {
4132       unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
4133       .Case("c", 1)
4134       .Case("x", 2)
4135       .Case("s", 4)
4136       .Case("f", 8)
4137       .Default(~0U);
4138 
4139       // If some specific flag is already set, it means that some letter is
4140       // present more than once, this is not acceptable.
4141       if (FlagsVal == ~0U || (FlagsVal & Flag))
4142         return MatchOperand_NoMatch;
4143       FlagsVal |= Flag;
4144     }
4145   } else // No match for special register.
4146     return MatchOperand_NoMatch;
4147 
4148   // Special register without flags is NOT equivalent to "fc" flags.
4149   // NOTE: This is a divergence from gas' behavior.  Uncommenting the following
4150   // two lines would enable gas compatibility at the expense of breaking
4151   // round-tripping.
4152   //
4153   // if (!FlagsVal)
4154   //  FlagsVal = 0x9;
4155 
4156   // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
4157   if (SpecReg == "spsr")
4158     FlagsVal |= 16;
4159 
4160   Parser.Lex(); // Eat identifier token.
4161   Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
4162   return MatchOperand_Success;
4163 }
4164 
4165 /// parseBankedRegOperand - Try to parse a banked register (e.g. "lr_irq") for
4166 /// use in the MRS/MSR instructions added to support virtualization.
4167 ARMAsmParser::OperandMatchResultTy
4168 ARMAsmParser::parseBankedRegOperand(OperandVector &Operands) {
4169   MCAsmParser &Parser = getParser();
4170   SMLoc S = Parser.getTok().getLoc();
4171   const AsmToken &Tok = Parser.getTok();
4172   if (!Tok.is(AsmToken::Identifier))
4173     return MatchOperand_NoMatch;
4174   StringRef RegName = Tok.getString();
4175 
4176   // The values here come from B9.2.3 of the ARM ARM, where bits 4-0 are SysM
4177   // and bit 5 is R.
4178   unsigned Encoding = StringSwitch<unsigned>(RegName.lower())
4179                           .Case("r8_usr", 0x00)
4180                           .Case("r9_usr", 0x01)
4181                           .Case("r10_usr", 0x02)
4182                           .Case("r11_usr", 0x03)
4183                           .Case("r12_usr", 0x04)
4184                           .Case("sp_usr", 0x05)
4185                           .Case("lr_usr", 0x06)
4186                           .Case("r8_fiq", 0x08)
4187                           .Case("r9_fiq", 0x09)
4188                           .Case("r10_fiq", 0x0a)
4189                           .Case("r11_fiq", 0x0b)
4190                           .Case("r12_fiq", 0x0c)
4191                           .Case("sp_fiq", 0x0d)
4192                           .Case("lr_fiq", 0x0e)
4193                           .Case("lr_irq", 0x10)
4194                           .Case("sp_irq", 0x11)
4195                           .Case("lr_svc", 0x12)
4196                           .Case("sp_svc", 0x13)
4197                           .Case("lr_abt", 0x14)
4198                           .Case("sp_abt", 0x15)
4199                           .Case("lr_und", 0x16)
4200                           .Case("sp_und", 0x17)
4201                           .Case("lr_mon", 0x1c)
4202                           .Case("sp_mon", 0x1d)
4203                           .Case("elr_hyp", 0x1e)
4204                           .Case("sp_hyp", 0x1f)
4205                           .Case("spsr_fiq", 0x2e)
4206                           .Case("spsr_irq", 0x30)
4207                           .Case("spsr_svc", 0x32)
4208                           .Case("spsr_abt", 0x34)
4209                           .Case("spsr_und", 0x36)
4210                           .Case("spsr_mon", 0x3c)
4211                           .Case("spsr_hyp", 0x3e)
4212                           .Default(~0U);
4213 
4214   if (Encoding == ~0U)
4215     return MatchOperand_NoMatch;
4216 
4217   Parser.Lex(); // Eat identifier token.
4218   Operands.push_back(ARMOperand::CreateBankedReg(Encoding, S));
4219   return MatchOperand_Success;
4220 }
4221 
4222 ARMAsmParser::OperandMatchResultTy
4223 ARMAsmParser::parsePKHImm(OperandVector &Operands, StringRef Op, int Low,
4224                           int High) {
4225   MCAsmParser &Parser = getParser();
4226   const AsmToken &Tok = Parser.getTok();
4227   if (Tok.isNot(AsmToken::Identifier)) {
4228     Error(Parser.getTok().getLoc(), Op + " operand expected.");
4229     return MatchOperand_ParseFail;
4230   }
4231   StringRef ShiftName = Tok.getString();
4232   std::string LowerOp = Op.lower();
4233   std::string UpperOp = Op.upper();
4234   if (ShiftName != LowerOp && ShiftName != UpperOp) {
4235     Error(Parser.getTok().getLoc(), Op + " operand expected.");
4236     return MatchOperand_ParseFail;
4237   }
4238   Parser.Lex(); // Eat shift type token.
4239 
4240   // There must be a '#' and a shift amount.
4241   if (Parser.getTok().isNot(AsmToken::Hash) &&
4242       Parser.getTok().isNot(AsmToken::Dollar)) {
4243     Error(Parser.getTok().getLoc(), "'#' expected");
4244     return MatchOperand_ParseFail;
4245   }
4246   Parser.Lex(); // Eat hash token.
4247 
4248   const MCExpr *ShiftAmount;
4249   SMLoc Loc = Parser.getTok().getLoc();
4250   SMLoc EndLoc;
4251   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
4252     Error(Loc, "illegal expression");
4253     return MatchOperand_ParseFail;
4254   }
4255   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
4256   if (!CE) {
4257     Error(Loc, "constant expression expected");
4258     return MatchOperand_ParseFail;
4259   }
4260   int Val = CE->getValue();
4261   if (Val < Low || Val > High) {
4262     Error(Loc, "immediate value out of range");
4263     return MatchOperand_ParseFail;
4264   }
4265 
4266   Operands.push_back(ARMOperand::CreateImm(CE, Loc, EndLoc));
4267 
4268   return MatchOperand_Success;
4269 }
4270 
4271 ARMAsmParser::OperandMatchResultTy
4272 ARMAsmParser::parseSetEndImm(OperandVector &Operands) {
4273   MCAsmParser &Parser = getParser();
4274   const AsmToken &Tok = Parser.getTok();
4275   SMLoc S = Tok.getLoc();
4276   if (Tok.isNot(AsmToken::Identifier)) {
4277     Error(S, "'be' or 'le' operand expected");
4278     return MatchOperand_ParseFail;
4279   }
4280   int Val = StringSwitch<int>(Tok.getString().lower())
4281     .Case("be", 1)
4282     .Case("le", 0)
4283     .Default(-1);
4284   Parser.Lex(); // Eat the token.
4285 
4286   if (Val == -1) {
4287     Error(S, "'be' or 'le' operand expected");
4288     return MatchOperand_ParseFail;
4289   }
4290   Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::create(Val,
4291                                                                   getContext()),
4292                                            S, Tok.getEndLoc()));
4293   return MatchOperand_Success;
4294 }
4295 
4296 /// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
4297 /// instructions. Legal values are:
4298 ///     lsl #n  'n' in [0,31]
4299 ///     asr #n  'n' in [1,32]
4300 ///             n == 32 encoded as n == 0.
4301 ARMAsmParser::OperandMatchResultTy
4302 ARMAsmParser::parseShifterImm(OperandVector &Operands) {
4303   MCAsmParser &Parser = getParser();
4304   const AsmToken &Tok = Parser.getTok();
4305   SMLoc S = Tok.getLoc();
4306   if (Tok.isNot(AsmToken::Identifier)) {
4307     Error(S, "shift operator 'asr' or 'lsl' expected");
4308     return MatchOperand_ParseFail;
4309   }
4310   StringRef ShiftName = Tok.getString();
4311   bool isASR;
4312   if (ShiftName == "lsl" || ShiftName == "LSL")
4313     isASR = false;
4314   else if (ShiftName == "asr" || ShiftName == "ASR")
4315     isASR = true;
4316   else {
4317     Error(S, "shift operator 'asr' or 'lsl' expected");
4318     return MatchOperand_ParseFail;
4319   }
4320   Parser.Lex(); // Eat the operator.
4321 
4322   // A '#' and a shift amount.
4323   if (Parser.getTok().isNot(AsmToken::Hash) &&
4324       Parser.getTok().isNot(AsmToken::Dollar)) {
4325     Error(Parser.getTok().getLoc(), "'#' expected");
4326     return MatchOperand_ParseFail;
4327   }
4328   Parser.Lex(); // Eat hash token.
4329   SMLoc ExLoc = Parser.getTok().getLoc();
4330 
4331   const MCExpr *ShiftAmount;
4332   SMLoc EndLoc;
4333   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
4334     Error(ExLoc, "malformed shift expression");
4335     return MatchOperand_ParseFail;
4336   }
4337   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
4338   if (!CE) {
4339     Error(ExLoc, "shift amount must be an immediate");
4340     return MatchOperand_ParseFail;
4341   }
4342 
4343   int64_t Val = CE->getValue();
4344   if (isASR) {
4345     // Shift amount must be in [1,32]
4346     if (Val < 1 || Val > 32) {
4347       Error(ExLoc, "'asr' shift amount must be in range [1,32]");
4348       return MatchOperand_ParseFail;
4349     }
4350     // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
4351     if (isThumb() && Val == 32) {
4352       Error(ExLoc, "'asr #32' shift amount not allowed in Thumb mode");
4353       return MatchOperand_ParseFail;
4354     }
4355     if (Val == 32) Val = 0;
4356   } else {
4357     // Shift amount must be in [1,32]
4358     if (Val < 0 || Val > 31) {
4359       Error(ExLoc, "'lsr' shift amount must be in range [0,31]");
4360       return MatchOperand_ParseFail;
4361     }
4362   }
4363 
4364   Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, EndLoc));
4365 
4366   return MatchOperand_Success;
4367 }
4368 
4369 /// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
4370 /// of instructions. Legal values are:
4371 ///     ror #n  'n' in {0, 8, 16, 24}
4372 ARMAsmParser::OperandMatchResultTy
4373 ARMAsmParser::parseRotImm(OperandVector &Operands) {
4374   MCAsmParser &Parser = getParser();
4375   const AsmToken &Tok = Parser.getTok();
4376   SMLoc S = Tok.getLoc();
4377   if (Tok.isNot(AsmToken::Identifier))
4378     return MatchOperand_NoMatch;
4379   StringRef ShiftName = Tok.getString();
4380   if (ShiftName != "ror" && ShiftName != "ROR")
4381     return MatchOperand_NoMatch;
4382   Parser.Lex(); // Eat the operator.
4383 
4384   // A '#' and a rotate amount.
4385   if (Parser.getTok().isNot(AsmToken::Hash) &&
4386       Parser.getTok().isNot(AsmToken::Dollar)) {
4387     Error(Parser.getTok().getLoc(), "'#' expected");
4388     return MatchOperand_ParseFail;
4389   }
4390   Parser.Lex(); // Eat hash token.
4391   SMLoc ExLoc = Parser.getTok().getLoc();
4392 
4393   const MCExpr *ShiftAmount;
4394   SMLoc EndLoc;
4395   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
4396     Error(ExLoc, "malformed rotate expression");
4397     return MatchOperand_ParseFail;
4398   }
4399   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
4400   if (!CE) {
4401     Error(ExLoc, "rotate amount must be an immediate");
4402     return MatchOperand_ParseFail;
4403   }
4404 
4405   int64_t Val = CE->getValue();
4406   // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
4407   // normally, zero is represented in asm by omitting the rotate operand
4408   // entirely.
4409   if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
4410     Error(ExLoc, "'ror' rotate amount must be 8, 16, or 24");
4411     return MatchOperand_ParseFail;
4412   }
4413 
4414   Operands.push_back(ARMOperand::CreateRotImm(Val, S, EndLoc));
4415 
4416   return MatchOperand_Success;
4417 }
4418 
4419 ARMAsmParser::OperandMatchResultTy
4420 ARMAsmParser::parseModImm(OperandVector &Operands) {
4421   MCAsmParser &Parser = getParser();
4422   MCAsmLexer &Lexer = getLexer();
4423   int64_t Imm1, Imm2;
4424 
4425   SMLoc S = Parser.getTok().getLoc();
4426 
4427   // 1) A mod_imm operand can appear in the place of a register name:
4428   //   add r0, #mod_imm
4429   //   add r0, r0, #mod_imm
4430   // to correctly handle the latter, we bail out as soon as we see an
4431   // identifier.
4432   //
4433   // 2) Similarly, we do not want to parse into complex operands:
4434   //   mov r0, #mod_imm
4435   //   mov r0, :lower16:(_foo)
4436   if (Parser.getTok().is(AsmToken::Identifier) ||
4437       Parser.getTok().is(AsmToken::Colon))
4438     return MatchOperand_NoMatch;
4439 
4440   // Hash (dollar) is optional as per the ARMARM
4441   if (Parser.getTok().is(AsmToken::Hash) ||
4442       Parser.getTok().is(AsmToken::Dollar)) {
4443     // Avoid parsing into complex operands (#:)
4444     if (Lexer.peekTok().is(AsmToken::Colon))
4445       return MatchOperand_NoMatch;
4446 
4447     // Eat the hash (dollar)
4448     Parser.Lex();
4449   }
4450 
4451   SMLoc Sx1, Ex1;
4452   Sx1 = Parser.getTok().getLoc();
4453   const MCExpr *Imm1Exp;
4454   if (getParser().parseExpression(Imm1Exp, Ex1)) {
4455     Error(Sx1, "malformed expression");
4456     return MatchOperand_ParseFail;
4457   }
4458 
4459   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm1Exp);
4460 
4461   if (CE) {
4462     // Immediate must fit within 32-bits
4463     Imm1 = CE->getValue();
4464     int Enc = ARM_AM::getSOImmVal(Imm1);
4465     if (Enc != -1 && Parser.getTok().is(AsmToken::EndOfStatement)) {
4466       // We have a match!
4467       Operands.push_back(ARMOperand::CreateModImm((Enc & 0xFF),
4468                                                   (Enc & 0xF00) >> 7,
4469                                                   Sx1, Ex1));
4470       return MatchOperand_Success;
4471     }
4472 
4473     // We have parsed an immediate which is not for us, fallback to a plain
4474     // immediate. This can happen for instruction aliases. For an example,
4475     // ARMInstrInfo.td defines the alias [mov <-> mvn] which can transform
4476     // a mov (mvn) with a mod_imm_neg/mod_imm_not operand into the opposite
4477     // instruction with a mod_imm operand. The alias is defined such that the
4478     // parser method is shared, that's why we have to do this here.
4479     if (Parser.getTok().is(AsmToken::EndOfStatement)) {
4480       Operands.push_back(ARMOperand::CreateImm(Imm1Exp, Sx1, Ex1));
4481       return MatchOperand_Success;
4482     }
4483   } else {
4484     // Operands like #(l1 - l2) can only be evaluated at a later stage (via an
4485     // MCFixup). Fallback to a plain immediate.
4486     Operands.push_back(ARMOperand::CreateImm(Imm1Exp, Sx1, Ex1));
4487     return MatchOperand_Success;
4488   }
4489 
4490   // From this point onward, we expect the input to be a (#bits, #rot) pair
4491   if (Parser.getTok().isNot(AsmToken::Comma)) {
4492     Error(Sx1, "expected modified immediate operand: #[0, 255], #even[0-30]");
4493     return MatchOperand_ParseFail;
4494   }
4495 
4496   if (Imm1 & ~0xFF) {
4497     Error(Sx1, "immediate operand must a number in the range [0, 255]");
4498     return MatchOperand_ParseFail;
4499   }
4500 
4501   // Eat the comma
4502   Parser.Lex();
4503 
4504   // Repeat for #rot
4505   SMLoc Sx2, Ex2;
4506   Sx2 = Parser.getTok().getLoc();
4507 
4508   // Eat the optional hash (dollar)
4509   if (Parser.getTok().is(AsmToken::Hash) ||
4510       Parser.getTok().is(AsmToken::Dollar))
4511     Parser.Lex();
4512 
4513   const MCExpr *Imm2Exp;
4514   if (getParser().parseExpression(Imm2Exp, Ex2)) {
4515     Error(Sx2, "malformed expression");
4516     return MatchOperand_ParseFail;
4517   }
4518 
4519   CE = dyn_cast<MCConstantExpr>(Imm2Exp);
4520 
4521   if (CE) {
4522     Imm2 = CE->getValue();
4523     if (!(Imm2 & ~0x1E)) {
4524       // We have a match!
4525       Operands.push_back(ARMOperand::CreateModImm(Imm1, Imm2, S, Ex2));
4526       return MatchOperand_Success;
4527     }
4528     Error(Sx2, "immediate operand must an even number in the range [0, 30]");
4529     return MatchOperand_ParseFail;
4530   } else {
4531     Error(Sx2, "constant expression expected");
4532     return MatchOperand_ParseFail;
4533   }
4534 }
4535 
4536 ARMAsmParser::OperandMatchResultTy
4537 ARMAsmParser::parseBitfield(OperandVector &Operands) {
4538   MCAsmParser &Parser = getParser();
4539   SMLoc S = Parser.getTok().getLoc();
4540   // The bitfield descriptor is really two operands, the LSB and the width.
4541   if (Parser.getTok().isNot(AsmToken::Hash) &&
4542       Parser.getTok().isNot(AsmToken::Dollar)) {
4543     Error(Parser.getTok().getLoc(), "'#' expected");
4544     return MatchOperand_ParseFail;
4545   }
4546   Parser.Lex(); // Eat hash token.
4547 
4548   const MCExpr *LSBExpr;
4549   SMLoc E = Parser.getTok().getLoc();
4550   if (getParser().parseExpression(LSBExpr)) {
4551     Error(E, "malformed immediate expression");
4552     return MatchOperand_ParseFail;
4553   }
4554   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
4555   if (!CE) {
4556     Error(E, "'lsb' operand must be an immediate");
4557     return MatchOperand_ParseFail;
4558   }
4559 
4560   int64_t LSB = CE->getValue();
4561   // The LSB must be in the range [0,31]
4562   if (LSB < 0 || LSB > 31) {
4563     Error(E, "'lsb' operand must be in the range [0,31]");
4564     return MatchOperand_ParseFail;
4565   }
4566   E = Parser.getTok().getLoc();
4567 
4568   // Expect another immediate operand.
4569   if (Parser.getTok().isNot(AsmToken::Comma)) {
4570     Error(Parser.getTok().getLoc(), "too few operands");
4571     return MatchOperand_ParseFail;
4572   }
4573   Parser.Lex(); // Eat hash token.
4574   if (Parser.getTok().isNot(AsmToken::Hash) &&
4575       Parser.getTok().isNot(AsmToken::Dollar)) {
4576     Error(Parser.getTok().getLoc(), "'#' expected");
4577     return MatchOperand_ParseFail;
4578   }
4579   Parser.Lex(); // Eat hash token.
4580 
4581   const MCExpr *WidthExpr;
4582   SMLoc EndLoc;
4583   if (getParser().parseExpression(WidthExpr, EndLoc)) {
4584     Error(E, "malformed immediate expression");
4585     return MatchOperand_ParseFail;
4586   }
4587   CE = dyn_cast<MCConstantExpr>(WidthExpr);
4588   if (!CE) {
4589     Error(E, "'width' operand must be an immediate");
4590     return MatchOperand_ParseFail;
4591   }
4592 
4593   int64_t Width = CE->getValue();
4594   // The LSB must be in the range [1,32-lsb]
4595   if (Width < 1 || Width > 32 - LSB) {
4596     Error(E, "'width' operand must be in the range [1,32-lsb]");
4597     return MatchOperand_ParseFail;
4598   }
4599 
4600   Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, EndLoc));
4601 
4602   return MatchOperand_Success;
4603 }
4604 
4605 ARMAsmParser::OperandMatchResultTy
4606 ARMAsmParser::parsePostIdxReg(OperandVector &Operands) {
4607   // Check for a post-index addressing register operand. Specifically:
4608   // postidx_reg := '+' register {, shift}
4609   //              | '-' register {, shift}
4610   //              | register {, shift}
4611 
4612   // This method must return MatchOperand_NoMatch without consuming any tokens
4613   // in the case where there is no match, as other alternatives take other
4614   // parse methods.
4615   MCAsmParser &Parser = getParser();
4616   AsmToken Tok = Parser.getTok();
4617   SMLoc S = Tok.getLoc();
4618   bool haveEaten = false;
4619   bool isAdd = true;
4620   if (Tok.is(AsmToken::Plus)) {
4621     Parser.Lex(); // Eat the '+' token.
4622     haveEaten = true;
4623   } else if (Tok.is(AsmToken::Minus)) {
4624     Parser.Lex(); // Eat the '-' token.
4625     isAdd = false;
4626     haveEaten = true;
4627   }
4628 
4629   SMLoc E = Parser.getTok().getEndLoc();
4630   int Reg = tryParseRegister();
4631   if (Reg == -1) {
4632     if (!haveEaten)
4633       return MatchOperand_NoMatch;
4634     Error(Parser.getTok().getLoc(), "register expected");
4635     return MatchOperand_ParseFail;
4636   }
4637 
4638   ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
4639   unsigned ShiftImm = 0;
4640   if (Parser.getTok().is(AsmToken::Comma)) {
4641     Parser.Lex(); // Eat the ','.
4642     if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
4643       return MatchOperand_ParseFail;
4644 
4645     // FIXME: Only approximates end...may include intervening whitespace.
4646     E = Parser.getTok().getLoc();
4647   }
4648 
4649   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
4650                                                   ShiftImm, S, E));
4651 
4652   return MatchOperand_Success;
4653 }
4654 
4655 ARMAsmParser::OperandMatchResultTy
4656 ARMAsmParser::parseAM3Offset(OperandVector &Operands) {
4657   // Check for a post-index addressing register operand. Specifically:
4658   // am3offset := '+' register
4659   //              | '-' register
4660   //              | register
4661   //              | # imm
4662   //              | # + imm
4663   //              | # - imm
4664 
4665   // This method must return MatchOperand_NoMatch without consuming any tokens
4666   // in the case where there is no match, as other alternatives take other
4667   // parse methods.
4668   MCAsmParser &Parser = getParser();
4669   AsmToken Tok = Parser.getTok();
4670   SMLoc S = Tok.getLoc();
4671 
4672   // Do immediates first, as we always parse those if we have a '#'.
4673   if (Parser.getTok().is(AsmToken::Hash) ||
4674       Parser.getTok().is(AsmToken::Dollar)) {
4675     Parser.Lex(); // Eat '#' or '$'.
4676     // Explicitly look for a '-', as we need to encode negative zero
4677     // differently.
4678     bool isNegative = Parser.getTok().is(AsmToken::Minus);
4679     const MCExpr *Offset;
4680     SMLoc E;
4681     if (getParser().parseExpression(Offset, E))
4682       return MatchOperand_ParseFail;
4683     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
4684     if (!CE) {
4685       Error(S, "constant expression expected");
4686       return MatchOperand_ParseFail;
4687     }
4688     // Negative zero is encoded as the flag value INT32_MIN.
4689     int32_t Val = CE->getValue();
4690     if (isNegative && Val == 0)
4691       Val = INT32_MIN;
4692 
4693     Operands.push_back(
4694       ARMOperand::CreateImm(MCConstantExpr::create(Val, getContext()), S, E));
4695 
4696     return MatchOperand_Success;
4697   }
4698 
4699 
4700   bool haveEaten = false;
4701   bool isAdd = true;
4702   if (Tok.is(AsmToken::Plus)) {
4703     Parser.Lex(); // Eat the '+' token.
4704     haveEaten = true;
4705   } else if (Tok.is(AsmToken::Minus)) {
4706     Parser.Lex(); // Eat the '-' token.
4707     isAdd = false;
4708     haveEaten = true;
4709   }
4710 
4711   Tok = Parser.getTok();
4712   int Reg = tryParseRegister();
4713   if (Reg == -1) {
4714     if (!haveEaten)
4715       return MatchOperand_NoMatch;
4716     Error(Tok.getLoc(), "register expected");
4717     return MatchOperand_ParseFail;
4718   }
4719 
4720   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
4721                                                   0, S, Tok.getEndLoc()));
4722 
4723   return MatchOperand_Success;
4724 }
4725 
4726 /// Convert parsed operands to MCInst.  Needed here because this instruction
4727 /// only has two register operands, but multiplication is commutative so
4728 /// assemblers should accept both "mul rD, rN, rD" and "mul rD, rD, rN".
4729 void ARMAsmParser::cvtThumbMultiply(MCInst &Inst,
4730                                     const OperandVector &Operands) {
4731   ((ARMOperand &)*Operands[3]).addRegOperands(Inst, 1);
4732   ((ARMOperand &)*Operands[1]).addCCOutOperands(Inst, 1);
4733   // If we have a three-operand form, make sure to set Rn to be the operand
4734   // that isn't the same as Rd.
4735   unsigned RegOp = 4;
4736   if (Operands.size() == 6 &&
4737       ((ARMOperand &)*Operands[4]).getReg() ==
4738           ((ARMOperand &)*Operands[3]).getReg())
4739     RegOp = 5;
4740   ((ARMOperand &)*Operands[RegOp]).addRegOperands(Inst, 1);
4741   Inst.addOperand(Inst.getOperand(0));
4742   ((ARMOperand &)*Operands[2]).addCondCodeOperands(Inst, 2);
4743 }
4744 
4745 void ARMAsmParser::cvtThumbBranches(MCInst &Inst,
4746                                     const OperandVector &Operands) {
4747   int CondOp = -1, ImmOp = -1;
4748   switch(Inst.getOpcode()) {
4749     case ARM::tB:
4750     case ARM::tBcc:  CondOp = 1; ImmOp = 2; break;
4751 
4752     case ARM::t2B:
4753     case ARM::t2Bcc: CondOp = 1; ImmOp = 3; break;
4754 
4755     default: llvm_unreachable("Unexpected instruction in cvtThumbBranches");
4756   }
4757   // first decide whether or not the branch should be conditional
4758   // by looking at it's location relative to an IT block
4759   if(inITBlock()) {
4760     // inside an IT block we cannot have any conditional branches. any
4761     // such instructions needs to be converted to unconditional form
4762     switch(Inst.getOpcode()) {
4763       case ARM::tBcc: Inst.setOpcode(ARM::tB); break;
4764       case ARM::t2Bcc: Inst.setOpcode(ARM::t2B); break;
4765     }
4766   } else {
4767     // outside IT blocks we can only have unconditional branches with AL
4768     // condition code or conditional branches with non-AL condition code
4769     unsigned Cond = static_cast<ARMOperand &>(*Operands[CondOp]).getCondCode();
4770     switch(Inst.getOpcode()) {
4771       case ARM::tB:
4772       case ARM::tBcc:
4773         Inst.setOpcode(Cond == ARMCC::AL ? ARM::tB : ARM::tBcc);
4774         break;
4775       case ARM::t2B:
4776       case ARM::t2Bcc:
4777         Inst.setOpcode(Cond == ARMCC::AL ? ARM::t2B : ARM::t2Bcc);
4778         break;
4779     }
4780   }
4781 
4782   // now decide on encoding size based on branch target range
4783   switch(Inst.getOpcode()) {
4784     // classify tB as either t2B or t1B based on range of immediate operand
4785     case ARM::tB: {
4786       ARMOperand &op = static_cast<ARMOperand &>(*Operands[ImmOp]);
4787       if (!op.isSignedOffset<11, 1>() && isThumb() && hasV8MBaseline())
4788         Inst.setOpcode(ARM::t2B);
4789       break;
4790     }
4791     // classify tBcc as either t2Bcc or t1Bcc based on range of immediate operand
4792     case ARM::tBcc: {
4793       ARMOperand &op = static_cast<ARMOperand &>(*Operands[ImmOp]);
4794       if (!op.isSignedOffset<8, 1>() && isThumb() && hasV8MBaseline())
4795         Inst.setOpcode(ARM::t2Bcc);
4796       break;
4797     }
4798   }
4799   ((ARMOperand &)*Operands[ImmOp]).addImmOperands(Inst, 1);
4800   ((ARMOperand &)*Operands[CondOp]).addCondCodeOperands(Inst, 2);
4801 }
4802 
4803 /// Parse an ARM memory expression, return false if successful else return true
4804 /// or an error.  The first token must be a '[' when called.
4805 bool ARMAsmParser::parseMemory(OperandVector &Operands) {
4806   MCAsmParser &Parser = getParser();
4807   SMLoc S, E;
4808   assert(Parser.getTok().is(AsmToken::LBrac) &&
4809          "Token is not a Left Bracket");
4810   S = Parser.getTok().getLoc();
4811   Parser.Lex(); // Eat left bracket token.
4812 
4813   const AsmToken &BaseRegTok = Parser.getTok();
4814   int BaseRegNum = tryParseRegister();
4815   if (BaseRegNum == -1)
4816     return Error(BaseRegTok.getLoc(), "register expected");
4817 
4818   // The next token must either be a comma, a colon or a closing bracket.
4819   const AsmToken &Tok = Parser.getTok();
4820   if (!Tok.is(AsmToken::Colon) && !Tok.is(AsmToken::Comma) &&
4821       !Tok.is(AsmToken::RBrac))
4822     return Error(Tok.getLoc(), "malformed memory operand");
4823 
4824   if (Tok.is(AsmToken::RBrac)) {
4825     E = Tok.getEndLoc();
4826     Parser.Lex(); // Eat right bracket token.
4827 
4828     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, 0,
4829                                              ARM_AM::no_shift, 0, 0, false,
4830                                              S, E));
4831 
4832     // If there's a pre-indexing writeback marker, '!', just add it as a token
4833     // operand. It's rather odd, but syntactically valid.
4834     if (Parser.getTok().is(AsmToken::Exclaim)) {
4835       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4836       Parser.Lex(); // Eat the '!'.
4837     }
4838 
4839     return false;
4840   }
4841 
4842   assert((Tok.is(AsmToken::Colon) || Tok.is(AsmToken::Comma)) &&
4843          "Lost colon or comma in memory operand?!");
4844   if (Tok.is(AsmToken::Comma)) {
4845     Parser.Lex(); // Eat the comma.
4846   }
4847 
4848   // If we have a ':', it's an alignment specifier.
4849   if (Parser.getTok().is(AsmToken::Colon)) {
4850     Parser.Lex(); // Eat the ':'.
4851     E = Parser.getTok().getLoc();
4852     SMLoc AlignmentLoc = Tok.getLoc();
4853 
4854     const MCExpr *Expr;
4855     if (getParser().parseExpression(Expr))
4856      return true;
4857 
4858     // The expression has to be a constant. Memory references with relocations
4859     // don't come through here, as they use the <label> forms of the relevant
4860     // instructions.
4861     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
4862     if (!CE)
4863       return Error (E, "constant expression expected");
4864 
4865     unsigned Align = 0;
4866     switch (CE->getValue()) {
4867     default:
4868       return Error(E,
4869                    "alignment specifier must be 16, 32, 64, 128, or 256 bits");
4870     case 16:  Align = 2; break;
4871     case 32:  Align = 4; break;
4872     case 64:  Align = 8; break;
4873     case 128: Align = 16; break;
4874     case 256: Align = 32; break;
4875     }
4876 
4877     // Now we should have the closing ']'
4878     if (Parser.getTok().isNot(AsmToken::RBrac))
4879       return Error(Parser.getTok().getLoc(), "']' expected");
4880     E = Parser.getTok().getEndLoc();
4881     Parser.Lex(); // Eat right bracket token.
4882 
4883     // Don't worry about range checking the value here. That's handled by
4884     // the is*() predicates.
4885     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, 0,
4886                                              ARM_AM::no_shift, 0, Align,
4887                                              false, S, E, AlignmentLoc));
4888 
4889     // If there's a pre-indexing writeback marker, '!', just add it as a token
4890     // operand.
4891     if (Parser.getTok().is(AsmToken::Exclaim)) {
4892       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4893       Parser.Lex(); // Eat the '!'.
4894     }
4895 
4896     return false;
4897   }
4898 
4899   // If we have a '#', it's an immediate offset, else assume it's a register
4900   // offset. Be friendly and also accept a plain integer (without a leading
4901   // hash) for gas compatibility.
4902   if (Parser.getTok().is(AsmToken::Hash) ||
4903       Parser.getTok().is(AsmToken::Dollar) ||
4904       Parser.getTok().is(AsmToken::Integer)) {
4905     if (Parser.getTok().isNot(AsmToken::Integer))
4906       Parser.Lex(); // Eat '#' or '$'.
4907     E = Parser.getTok().getLoc();
4908 
4909     bool isNegative = getParser().getTok().is(AsmToken::Minus);
4910     const MCExpr *Offset;
4911     if (getParser().parseExpression(Offset))
4912      return true;
4913 
4914     // The expression has to be a constant. Memory references with relocations
4915     // don't come through here, as they use the <label> forms of the relevant
4916     // instructions.
4917     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
4918     if (!CE)
4919       return Error (E, "constant expression expected");
4920 
4921     // If the constant was #-0, represent it as INT32_MIN.
4922     int32_t Val = CE->getValue();
4923     if (isNegative && Val == 0)
4924       CE = MCConstantExpr::create(INT32_MIN, getContext());
4925 
4926     // Now we should have the closing ']'
4927     if (Parser.getTok().isNot(AsmToken::RBrac))
4928       return Error(Parser.getTok().getLoc(), "']' expected");
4929     E = Parser.getTok().getEndLoc();
4930     Parser.Lex(); // Eat right bracket token.
4931 
4932     // Don't worry about range checking the value here. That's handled by
4933     // the is*() predicates.
4934     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
4935                                              ARM_AM::no_shift, 0, 0,
4936                                              false, S, E));
4937 
4938     // If there's a pre-indexing writeback marker, '!', just add it as a token
4939     // operand.
4940     if (Parser.getTok().is(AsmToken::Exclaim)) {
4941       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4942       Parser.Lex(); // Eat the '!'.
4943     }
4944 
4945     return false;
4946   }
4947 
4948   // The register offset is optionally preceded by a '+' or '-'
4949   bool isNegative = false;
4950   if (Parser.getTok().is(AsmToken::Minus)) {
4951     isNegative = true;
4952     Parser.Lex(); // Eat the '-'.
4953   } else if (Parser.getTok().is(AsmToken::Plus)) {
4954     // Nothing to do.
4955     Parser.Lex(); // Eat the '+'.
4956   }
4957 
4958   E = Parser.getTok().getLoc();
4959   int OffsetRegNum = tryParseRegister();
4960   if (OffsetRegNum == -1)
4961     return Error(E, "register expected");
4962 
4963   // If there's a shift operator, handle it.
4964   ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
4965   unsigned ShiftImm = 0;
4966   if (Parser.getTok().is(AsmToken::Comma)) {
4967     Parser.Lex(); // Eat the ','.
4968     if (parseMemRegOffsetShift(ShiftType, ShiftImm))
4969       return true;
4970   }
4971 
4972   // Now we should have the closing ']'
4973   if (Parser.getTok().isNot(AsmToken::RBrac))
4974     return Error(Parser.getTok().getLoc(), "']' expected");
4975   E = Parser.getTok().getEndLoc();
4976   Parser.Lex(); // Eat right bracket token.
4977 
4978   Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, OffsetRegNum,
4979                                            ShiftType, ShiftImm, 0, isNegative,
4980                                            S, E));
4981 
4982   // If there's a pre-indexing writeback marker, '!', just add it as a token
4983   // operand.
4984   if (Parser.getTok().is(AsmToken::Exclaim)) {
4985     Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4986     Parser.Lex(); // Eat the '!'.
4987   }
4988 
4989   return false;
4990 }
4991 
4992 /// parseMemRegOffsetShift - one of these two:
4993 ///   ( lsl | lsr | asr | ror ) , # shift_amount
4994 ///   rrx
4995 /// return true if it parses a shift otherwise it returns false.
4996 bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
4997                                           unsigned &Amount) {
4998   MCAsmParser &Parser = getParser();
4999   SMLoc Loc = Parser.getTok().getLoc();
5000   const AsmToken &Tok = Parser.getTok();
5001   if (Tok.isNot(AsmToken::Identifier))
5002     return true;
5003   StringRef ShiftName = Tok.getString();
5004   if (ShiftName == "lsl" || ShiftName == "LSL" ||
5005       ShiftName == "asl" || ShiftName == "ASL")
5006     St = ARM_AM::lsl;
5007   else if (ShiftName == "lsr" || ShiftName == "LSR")
5008     St = ARM_AM::lsr;
5009   else if (ShiftName == "asr" || ShiftName == "ASR")
5010     St = ARM_AM::asr;
5011   else if (ShiftName == "ror" || ShiftName == "ROR")
5012     St = ARM_AM::ror;
5013   else if (ShiftName == "rrx" || ShiftName == "RRX")
5014     St = ARM_AM::rrx;
5015   else
5016     return Error(Loc, "illegal shift operator");
5017   Parser.Lex(); // Eat shift type token.
5018 
5019   // rrx stands alone.
5020   Amount = 0;
5021   if (St != ARM_AM::rrx) {
5022     Loc = Parser.getTok().getLoc();
5023     // A '#' and a shift amount.
5024     const AsmToken &HashTok = Parser.getTok();
5025     if (HashTok.isNot(AsmToken::Hash) &&
5026         HashTok.isNot(AsmToken::Dollar))
5027       return Error(HashTok.getLoc(), "'#' expected");
5028     Parser.Lex(); // Eat hash token.
5029 
5030     const MCExpr *Expr;
5031     if (getParser().parseExpression(Expr))
5032       return true;
5033     // Range check the immediate.
5034     // lsl, ror: 0 <= imm <= 31
5035     // lsr, asr: 0 <= imm <= 32
5036     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
5037     if (!CE)
5038       return Error(Loc, "shift amount must be an immediate");
5039     int64_t Imm = CE->getValue();
5040     if (Imm < 0 ||
5041         ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
5042         ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
5043       return Error(Loc, "immediate shift value out of range");
5044     // If <ShiftTy> #0, turn it into a no_shift.
5045     if (Imm == 0)
5046       St = ARM_AM::lsl;
5047     // For consistency, treat lsr #32 and asr #32 as having immediate value 0.
5048     if (Imm == 32)
5049       Imm = 0;
5050     Amount = Imm;
5051   }
5052 
5053   return false;
5054 }
5055 
5056 /// parseFPImm - A floating point immediate expression operand.
5057 ARMAsmParser::OperandMatchResultTy
5058 ARMAsmParser::parseFPImm(OperandVector &Operands) {
5059   MCAsmParser &Parser = getParser();
5060   // Anything that can accept a floating point constant as an operand
5061   // needs to go through here, as the regular parseExpression is
5062   // integer only.
5063   //
5064   // This routine still creates a generic Immediate operand, containing
5065   // a bitcast of the 64-bit floating point value. The various operands
5066   // that accept floats can check whether the value is valid for them
5067   // via the standard is*() predicates.
5068 
5069   SMLoc S = Parser.getTok().getLoc();
5070 
5071   if (Parser.getTok().isNot(AsmToken::Hash) &&
5072       Parser.getTok().isNot(AsmToken::Dollar))
5073     return MatchOperand_NoMatch;
5074 
5075   // Disambiguate the VMOV forms that can accept an FP immediate.
5076   // vmov.f32 <sreg>, #imm
5077   // vmov.f64 <dreg>, #imm
5078   // vmov.f32 <dreg>, #imm  @ vector f32x2
5079   // vmov.f32 <qreg>, #imm  @ vector f32x4
5080   //
5081   // There are also the NEON VMOV instructions which expect an
5082   // integer constant. Make sure we don't try to parse an FPImm
5083   // for these:
5084   // vmov.i{8|16|32|64} <dreg|qreg>, #imm
5085   ARMOperand &TyOp = static_cast<ARMOperand &>(*Operands[2]);
5086   bool isVmovf = TyOp.isToken() &&
5087                  (TyOp.getToken() == ".f32" || TyOp.getToken() == ".f64" ||
5088                   TyOp.getToken() == ".f16");
5089   ARMOperand &Mnemonic = static_cast<ARMOperand &>(*Operands[0]);
5090   bool isFconst = Mnemonic.isToken() && (Mnemonic.getToken() == "fconstd" ||
5091                                          Mnemonic.getToken() == "fconsts");
5092   if (!(isVmovf || isFconst))
5093     return MatchOperand_NoMatch;
5094 
5095   Parser.Lex(); // Eat '#' or '$'.
5096 
5097   // Handle negation, as that still comes through as a separate token.
5098   bool isNegative = false;
5099   if (Parser.getTok().is(AsmToken::Minus)) {
5100     isNegative = true;
5101     Parser.Lex();
5102   }
5103   const AsmToken &Tok = Parser.getTok();
5104   SMLoc Loc = Tok.getLoc();
5105   if (Tok.is(AsmToken::Real) && isVmovf) {
5106     APFloat RealVal(APFloat::IEEEsingle, Tok.getString());
5107     uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
5108     // If we had a '-' in front, toggle the sign bit.
5109     IntVal ^= (uint64_t)isNegative << 31;
5110     Parser.Lex(); // Eat the token.
5111     Operands.push_back(ARMOperand::CreateImm(
5112           MCConstantExpr::create(IntVal, getContext()),
5113           S, Parser.getTok().getLoc()));
5114     return MatchOperand_Success;
5115   }
5116   // Also handle plain integers. Instructions which allow floating point
5117   // immediates also allow a raw encoded 8-bit value.
5118   if (Tok.is(AsmToken::Integer) && isFconst) {
5119     int64_t Val = Tok.getIntVal();
5120     Parser.Lex(); // Eat the token.
5121     if (Val > 255 || Val < 0) {
5122       Error(Loc, "encoded floating point value out of range");
5123       return MatchOperand_ParseFail;
5124     }
5125     float RealVal = ARM_AM::getFPImmFloat(Val);
5126     Val = APFloat(RealVal).bitcastToAPInt().getZExtValue();
5127 
5128     Operands.push_back(ARMOperand::CreateImm(
5129         MCConstantExpr::create(Val, getContext()), S,
5130         Parser.getTok().getLoc()));
5131     return MatchOperand_Success;
5132   }
5133 
5134   Error(Loc, "invalid floating point immediate");
5135   return MatchOperand_ParseFail;
5136 }
5137 
5138 /// Parse a arm instruction operand.  For now this parses the operand regardless
5139 /// of the mnemonic.
5140 bool ARMAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
5141   MCAsmParser &Parser = getParser();
5142   SMLoc S, E;
5143 
5144   // Check if the current operand has a custom associated parser, if so, try to
5145   // custom parse the operand, or fallback to the general approach.
5146   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
5147   if (ResTy == MatchOperand_Success)
5148     return false;
5149   // If there wasn't a custom match, try the generic matcher below. Otherwise,
5150   // there was a match, but an error occurred, in which case, just return that
5151   // the operand parsing failed.
5152   if (ResTy == MatchOperand_ParseFail)
5153     return true;
5154 
5155   switch (getLexer().getKind()) {
5156   default:
5157     Error(Parser.getTok().getLoc(), "unexpected token in operand");
5158     return true;
5159   case AsmToken::Identifier: {
5160     // If we've seen a branch mnemonic, the next operand must be a label.  This
5161     // is true even if the label is a register name.  So "br r1" means branch to
5162     // label "r1".
5163     bool ExpectLabel = Mnemonic == "b" || Mnemonic == "bl";
5164     if (!ExpectLabel) {
5165       if (!tryParseRegisterWithWriteBack(Operands))
5166         return false;
5167       int Res = tryParseShiftRegister(Operands);
5168       if (Res == 0) // success
5169         return false;
5170       else if (Res == -1) // irrecoverable error
5171         return true;
5172       // If this is VMRS, check for the apsr_nzcv operand.
5173       if (Mnemonic == "vmrs" &&
5174           Parser.getTok().getString().equals_lower("apsr_nzcv")) {
5175         S = Parser.getTok().getLoc();
5176         Parser.Lex();
5177         Operands.push_back(ARMOperand::CreateToken("APSR_nzcv", S));
5178         return false;
5179       }
5180     }
5181 
5182     // Fall though for the Identifier case that is not a register or a
5183     // special name.
5184   }
5185   case AsmToken::LParen:  // parenthesized expressions like (_strcmp-4)
5186   case AsmToken::Integer: // things like 1f and 2b as a branch targets
5187   case AsmToken::String:  // quoted label names.
5188   case AsmToken::Dot: {   // . as a branch target
5189     // This was not a register so parse other operands that start with an
5190     // identifier (like labels) as expressions and create them as immediates.
5191     const MCExpr *IdVal;
5192     S = Parser.getTok().getLoc();
5193     if (getParser().parseExpression(IdVal))
5194       return true;
5195     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5196     Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
5197     return false;
5198   }
5199   case AsmToken::LBrac:
5200     return parseMemory(Operands);
5201   case AsmToken::LCurly:
5202     return parseRegisterList(Operands);
5203   case AsmToken::Dollar:
5204   case AsmToken::Hash: {
5205     // #42 -> immediate.
5206     S = Parser.getTok().getLoc();
5207     Parser.Lex();
5208 
5209     if (Parser.getTok().isNot(AsmToken::Colon)) {
5210       bool isNegative = Parser.getTok().is(AsmToken::Minus);
5211       const MCExpr *ImmVal;
5212       if (getParser().parseExpression(ImmVal))
5213         return true;
5214       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
5215       if (CE) {
5216         int32_t Val = CE->getValue();
5217         if (isNegative && Val == 0)
5218           ImmVal = MCConstantExpr::create(INT32_MIN, getContext());
5219       }
5220       E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5221       Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
5222 
5223       // There can be a trailing '!' on operands that we want as a separate
5224       // '!' Token operand. Handle that here. For example, the compatibility
5225       // alias for 'srsdb sp!, #imm' is 'srsdb #imm!'.
5226       if (Parser.getTok().is(AsmToken::Exclaim)) {
5227         Operands.push_back(ARMOperand::CreateToken(Parser.getTok().getString(),
5228                                                    Parser.getTok().getLoc()));
5229         Parser.Lex(); // Eat exclaim token
5230       }
5231       return false;
5232     }
5233     // w/ a ':' after the '#', it's just like a plain ':'.
5234     // FALLTHROUGH
5235   }
5236   case AsmToken::Colon: {
5237     S = Parser.getTok().getLoc();
5238     // ":lower16:" and ":upper16:" expression prefixes
5239     // FIXME: Check it's an expression prefix,
5240     // e.g. (FOO - :lower16:BAR) isn't legal.
5241     ARMMCExpr::VariantKind RefKind;
5242     if (parsePrefix(RefKind))
5243       return true;
5244 
5245     const MCExpr *SubExprVal;
5246     if (getParser().parseExpression(SubExprVal))
5247       return true;
5248 
5249     const MCExpr *ExprVal = ARMMCExpr::create(RefKind, SubExprVal,
5250                                               getContext());
5251     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5252     Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
5253     return false;
5254   }
5255   case AsmToken::Equal: {
5256     S = Parser.getTok().getLoc();
5257     if (Mnemonic != "ldr") // only parse for ldr pseudo (e.g. ldr r0, =val)
5258       return Error(S, "unexpected token in operand");
5259     Parser.Lex(); // Eat '='
5260     const MCExpr *SubExprVal;
5261     if (getParser().parseExpression(SubExprVal))
5262       return true;
5263     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5264     Operands.push_back(ARMOperand::CreateConstantPoolImm(SubExprVal, S, E));
5265     return false;
5266   }
5267   }
5268 }
5269 
5270 // parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
5271 //  :lower16: and :upper16:.
5272 bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
5273   MCAsmParser &Parser = getParser();
5274   RefKind = ARMMCExpr::VK_ARM_None;
5275 
5276   // consume an optional '#' (GNU compatibility)
5277   if (getLexer().is(AsmToken::Hash))
5278     Parser.Lex();
5279 
5280   // :lower16: and :upper16: modifiers
5281   assert(getLexer().is(AsmToken::Colon) && "expected a :");
5282   Parser.Lex(); // Eat ':'
5283 
5284   if (getLexer().isNot(AsmToken::Identifier)) {
5285     Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
5286     return true;
5287   }
5288 
5289   enum {
5290     COFF = (1 << MCObjectFileInfo::IsCOFF),
5291     ELF = (1 << MCObjectFileInfo::IsELF),
5292     MACHO = (1 << MCObjectFileInfo::IsMachO)
5293   };
5294   static const struct PrefixEntry {
5295     const char *Spelling;
5296     ARMMCExpr::VariantKind VariantKind;
5297     uint8_t SupportedFormats;
5298   } PrefixEntries[] = {
5299     { "lower16", ARMMCExpr::VK_ARM_LO16, COFF | ELF | MACHO },
5300     { "upper16", ARMMCExpr::VK_ARM_HI16, COFF | ELF | MACHO },
5301   };
5302 
5303   StringRef IDVal = Parser.getTok().getIdentifier();
5304 
5305   const auto &Prefix =
5306       std::find_if(std::begin(PrefixEntries), std::end(PrefixEntries),
5307                    [&IDVal](const PrefixEntry &PE) {
5308                       return PE.Spelling == IDVal;
5309                    });
5310   if (Prefix == std::end(PrefixEntries)) {
5311     Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
5312     return true;
5313   }
5314 
5315   uint8_t CurrentFormat;
5316   switch (getContext().getObjectFileInfo()->getObjectFileType()) {
5317   case MCObjectFileInfo::IsMachO:
5318     CurrentFormat = MACHO;
5319     break;
5320   case MCObjectFileInfo::IsELF:
5321     CurrentFormat = ELF;
5322     break;
5323   case MCObjectFileInfo::IsCOFF:
5324     CurrentFormat = COFF;
5325     break;
5326   }
5327 
5328   if (~Prefix->SupportedFormats & CurrentFormat) {
5329     Error(Parser.getTok().getLoc(),
5330           "cannot represent relocation in the current file format");
5331     return true;
5332   }
5333 
5334   RefKind = Prefix->VariantKind;
5335   Parser.Lex();
5336 
5337   if (getLexer().isNot(AsmToken::Colon)) {
5338     Error(Parser.getTok().getLoc(), "unexpected token after prefix");
5339     return true;
5340   }
5341   Parser.Lex(); // Eat the last ':'
5342 
5343   return false;
5344 }
5345 
5346 /// \brief Given a mnemonic, split out possible predication code and carry
5347 /// setting letters to form a canonical mnemonic and flags.
5348 //
5349 // FIXME: Would be nice to autogen this.
5350 // FIXME: This is a bit of a maze of special cases.
5351 StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
5352                                       unsigned &PredicationCode,
5353                                       bool &CarrySetting,
5354                                       unsigned &ProcessorIMod,
5355                                       StringRef &ITMask) {
5356   PredicationCode = ARMCC::AL;
5357   CarrySetting = false;
5358   ProcessorIMod = 0;
5359 
5360   // Ignore some mnemonics we know aren't predicated forms.
5361   //
5362   // FIXME: Would be nice to autogen this.
5363   if ((Mnemonic == "movs" && isThumb()) ||
5364       Mnemonic == "teq"   || Mnemonic == "vceq"   || Mnemonic == "svc"   ||
5365       Mnemonic == "mls"   || Mnemonic == "smmls"  || Mnemonic == "vcls"  ||
5366       Mnemonic == "vmls"  || Mnemonic == "vnmls"  || Mnemonic == "vacge" ||
5367       Mnemonic == "vcge"  || Mnemonic == "vclt"   || Mnemonic == "vacgt" ||
5368       Mnemonic == "vaclt" || Mnemonic == "vacle"  || Mnemonic == "hlt" ||
5369       Mnemonic == "vcgt"  || Mnemonic == "vcle"   || Mnemonic == "smlal" ||
5370       Mnemonic == "umaal" || Mnemonic == "umlal"  || Mnemonic == "vabal" ||
5371       Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal" ||
5372       Mnemonic == "fmuls" || Mnemonic == "vmaxnm" || Mnemonic == "vminnm" ||
5373       Mnemonic == "vcvta" || Mnemonic == "vcvtn"  || Mnemonic == "vcvtp" ||
5374       Mnemonic == "vcvtm" || Mnemonic == "vrinta" || Mnemonic == "vrintn" ||
5375       Mnemonic == "vrintp" || Mnemonic == "vrintm" || Mnemonic == "hvc" ||
5376       Mnemonic.startswith("vsel") || Mnemonic == "vins" || Mnemonic == "vmovx" ||
5377       Mnemonic == "bxns"  || Mnemonic == "blxns")
5378     return Mnemonic;
5379 
5380   // First, split out any predication code. Ignore mnemonics we know aren't
5381   // predicated but do have a carry-set and so weren't caught above.
5382   if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
5383       Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
5384       Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
5385       Mnemonic != "sbcs" && Mnemonic != "rscs") {
5386     unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
5387       .Case("eq", ARMCC::EQ)
5388       .Case("ne", ARMCC::NE)
5389       .Case("hs", ARMCC::HS)
5390       .Case("cs", ARMCC::HS)
5391       .Case("lo", ARMCC::LO)
5392       .Case("cc", ARMCC::LO)
5393       .Case("mi", ARMCC::MI)
5394       .Case("pl", ARMCC::PL)
5395       .Case("vs", ARMCC::VS)
5396       .Case("vc", ARMCC::VC)
5397       .Case("hi", ARMCC::HI)
5398       .Case("ls", ARMCC::LS)
5399       .Case("ge", ARMCC::GE)
5400       .Case("lt", ARMCC::LT)
5401       .Case("gt", ARMCC::GT)
5402       .Case("le", ARMCC::LE)
5403       .Case("al", ARMCC::AL)
5404       .Default(~0U);
5405     if (CC != ~0U) {
5406       Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
5407       PredicationCode = CC;
5408     }
5409   }
5410 
5411   // Next, determine if we have a carry setting bit. We explicitly ignore all
5412   // the instructions we know end in 's'.
5413   if (Mnemonic.endswith("s") &&
5414       !(Mnemonic == "cps" || Mnemonic == "mls" ||
5415         Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
5416         Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
5417         Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
5418         Mnemonic == "vrsqrts" || Mnemonic == "srs" || Mnemonic == "flds" ||
5419         Mnemonic == "fmrs" || Mnemonic == "fsqrts" || Mnemonic == "fsubs" ||
5420         Mnemonic == "fsts" || Mnemonic == "fcpys" || Mnemonic == "fdivs" ||
5421         Mnemonic == "fmuls" || Mnemonic == "fcmps" || Mnemonic == "fcmpzs" ||
5422         Mnemonic == "vfms" || Mnemonic == "vfnms" || Mnemonic == "fconsts" ||
5423         Mnemonic == "bxns" || Mnemonic == "blxns" ||
5424         (Mnemonic == "movs" && isThumb()))) {
5425     Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
5426     CarrySetting = true;
5427   }
5428 
5429   // The "cps" instruction can have a interrupt mode operand which is glued into
5430   // the mnemonic. Check if this is the case, split it and parse the imod op
5431   if (Mnemonic.startswith("cps")) {
5432     // Split out any imod code.
5433     unsigned IMod =
5434       StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
5435       .Case("ie", ARM_PROC::IE)
5436       .Case("id", ARM_PROC::ID)
5437       .Default(~0U);
5438     if (IMod != ~0U) {
5439       Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
5440       ProcessorIMod = IMod;
5441     }
5442   }
5443 
5444   // The "it" instruction has the condition mask on the end of the mnemonic.
5445   if (Mnemonic.startswith("it")) {
5446     ITMask = Mnemonic.slice(2, Mnemonic.size());
5447     Mnemonic = Mnemonic.slice(0, 2);
5448   }
5449 
5450   return Mnemonic;
5451 }
5452 
5453 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
5454 /// inclusion of carry set or predication code operands.
5455 //
5456 // FIXME: It would be nice to autogen this.
5457 void ARMAsmParser::getMnemonicAcceptInfo(StringRef Mnemonic, StringRef FullInst,
5458                                          bool &CanAcceptCarrySet,
5459                                          bool &CanAcceptPredicationCode) {
5460   CanAcceptCarrySet =
5461       Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
5462       Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
5463       Mnemonic == "add" || Mnemonic == "adc" || Mnemonic == "mul" ||
5464       Mnemonic == "bic" || Mnemonic == "asr" || Mnemonic == "orr" ||
5465       Mnemonic == "mvn" || Mnemonic == "rsb" || Mnemonic == "rsc" ||
5466       Mnemonic == "orn" || Mnemonic == "sbc" || Mnemonic == "eor" ||
5467       Mnemonic == "neg" || Mnemonic == "vfm" || Mnemonic == "vfnm" ||
5468       (!isThumb() &&
5469        (Mnemonic == "smull" || Mnemonic == "mov" || Mnemonic == "mla" ||
5470         Mnemonic == "smlal" || Mnemonic == "umlal" || Mnemonic == "umull"));
5471 
5472   if (Mnemonic == "bkpt" || Mnemonic == "cbnz" || Mnemonic == "setend" ||
5473       Mnemonic == "cps" || Mnemonic == "it" || Mnemonic == "cbz" ||
5474       Mnemonic == "trap" || Mnemonic == "hlt" || Mnemonic == "udf" ||
5475       Mnemonic.startswith("crc32") || Mnemonic.startswith("cps") ||
5476       Mnemonic.startswith("vsel") || Mnemonic == "vmaxnm" ||
5477       Mnemonic == "vminnm" || Mnemonic == "vcvta" || Mnemonic == "vcvtn" ||
5478       Mnemonic == "vcvtp" || Mnemonic == "vcvtm" || Mnemonic == "vrinta" ||
5479       Mnemonic == "vrintn" || Mnemonic == "vrintp" || Mnemonic == "vrintm" ||
5480       Mnemonic.startswith("aes") || Mnemonic == "hvc" || Mnemonic == "setpan" ||
5481       Mnemonic.startswith("sha1") || Mnemonic.startswith("sha256") ||
5482       (FullInst.startswith("vmull") && FullInst.endswith(".p64")) ||
5483       Mnemonic == "vmovx" || Mnemonic == "vins") {
5484     // These mnemonics are never predicable
5485     CanAcceptPredicationCode = false;
5486   } else if (!isThumb()) {
5487     // Some instructions are only predicable in Thumb mode
5488     CanAcceptPredicationCode =
5489         Mnemonic != "cdp2" && Mnemonic != "clrex" && Mnemonic != "mcr2" &&
5490         Mnemonic != "mcrr2" && Mnemonic != "mrc2" && Mnemonic != "mrrc2" &&
5491         Mnemonic != "dmb" && Mnemonic != "dsb" && Mnemonic != "isb" &&
5492         Mnemonic != "pld" && Mnemonic != "pli" && Mnemonic != "pldw" &&
5493         Mnemonic != "ldc2" && Mnemonic != "ldc2l" && Mnemonic != "stc2" &&
5494         Mnemonic != "stc2l" && !Mnemonic.startswith("rfe") &&
5495         !Mnemonic.startswith("srs");
5496   } else if (isThumbOne()) {
5497     if (hasV6MOps())
5498       CanAcceptPredicationCode = Mnemonic != "movs";
5499     else
5500       CanAcceptPredicationCode = Mnemonic != "nop" && Mnemonic != "movs";
5501   } else
5502     CanAcceptPredicationCode = true;
5503 }
5504 
5505 // \brief Some Thumb instructions have two operand forms that are not
5506 // available as three operand, convert to two operand form if possible.
5507 //
5508 // FIXME: We would really like to be able to tablegen'erate this.
5509 void ARMAsmParser::tryConvertingToTwoOperandForm(StringRef Mnemonic,
5510                                                  bool CarrySetting,
5511                                                  OperandVector &Operands) {
5512   if (Operands.size() != 6)
5513     return;
5514 
5515   const auto &Op3 = static_cast<ARMOperand &>(*Operands[3]);
5516         auto &Op4 = static_cast<ARMOperand &>(*Operands[4]);
5517   if (!Op3.isReg() || !Op4.isReg())
5518     return;
5519 
5520   auto Op3Reg = Op3.getReg();
5521   auto Op4Reg = Op4.getReg();
5522 
5523   // For most Thumb2 cases we just generate the 3 operand form and reduce
5524   // it in processInstruction(), but the 3 operand form of ADD (t2ADDrr)
5525   // won't accept SP or PC so we do the transformation here taking care
5526   // with immediate range in the 'add sp, sp #imm' case.
5527   auto &Op5 = static_cast<ARMOperand &>(*Operands[5]);
5528   if (isThumbTwo()) {
5529     if (Mnemonic != "add")
5530       return;
5531     bool TryTransform = Op3Reg == ARM::PC || Op4Reg == ARM::PC ||
5532                         (Op5.isReg() && Op5.getReg() == ARM::PC);
5533     if (!TryTransform) {
5534       TryTransform = (Op3Reg == ARM::SP || Op4Reg == ARM::SP ||
5535                       (Op5.isReg() && Op5.getReg() == ARM::SP)) &&
5536                      !(Op3Reg == ARM::SP && Op4Reg == ARM::SP &&
5537                        Op5.isImm() && !Op5.isImm0_508s4());
5538     }
5539     if (!TryTransform)
5540       return;
5541   } else if (!isThumbOne())
5542     return;
5543 
5544   if (!(Mnemonic == "add" || Mnemonic == "sub" || Mnemonic == "and" ||
5545         Mnemonic == "eor" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
5546         Mnemonic == "asr" || Mnemonic == "adc" || Mnemonic == "sbc" ||
5547         Mnemonic == "ror" || Mnemonic == "orr" || Mnemonic == "bic"))
5548     return;
5549 
5550   // If first 2 operands of a 3 operand instruction are the same
5551   // then transform to 2 operand version of the same instruction
5552   // e.g. 'adds r0, r0, #1' transforms to 'adds r0, #1'
5553   bool Transform = Op3Reg == Op4Reg;
5554 
5555   // For communtative operations, we might be able to transform if we swap
5556   // Op4 and Op5.  The 'ADD Rdm, SP, Rdm' form is already handled specially
5557   // as tADDrsp.
5558   const ARMOperand *LastOp = &Op5;
5559   bool Swap = false;
5560   if (!Transform && Op5.isReg() && Op3Reg == Op5.getReg() &&
5561       ((Mnemonic == "add" && Op4Reg != ARM::SP) ||
5562        Mnemonic == "and" || Mnemonic == "eor" ||
5563        Mnemonic == "adc" || Mnemonic == "orr")) {
5564     Swap = true;
5565     LastOp = &Op4;
5566     Transform = true;
5567   }
5568 
5569   // If both registers are the same then remove one of them from
5570   // the operand list, with certain exceptions.
5571   if (Transform) {
5572     // Don't transform 'adds Rd, Rd, Rm' or 'sub{s} Rd, Rd, Rm' because the
5573     // 2 operand forms don't exist.
5574     if (((Mnemonic == "add" && CarrySetting) || Mnemonic == "sub") &&
5575         LastOp->isReg())
5576       Transform = false;
5577 
5578     // Don't transform 'add/sub{s} Rd, Rd, #imm' if the immediate fits into
5579     // 3-bits because the ARMARM says not to.
5580     if ((Mnemonic == "add" || Mnemonic == "sub") && LastOp->isImm0_7())
5581       Transform = false;
5582   }
5583 
5584   if (Transform) {
5585     if (Swap)
5586       std::swap(Op4, Op5);
5587     Operands.erase(Operands.begin() + 3);
5588   }
5589 }
5590 
5591 bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
5592                                           OperandVector &Operands) {
5593   // FIXME: This is all horribly hacky. We really need a better way to deal
5594   // with optional operands like this in the matcher table.
5595 
5596   // The 'mov' mnemonic is special. One variant has a cc_out operand, while
5597   // another does not. Specifically, the MOVW instruction does not. So we
5598   // special case it here and remove the defaulted (non-setting) cc_out
5599   // operand if that's the instruction we're trying to match.
5600   //
5601   // We do this as post-processing of the explicit operands rather than just
5602   // conditionally adding the cc_out in the first place because we need
5603   // to check the type of the parsed immediate operand.
5604   if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
5605       !static_cast<ARMOperand &>(*Operands[4]).isModImm() &&
5606       static_cast<ARMOperand &>(*Operands[4]).isImm0_65535Expr() &&
5607       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0)
5608     return true;
5609 
5610   // Register-register 'add' for thumb does not have a cc_out operand
5611   // when there are only two register operands.
5612   if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
5613       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5614       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5615       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0)
5616     return true;
5617   // Register-register 'add' for thumb does not have a cc_out operand
5618   // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
5619   // have to check the immediate range here since Thumb2 has a variant
5620   // that can handle a different range and has a cc_out operand.
5621   if (((isThumb() && Mnemonic == "add") ||
5622        (isThumbTwo() && Mnemonic == "sub")) &&
5623       Operands.size() == 6 && static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5624       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5625       static_cast<ARMOperand &>(*Operands[4]).getReg() == ARM::SP &&
5626       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5627       ((Mnemonic == "add" && static_cast<ARMOperand &>(*Operands[5]).isReg()) ||
5628        static_cast<ARMOperand &>(*Operands[5]).isImm0_1020s4()))
5629     return true;
5630   // For Thumb2, add/sub immediate does not have a cc_out operand for the
5631   // imm0_4095 variant. That's the least-preferred variant when
5632   // selecting via the generic "add" mnemonic, so to know that we
5633   // should remove the cc_out operand, we have to explicitly check that
5634   // it's not one of the other variants. Ugh.
5635   if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
5636       Operands.size() == 6 && static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5637       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5638       static_cast<ARMOperand &>(*Operands[5]).isImm()) {
5639     // Nest conditions rather than one big 'if' statement for readability.
5640     //
5641     // If both registers are low, we're in an IT block, and the immediate is
5642     // in range, we should use encoding T1 instead, which has a cc_out.
5643     if (inITBlock() &&
5644         isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) &&
5645         isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) &&
5646         static_cast<ARMOperand &>(*Operands[5]).isImm0_7())
5647       return false;
5648     // Check against T3. If the second register is the PC, this is an
5649     // alternate form of ADR, which uses encoding T4, so check for that too.
5650     if (static_cast<ARMOperand &>(*Operands[4]).getReg() != ARM::PC &&
5651         static_cast<ARMOperand &>(*Operands[5]).isT2SOImm())
5652       return false;
5653 
5654     // Otherwise, we use encoding T4, which does not have a cc_out
5655     // operand.
5656     return true;
5657   }
5658 
5659   // The thumb2 multiply instruction doesn't have a CCOut register, so
5660   // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
5661   // use the 16-bit encoding or not.
5662   if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
5663       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5664       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5665       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5666       static_cast<ARMOperand &>(*Operands[5]).isReg() &&
5667       // If the registers aren't low regs, the destination reg isn't the
5668       // same as one of the source regs, or the cc_out operand is zero
5669       // outside of an IT block, we have to use the 32-bit encoding, so
5670       // remove the cc_out operand.
5671       (!isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) ||
5672        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) ||
5673        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[5]).getReg()) ||
5674        !inITBlock() || (static_cast<ARMOperand &>(*Operands[3]).getReg() !=
5675                             static_cast<ARMOperand &>(*Operands[5]).getReg() &&
5676                         static_cast<ARMOperand &>(*Operands[3]).getReg() !=
5677                             static_cast<ARMOperand &>(*Operands[4]).getReg())))
5678     return true;
5679 
5680   // Also check the 'mul' syntax variant that doesn't specify an explicit
5681   // destination register.
5682   if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 5 &&
5683       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5684       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5685       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5686       // If the registers aren't low regs  or the cc_out operand is zero
5687       // outside of an IT block, we have to use the 32-bit encoding, so
5688       // remove the cc_out operand.
5689       (!isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) ||
5690        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) ||
5691        !inITBlock()))
5692     return true;
5693 
5694 
5695 
5696   // Register-register 'add/sub' for thumb does not have a cc_out operand
5697   // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
5698   // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
5699   // right, this will result in better diagnostics (which operand is off)
5700   // anyway.
5701   if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
5702       (Operands.size() == 5 || Operands.size() == 6) &&
5703       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5704       static_cast<ARMOperand &>(*Operands[3]).getReg() == ARM::SP &&
5705       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5706       (static_cast<ARMOperand &>(*Operands[4]).isImm() ||
5707        (Operands.size() == 6 &&
5708         static_cast<ARMOperand &>(*Operands[5]).isImm())))
5709     return true;
5710 
5711   return false;
5712 }
5713 
5714 bool ARMAsmParser::shouldOmitPredicateOperand(StringRef Mnemonic,
5715                                               OperandVector &Operands) {
5716   // VRINT{Z, R, X} have a predicate operand in VFP, but not in NEON
5717   unsigned RegIdx = 3;
5718   if ((Mnemonic == "vrintz" || Mnemonic == "vrintx" || Mnemonic == "vrintr") &&
5719       (static_cast<ARMOperand &>(*Operands[2]).getToken() == ".f32" ||
5720        static_cast<ARMOperand &>(*Operands[2]).getToken() == ".f16")) {
5721     if (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
5722         (static_cast<ARMOperand &>(*Operands[3]).getToken() == ".f32" ||
5723          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".f16"))
5724       RegIdx = 4;
5725 
5726     if (static_cast<ARMOperand &>(*Operands[RegIdx]).isReg() &&
5727         (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(
5728              static_cast<ARMOperand &>(*Operands[RegIdx]).getReg()) ||
5729          ARMMCRegisterClasses[ARM::QPRRegClassID].contains(
5730              static_cast<ARMOperand &>(*Operands[RegIdx]).getReg())))
5731       return true;
5732   }
5733   return false;
5734 }
5735 
5736 static bool isDataTypeToken(StringRef Tok) {
5737   return Tok == ".8" || Tok == ".16" || Tok == ".32" || Tok == ".64" ||
5738     Tok == ".i8" || Tok == ".i16" || Tok == ".i32" || Tok == ".i64" ||
5739     Tok == ".u8" || Tok == ".u16" || Tok == ".u32" || Tok == ".u64" ||
5740     Tok == ".s8" || Tok == ".s16" || Tok == ".s32" || Tok == ".s64" ||
5741     Tok == ".p8" || Tok == ".p16" || Tok == ".f32" || Tok == ".f64" ||
5742     Tok == ".f" || Tok == ".d";
5743 }
5744 
5745 // FIXME: This bit should probably be handled via an explicit match class
5746 // in the .td files that matches the suffix instead of having it be
5747 // a literal string token the way it is now.
5748 static bool doesIgnoreDataTypeSuffix(StringRef Mnemonic, StringRef DT) {
5749   return Mnemonic.startswith("vldm") || Mnemonic.startswith("vstm");
5750 }
5751 static void applyMnemonicAliases(StringRef &Mnemonic, uint64_t Features,
5752                                  unsigned VariantID);
5753 
5754 static bool RequiresVFPRegListValidation(StringRef Inst,
5755                                          bool &AcceptSinglePrecisionOnly,
5756                                          bool &AcceptDoublePrecisionOnly) {
5757   if (Inst.size() < 7)
5758     return false;
5759 
5760   if (Inst.startswith("fldm") || Inst.startswith("fstm")) {
5761     StringRef AddressingMode = Inst.substr(4, 2);
5762     if (AddressingMode == "ia" || AddressingMode == "db" ||
5763         AddressingMode == "ea" || AddressingMode == "fd") {
5764       AcceptSinglePrecisionOnly = Inst[6] == 's';
5765       AcceptDoublePrecisionOnly = Inst[6] == 'd' || Inst[6] == 'x';
5766       return true;
5767     }
5768   }
5769 
5770   return false;
5771 }
5772 
5773 /// Parse an arm instruction mnemonic followed by its operands.
5774 bool ARMAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
5775                                     SMLoc NameLoc, OperandVector &Operands) {
5776   MCAsmParser &Parser = getParser();
5777   // FIXME: Can this be done via tablegen in some fashion?
5778   bool RequireVFPRegisterListCheck;
5779   bool AcceptSinglePrecisionOnly;
5780   bool AcceptDoublePrecisionOnly;
5781   RequireVFPRegisterListCheck =
5782     RequiresVFPRegListValidation(Name, AcceptSinglePrecisionOnly,
5783                                  AcceptDoublePrecisionOnly);
5784 
5785   // Apply mnemonic aliases before doing anything else, as the destination
5786   // mnemonic may include suffices and we want to handle them normally.
5787   // The generic tblgen'erated code does this later, at the start of
5788   // MatchInstructionImpl(), but that's too late for aliases that include
5789   // any sort of suffix.
5790   uint64_t AvailableFeatures = getAvailableFeatures();
5791   unsigned AssemblerDialect = getParser().getAssemblerDialect();
5792   applyMnemonicAliases(Name, AvailableFeatures, AssemblerDialect);
5793 
5794   // First check for the ARM-specific .req directive.
5795   if (Parser.getTok().is(AsmToken::Identifier) &&
5796       Parser.getTok().getIdentifier() == ".req") {
5797     parseDirectiveReq(Name, NameLoc);
5798     // We always return 'error' for this, as we're done with this
5799     // statement and don't need to match the 'instruction."
5800     return true;
5801   }
5802 
5803   // Create the leading tokens for the mnemonic, split by '.' characters.
5804   size_t Start = 0, Next = Name.find('.');
5805   StringRef Mnemonic = Name.slice(Start, Next);
5806 
5807   // Split out the predication code and carry setting flag from the mnemonic.
5808   unsigned PredicationCode;
5809   unsigned ProcessorIMod;
5810   bool CarrySetting;
5811   StringRef ITMask;
5812   Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
5813                            ProcessorIMod, ITMask);
5814 
5815   // In Thumb1, only the branch (B) instruction can be predicated.
5816   if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
5817     Parser.eatToEndOfStatement();
5818     return Error(NameLoc, "conditional execution not supported in Thumb1");
5819   }
5820 
5821   Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
5822 
5823   // Handle the IT instruction ITMask. Convert it to a bitmask. This
5824   // is the mask as it will be for the IT encoding if the conditional
5825   // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
5826   // where the conditional bit0 is zero, the instruction post-processing
5827   // will adjust the mask accordingly.
5828   if (Mnemonic == "it") {
5829     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
5830     if (ITMask.size() > 3) {
5831       Parser.eatToEndOfStatement();
5832       return Error(Loc, "too many conditions on IT instruction");
5833     }
5834     unsigned Mask = 8;
5835     for (unsigned i = ITMask.size(); i != 0; --i) {
5836       char pos = ITMask[i - 1];
5837       if (pos != 't' && pos != 'e') {
5838         Parser.eatToEndOfStatement();
5839         return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
5840       }
5841       Mask >>= 1;
5842       if (ITMask[i - 1] == 't')
5843         Mask |= 8;
5844     }
5845     Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
5846   }
5847 
5848   // FIXME: This is all a pretty gross hack. We should automatically handle
5849   // optional operands like this via tblgen.
5850 
5851   // Next, add the CCOut and ConditionCode operands, if needed.
5852   //
5853   // For mnemonics which can ever incorporate a carry setting bit or predication
5854   // code, our matching model involves us always generating CCOut and
5855   // ConditionCode operands to match the mnemonic "as written" and then we let
5856   // the matcher deal with finding the right instruction or generating an
5857   // appropriate error.
5858   bool CanAcceptCarrySet, CanAcceptPredicationCode;
5859   getMnemonicAcceptInfo(Mnemonic, Name, CanAcceptCarrySet, CanAcceptPredicationCode);
5860 
5861   // If we had a carry-set on an instruction that can't do that, issue an
5862   // error.
5863   if (!CanAcceptCarrySet && CarrySetting) {
5864     Parser.eatToEndOfStatement();
5865     return Error(NameLoc, "instruction '" + Mnemonic +
5866                  "' can not set flags, but 's' suffix specified");
5867   }
5868   // If we had a predication code on an instruction that can't do that, issue an
5869   // error.
5870   if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
5871     Parser.eatToEndOfStatement();
5872     return Error(NameLoc, "instruction '" + Mnemonic +
5873                  "' is not predicable, but condition code specified");
5874   }
5875 
5876   // Add the carry setting operand, if necessary.
5877   if (CanAcceptCarrySet) {
5878     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
5879     Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
5880                                                Loc));
5881   }
5882 
5883   // Add the predication code operand, if necessary.
5884   if (CanAcceptPredicationCode) {
5885     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
5886                                       CarrySetting);
5887     Operands.push_back(ARMOperand::CreateCondCode(
5888                          ARMCC::CondCodes(PredicationCode), Loc));
5889   }
5890 
5891   // Add the processor imod operand, if necessary.
5892   if (ProcessorIMod) {
5893     Operands.push_back(ARMOperand::CreateImm(
5894           MCConstantExpr::create(ProcessorIMod, getContext()),
5895                                  NameLoc, NameLoc));
5896   } else if (Mnemonic == "cps" && isMClass()) {
5897     return Error(NameLoc, "instruction 'cps' requires effect for M-class");
5898   }
5899 
5900   // Add the remaining tokens in the mnemonic.
5901   while (Next != StringRef::npos) {
5902     Start = Next;
5903     Next = Name.find('.', Start + 1);
5904     StringRef ExtraToken = Name.slice(Start, Next);
5905 
5906     // Some NEON instructions have an optional datatype suffix that is
5907     // completely ignored. Check for that.
5908     if (isDataTypeToken(ExtraToken) &&
5909         doesIgnoreDataTypeSuffix(Mnemonic, ExtraToken))
5910       continue;
5911 
5912     // For for ARM mode generate an error if the .n qualifier is used.
5913     if (ExtraToken == ".n" && !isThumb()) {
5914       SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
5915       Parser.eatToEndOfStatement();
5916       return Error(Loc, "instruction with .n (narrow) qualifier not allowed in "
5917                    "arm mode");
5918     }
5919 
5920     // The .n qualifier is always discarded as that is what the tables
5921     // and matcher expect.  In ARM mode the .w qualifier has no effect,
5922     // so discard it to avoid errors that can be caused by the matcher.
5923     if (ExtraToken != ".n" && (isThumb() || ExtraToken != ".w")) {
5924       SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
5925       Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
5926     }
5927   }
5928 
5929   // Read the remaining operands.
5930   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5931     // Read the first operand.
5932     if (parseOperand(Operands, Mnemonic)) {
5933       Parser.eatToEndOfStatement();
5934       return true;
5935     }
5936 
5937     while (getLexer().is(AsmToken::Comma)) {
5938       Parser.Lex();  // Eat the comma.
5939 
5940       // Parse and remember the operand.
5941       if (parseOperand(Operands, Mnemonic)) {
5942         Parser.eatToEndOfStatement();
5943         return true;
5944       }
5945     }
5946   }
5947 
5948   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5949     SMLoc Loc = getLexer().getLoc();
5950     Parser.eatToEndOfStatement();
5951     return Error(Loc, "unexpected token in argument list");
5952   }
5953 
5954   Parser.Lex(); // Consume the EndOfStatement
5955 
5956   if (RequireVFPRegisterListCheck) {
5957     ARMOperand &Op = static_cast<ARMOperand &>(*Operands.back());
5958     if (AcceptSinglePrecisionOnly && !Op.isSPRRegList())
5959       return Error(Op.getStartLoc(),
5960                    "VFP/Neon single precision register expected");
5961     if (AcceptDoublePrecisionOnly && !Op.isDPRRegList())
5962       return Error(Op.getStartLoc(),
5963                    "VFP/Neon double precision register expected");
5964   }
5965 
5966   tryConvertingToTwoOperandForm(Mnemonic, CarrySetting, Operands);
5967 
5968   // Some instructions, mostly Thumb, have forms for the same mnemonic that
5969   // do and don't have a cc_out optional-def operand. With some spot-checks
5970   // of the operand list, we can figure out which variant we're trying to
5971   // parse and adjust accordingly before actually matching. We shouldn't ever
5972   // try to remove a cc_out operand that was explicitly set on the
5973   // mnemonic, of course (CarrySetting == true). Reason number #317 the
5974   // table driven matcher doesn't fit well with the ARM instruction set.
5975   if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands))
5976     Operands.erase(Operands.begin() + 1);
5977 
5978   // Some instructions have the same mnemonic, but don't always
5979   // have a predicate. Distinguish them here and delete the
5980   // predicate if needed.
5981   if (shouldOmitPredicateOperand(Mnemonic, Operands))
5982     Operands.erase(Operands.begin() + 1);
5983 
5984   // ARM mode 'blx' need special handling, as the register operand version
5985   // is predicable, but the label operand version is not. So, we can't rely
5986   // on the Mnemonic based checking to correctly figure out when to put
5987   // a k_CondCode operand in the list. If we're trying to match the label
5988   // version, remove the k_CondCode operand here.
5989   if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
5990       static_cast<ARMOperand &>(*Operands[2]).isImm())
5991     Operands.erase(Operands.begin() + 1);
5992 
5993   // Adjust operands of ldrexd/strexd to MCK_GPRPair.
5994   // ldrexd/strexd require even/odd GPR pair. To enforce this constraint,
5995   // a single GPRPair reg operand is used in the .td file to replace the two
5996   // GPRs. However, when parsing from asm, the two GRPs cannot be automatically
5997   // expressed as a GPRPair, so we have to manually merge them.
5998   // FIXME: We would really like to be able to tablegen'erate this.
5999   if (!isThumb() && Operands.size() > 4 &&
6000       (Mnemonic == "ldrexd" || Mnemonic == "strexd" || Mnemonic == "ldaexd" ||
6001        Mnemonic == "stlexd")) {
6002     bool isLoad = (Mnemonic == "ldrexd" || Mnemonic == "ldaexd");
6003     unsigned Idx = isLoad ? 2 : 3;
6004     ARMOperand &Op1 = static_cast<ARMOperand &>(*Operands[Idx]);
6005     ARMOperand &Op2 = static_cast<ARMOperand &>(*Operands[Idx + 1]);
6006 
6007     const MCRegisterClass& MRC = MRI->getRegClass(ARM::GPRRegClassID);
6008     // Adjust only if Op1 and Op2 are GPRs.
6009     if (Op1.isReg() && Op2.isReg() && MRC.contains(Op1.getReg()) &&
6010         MRC.contains(Op2.getReg())) {
6011       unsigned Reg1 = Op1.getReg();
6012       unsigned Reg2 = Op2.getReg();
6013       unsigned Rt = MRI->getEncodingValue(Reg1);
6014       unsigned Rt2 = MRI->getEncodingValue(Reg2);
6015 
6016       // Rt2 must be Rt + 1 and Rt must be even.
6017       if (Rt + 1 != Rt2 || (Rt & 1)) {
6018         Error(Op2.getStartLoc(), isLoad
6019                                      ? "destination operands must be sequential"
6020                                      : "source operands must be sequential");
6021         return true;
6022       }
6023       unsigned NewReg = MRI->getMatchingSuperReg(Reg1, ARM::gsub_0,
6024           &(MRI->getRegClass(ARM::GPRPairRegClassID)));
6025       Operands[Idx] =
6026           ARMOperand::CreateReg(NewReg, Op1.getStartLoc(), Op2.getEndLoc());
6027       Operands.erase(Operands.begin() + Idx + 1);
6028     }
6029   }
6030 
6031   // GNU Assembler extension (compatibility)
6032   if ((Mnemonic == "ldrd" || Mnemonic == "strd")) {
6033     ARMOperand &Op2 = static_cast<ARMOperand &>(*Operands[2]);
6034     ARMOperand &Op3 = static_cast<ARMOperand &>(*Operands[3]);
6035     if (Op3.isMem()) {
6036       assert(Op2.isReg() && "expected register argument");
6037 
6038       unsigned SuperReg = MRI->getMatchingSuperReg(
6039           Op2.getReg(), ARM::gsub_0, &MRI->getRegClass(ARM::GPRPairRegClassID));
6040 
6041       assert(SuperReg && "expected register pair");
6042 
6043       unsigned PairedReg = MRI->getSubReg(SuperReg, ARM::gsub_1);
6044 
6045       Operands.insert(
6046           Operands.begin() + 3,
6047           ARMOperand::CreateReg(PairedReg, Op2.getStartLoc(), Op2.getEndLoc()));
6048     }
6049   }
6050 
6051   // FIXME: As said above, this is all a pretty gross hack.  This instruction
6052   // does not fit with other "subs" and tblgen.
6053   // Adjust operands of B9.3.19 SUBS PC, LR, #imm (Thumb2) system instruction
6054   // so the Mnemonic is the original name "subs" and delete the predicate
6055   // operand so it will match the table entry.
6056   if (isThumbTwo() && Mnemonic == "sub" && Operands.size() == 6 &&
6057       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
6058       static_cast<ARMOperand &>(*Operands[3]).getReg() == ARM::PC &&
6059       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
6060       static_cast<ARMOperand &>(*Operands[4]).getReg() == ARM::LR &&
6061       static_cast<ARMOperand &>(*Operands[5]).isImm()) {
6062     Operands.front() = ARMOperand::CreateToken(Name, NameLoc);
6063     Operands.erase(Operands.begin() + 1);
6064   }
6065   return false;
6066 }
6067 
6068 // Validate context-sensitive operand constraints.
6069 
6070 // return 'true' if register list contains non-low GPR registers,
6071 // 'false' otherwise. If Reg is in the register list or is HiReg, set
6072 // 'containsReg' to true.
6073 static bool checkLowRegisterList(const MCInst &Inst, unsigned OpNo,
6074                                  unsigned Reg, unsigned HiReg,
6075                                  bool &containsReg) {
6076   containsReg = false;
6077   for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
6078     unsigned OpReg = Inst.getOperand(i).getReg();
6079     if (OpReg == Reg)
6080       containsReg = true;
6081     // Anything other than a low register isn't legal here.
6082     if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
6083       return true;
6084   }
6085   return false;
6086 }
6087 
6088 // Check if the specified regisgter is in the register list of the inst,
6089 // starting at the indicated operand number.
6090 static bool listContainsReg(const MCInst &Inst, unsigned OpNo, unsigned Reg) {
6091   for (unsigned i = OpNo, e = Inst.getNumOperands(); i < e; ++i) {
6092     unsigned OpReg = Inst.getOperand(i).getReg();
6093     if (OpReg == Reg)
6094       return true;
6095   }
6096   return false;
6097 }
6098 
6099 // Return true if instruction has the interesting property of being
6100 // allowed in IT blocks, but not being predicable.
6101 static bool instIsBreakpoint(const MCInst &Inst) {
6102     return Inst.getOpcode() == ARM::tBKPT ||
6103            Inst.getOpcode() == ARM::BKPT ||
6104            Inst.getOpcode() == ARM::tHLT ||
6105            Inst.getOpcode() == ARM::HLT;
6106 
6107 }
6108 
6109 bool ARMAsmParser::validatetLDMRegList(const MCInst &Inst,
6110                                        const OperandVector &Operands,
6111                                        unsigned ListNo, bool IsARPop) {
6112   const ARMOperand &Op = static_cast<const ARMOperand &>(*Operands[ListNo]);
6113   bool HasWritebackToken = Op.isToken() && Op.getToken() == "!";
6114 
6115   bool ListContainsSP = listContainsReg(Inst, ListNo, ARM::SP);
6116   bool ListContainsLR = listContainsReg(Inst, ListNo, ARM::LR);
6117   bool ListContainsPC = listContainsReg(Inst, ListNo, ARM::PC);
6118 
6119   if (!IsARPop && ListContainsSP)
6120     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6121                  "SP may not be in the register list");
6122   else if (ListContainsPC && ListContainsLR)
6123     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6124                  "PC and LR may not be in the register list simultaneously");
6125   else if (inITBlock() && !lastInITBlock() && ListContainsPC)
6126     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6127                  "instruction must be outside of IT block or the last "
6128                  "instruction in an IT block");
6129   return false;
6130 }
6131 
6132 bool ARMAsmParser::validatetSTMRegList(const MCInst &Inst,
6133                                        const OperandVector &Operands,
6134                                        unsigned ListNo) {
6135   const ARMOperand &Op = static_cast<const ARMOperand &>(*Operands[ListNo]);
6136   bool HasWritebackToken = Op.isToken() && Op.getToken() == "!";
6137 
6138   bool ListContainsSP = listContainsReg(Inst, ListNo, ARM::SP);
6139   bool ListContainsPC = listContainsReg(Inst, ListNo, ARM::PC);
6140 
6141   if (ListContainsSP && ListContainsPC)
6142     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6143                  "SP and PC may not be in the register list");
6144   else if (ListContainsSP)
6145     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6146                  "SP may not be in the register list");
6147   else if (ListContainsPC)
6148     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6149                  "PC may not be in the register list");
6150   return false;
6151 }
6152 
6153 // FIXME: We would really like to be able to tablegen'erate this.
6154 bool ARMAsmParser::validateInstruction(MCInst &Inst,
6155                                        const OperandVector &Operands) {
6156   const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
6157   SMLoc Loc = Operands[0]->getStartLoc();
6158 
6159   // Check the IT block state first.
6160   // NOTE: BKPT and HLT instructions have the interesting property of being
6161   // allowed in IT blocks, but not being predicable. They just always execute.
6162   if (inITBlock() && !instIsBreakpoint(Inst)) {
6163     unsigned Bit = 1;
6164     if (ITState.FirstCond)
6165       ITState.FirstCond = false;
6166     else
6167       Bit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
6168     // The instruction must be predicable.
6169     if (!MCID.isPredicable())
6170       return Error(Loc, "instructions in IT block must be predicable");
6171     unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
6172     unsigned ITCond = Bit ? ITState.Cond :
6173       ARMCC::getOppositeCondition(ITState.Cond);
6174     if (Cond != ITCond) {
6175       // Find the condition code Operand to get its SMLoc information.
6176       SMLoc CondLoc;
6177       for (unsigned I = 1; I < Operands.size(); ++I)
6178         if (static_cast<ARMOperand &>(*Operands[I]).isCondCode())
6179           CondLoc = Operands[I]->getStartLoc();
6180       return Error(CondLoc, "incorrect condition in IT block; got '" +
6181                    StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
6182                    "', but expected '" +
6183                    ARMCondCodeToString(ARMCC::CondCodes(ITCond)) + "'");
6184     }
6185   // Check for non-'al' condition codes outside of the IT block.
6186   } else if (isThumbTwo() && MCID.isPredicable() &&
6187              Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
6188              ARMCC::AL && Inst.getOpcode() != ARM::tBcc &&
6189              Inst.getOpcode() != ARM::t2Bcc)
6190     return Error(Loc, "predicated instructions must be in IT block");
6191 
6192   const unsigned Opcode = Inst.getOpcode();
6193   switch (Opcode) {
6194   case ARM::LDRD:
6195   case ARM::LDRD_PRE:
6196   case ARM::LDRD_POST: {
6197     const unsigned RtReg = Inst.getOperand(0).getReg();
6198 
6199     // Rt can't be R14.
6200     if (RtReg == ARM::LR)
6201       return Error(Operands[3]->getStartLoc(),
6202                    "Rt can't be R14");
6203 
6204     const unsigned Rt = MRI->getEncodingValue(RtReg);
6205     // Rt must be even-numbered.
6206     if ((Rt & 1) == 1)
6207       return Error(Operands[3]->getStartLoc(),
6208                    "Rt must be even-numbered");
6209 
6210     // Rt2 must be Rt + 1.
6211     const unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6212     if (Rt2 != Rt + 1)
6213       return Error(Operands[3]->getStartLoc(),
6214                    "destination operands must be sequential");
6215 
6216     if (Opcode == ARM::LDRD_PRE || Opcode == ARM::LDRD_POST) {
6217       const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(3).getReg());
6218       // For addressing modes with writeback, the base register needs to be
6219       // different from the destination registers.
6220       if (Rn == Rt || Rn == Rt2)
6221         return Error(Operands[3]->getStartLoc(),
6222                      "base register needs to be different from destination "
6223                      "registers");
6224     }
6225 
6226     return false;
6227   }
6228   case ARM::t2LDRDi8:
6229   case ARM::t2LDRD_PRE:
6230   case ARM::t2LDRD_POST: {
6231     // Rt2 must be different from Rt.
6232     unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
6233     unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6234     if (Rt2 == Rt)
6235       return Error(Operands[3]->getStartLoc(),
6236                    "destination operands can't be identical");
6237     return false;
6238   }
6239   case ARM::t2BXJ: {
6240     const unsigned RmReg = Inst.getOperand(0).getReg();
6241     // Rm = SP is no longer unpredictable in v8-A
6242     if (RmReg == ARM::SP && !hasV8Ops())
6243       return Error(Operands[2]->getStartLoc(),
6244                    "r13 (SP) is an unpredictable operand to BXJ");
6245     return false;
6246   }
6247   case ARM::STRD: {
6248     // Rt2 must be Rt + 1.
6249     unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
6250     unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6251     if (Rt2 != Rt + 1)
6252       return Error(Operands[3]->getStartLoc(),
6253                    "source operands must be sequential");
6254     return false;
6255   }
6256   case ARM::STRD_PRE:
6257   case ARM::STRD_POST: {
6258     // Rt2 must be Rt + 1.
6259     unsigned Rt = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6260     unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(2).getReg());
6261     if (Rt2 != Rt + 1)
6262       return Error(Operands[3]->getStartLoc(),
6263                    "source operands must be sequential");
6264     return false;
6265   }
6266   case ARM::STR_PRE_IMM:
6267   case ARM::STR_PRE_REG:
6268   case ARM::STR_POST_IMM:
6269   case ARM::STR_POST_REG:
6270   case ARM::STRH_PRE:
6271   case ARM::STRH_POST:
6272   case ARM::STRB_PRE_IMM:
6273   case ARM::STRB_PRE_REG:
6274   case ARM::STRB_POST_IMM:
6275   case ARM::STRB_POST_REG: {
6276     // Rt must be different from Rn.
6277     const unsigned Rt = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6278     const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(2).getReg());
6279 
6280     if (Rt == Rn)
6281       return Error(Operands[3]->getStartLoc(),
6282                    "source register and base register can't be identical");
6283     return false;
6284   }
6285   case ARM::LDR_PRE_IMM:
6286   case ARM::LDR_PRE_REG:
6287   case ARM::LDR_POST_IMM:
6288   case ARM::LDR_POST_REG:
6289   case ARM::LDRH_PRE:
6290   case ARM::LDRH_POST:
6291   case ARM::LDRSH_PRE:
6292   case ARM::LDRSH_POST:
6293   case ARM::LDRB_PRE_IMM:
6294   case ARM::LDRB_PRE_REG:
6295   case ARM::LDRB_POST_IMM:
6296   case ARM::LDRB_POST_REG:
6297   case ARM::LDRSB_PRE:
6298   case ARM::LDRSB_POST: {
6299     // Rt must be different from Rn.
6300     const unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
6301     const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(2).getReg());
6302 
6303     if (Rt == Rn)
6304       return Error(Operands[3]->getStartLoc(),
6305                    "destination register and base register can't be identical");
6306     return false;
6307   }
6308   case ARM::SBFX:
6309   case ARM::UBFX: {
6310     // Width must be in range [1, 32-lsb].
6311     unsigned LSB = Inst.getOperand(2).getImm();
6312     unsigned Widthm1 = Inst.getOperand(3).getImm();
6313     if (Widthm1 >= 32 - LSB)
6314       return Error(Operands[5]->getStartLoc(),
6315                    "bitfield width must be in range [1,32-lsb]");
6316     return false;
6317   }
6318   // Notionally handles ARM::tLDMIA_UPD too.
6319   case ARM::tLDMIA: {
6320     // If we're parsing Thumb2, the .w variant is available and handles
6321     // most cases that are normally illegal for a Thumb1 LDM instruction.
6322     // We'll make the transformation in processInstruction() if necessary.
6323     //
6324     // Thumb LDM instructions are writeback iff the base register is not
6325     // in the register list.
6326     unsigned Rn = Inst.getOperand(0).getReg();
6327     bool HasWritebackToken =
6328         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
6329          static_cast<ARMOperand &>(*Operands[3]).getToken() == "!");
6330     bool ListContainsBase;
6331     if (checkLowRegisterList(Inst, 3, Rn, 0, ListContainsBase) && !isThumbTwo())
6332       return Error(Operands[3 + HasWritebackToken]->getStartLoc(),
6333                    "registers must be in range r0-r7");
6334     // If we should have writeback, then there should be a '!' token.
6335     if (!ListContainsBase && !HasWritebackToken && !isThumbTwo())
6336       return Error(Operands[2]->getStartLoc(),
6337                    "writeback operator '!' expected");
6338     // If we should not have writeback, there must not be a '!'. This is
6339     // true even for the 32-bit wide encodings.
6340     if (ListContainsBase && HasWritebackToken)
6341       return Error(Operands[3]->getStartLoc(),
6342                    "writeback operator '!' not allowed when base register "
6343                    "in register list");
6344 
6345     if (validatetLDMRegList(Inst, Operands, 3))
6346       return true;
6347     break;
6348   }
6349   case ARM::LDMIA_UPD:
6350   case ARM::LDMDB_UPD:
6351   case ARM::LDMIB_UPD:
6352   case ARM::LDMDA_UPD:
6353     // ARM variants loading and updating the same register are only officially
6354     // UNPREDICTABLE on v7 upwards. Goodness knows what they did before.
6355     if (!hasV7Ops())
6356       break;
6357     if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
6358       return Error(Operands.back()->getStartLoc(),
6359                    "writeback register not allowed in register list");
6360     break;
6361   case ARM::t2LDMIA:
6362   case ARM::t2LDMDB:
6363     if (validatetLDMRegList(Inst, Operands, 3))
6364       return true;
6365     break;
6366   case ARM::t2STMIA:
6367   case ARM::t2STMDB:
6368     if (validatetSTMRegList(Inst, Operands, 3))
6369       return true;
6370     break;
6371   case ARM::t2LDMIA_UPD:
6372   case ARM::t2LDMDB_UPD:
6373   case ARM::t2STMIA_UPD:
6374   case ARM::t2STMDB_UPD: {
6375     if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
6376       return Error(Operands.back()->getStartLoc(),
6377                    "writeback register not allowed in register list");
6378 
6379     if (Opcode == ARM::t2LDMIA_UPD || Opcode == ARM::t2LDMDB_UPD) {
6380       if (validatetLDMRegList(Inst, Operands, 3))
6381         return true;
6382     } else {
6383       if (validatetSTMRegList(Inst, Operands, 3))
6384         return true;
6385     }
6386     break;
6387   }
6388   case ARM::sysLDMIA_UPD:
6389   case ARM::sysLDMDA_UPD:
6390   case ARM::sysLDMDB_UPD:
6391   case ARM::sysLDMIB_UPD:
6392     if (!listContainsReg(Inst, 3, ARM::PC))
6393       return Error(Operands[4]->getStartLoc(),
6394                    "writeback register only allowed on system LDM "
6395                    "if PC in register-list");
6396     break;
6397   case ARM::sysSTMIA_UPD:
6398   case ARM::sysSTMDA_UPD:
6399   case ARM::sysSTMDB_UPD:
6400   case ARM::sysSTMIB_UPD:
6401     return Error(Operands[2]->getStartLoc(),
6402                  "system STM cannot have writeback register");
6403   case ARM::tMUL: {
6404     // The second source operand must be the same register as the destination
6405     // operand.
6406     //
6407     // In this case, we must directly check the parsed operands because the
6408     // cvtThumbMultiply() function is written in such a way that it guarantees
6409     // this first statement is always true for the new Inst.  Essentially, the
6410     // destination is unconditionally copied into the second source operand
6411     // without checking to see if it matches what we actually parsed.
6412     if (Operands.size() == 6 && (((ARMOperand &)*Operands[3]).getReg() !=
6413                                  ((ARMOperand &)*Operands[5]).getReg()) &&
6414         (((ARMOperand &)*Operands[3]).getReg() !=
6415          ((ARMOperand &)*Operands[4]).getReg())) {
6416       return Error(Operands[3]->getStartLoc(),
6417                    "destination register must match source register");
6418     }
6419     break;
6420   }
6421   // Like for ldm/stm, push and pop have hi-reg handling version in Thumb2,
6422   // so only issue a diagnostic for thumb1. The instructions will be
6423   // switched to the t2 encodings in processInstruction() if necessary.
6424   case ARM::tPOP: {
6425     bool ListContainsBase;
6426     if (checkLowRegisterList(Inst, 2, 0, ARM::PC, ListContainsBase) &&
6427         !isThumbTwo())
6428       return Error(Operands[2]->getStartLoc(),
6429                    "registers must be in range r0-r7 or pc");
6430     if (validatetLDMRegList(Inst, Operands, 2, !isMClass()))
6431       return true;
6432     break;
6433   }
6434   case ARM::tPUSH: {
6435     bool ListContainsBase;
6436     if (checkLowRegisterList(Inst, 2, 0, ARM::LR, ListContainsBase) &&
6437         !isThumbTwo())
6438       return Error(Operands[2]->getStartLoc(),
6439                    "registers must be in range r0-r7 or lr");
6440     if (validatetSTMRegList(Inst, Operands, 2))
6441       return true;
6442     break;
6443   }
6444   case ARM::tSTMIA_UPD: {
6445     bool ListContainsBase, InvalidLowList;
6446     InvalidLowList = checkLowRegisterList(Inst, 4, Inst.getOperand(0).getReg(),
6447                                           0, ListContainsBase);
6448     if (InvalidLowList && !isThumbTwo())
6449       return Error(Operands[4]->getStartLoc(),
6450                    "registers must be in range r0-r7");
6451 
6452     // This would be converted to a 32-bit stm, but that's not valid if the
6453     // writeback register is in the list.
6454     if (InvalidLowList && ListContainsBase)
6455       return Error(Operands[4]->getStartLoc(),
6456                    "writeback operator '!' not allowed when base register "
6457                    "in register list");
6458 
6459     if (validatetSTMRegList(Inst, Operands, 4))
6460       return true;
6461     break;
6462   }
6463   case ARM::tADDrSP: {
6464     // If the non-SP source operand and the destination operand are not the
6465     // same, we need thumb2 (for the wide encoding), or we have an error.
6466     if (!isThumbTwo() &&
6467         Inst.getOperand(0).getReg() != Inst.getOperand(2).getReg()) {
6468       return Error(Operands[4]->getStartLoc(),
6469                    "source register must be the same as destination");
6470     }
6471     break;
6472   }
6473   // Final range checking for Thumb unconditional branch instructions.
6474   case ARM::tB:
6475     if (!(static_cast<ARMOperand &>(*Operands[2])).isSignedOffset<11, 1>())
6476       return Error(Operands[2]->getStartLoc(), "branch target out of range");
6477     break;
6478   case ARM::t2B: {
6479     int op = (Operands[2]->isImm()) ? 2 : 3;
6480     if (!static_cast<ARMOperand &>(*Operands[op]).isSignedOffset<24, 1>())
6481       return Error(Operands[op]->getStartLoc(), "branch target out of range");
6482     break;
6483   }
6484   // Final range checking for Thumb conditional branch instructions.
6485   case ARM::tBcc:
6486     if (!static_cast<ARMOperand &>(*Operands[2]).isSignedOffset<8, 1>())
6487       return Error(Operands[2]->getStartLoc(), "branch target out of range");
6488     break;
6489   case ARM::t2Bcc: {
6490     int Op = (Operands[2]->isImm()) ? 2 : 3;
6491     if (!static_cast<ARMOperand &>(*Operands[Op]).isSignedOffset<20, 1>())
6492       return Error(Operands[Op]->getStartLoc(), "branch target out of range");
6493     break;
6494   }
6495   case ARM::MOVi16:
6496   case ARM::t2MOVi16:
6497   case ARM::t2MOVTi16:
6498     {
6499     // We want to avoid misleadingly allowing something like "mov r0, <symbol>"
6500     // especially when we turn it into a movw and the expression <symbol> does
6501     // not have a :lower16: or :upper16 as part of the expression.  We don't
6502     // want the behavior of silently truncating, which can be unexpected and
6503     // lead to bugs that are difficult to find since this is an easy mistake
6504     // to make.
6505     int i = (Operands[3]->isImm()) ? 3 : 4;
6506     ARMOperand &Op = static_cast<ARMOperand &>(*Operands[i]);
6507     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm());
6508     if (CE) break;
6509     const MCExpr *E = dyn_cast<MCExpr>(Op.getImm());
6510     if (!E) break;
6511     const ARMMCExpr *ARM16Expr = dyn_cast<ARMMCExpr>(E);
6512     if (!ARM16Expr || (ARM16Expr->getKind() != ARMMCExpr::VK_ARM_HI16 &&
6513                        ARM16Expr->getKind() != ARMMCExpr::VK_ARM_LO16))
6514       return Error(
6515           Op.getStartLoc(),
6516           "immediate expression for mov requires :lower16: or :upper16");
6517     break;
6518   }
6519   case ARM::HINT:
6520   case ARM::t2HINT: {
6521     if (hasRAS()) {
6522       // ESB is not predicable (pred must be AL)
6523       unsigned Imm8 = Inst.getOperand(0).getImm();
6524       unsigned Pred = Inst.getOperand(1).getImm();
6525       if (Imm8 == 0x10 && Pred != ARMCC::AL)
6526         return Error(Operands[1]->getStartLoc(), "instruction 'esb' is not "
6527                                                  "predicable, but condition "
6528                                                  "code specified");
6529     }
6530     // Without the RAS extension, this behaves as any other unallocated hint.
6531     break;
6532   }
6533   }
6534 
6535   return false;
6536 }
6537 
6538 static unsigned getRealVSTOpcode(unsigned Opc, unsigned &Spacing) {
6539   switch(Opc) {
6540   default: llvm_unreachable("unexpected opcode!");
6541   // VST1LN
6542   case ARM::VST1LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST1LNd8_UPD;
6543   case ARM::VST1LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST1LNd16_UPD;
6544   case ARM::VST1LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST1LNd32_UPD;
6545   case ARM::VST1LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST1LNd8_UPD;
6546   case ARM::VST1LNdWB_register_Asm_16: Spacing = 1; return ARM::VST1LNd16_UPD;
6547   case ARM::VST1LNdWB_register_Asm_32: Spacing = 1; return ARM::VST1LNd32_UPD;
6548   case ARM::VST1LNdAsm_8:  Spacing = 1; return ARM::VST1LNd8;
6549   case ARM::VST1LNdAsm_16: Spacing = 1; return ARM::VST1LNd16;
6550   case ARM::VST1LNdAsm_32: Spacing = 1; return ARM::VST1LNd32;
6551 
6552   // VST2LN
6553   case ARM::VST2LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST2LNd8_UPD;
6554   case ARM::VST2LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST2LNd16_UPD;
6555   case ARM::VST2LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST2LNd32_UPD;
6556   case ARM::VST2LNqWB_fixed_Asm_16: Spacing = 2; return ARM::VST2LNq16_UPD;
6557   case ARM::VST2LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST2LNq32_UPD;
6558 
6559   case ARM::VST2LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST2LNd8_UPD;
6560   case ARM::VST2LNdWB_register_Asm_16: Spacing = 1; return ARM::VST2LNd16_UPD;
6561   case ARM::VST2LNdWB_register_Asm_32: Spacing = 1; return ARM::VST2LNd32_UPD;
6562   case ARM::VST2LNqWB_register_Asm_16: Spacing = 2; return ARM::VST2LNq16_UPD;
6563   case ARM::VST2LNqWB_register_Asm_32: Spacing = 2; return ARM::VST2LNq32_UPD;
6564 
6565   case ARM::VST2LNdAsm_8:  Spacing = 1; return ARM::VST2LNd8;
6566   case ARM::VST2LNdAsm_16: Spacing = 1; return ARM::VST2LNd16;
6567   case ARM::VST2LNdAsm_32: Spacing = 1; return ARM::VST2LNd32;
6568   case ARM::VST2LNqAsm_16: Spacing = 2; return ARM::VST2LNq16;
6569   case ARM::VST2LNqAsm_32: Spacing = 2; return ARM::VST2LNq32;
6570 
6571   // VST3LN
6572   case ARM::VST3LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST3LNd8_UPD;
6573   case ARM::VST3LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST3LNd16_UPD;
6574   case ARM::VST3LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST3LNd32_UPD;
6575   case ARM::VST3LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VST3LNq16_UPD;
6576   case ARM::VST3LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST3LNq32_UPD;
6577   case ARM::VST3LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST3LNd8_UPD;
6578   case ARM::VST3LNdWB_register_Asm_16: Spacing = 1; return ARM::VST3LNd16_UPD;
6579   case ARM::VST3LNdWB_register_Asm_32: Spacing = 1; return ARM::VST3LNd32_UPD;
6580   case ARM::VST3LNqWB_register_Asm_16: Spacing = 2; return ARM::VST3LNq16_UPD;
6581   case ARM::VST3LNqWB_register_Asm_32: Spacing = 2; return ARM::VST3LNq32_UPD;
6582   case ARM::VST3LNdAsm_8:  Spacing = 1; return ARM::VST3LNd8;
6583   case ARM::VST3LNdAsm_16: Spacing = 1; return ARM::VST3LNd16;
6584   case ARM::VST3LNdAsm_32: Spacing = 1; return ARM::VST3LNd32;
6585   case ARM::VST3LNqAsm_16: Spacing = 2; return ARM::VST3LNq16;
6586   case ARM::VST3LNqAsm_32: Spacing = 2; return ARM::VST3LNq32;
6587 
6588   // VST3
6589   case ARM::VST3dWB_fixed_Asm_8:  Spacing = 1; return ARM::VST3d8_UPD;
6590   case ARM::VST3dWB_fixed_Asm_16: Spacing = 1; return ARM::VST3d16_UPD;
6591   case ARM::VST3dWB_fixed_Asm_32: Spacing = 1; return ARM::VST3d32_UPD;
6592   case ARM::VST3qWB_fixed_Asm_8:  Spacing = 2; return ARM::VST3q8_UPD;
6593   case ARM::VST3qWB_fixed_Asm_16: Spacing = 2; return ARM::VST3q16_UPD;
6594   case ARM::VST3qWB_fixed_Asm_32: Spacing = 2; return ARM::VST3q32_UPD;
6595   case ARM::VST3dWB_register_Asm_8:  Spacing = 1; return ARM::VST3d8_UPD;
6596   case ARM::VST3dWB_register_Asm_16: Spacing = 1; return ARM::VST3d16_UPD;
6597   case ARM::VST3dWB_register_Asm_32: Spacing = 1; return ARM::VST3d32_UPD;
6598   case ARM::VST3qWB_register_Asm_8:  Spacing = 2; return ARM::VST3q8_UPD;
6599   case ARM::VST3qWB_register_Asm_16: Spacing = 2; return ARM::VST3q16_UPD;
6600   case ARM::VST3qWB_register_Asm_32: Spacing = 2; return ARM::VST3q32_UPD;
6601   case ARM::VST3dAsm_8:  Spacing = 1; return ARM::VST3d8;
6602   case ARM::VST3dAsm_16: Spacing = 1; return ARM::VST3d16;
6603   case ARM::VST3dAsm_32: Spacing = 1; return ARM::VST3d32;
6604   case ARM::VST3qAsm_8:  Spacing = 2; return ARM::VST3q8;
6605   case ARM::VST3qAsm_16: Spacing = 2; return ARM::VST3q16;
6606   case ARM::VST3qAsm_32: Spacing = 2; return ARM::VST3q32;
6607 
6608   // VST4LN
6609   case ARM::VST4LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST4LNd8_UPD;
6610   case ARM::VST4LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST4LNd16_UPD;
6611   case ARM::VST4LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST4LNd32_UPD;
6612   case ARM::VST4LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VST4LNq16_UPD;
6613   case ARM::VST4LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST4LNq32_UPD;
6614   case ARM::VST4LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST4LNd8_UPD;
6615   case ARM::VST4LNdWB_register_Asm_16: Spacing = 1; return ARM::VST4LNd16_UPD;
6616   case ARM::VST4LNdWB_register_Asm_32: Spacing = 1; return ARM::VST4LNd32_UPD;
6617   case ARM::VST4LNqWB_register_Asm_16: Spacing = 2; return ARM::VST4LNq16_UPD;
6618   case ARM::VST4LNqWB_register_Asm_32: Spacing = 2; return ARM::VST4LNq32_UPD;
6619   case ARM::VST4LNdAsm_8:  Spacing = 1; return ARM::VST4LNd8;
6620   case ARM::VST4LNdAsm_16: Spacing = 1; return ARM::VST4LNd16;
6621   case ARM::VST4LNdAsm_32: Spacing = 1; return ARM::VST4LNd32;
6622   case ARM::VST4LNqAsm_16: Spacing = 2; return ARM::VST4LNq16;
6623   case ARM::VST4LNqAsm_32: Spacing = 2; return ARM::VST4LNq32;
6624 
6625   // VST4
6626   case ARM::VST4dWB_fixed_Asm_8:  Spacing = 1; return ARM::VST4d8_UPD;
6627   case ARM::VST4dWB_fixed_Asm_16: Spacing = 1; return ARM::VST4d16_UPD;
6628   case ARM::VST4dWB_fixed_Asm_32: Spacing = 1; return ARM::VST4d32_UPD;
6629   case ARM::VST4qWB_fixed_Asm_8:  Spacing = 2; return ARM::VST4q8_UPD;
6630   case ARM::VST4qWB_fixed_Asm_16: Spacing = 2; return ARM::VST4q16_UPD;
6631   case ARM::VST4qWB_fixed_Asm_32: Spacing = 2; return ARM::VST4q32_UPD;
6632   case ARM::VST4dWB_register_Asm_8:  Spacing = 1; return ARM::VST4d8_UPD;
6633   case ARM::VST4dWB_register_Asm_16: Spacing = 1; return ARM::VST4d16_UPD;
6634   case ARM::VST4dWB_register_Asm_32: Spacing = 1; return ARM::VST4d32_UPD;
6635   case ARM::VST4qWB_register_Asm_8:  Spacing = 2; return ARM::VST4q8_UPD;
6636   case ARM::VST4qWB_register_Asm_16: Spacing = 2; return ARM::VST4q16_UPD;
6637   case ARM::VST4qWB_register_Asm_32: Spacing = 2; return ARM::VST4q32_UPD;
6638   case ARM::VST4dAsm_8:  Spacing = 1; return ARM::VST4d8;
6639   case ARM::VST4dAsm_16: Spacing = 1; return ARM::VST4d16;
6640   case ARM::VST4dAsm_32: Spacing = 1; return ARM::VST4d32;
6641   case ARM::VST4qAsm_8:  Spacing = 2; return ARM::VST4q8;
6642   case ARM::VST4qAsm_16: Spacing = 2; return ARM::VST4q16;
6643   case ARM::VST4qAsm_32: Spacing = 2; return ARM::VST4q32;
6644   }
6645 }
6646 
6647 static unsigned getRealVLDOpcode(unsigned Opc, unsigned &Spacing) {
6648   switch(Opc) {
6649   default: llvm_unreachable("unexpected opcode!");
6650   // VLD1LN
6651   case ARM::VLD1LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD1LNd8_UPD;
6652   case ARM::VLD1LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD1LNd16_UPD;
6653   case ARM::VLD1LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD1LNd32_UPD;
6654   case ARM::VLD1LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD1LNd8_UPD;
6655   case ARM::VLD1LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD1LNd16_UPD;
6656   case ARM::VLD1LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD1LNd32_UPD;
6657   case ARM::VLD1LNdAsm_8:  Spacing = 1; return ARM::VLD1LNd8;
6658   case ARM::VLD1LNdAsm_16: Spacing = 1; return ARM::VLD1LNd16;
6659   case ARM::VLD1LNdAsm_32: Spacing = 1; return ARM::VLD1LNd32;
6660 
6661   // VLD2LN
6662   case ARM::VLD2LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD2LNd8_UPD;
6663   case ARM::VLD2LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD2LNd16_UPD;
6664   case ARM::VLD2LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD2LNd32_UPD;
6665   case ARM::VLD2LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD2LNq16_UPD;
6666   case ARM::VLD2LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD2LNq32_UPD;
6667   case ARM::VLD2LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD2LNd8_UPD;
6668   case ARM::VLD2LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD2LNd16_UPD;
6669   case ARM::VLD2LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD2LNd32_UPD;
6670   case ARM::VLD2LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD2LNq16_UPD;
6671   case ARM::VLD2LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD2LNq32_UPD;
6672   case ARM::VLD2LNdAsm_8:  Spacing = 1; return ARM::VLD2LNd8;
6673   case ARM::VLD2LNdAsm_16: Spacing = 1; return ARM::VLD2LNd16;
6674   case ARM::VLD2LNdAsm_32: Spacing = 1; return ARM::VLD2LNd32;
6675   case ARM::VLD2LNqAsm_16: Spacing = 2; return ARM::VLD2LNq16;
6676   case ARM::VLD2LNqAsm_32: Spacing = 2; return ARM::VLD2LNq32;
6677 
6678   // VLD3DUP
6679   case ARM::VLD3DUPdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3DUPd8_UPD;
6680   case ARM::VLD3DUPdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3DUPd16_UPD;
6681   case ARM::VLD3DUPdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3DUPd32_UPD;
6682   case ARM::VLD3DUPqWB_fixed_Asm_8: Spacing = 1; return ARM::VLD3DUPq8_UPD;
6683   case ARM::VLD3DUPqWB_fixed_Asm_16: Spacing = 2; return ARM::VLD3DUPq16_UPD;
6684   case ARM::VLD3DUPqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3DUPq32_UPD;
6685   case ARM::VLD3DUPdWB_register_Asm_8:  Spacing = 1; return ARM::VLD3DUPd8_UPD;
6686   case ARM::VLD3DUPdWB_register_Asm_16: Spacing = 1; return ARM::VLD3DUPd16_UPD;
6687   case ARM::VLD3DUPdWB_register_Asm_32: Spacing = 1; return ARM::VLD3DUPd32_UPD;
6688   case ARM::VLD3DUPqWB_register_Asm_8: Spacing = 2; return ARM::VLD3DUPq8_UPD;
6689   case ARM::VLD3DUPqWB_register_Asm_16: Spacing = 2; return ARM::VLD3DUPq16_UPD;
6690   case ARM::VLD3DUPqWB_register_Asm_32: Spacing = 2; return ARM::VLD3DUPq32_UPD;
6691   case ARM::VLD3DUPdAsm_8:  Spacing = 1; return ARM::VLD3DUPd8;
6692   case ARM::VLD3DUPdAsm_16: Spacing = 1; return ARM::VLD3DUPd16;
6693   case ARM::VLD3DUPdAsm_32: Spacing = 1; return ARM::VLD3DUPd32;
6694   case ARM::VLD3DUPqAsm_8: Spacing = 2; return ARM::VLD3DUPq8;
6695   case ARM::VLD3DUPqAsm_16: Spacing = 2; return ARM::VLD3DUPq16;
6696   case ARM::VLD3DUPqAsm_32: Spacing = 2; return ARM::VLD3DUPq32;
6697 
6698   // VLD3LN
6699   case ARM::VLD3LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3LNd8_UPD;
6700   case ARM::VLD3LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3LNd16_UPD;
6701   case ARM::VLD3LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3LNd32_UPD;
6702   case ARM::VLD3LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3LNq16_UPD;
6703   case ARM::VLD3LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3LNq32_UPD;
6704   case ARM::VLD3LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD3LNd8_UPD;
6705   case ARM::VLD3LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD3LNd16_UPD;
6706   case ARM::VLD3LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD3LNd32_UPD;
6707   case ARM::VLD3LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD3LNq16_UPD;
6708   case ARM::VLD3LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD3LNq32_UPD;
6709   case ARM::VLD3LNdAsm_8:  Spacing = 1; return ARM::VLD3LNd8;
6710   case ARM::VLD3LNdAsm_16: Spacing = 1; return ARM::VLD3LNd16;
6711   case ARM::VLD3LNdAsm_32: Spacing = 1; return ARM::VLD3LNd32;
6712   case ARM::VLD3LNqAsm_16: Spacing = 2; return ARM::VLD3LNq16;
6713   case ARM::VLD3LNqAsm_32: Spacing = 2; return ARM::VLD3LNq32;
6714 
6715   // VLD3
6716   case ARM::VLD3dWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3d8_UPD;
6717   case ARM::VLD3dWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3d16_UPD;
6718   case ARM::VLD3dWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3d32_UPD;
6719   case ARM::VLD3qWB_fixed_Asm_8:  Spacing = 2; return ARM::VLD3q8_UPD;
6720   case ARM::VLD3qWB_fixed_Asm_16: Spacing = 2; return ARM::VLD3q16_UPD;
6721   case ARM::VLD3qWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3q32_UPD;
6722   case ARM::VLD3dWB_register_Asm_8:  Spacing = 1; return ARM::VLD3d8_UPD;
6723   case ARM::VLD3dWB_register_Asm_16: Spacing = 1; return ARM::VLD3d16_UPD;
6724   case ARM::VLD3dWB_register_Asm_32: Spacing = 1; return ARM::VLD3d32_UPD;
6725   case ARM::VLD3qWB_register_Asm_8:  Spacing = 2; return ARM::VLD3q8_UPD;
6726   case ARM::VLD3qWB_register_Asm_16: Spacing = 2; return ARM::VLD3q16_UPD;
6727   case ARM::VLD3qWB_register_Asm_32: Spacing = 2; return ARM::VLD3q32_UPD;
6728   case ARM::VLD3dAsm_8:  Spacing = 1; return ARM::VLD3d8;
6729   case ARM::VLD3dAsm_16: Spacing = 1; return ARM::VLD3d16;
6730   case ARM::VLD3dAsm_32: Spacing = 1; return ARM::VLD3d32;
6731   case ARM::VLD3qAsm_8:  Spacing = 2; return ARM::VLD3q8;
6732   case ARM::VLD3qAsm_16: Spacing = 2; return ARM::VLD3q16;
6733   case ARM::VLD3qAsm_32: Spacing = 2; return ARM::VLD3q32;
6734 
6735   // VLD4LN
6736   case ARM::VLD4LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4LNd8_UPD;
6737   case ARM::VLD4LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4LNd16_UPD;
6738   case ARM::VLD4LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4LNd32_UPD;
6739   case ARM::VLD4LNqWB_fixed_Asm_16: Spacing = 2; return ARM::VLD4LNq16_UPD;
6740   case ARM::VLD4LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4LNq32_UPD;
6741   case ARM::VLD4LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD4LNd8_UPD;
6742   case ARM::VLD4LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD4LNd16_UPD;
6743   case ARM::VLD4LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD4LNd32_UPD;
6744   case ARM::VLD4LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD4LNq16_UPD;
6745   case ARM::VLD4LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD4LNq32_UPD;
6746   case ARM::VLD4LNdAsm_8:  Spacing = 1; return ARM::VLD4LNd8;
6747   case ARM::VLD4LNdAsm_16: Spacing = 1; return ARM::VLD4LNd16;
6748   case ARM::VLD4LNdAsm_32: Spacing = 1; return ARM::VLD4LNd32;
6749   case ARM::VLD4LNqAsm_16: Spacing = 2; return ARM::VLD4LNq16;
6750   case ARM::VLD4LNqAsm_32: Spacing = 2; return ARM::VLD4LNq32;
6751 
6752   // VLD4DUP
6753   case ARM::VLD4DUPdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4DUPd8_UPD;
6754   case ARM::VLD4DUPdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4DUPd16_UPD;
6755   case ARM::VLD4DUPdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4DUPd32_UPD;
6756   case ARM::VLD4DUPqWB_fixed_Asm_8: Spacing = 1; return ARM::VLD4DUPq8_UPD;
6757   case ARM::VLD4DUPqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4DUPq16_UPD;
6758   case ARM::VLD4DUPqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4DUPq32_UPD;
6759   case ARM::VLD4DUPdWB_register_Asm_8:  Spacing = 1; return ARM::VLD4DUPd8_UPD;
6760   case ARM::VLD4DUPdWB_register_Asm_16: Spacing = 1; return ARM::VLD4DUPd16_UPD;
6761   case ARM::VLD4DUPdWB_register_Asm_32: Spacing = 1; return ARM::VLD4DUPd32_UPD;
6762   case ARM::VLD4DUPqWB_register_Asm_8: Spacing = 2; return ARM::VLD4DUPq8_UPD;
6763   case ARM::VLD4DUPqWB_register_Asm_16: Spacing = 2; return ARM::VLD4DUPq16_UPD;
6764   case ARM::VLD4DUPqWB_register_Asm_32: Spacing = 2; return ARM::VLD4DUPq32_UPD;
6765   case ARM::VLD4DUPdAsm_8:  Spacing = 1; return ARM::VLD4DUPd8;
6766   case ARM::VLD4DUPdAsm_16: Spacing = 1; return ARM::VLD4DUPd16;
6767   case ARM::VLD4DUPdAsm_32: Spacing = 1; return ARM::VLD4DUPd32;
6768   case ARM::VLD4DUPqAsm_8: Spacing = 2; return ARM::VLD4DUPq8;
6769   case ARM::VLD4DUPqAsm_16: Spacing = 2; return ARM::VLD4DUPq16;
6770   case ARM::VLD4DUPqAsm_32: Spacing = 2; return ARM::VLD4DUPq32;
6771 
6772   // VLD4
6773   case ARM::VLD4dWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4d8_UPD;
6774   case ARM::VLD4dWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4d16_UPD;
6775   case ARM::VLD4dWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4d32_UPD;
6776   case ARM::VLD4qWB_fixed_Asm_8:  Spacing = 2; return ARM::VLD4q8_UPD;
6777   case ARM::VLD4qWB_fixed_Asm_16: Spacing = 2; return ARM::VLD4q16_UPD;
6778   case ARM::VLD4qWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4q32_UPD;
6779   case ARM::VLD4dWB_register_Asm_8:  Spacing = 1; return ARM::VLD4d8_UPD;
6780   case ARM::VLD4dWB_register_Asm_16: Spacing = 1; return ARM::VLD4d16_UPD;
6781   case ARM::VLD4dWB_register_Asm_32: Spacing = 1; return ARM::VLD4d32_UPD;
6782   case ARM::VLD4qWB_register_Asm_8:  Spacing = 2; return ARM::VLD4q8_UPD;
6783   case ARM::VLD4qWB_register_Asm_16: Spacing = 2; return ARM::VLD4q16_UPD;
6784   case ARM::VLD4qWB_register_Asm_32: Spacing = 2; return ARM::VLD4q32_UPD;
6785   case ARM::VLD4dAsm_8:  Spacing = 1; return ARM::VLD4d8;
6786   case ARM::VLD4dAsm_16: Spacing = 1; return ARM::VLD4d16;
6787   case ARM::VLD4dAsm_32: Spacing = 1; return ARM::VLD4d32;
6788   case ARM::VLD4qAsm_8:  Spacing = 2; return ARM::VLD4q8;
6789   case ARM::VLD4qAsm_16: Spacing = 2; return ARM::VLD4q16;
6790   case ARM::VLD4qAsm_32: Spacing = 2; return ARM::VLD4q32;
6791   }
6792 }
6793 
6794 bool ARMAsmParser::processInstruction(MCInst &Inst,
6795                                       const OperandVector &Operands,
6796                                       MCStreamer &Out) {
6797   switch (Inst.getOpcode()) {
6798   // Alias for alternate form of 'ldr{,b}t Rt, [Rn], #imm' instruction.
6799   case ARM::LDRT_POST:
6800   case ARM::LDRBT_POST: {
6801     const unsigned Opcode =
6802       (Inst.getOpcode() == ARM::LDRT_POST) ? ARM::LDRT_POST_IMM
6803                                            : ARM::LDRBT_POST_IMM;
6804     MCInst TmpInst;
6805     TmpInst.setOpcode(Opcode);
6806     TmpInst.addOperand(Inst.getOperand(0));
6807     TmpInst.addOperand(Inst.getOperand(1));
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 'str{,b}t Rt, [Rn], #imm' instruction.
6817   case ARM::STRT_POST:
6818   case ARM::STRBT_POST: {
6819     const unsigned Opcode =
6820       (Inst.getOpcode() == ARM::STRT_POST) ? ARM::STRT_POST_IMM
6821                                            : ARM::STRBT_POST_IMM;
6822     MCInst TmpInst;
6823     TmpInst.setOpcode(Opcode);
6824     TmpInst.addOperand(Inst.getOperand(1));
6825     TmpInst.addOperand(Inst.getOperand(0));
6826     TmpInst.addOperand(Inst.getOperand(1));
6827     TmpInst.addOperand(MCOperand::createReg(0));
6828     TmpInst.addOperand(MCOperand::createImm(0));
6829     TmpInst.addOperand(Inst.getOperand(2));
6830     TmpInst.addOperand(Inst.getOperand(3));
6831     Inst = TmpInst;
6832     return true;
6833   }
6834   // Alias for alternate form of 'ADR Rd, #imm' instruction.
6835   case ARM::ADDri: {
6836     if (Inst.getOperand(1).getReg() != ARM::PC ||
6837         Inst.getOperand(5).getReg() != 0 ||
6838         !(Inst.getOperand(2).isExpr() || Inst.getOperand(2).isImm()))
6839       return false;
6840     MCInst TmpInst;
6841     TmpInst.setOpcode(ARM::ADR);
6842     TmpInst.addOperand(Inst.getOperand(0));
6843     if (Inst.getOperand(2).isImm()) {
6844       // Immediate (mod_imm) will be in its encoded form, we must unencode it
6845       // before passing it to the ADR instruction.
6846       unsigned Enc = Inst.getOperand(2).getImm();
6847       TmpInst.addOperand(MCOperand::createImm(
6848         ARM_AM::rotr32(Enc & 0xFF, (Enc & 0xF00) >> 7)));
6849     } else {
6850       // Turn PC-relative expression into absolute expression.
6851       // Reading PC provides the start of the current instruction + 8 and
6852       // the transform to adr is biased by that.
6853       MCSymbol *Dot = getContext().createTempSymbol();
6854       Out.EmitLabel(Dot);
6855       const MCExpr *OpExpr = Inst.getOperand(2).getExpr();
6856       const MCExpr *InstPC = MCSymbolRefExpr::create(Dot,
6857                                                      MCSymbolRefExpr::VK_None,
6858                                                      getContext());
6859       const MCExpr *Const8 = MCConstantExpr::create(8, getContext());
6860       const MCExpr *ReadPC = MCBinaryExpr::createAdd(InstPC, Const8,
6861                                                      getContext());
6862       const MCExpr *FixupAddr = MCBinaryExpr::createAdd(ReadPC, OpExpr,
6863                                                         getContext());
6864       TmpInst.addOperand(MCOperand::createExpr(FixupAddr));
6865     }
6866     TmpInst.addOperand(Inst.getOperand(3));
6867     TmpInst.addOperand(Inst.getOperand(4));
6868     Inst = TmpInst;
6869     return true;
6870   }
6871   // Aliases for alternate PC+imm syntax of LDR instructions.
6872   case ARM::t2LDRpcrel:
6873     // Select the narrow version if the immediate will fit.
6874     if (Inst.getOperand(1).getImm() > 0 &&
6875         Inst.getOperand(1).getImm() <= 0xff &&
6876         !(static_cast<ARMOperand &>(*Operands[2]).isToken() &&
6877           static_cast<ARMOperand &>(*Operands[2]).getToken() == ".w"))
6878       Inst.setOpcode(ARM::tLDRpci);
6879     else
6880       Inst.setOpcode(ARM::t2LDRpci);
6881     return true;
6882   case ARM::t2LDRBpcrel:
6883     Inst.setOpcode(ARM::t2LDRBpci);
6884     return true;
6885   case ARM::t2LDRHpcrel:
6886     Inst.setOpcode(ARM::t2LDRHpci);
6887     return true;
6888   case ARM::t2LDRSBpcrel:
6889     Inst.setOpcode(ARM::t2LDRSBpci);
6890     return true;
6891   case ARM::t2LDRSHpcrel:
6892     Inst.setOpcode(ARM::t2LDRSHpci);
6893     return true;
6894   case ARM::LDRConstPool:
6895   case ARM::tLDRConstPool:
6896   case ARM::t2LDRConstPool: {
6897     // Pseudo instruction ldr rt, =immediate is converted to a
6898     // MOV rt, immediate if immediate is known and representable
6899     // otherwise we create a constant pool entry that we load from.
6900     MCInst TmpInst;
6901     if (Inst.getOpcode() == ARM::LDRConstPool)
6902       TmpInst.setOpcode(ARM::LDRi12);
6903     else if (Inst.getOpcode() == ARM::tLDRConstPool)
6904       TmpInst.setOpcode(ARM::tLDRpci);
6905     else if (Inst.getOpcode() == ARM::t2LDRConstPool)
6906       TmpInst.setOpcode(ARM::t2LDRpci);
6907     const ARMOperand &PoolOperand =
6908       static_cast<ARMOperand &>(*Operands[3]);
6909     const MCExpr *SubExprVal = PoolOperand.getConstantPoolImm();
6910     // If SubExprVal is a constant we may be able to use a MOV
6911     if (isa<MCConstantExpr>(SubExprVal) &&
6912         Inst.getOperand(0).getReg() != ARM::PC &&
6913         Inst.getOperand(0).getReg() != ARM::SP) {
6914       int64_t Value =
6915         (int64_t) (cast<MCConstantExpr>(SubExprVal))->getValue();
6916       bool UseMov  = true;
6917       bool MovHasS = true;
6918       if (Inst.getOpcode() == ARM::LDRConstPool) {
6919         // ARM Constant
6920         if (ARM_AM::getSOImmVal(Value) != -1) {
6921           Value = ARM_AM::getSOImmVal(Value);
6922           TmpInst.setOpcode(ARM::MOVi);
6923         }
6924         else if (ARM_AM::getSOImmVal(~Value) != -1) {
6925           Value = ARM_AM::getSOImmVal(~Value);
6926           TmpInst.setOpcode(ARM::MVNi);
6927         }
6928         else if (hasV6T2Ops() &&
6929                  Value >=0 && Value < 65536) {
6930           TmpInst.setOpcode(ARM::MOVi16);
6931           MovHasS = false;
6932         }
6933         else
6934           UseMov = false;
6935       }
6936       else {
6937         // Thumb/Thumb2 Constant
6938         if (hasThumb2() &&
6939             ARM_AM::getT2SOImmVal(Value) != -1)
6940           TmpInst.setOpcode(ARM::t2MOVi);
6941         else if (hasThumb2() &&
6942                  ARM_AM::getT2SOImmVal(~Value) != -1) {
6943           TmpInst.setOpcode(ARM::t2MVNi);
6944           Value = ~Value;
6945         }
6946         else if (hasV8MBaseline() &&
6947                  Value >=0 && Value < 65536) {
6948           TmpInst.setOpcode(ARM::t2MOVi16);
6949           MovHasS = false;
6950         }
6951         else
6952           UseMov = false;
6953       }
6954       if (UseMov) {
6955         TmpInst.addOperand(Inst.getOperand(0));           // Rt
6956         TmpInst.addOperand(MCOperand::createImm(Value));  // Immediate
6957         TmpInst.addOperand(Inst.getOperand(2));           // CondCode
6958         TmpInst.addOperand(Inst.getOperand(3));           // CondCode
6959         if (MovHasS)
6960           TmpInst.addOperand(MCOperand::createReg(0));    // S
6961         Inst = TmpInst;
6962         return true;
6963       }
6964     }
6965     // No opportunity to use MOV/MVN create constant pool
6966     const MCExpr *CPLoc =
6967       getTargetStreamer().addConstantPoolEntry(SubExprVal,
6968                                                PoolOperand.getStartLoc());
6969     TmpInst.addOperand(Inst.getOperand(0));           // Rt
6970     TmpInst.addOperand(MCOperand::createExpr(CPLoc)); // offset to constpool
6971     if (TmpInst.getOpcode() == ARM::LDRi12)
6972       TmpInst.addOperand(MCOperand::createImm(0));    // unused offset
6973     TmpInst.addOperand(Inst.getOperand(2));           // CondCode
6974     TmpInst.addOperand(Inst.getOperand(3));           // CondCode
6975     Inst = TmpInst;
6976     return true;
6977   }
6978   // Handle NEON VST complex aliases.
6979   case ARM::VST1LNdWB_register_Asm_8:
6980   case ARM::VST1LNdWB_register_Asm_16:
6981   case ARM::VST1LNdWB_register_Asm_32: {
6982     MCInst TmpInst;
6983     // Shuffle the operands around so the lane index operand is in the
6984     // right place.
6985     unsigned Spacing;
6986     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
6987     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
6988     TmpInst.addOperand(Inst.getOperand(2)); // Rn
6989     TmpInst.addOperand(Inst.getOperand(3)); // alignment
6990     TmpInst.addOperand(Inst.getOperand(4)); // Rm
6991     TmpInst.addOperand(Inst.getOperand(0)); // Vd
6992     TmpInst.addOperand(Inst.getOperand(1)); // lane
6993     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
6994     TmpInst.addOperand(Inst.getOperand(6));
6995     Inst = TmpInst;
6996     return true;
6997   }
6998 
6999   case ARM::VST2LNdWB_register_Asm_8:
7000   case ARM::VST2LNdWB_register_Asm_16:
7001   case ARM::VST2LNdWB_register_Asm_32:
7002   case ARM::VST2LNqWB_register_Asm_16:
7003   case ARM::VST2LNqWB_register_Asm_32: {
7004     MCInst TmpInst;
7005     // Shuffle the operands around so the lane index operand is in the
7006     // right place.
7007     unsigned Spacing;
7008     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7009     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7010     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7011     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7012     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7013     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7014     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7015                                             Spacing));
7016     TmpInst.addOperand(Inst.getOperand(1)); // lane
7017     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7018     TmpInst.addOperand(Inst.getOperand(6));
7019     Inst = TmpInst;
7020     return true;
7021   }
7022 
7023   case ARM::VST3LNdWB_register_Asm_8:
7024   case ARM::VST3LNdWB_register_Asm_16:
7025   case ARM::VST3LNdWB_register_Asm_32:
7026   case ARM::VST3LNqWB_register_Asm_16:
7027   case ARM::VST3LNqWB_register_Asm_32: {
7028     MCInst TmpInst;
7029     // Shuffle the operands around so the lane index operand is in the
7030     // right place.
7031     unsigned Spacing;
7032     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7033     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7034     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7035     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7036     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7037     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7038     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7039                                             Spacing));
7040     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7041                                             Spacing * 2));
7042     TmpInst.addOperand(Inst.getOperand(1)); // lane
7043     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7044     TmpInst.addOperand(Inst.getOperand(6));
7045     Inst = TmpInst;
7046     return true;
7047   }
7048 
7049   case ARM::VST4LNdWB_register_Asm_8:
7050   case ARM::VST4LNdWB_register_Asm_16:
7051   case ARM::VST4LNdWB_register_Asm_32:
7052   case ARM::VST4LNqWB_register_Asm_16:
7053   case ARM::VST4LNqWB_register_Asm_32: {
7054     MCInst TmpInst;
7055     // Shuffle the operands around so the lane index operand is in the
7056     // right place.
7057     unsigned Spacing;
7058     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7059     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7060     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7061     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7062     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7063     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7064     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7065                                             Spacing));
7066     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7067                                             Spacing * 2));
7068     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7069                                             Spacing * 3));
7070     TmpInst.addOperand(Inst.getOperand(1)); // lane
7071     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7072     TmpInst.addOperand(Inst.getOperand(6));
7073     Inst = TmpInst;
7074     return true;
7075   }
7076 
7077   case ARM::VST1LNdWB_fixed_Asm_8:
7078   case ARM::VST1LNdWB_fixed_Asm_16:
7079   case ARM::VST1LNdWB_fixed_Asm_32: {
7080     MCInst TmpInst;
7081     // Shuffle the operands around so the lane index operand is in the
7082     // right place.
7083     unsigned Spacing;
7084     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7085     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7086     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7087     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7088     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7089     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7090     TmpInst.addOperand(Inst.getOperand(1)); // lane
7091     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7092     TmpInst.addOperand(Inst.getOperand(5));
7093     Inst = TmpInst;
7094     return true;
7095   }
7096 
7097   case ARM::VST2LNdWB_fixed_Asm_8:
7098   case ARM::VST2LNdWB_fixed_Asm_16:
7099   case ARM::VST2LNdWB_fixed_Asm_32:
7100   case ARM::VST2LNqWB_fixed_Asm_16:
7101   case ARM::VST2LNqWB_fixed_Asm_32: {
7102     MCInst TmpInst;
7103     // Shuffle the operands around so the lane index operand is in the
7104     // right place.
7105     unsigned Spacing;
7106     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7107     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7108     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7109     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7110     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7111     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7112     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7113                                             Spacing));
7114     TmpInst.addOperand(Inst.getOperand(1)); // lane
7115     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7116     TmpInst.addOperand(Inst.getOperand(5));
7117     Inst = TmpInst;
7118     return true;
7119   }
7120 
7121   case ARM::VST3LNdWB_fixed_Asm_8:
7122   case ARM::VST3LNdWB_fixed_Asm_16:
7123   case ARM::VST3LNdWB_fixed_Asm_32:
7124   case ARM::VST3LNqWB_fixed_Asm_16:
7125   case ARM::VST3LNqWB_fixed_Asm_32: {
7126     MCInst TmpInst;
7127     // Shuffle the operands around so the lane index operand is in the
7128     // right place.
7129     unsigned Spacing;
7130     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7131     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7132     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7133     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7134     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7135     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7136     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7137                                             Spacing));
7138     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7139                                             Spacing * 2));
7140     TmpInst.addOperand(Inst.getOperand(1)); // lane
7141     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7142     TmpInst.addOperand(Inst.getOperand(5));
7143     Inst = TmpInst;
7144     return true;
7145   }
7146 
7147   case ARM::VST4LNdWB_fixed_Asm_8:
7148   case ARM::VST4LNdWB_fixed_Asm_16:
7149   case ARM::VST4LNdWB_fixed_Asm_32:
7150   case ARM::VST4LNqWB_fixed_Asm_16:
7151   case ARM::VST4LNqWB_fixed_Asm_32: {
7152     MCInst TmpInst;
7153     // Shuffle the operands around so the lane index operand is in the
7154     // right place.
7155     unsigned Spacing;
7156     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7157     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7158     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7159     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7160     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7161     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7162     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7163                                             Spacing));
7164     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7165                                             Spacing * 2));
7166     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7167                                             Spacing * 3));
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::VST1LNdAsm_8:
7176   case ARM::VST1LNdAsm_16:
7177   case ARM::VST1LNdAsm_32: {
7178     MCInst TmpInst;
7179     // Shuffle the operands around so the lane index operand is in the
7180     // right place.
7181     unsigned Spacing;
7182     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7183     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7184     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7185     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7186     TmpInst.addOperand(Inst.getOperand(1)); // lane
7187     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7188     TmpInst.addOperand(Inst.getOperand(5));
7189     Inst = TmpInst;
7190     return true;
7191   }
7192 
7193   case ARM::VST2LNdAsm_8:
7194   case ARM::VST2LNdAsm_16:
7195   case ARM::VST2LNdAsm_32:
7196   case ARM::VST2LNqAsm_16:
7197   case ARM::VST2LNqAsm_32: {
7198     MCInst TmpInst;
7199     // Shuffle the operands around so the lane index operand is in the
7200     // right place.
7201     unsigned Spacing;
7202     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7203     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7204     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7205     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7206     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7207                                             Spacing));
7208     TmpInst.addOperand(Inst.getOperand(1)); // lane
7209     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7210     TmpInst.addOperand(Inst.getOperand(5));
7211     Inst = TmpInst;
7212     return true;
7213   }
7214 
7215   case ARM::VST3LNdAsm_8:
7216   case ARM::VST3LNdAsm_16:
7217   case ARM::VST3LNdAsm_32:
7218   case ARM::VST3LNqAsm_16:
7219   case ARM::VST3LNqAsm_32: {
7220     MCInst TmpInst;
7221     // Shuffle the operands around so the lane index operand is in the
7222     // right place.
7223     unsigned Spacing;
7224     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7225     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7226     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7227     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7228     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7229                                             Spacing));
7230     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7231                                             Spacing * 2));
7232     TmpInst.addOperand(Inst.getOperand(1)); // lane
7233     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7234     TmpInst.addOperand(Inst.getOperand(5));
7235     Inst = TmpInst;
7236     return true;
7237   }
7238 
7239   case ARM::VST4LNdAsm_8:
7240   case ARM::VST4LNdAsm_16:
7241   case ARM::VST4LNdAsm_32:
7242   case ARM::VST4LNqAsm_16:
7243   case ARM::VST4LNqAsm_32: {
7244     MCInst TmpInst;
7245     // Shuffle the operands around so the lane index operand is in the
7246     // right place.
7247     unsigned Spacing;
7248     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7249     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7250     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7251     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7252     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7253                                             Spacing));
7254     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7255                                             Spacing * 2));
7256     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7257                                             Spacing * 3));
7258     TmpInst.addOperand(Inst.getOperand(1)); // lane
7259     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7260     TmpInst.addOperand(Inst.getOperand(5));
7261     Inst = TmpInst;
7262     return true;
7263   }
7264 
7265   // Handle NEON VLD complex aliases.
7266   case ARM::VLD1LNdWB_register_Asm_8:
7267   case ARM::VLD1LNdWB_register_Asm_16:
7268   case ARM::VLD1LNdWB_register_Asm_32: {
7269     MCInst TmpInst;
7270     // Shuffle the operands around so the lane index operand is in the
7271     // right place.
7272     unsigned Spacing;
7273     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7274     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7275     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7276     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7277     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7278     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7279     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7280     TmpInst.addOperand(Inst.getOperand(1)); // lane
7281     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7282     TmpInst.addOperand(Inst.getOperand(6));
7283     Inst = TmpInst;
7284     return true;
7285   }
7286 
7287   case ARM::VLD2LNdWB_register_Asm_8:
7288   case ARM::VLD2LNdWB_register_Asm_16:
7289   case ARM::VLD2LNdWB_register_Asm_32:
7290   case ARM::VLD2LNqWB_register_Asm_16:
7291   case ARM::VLD2LNqWB_register_Asm_32: {
7292     MCInst TmpInst;
7293     // Shuffle the operands around so the lane index operand is in the
7294     // right place.
7295     unsigned Spacing;
7296     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7297     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7298     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7299                                             Spacing));
7300     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7301     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7302     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7303     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7304     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7305     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7306                                             Spacing));
7307     TmpInst.addOperand(Inst.getOperand(1)); // lane
7308     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7309     TmpInst.addOperand(Inst.getOperand(6));
7310     Inst = TmpInst;
7311     return true;
7312   }
7313 
7314   case ARM::VLD3LNdWB_register_Asm_8:
7315   case ARM::VLD3LNdWB_register_Asm_16:
7316   case ARM::VLD3LNdWB_register_Asm_32:
7317   case ARM::VLD3LNqWB_register_Asm_16:
7318   case ARM::VLD3LNqWB_register_Asm_32: {
7319     MCInst TmpInst;
7320     // Shuffle the operands around so the lane index operand is in the
7321     // right place.
7322     unsigned Spacing;
7323     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7324     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7325     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7326                                             Spacing));
7327     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7328                                             Spacing * 2));
7329     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7330     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7331     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7332     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7333     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7334     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7335                                             Spacing));
7336     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7337                                             Spacing * 2));
7338     TmpInst.addOperand(Inst.getOperand(1)); // lane
7339     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7340     TmpInst.addOperand(Inst.getOperand(6));
7341     Inst = TmpInst;
7342     return true;
7343   }
7344 
7345   case ARM::VLD4LNdWB_register_Asm_8:
7346   case ARM::VLD4LNdWB_register_Asm_16:
7347   case ARM::VLD4LNdWB_register_Asm_32:
7348   case ARM::VLD4LNqWB_register_Asm_16:
7349   case ARM::VLD4LNqWB_register_Asm_32: {
7350     MCInst TmpInst;
7351     // Shuffle the operands around so the lane index operand is in the
7352     // right place.
7353     unsigned Spacing;
7354     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7355     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7356     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7357                                             Spacing));
7358     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7359                                             Spacing * 2));
7360     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7361                                             Spacing * 3));
7362     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7363     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7364     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7365     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7366     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7367     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7368                                             Spacing));
7369     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7370                                             Spacing * 2));
7371     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7372                                             Spacing * 3));
7373     TmpInst.addOperand(Inst.getOperand(1)); // lane
7374     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7375     TmpInst.addOperand(Inst.getOperand(6));
7376     Inst = TmpInst;
7377     return true;
7378   }
7379 
7380   case ARM::VLD1LNdWB_fixed_Asm_8:
7381   case ARM::VLD1LNdWB_fixed_Asm_16:
7382   case ARM::VLD1LNdWB_fixed_Asm_32: {
7383     MCInst TmpInst;
7384     // Shuffle the operands around so the lane index operand is in the
7385     // right place.
7386     unsigned Spacing;
7387     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7388     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7389     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7390     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7391     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7392     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7393     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7394     TmpInst.addOperand(Inst.getOperand(1)); // lane
7395     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7396     TmpInst.addOperand(Inst.getOperand(5));
7397     Inst = TmpInst;
7398     return true;
7399   }
7400 
7401   case ARM::VLD2LNdWB_fixed_Asm_8:
7402   case ARM::VLD2LNdWB_fixed_Asm_16:
7403   case ARM::VLD2LNdWB_fixed_Asm_32:
7404   case ARM::VLD2LNqWB_fixed_Asm_16:
7405   case ARM::VLD2LNqWB_fixed_Asm_32: {
7406     MCInst TmpInst;
7407     // Shuffle the operands around so the lane index operand is in the
7408     // right place.
7409     unsigned Spacing;
7410     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7411     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7412     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7413                                             Spacing));
7414     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7415     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7416     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7417     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7418     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7419     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7420                                             Spacing));
7421     TmpInst.addOperand(Inst.getOperand(1)); // lane
7422     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7423     TmpInst.addOperand(Inst.getOperand(5));
7424     Inst = TmpInst;
7425     return true;
7426   }
7427 
7428   case ARM::VLD3LNdWB_fixed_Asm_8:
7429   case ARM::VLD3LNdWB_fixed_Asm_16:
7430   case ARM::VLD3LNdWB_fixed_Asm_32:
7431   case ARM::VLD3LNqWB_fixed_Asm_16:
7432   case ARM::VLD3LNqWB_fixed_Asm_32: {
7433     MCInst TmpInst;
7434     // Shuffle the operands around so the lane index operand is in the
7435     // right place.
7436     unsigned Spacing;
7437     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7438     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7439     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7440                                             Spacing));
7441     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7442                                             Spacing * 2));
7443     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7444     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7445     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7446     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7447     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7448     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7449                                             Spacing));
7450     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7451                                             Spacing * 2));
7452     TmpInst.addOperand(Inst.getOperand(1)); // lane
7453     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7454     TmpInst.addOperand(Inst.getOperand(5));
7455     Inst = TmpInst;
7456     return true;
7457   }
7458 
7459   case ARM::VLD4LNdWB_fixed_Asm_8:
7460   case ARM::VLD4LNdWB_fixed_Asm_16:
7461   case ARM::VLD4LNdWB_fixed_Asm_32:
7462   case ARM::VLD4LNqWB_fixed_Asm_16:
7463   case ARM::VLD4LNqWB_fixed_Asm_32: {
7464     MCInst TmpInst;
7465     // Shuffle the operands around so the lane index operand is in the
7466     // right place.
7467     unsigned Spacing;
7468     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7469     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7470     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7471                                             Spacing));
7472     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7473                                             Spacing * 2));
7474     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7475                                             Spacing * 3));
7476     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7477     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7478     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7479     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7480     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7481     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7482                                             Spacing));
7483     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7484                                             Spacing * 2));
7485     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7486                                             Spacing * 3));
7487     TmpInst.addOperand(Inst.getOperand(1)); // lane
7488     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7489     TmpInst.addOperand(Inst.getOperand(5));
7490     Inst = TmpInst;
7491     return true;
7492   }
7493 
7494   case ARM::VLD1LNdAsm_8:
7495   case ARM::VLD1LNdAsm_16:
7496   case ARM::VLD1LNdAsm_32: {
7497     MCInst TmpInst;
7498     // Shuffle the operands around so the lane index operand is in the
7499     // right place.
7500     unsigned Spacing;
7501     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7502     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7503     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7504     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7505     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7506     TmpInst.addOperand(Inst.getOperand(1)); // lane
7507     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7508     TmpInst.addOperand(Inst.getOperand(5));
7509     Inst = TmpInst;
7510     return true;
7511   }
7512 
7513   case ARM::VLD2LNdAsm_8:
7514   case ARM::VLD2LNdAsm_16:
7515   case ARM::VLD2LNdAsm_32:
7516   case ARM::VLD2LNqAsm_16:
7517   case ARM::VLD2LNqAsm_32: {
7518     MCInst TmpInst;
7519     // Shuffle the operands around so the lane index operand is in the
7520     // right place.
7521     unsigned Spacing;
7522     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7523     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7524     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7525                                             Spacing));
7526     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7527     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7528     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7529     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7530                                             Spacing));
7531     TmpInst.addOperand(Inst.getOperand(1)); // lane
7532     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7533     TmpInst.addOperand(Inst.getOperand(5));
7534     Inst = TmpInst;
7535     return true;
7536   }
7537 
7538   case ARM::VLD3LNdAsm_8:
7539   case ARM::VLD3LNdAsm_16:
7540   case ARM::VLD3LNdAsm_32:
7541   case ARM::VLD3LNqAsm_16:
7542   case ARM::VLD3LNqAsm_32: {
7543     MCInst TmpInst;
7544     // Shuffle the operands around so the lane index operand is in the
7545     // right place.
7546     unsigned Spacing;
7547     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7548     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7549     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7550                                             Spacing));
7551     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7552                                             Spacing * 2));
7553     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7554     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7555     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7556     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7557                                             Spacing));
7558     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7559                                             Spacing * 2));
7560     TmpInst.addOperand(Inst.getOperand(1)); // lane
7561     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7562     TmpInst.addOperand(Inst.getOperand(5));
7563     Inst = TmpInst;
7564     return true;
7565   }
7566 
7567   case ARM::VLD4LNdAsm_8:
7568   case ARM::VLD4LNdAsm_16:
7569   case ARM::VLD4LNdAsm_32:
7570   case ARM::VLD4LNqAsm_16:
7571   case ARM::VLD4LNqAsm_32: {
7572     MCInst TmpInst;
7573     // Shuffle the operands around so the lane index operand is in the
7574     // right place.
7575     unsigned Spacing;
7576     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7577     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7578     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7579                                             Spacing));
7580     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7581                                             Spacing * 2));
7582     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7583                                             Spacing * 3));
7584     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7585     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7586     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7587     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7588                                             Spacing));
7589     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7590                                             Spacing * 2));
7591     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7592                                             Spacing * 3));
7593     TmpInst.addOperand(Inst.getOperand(1)); // lane
7594     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7595     TmpInst.addOperand(Inst.getOperand(5));
7596     Inst = TmpInst;
7597     return true;
7598   }
7599 
7600   // VLD3DUP single 3-element structure to all lanes instructions.
7601   case ARM::VLD3DUPdAsm_8:
7602   case ARM::VLD3DUPdAsm_16:
7603   case ARM::VLD3DUPdAsm_32:
7604   case ARM::VLD3DUPqAsm_8:
7605   case ARM::VLD3DUPqAsm_16:
7606   case ARM::VLD3DUPqAsm_32: {
7607     MCInst TmpInst;
7608     unsigned Spacing;
7609     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7610     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7611     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7612                                             Spacing));
7613     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7614                                             Spacing * 2));
7615     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7616     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7617     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7618     TmpInst.addOperand(Inst.getOperand(4));
7619     Inst = TmpInst;
7620     return true;
7621   }
7622 
7623   case ARM::VLD3DUPdWB_fixed_Asm_8:
7624   case ARM::VLD3DUPdWB_fixed_Asm_16:
7625   case ARM::VLD3DUPdWB_fixed_Asm_32:
7626   case ARM::VLD3DUPqWB_fixed_Asm_8:
7627   case ARM::VLD3DUPqWB_fixed_Asm_16:
7628   case ARM::VLD3DUPqWB_fixed_Asm_32: {
7629     MCInst TmpInst;
7630     unsigned Spacing;
7631     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7632     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7633     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7634                                             Spacing));
7635     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7636                                             Spacing * 2));
7637     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7638     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7639     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7640     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7641     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7642     TmpInst.addOperand(Inst.getOperand(4));
7643     Inst = TmpInst;
7644     return true;
7645   }
7646 
7647   case ARM::VLD3DUPdWB_register_Asm_8:
7648   case ARM::VLD3DUPdWB_register_Asm_16:
7649   case ARM::VLD3DUPdWB_register_Asm_32:
7650   case ARM::VLD3DUPqWB_register_Asm_8:
7651   case ARM::VLD3DUPqWB_register_Asm_16:
7652   case ARM::VLD3DUPqWB_register_Asm_32: {
7653     MCInst TmpInst;
7654     unsigned Spacing;
7655     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7656     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7657     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7658                                             Spacing));
7659     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7660                                             Spacing * 2));
7661     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7662     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7663     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7664     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7665     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7666     TmpInst.addOperand(Inst.getOperand(5));
7667     Inst = TmpInst;
7668     return true;
7669   }
7670 
7671   // VLD3 multiple 3-element structure instructions.
7672   case ARM::VLD3dAsm_8:
7673   case ARM::VLD3dAsm_16:
7674   case ARM::VLD3dAsm_32:
7675   case ARM::VLD3qAsm_8:
7676   case ARM::VLD3qAsm_16:
7677   case ARM::VLD3qAsm_32: {
7678     MCInst TmpInst;
7679     unsigned Spacing;
7680     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7681     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7682     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7683                                             Spacing));
7684     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7685                                             Spacing * 2));
7686     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7687     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7688     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7689     TmpInst.addOperand(Inst.getOperand(4));
7690     Inst = TmpInst;
7691     return true;
7692   }
7693 
7694   case ARM::VLD3dWB_fixed_Asm_8:
7695   case ARM::VLD3dWB_fixed_Asm_16:
7696   case ARM::VLD3dWB_fixed_Asm_32:
7697   case ARM::VLD3qWB_fixed_Asm_8:
7698   case ARM::VLD3qWB_fixed_Asm_16:
7699   case ARM::VLD3qWB_fixed_Asm_32: {
7700     MCInst TmpInst;
7701     unsigned Spacing;
7702     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7703     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7704     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7705                                             Spacing));
7706     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7707                                             Spacing * 2));
7708     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7709     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7710     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7711     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7712     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7713     TmpInst.addOperand(Inst.getOperand(4));
7714     Inst = TmpInst;
7715     return true;
7716   }
7717 
7718   case ARM::VLD3dWB_register_Asm_8:
7719   case ARM::VLD3dWB_register_Asm_16:
7720   case ARM::VLD3dWB_register_Asm_32:
7721   case ARM::VLD3qWB_register_Asm_8:
7722   case ARM::VLD3qWB_register_Asm_16:
7723   case ARM::VLD3qWB_register_Asm_32: {
7724     MCInst TmpInst;
7725     unsigned Spacing;
7726     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7727     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7728     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7729                                             Spacing));
7730     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7731                                             Spacing * 2));
7732     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7733     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7734     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7735     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7736     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7737     TmpInst.addOperand(Inst.getOperand(5));
7738     Inst = TmpInst;
7739     return true;
7740   }
7741 
7742   // VLD4DUP single 3-element structure to all lanes instructions.
7743   case ARM::VLD4DUPdAsm_8:
7744   case ARM::VLD4DUPdAsm_16:
7745   case ARM::VLD4DUPdAsm_32:
7746   case ARM::VLD4DUPqAsm_8:
7747   case ARM::VLD4DUPqAsm_16:
7748   case ARM::VLD4DUPqAsm_32: {
7749     MCInst TmpInst;
7750     unsigned Spacing;
7751     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7752     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7753     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7754                                             Spacing));
7755     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7756                                             Spacing * 2));
7757     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7758                                             Spacing * 3));
7759     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7760     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7761     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7762     TmpInst.addOperand(Inst.getOperand(4));
7763     Inst = TmpInst;
7764     return true;
7765   }
7766 
7767   case ARM::VLD4DUPdWB_fixed_Asm_8:
7768   case ARM::VLD4DUPdWB_fixed_Asm_16:
7769   case ARM::VLD4DUPdWB_fixed_Asm_32:
7770   case ARM::VLD4DUPqWB_fixed_Asm_8:
7771   case ARM::VLD4DUPqWB_fixed_Asm_16:
7772   case ARM::VLD4DUPqWB_fixed_Asm_32: {
7773     MCInst TmpInst;
7774     unsigned Spacing;
7775     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7776     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7777     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7778                                             Spacing));
7779     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7780                                             Spacing * 2));
7781     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7782                                             Spacing * 3));
7783     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7784     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7785     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7786     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7787     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7788     TmpInst.addOperand(Inst.getOperand(4));
7789     Inst = TmpInst;
7790     return true;
7791   }
7792 
7793   case ARM::VLD4DUPdWB_register_Asm_8:
7794   case ARM::VLD4DUPdWB_register_Asm_16:
7795   case ARM::VLD4DUPdWB_register_Asm_32:
7796   case ARM::VLD4DUPqWB_register_Asm_8:
7797   case ARM::VLD4DUPqWB_register_Asm_16:
7798   case ARM::VLD4DUPqWB_register_Asm_32: {
7799     MCInst TmpInst;
7800     unsigned Spacing;
7801     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7802     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7803     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7804                                             Spacing));
7805     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7806                                             Spacing * 2));
7807     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7808                                             Spacing * 3));
7809     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7810     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7811     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7812     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7813     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7814     TmpInst.addOperand(Inst.getOperand(5));
7815     Inst = TmpInst;
7816     return true;
7817   }
7818 
7819   // VLD4 multiple 4-element structure instructions.
7820   case ARM::VLD4dAsm_8:
7821   case ARM::VLD4dAsm_16:
7822   case ARM::VLD4dAsm_32:
7823   case ARM::VLD4qAsm_8:
7824   case ARM::VLD4qAsm_16:
7825   case ARM::VLD4qAsm_32: {
7826     MCInst TmpInst;
7827     unsigned Spacing;
7828     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7829     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7830     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7831                                             Spacing));
7832     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7833                                             Spacing * 2));
7834     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7835                                             Spacing * 3));
7836     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7837     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7838     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7839     TmpInst.addOperand(Inst.getOperand(4));
7840     Inst = TmpInst;
7841     return true;
7842   }
7843 
7844   case ARM::VLD4dWB_fixed_Asm_8:
7845   case ARM::VLD4dWB_fixed_Asm_16:
7846   case ARM::VLD4dWB_fixed_Asm_32:
7847   case ARM::VLD4qWB_fixed_Asm_8:
7848   case ARM::VLD4qWB_fixed_Asm_16:
7849   case ARM::VLD4qWB_fixed_Asm_32: {
7850     MCInst TmpInst;
7851     unsigned Spacing;
7852     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7853     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7854     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7855                                             Spacing));
7856     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7857                                             Spacing * 2));
7858     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7859                                             Spacing * 3));
7860     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7861     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7862     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7863     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7864     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7865     TmpInst.addOperand(Inst.getOperand(4));
7866     Inst = TmpInst;
7867     return true;
7868   }
7869 
7870   case ARM::VLD4dWB_register_Asm_8:
7871   case ARM::VLD4dWB_register_Asm_16:
7872   case ARM::VLD4dWB_register_Asm_32:
7873   case ARM::VLD4qWB_register_Asm_8:
7874   case ARM::VLD4qWB_register_Asm_16:
7875   case ARM::VLD4qWB_register_Asm_32: {
7876     MCInst TmpInst;
7877     unsigned Spacing;
7878     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7879     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7880     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7881                                             Spacing));
7882     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7883                                             Spacing * 2));
7884     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7885                                             Spacing * 3));
7886     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7887     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7888     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7889     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7890     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7891     TmpInst.addOperand(Inst.getOperand(5));
7892     Inst = TmpInst;
7893     return true;
7894   }
7895 
7896   // VST3 multiple 3-element structure instructions.
7897   case ARM::VST3dAsm_8:
7898   case ARM::VST3dAsm_16:
7899   case ARM::VST3dAsm_32:
7900   case ARM::VST3qAsm_8:
7901   case ARM::VST3qAsm_16:
7902   case ARM::VST3qAsm_32: {
7903     MCInst TmpInst;
7904     unsigned Spacing;
7905     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7906     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7907     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7908     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7909     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7910                                             Spacing));
7911     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7912                                             Spacing * 2));
7913     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7914     TmpInst.addOperand(Inst.getOperand(4));
7915     Inst = TmpInst;
7916     return true;
7917   }
7918 
7919   case ARM::VST3dWB_fixed_Asm_8:
7920   case ARM::VST3dWB_fixed_Asm_16:
7921   case ARM::VST3dWB_fixed_Asm_32:
7922   case ARM::VST3qWB_fixed_Asm_8:
7923   case ARM::VST3qWB_fixed_Asm_16:
7924   case ARM::VST3qWB_fixed_Asm_32: {
7925     MCInst TmpInst;
7926     unsigned Spacing;
7927     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7928     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7929     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7930     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7931     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7932     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7933     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7934                                             Spacing));
7935     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7936                                             Spacing * 2));
7937     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7938     TmpInst.addOperand(Inst.getOperand(4));
7939     Inst = TmpInst;
7940     return true;
7941   }
7942 
7943   case ARM::VST3dWB_register_Asm_8:
7944   case ARM::VST3dWB_register_Asm_16:
7945   case ARM::VST3dWB_register_Asm_32:
7946   case ARM::VST3qWB_register_Asm_8:
7947   case ARM::VST3qWB_register_Asm_16:
7948   case ARM::VST3qWB_register_Asm_32: {
7949     MCInst TmpInst;
7950     unsigned Spacing;
7951     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7952     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7953     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7954     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7955     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7956     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7957     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7958                                             Spacing));
7959     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7960                                             Spacing * 2));
7961     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7962     TmpInst.addOperand(Inst.getOperand(5));
7963     Inst = TmpInst;
7964     return true;
7965   }
7966 
7967   // VST4 multiple 3-element structure instructions.
7968   case ARM::VST4dAsm_8:
7969   case ARM::VST4dAsm_16:
7970   case ARM::VST4dAsm_32:
7971   case ARM::VST4qAsm_8:
7972   case ARM::VST4qAsm_16:
7973   case ARM::VST4qAsm_32: {
7974     MCInst TmpInst;
7975     unsigned Spacing;
7976     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7977     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7978     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7979     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7980     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7981                                             Spacing));
7982     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7983                                             Spacing * 2));
7984     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7985                                             Spacing * 3));
7986     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7987     TmpInst.addOperand(Inst.getOperand(4));
7988     Inst = TmpInst;
7989     return true;
7990   }
7991 
7992   case ARM::VST4dWB_fixed_Asm_8:
7993   case ARM::VST4dWB_fixed_Asm_16:
7994   case ARM::VST4dWB_fixed_Asm_32:
7995   case ARM::VST4qWB_fixed_Asm_8:
7996   case ARM::VST4qWB_fixed_Asm_16:
7997   case ARM::VST4qWB_fixed_Asm_32: {
7998     MCInst TmpInst;
7999     unsigned Spacing;
8000     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8001     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8002     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8003     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8004     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
8005     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8006     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8007                                             Spacing));
8008     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8009                                             Spacing * 2));
8010     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8011                                             Spacing * 3));
8012     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8013     TmpInst.addOperand(Inst.getOperand(4));
8014     Inst = TmpInst;
8015     return true;
8016   }
8017 
8018   case ARM::VST4dWB_register_Asm_8:
8019   case ARM::VST4dWB_register_Asm_16:
8020   case ARM::VST4dWB_register_Asm_32:
8021   case ARM::VST4qWB_register_Asm_8:
8022   case ARM::VST4qWB_register_Asm_16:
8023   case ARM::VST4qWB_register_Asm_32: {
8024     MCInst TmpInst;
8025     unsigned Spacing;
8026     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8027     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8028     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8029     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8030     TmpInst.addOperand(Inst.getOperand(3)); // Rm
8031     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8032     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8033                                             Spacing));
8034     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8035                                             Spacing * 2));
8036     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8037                                             Spacing * 3));
8038     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
8039     TmpInst.addOperand(Inst.getOperand(5));
8040     Inst = TmpInst;
8041     return true;
8042   }
8043 
8044   // Handle encoding choice for the shift-immediate instructions.
8045   case ARM::t2LSLri:
8046   case ARM::t2LSRri:
8047   case ARM::t2ASRri: {
8048     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8049         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
8050         Inst.getOperand(5).getReg() == (inITBlock() ? 0 : ARM::CPSR) &&
8051         !(static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8052           static_cast<ARMOperand &>(*Operands[3]).getToken() == ".w")) {
8053       unsigned NewOpc;
8054       switch (Inst.getOpcode()) {
8055       default: llvm_unreachable("unexpected opcode");
8056       case ARM::t2LSLri: NewOpc = ARM::tLSLri; break;
8057       case ARM::t2LSRri: NewOpc = ARM::tLSRri; break;
8058       case ARM::t2ASRri: NewOpc = ARM::tASRri; break;
8059       }
8060       // The Thumb1 operands aren't in the same order. Awesome, eh?
8061       MCInst TmpInst;
8062       TmpInst.setOpcode(NewOpc);
8063       TmpInst.addOperand(Inst.getOperand(0));
8064       TmpInst.addOperand(Inst.getOperand(5));
8065       TmpInst.addOperand(Inst.getOperand(1));
8066       TmpInst.addOperand(Inst.getOperand(2));
8067       TmpInst.addOperand(Inst.getOperand(3));
8068       TmpInst.addOperand(Inst.getOperand(4));
8069       Inst = TmpInst;
8070       return true;
8071     }
8072     return false;
8073   }
8074 
8075   // Handle the Thumb2 mode MOV complex aliases.
8076   case ARM::t2MOVsr:
8077   case ARM::t2MOVSsr: {
8078     // Which instruction to expand to depends on the CCOut operand and
8079     // whether we're in an IT block if the register operands are low
8080     // registers.
8081     bool isNarrow = false;
8082     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8083         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8084         isARMLowRegister(Inst.getOperand(2).getReg()) &&
8085         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
8086         inITBlock() == (Inst.getOpcode() == ARM::t2MOVsr))
8087       isNarrow = true;
8088     MCInst TmpInst;
8089     unsigned newOpc;
8090     switch(ARM_AM::getSORegShOp(Inst.getOperand(3).getImm())) {
8091     default: llvm_unreachable("unexpected opcode!");
8092     case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRrr : ARM::t2ASRrr; break;
8093     case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRrr : ARM::t2LSRrr; break;
8094     case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLrr : ARM::t2LSLrr; break;
8095     case ARM_AM::ror: newOpc = isNarrow ? ARM::tROR   : ARM::t2RORrr; break;
8096     }
8097     TmpInst.setOpcode(newOpc);
8098     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8099     if (isNarrow)
8100       TmpInst.addOperand(MCOperand::createReg(
8101           Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
8102     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8103     TmpInst.addOperand(Inst.getOperand(2)); // Rm
8104     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
8105     TmpInst.addOperand(Inst.getOperand(5));
8106     if (!isNarrow)
8107       TmpInst.addOperand(MCOperand::createReg(
8108           Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
8109     Inst = TmpInst;
8110     return true;
8111   }
8112   case ARM::t2MOVsi:
8113   case ARM::t2MOVSsi: {
8114     // Which instruction to expand to depends on the CCOut operand and
8115     // whether we're in an IT block if the register operands are low
8116     // registers.
8117     bool isNarrow = false;
8118     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8119         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8120         inITBlock() == (Inst.getOpcode() == ARM::t2MOVsi))
8121       isNarrow = true;
8122     MCInst TmpInst;
8123     unsigned newOpc;
8124     switch(ARM_AM::getSORegShOp(Inst.getOperand(2).getImm())) {
8125     default: llvm_unreachable("unexpected opcode!");
8126     case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRri : ARM::t2ASRri; break;
8127     case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRri : ARM::t2LSRri; break;
8128     case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLri : ARM::t2LSLri; break;
8129     case ARM_AM::ror: newOpc = ARM::t2RORri; isNarrow = false; break;
8130     case ARM_AM::rrx: isNarrow = false; newOpc = ARM::t2RRX; break;
8131     }
8132     unsigned Amount = ARM_AM::getSORegOffset(Inst.getOperand(2).getImm());
8133     if (Amount == 32) Amount = 0;
8134     TmpInst.setOpcode(newOpc);
8135     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8136     if (isNarrow)
8137       TmpInst.addOperand(MCOperand::createReg(
8138           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
8139     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8140     if (newOpc != ARM::t2RRX)
8141       TmpInst.addOperand(MCOperand::createImm(Amount));
8142     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8143     TmpInst.addOperand(Inst.getOperand(4));
8144     if (!isNarrow)
8145       TmpInst.addOperand(MCOperand::createReg(
8146           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
8147     Inst = TmpInst;
8148     return true;
8149   }
8150   // Handle the ARM mode MOV complex aliases.
8151   case ARM::ASRr:
8152   case ARM::LSRr:
8153   case ARM::LSLr:
8154   case ARM::RORr: {
8155     ARM_AM::ShiftOpc ShiftTy;
8156     switch(Inst.getOpcode()) {
8157     default: llvm_unreachable("unexpected opcode!");
8158     case ARM::ASRr: ShiftTy = ARM_AM::asr; break;
8159     case ARM::LSRr: ShiftTy = ARM_AM::lsr; break;
8160     case ARM::LSLr: ShiftTy = ARM_AM::lsl; break;
8161     case ARM::RORr: ShiftTy = ARM_AM::ror; break;
8162     }
8163     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, 0);
8164     MCInst TmpInst;
8165     TmpInst.setOpcode(ARM::MOVsr);
8166     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8167     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8168     TmpInst.addOperand(Inst.getOperand(2)); // Rm
8169     TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8170     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8171     TmpInst.addOperand(Inst.getOperand(4));
8172     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
8173     Inst = TmpInst;
8174     return true;
8175   }
8176   case ARM::ASRi:
8177   case ARM::LSRi:
8178   case ARM::LSLi:
8179   case ARM::RORi: {
8180     ARM_AM::ShiftOpc ShiftTy;
8181     switch(Inst.getOpcode()) {
8182     default: llvm_unreachable("unexpected opcode!");
8183     case ARM::ASRi: ShiftTy = ARM_AM::asr; break;
8184     case ARM::LSRi: ShiftTy = ARM_AM::lsr; break;
8185     case ARM::LSLi: ShiftTy = ARM_AM::lsl; break;
8186     case ARM::RORi: ShiftTy = ARM_AM::ror; break;
8187     }
8188     // A shift by zero is a plain MOVr, not a MOVsi.
8189     unsigned Amt = Inst.getOperand(2).getImm();
8190     unsigned Opc = Amt == 0 ? ARM::MOVr : ARM::MOVsi;
8191     // A shift by 32 should be encoded as 0 when permitted
8192     if (Amt == 32 && (ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr))
8193       Amt = 0;
8194     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, Amt);
8195     MCInst TmpInst;
8196     TmpInst.setOpcode(Opc);
8197     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8198     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8199     if (Opc == ARM::MOVsi)
8200       TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8201     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8202     TmpInst.addOperand(Inst.getOperand(4));
8203     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
8204     Inst = TmpInst;
8205     return true;
8206   }
8207   case ARM::RRXi: {
8208     unsigned Shifter = ARM_AM::getSORegOpc(ARM_AM::rrx, 0);
8209     MCInst TmpInst;
8210     TmpInst.setOpcode(ARM::MOVsi);
8211     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8212     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8213     TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8214     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8215     TmpInst.addOperand(Inst.getOperand(3));
8216     TmpInst.addOperand(Inst.getOperand(4)); // cc_out
8217     Inst = TmpInst;
8218     return true;
8219   }
8220   case ARM::t2LDMIA_UPD: {
8221     // If this is a load of a single register, then we should use
8222     // a post-indexed LDR instruction instead, per the ARM ARM.
8223     if (Inst.getNumOperands() != 5)
8224       return false;
8225     MCInst TmpInst;
8226     TmpInst.setOpcode(ARM::t2LDR_POST);
8227     TmpInst.addOperand(Inst.getOperand(4)); // Rt
8228     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8229     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8230     TmpInst.addOperand(MCOperand::createImm(4));
8231     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8232     TmpInst.addOperand(Inst.getOperand(3));
8233     Inst = TmpInst;
8234     return true;
8235   }
8236   case ARM::t2STMDB_UPD: {
8237     // If this is a store of a single register, then we should use
8238     // a pre-indexed STR instruction instead, per the ARM ARM.
8239     if (Inst.getNumOperands() != 5)
8240       return false;
8241     MCInst TmpInst;
8242     TmpInst.setOpcode(ARM::t2STR_PRE);
8243     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8244     TmpInst.addOperand(Inst.getOperand(4)); // Rt
8245     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8246     TmpInst.addOperand(MCOperand::createImm(-4));
8247     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8248     TmpInst.addOperand(Inst.getOperand(3));
8249     Inst = TmpInst;
8250     return true;
8251   }
8252   case ARM::LDMIA_UPD:
8253     // If this is a load of a single register via a 'pop', then we should use
8254     // a post-indexed LDR instruction instead, per the ARM ARM.
8255     if (static_cast<ARMOperand &>(*Operands[0]).getToken() == "pop" &&
8256         Inst.getNumOperands() == 5) {
8257       MCInst TmpInst;
8258       TmpInst.setOpcode(ARM::LDR_POST_IMM);
8259       TmpInst.addOperand(Inst.getOperand(4)); // Rt
8260       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8261       TmpInst.addOperand(Inst.getOperand(1)); // Rn
8262       TmpInst.addOperand(MCOperand::createReg(0));  // am2offset
8263       TmpInst.addOperand(MCOperand::createImm(4));
8264       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8265       TmpInst.addOperand(Inst.getOperand(3));
8266       Inst = TmpInst;
8267       return true;
8268     }
8269     break;
8270   case ARM::STMDB_UPD:
8271     // If this is a store of a single register via a 'push', then we should use
8272     // a pre-indexed STR instruction instead, per the ARM ARM.
8273     if (static_cast<ARMOperand &>(*Operands[0]).getToken() == "push" &&
8274         Inst.getNumOperands() == 5) {
8275       MCInst TmpInst;
8276       TmpInst.setOpcode(ARM::STR_PRE_IMM);
8277       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8278       TmpInst.addOperand(Inst.getOperand(4)); // Rt
8279       TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
8280       TmpInst.addOperand(MCOperand::createImm(-4));
8281       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8282       TmpInst.addOperand(Inst.getOperand(3));
8283       Inst = TmpInst;
8284     }
8285     break;
8286   case ARM::t2ADDri12:
8287     // If the immediate fits for encoding T3 (t2ADDri) and the generic "add"
8288     // mnemonic was used (not "addw"), encoding T3 is preferred.
8289     if (static_cast<ARMOperand &>(*Operands[0]).getToken() != "add" ||
8290         ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
8291       break;
8292     Inst.setOpcode(ARM::t2ADDri);
8293     Inst.addOperand(MCOperand::createReg(0)); // cc_out
8294     break;
8295   case ARM::t2SUBri12:
8296     // If the immediate fits for encoding T3 (t2SUBri) and the generic "sub"
8297     // mnemonic was used (not "subw"), encoding T3 is preferred.
8298     if (static_cast<ARMOperand &>(*Operands[0]).getToken() != "sub" ||
8299         ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
8300       break;
8301     Inst.setOpcode(ARM::t2SUBri);
8302     Inst.addOperand(MCOperand::createReg(0)); // cc_out
8303     break;
8304   case ARM::tADDi8:
8305     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
8306     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
8307     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
8308     // to encoding T1 if <Rd> is omitted."
8309     if ((unsigned)Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
8310       Inst.setOpcode(ARM::tADDi3);
8311       return true;
8312     }
8313     break;
8314   case ARM::tSUBi8:
8315     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
8316     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
8317     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
8318     // to encoding T1 if <Rd> is omitted."
8319     if ((unsigned)Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
8320       Inst.setOpcode(ARM::tSUBi3);
8321       return true;
8322     }
8323     break;
8324   case ARM::t2ADDri:
8325   case ARM::t2SUBri: {
8326     // If the destination and first source operand are the same, and
8327     // the flags are compatible with the current IT status, use encoding T2
8328     // instead of T3. For compatibility with the system 'as'. Make sure the
8329     // wide encoding wasn't explicit.
8330     if (Inst.getOperand(0).getReg() != Inst.getOperand(1).getReg() ||
8331         !isARMLowRegister(Inst.getOperand(0).getReg()) ||
8332         (unsigned)Inst.getOperand(2).getImm() > 255 ||
8333         ((!inITBlock() && Inst.getOperand(5).getReg() != ARM::CPSR) ||
8334          (inITBlock() && Inst.getOperand(5).getReg() != 0)) ||
8335         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8336          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".w"))
8337       break;
8338     MCInst TmpInst;
8339     TmpInst.setOpcode(Inst.getOpcode() == ARM::t2ADDri ?
8340                       ARM::tADDi8 : ARM::tSUBi8);
8341     TmpInst.addOperand(Inst.getOperand(0));
8342     TmpInst.addOperand(Inst.getOperand(5));
8343     TmpInst.addOperand(Inst.getOperand(0));
8344     TmpInst.addOperand(Inst.getOperand(2));
8345     TmpInst.addOperand(Inst.getOperand(3));
8346     TmpInst.addOperand(Inst.getOperand(4));
8347     Inst = TmpInst;
8348     return true;
8349   }
8350   case ARM::t2ADDrr: {
8351     // If the destination and first source operand are the same, and
8352     // there's no setting of the flags, use encoding T2 instead of T3.
8353     // Note that this is only for ADD, not SUB. This mirrors the system
8354     // 'as' behaviour.  Also take advantage of ADD being commutative.
8355     // Make sure the wide encoding wasn't explicit.
8356     bool Swap = false;
8357     auto DestReg = Inst.getOperand(0).getReg();
8358     bool Transform = DestReg == Inst.getOperand(1).getReg();
8359     if (!Transform && DestReg == Inst.getOperand(2).getReg()) {
8360       Transform = true;
8361       Swap = true;
8362     }
8363     if (!Transform ||
8364         Inst.getOperand(5).getReg() != 0 ||
8365         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8366          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".w"))
8367       break;
8368     MCInst TmpInst;
8369     TmpInst.setOpcode(ARM::tADDhirr);
8370     TmpInst.addOperand(Inst.getOperand(0));
8371     TmpInst.addOperand(Inst.getOperand(0));
8372     TmpInst.addOperand(Inst.getOperand(Swap ? 1 : 2));
8373     TmpInst.addOperand(Inst.getOperand(3));
8374     TmpInst.addOperand(Inst.getOperand(4));
8375     Inst = TmpInst;
8376     return true;
8377   }
8378   case ARM::tADDrSP: {
8379     // If the non-SP source operand and the destination operand are not the
8380     // same, we need to use the 32-bit encoding if it's available.
8381     if (Inst.getOperand(0).getReg() != Inst.getOperand(2).getReg()) {
8382       Inst.setOpcode(ARM::t2ADDrr);
8383       Inst.addOperand(MCOperand::createReg(0)); // cc_out
8384       return true;
8385     }
8386     break;
8387   }
8388   case ARM::tB:
8389     // A Thumb conditional branch outside of an IT block is a tBcc.
8390     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()) {
8391       Inst.setOpcode(ARM::tBcc);
8392       return true;
8393     }
8394     break;
8395   case ARM::t2B:
8396     // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
8397     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()){
8398       Inst.setOpcode(ARM::t2Bcc);
8399       return true;
8400     }
8401     break;
8402   case ARM::t2Bcc:
8403     // If the conditional is AL or we're in an IT block, we really want t2B.
8404     if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock()) {
8405       Inst.setOpcode(ARM::t2B);
8406       return true;
8407     }
8408     break;
8409   case ARM::tBcc:
8410     // If the conditional is AL, we really want tB.
8411     if (Inst.getOperand(1).getImm() == ARMCC::AL) {
8412       Inst.setOpcode(ARM::tB);
8413       return true;
8414     }
8415     break;
8416   case ARM::tLDMIA: {
8417     // If the register list contains any high registers, or if the writeback
8418     // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
8419     // instead if we're in Thumb2. Otherwise, this should have generated
8420     // an error in validateInstruction().
8421     unsigned Rn = Inst.getOperand(0).getReg();
8422     bool hasWritebackToken =
8423         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8424          static_cast<ARMOperand &>(*Operands[3]).getToken() == "!");
8425     bool listContainsBase;
8426     if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
8427         (!listContainsBase && !hasWritebackToken) ||
8428         (listContainsBase && hasWritebackToken)) {
8429       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
8430       assert (isThumbTwo());
8431       Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
8432       // If we're switching to the updating version, we need to insert
8433       // the writeback tied operand.
8434       if (hasWritebackToken)
8435         Inst.insert(Inst.begin(),
8436                     MCOperand::createReg(Inst.getOperand(0).getReg()));
8437       return true;
8438     }
8439     break;
8440   }
8441   case ARM::tSTMIA_UPD: {
8442     // If the register list contains any high registers, we need to use
8443     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
8444     // should have generated an error in validateInstruction().
8445     unsigned Rn = Inst.getOperand(0).getReg();
8446     bool listContainsBase;
8447     if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
8448       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
8449       assert (isThumbTwo());
8450       Inst.setOpcode(ARM::t2STMIA_UPD);
8451       return true;
8452     }
8453     break;
8454   }
8455   case ARM::tPOP: {
8456     bool listContainsBase;
8457     // If the register list contains any high registers, we need to use
8458     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
8459     // should have generated an error in validateInstruction().
8460     if (!checkLowRegisterList(Inst, 2, 0, ARM::PC, listContainsBase))
8461       return false;
8462     assert (isThumbTwo());
8463     Inst.setOpcode(ARM::t2LDMIA_UPD);
8464     // Add the base register and writeback operands.
8465     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8466     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8467     return true;
8468   }
8469   case ARM::tPUSH: {
8470     bool listContainsBase;
8471     if (!checkLowRegisterList(Inst, 2, 0, ARM::LR, listContainsBase))
8472       return false;
8473     assert (isThumbTwo());
8474     Inst.setOpcode(ARM::t2STMDB_UPD);
8475     // Add the base register and writeback operands.
8476     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8477     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8478     return true;
8479   }
8480   case ARM::t2MOVi: {
8481     // If we can use the 16-bit encoding and the user didn't explicitly
8482     // request the 32-bit variant, transform it here.
8483     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8484         (unsigned)Inst.getOperand(1).getImm() <= 255 &&
8485         ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
8486           Inst.getOperand(4).getReg() == ARM::CPSR) ||
8487          (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
8488         (!static_cast<ARMOperand &>(*Operands[2]).isToken() ||
8489          static_cast<ARMOperand &>(*Operands[2]).getToken() != ".w")) {
8490       // The operands aren't in the same order for tMOVi8...
8491       MCInst TmpInst;
8492       TmpInst.setOpcode(ARM::tMOVi8);
8493       TmpInst.addOperand(Inst.getOperand(0));
8494       TmpInst.addOperand(Inst.getOperand(4));
8495       TmpInst.addOperand(Inst.getOperand(1));
8496       TmpInst.addOperand(Inst.getOperand(2));
8497       TmpInst.addOperand(Inst.getOperand(3));
8498       Inst = TmpInst;
8499       return true;
8500     }
8501     break;
8502   }
8503   case ARM::t2MOVr: {
8504     // If we can use the 16-bit encoding and the user didn't explicitly
8505     // request the 32-bit variant, transform it here.
8506     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8507         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8508         Inst.getOperand(2).getImm() == ARMCC::AL &&
8509         Inst.getOperand(4).getReg() == ARM::CPSR &&
8510         (!static_cast<ARMOperand &>(*Operands[2]).isToken() ||
8511          static_cast<ARMOperand &>(*Operands[2]).getToken() != ".w")) {
8512       // The operands aren't the same for tMOV[S]r... (no cc_out)
8513       MCInst TmpInst;
8514       TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
8515       TmpInst.addOperand(Inst.getOperand(0));
8516       TmpInst.addOperand(Inst.getOperand(1));
8517       TmpInst.addOperand(Inst.getOperand(2));
8518       TmpInst.addOperand(Inst.getOperand(3));
8519       Inst = TmpInst;
8520       return true;
8521     }
8522     break;
8523   }
8524   case ARM::t2SXTH:
8525   case ARM::t2SXTB:
8526   case ARM::t2UXTH:
8527   case ARM::t2UXTB: {
8528     // If we can use the 16-bit encoding and the user didn't explicitly
8529     // request the 32-bit variant, transform it here.
8530     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8531         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8532         Inst.getOperand(2).getImm() == 0 &&
8533         (!static_cast<ARMOperand &>(*Operands[2]).isToken() ||
8534          static_cast<ARMOperand &>(*Operands[2]).getToken() != ".w")) {
8535       unsigned NewOpc;
8536       switch (Inst.getOpcode()) {
8537       default: llvm_unreachable("Illegal opcode!");
8538       case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
8539       case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
8540       case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
8541       case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
8542       }
8543       // The operands aren't the same for thumb1 (no rotate operand).
8544       MCInst TmpInst;
8545       TmpInst.setOpcode(NewOpc);
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       Inst = TmpInst;
8551       return true;
8552     }
8553     break;
8554   }
8555   case ARM::MOVsi: {
8556     ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(2).getImm());
8557     // rrx shifts and asr/lsr of #32 is encoded as 0
8558     if (SOpc == ARM_AM::rrx || SOpc == ARM_AM::asr || SOpc == ARM_AM::lsr)
8559       return false;
8560     if (ARM_AM::getSORegOffset(Inst.getOperand(2).getImm()) == 0) {
8561       // Shifting by zero is accepted as a vanilla 'MOVr'
8562       MCInst TmpInst;
8563       TmpInst.setOpcode(ARM::MOVr);
8564       TmpInst.addOperand(Inst.getOperand(0));
8565       TmpInst.addOperand(Inst.getOperand(1));
8566       TmpInst.addOperand(Inst.getOperand(3));
8567       TmpInst.addOperand(Inst.getOperand(4));
8568       TmpInst.addOperand(Inst.getOperand(5));
8569       Inst = TmpInst;
8570       return true;
8571     }
8572     return false;
8573   }
8574   case ARM::ANDrsi:
8575   case ARM::ORRrsi:
8576   case ARM::EORrsi:
8577   case ARM::BICrsi:
8578   case ARM::SUBrsi:
8579   case ARM::ADDrsi: {
8580     unsigned newOpc;
8581     ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(3).getImm());
8582     if (SOpc == ARM_AM::rrx) return false;
8583     switch (Inst.getOpcode()) {
8584     default: llvm_unreachable("unexpected opcode!");
8585     case ARM::ANDrsi: newOpc = ARM::ANDrr; break;
8586     case ARM::ORRrsi: newOpc = ARM::ORRrr; break;
8587     case ARM::EORrsi: newOpc = ARM::EORrr; break;
8588     case ARM::BICrsi: newOpc = ARM::BICrr; break;
8589     case ARM::SUBrsi: newOpc = ARM::SUBrr; break;
8590     case ARM::ADDrsi: newOpc = ARM::ADDrr; break;
8591     }
8592     // If the shift is by zero, use the non-shifted instruction definition.
8593     // The exception is for right shifts, where 0 == 32
8594     if (ARM_AM::getSORegOffset(Inst.getOperand(3).getImm()) == 0 &&
8595         !(SOpc == ARM_AM::lsr || SOpc == ARM_AM::asr)) {
8596       MCInst TmpInst;
8597       TmpInst.setOpcode(newOpc);
8598       TmpInst.addOperand(Inst.getOperand(0));
8599       TmpInst.addOperand(Inst.getOperand(1));
8600       TmpInst.addOperand(Inst.getOperand(2));
8601       TmpInst.addOperand(Inst.getOperand(4));
8602       TmpInst.addOperand(Inst.getOperand(5));
8603       TmpInst.addOperand(Inst.getOperand(6));
8604       Inst = TmpInst;
8605       return true;
8606     }
8607     return false;
8608   }
8609   case ARM::ITasm:
8610   case ARM::t2IT: {
8611     // The mask bits for all but the first condition are represented as
8612     // the low bit of the condition code value implies 't'. We currently
8613     // always have 1 implies 't', so XOR toggle the bits if the low bit
8614     // of the condition code is zero.
8615     MCOperand &MO = Inst.getOperand(1);
8616     unsigned Mask = MO.getImm();
8617     unsigned OrigMask = Mask;
8618     unsigned TZ = countTrailingZeros(Mask);
8619     if ((Inst.getOperand(0).getImm() & 1) == 0) {
8620       assert(Mask && TZ <= 3 && "illegal IT mask value!");
8621       Mask ^= (0xE << TZ) & 0xF;
8622     }
8623     MO.setImm(Mask);
8624 
8625     // Set up the IT block state according to the IT instruction we just
8626     // matched.
8627     assert(!inITBlock() && "nested IT blocks?!");
8628     ITState.Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
8629     ITState.Mask = OrigMask; // Use the original mask, not the updated one.
8630     ITState.CurPosition = 0;
8631     ITState.FirstCond = true;
8632     break;
8633   }
8634   case ARM::t2LSLrr:
8635   case ARM::t2LSRrr:
8636   case ARM::t2ASRrr:
8637   case ARM::t2SBCrr:
8638   case ARM::t2RORrr:
8639   case ARM::t2BICrr:
8640   {
8641     // Assemblers should use the narrow encodings of these instructions when permissible.
8642     if ((isARMLowRegister(Inst.getOperand(1).getReg()) &&
8643          isARMLowRegister(Inst.getOperand(2).getReg())) &&
8644         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
8645         ((!inITBlock() && Inst.getOperand(5).getReg() == ARM::CPSR) ||
8646          (inITBlock() && Inst.getOperand(5).getReg() != ARM::CPSR)) &&
8647         (!static_cast<ARMOperand &>(*Operands[3]).isToken() ||
8648          !static_cast<ARMOperand &>(*Operands[3]).getToken().equals_lower(
8649              ".w"))) {
8650       unsigned NewOpc;
8651       switch (Inst.getOpcode()) {
8652         default: llvm_unreachable("unexpected opcode");
8653         case ARM::t2LSLrr: NewOpc = ARM::tLSLrr; break;
8654         case ARM::t2LSRrr: NewOpc = ARM::tLSRrr; break;
8655         case ARM::t2ASRrr: NewOpc = ARM::tASRrr; break;
8656         case ARM::t2SBCrr: NewOpc = ARM::tSBC; break;
8657         case ARM::t2RORrr: NewOpc = ARM::tROR; break;
8658         case ARM::t2BICrr: NewOpc = ARM::tBIC; break;
8659       }
8660       MCInst TmpInst;
8661       TmpInst.setOpcode(NewOpc);
8662       TmpInst.addOperand(Inst.getOperand(0));
8663       TmpInst.addOperand(Inst.getOperand(5));
8664       TmpInst.addOperand(Inst.getOperand(1));
8665       TmpInst.addOperand(Inst.getOperand(2));
8666       TmpInst.addOperand(Inst.getOperand(3));
8667       TmpInst.addOperand(Inst.getOperand(4));
8668       Inst = TmpInst;
8669       return true;
8670     }
8671     return false;
8672   }
8673   case ARM::t2ANDrr:
8674   case ARM::t2EORrr:
8675   case ARM::t2ADCrr:
8676   case ARM::t2ORRrr:
8677   {
8678     // Assemblers should use the narrow encodings of these instructions when permissible.
8679     // These instructions are special in that they are commutable, so shorter encodings
8680     // are available more often.
8681     if ((isARMLowRegister(Inst.getOperand(1).getReg()) &&
8682          isARMLowRegister(Inst.getOperand(2).getReg())) &&
8683         (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() ||
8684          Inst.getOperand(0).getReg() == Inst.getOperand(2).getReg()) &&
8685         ((!inITBlock() && Inst.getOperand(5).getReg() == ARM::CPSR) ||
8686          (inITBlock() && Inst.getOperand(5).getReg() != ARM::CPSR)) &&
8687         (!static_cast<ARMOperand &>(*Operands[3]).isToken() ||
8688          !static_cast<ARMOperand &>(*Operands[3]).getToken().equals_lower(
8689              ".w"))) {
8690       unsigned NewOpc;
8691       switch (Inst.getOpcode()) {
8692         default: llvm_unreachable("unexpected opcode");
8693         case ARM::t2ADCrr: NewOpc = ARM::tADC; break;
8694         case ARM::t2ANDrr: NewOpc = ARM::tAND; break;
8695         case ARM::t2EORrr: NewOpc = ARM::tEOR; break;
8696         case ARM::t2ORRrr: NewOpc = ARM::tORR; break;
8697       }
8698       MCInst TmpInst;
8699       TmpInst.setOpcode(NewOpc);
8700       TmpInst.addOperand(Inst.getOperand(0));
8701       TmpInst.addOperand(Inst.getOperand(5));
8702       if (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()) {
8703         TmpInst.addOperand(Inst.getOperand(1));
8704         TmpInst.addOperand(Inst.getOperand(2));
8705       } else {
8706         TmpInst.addOperand(Inst.getOperand(2));
8707         TmpInst.addOperand(Inst.getOperand(1));
8708       }
8709       TmpInst.addOperand(Inst.getOperand(3));
8710       TmpInst.addOperand(Inst.getOperand(4));
8711       Inst = TmpInst;
8712       return true;
8713     }
8714     return false;
8715   }
8716   }
8717   return false;
8718 }
8719 
8720 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
8721   // 16-bit thumb arithmetic instructions either require or preclude the 'S'
8722   // suffix depending on whether they're in an IT block or not.
8723   unsigned Opc = Inst.getOpcode();
8724   const MCInstrDesc &MCID = MII.get(Opc);
8725   if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
8726     assert(MCID.hasOptionalDef() &&
8727            "optionally flag setting instruction missing optional def operand");
8728     assert(MCID.NumOperands == Inst.getNumOperands() &&
8729            "operand count mismatch!");
8730     // Find the optional-def operand (cc_out).
8731     unsigned OpNo;
8732     for (OpNo = 0;
8733          !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
8734          ++OpNo)
8735       ;
8736     // If we're parsing Thumb1, reject it completely.
8737     if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
8738       return Match_MnemonicFail;
8739     // If we're parsing Thumb2, which form is legal depends on whether we're
8740     // in an IT block.
8741     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
8742         !inITBlock())
8743       return Match_RequiresITBlock;
8744     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
8745         inITBlock())
8746       return Match_RequiresNotITBlock;
8747   } else if (isThumbOne()) {
8748     // Some high-register supporting Thumb1 encodings only allow both registers
8749     // to be from r0-r7 when in Thumb2.
8750     if (Opc == ARM::tADDhirr && !hasV6MOps() &&
8751         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8752         isARMLowRegister(Inst.getOperand(2).getReg()))
8753       return Match_RequiresThumb2;
8754     // Others only require ARMv6 or later.
8755     else if (Opc == ARM::tMOVr && !hasV6Ops() &&
8756              isARMLowRegister(Inst.getOperand(0).getReg()) &&
8757              isARMLowRegister(Inst.getOperand(1).getReg()))
8758       return Match_RequiresV6;
8759   }
8760 
8761   for (unsigned I = 0; I < MCID.NumOperands; ++I)
8762     if (MCID.OpInfo[I].RegClass == ARM::rGPRRegClassID) {
8763       // rGPRRegClass excludes PC, and also excluded SP before ARMv8
8764       if ((Inst.getOperand(I).getReg() == ARM::SP) && !hasV8Ops())
8765         return Match_RequiresV8;
8766       else if (Inst.getOperand(I).getReg() == ARM::PC)
8767         return Match_InvalidOperand;
8768     }
8769 
8770   return Match_Success;
8771 }
8772 
8773 namespace llvm {
8774 template <> inline bool IsCPSRDead<MCInst>(MCInst *Instr) {
8775   return true; // In an assembly source, no need to second-guess
8776 }
8777 }
8778 
8779 static const char *getSubtargetFeatureName(uint64_t Val);
8780 bool ARMAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
8781                                            OperandVector &Operands,
8782                                            MCStreamer &Out, uint64_t &ErrorInfo,
8783                                            bool MatchingInlineAsm) {
8784   MCInst Inst;
8785   unsigned MatchResult;
8786 
8787   MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
8788                                      MatchingInlineAsm);
8789   switch (MatchResult) {
8790   case Match_Success:
8791     // Context sensitive operand constraints aren't handled by the matcher,
8792     // so check them here.
8793     if (validateInstruction(Inst, Operands)) {
8794       // Still progress the IT block, otherwise one wrong condition causes
8795       // nasty cascading errors.
8796       forwardITPosition();
8797       return true;
8798     }
8799 
8800     { // processInstruction() updates inITBlock state, we need to save it away
8801       bool wasInITBlock = inITBlock();
8802 
8803       // Some instructions need post-processing to, for example, tweak which
8804       // encoding is selected. Loop on it while changes happen so the
8805       // individual transformations can chain off each other. E.g.,
8806       // tPOP(r8)->t2LDMIA_UPD(sp,r8)->t2STR_POST(sp,r8)
8807       while (processInstruction(Inst, Operands, Out))
8808         ;
8809 
8810       // Only after the instruction is fully processed, we can validate it
8811       if (wasInITBlock && hasV8Ops() && isThumb() &&
8812           !isV8EligibleForIT(&Inst)) {
8813         Warning(IDLoc, "deprecated instruction in IT block");
8814       }
8815     }
8816 
8817     // Only move forward at the very end so that everything in validate
8818     // and process gets a consistent answer about whether we're in an IT
8819     // block.
8820     forwardITPosition();
8821 
8822     // ITasm is an ARM mode pseudo-instruction that just sets the ITblock and
8823     // doesn't actually encode.
8824     if (Inst.getOpcode() == ARM::ITasm)
8825       return false;
8826 
8827     Inst.setLoc(IDLoc);
8828     Out.EmitInstruction(Inst, getSTI());
8829     return false;
8830   case Match_MissingFeature: {
8831     assert(ErrorInfo && "Unknown missing feature!");
8832     // Special case the error message for the very common case where only
8833     // a single subtarget feature is missing (Thumb vs. ARM, e.g.).
8834     std::string Msg = "instruction requires:";
8835     uint64_t Mask = 1;
8836     for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
8837       if (ErrorInfo & Mask) {
8838         Msg += " ";
8839         Msg += getSubtargetFeatureName(ErrorInfo & Mask);
8840       }
8841       Mask <<= 1;
8842     }
8843     return Error(IDLoc, Msg);
8844   }
8845   case Match_InvalidOperand: {
8846     SMLoc ErrorLoc = IDLoc;
8847     if (ErrorInfo != ~0ULL) {
8848       if (ErrorInfo >= Operands.size())
8849         return Error(IDLoc, "too few operands for instruction");
8850 
8851       ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
8852       if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
8853     }
8854 
8855     return Error(ErrorLoc, "invalid operand for instruction");
8856   }
8857   case Match_MnemonicFail:
8858     return Error(IDLoc, "invalid instruction",
8859                  ((ARMOperand &)*Operands[0]).getLocRange());
8860   case Match_RequiresNotITBlock:
8861     return Error(IDLoc, "flag setting instruction only valid outside IT block");
8862   case Match_RequiresITBlock:
8863     return Error(IDLoc, "instruction only valid inside IT block");
8864   case Match_RequiresV6:
8865     return Error(IDLoc, "instruction variant requires ARMv6 or later");
8866   case Match_RequiresThumb2:
8867     return Error(IDLoc, "instruction variant requires Thumb2");
8868   case Match_RequiresV8:
8869     return Error(IDLoc, "instruction variant requires ARMv8 or later");
8870   case Match_ImmRange0_15: {
8871     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
8872     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
8873     return Error(ErrorLoc, "immediate operand must be in the range [0,15]");
8874   }
8875   case Match_ImmRange0_239: {
8876     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
8877     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
8878     return Error(ErrorLoc, "immediate operand must be in the range [0,239]");
8879   }
8880   case Match_AlignedMemoryRequiresNone:
8881   case Match_DupAlignedMemoryRequiresNone:
8882   case Match_AlignedMemoryRequires16:
8883   case Match_DupAlignedMemoryRequires16:
8884   case Match_AlignedMemoryRequires32:
8885   case Match_DupAlignedMemoryRequires32:
8886   case Match_AlignedMemoryRequires64:
8887   case Match_DupAlignedMemoryRequires64:
8888   case Match_AlignedMemoryRequires64or128:
8889   case Match_DupAlignedMemoryRequires64or128:
8890   case Match_AlignedMemoryRequires64or128or256:
8891   {
8892     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getAlignmentLoc();
8893     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
8894     switch (MatchResult) {
8895       default:
8896         llvm_unreachable("Missing Match_Aligned type");
8897       case Match_AlignedMemoryRequiresNone:
8898       case Match_DupAlignedMemoryRequiresNone:
8899         return Error(ErrorLoc, "alignment must be omitted");
8900       case Match_AlignedMemoryRequires16:
8901       case Match_DupAlignedMemoryRequires16:
8902         return Error(ErrorLoc, "alignment must be 16 or omitted");
8903       case Match_AlignedMemoryRequires32:
8904       case Match_DupAlignedMemoryRequires32:
8905         return Error(ErrorLoc, "alignment must be 32 or omitted");
8906       case Match_AlignedMemoryRequires64:
8907       case Match_DupAlignedMemoryRequires64:
8908         return Error(ErrorLoc, "alignment must be 64 or omitted");
8909       case Match_AlignedMemoryRequires64or128:
8910       case Match_DupAlignedMemoryRequires64or128:
8911         return Error(ErrorLoc, "alignment must be 64, 128 or omitted");
8912       case Match_AlignedMemoryRequires64or128or256:
8913         return Error(ErrorLoc, "alignment must be 64, 128, 256 or omitted");
8914     }
8915   }
8916   }
8917 
8918   llvm_unreachable("Implement any new match types added!");
8919 }
8920 
8921 /// parseDirective parses the arm specific directives
8922 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
8923   const MCObjectFileInfo::Environment Format =
8924     getContext().getObjectFileInfo()->getObjectFileType();
8925   bool IsMachO = Format == MCObjectFileInfo::IsMachO;
8926   bool IsCOFF = Format == MCObjectFileInfo::IsCOFF;
8927 
8928   StringRef IDVal = DirectiveID.getIdentifier();
8929   if (IDVal == ".word")
8930     return parseLiteralValues(4, DirectiveID.getLoc());
8931   else if (IDVal == ".short" || IDVal == ".hword")
8932     return parseLiteralValues(2, DirectiveID.getLoc());
8933   else if (IDVal == ".thumb")
8934     return parseDirectiveThumb(DirectiveID.getLoc());
8935   else if (IDVal == ".arm")
8936     return parseDirectiveARM(DirectiveID.getLoc());
8937   else if (IDVal == ".thumb_func")
8938     return parseDirectiveThumbFunc(DirectiveID.getLoc());
8939   else if (IDVal == ".code")
8940     return parseDirectiveCode(DirectiveID.getLoc());
8941   else if (IDVal == ".syntax")
8942     return parseDirectiveSyntax(DirectiveID.getLoc());
8943   else if (IDVal == ".unreq")
8944     return parseDirectiveUnreq(DirectiveID.getLoc());
8945   else if (IDVal == ".fnend")
8946     return parseDirectiveFnEnd(DirectiveID.getLoc());
8947   else if (IDVal == ".cantunwind")
8948     return parseDirectiveCantUnwind(DirectiveID.getLoc());
8949   else if (IDVal == ".personality")
8950     return parseDirectivePersonality(DirectiveID.getLoc());
8951   else if (IDVal == ".handlerdata")
8952     return parseDirectiveHandlerData(DirectiveID.getLoc());
8953   else if (IDVal == ".setfp")
8954     return parseDirectiveSetFP(DirectiveID.getLoc());
8955   else if (IDVal == ".pad")
8956     return parseDirectivePad(DirectiveID.getLoc());
8957   else if (IDVal == ".save")
8958     return parseDirectiveRegSave(DirectiveID.getLoc(), false);
8959   else if (IDVal == ".vsave")
8960     return parseDirectiveRegSave(DirectiveID.getLoc(), true);
8961   else if (IDVal == ".ltorg" || IDVal == ".pool")
8962     return parseDirectiveLtorg(DirectiveID.getLoc());
8963   else if (IDVal == ".even")
8964     return parseDirectiveEven(DirectiveID.getLoc());
8965   else if (IDVal == ".personalityindex")
8966     return parseDirectivePersonalityIndex(DirectiveID.getLoc());
8967   else if (IDVal == ".unwind_raw")
8968     return parseDirectiveUnwindRaw(DirectiveID.getLoc());
8969   else if (IDVal == ".movsp")
8970     return parseDirectiveMovSP(DirectiveID.getLoc());
8971   else if (IDVal == ".arch_extension")
8972     return parseDirectiveArchExtension(DirectiveID.getLoc());
8973   else if (IDVal == ".align")
8974     return parseDirectiveAlign(DirectiveID.getLoc());
8975   else if (IDVal == ".thumb_set")
8976     return parseDirectiveThumbSet(DirectiveID.getLoc());
8977 
8978   if (!IsMachO && !IsCOFF) {
8979     if (IDVal == ".arch")
8980       return parseDirectiveArch(DirectiveID.getLoc());
8981     else if (IDVal == ".cpu")
8982       return parseDirectiveCPU(DirectiveID.getLoc());
8983     else if (IDVal == ".eabi_attribute")
8984       return parseDirectiveEabiAttr(DirectiveID.getLoc());
8985     else if (IDVal == ".fpu")
8986       return parseDirectiveFPU(DirectiveID.getLoc());
8987     else if (IDVal == ".fnstart")
8988       return parseDirectiveFnStart(DirectiveID.getLoc());
8989     else if (IDVal == ".inst")
8990       return parseDirectiveInst(DirectiveID.getLoc());
8991     else if (IDVal == ".inst.n")
8992       return parseDirectiveInst(DirectiveID.getLoc(), 'n');
8993     else if (IDVal == ".inst.w")
8994       return parseDirectiveInst(DirectiveID.getLoc(), 'w');
8995     else if (IDVal == ".object_arch")
8996       return parseDirectiveObjectArch(DirectiveID.getLoc());
8997     else if (IDVal == ".tlsdescseq")
8998       return parseDirectiveTLSDescSeq(DirectiveID.getLoc());
8999   }
9000 
9001   return true;
9002 }
9003 
9004 /// parseLiteralValues
9005 ///  ::= .hword expression [, expression]*
9006 ///  ::= .short expression [, expression]*
9007 ///  ::= .word expression [, expression]*
9008 bool ARMAsmParser::parseLiteralValues(unsigned Size, SMLoc L) {
9009   MCAsmParser &Parser = getParser();
9010   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9011     for (;;) {
9012       const MCExpr *Value;
9013       if (getParser().parseExpression(Value)) {
9014         Parser.eatToEndOfStatement();
9015         return false;
9016       }
9017 
9018       getParser().getStreamer().EmitValue(Value, Size, L);
9019 
9020       if (getLexer().is(AsmToken::EndOfStatement))
9021         break;
9022 
9023       // FIXME: Improve diagnostic.
9024       if (getLexer().isNot(AsmToken::Comma)) {
9025         Error(L, "unexpected token in directive");
9026         return false;
9027       }
9028       Parser.Lex();
9029     }
9030   }
9031 
9032   Parser.Lex();
9033   return false;
9034 }
9035 
9036 /// parseDirectiveThumb
9037 ///  ::= .thumb
9038 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
9039   MCAsmParser &Parser = getParser();
9040   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9041     Error(L, "unexpected token in directive");
9042     return false;
9043   }
9044   Parser.Lex();
9045 
9046   if (!hasThumb()) {
9047     Error(L, "target does not support Thumb mode");
9048     return false;
9049   }
9050 
9051   if (!isThumb())
9052     SwitchMode();
9053 
9054   getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
9055   return false;
9056 }
9057 
9058 /// parseDirectiveARM
9059 ///  ::= .arm
9060 bool ARMAsmParser::parseDirectiveARM(SMLoc L) {
9061   MCAsmParser &Parser = getParser();
9062   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9063     Error(L, "unexpected token in directive");
9064     return false;
9065   }
9066   Parser.Lex();
9067 
9068   if (!hasARM()) {
9069     Error(L, "target does not support ARM mode");
9070     return false;
9071   }
9072 
9073   if (isThumb())
9074     SwitchMode();
9075 
9076   getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
9077   return false;
9078 }
9079 
9080 void ARMAsmParser::onLabelParsed(MCSymbol *Symbol) {
9081   if (NextSymbolIsThumb) {
9082     getParser().getStreamer().EmitThumbFunc(Symbol);
9083     NextSymbolIsThumb = false;
9084   }
9085 }
9086 
9087 /// parseDirectiveThumbFunc
9088 ///  ::= .thumbfunc symbol_name
9089 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
9090   MCAsmParser &Parser = getParser();
9091   const auto Format = getContext().getObjectFileInfo()->getObjectFileType();
9092   bool IsMachO = Format == MCObjectFileInfo::IsMachO;
9093 
9094   // Darwin asm has (optionally) function name after .thumb_func direction
9095   // ELF doesn't
9096   if (IsMachO) {
9097     const AsmToken &Tok = Parser.getTok();
9098     if (Tok.isNot(AsmToken::EndOfStatement)) {
9099       if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String)) {
9100         Error(L, "unexpected token in .thumb_func directive");
9101         return false;
9102       }
9103 
9104       MCSymbol *Func =
9105           getParser().getContext().getOrCreateSymbol(Tok.getIdentifier());
9106       getParser().getStreamer().EmitThumbFunc(Func);
9107       Parser.Lex(); // Consume the identifier token.
9108       return false;
9109     }
9110   }
9111 
9112   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9113     Error(Parser.getTok().getLoc(), "unexpected token in directive");
9114     Parser.eatToEndOfStatement();
9115     return false;
9116   }
9117 
9118   NextSymbolIsThumb = true;
9119   return false;
9120 }
9121 
9122 /// parseDirectiveSyntax
9123 ///  ::= .syntax unified | divided
9124 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
9125   MCAsmParser &Parser = getParser();
9126   const AsmToken &Tok = Parser.getTok();
9127   if (Tok.isNot(AsmToken::Identifier)) {
9128     Error(L, "unexpected token in .syntax directive");
9129     return false;
9130   }
9131 
9132   StringRef Mode = Tok.getString();
9133   if (Mode == "unified" || Mode == "UNIFIED") {
9134     Parser.Lex();
9135   } else if (Mode == "divided" || Mode == "DIVIDED") {
9136     Error(L, "'.syntax divided' arm asssembly not supported");
9137     return false;
9138   } else {
9139     Error(L, "unrecognized syntax mode in .syntax directive");
9140     return false;
9141   }
9142 
9143   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9144     Error(Parser.getTok().getLoc(), "unexpected token in directive");
9145     return false;
9146   }
9147   Parser.Lex();
9148 
9149   // TODO tell the MC streamer the mode
9150   // getParser().getStreamer().Emit???();
9151   return false;
9152 }
9153 
9154 /// parseDirectiveCode
9155 ///  ::= .code 16 | 32
9156 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
9157   MCAsmParser &Parser = getParser();
9158   const AsmToken &Tok = Parser.getTok();
9159   if (Tok.isNot(AsmToken::Integer)) {
9160     Error(L, "unexpected token in .code directive");
9161     return false;
9162   }
9163   int64_t Val = Parser.getTok().getIntVal();
9164   if (Val != 16 && Val != 32) {
9165     Error(L, "invalid operand to .code directive");
9166     return false;
9167   }
9168   Parser.Lex();
9169 
9170   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9171     Error(Parser.getTok().getLoc(), "unexpected token in directive");
9172     return false;
9173   }
9174   Parser.Lex();
9175 
9176   if (Val == 16) {
9177     if (!hasThumb()) {
9178       Error(L, "target does not support Thumb mode");
9179       return false;
9180     }
9181 
9182     if (!isThumb())
9183       SwitchMode();
9184     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
9185   } else {
9186     if (!hasARM()) {
9187       Error(L, "target does not support ARM mode");
9188       return false;
9189     }
9190 
9191     if (isThumb())
9192       SwitchMode();
9193     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
9194   }
9195 
9196   return false;
9197 }
9198 
9199 /// parseDirectiveReq
9200 ///  ::= name .req registername
9201 bool ARMAsmParser::parseDirectiveReq(StringRef Name, SMLoc L) {
9202   MCAsmParser &Parser = getParser();
9203   Parser.Lex(); // Eat the '.req' token.
9204   unsigned Reg;
9205   SMLoc SRegLoc, ERegLoc;
9206   if (ParseRegister(Reg, SRegLoc, ERegLoc)) {
9207     Parser.eatToEndOfStatement();
9208     Error(SRegLoc, "register name expected");
9209     return false;
9210   }
9211 
9212   // Shouldn't be anything else.
9213   if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
9214     Parser.eatToEndOfStatement();
9215     Error(Parser.getTok().getLoc(), "unexpected input in .req directive.");
9216     return false;
9217   }
9218 
9219   Parser.Lex(); // Consume the EndOfStatement
9220 
9221   if (RegisterReqs.insert(std::make_pair(Name, Reg)).first->second != Reg) {
9222     Error(SRegLoc, "redefinition of '" + Name + "' does not match original.");
9223     return false;
9224   }
9225 
9226   return false;
9227 }
9228 
9229 /// parseDirectiveUneq
9230 ///  ::= .unreq registername
9231 bool ARMAsmParser::parseDirectiveUnreq(SMLoc L) {
9232   MCAsmParser &Parser = getParser();
9233   if (Parser.getTok().isNot(AsmToken::Identifier)) {
9234     Parser.eatToEndOfStatement();
9235     Error(L, "unexpected input in .unreq directive.");
9236     return false;
9237   }
9238   RegisterReqs.erase(Parser.getTok().getIdentifier().lower());
9239   Parser.Lex(); // Eat the identifier.
9240   return false;
9241 }
9242 
9243 // After changing arch/CPU, try to put the ARM/Thumb mode back to what it was
9244 // before, if supported by the new target, or emit mapping symbols for the mode
9245 // switch.
9246 void ARMAsmParser::FixModeAfterArchChange(bool WasThumb, SMLoc Loc) {
9247   if (WasThumb != isThumb()) {
9248     if (WasThumb && hasThumb()) {
9249       // Stay in Thumb mode
9250       SwitchMode();
9251     } else if (!WasThumb && hasARM()) {
9252       // Stay in ARM mode
9253       SwitchMode();
9254     } else {
9255       // Mode switch forced, because the new arch doesn't support the old mode.
9256       getParser().getStreamer().EmitAssemblerFlag(isThumb() ? MCAF_Code16
9257                                                             : MCAF_Code32);
9258       // Warn about the implcit mode switch. GAS does not switch modes here,
9259       // but instead stays in the old mode, reporting an error on any following
9260       // instructions as the mode does not exist on the target.
9261       Warning(Loc, Twine("new target does not support ") +
9262                        (WasThumb ? "thumb" : "arm") + " mode, switching to " +
9263                        (!WasThumb ? "thumb" : "arm") + " mode");
9264     }
9265   }
9266 }
9267 
9268 /// parseDirectiveArch
9269 ///  ::= .arch token
9270 bool ARMAsmParser::parseDirectiveArch(SMLoc L) {
9271   StringRef Arch = getParser().parseStringToEndOfStatement().trim();
9272 
9273   unsigned ID = ARM::parseArch(Arch);
9274 
9275   if (ID == ARM::AK_INVALID) {
9276     Error(L, "Unknown arch name");
9277     return false;
9278   }
9279 
9280   bool WasThumb = isThumb();
9281   Triple T;
9282   MCSubtargetInfo &STI = copySTI();
9283   STI.setDefaultFeatures("", ("+" + ARM::getArchName(ID)).str());
9284   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9285   FixModeAfterArchChange(WasThumb, L);
9286 
9287   getTargetStreamer().emitArch(ID);
9288   return false;
9289 }
9290 
9291 /// parseDirectiveEabiAttr
9292 ///  ::= .eabi_attribute int, int [, "str"]
9293 ///  ::= .eabi_attribute Tag_name, int [, "str"]
9294 bool ARMAsmParser::parseDirectiveEabiAttr(SMLoc L) {
9295   MCAsmParser &Parser = getParser();
9296   int64_t Tag;
9297   SMLoc TagLoc;
9298   TagLoc = Parser.getTok().getLoc();
9299   if (Parser.getTok().is(AsmToken::Identifier)) {
9300     StringRef Name = Parser.getTok().getIdentifier();
9301     Tag = ARMBuildAttrs::AttrTypeFromString(Name);
9302     if (Tag == -1) {
9303       Error(TagLoc, "attribute name not recognised: " + Name);
9304       Parser.eatToEndOfStatement();
9305       return false;
9306     }
9307     Parser.Lex();
9308   } else {
9309     const MCExpr *AttrExpr;
9310 
9311     TagLoc = Parser.getTok().getLoc();
9312     if (Parser.parseExpression(AttrExpr)) {
9313       Parser.eatToEndOfStatement();
9314       return false;
9315     }
9316 
9317     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(AttrExpr);
9318     if (!CE) {
9319       Error(TagLoc, "expected numeric constant");
9320       Parser.eatToEndOfStatement();
9321       return false;
9322     }
9323 
9324     Tag = CE->getValue();
9325   }
9326 
9327   if (Parser.getTok().isNot(AsmToken::Comma)) {
9328     Error(Parser.getTok().getLoc(), "comma expected");
9329     Parser.eatToEndOfStatement();
9330     return false;
9331   }
9332   Parser.Lex(); // skip comma
9333 
9334   StringRef StringValue = "";
9335   bool IsStringValue = false;
9336 
9337   int64_t IntegerValue = 0;
9338   bool IsIntegerValue = false;
9339 
9340   if (Tag == ARMBuildAttrs::CPU_raw_name || Tag == ARMBuildAttrs::CPU_name)
9341     IsStringValue = true;
9342   else if (Tag == ARMBuildAttrs::compatibility) {
9343     IsStringValue = true;
9344     IsIntegerValue = true;
9345   } else if (Tag < 32 || Tag % 2 == 0)
9346     IsIntegerValue = true;
9347   else if (Tag % 2 == 1)
9348     IsStringValue = true;
9349   else
9350     llvm_unreachable("invalid tag type");
9351 
9352   if (IsIntegerValue) {
9353     const MCExpr *ValueExpr;
9354     SMLoc ValueExprLoc = Parser.getTok().getLoc();
9355     if (Parser.parseExpression(ValueExpr)) {
9356       Parser.eatToEndOfStatement();
9357       return false;
9358     }
9359 
9360     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ValueExpr);
9361     if (!CE) {
9362       Error(ValueExprLoc, "expected numeric constant");
9363       Parser.eatToEndOfStatement();
9364       return false;
9365     }
9366 
9367     IntegerValue = CE->getValue();
9368   }
9369 
9370   if (Tag == ARMBuildAttrs::compatibility) {
9371     if (Parser.getTok().isNot(AsmToken::Comma))
9372       IsStringValue = false;
9373     if (Parser.getTok().isNot(AsmToken::Comma)) {
9374       Error(Parser.getTok().getLoc(), "comma expected");
9375       Parser.eatToEndOfStatement();
9376       return false;
9377     } else {
9378        Parser.Lex();
9379     }
9380   }
9381 
9382   if (IsStringValue) {
9383     if (Parser.getTok().isNot(AsmToken::String)) {
9384       Error(Parser.getTok().getLoc(), "bad string constant");
9385       Parser.eatToEndOfStatement();
9386       return false;
9387     }
9388 
9389     StringValue = Parser.getTok().getStringContents();
9390     Parser.Lex();
9391   }
9392 
9393   if (IsIntegerValue && IsStringValue) {
9394     assert(Tag == ARMBuildAttrs::compatibility);
9395     getTargetStreamer().emitIntTextAttribute(Tag, IntegerValue, StringValue);
9396   } else if (IsIntegerValue)
9397     getTargetStreamer().emitAttribute(Tag, IntegerValue);
9398   else if (IsStringValue)
9399     getTargetStreamer().emitTextAttribute(Tag, StringValue);
9400   return false;
9401 }
9402 
9403 /// parseDirectiveCPU
9404 ///  ::= .cpu str
9405 bool ARMAsmParser::parseDirectiveCPU(SMLoc L) {
9406   StringRef CPU = getParser().parseStringToEndOfStatement().trim();
9407   getTargetStreamer().emitTextAttribute(ARMBuildAttrs::CPU_name, CPU);
9408 
9409   // FIXME: This is using table-gen data, but should be moved to
9410   // ARMTargetParser once that is table-gen'd.
9411   if (!getSTI().isCPUStringValid(CPU)) {
9412     Error(L, "Unknown CPU name");
9413     return false;
9414   }
9415 
9416   bool WasThumb = isThumb();
9417   MCSubtargetInfo &STI = copySTI();
9418   STI.setDefaultFeatures(CPU, "");
9419   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9420   FixModeAfterArchChange(WasThumb, L);
9421 
9422   return false;
9423 }
9424 /// parseDirectiveFPU
9425 ///  ::= .fpu str
9426 bool ARMAsmParser::parseDirectiveFPU(SMLoc L) {
9427   SMLoc FPUNameLoc = getTok().getLoc();
9428   StringRef FPU = getParser().parseStringToEndOfStatement().trim();
9429 
9430   unsigned ID = ARM::parseFPU(FPU);
9431   std::vector<const char *> Features;
9432   if (!ARM::getFPUFeatures(ID, Features)) {
9433     Error(FPUNameLoc, "Unknown FPU name");
9434     return false;
9435   }
9436 
9437   MCSubtargetInfo &STI = copySTI();
9438   for (auto Feature : Features)
9439     STI.ApplyFeatureFlag(Feature);
9440   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9441 
9442   getTargetStreamer().emitFPU(ID);
9443   return false;
9444 }
9445 
9446 /// parseDirectiveFnStart
9447 ///  ::= .fnstart
9448 bool ARMAsmParser::parseDirectiveFnStart(SMLoc L) {
9449   if (UC.hasFnStart()) {
9450     Error(L, ".fnstart starts before the end of previous one");
9451     UC.emitFnStartLocNotes();
9452     return false;
9453   }
9454 
9455   // Reset the unwind directives parser state
9456   UC.reset();
9457 
9458   getTargetStreamer().emitFnStart();
9459 
9460   UC.recordFnStart(L);
9461   return false;
9462 }
9463 
9464 /// parseDirectiveFnEnd
9465 ///  ::= .fnend
9466 bool ARMAsmParser::parseDirectiveFnEnd(SMLoc L) {
9467   // Check the ordering of unwind directives
9468   if (!UC.hasFnStart()) {
9469     Error(L, ".fnstart must precede .fnend directive");
9470     return false;
9471   }
9472 
9473   // Reset the unwind directives parser state
9474   getTargetStreamer().emitFnEnd();
9475 
9476   UC.reset();
9477   return false;
9478 }
9479 
9480 /// parseDirectiveCantUnwind
9481 ///  ::= .cantunwind
9482 bool ARMAsmParser::parseDirectiveCantUnwind(SMLoc L) {
9483   UC.recordCantUnwind(L);
9484 
9485   // Check the ordering of unwind directives
9486   if (!UC.hasFnStart()) {
9487     Error(L, ".fnstart must precede .cantunwind directive");
9488     return false;
9489   }
9490   if (UC.hasHandlerData()) {
9491     Error(L, ".cantunwind can't be used with .handlerdata directive");
9492     UC.emitHandlerDataLocNotes();
9493     return false;
9494   }
9495   if (UC.hasPersonality()) {
9496     Error(L, ".cantunwind can't be used with .personality directive");
9497     UC.emitPersonalityLocNotes();
9498     return false;
9499   }
9500 
9501   getTargetStreamer().emitCantUnwind();
9502   return false;
9503 }
9504 
9505 /// parseDirectivePersonality
9506 ///  ::= .personality name
9507 bool ARMAsmParser::parseDirectivePersonality(SMLoc L) {
9508   MCAsmParser &Parser = getParser();
9509   bool HasExistingPersonality = UC.hasPersonality();
9510 
9511   UC.recordPersonality(L);
9512 
9513   // Check the ordering of unwind directives
9514   if (!UC.hasFnStart()) {
9515     Error(L, ".fnstart must precede .personality directive");
9516     return false;
9517   }
9518   if (UC.cantUnwind()) {
9519     Error(L, ".personality can't be used with .cantunwind directive");
9520     UC.emitCantUnwindLocNotes();
9521     return false;
9522   }
9523   if (UC.hasHandlerData()) {
9524     Error(L, ".personality must precede .handlerdata directive");
9525     UC.emitHandlerDataLocNotes();
9526     return false;
9527   }
9528   if (HasExistingPersonality) {
9529     Parser.eatToEndOfStatement();
9530     Error(L, "multiple personality directives");
9531     UC.emitPersonalityLocNotes();
9532     return false;
9533   }
9534 
9535   // Parse the name of the personality routine
9536   if (Parser.getTok().isNot(AsmToken::Identifier)) {
9537     Parser.eatToEndOfStatement();
9538     Error(L, "unexpected input in .personality directive.");
9539     return false;
9540   }
9541   StringRef Name(Parser.getTok().getIdentifier());
9542   Parser.Lex();
9543 
9544   MCSymbol *PR = getParser().getContext().getOrCreateSymbol(Name);
9545   getTargetStreamer().emitPersonality(PR);
9546   return false;
9547 }
9548 
9549 /// parseDirectiveHandlerData
9550 ///  ::= .handlerdata
9551 bool ARMAsmParser::parseDirectiveHandlerData(SMLoc L) {
9552   UC.recordHandlerData(L);
9553 
9554   // Check the ordering of unwind directives
9555   if (!UC.hasFnStart()) {
9556     Error(L, ".fnstart must precede .personality directive");
9557     return false;
9558   }
9559   if (UC.cantUnwind()) {
9560     Error(L, ".handlerdata can't be used with .cantunwind directive");
9561     UC.emitCantUnwindLocNotes();
9562     return false;
9563   }
9564 
9565   getTargetStreamer().emitHandlerData();
9566   return false;
9567 }
9568 
9569 /// parseDirectiveSetFP
9570 ///  ::= .setfp fpreg, spreg [, offset]
9571 bool ARMAsmParser::parseDirectiveSetFP(SMLoc L) {
9572   MCAsmParser &Parser = getParser();
9573   // Check the ordering of unwind directives
9574   if (!UC.hasFnStart()) {
9575     Error(L, ".fnstart must precede .setfp directive");
9576     return false;
9577   }
9578   if (UC.hasHandlerData()) {
9579     Error(L, ".setfp must precede .handlerdata directive");
9580     return false;
9581   }
9582 
9583   // Parse fpreg
9584   SMLoc FPRegLoc = Parser.getTok().getLoc();
9585   int FPReg = tryParseRegister();
9586   if (FPReg == -1) {
9587     Error(FPRegLoc, "frame pointer register expected");
9588     return false;
9589   }
9590 
9591   // Consume comma
9592   if (Parser.getTok().isNot(AsmToken::Comma)) {
9593     Error(Parser.getTok().getLoc(), "comma expected");
9594     return false;
9595   }
9596   Parser.Lex(); // skip comma
9597 
9598   // Parse spreg
9599   SMLoc SPRegLoc = Parser.getTok().getLoc();
9600   int SPReg = tryParseRegister();
9601   if (SPReg == -1) {
9602     Error(SPRegLoc, "stack pointer register expected");
9603     return false;
9604   }
9605 
9606   if (SPReg != ARM::SP && SPReg != UC.getFPReg()) {
9607     Error(SPRegLoc, "register should be either $sp or the latest fp register");
9608     return false;
9609   }
9610 
9611   // Update the frame pointer register
9612   UC.saveFPReg(FPReg);
9613 
9614   // Parse offset
9615   int64_t Offset = 0;
9616   if (Parser.getTok().is(AsmToken::Comma)) {
9617     Parser.Lex(); // skip comma
9618 
9619     if (Parser.getTok().isNot(AsmToken::Hash) &&
9620         Parser.getTok().isNot(AsmToken::Dollar)) {
9621       Error(Parser.getTok().getLoc(), "'#' expected");
9622       return false;
9623     }
9624     Parser.Lex(); // skip hash token.
9625 
9626     const MCExpr *OffsetExpr;
9627     SMLoc ExLoc = Parser.getTok().getLoc();
9628     SMLoc EndLoc;
9629     if (getParser().parseExpression(OffsetExpr, EndLoc)) {
9630       Error(ExLoc, "malformed setfp offset");
9631       return false;
9632     }
9633     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9634     if (!CE) {
9635       Error(ExLoc, "setfp offset must be an immediate");
9636       return false;
9637     }
9638 
9639     Offset = CE->getValue();
9640   }
9641 
9642   getTargetStreamer().emitSetFP(static_cast<unsigned>(FPReg),
9643                                 static_cast<unsigned>(SPReg), Offset);
9644   return false;
9645 }
9646 
9647 /// parseDirective
9648 ///  ::= .pad offset
9649 bool ARMAsmParser::parseDirectivePad(SMLoc L) {
9650   MCAsmParser &Parser = getParser();
9651   // Check the ordering of unwind directives
9652   if (!UC.hasFnStart()) {
9653     Error(L, ".fnstart must precede .pad directive");
9654     return false;
9655   }
9656   if (UC.hasHandlerData()) {
9657     Error(L, ".pad must precede .handlerdata directive");
9658     return false;
9659   }
9660 
9661   // Parse the offset
9662   if (Parser.getTok().isNot(AsmToken::Hash) &&
9663       Parser.getTok().isNot(AsmToken::Dollar)) {
9664     Error(Parser.getTok().getLoc(), "'#' expected");
9665     return false;
9666   }
9667   Parser.Lex(); // skip hash token.
9668 
9669   const MCExpr *OffsetExpr;
9670   SMLoc ExLoc = Parser.getTok().getLoc();
9671   SMLoc EndLoc;
9672   if (getParser().parseExpression(OffsetExpr, EndLoc)) {
9673     Error(ExLoc, "malformed pad offset");
9674     return false;
9675   }
9676   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9677   if (!CE) {
9678     Error(ExLoc, "pad offset must be an immediate");
9679     return false;
9680   }
9681 
9682   getTargetStreamer().emitPad(CE->getValue());
9683   return false;
9684 }
9685 
9686 /// parseDirectiveRegSave
9687 ///  ::= .save  { registers }
9688 ///  ::= .vsave { registers }
9689 bool ARMAsmParser::parseDirectiveRegSave(SMLoc L, bool IsVector) {
9690   // Check the ordering of unwind directives
9691   if (!UC.hasFnStart()) {
9692     Error(L, ".fnstart must precede .save or .vsave directives");
9693     return false;
9694   }
9695   if (UC.hasHandlerData()) {
9696     Error(L, ".save or .vsave must precede .handlerdata directive");
9697     return false;
9698   }
9699 
9700   // RAII object to make sure parsed operands are deleted.
9701   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Operands;
9702 
9703   // Parse the register list
9704   if (parseRegisterList(Operands))
9705     return false;
9706   ARMOperand &Op = (ARMOperand &)*Operands[0];
9707   if (!IsVector && !Op.isRegList()) {
9708     Error(L, ".save expects GPR registers");
9709     return false;
9710   }
9711   if (IsVector && !Op.isDPRRegList()) {
9712     Error(L, ".vsave expects DPR registers");
9713     return false;
9714   }
9715 
9716   getTargetStreamer().emitRegSave(Op.getRegList(), IsVector);
9717   return false;
9718 }
9719 
9720 /// parseDirectiveInst
9721 ///  ::= .inst opcode [, ...]
9722 ///  ::= .inst.n opcode [, ...]
9723 ///  ::= .inst.w opcode [, ...]
9724 bool ARMAsmParser::parseDirectiveInst(SMLoc Loc, char Suffix) {
9725   MCAsmParser &Parser = getParser();
9726   int Width;
9727 
9728   if (isThumb()) {
9729     switch (Suffix) {
9730     case 'n':
9731       Width = 2;
9732       break;
9733     case 'w':
9734       Width = 4;
9735       break;
9736     default:
9737       Parser.eatToEndOfStatement();
9738       Error(Loc, "cannot determine Thumb instruction size, "
9739                  "use inst.n/inst.w instead");
9740       return false;
9741     }
9742   } else {
9743     if (Suffix) {
9744       Parser.eatToEndOfStatement();
9745       Error(Loc, "width suffixes are invalid in ARM mode");
9746       return false;
9747     }
9748     Width = 4;
9749   }
9750 
9751   if (getLexer().is(AsmToken::EndOfStatement)) {
9752     Parser.eatToEndOfStatement();
9753     Error(Loc, "expected expression following directive");
9754     return false;
9755   }
9756 
9757   for (;;) {
9758     const MCExpr *Expr;
9759 
9760     if (getParser().parseExpression(Expr)) {
9761       Error(Loc, "expected expression");
9762       return false;
9763     }
9764 
9765     const MCConstantExpr *Value = dyn_cast_or_null<MCConstantExpr>(Expr);
9766     if (!Value) {
9767       Error(Loc, "expected constant expression");
9768       return false;
9769     }
9770 
9771     switch (Width) {
9772     case 2:
9773       if (Value->getValue() > 0xffff) {
9774         Error(Loc, "inst.n operand is too big, use inst.w instead");
9775         return false;
9776       }
9777       break;
9778     case 4:
9779       if (Value->getValue() > 0xffffffff) {
9780         Error(Loc,
9781               StringRef(Suffix ? "inst.w" : "inst") + " operand is too big");
9782         return false;
9783       }
9784       break;
9785     default:
9786       llvm_unreachable("only supported widths are 2 and 4");
9787     }
9788 
9789     getTargetStreamer().emitInst(Value->getValue(), Suffix);
9790 
9791     if (getLexer().is(AsmToken::EndOfStatement))
9792       break;
9793 
9794     if (getLexer().isNot(AsmToken::Comma)) {
9795       Error(Loc, "unexpected token in directive");
9796       return false;
9797     }
9798 
9799     Parser.Lex();
9800   }
9801 
9802   Parser.Lex();
9803   return false;
9804 }
9805 
9806 /// parseDirectiveLtorg
9807 ///  ::= .ltorg | .pool
9808 bool ARMAsmParser::parseDirectiveLtorg(SMLoc L) {
9809   getTargetStreamer().emitCurrentConstantPool();
9810   return false;
9811 }
9812 
9813 bool ARMAsmParser::parseDirectiveEven(SMLoc L) {
9814   const MCSection *Section = getStreamer().getCurrentSection().first;
9815 
9816   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9817     TokError("unexpected token in directive");
9818     return false;
9819   }
9820 
9821   if (!Section) {
9822     getStreamer().InitSections(false);
9823     Section = getStreamer().getCurrentSection().first;
9824   }
9825 
9826   assert(Section && "must have section to emit alignment");
9827   if (Section->UseCodeAlign())
9828     getStreamer().EmitCodeAlignment(2);
9829   else
9830     getStreamer().EmitValueToAlignment(2);
9831 
9832   return false;
9833 }
9834 
9835 /// parseDirectivePersonalityIndex
9836 ///   ::= .personalityindex index
9837 bool ARMAsmParser::parseDirectivePersonalityIndex(SMLoc L) {
9838   MCAsmParser &Parser = getParser();
9839   bool HasExistingPersonality = UC.hasPersonality();
9840 
9841   UC.recordPersonalityIndex(L);
9842 
9843   if (!UC.hasFnStart()) {
9844     Parser.eatToEndOfStatement();
9845     Error(L, ".fnstart must precede .personalityindex directive");
9846     return false;
9847   }
9848   if (UC.cantUnwind()) {
9849     Parser.eatToEndOfStatement();
9850     Error(L, ".personalityindex cannot be used with .cantunwind");
9851     UC.emitCantUnwindLocNotes();
9852     return false;
9853   }
9854   if (UC.hasHandlerData()) {
9855     Parser.eatToEndOfStatement();
9856     Error(L, ".personalityindex must precede .handlerdata directive");
9857     UC.emitHandlerDataLocNotes();
9858     return false;
9859   }
9860   if (HasExistingPersonality) {
9861     Parser.eatToEndOfStatement();
9862     Error(L, "multiple personality directives");
9863     UC.emitPersonalityLocNotes();
9864     return false;
9865   }
9866 
9867   const MCExpr *IndexExpression;
9868   SMLoc IndexLoc = Parser.getTok().getLoc();
9869   if (Parser.parseExpression(IndexExpression)) {
9870     Parser.eatToEndOfStatement();
9871     return false;
9872   }
9873 
9874   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(IndexExpression);
9875   if (!CE) {
9876     Parser.eatToEndOfStatement();
9877     Error(IndexLoc, "index must be a constant number");
9878     return false;
9879   }
9880   if (CE->getValue() < 0 ||
9881       CE->getValue() >= ARM::EHABI::NUM_PERSONALITY_INDEX) {
9882     Parser.eatToEndOfStatement();
9883     Error(IndexLoc, "personality routine index should be in range [0-3]");
9884     return false;
9885   }
9886 
9887   getTargetStreamer().emitPersonalityIndex(CE->getValue());
9888   return false;
9889 }
9890 
9891 /// parseDirectiveUnwindRaw
9892 ///   ::= .unwind_raw offset, opcode [, opcode...]
9893 bool ARMAsmParser::parseDirectiveUnwindRaw(SMLoc L) {
9894   MCAsmParser &Parser = getParser();
9895   if (!UC.hasFnStart()) {
9896     Parser.eatToEndOfStatement();
9897     Error(L, ".fnstart must precede .unwind_raw directives");
9898     return false;
9899   }
9900 
9901   int64_t StackOffset;
9902 
9903   const MCExpr *OffsetExpr;
9904   SMLoc OffsetLoc = getLexer().getLoc();
9905   if (getLexer().is(AsmToken::EndOfStatement) ||
9906       getParser().parseExpression(OffsetExpr)) {
9907     Error(OffsetLoc, "expected expression");
9908     Parser.eatToEndOfStatement();
9909     return false;
9910   }
9911 
9912   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9913   if (!CE) {
9914     Error(OffsetLoc, "offset must be a constant");
9915     Parser.eatToEndOfStatement();
9916     return false;
9917   }
9918 
9919   StackOffset = CE->getValue();
9920 
9921   if (getLexer().isNot(AsmToken::Comma)) {
9922     Error(getLexer().getLoc(), "expected comma");
9923     Parser.eatToEndOfStatement();
9924     return false;
9925   }
9926   Parser.Lex();
9927 
9928   SmallVector<uint8_t, 16> Opcodes;
9929   for (;;) {
9930     const MCExpr *OE;
9931 
9932     SMLoc OpcodeLoc = getLexer().getLoc();
9933     if (getLexer().is(AsmToken::EndOfStatement) || Parser.parseExpression(OE)) {
9934       Error(OpcodeLoc, "expected opcode expression");
9935       Parser.eatToEndOfStatement();
9936       return false;
9937     }
9938 
9939     const MCConstantExpr *OC = dyn_cast<MCConstantExpr>(OE);
9940     if (!OC) {
9941       Error(OpcodeLoc, "opcode value must be a constant");
9942       Parser.eatToEndOfStatement();
9943       return false;
9944     }
9945 
9946     const int64_t Opcode = OC->getValue();
9947     if (Opcode & ~0xff) {
9948       Error(OpcodeLoc, "invalid opcode");
9949       Parser.eatToEndOfStatement();
9950       return false;
9951     }
9952 
9953     Opcodes.push_back(uint8_t(Opcode));
9954 
9955     if (getLexer().is(AsmToken::EndOfStatement))
9956       break;
9957 
9958     if (getLexer().isNot(AsmToken::Comma)) {
9959       Error(getLexer().getLoc(), "unexpected token in directive");
9960       Parser.eatToEndOfStatement();
9961       return false;
9962     }
9963 
9964     Parser.Lex();
9965   }
9966 
9967   getTargetStreamer().emitUnwindRaw(StackOffset, Opcodes);
9968 
9969   Parser.Lex();
9970   return false;
9971 }
9972 
9973 /// parseDirectiveTLSDescSeq
9974 ///   ::= .tlsdescseq tls-variable
9975 bool ARMAsmParser::parseDirectiveTLSDescSeq(SMLoc L) {
9976   MCAsmParser &Parser = getParser();
9977 
9978   if (getLexer().isNot(AsmToken::Identifier)) {
9979     TokError("expected variable after '.tlsdescseq' directive");
9980     Parser.eatToEndOfStatement();
9981     return false;
9982   }
9983 
9984   const MCSymbolRefExpr *SRE =
9985     MCSymbolRefExpr::create(Parser.getTok().getIdentifier(),
9986                             MCSymbolRefExpr::VK_ARM_TLSDESCSEQ, getContext());
9987   Lex();
9988 
9989   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9990     Error(Parser.getTok().getLoc(), "unexpected token");
9991     Parser.eatToEndOfStatement();
9992     return false;
9993   }
9994 
9995   getTargetStreamer().AnnotateTLSDescriptorSequence(SRE);
9996   return false;
9997 }
9998 
9999 /// parseDirectiveMovSP
10000 ///  ::= .movsp reg [, #offset]
10001 bool ARMAsmParser::parseDirectiveMovSP(SMLoc L) {
10002   MCAsmParser &Parser = getParser();
10003   if (!UC.hasFnStart()) {
10004     Parser.eatToEndOfStatement();
10005     Error(L, ".fnstart must precede .movsp directives");
10006     return false;
10007   }
10008   if (UC.getFPReg() != ARM::SP) {
10009     Parser.eatToEndOfStatement();
10010     Error(L, "unexpected .movsp directive");
10011     return false;
10012   }
10013 
10014   SMLoc SPRegLoc = Parser.getTok().getLoc();
10015   int SPReg = tryParseRegister();
10016   if (SPReg == -1) {
10017     Parser.eatToEndOfStatement();
10018     Error(SPRegLoc, "register expected");
10019     return false;
10020   }
10021 
10022   if (SPReg == ARM::SP || SPReg == ARM::PC) {
10023     Parser.eatToEndOfStatement();
10024     Error(SPRegLoc, "sp and pc are not permitted in .movsp directive");
10025     return false;
10026   }
10027 
10028   int64_t Offset = 0;
10029   if (Parser.getTok().is(AsmToken::Comma)) {
10030     Parser.Lex();
10031 
10032     if (Parser.getTok().isNot(AsmToken::Hash)) {
10033       Error(Parser.getTok().getLoc(), "expected #constant");
10034       Parser.eatToEndOfStatement();
10035       return false;
10036     }
10037     Parser.Lex();
10038 
10039     const MCExpr *OffsetExpr;
10040     SMLoc OffsetLoc = Parser.getTok().getLoc();
10041     if (Parser.parseExpression(OffsetExpr)) {
10042       Parser.eatToEndOfStatement();
10043       Error(OffsetLoc, "malformed offset expression");
10044       return false;
10045     }
10046 
10047     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
10048     if (!CE) {
10049       Parser.eatToEndOfStatement();
10050       Error(OffsetLoc, "offset must be an immediate constant");
10051       return false;
10052     }
10053 
10054     Offset = CE->getValue();
10055   }
10056 
10057   getTargetStreamer().emitMovSP(SPReg, Offset);
10058   UC.saveFPReg(SPReg);
10059 
10060   return false;
10061 }
10062 
10063 /// parseDirectiveObjectArch
10064 ///   ::= .object_arch name
10065 bool ARMAsmParser::parseDirectiveObjectArch(SMLoc L) {
10066   MCAsmParser &Parser = getParser();
10067   if (getLexer().isNot(AsmToken::Identifier)) {
10068     Error(getLexer().getLoc(), "unexpected token");
10069     Parser.eatToEndOfStatement();
10070     return false;
10071   }
10072 
10073   StringRef Arch = Parser.getTok().getString();
10074   SMLoc ArchLoc = Parser.getTok().getLoc();
10075   Lex();
10076 
10077   unsigned ID = ARM::parseArch(Arch);
10078 
10079   if (ID == ARM::AK_INVALID) {
10080     Error(ArchLoc, "unknown architecture '" + Arch + "'");
10081     Parser.eatToEndOfStatement();
10082     return false;
10083   }
10084 
10085   getTargetStreamer().emitObjectArch(ID);
10086 
10087   if (getLexer().isNot(AsmToken::EndOfStatement)) {
10088     Error(getLexer().getLoc(), "unexpected token");
10089     Parser.eatToEndOfStatement();
10090   }
10091 
10092   return false;
10093 }
10094 
10095 /// parseDirectiveAlign
10096 ///   ::= .align
10097 bool ARMAsmParser::parseDirectiveAlign(SMLoc L) {
10098   // NOTE: if this is not the end of the statement, fall back to the target
10099   // agnostic handling for this directive which will correctly handle this.
10100   if (getLexer().isNot(AsmToken::EndOfStatement))
10101     return true;
10102 
10103   // '.align' is target specifically handled to mean 2**2 byte alignment.
10104   const MCSection *Section = getStreamer().getCurrentSection().first;
10105   assert(Section && "must have section to emit alignment");
10106   if (Section->UseCodeAlign())
10107     getStreamer().EmitCodeAlignment(4, 0);
10108   else
10109     getStreamer().EmitValueToAlignment(4, 0, 1, 0);
10110 
10111   return false;
10112 }
10113 
10114 /// parseDirectiveThumbSet
10115 ///  ::= .thumb_set name, value
10116 bool ARMAsmParser::parseDirectiveThumbSet(SMLoc L) {
10117   MCAsmParser &Parser = getParser();
10118 
10119   StringRef Name;
10120   if (Parser.parseIdentifier(Name)) {
10121     TokError("expected identifier after '.thumb_set'");
10122     Parser.eatToEndOfStatement();
10123     return false;
10124   }
10125 
10126   if (getLexer().isNot(AsmToken::Comma)) {
10127     TokError("expected comma after name '" + Name + "'");
10128     Parser.eatToEndOfStatement();
10129     return false;
10130   }
10131   Lex();
10132 
10133   MCSymbol *Sym;
10134   const MCExpr *Value;
10135   if (MCParserUtils::parseAssignmentExpression(Name, /* allow_redef */ true,
10136                                                Parser, Sym, Value))
10137     return true;
10138 
10139   getTargetStreamer().emitThumbSet(Sym, Value);
10140   return false;
10141 }
10142 
10143 /// Force static initialization.
10144 extern "C" void LLVMInitializeARMAsmParser() {
10145   RegisterMCAsmParser<ARMAsmParser> X(TheARMLETarget);
10146   RegisterMCAsmParser<ARMAsmParser> Y(TheARMBETarget);
10147   RegisterMCAsmParser<ARMAsmParser> A(TheThumbLETarget);
10148   RegisterMCAsmParser<ARMAsmParser> B(TheThumbBETarget);
10149 }
10150 
10151 #define GET_REGISTER_MATCHER
10152 #define GET_SUBTARGET_FEATURE_NAME
10153 #define GET_MATCHER_IMPLEMENTATION
10154 #include "ARMGenAsmMatcher.inc"
10155 
10156 // FIXME: This structure should be moved inside ARMTargetParser
10157 // when we start to table-generate them, and we can use the ARM
10158 // flags below, that were generated by table-gen.
10159 static const struct {
10160   const unsigned Kind;
10161   const uint64_t ArchCheck;
10162   const FeatureBitset Features;
10163 } Extensions[] = {
10164   { ARM::AEK_CRC, Feature_HasV8, {ARM::FeatureCRC} },
10165   { ARM::AEK_CRYPTO,  Feature_HasV8,
10166     {ARM::FeatureCrypto, ARM::FeatureNEON, ARM::FeatureFPARMv8} },
10167   { ARM::AEK_FP, Feature_HasV8, {ARM::FeatureFPARMv8} },
10168   { (ARM::AEK_HWDIV | ARM::AEK_HWDIVARM), Feature_HasV7 | Feature_IsNotMClass,
10169     {ARM::FeatureHWDiv, ARM::FeatureHWDivARM} },
10170   { ARM::AEK_MP, Feature_HasV7 | Feature_IsNotMClass, {ARM::FeatureMP} },
10171   { ARM::AEK_SIMD, Feature_HasV8, {ARM::FeatureNEON, ARM::FeatureFPARMv8} },
10172   { ARM::AEK_SEC, Feature_HasV6K, {ARM::FeatureTrustZone} },
10173   // FIXME: Only available in A-class, isel not predicated
10174   { ARM::AEK_VIRT, Feature_HasV7, {ARM::FeatureVirtualization} },
10175   { ARM::AEK_FP16, Feature_HasV8_2a, {ARM::FeatureFPARMv8, ARM::FeatureFullFP16} },
10176   { ARM::AEK_RAS, Feature_HasV8, {ARM::FeatureRAS} },
10177   // FIXME: Unsupported extensions.
10178   { ARM::AEK_OS, Feature_None, {} },
10179   { ARM::AEK_IWMMXT, Feature_None, {} },
10180   { ARM::AEK_IWMMXT2, Feature_None, {} },
10181   { ARM::AEK_MAVERICK, Feature_None, {} },
10182   { ARM::AEK_XSCALE, Feature_None, {} },
10183 };
10184 
10185 /// parseDirectiveArchExtension
10186 ///   ::= .arch_extension [no]feature
10187 bool ARMAsmParser::parseDirectiveArchExtension(SMLoc L) {
10188   MCAsmParser &Parser = getParser();
10189 
10190   if (getLexer().isNot(AsmToken::Identifier)) {
10191     Error(getLexer().getLoc(), "unexpected token");
10192     Parser.eatToEndOfStatement();
10193     return false;
10194   }
10195 
10196   StringRef Name = Parser.getTok().getString();
10197   SMLoc ExtLoc = Parser.getTok().getLoc();
10198   Lex();
10199 
10200   bool EnableFeature = true;
10201   if (Name.startswith_lower("no")) {
10202     EnableFeature = false;
10203     Name = Name.substr(2);
10204   }
10205   unsigned FeatureKind = ARM::parseArchExt(Name);
10206   if (FeatureKind == ARM::AEK_INVALID)
10207     Error(ExtLoc, "unknown architectural extension: " + Name);
10208 
10209   for (const auto &Extension : Extensions) {
10210     if (Extension.Kind != FeatureKind)
10211       continue;
10212 
10213     if (Extension.Features.none())
10214       report_fatal_error("unsupported architectural extension: " + Name);
10215 
10216     if ((getAvailableFeatures() & Extension.ArchCheck) != Extension.ArchCheck) {
10217       Error(ExtLoc, "architectural extension '" + Name + "' is not "
10218             "allowed for the current base architecture");
10219       return false;
10220     }
10221 
10222     MCSubtargetInfo &STI = copySTI();
10223     FeatureBitset ToggleFeatures = EnableFeature
10224       ? (~STI.getFeatureBits() & Extension.Features)
10225       : ( STI.getFeatureBits() & Extension.Features);
10226 
10227     uint64_t Features =
10228         ComputeAvailableFeatures(STI.ToggleFeature(ToggleFeatures));
10229     setAvailableFeatures(Features);
10230     return false;
10231   }
10232 
10233   Error(ExtLoc, "unknown architectural extension: " + Name);
10234   Parser.eatToEndOfStatement();
10235   return false;
10236 }
10237 
10238 // Define this matcher function after the auto-generated include so we
10239 // have the match class enum definitions.
10240 unsigned ARMAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
10241                                                   unsigned Kind) {
10242   ARMOperand &Op = static_cast<ARMOperand &>(AsmOp);
10243   // If the kind is a token for a literal immediate, check if our asm
10244   // operand matches. This is for InstAliases which have a fixed-value
10245   // immediate in the syntax.
10246   switch (Kind) {
10247   default: break;
10248   case MCK__35_0:
10249     if (Op.isImm())
10250       if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm()))
10251         if (CE->getValue() == 0)
10252           return Match_Success;
10253     break;
10254   case MCK_ModImm:
10255     if (Op.isImm()) {
10256       const MCExpr *SOExpr = Op.getImm();
10257       int64_t Value;
10258       if (!SOExpr->evaluateAsAbsolute(Value))
10259         return Match_Success;
10260       assert((Value >= INT32_MIN && Value <= UINT32_MAX) &&
10261              "expression value must be representable in 32 bits");
10262     }
10263     break;
10264   case MCK_rGPR:
10265     if (hasV8Ops() && Op.isReg() && Op.getReg() == ARM::SP)
10266       return Match_Success;
10267     break;
10268   case MCK_GPRPair:
10269     if (Op.isReg() &&
10270         MRI->getRegClass(ARM::GPRRegClassID).contains(Op.getReg()))
10271       return Match_Success;
10272     break;
10273   }
10274   return Match_InvalidOperand;
10275 }
10276