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 hasV6Ops() const {
261     return getSTI().getFeatureBits()[ARM::HasV6Ops];
262   }
263   bool hasV6MOps() const {
264     return getSTI().getFeatureBits()[ARM::HasV6MOps];
265   }
266   bool hasV7Ops() const {
267     return getSTI().getFeatureBits()[ARM::HasV7Ops];
268   }
269   bool hasV8Ops() const {
270     return getSTI().getFeatureBits()[ARM::HasV8Ops];
271   }
272   bool hasV8MBaseline() const {
273     return getSTI().getFeatureBits()[ARM::HasV8MBaselineOps];
274   }
275   bool hasV8MMainline() const {
276     return getSTI().getFeatureBits()[ARM::HasV8MMainlineOps];
277   }
278   bool has8MSecExt() const {
279     return getSTI().getFeatureBits()[ARM::Feature8MSecExt];
280   }
281   bool hasARM() const {
282     return !getSTI().getFeatureBits()[ARM::FeatureNoARM];
283   }
284   bool hasDSP() const {
285     return getSTI().getFeatureBits()[ARM::FeatureDSP];
286   }
287   bool hasD16() const {
288     return getSTI().getFeatureBits()[ARM::FeatureD16];
289   }
290   bool hasV8_1aOps() const {
291     return getSTI().getFeatureBits()[ARM::HasV8_1aOps];
292   }
293 
294   void SwitchMode() {
295     MCSubtargetInfo &STI = copySTI();
296     uint64_t FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
297     setAvailableFeatures(FB);
298   }
299   void FixModeAfterArchChange(bool WasThumb, SMLoc Loc);
300   bool isMClass() const {
301     return getSTI().getFeatureBits()[ARM::FeatureMClass];
302   }
303 
304   /// @name Auto-generated Match Functions
305   /// {
306 
307 #define GET_ASSEMBLER_HEADER
308 #include "ARMGenAsmMatcher.inc"
309 
310   /// }
311 
312   OperandMatchResultTy parseITCondCode(OperandVector &);
313   OperandMatchResultTy parseCoprocNumOperand(OperandVector &);
314   OperandMatchResultTy parseCoprocRegOperand(OperandVector &);
315   OperandMatchResultTy parseCoprocOptionOperand(OperandVector &);
316   OperandMatchResultTy parseMemBarrierOptOperand(OperandVector &);
317   OperandMatchResultTy parseInstSyncBarrierOptOperand(OperandVector &);
318   OperandMatchResultTy parseProcIFlagsOperand(OperandVector &);
319   OperandMatchResultTy parseMSRMaskOperand(OperandVector &);
320   OperandMatchResultTy parseBankedRegOperand(OperandVector &);
321   OperandMatchResultTy parsePKHImm(OperandVector &O, StringRef Op, int Low,
322                                    int High);
323   OperandMatchResultTy parsePKHLSLImm(OperandVector &O) {
324     return parsePKHImm(O, "lsl", 0, 31);
325   }
326   OperandMatchResultTy parsePKHASRImm(OperandVector &O) {
327     return parsePKHImm(O, "asr", 1, 32);
328   }
329   OperandMatchResultTy parseSetEndImm(OperandVector &);
330   OperandMatchResultTy parseShifterImm(OperandVector &);
331   OperandMatchResultTy parseRotImm(OperandVector &);
332   OperandMatchResultTy parseModImm(OperandVector &);
333   OperandMatchResultTy parseBitfield(OperandVector &);
334   OperandMatchResultTy parsePostIdxReg(OperandVector &);
335   OperandMatchResultTy parseAM3Offset(OperandVector &);
336   OperandMatchResultTy parseFPImm(OperandVector &);
337   OperandMatchResultTy parseVectorList(OperandVector &);
338   OperandMatchResultTy parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index,
339                                        SMLoc &EndLoc);
340 
341   // Asm Match Converter Methods
342   void cvtThumbMultiply(MCInst &Inst, const OperandVector &);
343   void cvtThumbBranches(MCInst &Inst, const OperandVector &);
344 
345   bool validateInstruction(MCInst &Inst, const OperandVector &Ops);
346   bool processInstruction(MCInst &Inst, const OperandVector &Ops, MCStreamer &Out);
347   bool shouldOmitCCOutOperand(StringRef Mnemonic, OperandVector &Operands);
348   bool shouldOmitPredicateOperand(StringRef Mnemonic, OperandVector &Operands);
349 
350 public:
351   enum ARMMatchResultTy {
352     Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
353     Match_RequiresNotITBlock,
354     Match_RequiresV6,
355     Match_RequiresThumb2,
356     Match_RequiresV8,
357 #define GET_OPERAND_DIAGNOSTIC_TYPES
358 #include "ARMGenAsmMatcher.inc"
359 
360   };
361 
362   ARMAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
363                const MCInstrInfo &MII, const MCTargetOptions &Options)
364     : MCTargetAsmParser(Options, STI), MII(MII), UC(Parser) {
365     MCAsmParserExtension::Initialize(Parser);
366 
367     // Cache the MCRegisterInfo.
368     MRI = getContext().getRegisterInfo();
369 
370     // Initialize the set of available features.
371     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
372 
373     // Not in an ITBlock to start with.
374     ITState.CurPosition = ~0U;
375 
376     NextSymbolIsThumb = false;
377   }
378 
379   // Implementation of the MCTargetAsmParser interface:
380   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
381   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
382                         SMLoc NameLoc, OperandVector &Operands) override;
383   bool ParseDirective(AsmToken DirectiveID) override;
384 
385   unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
386                                       unsigned Kind) override;
387   unsigned checkTargetMatchPredicate(MCInst &Inst) override;
388 
389   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
390                                OperandVector &Operands, MCStreamer &Out,
391                                uint64_t &ErrorInfo,
392                                bool MatchingInlineAsm) override;
393   void onLabelParsed(MCSymbol *Symbol) override;
394 };
395 } // end anonymous namespace
396 
397 namespace {
398 
399 /// ARMOperand - Instances of this class represent a parsed ARM machine
400 /// operand.
401 class ARMOperand : public MCParsedAsmOperand {
402   enum KindTy {
403     k_CondCode,
404     k_CCOut,
405     k_ITCondMask,
406     k_CoprocNum,
407     k_CoprocReg,
408     k_CoprocOption,
409     k_Immediate,
410     k_MemBarrierOpt,
411     k_InstSyncBarrierOpt,
412     k_Memory,
413     k_PostIndexRegister,
414     k_MSRMask,
415     k_BankedReg,
416     k_ProcIFlags,
417     k_VectorIndex,
418     k_Register,
419     k_RegisterList,
420     k_DPRRegisterList,
421     k_SPRRegisterList,
422     k_VectorList,
423     k_VectorListAllLanes,
424     k_VectorListIndexed,
425     k_ShiftedRegister,
426     k_ShiftedImmediate,
427     k_ShifterImmediate,
428     k_RotateImmediate,
429     k_ModifiedImmediate,
430     k_BitfieldDescriptor,
431     k_Token
432   } Kind;
433 
434   SMLoc StartLoc, EndLoc, AlignmentLoc;
435   SmallVector<unsigned, 8> Registers;
436 
437   struct CCOp {
438     ARMCC::CondCodes Val;
439   };
440 
441   struct CopOp {
442     unsigned Val;
443   };
444 
445   struct CoprocOptionOp {
446     unsigned Val;
447   };
448 
449   struct ITMaskOp {
450     unsigned Mask:4;
451   };
452 
453   struct MBOptOp {
454     ARM_MB::MemBOpt Val;
455   };
456 
457   struct ISBOptOp {
458     ARM_ISB::InstSyncBOpt Val;
459   };
460 
461   struct IFlagsOp {
462     ARM_PROC::IFlags Val;
463   };
464 
465   struct MMaskOp {
466     unsigned Val;
467   };
468 
469   struct BankedRegOp {
470     unsigned Val;
471   };
472 
473   struct TokOp {
474     const char *Data;
475     unsigned Length;
476   };
477 
478   struct RegOp {
479     unsigned RegNum;
480   };
481 
482   // A vector register list is a sequential list of 1 to 4 registers.
483   struct VectorListOp {
484     unsigned RegNum;
485     unsigned Count;
486     unsigned LaneIndex;
487     bool isDoubleSpaced;
488   };
489 
490   struct VectorIndexOp {
491     unsigned Val;
492   };
493 
494   struct ImmOp {
495     const MCExpr *Val;
496   };
497 
498   /// Combined record for all forms of ARM address expressions.
499   struct MemoryOp {
500     unsigned BaseRegNum;
501     // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
502     // was specified.
503     const MCConstantExpr *OffsetImm;  // Offset immediate value
504     unsigned OffsetRegNum;    // Offset register num, when OffsetImm == NULL
505     ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
506     unsigned ShiftImm;        // shift for OffsetReg.
507     unsigned Alignment;       // 0 = no alignment specified
508     // n = alignment in bytes (2, 4, 8, 16, or 32)
509     unsigned isNegative : 1;  // Negated OffsetReg? (~'U' bit)
510   };
511 
512   struct PostIdxRegOp {
513     unsigned RegNum;
514     bool isAdd;
515     ARM_AM::ShiftOpc ShiftTy;
516     unsigned ShiftImm;
517   };
518 
519   struct ShifterImmOp {
520     bool isASR;
521     unsigned Imm;
522   };
523 
524   struct RegShiftedRegOp {
525     ARM_AM::ShiftOpc ShiftTy;
526     unsigned SrcReg;
527     unsigned ShiftReg;
528     unsigned ShiftImm;
529   };
530 
531   struct RegShiftedImmOp {
532     ARM_AM::ShiftOpc ShiftTy;
533     unsigned SrcReg;
534     unsigned ShiftImm;
535   };
536 
537   struct RotImmOp {
538     unsigned Imm;
539   };
540 
541   struct ModImmOp {
542     unsigned Bits;
543     unsigned Rot;
544   };
545 
546   struct BitfieldOp {
547     unsigned LSB;
548     unsigned Width;
549   };
550 
551   union {
552     struct CCOp CC;
553     struct CopOp Cop;
554     struct CoprocOptionOp CoprocOption;
555     struct MBOptOp MBOpt;
556     struct ISBOptOp ISBOpt;
557     struct ITMaskOp ITMask;
558     struct IFlagsOp IFlags;
559     struct MMaskOp MMask;
560     struct BankedRegOp BankedReg;
561     struct TokOp Tok;
562     struct RegOp Reg;
563     struct VectorListOp VectorList;
564     struct VectorIndexOp VectorIndex;
565     struct ImmOp Imm;
566     struct MemoryOp Memory;
567     struct PostIdxRegOp PostIdxReg;
568     struct ShifterImmOp ShifterImm;
569     struct RegShiftedRegOp RegShiftedReg;
570     struct RegShiftedImmOp RegShiftedImm;
571     struct RotImmOp RotImm;
572     struct ModImmOp ModImm;
573     struct BitfieldOp Bitfield;
574   };
575 
576 public:
577   ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
578 
579   /// getStartLoc - Get the location of the first token of this operand.
580   SMLoc getStartLoc() const override { return StartLoc; }
581   /// getEndLoc - Get the location of the last token of this operand.
582   SMLoc getEndLoc() const override { return EndLoc; }
583   /// getLocRange - Get the range between the first and last token of this
584   /// operand.
585   SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
586 
587   /// getAlignmentLoc - Get the location of the Alignment token of this operand.
588   SMLoc getAlignmentLoc() const {
589     assert(Kind == k_Memory && "Invalid access!");
590     return AlignmentLoc;
591   }
592 
593   ARMCC::CondCodes getCondCode() const {
594     assert(Kind == k_CondCode && "Invalid access!");
595     return CC.Val;
596   }
597 
598   unsigned getCoproc() const {
599     assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
600     return Cop.Val;
601   }
602 
603   StringRef getToken() const {
604     assert(Kind == k_Token && "Invalid access!");
605     return StringRef(Tok.Data, Tok.Length);
606   }
607 
608   unsigned getReg() const override {
609     assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
610     return Reg.RegNum;
611   }
612 
613   const SmallVectorImpl<unsigned> &getRegList() const {
614     assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
615             Kind == k_SPRRegisterList) && "Invalid access!");
616     return Registers;
617   }
618 
619   const MCExpr *getImm() const {
620     assert(isImm() && "Invalid access!");
621     return Imm.Val;
622   }
623 
624   unsigned getVectorIndex() const {
625     assert(Kind == k_VectorIndex && "Invalid access!");
626     return VectorIndex.Val;
627   }
628 
629   ARM_MB::MemBOpt getMemBarrierOpt() const {
630     assert(Kind == k_MemBarrierOpt && "Invalid access!");
631     return MBOpt.Val;
632   }
633 
634   ARM_ISB::InstSyncBOpt getInstSyncBarrierOpt() const {
635     assert(Kind == k_InstSyncBarrierOpt && "Invalid access!");
636     return ISBOpt.Val;
637   }
638 
639   ARM_PROC::IFlags getProcIFlags() const {
640     assert(Kind == k_ProcIFlags && "Invalid access!");
641     return IFlags.Val;
642   }
643 
644   unsigned getMSRMask() const {
645     assert(Kind == k_MSRMask && "Invalid access!");
646     return MMask.Val;
647   }
648 
649   unsigned getBankedReg() const {
650     assert(Kind == k_BankedReg && "Invalid access!");
651     return BankedReg.Val;
652   }
653 
654   bool isCoprocNum() const { return Kind == k_CoprocNum; }
655   bool isCoprocReg() const { return Kind == k_CoprocReg; }
656   bool isCoprocOption() const { return Kind == k_CoprocOption; }
657   bool isCondCode() const { return Kind == k_CondCode; }
658   bool isCCOut() const { return Kind == k_CCOut; }
659   bool isITMask() const { return Kind == k_ITCondMask; }
660   bool isITCondCode() const { return Kind == k_CondCode; }
661   bool isImm() const override { return Kind == k_Immediate; }
662   // checks whether this operand is an unsigned offset which fits is a field
663   // of specified width and scaled by a specific number of bits
664   template<unsigned width, unsigned scale>
665   bool isUnsignedOffset() const {
666     if (!isImm()) return false;
667     if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
668     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
669       int64_t Val = CE->getValue();
670       int64_t Align = 1LL << scale;
671       int64_t Max = Align * ((1LL << width) - 1);
672       return ((Val % Align) == 0) && (Val >= 0) && (Val <= Max);
673     }
674     return false;
675   }
676   // checks whether this operand is an signed offset which fits is a field
677   // of specified width and scaled by a specific number of bits
678   template<unsigned width, unsigned scale>
679   bool isSignedOffset() const {
680     if (!isImm()) return false;
681     if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
682     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
683       int64_t Val = CE->getValue();
684       int64_t Align = 1LL << scale;
685       int64_t Max = Align * ((1LL << (width-1)) - 1);
686       int64_t Min = -Align * (1LL << (width-1));
687       return ((Val % Align) == 0) && (Val >= Min) && (Val <= Max);
688     }
689     return false;
690   }
691 
692   // checks whether this operand is a memory operand computed as an offset
693   // applied to PC. the offset may have 8 bits of magnitude and is represented
694   // with two bits of shift. textually it may be either [pc, #imm], #imm or
695   // relocable expression...
696   bool isThumbMemPC() const {
697     int64_t Val = 0;
698     if (isImm()) {
699       if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
700       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val);
701       if (!CE) return false;
702       Val = CE->getValue();
703     }
704     else if (isMem()) {
705       if(!Memory.OffsetImm || Memory.OffsetRegNum) return false;
706       if(Memory.BaseRegNum != ARM::PC) return false;
707       Val = Memory.OffsetImm->getValue();
708     }
709     else return false;
710     return ((Val % 4) == 0) && (Val >= 0) && (Val <= 1020);
711   }
712   bool isFPImm() const {
713     if (!isImm()) return false;
714     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
715     if (!CE) return false;
716     int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
717     return Val != -1;
718   }
719   bool isFBits16() const {
720     if (!isImm()) return false;
721     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
722     if (!CE) return false;
723     int64_t Value = CE->getValue();
724     return Value >= 0 && Value <= 16;
725   }
726   bool isFBits32() const {
727     if (!isImm()) return false;
728     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
729     if (!CE) return false;
730     int64_t Value = CE->getValue();
731     return Value >= 1 && Value <= 32;
732   }
733   bool isImm8s4() const {
734     if (!isImm()) return false;
735     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
736     if (!CE) return false;
737     int64_t Value = CE->getValue();
738     return ((Value & 3) == 0) && Value >= -1020 && Value <= 1020;
739   }
740   bool isImm0_1020s4() const {
741     if (!isImm()) return false;
742     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
743     if (!CE) return false;
744     int64_t Value = CE->getValue();
745     return ((Value & 3) == 0) && Value >= 0 && Value <= 1020;
746   }
747   bool isImm0_508s4() const {
748     if (!isImm()) return false;
749     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
750     if (!CE) return false;
751     int64_t Value = CE->getValue();
752     return ((Value & 3) == 0) && Value >= 0 && Value <= 508;
753   }
754   bool isImm0_508s4Neg() const {
755     if (!isImm()) return false;
756     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
757     if (!CE) return false;
758     int64_t Value = -CE->getValue();
759     // explicitly exclude zero. we want that to use the normal 0_508 version.
760     return ((Value & 3) == 0) && Value > 0 && Value <= 508;
761   }
762   bool isImm0_239() const {
763     if (!isImm()) return false;
764     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
765     if (!CE) return false;
766     int64_t Value = CE->getValue();
767     return Value >= 0 && Value < 240;
768   }
769   bool isImm0_255() const {
770     if (!isImm()) return false;
771     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
772     if (!CE) return false;
773     int64_t Value = CE->getValue();
774     return Value >= 0 && Value < 256;
775   }
776   bool isImm0_4095() const {
777     if (!isImm()) return false;
778     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
779     if (!CE) return false;
780     int64_t Value = CE->getValue();
781     return Value >= 0 && Value < 4096;
782   }
783   bool isImm0_4095Neg() const {
784     if (!isImm()) return false;
785     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
786     if (!CE) return false;
787     int64_t Value = -CE->getValue();
788     return Value > 0 && Value < 4096;
789   }
790   bool isImm0_1() const {
791     if (!isImm()) return false;
792     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
793     if (!CE) return false;
794     int64_t Value = CE->getValue();
795     return Value >= 0 && Value < 2;
796   }
797   bool isImm0_3() const {
798     if (!isImm()) return false;
799     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
800     if (!CE) return false;
801     int64_t Value = CE->getValue();
802     return Value >= 0 && Value < 4;
803   }
804   bool isImm0_7() const {
805     if (!isImm()) return false;
806     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
807     if (!CE) return false;
808     int64_t Value = CE->getValue();
809     return Value >= 0 && Value < 8;
810   }
811   bool isImm0_15() const {
812     if (!isImm()) return false;
813     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
814     if (!CE) return false;
815     int64_t Value = CE->getValue();
816     return Value >= 0 && Value < 16;
817   }
818   bool isImm0_31() const {
819     if (!isImm()) return false;
820     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
821     if (!CE) return false;
822     int64_t Value = CE->getValue();
823     return Value >= 0 && Value < 32;
824   }
825   bool isImm0_63() const {
826     if (!isImm()) return false;
827     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
828     if (!CE) return false;
829     int64_t Value = CE->getValue();
830     return Value >= 0 && Value < 64;
831   }
832   bool isImm8() const {
833     if (!isImm()) return false;
834     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
835     if (!CE) return false;
836     int64_t Value = CE->getValue();
837     return Value == 8;
838   }
839   bool isImm16() const {
840     if (!isImm()) return false;
841     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
842     if (!CE) return false;
843     int64_t Value = CE->getValue();
844     return Value == 16;
845   }
846   bool isImm32() const {
847     if (!isImm()) return false;
848     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
849     if (!CE) return false;
850     int64_t Value = CE->getValue();
851     return Value == 32;
852   }
853   bool isShrImm8() const {
854     if (!isImm()) return false;
855     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
856     if (!CE) return false;
857     int64_t Value = CE->getValue();
858     return Value > 0 && Value <= 8;
859   }
860   bool isShrImm16() const {
861     if (!isImm()) return false;
862     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
863     if (!CE) return false;
864     int64_t Value = CE->getValue();
865     return Value > 0 && Value <= 16;
866   }
867   bool isShrImm32() const {
868     if (!isImm()) return false;
869     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
870     if (!CE) return false;
871     int64_t Value = CE->getValue();
872     return Value > 0 && Value <= 32;
873   }
874   bool isShrImm64() const {
875     if (!isImm()) return false;
876     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
877     if (!CE) return false;
878     int64_t Value = CE->getValue();
879     return Value > 0 && Value <= 64;
880   }
881   bool isImm1_7() const {
882     if (!isImm()) return false;
883     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
884     if (!CE) return false;
885     int64_t Value = CE->getValue();
886     return Value > 0 && Value < 8;
887   }
888   bool isImm1_15() const {
889     if (!isImm()) return false;
890     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
891     if (!CE) return false;
892     int64_t Value = CE->getValue();
893     return Value > 0 && Value < 16;
894   }
895   bool isImm1_31() const {
896     if (!isImm()) return false;
897     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
898     if (!CE) return false;
899     int64_t Value = CE->getValue();
900     return Value > 0 && Value < 32;
901   }
902   bool isImm1_16() const {
903     if (!isImm()) return false;
904     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
905     if (!CE) return false;
906     int64_t Value = CE->getValue();
907     return Value > 0 && Value < 17;
908   }
909   bool isImm1_32() const {
910     if (!isImm()) return false;
911     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
912     if (!CE) return false;
913     int64_t Value = CE->getValue();
914     return Value > 0 && Value < 33;
915   }
916   bool isImm0_32() const {
917     if (!isImm()) return false;
918     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
919     if (!CE) return false;
920     int64_t Value = CE->getValue();
921     return Value >= 0 && Value < 33;
922   }
923   bool isImm0_65535() const {
924     if (!isImm()) return false;
925     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
926     if (!CE) return false;
927     int64_t Value = CE->getValue();
928     return Value >= 0 && Value < 65536;
929   }
930   bool isImm256_65535Expr() const {
931     if (!isImm()) return false;
932     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
933     // If it's not a constant expression, it'll generate a fixup and be
934     // handled later.
935     if (!CE) return true;
936     int64_t Value = CE->getValue();
937     return Value >= 256 && Value < 65536;
938   }
939   bool isImm0_65535Expr() const {
940     if (!isImm()) return false;
941     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
942     // If it's not a constant expression, it'll generate a fixup and be
943     // handled later.
944     if (!CE) return true;
945     int64_t Value = CE->getValue();
946     return Value >= 0 && Value < 65536;
947   }
948   bool isImm24bit() const {
949     if (!isImm()) return false;
950     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
951     if (!CE) return false;
952     int64_t Value = CE->getValue();
953     return Value >= 0 && Value <= 0xffffff;
954   }
955   bool isImmThumbSR() const {
956     if (!isImm()) return false;
957     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
958     if (!CE) return false;
959     int64_t Value = CE->getValue();
960     return Value > 0 && Value < 33;
961   }
962   bool isPKHLSLImm() const {
963     if (!isImm()) return false;
964     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
965     if (!CE) return false;
966     int64_t Value = CE->getValue();
967     return Value >= 0 && Value < 32;
968   }
969   bool isPKHASRImm() const {
970     if (!isImm()) return false;
971     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
972     if (!CE) return false;
973     int64_t Value = CE->getValue();
974     return Value > 0 && Value <= 32;
975   }
976   bool isAdrLabel() const {
977     // If we have an immediate that's not a constant, treat it as a label
978     // reference needing a fixup.
979     if (isImm() && !isa<MCConstantExpr>(getImm()))
980       return true;
981 
982     // If it is a constant, it must fit into a modified immediate encoding.
983     if (!isImm()) return false;
984     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
985     if (!CE) return false;
986     int64_t Value = CE->getValue();
987     return (ARM_AM::getSOImmVal(Value) != -1 ||
988             ARM_AM::getSOImmVal(-Value) != -1);
989   }
990   bool isT2SOImm() const {
991     if (!isImm()) return false;
992     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
993     if (!CE) return false;
994     int64_t Value = CE->getValue();
995     return ARM_AM::getT2SOImmVal(Value) != -1;
996   }
997   bool isT2SOImmNot() const {
998     if (!isImm()) return false;
999     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1000     if (!CE) return false;
1001     int64_t Value = CE->getValue();
1002     return ARM_AM::getT2SOImmVal(Value) == -1 &&
1003       ARM_AM::getT2SOImmVal(~Value) != -1;
1004   }
1005   bool isT2SOImmNeg() const {
1006     if (!isImm()) return false;
1007     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1008     if (!CE) return false;
1009     int64_t Value = CE->getValue();
1010     // Only use this when not representable as a plain so_imm.
1011     return ARM_AM::getT2SOImmVal(Value) == -1 &&
1012       ARM_AM::getT2SOImmVal(-Value) != -1;
1013   }
1014   bool isSetEndImm() 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 Value == 1 || Value == 0;
1020   }
1021   bool isReg() const override { return Kind == k_Register; }
1022   bool isRegList() const { return Kind == k_RegisterList; }
1023   bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
1024   bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
1025   bool isToken() const override { return Kind == k_Token; }
1026   bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
1027   bool isInstSyncBarrierOpt() const { return Kind == k_InstSyncBarrierOpt; }
1028   bool isMem() const override { return Kind == k_Memory; }
1029   bool isShifterImm() const { return Kind == k_ShifterImmediate; }
1030   bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
1031   bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
1032   bool isRotImm() const { return Kind == k_RotateImmediate; }
1033   bool isModImm() const { return Kind == k_ModifiedImmediate; }
1034   bool isModImmNot() const {
1035     if (!isImm()) return false;
1036     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1037     if (!CE) return false;
1038     int64_t Value = CE->getValue();
1039     return ARM_AM::getSOImmVal(~Value) != -1;
1040   }
1041   bool isModImmNeg() const {
1042     if (!isImm()) return false;
1043     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1044     if (!CE) return false;
1045     int64_t Value = CE->getValue();
1046     return ARM_AM::getSOImmVal(Value) == -1 &&
1047       ARM_AM::getSOImmVal(-Value) != -1;
1048   }
1049   bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
1050   bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
1051   bool isPostIdxReg() const {
1052     return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy ==ARM_AM::no_shift;
1053   }
1054   bool isMemNoOffset(bool alignOK = false, unsigned Alignment = 0) const {
1055     if (!isMem())
1056       return false;
1057     // No offset of any kind.
1058     return Memory.OffsetRegNum == 0 && Memory.OffsetImm == nullptr &&
1059      (alignOK || Memory.Alignment == Alignment);
1060   }
1061   bool isMemPCRelImm12() const {
1062     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1063       return false;
1064     // Base register must be PC.
1065     if (Memory.BaseRegNum != ARM::PC)
1066       return false;
1067     // Immediate offset in range [-4095, 4095].
1068     if (!Memory.OffsetImm) return true;
1069     int64_t Val = Memory.OffsetImm->getValue();
1070     return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
1071   }
1072   bool isAlignedMemory() const {
1073     return isMemNoOffset(true);
1074   }
1075   bool isAlignedMemoryNone() const {
1076     return isMemNoOffset(false, 0);
1077   }
1078   bool isDupAlignedMemoryNone() const {
1079     return isMemNoOffset(false, 0);
1080   }
1081   bool isAlignedMemory16() const {
1082     if (isMemNoOffset(false, 2)) // alignment in bytes for 16-bits is 2.
1083       return true;
1084     return isMemNoOffset(false, 0);
1085   }
1086   bool isDupAlignedMemory16() const {
1087     if (isMemNoOffset(false, 2)) // alignment in bytes for 16-bits is 2.
1088       return true;
1089     return isMemNoOffset(false, 0);
1090   }
1091   bool isAlignedMemory32() const {
1092     if (isMemNoOffset(false, 4)) // alignment in bytes for 32-bits is 4.
1093       return true;
1094     return isMemNoOffset(false, 0);
1095   }
1096   bool isDupAlignedMemory32() const {
1097     if (isMemNoOffset(false, 4)) // alignment in bytes for 32-bits is 4.
1098       return true;
1099     return isMemNoOffset(false, 0);
1100   }
1101   bool isAlignedMemory64() const {
1102     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1103       return true;
1104     return isMemNoOffset(false, 0);
1105   }
1106   bool isDupAlignedMemory64() const {
1107     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1108       return true;
1109     return isMemNoOffset(false, 0);
1110   }
1111   bool isAlignedMemory64or128() const {
1112     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1113       return true;
1114     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1115       return true;
1116     return isMemNoOffset(false, 0);
1117   }
1118   bool isDupAlignedMemory64or128() const {
1119     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1120       return true;
1121     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1122       return true;
1123     return isMemNoOffset(false, 0);
1124   }
1125   bool isAlignedMemory64or128or256() const {
1126     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1127       return true;
1128     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1129       return true;
1130     if (isMemNoOffset(false, 32)) // alignment in bytes for 256-bits is 32.
1131       return true;
1132     return isMemNoOffset(false, 0);
1133   }
1134   bool isAddrMode2() const {
1135     if (!isMem() || Memory.Alignment != 0) return false;
1136     // Check for register offset.
1137     if (Memory.OffsetRegNum) return true;
1138     // Immediate offset in range [-4095, 4095].
1139     if (!Memory.OffsetImm) return true;
1140     int64_t Val = Memory.OffsetImm->getValue();
1141     return Val > -4096 && Val < 4096;
1142   }
1143   bool isAM2OffsetImm() const {
1144     if (!isImm()) return false;
1145     // Immediate offset in range [-4095, 4095].
1146     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1147     if (!CE) return false;
1148     int64_t Val = CE->getValue();
1149     return (Val == INT32_MIN) || (Val > -4096 && Val < 4096);
1150   }
1151   bool isAddrMode3() const {
1152     // If we have an immediate that's not a constant, treat it as a label
1153     // reference needing a fixup. If it is a constant, it's something else
1154     // and we reject it.
1155     if (isImm() && !isa<MCConstantExpr>(getImm()))
1156       return true;
1157     if (!isMem() || Memory.Alignment != 0) return false;
1158     // No shifts are legal for AM3.
1159     if (Memory.ShiftType != ARM_AM::no_shift) return false;
1160     // Check for register offset.
1161     if (Memory.OffsetRegNum) return true;
1162     // Immediate offset in range [-255, 255].
1163     if (!Memory.OffsetImm) return true;
1164     int64_t Val = Memory.OffsetImm->getValue();
1165     // The #-0 offset is encoded as INT32_MIN, and we have to check
1166     // for this too.
1167     return (Val > -256 && Val < 256) || Val == INT32_MIN;
1168   }
1169   bool isAM3Offset() const {
1170     if (Kind != k_Immediate && Kind != k_PostIndexRegister)
1171       return false;
1172     if (Kind == k_PostIndexRegister)
1173       return PostIdxReg.ShiftTy == ARM_AM::no_shift;
1174     // Immediate offset in range [-255, 255].
1175     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1176     if (!CE) return false;
1177     int64_t Val = CE->getValue();
1178     // Special case, #-0 is INT32_MIN.
1179     return (Val > -256 && Val < 256) || Val == INT32_MIN;
1180   }
1181   bool isAddrMode5() const {
1182     // If we have an immediate that's not a constant, treat it as a label
1183     // reference needing a fixup. If it is a constant, it's something else
1184     // and we reject it.
1185     if (isImm() && !isa<MCConstantExpr>(getImm()))
1186       return true;
1187     if (!isMem() || Memory.Alignment != 0) return false;
1188     // Check for register offset.
1189     if (Memory.OffsetRegNum) return false;
1190     // Immediate offset in range [-1020, 1020] and a multiple of 4.
1191     if (!Memory.OffsetImm) return true;
1192     int64_t Val = Memory.OffsetImm->getValue();
1193     return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
1194       Val == INT32_MIN;
1195   }
1196   bool isAddrMode5FP16() const {
1197     // If we have an immediate that's not a constant, treat it as a label
1198     // reference needing a fixup. If it is a constant, it's something else
1199     // and we reject it.
1200     if (isImm() && !isa<MCConstantExpr>(getImm()))
1201       return true;
1202     if (!isMem() || Memory.Alignment != 0) return false;
1203     // Check for register offset.
1204     if (Memory.OffsetRegNum) return false;
1205     // Immediate offset in range [-510, 510] and a multiple of 2.
1206     if (!Memory.OffsetImm) return true;
1207     int64_t Val = Memory.OffsetImm->getValue();
1208     return (Val >= -510 && Val <= 510 && ((Val & 1) == 0)) || Val == INT32_MIN;
1209   }
1210   bool isMemTBB() const {
1211     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1212         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
1213       return false;
1214     return true;
1215   }
1216   bool isMemTBH() const {
1217     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1218         Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
1219         Memory.Alignment != 0 )
1220       return false;
1221     return true;
1222   }
1223   bool isMemRegOffset() const {
1224     if (!isMem() || !Memory.OffsetRegNum || Memory.Alignment != 0)
1225       return false;
1226     return true;
1227   }
1228   bool isT2MemRegOffset() const {
1229     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1230         Memory.Alignment != 0 || Memory.BaseRegNum == ARM::PC)
1231       return false;
1232     // Only lsl #{0, 1, 2, 3} allowed.
1233     if (Memory.ShiftType == ARM_AM::no_shift)
1234       return true;
1235     if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
1236       return false;
1237     return true;
1238   }
1239   bool isMemThumbRR() const {
1240     // Thumb reg+reg addressing is simple. Just two registers, a base and
1241     // an offset. No shifts, negations or any other complicating factors.
1242     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1243         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
1244       return false;
1245     return isARMLowRegister(Memory.BaseRegNum) &&
1246       (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
1247   }
1248   bool isMemThumbRIs4() const {
1249     if (!isMem() || Memory.OffsetRegNum != 0 ||
1250         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1251       return false;
1252     // Immediate offset, multiple of 4 in range [0, 124].
1253     if (!Memory.OffsetImm) return true;
1254     int64_t Val = Memory.OffsetImm->getValue();
1255     return Val >= 0 && Val <= 124 && (Val % 4) == 0;
1256   }
1257   bool isMemThumbRIs2() const {
1258     if (!isMem() || Memory.OffsetRegNum != 0 ||
1259         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1260       return false;
1261     // Immediate offset, multiple of 4 in range [0, 62].
1262     if (!Memory.OffsetImm) return true;
1263     int64_t Val = Memory.OffsetImm->getValue();
1264     return Val >= 0 && Val <= 62 && (Val % 2) == 0;
1265   }
1266   bool isMemThumbRIs1() const {
1267     if (!isMem() || Memory.OffsetRegNum != 0 ||
1268         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1269       return false;
1270     // Immediate offset in range [0, 31].
1271     if (!Memory.OffsetImm) return true;
1272     int64_t Val = Memory.OffsetImm->getValue();
1273     return Val >= 0 && Val <= 31;
1274   }
1275   bool isMemThumbSPI() const {
1276     if (!isMem() || Memory.OffsetRegNum != 0 ||
1277         Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
1278       return false;
1279     // Immediate offset, multiple of 4 in range [0, 1020].
1280     if (!Memory.OffsetImm) return true;
1281     int64_t Val = Memory.OffsetImm->getValue();
1282     return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
1283   }
1284   bool isMemImm8s4Offset() const {
1285     // If we have an immediate that's not a constant, treat it as a label
1286     // reference needing a fixup. If it is a constant, it's something else
1287     // and we reject it.
1288     if (isImm() && !isa<MCConstantExpr>(getImm()))
1289       return true;
1290     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1291       return false;
1292     // Immediate offset a multiple of 4 in range [-1020, 1020].
1293     if (!Memory.OffsetImm) return true;
1294     int64_t Val = Memory.OffsetImm->getValue();
1295     // Special case, #-0 is INT32_MIN.
1296     return (Val >= -1020 && Val <= 1020 && (Val & 3) == 0) || Val == INT32_MIN;
1297   }
1298   bool isMemImm0_1020s4Offset() const {
1299     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1300       return false;
1301     // Immediate offset a multiple of 4 in range [0, 1020].
1302     if (!Memory.OffsetImm) return true;
1303     int64_t Val = Memory.OffsetImm->getValue();
1304     return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
1305   }
1306   bool isMemImm8Offset() const {
1307     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1308       return false;
1309     // Base reg of PC isn't allowed for these encodings.
1310     if (Memory.BaseRegNum == ARM::PC) return false;
1311     // Immediate offset in range [-255, 255].
1312     if (!Memory.OffsetImm) return true;
1313     int64_t Val = Memory.OffsetImm->getValue();
1314     return (Val == INT32_MIN) || (Val > -256 && Val < 256);
1315   }
1316   bool isMemPosImm8Offset() const {
1317     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1318       return false;
1319     // Immediate offset in range [0, 255].
1320     if (!Memory.OffsetImm) return true;
1321     int64_t Val = Memory.OffsetImm->getValue();
1322     return Val >= 0 && Val < 256;
1323   }
1324   bool isMemNegImm8Offset() 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, -1].
1330     if (!Memory.OffsetImm) return false;
1331     int64_t Val = Memory.OffsetImm->getValue();
1332     return (Val == INT32_MIN) || (Val > -256 && Val < 0);
1333   }
1334   bool isMemUImm12Offset() const {
1335     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1336       return false;
1337     // Immediate offset in range [0, 4095].
1338     if (!Memory.OffsetImm) return true;
1339     int64_t Val = Memory.OffsetImm->getValue();
1340     return (Val >= 0 && Val < 4096);
1341   }
1342   bool isMemImm12Offset() const {
1343     // If we have an immediate that's not a constant, treat it as a label
1344     // reference needing a fixup. If it is a constant, it's something else
1345     // and we reject it.
1346     if (isImm() && !isa<MCConstantExpr>(getImm()))
1347       return true;
1348 
1349     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1350       return false;
1351     // Immediate offset in range [-4095, 4095].
1352     if (!Memory.OffsetImm) return true;
1353     int64_t Val = Memory.OffsetImm->getValue();
1354     return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
1355   }
1356   bool isPostIdxImm8() const {
1357     if (!isImm()) return false;
1358     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1359     if (!CE) return false;
1360     int64_t Val = CE->getValue();
1361     return (Val > -256 && Val < 256) || (Val == INT32_MIN);
1362   }
1363   bool isPostIdxImm8s4() const {
1364     if (!isImm()) return false;
1365     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1366     if (!CE) return false;
1367     int64_t Val = CE->getValue();
1368     return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
1369       (Val == INT32_MIN);
1370   }
1371 
1372   bool isMSRMask() const { return Kind == k_MSRMask; }
1373   bool isBankedReg() const { return Kind == k_BankedReg; }
1374   bool isProcIFlags() const { return Kind == k_ProcIFlags; }
1375 
1376   // NEON operands.
1377   bool isSingleSpacedVectorList() const {
1378     return Kind == k_VectorList && !VectorList.isDoubleSpaced;
1379   }
1380   bool isDoubleSpacedVectorList() const {
1381     return Kind == k_VectorList && VectorList.isDoubleSpaced;
1382   }
1383   bool isVecListOneD() const {
1384     if (!isSingleSpacedVectorList()) return false;
1385     return VectorList.Count == 1;
1386   }
1387 
1388   bool isVecListDPair() const {
1389     if (!isSingleSpacedVectorList()) return false;
1390     return (ARMMCRegisterClasses[ARM::DPairRegClassID]
1391               .contains(VectorList.RegNum));
1392   }
1393 
1394   bool isVecListThreeD() const {
1395     if (!isSingleSpacedVectorList()) return false;
1396     return VectorList.Count == 3;
1397   }
1398 
1399   bool isVecListFourD() const {
1400     if (!isSingleSpacedVectorList()) return false;
1401     return VectorList.Count == 4;
1402   }
1403 
1404   bool isVecListDPairSpaced() const {
1405     if (Kind != k_VectorList) return false;
1406     if (isSingleSpacedVectorList()) return false;
1407     return (ARMMCRegisterClasses[ARM::DPairSpcRegClassID]
1408               .contains(VectorList.RegNum));
1409   }
1410 
1411   bool isVecListThreeQ() const {
1412     if (!isDoubleSpacedVectorList()) return false;
1413     return VectorList.Count == 3;
1414   }
1415 
1416   bool isVecListFourQ() const {
1417     if (!isDoubleSpacedVectorList()) return false;
1418     return VectorList.Count == 4;
1419   }
1420 
1421   bool isSingleSpacedVectorAllLanes() const {
1422     return Kind == k_VectorListAllLanes && !VectorList.isDoubleSpaced;
1423   }
1424   bool isDoubleSpacedVectorAllLanes() const {
1425     return Kind == k_VectorListAllLanes && VectorList.isDoubleSpaced;
1426   }
1427   bool isVecListOneDAllLanes() const {
1428     if (!isSingleSpacedVectorAllLanes()) return false;
1429     return VectorList.Count == 1;
1430   }
1431 
1432   bool isVecListDPairAllLanes() const {
1433     if (!isSingleSpacedVectorAllLanes()) return false;
1434     return (ARMMCRegisterClasses[ARM::DPairRegClassID]
1435               .contains(VectorList.RegNum));
1436   }
1437 
1438   bool isVecListDPairSpacedAllLanes() const {
1439     if (!isDoubleSpacedVectorAllLanes()) return false;
1440     return VectorList.Count == 2;
1441   }
1442 
1443   bool isVecListThreeDAllLanes() const {
1444     if (!isSingleSpacedVectorAllLanes()) return false;
1445     return VectorList.Count == 3;
1446   }
1447 
1448   bool isVecListThreeQAllLanes() const {
1449     if (!isDoubleSpacedVectorAllLanes()) return false;
1450     return VectorList.Count == 3;
1451   }
1452 
1453   bool isVecListFourDAllLanes() const {
1454     if (!isSingleSpacedVectorAllLanes()) return false;
1455     return VectorList.Count == 4;
1456   }
1457 
1458   bool isVecListFourQAllLanes() const {
1459     if (!isDoubleSpacedVectorAllLanes()) return false;
1460     return VectorList.Count == 4;
1461   }
1462 
1463   bool isSingleSpacedVectorIndexed() const {
1464     return Kind == k_VectorListIndexed && !VectorList.isDoubleSpaced;
1465   }
1466   bool isDoubleSpacedVectorIndexed() const {
1467     return Kind == k_VectorListIndexed && VectorList.isDoubleSpaced;
1468   }
1469   bool isVecListOneDByteIndexed() const {
1470     if (!isSingleSpacedVectorIndexed()) return false;
1471     return VectorList.Count == 1 && VectorList.LaneIndex <= 7;
1472   }
1473 
1474   bool isVecListOneDHWordIndexed() const {
1475     if (!isSingleSpacedVectorIndexed()) return false;
1476     return VectorList.Count == 1 && VectorList.LaneIndex <= 3;
1477   }
1478 
1479   bool isVecListOneDWordIndexed() const {
1480     if (!isSingleSpacedVectorIndexed()) return false;
1481     return VectorList.Count == 1 && VectorList.LaneIndex <= 1;
1482   }
1483 
1484   bool isVecListTwoDByteIndexed() const {
1485     if (!isSingleSpacedVectorIndexed()) return false;
1486     return VectorList.Count == 2 && VectorList.LaneIndex <= 7;
1487   }
1488 
1489   bool isVecListTwoDHWordIndexed() const {
1490     if (!isSingleSpacedVectorIndexed()) return false;
1491     return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
1492   }
1493 
1494   bool isVecListTwoQWordIndexed() const {
1495     if (!isDoubleSpacedVectorIndexed()) return false;
1496     return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
1497   }
1498 
1499   bool isVecListTwoQHWordIndexed() const {
1500     if (!isDoubleSpacedVectorIndexed()) return false;
1501     return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
1502   }
1503 
1504   bool isVecListTwoDWordIndexed() const {
1505     if (!isSingleSpacedVectorIndexed()) return false;
1506     return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
1507   }
1508 
1509   bool isVecListThreeDByteIndexed() const {
1510     if (!isSingleSpacedVectorIndexed()) return false;
1511     return VectorList.Count == 3 && VectorList.LaneIndex <= 7;
1512   }
1513 
1514   bool isVecListThreeDHWordIndexed() const {
1515     if (!isSingleSpacedVectorIndexed()) return false;
1516     return VectorList.Count == 3 && VectorList.LaneIndex <= 3;
1517   }
1518 
1519   bool isVecListThreeQWordIndexed() const {
1520     if (!isDoubleSpacedVectorIndexed()) return false;
1521     return VectorList.Count == 3 && VectorList.LaneIndex <= 1;
1522   }
1523 
1524   bool isVecListThreeQHWordIndexed() const {
1525     if (!isDoubleSpacedVectorIndexed()) return false;
1526     return VectorList.Count == 3 && VectorList.LaneIndex <= 3;
1527   }
1528 
1529   bool isVecListThreeDWordIndexed() const {
1530     if (!isSingleSpacedVectorIndexed()) return false;
1531     return VectorList.Count == 3 && VectorList.LaneIndex <= 1;
1532   }
1533 
1534   bool isVecListFourDByteIndexed() const {
1535     if (!isSingleSpacedVectorIndexed()) return false;
1536     return VectorList.Count == 4 && VectorList.LaneIndex <= 7;
1537   }
1538 
1539   bool isVecListFourDHWordIndexed() const {
1540     if (!isSingleSpacedVectorIndexed()) return false;
1541     return VectorList.Count == 4 && VectorList.LaneIndex <= 3;
1542   }
1543 
1544   bool isVecListFourQWordIndexed() const {
1545     if (!isDoubleSpacedVectorIndexed()) return false;
1546     return VectorList.Count == 4 && VectorList.LaneIndex <= 1;
1547   }
1548 
1549   bool isVecListFourQHWordIndexed() const {
1550     if (!isDoubleSpacedVectorIndexed()) return false;
1551     return VectorList.Count == 4 && VectorList.LaneIndex <= 3;
1552   }
1553 
1554   bool isVecListFourDWordIndexed() const {
1555     if (!isSingleSpacedVectorIndexed()) return false;
1556     return VectorList.Count == 4 && VectorList.LaneIndex <= 1;
1557   }
1558 
1559   bool isVectorIndex8() const {
1560     if (Kind != k_VectorIndex) return false;
1561     return VectorIndex.Val < 8;
1562   }
1563   bool isVectorIndex16() const {
1564     if (Kind != k_VectorIndex) return false;
1565     return VectorIndex.Val < 4;
1566   }
1567   bool isVectorIndex32() const {
1568     if (Kind != k_VectorIndex) return false;
1569     return VectorIndex.Val < 2;
1570   }
1571 
1572   bool isNEONi8splat() const {
1573     if (!isImm()) return false;
1574     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1575     // Must be a constant.
1576     if (!CE) return false;
1577     int64_t Value = CE->getValue();
1578     // i8 value splatted across 8 bytes. The immediate is just the 8 byte
1579     // value.
1580     return Value >= 0 && Value < 256;
1581   }
1582 
1583   bool isNEONi16splat() const {
1584     if (isNEONByteReplicate(2))
1585       return false; // Leave that for bytes replication and forbid by default.
1586     if (!isImm())
1587       return false;
1588     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1589     // Must be a constant.
1590     if (!CE) return false;
1591     unsigned Value = CE->getValue();
1592     return ARM_AM::isNEONi16splat(Value);
1593   }
1594 
1595   bool isNEONi16splatNot() const {
1596     if (!isImm())
1597       return false;
1598     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1599     // Must be a constant.
1600     if (!CE) return false;
1601     unsigned Value = CE->getValue();
1602     return ARM_AM::isNEONi16splat(~Value & 0xffff);
1603   }
1604 
1605   bool isNEONi32splat() const {
1606     if (isNEONByteReplicate(4))
1607       return false; // Leave that for bytes replication and forbid by default.
1608     if (!isImm())
1609       return false;
1610     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1611     // Must be a constant.
1612     if (!CE) return false;
1613     unsigned Value = CE->getValue();
1614     return ARM_AM::isNEONi32splat(Value);
1615   }
1616 
1617   bool isNEONi32splatNot() const {
1618     if (!isImm())
1619       return false;
1620     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1621     // Must be a constant.
1622     if (!CE) return false;
1623     unsigned Value = CE->getValue();
1624     return ARM_AM::isNEONi32splat(~Value);
1625   }
1626 
1627   bool isNEONByteReplicate(unsigned NumBytes) const {
1628     if (!isImm())
1629       return false;
1630     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1631     // Must be a constant.
1632     if (!CE)
1633       return false;
1634     int64_t Value = CE->getValue();
1635     if (!Value)
1636       return false; // Don't bother with zero.
1637 
1638     unsigned char B = Value & 0xff;
1639     for (unsigned i = 1; i < NumBytes; ++i) {
1640       Value >>= 8;
1641       if ((Value & 0xff) != B)
1642         return false;
1643     }
1644     return true;
1645   }
1646   bool isNEONi16ByteReplicate() const { return isNEONByteReplicate(2); }
1647   bool isNEONi32ByteReplicate() const { return isNEONByteReplicate(4); }
1648   bool isNEONi32vmov() const {
1649     if (isNEONByteReplicate(4))
1650       return false; // Let it to be classified as byte-replicate case.
1651     if (!isImm())
1652       return false;
1653     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1654     // Must be a constant.
1655     if (!CE)
1656       return false;
1657     int64_t Value = CE->getValue();
1658     // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1659     // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1660     // FIXME: This is probably wrong and a copy and paste from previous example
1661     return (Value >= 0 && Value < 256) ||
1662       (Value >= 0x0100 && Value <= 0xff00) ||
1663       (Value >= 0x010000 && Value <= 0xff0000) ||
1664       (Value >= 0x01000000 && Value <= 0xff000000) ||
1665       (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1666       (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1667   }
1668   bool isNEONi32vmovNeg() const {
1669     if (!isImm()) return false;
1670     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1671     // Must be a constant.
1672     if (!CE) return false;
1673     int64_t Value = ~CE->getValue();
1674     // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1675     // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1676     // FIXME: This is probably wrong and a copy and paste from previous example
1677     return (Value >= 0 && Value < 256) ||
1678       (Value >= 0x0100 && Value <= 0xff00) ||
1679       (Value >= 0x010000 && Value <= 0xff0000) ||
1680       (Value >= 0x01000000 && Value <= 0xff000000) ||
1681       (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1682       (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1683   }
1684 
1685   bool isNEONi64splat() const {
1686     if (!isImm()) return false;
1687     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1688     // Must be a constant.
1689     if (!CE) return false;
1690     uint64_t Value = CE->getValue();
1691     // i64 value with each byte being either 0 or 0xff.
1692     for (unsigned i = 0; i < 8; ++i)
1693       if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
1694     return true;
1695   }
1696 
1697   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
1698     // Add as immediates when possible.  Null MCExpr = 0.
1699     if (!Expr)
1700       Inst.addOperand(MCOperand::createImm(0));
1701     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
1702       Inst.addOperand(MCOperand::createImm(CE->getValue()));
1703     else
1704       Inst.addOperand(MCOperand::createExpr(Expr));
1705   }
1706 
1707   void addCondCodeOperands(MCInst &Inst, unsigned N) const {
1708     assert(N == 2 && "Invalid number of operands!");
1709     Inst.addOperand(MCOperand::createImm(unsigned(getCondCode())));
1710     unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
1711     Inst.addOperand(MCOperand::createReg(RegNum));
1712   }
1713 
1714   void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
1715     assert(N == 1 && "Invalid number of operands!");
1716     Inst.addOperand(MCOperand::createImm(getCoproc()));
1717   }
1718 
1719   void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
1720     assert(N == 1 && "Invalid number of operands!");
1721     Inst.addOperand(MCOperand::createImm(getCoproc()));
1722   }
1723 
1724   void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
1725     assert(N == 1 && "Invalid number of operands!");
1726     Inst.addOperand(MCOperand::createImm(CoprocOption.Val));
1727   }
1728 
1729   void addITMaskOperands(MCInst &Inst, unsigned N) const {
1730     assert(N == 1 && "Invalid number of operands!");
1731     Inst.addOperand(MCOperand::createImm(ITMask.Mask));
1732   }
1733 
1734   void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
1735     assert(N == 1 && "Invalid number of operands!");
1736     Inst.addOperand(MCOperand::createImm(unsigned(getCondCode())));
1737   }
1738 
1739   void addCCOutOperands(MCInst &Inst, unsigned N) const {
1740     assert(N == 1 && "Invalid number of operands!");
1741     Inst.addOperand(MCOperand::createReg(getReg()));
1742   }
1743 
1744   void addRegOperands(MCInst &Inst, unsigned N) const {
1745     assert(N == 1 && "Invalid number of operands!");
1746     Inst.addOperand(MCOperand::createReg(getReg()));
1747   }
1748 
1749   void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
1750     assert(N == 3 && "Invalid number of operands!");
1751     assert(isRegShiftedReg() &&
1752            "addRegShiftedRegOperands() on non-RegShiftedReg!");
1753     Inst.addOperand(MCOperand::createReg(RegShiftedReg.SrcReg));
1754     Inst.addOperand(MCOperand::createReg(RegShiftedReg.ShiftReg));
1755     Inst.addOperand(MCOperand::createImm(
1756       ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
1757   }
1758 
1759   void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
1760     assert(N == 2 && "Invalid number of operands!");
1761     assert(isRegShiftedImm() &&
1762            "addRegShiftedImmOperands() on non-RegShiftedImm!");
1763     Inst.addOperand(MCOperand::createReg(RegShiftedImm.SrcReg));
1764     // Shift of #32 is encoded as 0 where permitted
1765     unsigned Imm = (RegShiftedImm.ShiftImm == 32 ? 0 : RegShiftedImm.ShiftImm);
1766     Inst.addOperand(MCOperand::createImm(
1767       ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, Imm)));
1768   }
1769 
1770   void addShifterImmOperands(MCInst &Inst, unsigned N) const {
1771     assert(N == 1 && "Invalid number of operands!");
1772     Inst.addOperand(MCOperand::createImm((ShifterImm.isASR << 5) |
1773                                          ShifterImm.Imm));
1774   }
1775 
1776   void addRegListOperands(MCInst &Inst, unsigned N) const {
1777     assert(N == 1 && "Invalid number of operands!");
1778     const SmallVectorImpl<unsigned> &RegList = getRegList();
1779     for (SmallVectorImpl<unsigned>::const_iterator
1780            I = RegList.begin(), E = RegList.end(); I != E; ++I)
1781       Inst.addOperand(MCOperand::createReg(*I));
1782   }
1783 
1784   void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
1785     addRegListOperands(Inst, N);
1786   }
1787 
1788   void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
1789     addRegListOperands(Inst, N);
1790   }
1791 
1792   void addRotImmOperands(MCInst &Inst, unsigned N) const {
1793     assert(N == 1 && "Invalid number of operands!");
1794     // Encoded as val>>3. The printer handles display as 8, 16, 24.
1795     Inst.addOperand(MCOperand::createImm(RotImm.Imm >> 3));
1796   }
1797 
1798   void addModImmOperands(MCInst &Inst, unsigned N) const {
1799     assert(N == 1 && "Invalid number of operands!");
1800 
1801     // Support for fixups (MCFixup)
1802     if (isImm())
1803       return addImmOperands(Inst, N);
1804 
1805     Inst.addOperand(MCOperand::createImm(ModImm.Bits | (ModImm.Rot << 7)));
1806   }
1807 
1808   void addModImmNotOperands(MCInst &Inst, unsigned N) const {
1809     assert(N == 1 && "Invalid number of operands!");
1810     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1811     uint32_t Enc = ARM_AM::getSOImmVal(~CE->getValue());
1812     Inst.addOperand(MCOperand::createImm(Enc));
1813   }
1814 
1815   void addModImmNegOperands(MCInst &Inst, unsigned N) const {
1816     assert(N == 1 && "Invalid number of operands!");
1817     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1818     uint32_t Enc = ARM_AM::getSOImmVal(-CE->getValue());
1819     Inst.addOperand(MCOperand::createImm(Enc));
1820   }
1821 
1822   void addBitfieldOperands(MCInst &Inst, unsigned N) const {
1823     assert(N == 1 && "Invalid number of operands!");
1824     // Munge the lsb/width into a bitfield mask.
1825     unsigned lsb = Bitfield.LSB;
1826     unsigned width = Bitfield.Width;
1827     // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
1828     uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
1829                       (32 - (lsb + width)));
1830     Inst.addOperand(MCOperand::createImm(Mask));
1831   }
1832 
1833   void addImmOperands(MCInst &Inst, unsigned N) const {
1834     assert(N == 1 && "Invalid number of operands!");
1835     addExpr(Inst, getImm());
1836   }
1837 
1838   void addFBits16Operands(MCInst &Inst, unsigned N) const {
1839     assert(N == 1 && "Invalid number of operands!");
1840     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1841     Inst.addOperand(MCOperand::createImm(16 - CE->getValue()));
1842   }
1843 
1844   void addFBits32Operands(MCInst &Inst, unsigned N) const {
1845     assert(N == 1 && "Invalid number of operands!");
1846     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1847     Inst.addOperand(MCOperand::createImm(32 - CE->getValue()));
1848   }
1849 
1850   void addFPImmOperands(MCInst &Inst, unsigned N) const {
1851     assert(N == 1 && "Invalid number of operands!");
1852     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1853     int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
1854     Inst.addOperand(MCOperand::createImm(Val));
1855   }
1856 
1857   void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1858     assert(N == 1 && "Invalid number of operands!");
1859     // FIXME: We really want to scale the value here, but the LDRD/STRD
1860     // instruction don't encode operands that way yet.
1861     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1862     Inst.addOperand(MCOperand::createImm(CE->getValue()));
1863   }
1864 
1865   void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1866     assert(N == 1 && "Invalid number of operands!");
1867     // The immediate is scaled by four in the encoding and is stored
1868     // in the MCInst as such. Lop off the low two bits here.
1869     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1870     Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
1871   }
1872 
1873   void addImm0_508s4NegOperands(MCInst &Inst, unsigned N) const {
1874     assert(N == 1 && "Invalid number of operands!");
1875     // The immediate is scaled by four in the encoding and is stored
1876     // in the MCInst as such. Lop off the low two bits here.
1877     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1878     Inst.addOperand(MCOperand::createImm(-(CE->getValue() / 4)));
1879   }
1880 
1881   void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1882     assert(N == 1 && "Invalid number of operands!");
1883     // The immediate is scaled by four in the encoding and is stored
1884     // in the MCInst as such. Lop off the low two bits here.
1885     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1886     Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
1887   }
1888 
1889   void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1890     assert(N == 1 && "Invalid number of operands!");
1891     // The constant encodes as the immediate-1, and we store in the instruction
1892     // the bits as encoded, so subtract off one here.
1893     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1894     Inst.addOperand(MCOperand::createImm(CE->getValue() - 1));
1895   }
1896 
1897   void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1898     assert(N == 1 && "Invalid number of operands!");
1899     // The constant encodes as the immediate-1, and we store in the instruction
1900     // the bits as encoded, so subtract off one here.
1901     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1902     Inst.addOperand(MCOperand::createImm(CE->getValue() - 1));
1903   }
1904 
1905   void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
1906     assert(N == 1 && "Invalid number of operands!");
1907     // The constant encodes as the immediate, except for 32, which encodes as
1908     // zero.
1909     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1910     unsigned Imm = CE->getValue();
1911     Inst.addOperand(MCOperand::createImm((Imm == 32 ? 0 : Imm)));
1912   }
1913 
1914   void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
1915     assert(N == 1 && "Invalid number of operands!");
1916     // An ASR value of 32 encodes as 0, so that's how we want to add it to
1917     // the instruction as well.
1918     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1919     int Val = CE->getValue();
1920     Inst.addOperand(MCOperand::createImm(Val == 32 ? 0 : Val));
1921   }
1922 
1923   void addT2SOImmNotOperands(MCInst &Inst, unsigned N) const {
1924     assert(N == 1 && "Invalid number of operands!");
1925     // The operand is actually a t2_so_imm, but we have its bitwise
1926     // negation in the assembly source, so twiddle it here.
1927     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1928     Inst.addOperand(MCOperand::createImm(~CE->getValue()));
1929   }
1930 
1931   void addT2SOImmNegOperands(MCInst &Inst, unsigned N) const {
1932     assert(N == 1 && "Invalid number of operands!");
1933     // The operand is actually a t2_so_imm, but we have its
1934     // negation in the assembly source, so twiddle it here.
1935     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1936     Inst.addOperand(MCOperand::createImm(-CE->getValue()));
1937   }
1938 
1939   void addImm0_4095NegOperands(MCInst &Inst, unsigned N) const {
1940     assert(N == 1 && "Invalid number of operands!");
1941     // The operand is actually an imm0_4095, but we have its
1942     // negation in the assembly source, so twiddle it here.
1943     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1944     Inst.addOperand(MCOperand::createImm(-CE->getValue()));
1945   }
1946 
1947   void addUnsignedOffset_b8s2Operands(MCInst &Inst, unsigned N) const {
1948     if(const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
1949       Inst.addOperand(MCOperand::createImm(CE->getValue() >> 2));
1950       return;
1951     }
1952 
1953     const MCSymbolRefExpr *SR = dyn_cast<MCSymbolRefExpr>(Imm.Val);
1954     assert(SR && "Unknown value type!");
1955     Inst.addOperand(MCOperand::createExpr(SR));
1956   }
1957 
1958   void addThumbMemPCOperands(MCInst &Inst, unsigned N) const {
1959     assert(N == 1 && "Invalid number of operands!");
1960     if (isImm()) {
1961       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1962       if (CE) {
1963         Inst.addOperand(MCOperand::createImm(CE->getValue()));
1964         return;
1965       }
1966 
1967       const MCSymbolRefExpr *SR = dyn_cast<MCSymbolRefExpr>(Imm.Val);
1968       assert(SR && "Unknown value type!");
1969       Inst.addOperand(MCOperand::createExpr(SR));
1970       return;
1971     }
1972 
1973     assert(isMem()  && "Unknown value type!");
1974     assert(isa<MCConstantExpr>(Memory.OffsetImm) && "Unknown value type!");
1975     Inst.addOperand(MCOperand::createImm(Memory.OffsetImm->getValue()));
1976   }
1977 
1978   void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
1979     assert(N == 1 && "Invalid number of operands!");
1980     Inst.addOperand(MCOperand::createImm(unsigned(getMemBarrierOpt())));
1981   }
1982 
1983   void addInstSyncBarrierOptOperands(MCInst &Inst, unsigned N) const {
1984     assert(N == 1 && "Invalid number of operands!");
1985     Inst.addOperand(MCOperand::createImm(unsigned(getInstSyncBarrierOpt())));
1986   }
1987 
1988   void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
1989     assert(N == 1 && "Invalid number of operands!");
1990     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
1991   }
1992 
1993   void addMemPCRelImm12Operands(MCInst &Inst, unsigned N) const {
1994     assert(N == 1 && "Invalid number of operands!");
1995     int32_t Imm = Memory.OffsetImm->getValue();
1996     Inst.addOperand(MCOperand::createImm(Imm));
1997   }
1998 
1999   void addAdrLabelOperands(MCInst &Inst, unsigned N) const {
2000     assert(N == 1 && "Invalid number of operands!");
2001     assert(isImm() && "Not an immediate!");
2002 
2003     // If we have an immediate that's not a constant, treat it as a label
2004     // reference needing a fixup.
2005     if (!isa<MCConstantExpr>(getImm())) {
2006       Inst.addOperand(MCOperand::createExpr(getImm()));
2007       return;
2008     }
2009 
2010     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2011     int Val = CE->getValue();
2012     Inst.addOperand(MCOperand::createImm(Val));
2013   }
2014 
2015   void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
2016     assert(N == 2 && "Invalid number of operands!");
2017     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2018     Inst.addOperand(MCOperand::createImm(Memory.Alignment));
2019   }
2020 
2021   void addDupAlignedMemoryNoneOperands(MCInst &Inst, unsigned N) const {
2022     addAlignedMemoryOperands(Inst, N);
2023   }
2024 
2025   void addAlignedMemoryNoneOperands(MCInst &Inst, unsigned N) const {
2026     addAlignedMemoryOperands(Inst, N);
2027   }
2028 
2029   void addAlignedMemory16Operands(MCInst &Inst, unsigned N) const {
2030     addAlignedMemoryOperands(Inst, N);
2031   }
2032 
2033   void addDupAlignedMemory16Operands(MCInst &Inst, unsigned N) const {
2034     addAlignedMemoryOperands(Inst, N);
2035   }
2036 
2037   void addAlignedMemory32Operands(MCInst &Inst, unsigned N) const {
2038     addAlignedMemoryOperands(Inst, N);
2039   }
2040 
2041   void addDupAlignedMemory32Operands(MCInst &Inst, unsigned N) const {
2042     addAlignedMemoryOperands(Inst, N);
2043   }
2044 
2045   void addAlignedMemory64Operands(MCInst &Inst, unsigned N) const {
2046     addAlignedMemoryOperands(Inst, N);
2047   }
2048 
2049   void addDupAlignedMemory64Operands(MCInst &Inst, unsigned N) const {
2050     addAlignedMemoryOperands(Inst, N);
2051   }
2052 
2053   void addAlignedMemory64or128Operands(MCInst &Inst, unsigned N) const {
2054     addAlignedMemoryOperands(Inst, N);
2055   }
2056 
2057   void addDupAlignedMemory64or128Operands(MCInst &Inst, unsigned N) const {
2058     addAlignedMemoryOperands(Inst, N);
2059   }
2060 
2061   void addAlignedMemory64or128or256Operands(MCInst &Inst, unsigned N) const {
2062     addAlignedMemoryOperands(Inst, N);
2063   }
2064 
2065   void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
2066     assert(N == 3 && "Invalid number of operands!");
2067     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2068     if (!Memory.OffsetRegNum) {
2069       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2070       // Special case for #-0
2071       if (Val == INT32_MIN) Val = 0;
2072       if (Val < 0) Val = -Val;
2073       Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
2074     } else {
2075       // For register offset, we encode the shift type and negation flag
2076       // here.
2077       Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
2078                               Memory.ShiftImm, Memory.ShiftType);
2079     }
2080     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2081     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2082     Inst.addOperand(MCOperand::createImm(Val));
2083   }
2084 
2085   void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
2086     assert(N == 2 && "Invalid number of operands!");
2087     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2088     assert(CE && "non-constant AM2OffsetImm operand!");
2089     int32_t Val = CE->getValue();
2090     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2091     // Special case for #-0
2092     if (Val == INT32_MIN) Val = 0;
2093     if (Val < 0) Val = -Val;
2094     Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
2095     Inst.addOperand(MCOperand::createReg(0));
2096     Inst.addOperand(MCOperand::createImm(Val));
2097   }
2098 
2099   void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
2100     assert(N == 3 && "Invalid number of operands!");
2101     // If we have an immediate that's not a constant, treat it as a label
2102     // reference needing a fixup. If it is a constant, it's something else
2103     // and we reject it.
2104     if (isImm()) {
2105       Inst.addOperand(MCOperand::createExpr(getImm()));
2106       Inst.addOperand(MCOperand::createReg(0));
2107       Inst.addOperand(MCOperand::createImm(0));
2108       return;
2109     }
2110 
2111     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2112     if (!Memory.OffsetRegNum) {
2113       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2114       // Special case for #-0
2115       if (Val == INT32_MIN) Val = 0;
2116       if (Val < 0) Val = -Val;
2117       Val = ARM_AM::getAM3Opc(AddSub, Val);
2118     } else {
2119       // For register offset, we encode the shift type and negation flag
2120       // here.
2121       Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
2122     }
2123     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2124     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2125     Inst.addOperand(MCOperand::createImm(Val));
2126   }
2127 
2128   void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
2129     assert(N == 2 && "Invalid number of operands!");
2130     if (Kind == k_PostIndexRegister) {
2131       int32_t Val =
2132         ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
2133       Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
2134       Inst.addOperand(MCOperand::createImm(Val));
2135       return;
2136     }
2137 
2138     // Constant offset.
2139     const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
2140     int32_t Val = CE->getValue();
2141     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2142     // Special case for #-0
2143     if (Val == INT32_MIN) Val = 0;
2144     if (Val < 0) Val = -Val;
2145     Val = ARM_AM::getAM3Opc(AddSub, Val);
2146     Inst.addOperand(MCOperand::createReg(0));
2147     Inst.addOperand(MCOperand::createImm(Val));
2148   }
2149 
2150   void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
2151     assert(N == 2 && "Invalid number of operands!");
2152     // If we have an immediate that's not a constant, treat it as a label
2153     // reference needing a fixup. If it is a constant, it's something else
2154     // and we reject it.
2155     if (isImm()) {
2156       Inst.addOperand(MCOperand::createExpr(getImm()));
2157       Inst.addOperand(MCOperand::createImm(0));
2158       return;
2159     }
2160 
2161     // The lower two bits are always zero and as such are not encoded.
2162     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
2163     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2164     // Special case for #-0
2165     if (Val == INT32_MIN) Val = 0;
2166     if (Val < 0) Val = -Val;
2167     Val = ARM_AM::getAM5Opc(AddSub, Val);
2168     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2169     Inst.addOperand(MCOperand::createImm(Val));
2170   }
2171 
2172   void addAddrMode5FP16Operands(MCInst &Inst, unsigned N) const {
2173     assert(N == 2 && "Invalid number of operands!");
2174     // If we have an immediate that's not a constant, treat it as a label
2175     // reference needing a fixup. If it is a constant, it's something else
2176     // and we reject it.
2177     if (isImm()) {
2178       Inst.addOperand(MCOperand::createExpr(getImm()));
2179       Inst.addOperand(MCOperand::createImm(0));
2180       return;
2181     }
2182 
2183     // The lower bit is always zero and as such is not encoded.
2184     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 2 : 0;
2185     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2186     // Special case for #-0
2187     if (Val == INT32_MIN) Val = 0;
2188     if (Val < 0) Val = -Val;
2189     Val = ARM_AM::getAM5FP16Opc(AddSub, Val);
2190     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2191     Inst.addOperand(MCOperand::createImm(Val));
2192   }
2193 
2194   void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
2195     assert(N == 2 && "Invalid number of operands!");
2196     // If we have an immediate that's not a constant, treat it as a label
2197     // reference needing a fixup. If it is a constant, it's something else
2198     // and we reject it.
2199     if (isImm()) {
2200       Inst.addOperand(MCOperand::createExpr(getImm()));
2201       Inst.addOperand(MCOperand::createImm(0));
2202       return;
2203     }
2204 
2205     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2206     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2207     Inst.addOperand(MCOperand::createImm(Val));
2208   }
2209 
2210   void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
2211     assert(N == 2 && "Invalid number of operands!");
2212     // The lower two bits are always zero and as such are not encoded.
2213     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
2214     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2215     Inst.addOperand(MCOperand::createImm(Val));
2216   }
2217 
2218   void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
2219     assert(N == 2 && "Invalid number of operands!");
2220     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2221     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2222     Inst.addOperand(MCOperand::createImm(Val));
2223   }
2224 
2225   void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
2226     addMemImm8OffsetOperands(Inst, N);
2227   }
2228 
2229   void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
2230     addMemImm8OffsetOperands(Inst, N);
2231   }
2232 
2233   void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
2234     assert(N == 2 && "Invalid number of operands!");
2235     // If this is an immediate, it's a label reference.
2236     if (isImm()) {
2237       addExpr(Inst, getImm());
2238       Inst.addOperand(MCOperand::createImm(0));
2239       return;
2240     }
2241 
2242     // Otherwise, it's a normal memory reg+offset.
2243     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2244     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2245     Inst.addOperand(MCOperand::createImm(Val));
2246   }
2247 
2248   void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
2249     assert(N == 2 && "Invalid number of operands!");
2250     // If this is an immediate, it's a label reference.
2251     if (isImm()) {
2252       addExpr(Inst, getImm());
2253       Inst.addOperand(MCOperand::createImm(0));
2254       return;
2255     }
2256 
2257     // Otherwise, it's a normal memory reg+offset.
2258     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2259     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2260     Inst.addOperand(MCOperand::createImm(Val));
2261   }
2262 
2263   void addMemTBBOperands(MCInst &Inst, unsigned N) const {
2264     assert(N == 2 && "Invalid number of operands!");
2265     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2266     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2267   }
2268 
2269   void addMemTBHOperands(MCInst &Inst, unsigned N) const {
2270     assert(N == 2 && "Invalid number of operands!");
2271     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2272     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2273   }
2274 
2275   void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
2276     assert(N == 3 && "Invalid number of operands!");
2277     unsigned Val =
2278       ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
2279                         Memory.ShiftImm, Memory.ShiftType);
2280     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2281     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2282     Inst.addOperand(MCOperand::createImm(Val));
2283   }
2284 
2285   void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
2286     assert(N == 3 && "Invalid number of operands!");
2287     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2288     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2289     Inst.addOperand(MCOperand::createImm(Memory.ShiftImm));
2290   }
2291 
2292   void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
2293     assert(N == 2 && "Invalid number of operands!");
2294     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2295     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2296   }
2297 
2298   void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
2299     assert(N == 2 && "Invalid number of operands!");
2300     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
2301     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2302     Inst.addOperand(MCOperand::createImm(Val));
2303   }
2304 
2305   void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
2306     assert(N == 2 && "Invalid number of operands!");
2307     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
2308     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2309     Inst.addOperand(MCOperand::createImm(Val));
2310   }
2311 
2312   void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
2313     assert(N == 2 && "Invalid number of operands!");
2314     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
2315     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2316     Inst.addOperand(MCOperand::createImm(Val));
2317   }
2318 
2319   void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
2320     assert(N == 2 && "Invalid number of operands!");
2321     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
2322     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2323     Inst.addOperand(MCOperand::createImm(Val));
2324   }
2325 
2326   void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
2327     assert(N == 1 && "Invalid number of operands!");
2328     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2329     assert(CE && "non-constant post-idx-imm8 operand!");
2330     int Imm = CE->getValue();
2331     bool isAdd = Imm >= 0;
2332     if (Imm == INT32_MIN) Imm = 0;
2333     Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
2334     Inst.addOperand(MCOperand::createImm(Imm));
2335   }
2336 
2337   void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
2338     assert(N == 1 && "Invalid number of operands!");
2339     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2340     assert(CE && "non-constant post-idx-imm8s4 operand!");
2341     int Imm = CE->getValue();
2342     bool isAdd = Imm >= 0;
2343     if (Imm == INT32_MIN) Imm = 0;
2344     // Immediate is scaled by 4.
2345     Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
2346     Inst.addOperand(MCOperand::createImm(Imm));
2347   }
2348 
2349   void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
2350     assert(N == 2 && "Invalid number of operands!");
2351     Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
2352     Inst.addOperand(MCOperand::createImm(PostIdxReg.isAdd));
2353   }
2354 
2355   void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
2356     assert(N == 2 && "Invalid number of operands!");
2357     Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
2358     // The sign, shift type, and shift amount are encoded in a single operand
2359     // using the AM2 encoding helpers.
2360     ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
2361     unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
2362                                      PostIdxReg.ShiftTy);
2363     Inst.addOperand(MCOperand::createImm(Imm));
2364   }
2365 
2366   void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
2367     assert(N == 1 && "Invalid number of operands!");
2368     Inst.addOperand(MCOperand::createImm(unsigned(getMSRMask())));
2369   }
2370 
2371   void addBankedRegOperands(MCInst &Inst, unsigned N) const {
2372     assert(N == 1 && "Invalid number of operands!");
2373     Inst.addOperand(MCOperand::createImm(unsigned(getBankedReg())));
2374   }
2375 
2376   void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
2377     assert(N == 1 && "Invalid number of operands!");
2378     Inst.addOperand(MCOperand::createImm(unsigned(getProcIFlags())));
2379   }
2380 
2381   void addVecListOperands(MCInst &Inst, unsigned N) const {
2382     assert(N == 1 && "Invalid number of operands!");
2383     Inst.addOperand(MCOperand::createReg(VectorList.RegNum));
2384   }
2385 
2386   void addVecListIndexedOperands(MCInst &Inst, unsigned N) const {
2387     assert(N == 2 && "Invalid number of operands!");
2388     Inst.addOperand(MCOperand::createReg(VectorList.RegNum));
2389     Inst.addOperand(MCOperand::createImm(VectorList.LaneIndex));
2390   }
2391 
2392   void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
2393     assert(N == 1 && "Invalid number of operands!");
2394     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
2395   }
2396 
2397   void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
2398     assert(N == 1 && "Invalid number of operands!");
2399     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
2400   }
2401 
2402   void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
2403     assert(N == 1 && "Invalid number of operands!");
2404     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
2405   }
2406 
2407   void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
2408     assert(N == 1 && "Invalid number of operands!");
2409     // The immediate encodes the type of constant as well as the value.
2410     // Mask in that this is an i8 splat.
2411     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2412     Inst.addOperand(MCOperand::createImm(CE->getValue() | 0xe00));
2413   }
2414 
2415   void addNEONi16splatOperands(MCInst &Inst, unsigned N) const {
2416     assert(N == 1 && "Invalid number of operands!");
2417     // The immediate encodes the type of constant as well as the value.
2418     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2419     unsigned Value = CE->getValue();
2420     Value = ARM_AM::encodeNEONi16splat(Value);
2421     Inst.addOperand(MCOperand::createImm(Value));
2422   }
2423 
2424   void addNEONi16splatNotOperands(MCInst &Inst, unsigned N) const {
2425     assert(N == 1 && "Invalid number of operands!");
2426     // The immediate encodes the type of constant as well as the value.
2427     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2428     unsigned Value = CE->getValue();
2429     Value = ARM_AM::encodeNEONi16splat(~Value & 0xffff);
2430     Inst.addOperand(MCOperand::createImm(Value));
2431   }
2432 
2433   void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
2434     assert(N == 1 && "Invalid number of operands!");
2435     // The immediate encodes the type of constant as well as the value.
2436     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2437     unsigned Value = CE->getValue();
2438     Value = ARM_AM::encodeNEONi32splat(Value);
2439     Inst.addOperand(MCOperand::createImm(Value));
2440   }
2441 
2442   void addNEONi32splatNotOperands(MCInst &Inst, unsigned N) const {
2443     assert(N == 1 && "Invalid number of operands!");
2444     // The immediate encodes the type of constant as well as the value.
2445     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2446     unsigned Value = CE->getValue();
2447     Value = ARM_AM::encodeNEONi32splat(~Value);
2448     Inst.addOperand(MCOperand::createImm(Value));
2449   }
2450 
2451   void addNEONinvByteReplicateOperands(MCInst &Inst, unsigned N) const {
2452     assert(N == 1 && "Invalid number of operands!");
2453     // The immediate encodes the type of constant as well as the value.
2454     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2455     unsigned Value = CE->getValue();
2456     assert((Inst.getOpcode() == ARM::VMOVv8i8 ||
2457             Inst.getOpcode() == ARM::VMOVv16i8) &&
2458            "All vmvn instructions that wants to replicate non-zero byte "
2459            "always must be replaced with VMOVv8i8 or VMOVv16i8.");
2460     unsigned B = ((~Value) & 0xff);
2461     B |= 0xe00; // cmode = 0b1110
2462     Inst.addOperand(MCOperand::createImm(B));
2463   }
2464   void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
2465     assert(N == 1 && "Invalid number of operands!");
2466     // The immediate encodes the type of constant as well as the value.
2467     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2468     unsigned Value = CE->getValue();
2469     if (Value >= 256 && Value <= 0xffff)
2470       Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
2471     else if (Value > 0xffff && Value <= 0xffffff)
2472       Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
2473     else if (Value > 0xffffff)
2474       Value = (Value >> 24) | 0x600;
2475     Inst.addOperand(MCOperand::createImm(Value));
2476   }
2477 
2478   void addNEONvmovByteReplicateOperands(MCInst &Inst, unsigned N) const {
2479     assert(N == 1 && "Invalid number of operands!");
2480     // The immediate encodes the type of constant as well as the value.
2481     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2482     unsigned Value = CE->getValue();
2483     assert((Inst.getOpcode() == ARM::VMOVv8i8 ||
2484             Inst.getOpcode() == ARM::VMOVv16i8) &&
2485            "All instructions that wants to replicate non-zero byte "
2486            "always must be replaced with VMOVv8i8 or VMOVv16i8.");
2487     unsigned B = Value & 0xff;
2488     B |= 0xe00; // cmode = 0b1110
2489     Inst.addOperand(MCOperand::createImm(B));
2490   }
2491   void addNEONi32vmovNegOperands(MCInst &Inst, unsigned N) const {
2492     assert(N == 1 && "Invalid number of operands!");
2493     // The immediate encodes the type of constant as well as the value.
2494     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2495     unsigned Value = ~CE->getValue();
2496     if (Value >= 256 && Value <= 0xffff)
2497       Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
2498     else if (Value > 0xffff && Value <= 0xffffff)
2499       Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
2500     else if (Value > 0xffffff)
2501       Value = (Value >> 24) | 0x600;
2502     Inst.addOperand(MCOperand::createImm(Value));
2503   }
2504 
2505   void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
2506     assert(N == 1 && "Invalid number of operands!");
2507     // The immediate encodes the type of constant as well as the value.
2508     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2509     uint64_t Value = CE->getValue();
2510     unsigned Imm = 0;
2511     for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
2512       Imm |= (Value & 1) << i;
2513     }
2514     Inst.addOperand(MCOperand::createImm(Imm | 0x1e00));
2515   }
2516 
2517   void print(raw_ostream &OS) const override;
2518 
2519   static std::unique_ptr<ARMOperand> CreateITMask(unsigned Mask, SMLoc S) {
2520     auto Op = make_unique<ARMOperand>(k_ITCondMask);
2521     Op->ITMask.Mask = Mask;
2522     Op->StartLoc = S;
2523     Op->EndLoc = S;
2524     return Op;
2525   }
2526 
2527   static std::unique_ptr<ARMOperand> CreateCondCode(ARMCC::CondCodes CC,
2528                                                     SMLoc S) {
2529     auto Op = make_unique<ARMOperand>(k_CondCode);
2530     Op->CC.Val = CC;
2531     Op->StartLoc = S;
2532     Op->EndLoc = S;
2533     return Op;
2534   }
2535 
2536   static std::unique_ptr<ARMOperand> CreateCoprocNum(unsigned CopVal, SMLoc S) {
2537     auto Op = make_unique<ARMOperand>(k_CoprocNum);
2538     Op->Cop.Val = CopVal;
2539     Op->StartLoc = S;
2540     Op->EndLoc = S;
2541     return Op;
2542   }
2543 
2544   static std::unique_ptr<ARMOperand> CreateCoprocReg(unsigned CopVal, SMLoc S) {
2545     auto Op = make_unique<ARMOperand>(k_CoprocReg);
2546     Op->Cop.Val = CopVal;
2547     Op->StartLoc = S;
2548     Op->EndLoc = S;
2549     return Op;
2550   }
2551 
2552   static std::unique_ptr<ARMOperand> CreateCoprocOption(unsigned Val, SMLoc S,
2553                                                         SMLoc E) {
2554     auto Op = make_unique<ARMOperand>(k_CoprocOption);
2555     Op->Cop.Val = Val;
2556     Op->StartLoc = S;
2557     Op->EndLoc = E;
2558     return Op;
2559   }
2560 
2561   static std::unique_ptr<ARMOperand> CreateCCOut(unsigned RegNum, SMLoc S) {
2562     auto Op = make_unique<ARMOperand>(k_CCOut);
2563     Op->Reg.RegNum = RegNum;
2564     Op->StartLoc = S;
2565     Op->EndLoc = S;
2566     return Op;
2567   }
2568 
2569   static std::unique_ptr<ARMOperand> CreateToken(StringRef Str, SMLoc S) {
2570     auto Op = make_unique<ARMOperand>(k_Token);
2571     Op->Tok.Data = Str.data();
2572     Op->Tok.Length = Str.size();
2573     Op->StartLoc = S;
2574     Op->EndLoc = S;
2575     return Op;
2576   }
2577 
2578   static std::unique_ptr<ARMOperand> CreateReg(unsigned RegNum, SMLoc S,
2579                                                SMLoc E) {
2580     auto Op = make_unique<ARMOperand>(k_Register);
2581     Op->Reg.RegNum = RegNum;
2582     Op->StartLoc = S;
2583     Op->EndLoc = E;
2584     return Op;
2585   }
2586 
2587   static std::unique_ptr<ARMOperand>
2588   CreateShiftedRegister(ARM_AM::ShiftOpc ShTy, unsigned SrcReg,
2589                         unsigned ShiftReg, unsigned ShiftImm, SMLoc S,
2590                         SMLoc E) {
2591     auto Op = make_unique<ARMOperand>(k_ShiftedRegister);
2592     Op->RegShiftedReg.ShiftTy = ShTy;
2593     Op->RegShiftedReg.SrcReg = SrcReg;
2594     Op->RegShiftedReg.ShiftReg = ShiftReg;
2595     Op->RegShiftedReg.ShiftImm = ShiftImm;
2596     Op->StartLoc = S;
2597     Op->EndLoc = E;
2598     return Op;
2599   }
2600 
2601   static std::unique_ptr<ARMOperand>
2602   CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy, unsigned SrcReg,
2603                          unsigned ShiftImm, SMLoc S, SMLoc E) {
2604     auto Op = make_unique<ARMOperand>(k_ShiftedImmediate);
2605     Op->RegShiftedImm.ShiftTy = ShTy;
2606     Op->RegShiftedImm.SrcReg = SrcReg;
2607     Op->RegShiftedImm.ShiftImm = ShiftImm;
2608     Op->StartLoc = S;
2609     Op->EndLoc = E;
2610     return Op;
2611   }
2612 
2613   static std::unique_ptr<ARMOperand> CreateShifterImm(bool isASR, unsigned Imm,
2614                                                       SMLoc S, SMLoc E) {
2615     auto Op = make_unique<ARMOperand>(k_ShifterImmediate);
2616     Op->ShifterImm.isASR = isASR;
2617     Op->ShifterImm.Imm = Imm;
2618     Op->StartLoc = S;
2619     Op->EndLoc = E;
2620     return Op;
2621   }
2622 
2623   static std::unique_ptr<ARMOperand> CreateRotImm(unsigned Imm, SMLoc S,
2624                                                   SMLoc E) {
2625     auto Op = make_unique<ARMOperand>(k_RotateImmediate);
2626     Op->RotImm.Imm = Imm;
2627     Op->StartLoc = S;
2628     Op->EndLoc = E;
2629     return Op;
2630   }
2631 
2632   static std::unique_ptr<ARMOperand> CreateModImm(unsigned Bits, unsigned Rot,
2633                                                   SMLoc S, SMLoc E) {
2634     auto Op = make_unique<ARMOperand>(k_ModifiedImmediate);
2635     Op->ModImm.Bits = Bits;
2636     Op->ModImm.Rot = Rot;
2637     Op->StartLoc = S;
2638     Op->EndLoc = E;
2639     return Op;
2640   }
2641 
2642   static std::unique_ptr<ARMOperand>
2643   CreateBitfield(unsigned LSB, unsigned Width, SMLoc S, SMLoc E) {
2644     auto Op = make_unique<ARMOperand>(k_BitfieldDescriptor);
2645     Op->Bitfield.LSB = LSB;
2646     Op->Bitfield.Width = Width;
2647     Op->StartLoc = S;
2648     Op->EndLoc = E;
2649     return Op;
2650   }
2651 
2652   static std::unique_ptr<ARMOperand>
2653   CreateRegList(SmallVectorImpl<std::pair<unsigned, unsigned>> &Regs,
2654                 SMLoc StartLoc, SMLoc EndLoc) {
2655     assert (Regs.size() > 0 && "RegList contains no registers?");
2656     KindTy Kind = k_RegisterList;
2657 
2658     if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().second))
2659       Kind = k_DPRRegisterList;
2660     else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
2661              contains(Regs.front().second))
2662       Kind = k_SPRRegisterList;
2663 
2664     // Sort based on the register encoding values.
2665     array_pod_sort(Regs.begin(), Regs.end());
2666 
2667     auto Op = make_unique<ARMOperand>(Kind);
2668     for (SmallVectorImpl<std::pair<unsigned, unsigned> >::const_iterator
2669            I = Regs.begin(), E = Regs.end(); I != E; ++I)
2670       Op->Registers.push_back(I->second);
2671     Op->StartLoc = StartLoc;
2672     Op->EndLoc = EndLoc;
2673     return Op;
2674   }
2675 
2676   static std::unique_ptr<ARMOperand> CreateVectorList(unsigned RegNum,
2677                                                       unsigned Count,
2678                                                       bool isDoubleSpaced,
2679                                                       SMLoc S, SMLoc E) {
2680     auto Op = make_unique<ARMOperand>(k_VectorList);
2681     Op->VectorList.RegNum = RegNum;
2682     Op->VectorList.Count = Count;
2683     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2684     Op->StartLoc = S;
2685     Op->EndLoc = E;
2686     return Op;
2687   }
2688 
2689   static std::unique_ptr<ARMOperand>
2690   CreateVectorListAllLanes(unsigned RegNum, unsigned Count, bool isDoubleSpaced,
2691                            SMLoc S, SMLoc E) {
2692     auto Op = make_unique<ARMOperand>(k_VectorListAllLanes);
2693     Op->VectorList.RegNum = RegNum;
2694     Op->VectorList.Count = Count;
2695     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2696     Op->StartLoc = S;
2697     Op->EndLoc = E;
2698     return Op;
2699   }
2700 
2701   static std::unique_ptr<ARMOperand>
2702   CreateVectorListIndexed(unsigned RegNum, unsigned Count, unsigned Index,
2703                           bool isDoubleSpaced, SMLoc S, SMLoc E) {
2704     auto Op = make_unique<ARMOperand>(k_VectorListIndexed);
2705     Op->VectorList.RegNum = RegNum;
2706     Op->VectorList.Count = Count;
2707     Op->VectorList.LaneIndex = Index;
2708     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2709     Op->StartLoc = S;
2710     Op->EndLoc = E;
2711     return Op;
2712   }
2713 
2714   static std::unique_ptr<ARMOperand>
2715   CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E, MCContext &Ctx) {
2716     auto Op = make_unique<ARMOperand>(k_VectorIndex);
2717     Op->VectorIndex.Val = Idx;
2718     Op->StartLoc = S;
2719     Op->EndLoc = E;
2720     return Op;
2721   }
2722 
2723   static std::unique_ptr<ARMOperand> CreateImm(const MCExpr *Val, SMLoc S,
2724                                                SMLoc E) {
2725     auto Op = make_unique<ARMOperand>(k_Immediate);
2726     Op->Imm.Val = Val;
2727     Op->StartLoc = S;
2728     Op->EndLoc = E;
2729     return Op;
2730   }
2731 
2732   static std::unique_ptr<ARMOperand>
2733   CreateMem(unsigned BaseRegNum, const MCConstantExpr *OffsetImm,
2734             unsigned OffsetRegNum, ARM_AM::ShiftOpc ShiftType,
2735             unsigned ShiftImm, unsigned Alignment, bool isNegative, SMLoc S,
2736             SMLoc E, SMLoc AlignmentLoc = SMLoc()) {
2737     auto Op = make_unique<ARMOperand>(k_Memory);
2738     Op->Memory.BaseRegNum = BaseRegNum;
2739     Op->Memory.OffsetImm = OffsetImm;
2740     Op->Memory.OffsetRegNum = OffsetRegNum;
2741     Op->Memory.ShiftType = ShiftType;
2742     Op->Memory.ShiftImm = ShiftImm;
2743     Op->Memory.Alignment = Alignment;
2744     Op->Memory.isNegative = isNegative;
2745     Op->StartLoc = S;
2746     Op->EndLoc = E;
2747     Op->AlignmentLoc = AlignmentLoc;
2748     return Op;
2749   }
2750 
2751   static std::unique_ptr<ARMOperand>
2752   CreatePostIdxReg(unsigned RegNum, bool isAdd, ARM_AM::ShiftOpc ShiftTy,
2753                    unsigned ShiftImm, SMLoc S, SMLoc E) {
2754     auto Op = make_unique<ARMOperand>(k_PostIndexRegister);
2755     Op->PostIdxReg.RegNum = RegNum;
2756     Op->PostIdxReg.isAdd = isAdd;
2757     Op->PostIdxReg.ShiftTy = ShiftTy;
2758     Op->PostIdxReg.ShiftImm = ShiftImm;
2759     Op->StartLoc = S;
2760     Op->EndLoc = E;
2761     return Op;
2762   }
2763 
2764   static std::unique_ptr<ARMOperand> CreateMemBarrierOpt(ARM_MB::MemBOpt Opt,
2765                                                          SMLoc S) {
2766     auto Op = make_unique<ARMOperand>(k_MemBarrierOpt);
2767     Op->MBOpt.Val = Opt;
2768     Op->StartLoc = S;
2769     Op->EndLoc = S;
2770     return Op;
2771   }
2772 
2773   static std::unique_ptr<ARMOperand>
2774   CreateInstSyncBarrierOpt(ARM_ISB::InstSyncBOpt Opt, SMLoc S) {
2775     auto Op = make_unique<ARMOperand>(k_InstSyncBarrierOpt);
2776     Op->ISBOpt.Val = Opt;
2777     Op->StartLoc = S;
2778     Op->EndLoc = S;
2779     return Op;
2780   }
2781 
2782   static std::unique_ptr<ARMOperand> CreateProcIFlags(ARM_PROC::IFlags IFlags,
2783                                                       SMLoc S) {
2784     auto Op = make_unique<ARMOperand>(k_ProcIFlags);
2785     Op->IFlags.Val = IFlags;
2786     Op->StartLoc = S;
2787     Op->EndLoc = S;
2788     return Op;
2789   }
2790 
2791   static std::unique_ptr<ARMOperand> CreateMSRMask(unsigned MMask, SMLoc S) {
2792     auto Op = make_unique<ARMOperand>(k_MSRMask);
2793     Op->MMask.Val = MMask;
2794     Op->StartLoc = S;
2795     Op->EndLoc = S;
2796     return Op;
2797   }
2798 
2799   static std::unique_ptr<ARMOperand> CreateBankedReg(unsigned Reg, SMLoc S) {
2800     auto Op = make_unique<ARMOperand>(k_BankedReg);
2801     Op->BankedReg.Val = Reg;
2802     Op->StartLoc = S;
2803     Op->EndLoc = S;
2804     return Op;
2805   }
2806 };
2807 
2808 } // end anonymous namespace.
2809 
2810 void ARMOperand::print(raw_ostream &OS) const {
2811   switch (Kind) {
2812   case k_CondCode:
2813     OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
2814     break;
2815   case k_CCOut:
2816     OS << "<ccout " << getReg() << ">";
2817     break;
2818   case k_ITCondMask: {
2819     static const char *const MaskStr[] = {
2820       "()", "(t)", "(e)", "(tt)", "(et)", "(te)", "(ee)", "(ttt)", "(ett)",
2821       "(tet)", "(eet)", "(tte)", "(ete)", "(tee)", "(eee)"
2822     };
2823     assert((ITMask.Mask & 0xf) == ITMask.Mask);
2824     OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
2825     break;
2826   }
2827   case k_CoprocNum:
2828     OS << "<coprocessor number: " << getCoproc() << ">";
2829     break;
2830   case k_CoprocReg:
2831     OS << "<coprocessor register: " << getCoproc() << ">";
2832     break;
2833   case k_CoprocOption:
2834     OS << "<coprocessor option: " << CoprocOption.Val << ">";
2835     break;
2836   case k_MSRMask:
2837     OS << "<mask: " << getMSRMask() << ">";
2838     break;
2839   case k_BankedReg:
2840     OS << "<banked reg: " << getBankedReg() << ">";
2841     break;
2842   case k_Immediate:
2843     OS << *getImm();
2844     break;
2845   case k_MemBarrierOpt:
2846     OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt(), false) << ">";
2847     break;
2848   case k_InstSyncBarrierOpt:
2849     OS << "<ARM_ISB::" << InstSyncBOptToString(getInstSyncBarrierOpt()) << ">";
2850     break;
2851   case k_Memory:
2852     OS << "<memory "
2853        << " base:" << Memory.BaseRegNum;
2854     OS << ">";
2855     break;
2856   case k_PostIndexRegister:
2857     OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
2858        << PostIdxReg.RegNum;
2859     if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
2860       OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
2861          << PostIdxReg.ShiftImm;
2862     OS << ">";
2863     break;
2864   case k_ProcIFlags: {
2865     OS << "<ARM_PROC::";
2866     unsigned IFlags = getProcIFlags();
2867     for (int i=2; i >= 0; --i)
2868       if (IFlags & (1 << i))
2869         OS << ARM_PROC::IFlagsToString(1 << i);
2870     OS << ">";
2871     break;
2872   }
2873   case k_Register:
2874     OS << "<register " << getReg() << ">";
2875     break;
2876   case k_ShifterImmediate:
2877     OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
2878        << " #" << ShifterImm.Imm << ">";
2879     break;
2880   case k_ShiftedRegister:
2881     OS << "<so_reg_reg "
2882        << RegShiftedReg.SrcReg << " "
2883        << ARM_AM::getShiftOpcStr(RegShiftedReg.ShiftTy)
2884        << " " << RegShiftedReg.ShiftReg << ">";
2885     break;
2886   case k_ShiftedImmediate:
2887     OS << "<so_reg_imm "
2888        << RegShiftedImm.SrcReg << " "
2889        << ARM_AM::getShiftOpcStr(RegShiftedImm.ShiftTy)
2890        << " #" << RegShiftedImm.ShiftImm << ">";
2891     break;
2892   case k_RotateImmediate:
2893     OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
2894     break;
2895   case k_ModifiedImmediate:
2896     OS << "<mod_imm #" << ModImm.Bits << ", #"
2897        <<  ModImm.Rot << ")>";
2898     break;
2899   case k_BitfieldDescriptor:
2900     OS << "<bitfield " << "lsb: " << Bitfield.LSB
2901        << ", width: " << Bitfield.Width << ">";
2902     break;
2903   case k_RegisterList:
2904   case k_DPRRegisterList:
2905   case k_SPRRegisterList: {
2906     OS << "<register_list ";
2907 
2908     const SmallVectorImpl<unsigned> &RegList = getRegList();
2909     for (SmallVectorImpl<unsigned>::const_iterator
2910            I = RegList.begin(), E = RegList.end(); I != E; ) {
2911       OS << *I;
2912       if (++I < E) OS << ", ";
2913     }
2914 
2915     OS << ">";
2916     break;
2917   }
2918   case k_VectorList:
2919     OS << "<vector_list " << VectorList.Count << " * "
2920        << VectorList.RegNum << ">";
2921     break;
2922   case k_VectorListAllLanes:
2923     OS << "<vector_list(all lanes) " << VectorList.Count << " * "
2924        << VectorList.RegNum << ">";
2925     break;
2926   case k_VectorListIndexed:
2927     OS << "<vector_list(lane " << VectorList.LaneIndex << ") "
2928        << VectorList.Count << " * " << VectorList.RegNum << ">";
2929     break;
2930   case k_Token:
2931     OS << "'" << getToken() << "'";
2932     break;
2933   case k_VectorIndex:
2934     OS << "<vectorindex " << getVectorIndex() << ">";
2935     break;
2936   }
2937 }
2938 
2939 /// @name Auto-generated Match Functions
2940 /// {
2941 
2942 static unsigned MatchRegisterName(StringRef Name);
2943 
2944 /// }
2945 
2946 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
2947                                  SMLoc &StartLoc, SMLoc &EndLoc) {
2948   const AsmToken &Tok = getParser().getTok();
2949   StartLoc = Tok.getLoc();
2950   EndLoc = Tok.getEndLoc();
2951   RegNo = tryParseRegister();
2952 
2953   return (RegNo == (unsigned)-1);
2954 }
2955 
2956 /// Try to parse a register name.  The token must be an Identifier when called,
2957 /// and if it is a register name the token is eaten and the register number is
2958 /// returned.  Otherwise return -1.
2959 ///
2960 int ARMAsmParser::tryParseRegister() {
2961   MCAsmParser &Parser = getParser();
2962   const AsmToken &Tok = Parser.getTok();
2963   if (Tok.isNot(AsmToken::Identifier)) return -1;
2964 
2965   std::string lowerCase = Tok.getString().lower();
2966   unsigned RegNum = MatchRegisterName(lowerCase);
2967   if (!RegNum) {
2968     RegNum = StringSwitch<unsigned>(lowerCase)
2969       .Case("r13", ARM::SP)
2970       .Case("r14", ARM::LR)
2971       .Case("r15", ARM::PC)
2972       .Case("ip", ARM::R12)
2973       // Additional register name aliases for 'gas' compatibility.
2974       .Case("a1", ARM::R0)
2975       .Case("a2", ARM::R1)
2976       .Case("a3", ARM::R2)
2977       .Case("a4", ARM::R3)
2978       .Case("v1", ARM::R4)
2979       .Case("v2", ARM::R5)
2980       .Case("v3", ARM::R6)
2981       .Case("v4", ARM::R7)
2982       .Case("v5", ARM::R8)
2983       .Case("v6", ARM::R9)
2984       .Case("v7", ARM::R10)
2985       .Case("v8", ARM::R11)
2986       .Case("sb", ARM::R9)
2987       .Case("sl", ARM::R10)
2988       .Case("fp", ARM::R11)
2989       .Default(0);
2990   }
2991   if (!RegNum) {
2992     // Check for aliases registered via .req. Canonicalize to lower case.
2993     // That's more consistent since register names are case insensitive, and
2994     // it's how the original entry was passed in from MC/MCParser/AsmParser.
2995     StringMap<unsigned>::const_iterator Entry = RegisterReqs.find(lowerCase);
2996     // If no match, return failure.
2997     if (Entry == RegisterReqs.end())
2998       return -1;
2999     Parser.Lex(); // Eat identifier token.
3000     return Entry->getValue();
3001   }
3002 
3003   // Some FPUs only have 16 D registers, so D16-D31 are invalid
3004   if (hasD16() && RegNum >= ARM::D16 && RegNum <= ARM::D31)
3005     return -1;
3006 
3007   Parser.Lex(); // Eat identifier token.
3008 
3009   return RegNum;
3010 }
3011 
3012 // Try to parse a shifter  (e.g., "lsl <amt>"). On success, return 0.
3013 // If a recoverable error occurs, return 1. If an irrecoverable error
3014 // occurs, return -1. An irrecoverable error is one where tokens have been
3015 // consumed in the process of trying to parse the shifter (i.e., when it is
3016 // indeed a shifter operand, but malformed).
3017 int ARMAsmParser::tryParseShiftRegister(OperandVector &Operands) {
3018   MCAsmParser &Parser = getParser();
3019   SMLoc S = Parser.getTok().getLoc();
3020   const AsmToken &Tok = Parser.getTok();
3021   if (Tok.isNot(AsmToken::Identifier))
3022     return -1;
3023 
3024   std::string lowerCase = Tok.getString().lower();
3025   ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
3026       .Case("asl", ARM_AM::lsl)
3027       .Case("lsl", ARM_AM::lsl)
3028       .Case("lsr", ARM_AM::lsr)
3029       .Case("asr", ARM_AM::asr)
3030       .Case("ror", ARM_AM::ror)
3031       .Case("rrx", ARM_AM::rrx)
3032       .Default(ARM_AM::no_shift);
3033 
3034   if (ShiftTy == ARM_AM::no_shift)
3035     return 1;
3036 
3037   Parser.Lex(); // Eat the operator.
3038 
3039   // The source register for the shift has already been added to the
3040   // operand list, so we need to pop it off and combine it into the shifted
3041   // register operand instead.
3042   std::unique_ptr<ARMOperand> PrevOp(
3043       (ARMOperand *)Operands.pop_back_val().release());
3044   if (!PrevOp->isReg())
3045     return Error(PrevOp->getStartLoc(), "shift must be of a register");
3046   int SrcReg = PrevOp->getReg();
3047 
3048   SMLoc EndLoc;
3049   int64_t Imm = 0;
3050   int ShiftReg = 0;
3051   if (ShiftTy == ARM_AM::rrx) {
3052     // RRX Doesn't have an explicit shift amount. The encoder expects
3053     // the shift register to be the same as the source register. Seems odd,
3054     // but OK.
3055     ShiftReg = SrcReg;
3056   } else {
3057     // Figure out if this is shifted by a constant or a register (for non-RRX).
3058     if (Parser.getTok().is(AsmToken::Hash) ||
3059         Parser.getTok().is(AsmToken::Dollar)) {
3060       Parser.Lex(); // Eat hash.
3061       SMLoc ImmLoc = Parser.getTok().getLoc();
3062       const MCExpr *ShiftExpr = nullptr;
3063       if (getParser().parseExpression(ShiftExpr, EndLoc)) {
3064         Error(ImmLoc, "invalid immediate shift value");
3065         return -1;
3066       }
3067       // The expression must be evaluatable as an immediate.
3068       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
3069       if (!CE) {
3070         Error(ImmLoc, "invalid immediate shift value");
3071         return -1;
3072       }
3073       // Range check the immediate.
3074       // lsl, ror: 0 <= imm <= 31
3075       // lsr, asr: 0 <= imm <= 32
3076       Imm = CE->getValue();
3077       if (Imm < 0 ||
3078           ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
3079           ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
3080         Error(ImmLoc, "immediate shift value out of range");
3081         return -1;
3082       }
3083       // shift by zero is a nop. Always send it through as lsl.
3084       // ('as' compatibility)
3085       if (Imm == 0)
3086         ShiftTy = ARM_AM::lsl;
3087     } else if (Parser.getTok().is(AsmToken::Identifier)) {
3088       SMLoc L = Parser.getTok().getLoc();
3089       EndLoc = Parser.getTok().getEndLoc();
3090       ShiftReg = tryParseRegister();
3091       if (ShiftReg == -1) {
3092         Error(L, "expected immediate or register in shift operand");
3093         return -1;
3094       }
3095     } else {
3096       Error(Parser.getTok().getLoc(),
3097             "expected immediate or register in shift operand");
3098       return -1;
3099     }
3100   }
3101 
3102   if (ShiftReg && ShiftTy != ARM_AM::rrx)
3103     Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
3104                                                          ShiftReg, Imm,
3105                                                          S, EndLoc));
3106   else
3107     Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
3108                                                           S, EndLoc));
3109 
3110   return 0;
3111 }
3112 
3113 
3114 /// Try to parse a register name.  The token must be an Identifier when called.
3115 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
3116 /// if there is a "writeback". 'true' if it's not a register.
3117 ///
3118 /// TODO this is likely to change to allow different register types and or to
3119 /// parse for a specific register type.
3120 bool ARMAsmParser::tryParseRegisterWithWriteBack(OperandVector &Operands) {
3121   MCAsmParser &Parser = getParser();
3122   const AsmToken &RegTok = Parser.getTok();
3123   int RegNo = tryParseRegister();
3124   if (RegNo == -1)
3125     return true;
3126 
3127   Operands.push_back(ARMOperand::CreateReg(RegNo, RegTok.getLoc(),
3128                                            RegTok.getEndLoc()));
3129 
3130   const AsmToken &ExclaimTok = Parser.getTok();
3131   if (ExclaimTok.is(AsmToken::Exclaim)) {
3132     Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
3133                                                ExclaimTok.getLoc()));
3134     Parser.Lex(); // Eat exclaim token
3135     return false;
3136   }
3137 
3138   // Also check for an index operand. This is only legal for vector registers,
3139   // but that'll get caught OK in operand matching, so we don't need to
3140   // explicitly filter everything else out here.
3141   if (Parser.getTok().is(AsmToken::LBrac)) {
3142     SMLoc SIdx = Parser.getTok().getLoc();
3143     Parser.Lex(); // Eat left bracket token.
3144 
3145     const MCExpr *ImmVal;
3146     if (getParser().parseExpression(ImmVal))
3147       return true;
3148     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
3149     if (!MCE)
3150       return TokError("immediate value expected for vector index");
3151 
3152     if (Parser.getTok().isNot(AsmToken::RBrac))
3153       return Error(Parser.getTok().getLoc(), "']' expected");
3154 
3155     SMLoc E = Parser.getTok().getEndLoc();
3156     Parser.Lex(); // Eat right bracket token.
3157 
3158     Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
3159                                                      SIdx, E,
3160                                                      getContext()));
3161   }
3162 
3163   return false;
3164 }
3165 
3166 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
3167 /// instruction with a symbolic operand name.
3168 /// We accept "crN" syntax for GAS compatibility.
3169 /// <operand-name> ::= <prefix><number>
3170 /// If CoprocOp is 'c', then:
3171 ///   <prefix> ::= c | cr
3172 /// If CoprocOp is 'p', then :
3173 ///   <prefix> ::= p
3174 /// <number> ::= integer in range [0, 15]
3175 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
3176   // Use the same layout as the tablegen'erated register name matcher. Ugly,
3177   // but efficient.
3178   if (Name.size() < 2 || Name[0] != CoprocOp)
3179     return -1;
3180   Name = (Name[1] == 'r') ? Name.drop_front(2) : Name.drop_front();
3181 
3182   switch (Name.size()) {
3183   default: return -1;
3184   case 1:
3185     switch (Name[0]) {
3186     default:  return -1;
3187     case '0': return 0;
3188     case '1': return 1;
3189     case '2': return 2;
3190     case '3': return 3;
3191     case '4': return 4;
3192     case '5': return 5;
3193     case '6': return 6;
3194     case '7': return 7;
3195     case '8': return 8;
3196     case '9': return 9;
3197     }
3198   case 2:
3199     if (Name[0] != '1')
3200       return -1;
3201     switch (Name[1]) {
3202     default:  return -1;
3203     // CP10 and CP11 are VFP/NEON and so vector instructions should be used.
3204     // However, old cores (v5/v6) did use them in that way.
3205     case '0': return 10;
3206     case '1': return 11;
3207     case '2': return 12;
3208     case '3': return 13;
3209     case '4': return 14;
3210     case '5': return 15;
3211     }
3212   }
3213 }
3214 
3215 /// parseITCondCode - Try to parse a condition code for an IT instruction.
3216 ARMAsmParser::OperandMatchResultTy
3217 ARMAsmParser::parseITCondCode(OperandVector &Operands) {
3218   MCAsmParser &Parser = getParser();
3219   SMLoc S = Parser.getTok().getLoc();
3220   const AsmToken &Tok = Parser.getTok();
3221   if (!Tok.is(AsmToken::Identifier))
3222     return MatchOperand_NoMatch;
3223   unsigned CC = StringSwitch<unsigned>(Tok.getString().lower())
3224     .Case("eq", ARMCC::EQ)
3225     .Case("ne", ARMCC::NE)
3226     .Case("hs", ARMCC::HS)
3227     .Case("cs", ARMCC::HS)
3228     .Case("lo", ARMCC::LO)
3229     .Case("cc", ARMCC::LO)
3230     .Case("mi", ARMCC::MI)
3231     .Case("pl", ARMCC::PL)
3232     .Case("vs", ARMCC::VS)
3233     .Case("vc", ARMCC::VC)
3234     .Case("hi", ARMCC::HI)
3235     .Case("ls", ARMCC::LS)
3236     .Case("ge", ARMCC::GE)
3237     .Case("lt", ARMCC::LT)
3238     .Case("gt", ARMCC::GT)
3239     .Case("le", ARMCC::LE)
3240     .Case("al", ARMCC::AL)
3241     .Default(~0U);
3242   if (CC == ~0U)
3243     return MatchOperand_NoMatch;
3244   Parser.Lex(); // Eat the token.
3245 
3246   Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
3247 
3248   return MatchOperand_Success;
3249 }
3250 
3251 /// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
3252 /// token must be an Identifier when called, and if it is a coprocessor
3253 /// number, the token is eaten and the operand is added to the operand list.
3254 ARMAsmParser::OperandMatchResultTy
3255 ARMAsmParser::parseCoprocNumOperand(OperandVector &Operands) {
3256   MCAsmParser &Parser = getParser();
3257   SMLoc S = Parser.getTok().getLoc();
3258   const AsmToken &Tok = Parser.getTok();
3259   if (Tok.isNot(AsmToken::Identifier))
3260     return MatchOperand_NoMatch;
3261 
3262   int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
3263   if (Num == -1)
3264     return MatchOperand_NoMatch;
3265   // ARMv7 and v8 don't allow cp10/cp11 due to VFP/NEON specific instructions
3266   if ((hasV7Ops() || hasV8Ops()) && (Num == 10 || Num == 11))
3267     return MatchOperand_NoMatch;
3268 
3269   Parser.Lex(); // Eat identifier token.
3270   Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
3271   return MatchOperand_Success;
3272 }
3273 
3274 /// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
3275 /// token must be an Identifier when called, and if it is a coprocessor
3276 /// number, the token is eaten and the operand is added to the operand list.
3277 ARMAsmParser::OperandMatchResultTy
3278 ARMAsmParser::parseCoprocRegOperand(OperandVector &Operands) {
3279   MCAsmParser &Parser = getParser();
3280   SMLoc S = Parser.getTok().getLoc();
3281   const AsmToken &Tok = Parser.getTok();
3282   if (Tok.isNot(AsmToken::Identifier))
3283     return MatchOperand_NoMatch;
3284 
3285   int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
3286   if (Reg == -1)
3287     return MatchOperand_NoMatch;
3288 
3289   Parser.Lex(); // Eat identifier token.
3290   Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
3291   return MatchOperand_Success;
3292 }
3293 
3294 /// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
3295 /// coproc_option : '{' imm0_255 '}'
3296 ARMAsmParser::OperandMatchResultTy
3297 ARMAsmParser::parseCoprocOptionOperand(OperandVector &Operands) {
3298   MCAsmParser &Parser = getParser();
3299   SMLoc S = Parser.getTok().getLoc();
3300 
3301   // If this isn't a '{', this isn't a coprocessor immediate operand.
3302   if (Parser.getTok().isNot(AsmToken::LCurly))
3303     return MatchOperand_NoMatch;
3304   Parser.Lex(); // Eat the '{'
3305 
3306   const MCExpr *Expr;
3307   SMLoc Loc = Parser.getTok().getLoc();
3308   if (getParser().parseExpression(Expr)) {
3309     Error(Loc, "illegal expression");
3310     return MatchOperand_ParseFail;
3311   }
3312   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3313   if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
3314     Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
3315     return MatchOperand_ParseFail;
3316   }
3317   int Val = CE->getValue();
3318 
3319   // Check for and consume the closing '}'
3320   if (Parser.getTok().isNot(AsmToken::RCurly))
3321     return MatchOperand_ParseFail;
3322   SMLoc E = Parser.getTok().getEndLoc();
3323   Parser.Lex(); // Eat the '}'
3324 
3325   Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
3326   return MatchOperand_Success;
3327 }
3328 
3329 // For register list parsing, we need to map from raw GPR register numbering
3330 // to the enumeration values. The enumeration values aren't sorted by
3331 // register number due to our using "sp", "lr" and "pc" as canonical names.
3332 static unsigned getNextRegister(unsigned Reg) {
3333   // If this is a GPR, we need to do it manually, otherwise we can rely
3334   // on the sort ordering of the enumeration since the other reg-classes
3335   // are sane.
3336   if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
3337     return Reg + 1;
3338   switch(Reg) {
3339   default: llvm_unreachable("Invalid GPR number!");
3340   case ARM::R0:  return ARM::R1;  case ARM::R1:  return ARM::R2;
3341   case ARM::R2:  return ARM::R3;  case ARM::R3:  return ARM::R4;
3342   case ARM::R4:  return ARM::R5;  case ARM::R5:  return ARM::R6;
3343   case ARM::R6:  return ARM::R7;  case ARM::R7:  return ARM::R8;
3344   case ARM::R8:  return ARM::R9;  case ARM::R9:  return ARM::R10;
3345   case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
3346   case ARM::R12: return ARM::SP;  case ARM::SP:  return ARM::LR;
3347   case ARM::LR:  return ARM::PC;  case ARM::PC:  return ARM::R0;
3348   }
3349 }
3350 
3351 // Return the low-subreg of a given Q register.
3352 static unsigned getDRegFromQReg(unsigned QReg) {
3353   switch (QReg) {
3354   default: llvm_unreachable("expected a Q register!");
3355   case ARM::Q0:  return ARM::D0;
3356   case ARM::Q1:  return ARM::D2;
3357   case ARM::Q2:  return ARM::D4;
3358   case ARM::Q3:  return ARM::D6;
3359   case ARM::Q4:  return ARM::D8;
3360   case ARM::Q5:  return ARM::D10;
3361   case ARM::Q6:  return ARM::D12;
3362   case ARM::Q7:  return ARM::D14;
3363   case ARM::Q8:  return ARM::D16;
3364   case ARM::Q9:  return ARM::D18;
3365   case ARM::Q10: return ARM::D20;
3366   case ARM::Q11: return ARM::D22;
3367   case ARM::Q12: return ARM::D24;
3368   case ARM::Q13: return ARM::D26;
3369   case ARM::Q14: return ARM::D28;
3370   case ARM::Q15: return ARM::D30;
3371   }
3372 }
3373 
3374 /// Parse a register list.
3375 bool ARMAsmParser::parseRegisterList(OperandVector &Operands) {
3376   MCAsmParser &Parser = getParser();
3377   assert(Parser.getTok().is(AsmToken::LCurly) &&
3378          "Token is not a Left Curly Brace");
3379   SMLoc S = Parser.getTok().getLoc();
3380   Parser.Lex(); // Eat '{' token.
3381   SMLoc RegLoc = Parser.getTok().getLoc();
3382 
3383   // Check the first register in the list to see what register class
3384   // this is a list of.
3385   int Reg = tryParseRegister();
3386   if (Reg == -1)
3387     return Error(RegLoc, "register expected");
3388 
3389   // The reglist instructions have at most 16 registers, so reserve
3390   // space for that many.
3391   int EReg = 0;
3392   SmallVector<std::pair<unsigned, unsigned>, 16> Registers;
3393 
3394   // Allow Q regs and just interpret them as the two D sub-registers.
3395   if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3396     Reg = getDRegFromQReg(Reg);
3397     EReg = MRI->getEncodingValue(Reg);
3398     Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3399     ++Reg;
3400   }
3401   const MCRegisterClass *RC;
3402   if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
3403     RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
3404   else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
3405     RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
3406   else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
3407     RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
3408   else
3409     return Error(RegLoc, "invalid register in register list");
3410 
3411   // Store the register.
3412   EReg = MRI->getEncodingValue(Reg);
3413   Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3414 
3415   // This starts immediately after the first register token in the list,
3416   // so we can see either a comma or a minus (range separator) as a legal
3417   // next token.
3418   while (Parser.getTok().is(AsmToken::Comma) ||
3419          Parser.getTok().is(AsmToken::Minus)) {
3420     if (Parser.getTok().is(AsmToken::Minus)) {
3421       Parser.Lex(); // Eat the minus.
3422       SMLoc AfterMinusLoc = Parser.getTok().getLoc();
3423       int EndReg = tryParseRegister();
3424       if (EndReg == -1)
3425         return Error(AfterMinusLoc, "register expected");
3426       // Allow Q regs and just interpret them as the two D sub-registers.
3427       if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
3428         EndReg = getDRegFromQReg(EndReg) + 1;
3429       // If the register is the same as the start reg, there's nothing
3430       // more to do.
3431       if (Reg == EndReg)
3432         continue;
3433       // The register must be in the same register class as the first.
3434       if (!RC->contains(EndReg))
3435         return Error(AfterMinusLoc, "invalid register in register list");
3436       // Ranges must go from low to high.
3437       if (MRI->getEncodingValue(Reg) > MRI->getEncodingValue(EndReg))
3438         return Error(AfterMinusLoc, "bad range in register list");
3439 
3440       // Add all the registers in the range to the register list.
3441       while (Reg != EndReg) {
3442         Reg = getNextRegister(Reg);
3443         EReg = MRI->getEncodingValue(Reg);
3444         Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3445       }
3446       continue;
3447     }
3448     Parser.Lex(); // Eat the comma.
3449     RegLoc = Parser.getTok().getLoc();
3450     int OldReg = Reg;
3451     const AsmToken RegTok = Parser.getTok();
3452     Reg = tryParseRegister();
3453     if (Reg == -1)
3454       return Error(RegLoc, "register expected");
3455     // Allow Q regs and just interpret them as the two D sub-registers.
3456     bool isQReg = false;
3457     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3458       Reg = getDRegFromQReg(Reg);
3459       isQReg = true;
3460     }
3461     // The register must be in the same register class as the first.
3462     if (!RC->contains(Reg))
3463       return Error(RegLoc, "invalid register in register list");
3464     // List must be monotonically increasing.
3465     if (MRI->getEncodingValue(Reg) < MRI->getEncodingValue(OldReg)) {
3466       if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
3467         Warning(RegLoc, "register list not in ascending order");
3468       else
3469         return Error(RegLoc, "register list not in ascending order");
3470     }
3471     if (MRI->getEncodingValue(Reg) == MRI->getEncodingValue(OldReg)) {
3472       Warning(RegLoc, "duplicated register (" + RegTok.getString() +
3473               ") in register list");
3474       continue;
3475     }
3476     // VFP register lists must also be contiguous.
3477     if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
3478         Reg != OldReg + 1)
3479       return Error(RegLoc, "non-contiguous register range");
3480     EReg = MRI->getEncodingValue(Reg);
3481     Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3482     if (isQReg) {
3483       EReg = MRI->getEncodingValue(++Reg);
3484       Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3485     }
3486   }
3487 
3488   if (Parser.getTok().isNot(AsmToken::RCurly))
3489     return Error(Parser.getTok().getLoc(), "'}' expected");
3490   SMLoc E = Parser.getTok().getEndLoc();
3491   Parser.Lex(); // Eat '}' token.
3492 
3493   // Push the register list operand.
3494   Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
3495 
3496   // The ARM system instruction variants for LDM/STM have a '^' token here.
3497   if (Parser.getTok().is(AsmToken::Caret)) {
3498     Operands.push_back(ARMOperand::CreateToken("^",Parser.getTok().getLoc()));
3499     Parser.Lex(); // Eat '^' token.
3500   }
3501 
3502   return false;
3503 }
3504 
3505 // Helper function to parse the lane index for vector lists.
3506 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3507 parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index, SMLoc &EndLoc) {
3508   MCAsmParser &Parser = getParser();
3509   Index = 0; // Always return a defined index value.
3510   if (Parser.getTok().is(AsmToken::LBrac)) {
3511     Parser.Lex(); // Eat the '['.
3512     if (Parser.getTok().is(AsmToken::RBrac)) {
3513       // "Dn[]" is the 'all lanes' syntax.
3514       LaneKind = AllLanes;
3515       EndLoc = Parser.getTok().getEndLoc();
3516       Parser.Lex(); // Eat the ']'.
3517       return MatchOperand_Success;
3518     }
3519 
3520     // There's an optional '#' token here. Normally there wouldn't be, but
3521     // inline assemble puts one in, and it's friendly to accept that.
3522     if (Parser.getTok().is(AsmToken::Hash))
3523       Parser.Lex(); // Eat '#' or '$'.
3524 
3525     const MCExpr *LaneIndex;
3526     SMLoc Loc = Parser.getTok().getLoc();
3527     if (getParser().parseExpression(LaneIndex)) {
3528       Error(Loc, "illegal expression");
3529       return MatchOperand_ParseFail;
3530     }
3531     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LaneIndex);
3532     if (!CE) {
3533       Error(Loc, "lane index must be empty or an integer");
3534       return MatchOperand_ParseFail;
3535     }
3536     if (Parser.getTok().isNot(AsmToken::RBrac)) {
3537       Error(Parser.getTok().getLoc(), "']' expected");
3538       return MatchOperand_ParseFail;
3539     }
3540     EndLoc = Parser.getTok().getEndLoc();
3541     Parser.Lex(); // Eat the ']'.
3542     int64_t Val = CE->getValue();
3543 
3544     // FIXME: Make this range check context sensitive for .8, .16, .32.
3545     if (Val < 0 || Val > 7) {
3546       Error(Parser.getTok().getLoc(), "lane index out of range");
3547       return MatchOperand_ParseFail;
3548     }
3549     Index = Val;
3550     LaneKind = IndexedLane;
3551     return MatchOperand_Success;
3552   }
3553   LaneKind = NoLanes;
3554   return MatchOperand_Success;
3555 }
3556 
3557 // parse a vector register list
3558 ARMAsmParser::OperandMatchResultTy
3559 ARMAsmParser::parseVectorList(OperandVector &Operands) {
3560   MCAsmParser &Parser = getParser();
3561   VectorLaneTy LaneKind;
3562   unsigned LaneIndex;
3563   SMLoc S = Parser.getTok().getLoc();
3564   // As an extension (to match gas), support a plain D register or Q register
3565   // (without encosing curly braces) as a single or double entry list,
3566   // respectively.
3567   if (Parser.getTok().is(AsmToken::Identifier)) {
3568     SMLoc E = Parser.getTok().getEndLoc();
3569     int Reg = tryParseRegister();
3570     if (Reg == -1)
3571       return MatchOperand_NoMatch;
3572     if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg)) {
3573       OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex, E);
3574       if (Res != MatchOperand_Success)
3575         return Res;
3576       switch (LaneKind) {
3577       case NoLanes:
3578         Operands.push_back(ARMOperand::CreateVectorList(Reg, 1, false, S, E));
3579         break;
3580       case AllLanes:
3581         Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 1, false,
3582                                                                 S, E));
3583         break;
3584       case IndexedLane:
3585         Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 1,
3586                                                                LaneIndex,
3587                                                                false, S, E));
3588         break;
3589       }
3590       return MatchOperand_Success;
3591     }
3592     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3593       Reg = getDRegFromQReg(Reg);
3594       OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex, E);
3595       if (Res != MatchOperand_Success)
3596         return Res;
3597       switch (LaneKind) {
3598       case NoLanes:
3599         Reg = MRI->getMatchingSuperReg(Reg, ARM::dsub_0,
3600                                    &ARMMCRegisterClasses[ARM::DPairRegClassID]);
3601         Operands.push_back(ARMOperand::CreateVectorList(Reg, 2, false, S, E));
3602         break;
3603       case AllLanes:
3604         Reg = MRI->getMatchingSuperReg(Reg, ARM::dsub_0,
3605                                    &ARMMCRegisterClasses[ARM::DPairRegClassID]);
3606         Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 2, false,
3607                                                                 S, E));
3608         break;
3609       case IndexedLane:
3610         Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 2,
3611                                                                LaneIndex,
3612                                                                false, S, E));
3613         break;
3614       }
3615       return MatchOperand_Success;
3616     }
3617     Error(S, "vector register expected");
3618     return MatchOperand_ParseFail;
3619   }
3620 
3621   if (Parser.getTok().isNot(AsmToken::LCurly))
3622     return MatchOperand_NoMatch;
3623 
3624   Parser.Lex(); // Eat '{' token.
3625   SMLoc RegLoc = Parser.getTok().getLoc();
3626 
3627   int Reg = tryParseRegister();
3628   if (Reg == -1) {
3629     Error(RegLoc, "register expected");
3630     return MatchOperand_ParseFail;
3631   }
3632   unsigned Count = 1;
3633   int Spacing = 0;
3634   unsigned FirstReg = Reg;
3635   // The list is of D registers, but we also allow Q regs and just interpret
3636   // them as the two D sub-registers.
3637   if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3638     FirstReg = Reg = getDRegFromQReg(Reg);
3639     Spacing = 1; // double-spacing requires explicit D registers, otherwise
3640                  // it's ambiguous with four-register single spaced.
3641     ++Reg;
3642     ++Count;
3643   }
3644 
3645   SMLoc E;
3646   if (parseVectorLane(LaneKind, LaneIndex, E) != MatchOperand_Success)
3647     return MatchOperand_ParseFail;
3648 
3649   while (Parser.getTok().is(AsmToken::Comma) ||
3650          Parser.getTok().is(AsmToken::Minus)) {
3651     if (Parser.getTok().is(AsmToken::Minus)) {
3652       if (!Spacing)
3653         Spacing = 1; // Register range implies a single spaced list.
3654       else if (Spacing == 2) {
3655         Error(Parser.getTok().getLoc(),
3656               "sequential registers in double spaced list");
3657         return MatchOperand_ParseFail;
3658       }
3659       Parser.Lex(); // Eat the minus.
3660       SMLoc AfterMinusLoc = Parser.getTok().getLoc();
3661       int EndReg = tryParseRegister();
3662       if (EndReg == -1) {
3663         Error(AfterMinusLoc, "register expected");
3664         return MatchOperand_ParseFail;
3665       }
3666       // Allow Q regs and just interpret them as the two D sub-registers.
3667       if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
3668         EndReg = getDRegFromQReg(EndReg) + 1;
3669       // If the register is the same as the start reg, there's nothing
3670       // more to do.
3671       if (Reg == EndReg)
3672         continue;
3673       // The register must be in the same register class as the first.
3674       if (!ARMMCRegisterClasses[ARM::DPRRegClassID].contains(EndReg)) {
3675         Error(AfterMinusLoc, "invalid register in register list");
3676         return MatchOperand_ParseFail;
3677       }
3678       // Ranges must go from low to high.
3679       if (Reg > EndReg) {
3680         Error(AfterMinusLoc, "bad range in register list");
3681         return MatchOperand_ParseFail;
3682       }
3683       // Parse the lane specifier if present.
3684       VectorLaneTy NextLaneKind;
3685       unsigned NextLaneIndex;
3686       if (parseVectorLane(NextLaneKind, NextLaneIndex, E) !=
3687           MatchOperand_Success)
3688         return MatchOperand_ParseFail;
3689       if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3690         Error(AfterMinusLoc, "mismatched lane index in register list");
3691         return MatchOperand_ParseFail;
3692       }
3693 
3694       // Add all the registers in the range to the register list.
3695       Count += EndReg - Reg;
3696       Reg = EndReg;
3697       continue;
3698     }
3699     Parser.Lex(); // Eat the comma.
3700     RegLoc = Parser.getTok().getLoc();
3701     int OldReg = Reg;
3702     Reg = tryParseRegister();
3703     if (Reg == -1) {
3704       Error(RegLoc, "register expected");
3705       return MatchOperand_ParseFail;
3706     }
3707     // vector register lists must be contiguous.
3708     // It's OK to use the enumeration values directly here rather, as the
3709     // VFP register classes have the enum sorted properly.
3710     //
3711     // The list is of D registers, but we also allow Q regs and just interpret
3712     // them as the two D sub-registers.
3713     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3714       if (!Spacing)
3715         Spacing = 1; // Register range implies a single spaced list.
3716       else if (Spacing == 2) {
3717         Error(RegLoc,
3718               "invalid register in double-spaced list (must be 'D' register')");
3719         return MatchOperand_ParseFail;
3720       }
3721       Reg = getDRegFromQReg(Reg);
3722       if (Reg != OldReg + 1) {
3723         Error(RegLoc, "non-contiguous register range");
3724         return MatchOperand_ParseFail;
3725       }
3726       ++Reg;
3727       Count += 2;
3728       // Parse the lane specifier if present.
3729       VectorLaneTy NextLaneKind;
3730       unsigned NextLaneIndex;
3731       SMLoc LaneLoc = Parser.getTok().getLoc();
3732       if (parseVectorLane(NextLaneKind, NextLaneIndex, E) !=
3733           MatchOperand_Success)
3734         return MatchOperand_ParseFail;
3735       if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3736         Error(LaneLoc, "mismatched lane index in register list");
3737         return MatchOperand_ParseFail;
3738       }
3739       continue;
3740     }
3741     // Normal D register.
3742     // Figure out the register spacing (single or double) of the list if
3743     // we don't know it already.
3744     if (!Spacing)
3745       Spacing = 1 + (Reg == OldReg + 2);
3746 
3747     // Just check that it's contiguous and keep going.
3748     if (Reg != OldReg + Spacing) {
3749       Error(RegLoc, "non-contiguous register range");
3750       return MatchOperand_ParseFail;
3751     }
3752     ++Count;
3753     // Parse the lane specifier if present.
3754     VectorLaneTy NextLaneKind;
3755     unsigned NextLaneIndex;
3756     SMLoc EndLoc = Parser.getTok().getLoc();
3757     if (parseVectorLane(NextLaneKind, NextLaneIndex, E) != MatchOperand_Success)
3758       return MatchOperand_ParseFail;
3759     if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3760       Error(EndLoc, "mismatched lane index in register list");
3761       return MatchOperand_ParseFail;
3762     }
3763   }
3764 
3765   if (Parser.getTok().isNot(AsmToken::RCurly)) {
3766     Error(Parser.getTok().getLoc(), "'}' expected");
3767     return MatchOperand_ParseFail;
3768   }
3769   E = Parser.getTok().getEndLoc();
3770   Parser.Lex(); // Eat '}' token.
3771 
3772   switch (LaneKind) {
3773   case NoLanes:
3774     // Two-register operands have been converted to the
3775     // composite register classes.
3776     if (Count == 2) {
3777       const MCRegisterClass *RC = (Spacing == 1) ?
3778         &ARMMCRegisterClasses[ARM::DPairRegClassID] :
3779         &ARMMCRegisterClasses[ARM::DPairSpcRegClassID];
3780       FirstReg = MRI->getMatchingSuperReg(FirstReg, ARM::dsub_0, RC);
3781     }
3782 
3783     Operands.push_back(ARMOperand::CreateVectorList(FirstReg, Count,
3784                                                     (Spacing == 2), S, E));
3785     break;
3786   case AllLanes:
3787     // Two-register operands have been converted to the
3788     // composite register classes.
3789     if (Count == 2) {
3790       const MCRegisterClass *RC = (Spacing == 1) ?
3791         &ARMMCRegisterClasses[ARM::DPairRegClassID] :
3792         &ARMMCRegisterClasses[ARM::DPairSpcRegClassID];
3793       FirstReg = MRI->getMatchingSuperReg(FirstReg, ARM::dsub_0, RC);
3794     }
3795     Operands.push_back(ARMOperand::CreateVectorListAllLanes(FirstReg, Count,
3796                                                             (Spacing == 2),
3797                                                             S, E));
3798     break;
3799   case IndexedLane:
3800     Operands.push_back(ARMOperand::CreateVectorListIndexed(FirstReg, Count,
3801                                                            LaneIndex,
3802                                                            (Spacing == 2),
3803                                                            S, E));
3804     break;
3805   }
3806   return MatchOperand_Success;
3807 }
3808 
3809 /// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
3810 ARMAsmParser::OperandMatchResultTy
3811 ARMAsmParser::parseMemBarrierOptOperand(OperandVector &Operands) {
3812   MCAsmParser &Parser = getParser();
3813   SMLoc S = Parser.getTok().getLoc();
3814   const AsmToken &Tok = Parser.getTok();
3815   unsigned Opt;
3816 
3817   if (Tok.is(AsmToken::Identifier)) {
3818     StringRef OptStr = Tok.getString();
3819 
3820     Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()).lower())
3821       .Case("sy",    ARM_MB::SY)
3822       .Case("st",    ARM_MB::ST)
3823       .Case("ld",    ARM_MB::LD)
3824       .Case("sh",    ARM_MB::ISH)
3825       .Case("ish",   ARM_MB::ISH)
3826       .Case("shst",  ARM_MB::ISHST)
3827       .Case("ishst", ARM_MB::ISHST)
3828       .Case("ishld", ARM_MB::ISHLD)
3829       .Case("nsh",   ARM_MB::NSH)
3830       .Case("un",    ARM_MB::NSH)
3831       .Case("nshst", ARM_MB::NSHST)
3832       .Case("nshld", ARM_MB::NSHLD)
3833       .Case("unst",  ARM_MB::NSHST)
3834       .Case("osh",   ARM_MB::OSH)
3835       .Case("oshst", ARM_MB::OSHST)
3836       .Case("oshld", ARM_MB::OSHLD)
3837       .Default(~0U);
3838 
3839     // ishld, oshld, nshld and ld are only available from ARMv8.
3840     if (!hasV8Ops() && (Opt == ARM_MB::ISHLD || Opt == ARM_MB::OSHLD ||
3841                         Opt == ARM_MB::NSHLD || Opt == ARM_MB::LD))
3842       Opt = ~0U;
3843 
3844     if (Opt == ~0U)
3845       return MatchOperand_NoMatch;
3846 
3847     Parser.Lex(); // Eat identifier token.
3848   } else if (Tok.is(AsmToken::Hash) ||
3849              Tok.is(AsmToken::Dollar) ||
3850              Tok.is(AsmToken::Integer)) {
3851     if (Parser.getTok().isNot(AsmToken::Integer))
3852       Parser.Lex(); // Eat '#' or '$'.
3853     SMLoc Loc = Parser.getTok().getLoc();
3854 
3855     const MCExpr *MemBarrierID;
3856     if (getParser().parseExpression(MemBarrierID)) {
3857       Error(Loc, "illegal expression");
3858       return MatchOperand_ParseFail;
3859     }
3860 
3861     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(MemBarrierID);
3862     if (!CE) {
3863       Error(Loc, "constant expression expected");
3864       return MatchOperand_ParseFail;
3865     }
3866 
3867     int Val = CE->getValue();
3868     if (Val & ~0xf) {
3869       Error(Loc, "immediate value out of range");
3870       return MatchOperand_ParseFail;
3871     }
3872 
3873     Opt = ARM_MB::RESERVED_0 + Val;
3874   } else
3875     return MatchOperand_ParseFail;
3876 
3877   Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
3878   return MatchOperand_Success;
3879 }
3880 
3881 /// parseInstSyncBarrierOptOperand - Try to parse ISB inst sync barrier options.
3882 ARMAsmParser::OperandMatchResultTy
3883 ARMAsmParser::parseInstSyncBarrierOptOperand(OperandVector &Operands) {
3884   MCAsmParser &Parser = getParser();
3885   SMLoc S = Parser.getTok().getLoc();
3886   const AsmToken &Tok = Parser.getTok();
3887   unsigned Opt;
3888 
3889   if (Tok.is(AsmToken::Identifier)) {
3890     StringRef OptStr = Tok.getString();
3891 
3892     if (OptStr.equals_lower("sy"))
3893       Opt = ARM_ISB::SY;
3894     else
3895       return MatchOperand_NoMatch;
3896 
3897     Parser.Lex(); // Eat identifier token.
3898   } else if (Tok.is(AsmToken::Hash) ||
3899              Tok.is(AsmToken::Dollar) ||
3900              Tok.is(AsmToken::Integer)) {
3901     if (Parser.getTok().isNot(AsmToken::Integer))
3902       Parser.Lex(); // Eat '#' or '$'.
3903     SMLoc Loc = Parser.getTok().getLoc();
3904 
3905     const MCExpr *ISBarrierID;
3906     if (getParser().parseExpression(ISBarrierID)) {
3907       Error(Loc, "illegal expression");
3908       return MatchOperand_ParseFail;
3909     }
3910 
3911     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ISBarrierID);
3912     if (!CE) {
3913       Error(Loc, "constant expression expected");
3914       return MatchOperand_ParseFail;
3915     }
3916 
3917     int Val = CE->getValue();
3918     if (Val & ~0xf) {
3919       Error(Loc, "immediate value out of range");
3920       return MatchOperand_ParseFail;
3921     }
3922 
3923     Opt = ARM_ISB::RESERVED_0 + Val;
3924   } else
3925     return MatchOperand_ParseFail;
3926 
3927   Operands.push_back(ARMOperand::CreateInstSyncBarrierOpt(
3928           (ARM_ISB::InstSyncBOpt)Opt, S));
3929   return MatchOperand_Success;
3930 }
3931 
3932 
3933 /// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
3934 ARMAsmParser::OperandMatchResultTy
3935 ARMAsmParser::parseProcIFlagsOperand(OperandVector &Operands) {
3936   MCAsmParser &Parser = getParser();
3937   SMLoc S = Parser.getTok().getLoc();
3938   const AsmToken &Tok = Parser.getTok();
3939   if (!Tok.is(AsmToken::Identifier))
3940     return MatchOperand_NoMatch;
3941   StringRef IFlagsStr = Tok.getString();
3942 
3943   // An iflags string of "none" is interpreted to mean that none of the AIF
3944   // bits are set.  Not a terribly useful instruction, but a valid encoding.
3945   unsigned IFlags = 0;
3946   if (IFlagsStr != "none") {
3947         for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
3948       unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
3949         .Case("a", ARM_PROC::A)
3950         .Case("i", ARM_PROC::I)
3951         .Case("f", ARM_PROC::F)
3952         .Default(~0U);
3953 
3954       // If some specific iflag is already set, it means that some letter is
3955       // present more than once, this is not acceptable.
3956       if (Flag == ~0U || (IFlags & Flag))
3957         return MatchOperand_NoMatch;
3958 
3959       IFlags |= Flag;
3960     }
3961   }
3962 
3963   Parser.Lex(); // Eat identifier token.
3964   Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
3965   return MatchOperand_Success;
3966 }
3967 
3968 /// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
3969 ARMAsmParser::OperandMatchResultTy
3970 ARMAsmParser::parseMSRMaskOperand(OperandVector &Operands) {
3971   MCAsmParser &Parser = getParser();
3972   SMLoc S = Parser.getTok().getLoc();
3973   const AsmToken &Tok = Parser.getTok();
3974   if (!Tok.is(AsmToken::Identifier))
3975     return MatchOperand_NoMatch;
3976   StringRef Mask = Tok.getString();
3977 
3978   if (isMClass()) {
3979     // See ARMv6-M 10.1.1
3980     std::string Name = Mask.lower();
3981     unsigned FlagsVal = StringSwitch<unsigned>(Name)
3982       // Note: in the documentation:
3983       //  ARM deprecates using MSR APSR without a _<bits> qualifier as an alias
3984       //  for MSR APSR_nzcvq.
3985       // but we do make it an alias here.  This is so to get the "mask encoding"
3986       // bits correct on MSR APSR writes.
3987       //
3988       // FIXME: Note the 0xc00 "mask encoding" bits version of the registers
3989       // should really only be allowed when writing a special register.  Note
3990       // they get dropped in the MRS instruction reading a special register as
3991       // the SYSm field is only 8 bits.
3992       .Case("apsr", 0x800)
3993       .Case("apsr_nzcvq", 0x800)
3994       .Case("apsr_g", 0x400)
3995       .Case("apsr_nzcvqg", 0xc00)
3996       .Case("iapsr", 0x801)
3997       .Case("iapsr_nzcvq", 0x801)
3998       .Case("iapsr_g", 0x401)
3999       .Case("iapsr_nzcvqg", 0xc01)
4000       .Case("eapsr", 0x802)
4001       .Case("eapsr_nzcvq", 0x802)
4002       .Case("eapsr_g", 0x402)
4003       .Case("eapsr_nzcvqg", 0xc02)
4004       .Case("xpsr", 0x803)
4005       .Case("xpsr_nzcvq", 0x803)
4006       .Case("xpsr_g", 0x403)
4007       .Case("xpsr_nzcvqg", 0xc03)
4008       .Case("ipsr", 0x805)
4009       .Case("epsr", 0x806)
4010       .Case("iepsr", 0x807)
4011       .Case("msp", 0x808)
4012       .Case("psp", 0x809)
4013       .Case("primask", 0x810)
4014       .Case("basepri", 0x811)
4015       .Case("basepri_max", 0x812)
4016       .Case("faultmask", 0x813)
4017       .Case("control", 0x814)
4018       .Case("msplim", 0x80a)
4019       .Case("psplim", 0x80b)
4020       .Case("msp_ns", 0x888)
4021       .Case("psp_ns", 0x889)
4022       .Case("msplim_ns", 0x88a)
4023       .Case("psplim_ns", 0x88b)
4024       .Case("primask_ns", 0x890)
4025       .Case("basepri_ns", 0x891)
4026       .Case("basepri_max_ns", 0x892)
4027       .Case("faultmask_ns", 0x893)
4028       .Case("control_ns", 0x894)
4029       .Case("sp_ns", 0x898)
4030       .Default(~0U);
4031 
4032     if (FlagsVal == ~0U)
4033       return MatchOperand_NoMatch;
4034 
4035     if (!hasDSP() && (FlagsVal & 0x400))
4036       // The _g and _nzcvqg versions are only valid if the DSP extension is
4037       // available.
4038       return MatchOperand_NoMatch;
4039 
4040     if (!hasV7Ops() && FlagsVal >= 0x811 && FlagsVal <= 0x813)
4041       // basepri, basepri_max and faultmask only valid for V7m.
4042       return MatchOperand_NoMatch;
4043 
4044     if (!has8MSecExt() && (FlagsVal == 0x80a || FlagsVal == 0x80b ||
4045                              (FlagsVal > 0x814 && FlagsVal < 0xc00)))
4046       return MatchOperand_NoMatch;
4047 
4048     if (!hasV8MMainline() && (FlagsVal == 0x88a || FlagsVal == 0x88b ||
4049                               (FlagsVal > 0x890 && FlagsVal <= 0x893)))
4050       return MatchOperand_NoMatch;
4051 
4052     Parser.Lex(); // Eat identifier token.
4053     Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
4054     return MatchOperand_Success;
4055   }
4056 
4057   // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
4058   size_t Start = 0, Next = Mask.find('_');
4059   StringRef Flags = "";
4060   std::string SpecReg = Mask.slice(Start, Next).lower();
4061   if (Next != StringRef::npos)
4062     Flags = Mask.slice(Next+1, Mask.size());
4063 
4064   // FlagsVal contains the complete mask:
4065   // 3-0: Mask
4066   // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
4067   unsigned FlagsVal = 0;
4068 
4069   if (SpecReg == "apsr") {
4070     FlagsVal = StringSwitch<unsigned>(Flags)
4071     .Case("nzcvq",  0x8) // same as CPSR_f
4072     .Case("g",      0x4) // same as CPSR_s
4073     .Case("nzcvqg", 0xc) // same as CPSR_fs
4074     .Default(~0U);
4075 
4076     if (FlagsVal == ~0U) {
4077       if (!Flags.empty())
4078         return MatchOperand_NoMatch;
4079       else
4080         FlagsVal = 8; // No flag
4081     }
4082   } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
4083     // cpsr_all is an alias for cpsr_fc, as is plain cpsr.
4084     if (Flags == "all" || Flags == "")
4085       Flags = "fc";
4086     for (int i = 0, e = Flags.size(); i != e; ++i) {
4087       unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
4088       .Case("c", 1)
4089       .Case("x", 2)
4090       .Case("s", 4)
4091       .Case("f", 8)
4092       .Default(~0U);
4093 
4094       // If some specific flag is already set, it means that some letter is
4095       // present more than once, this is not acceptable.
4096       if (FlagsVal == ~0U || (FlagsVal & Flag))
4097         return MatchOperand_NoMatch;
4098       FlagsVal |= Flag;
4099     }
4100   } else // No match for special register.
4101     return MatchOperand_NoMatch;
4102 
4103   // Special register without flags is NOT equivalent to "fc" flags.
4104   // NOTE: This is a divergence from gas' behavior.  Uncommenting the following
4105   // two lines would enable gas compatibility at the expense of breaking
4106   // round-tripping.
4107   //
4108   // if (!FlagsVal)
4109   //  FlagsVal = 0x9;
4110 
4111   // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
4112   if (SpecReg == "spsr")
4113     FlagsVal |= 16;
4114 
4115   Parser.Lex(); // Eat identifier token.
4116   Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
4117   return MatchOperand_Success;
4118 }
4119 
4120 /// parseBankedRegOperand - Try to parse a banked register (e.g. "lr_irq") for
4121 /// use in the MRS/MSR instructions added to support virtualization.
4122 ARMAsmParser::OperandMatchResultTy
4123 ARMAsmParser::parseBankedRegOperand(OperandVector &Operands) {
4124   MCAsmParser &Parser = getParser();
4125   SMLoc S = Parser.getTok().getLoc();
4126   const AsmToken &Tok = Parser.getTok();
4127   if (!Tok.is(AsmToken::Identifier))
4128     return MatchOperand_NoMatch;
4129   StringRef RegName = Tok.getString();
4130 
4131   // The values here come from B9.2.3 of the ARM ARM, where bits 4-0 are SysM
4132   // and bit 5 is R.
4133   unsigned Encoding = StringSwitch<unsigned>(RegName.lower())
4134                           .Case("r8_usr", 0x00)
4135                           .Case("r9_usr", 0x01)
4136                           .Case("r10_usr", 0x02)
4137                           .Case("r11_usr", 0x03)
4138                           .Case("r12_usr", 0x04)
4139                           .Case("sp_usr", 0x05)
4140                           .Case("lr_usr", 0x06)
4141                           .Case("r8_fiq", 0x08)
4142                           .Case("r9_fiq", 0x09)
4143                           .Case("r10_fiq", 0x0a)
4144                           .Case("r11_fiq", 0x0b)
4145                           .Case("r12_fiq", 0x0c)
4146                           .Case("sp_fiq", 0x0d)
4147                           .Case("lr_fiq", 0x0e)
4148                           .Case("lr_irq", 0x10)
4149                           .Case("sp_irq", 0x11)
4150                           .Case("lr_svc", 0x12)
4151                           .Case("sp_svc", 0x13)
4152                           .Case("lr_abt", 0x14)
4153                           .Case("sp_abt", 0x15)
4154                           .Case("lr_und", 0x16)
4155                           .Case("sp_und", 0x17)
4156                           .Case("lr_mon", 0x1c)
4157                           .Case("sp_mon", 0x1d)
4158                           .Case("elr_hyp", 0x1e)
4159                           .Case("sp_hyp", 0x1f)
4160                           .Case("spsr_fiq", 0x2e)
4161                           .Case("spsr_irq", 0x30)
4162                           .Case("spsr_svc", 0x32)
4163                           .Case("spsr_abt", 0x34)
4164                           .Case("spsr_und", 0x36)
4165                           .Case("spsr_mon", 0x3c)
4166                           .Case("spsr_hyp", 0x3e)
4167                           .Default(~0U);
4168 
4169   if (Encoding == ~0U)
4170     return MatchOperand_NoMatch;
4171 
4172   Parser.Lex(); // Eat identifier token.
4173   Operands.push_back(ARMOperand::CreateBankedReg(Encoding, S));
4174   return MatchOperand_Success;
4175 }
4176 
4177 ARMAsmParser::OperandMatchResultTy
4178 ARMAsmParser::parsePKHImm(OperandVector &Operands, StringRef Op, int Low,
4179                           int High) {
4180   MCAsmParser &Parser = getParser();
4181   const AsmToken &Tok = Parser.getTok();
4182   if (Tok.isNot(AsmToken::Identifier)) {
4183     Error(Parser.getTok().getLoc(), Op + " operand expected.");
4184     return MatchOperand_ParseFail;
4185   }
4186   StringRef ShiftName = Tok.getString();
4187   std::string LowerOp = Op.lower();
4188   std::string UpperOp = Op.upper();
4189   if (ShiftName != LowerOp && ShiftName != UpperOp) {
4190     Error(Parser.getTok().getLoc(), Op + " operand expected.");
4191     return MatchOperand_ParseFail;
4192   }
4193   Parser.Lex(); // Eat shift type token.
4194 
4195   // There must be a '#' and a shift amount.
4196   if (Parser.getTok().isNot(AsmToken::Hash) &&
4197       Parser.getTok().isNot(AsmToken::Dollar)) {
4198     Error(Parser.getTok().getLoc(), "'#' expected");
4199     return MatchOperand_ParseFail;
4200   }
4201   Parser.Lex(); // Eat hash token.
4202 
4203   const MCExpr *ShiftAmount;
4204   SMLoc Loc = Parser.getTok().getLoc();
4205   SMLoc EndLoc;
4206   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
4207     Error(Loc, "illegal expression");
4208     return MatchOperand_ParseFail;
4209   }
4210   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
4211   if (!CE) {
4212     Error(Loc, "constant expression expected");
4213     return MatchOperand_ParseFail;
4214   }
4215   int Val = CE->getValue();
4216   if (Val < Low || Val > High) {
4217     Error(Loc, "immediate value out of range");
4218     return MatchOperand_ParseFail;
4219   }
4220 
4221   Operands.push_back(ARMOperand::CreateImm(CE, Loc, EndLoc));
4222 
4223   return MatchOperand_Success;
4224 }
4225 
4226 ARMAsmParser::OperandMatchResultTy
4227 ARMAsmParser::parseSetEndImm(OperandVector &Operands) {
4228   MCAsmParser &Parser = getParser();
4229   const AsmToken &Tok = Parser.getTok();
4230   SMLoc S = Tok.getLoc();
4231   if (Tok.isNot(AsmToken::Identifier)) {
4232     Error(S, "'be' or 'le' operand expected");
4233     return MatchOperand_ParseFail;
4234   }
4235   int Val = StringSwitch<int>(Tok.getString().lower())
4236     .Case("be", 1)
4237     .Case("le", 0)
4238     .Default(-1);
4239   Parser.Lex(); // Eat the token.
4240 
4241   if (Val == -1) {
4242     Error(S, "'be' or 'le' operand expected");
4243     return MatchOperand_ParseFail;
4244   }
4245   Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::create(Val,
4246                                                                   getContext()),
4247                                            S, Tok.getEndLoc()));
4248   return MatchOperand_Success;
4249 }
4250 
4251 /// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
4252 /// instructions. Legal values are:
4253 ///     lsl #n  'n' in [0,31]
4254 ///     asr #n  'n' in [1,32]
4255 ///             n == 32 encoded as n == 0.
4256 ARMAsmParser::OperandMatchResultTy
4257 ARMAsmParser::parseShifterImm(OperandVector &Operands) {
4258   MCAsmParser &Parser = getParser();
4259   const AsmToken &Tok = Parser.getTok();
4260   SMLoc S = Tok.getLoc();
4261   if (Tok.isNot(AsmToken::Identifier)) {
4262     Error(S, "shift operator 'asr' or 'lsl' expected");
4263     return MatchOperand_ParseFail;
4264   }
4265   StringRef ShiftName = Tok.getString();
4266   bool isASR;
4267   if (ShiftName == "lsl" || ShiftName == "LSL")
4268     isASR = false;
4269   else if (ShiftName == "asr" || ShiftName == "ASR")
4270     isASR = true;
4271   else {
4272     Error(S, "shift operator 'asr' or 'lsl' expected");
4273     return MatchOperand_ParseFail;
4274   }
4275   Parser.Lex(); // Eat the operator.
4276 
4277   // A '#' and a shift amount.
4278   if (Parser.getTok().isNot(AsmToken::Hash) &&
4279       Parser.getTok().isNot(AsmToken::Dollar)) {
4280     Error(Parser.getTok().getLoc(), "'#' expected");
4281     return MatchOperand_ParseFail;
4282   }
4283   Parser.Lex(); // Eat hash token.
4284   SMLoc ExLoc = Parser.getTok().getLoc();
4285 
4286   const MCExpr *ShiftAmount;
4287   SMLoc EndLoc;
4288   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
4289     Error(ExLoc, "malformed shift expression");
4290     return MatchOperand_ParseFail;
4291   }
4292   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
4293   if (!CE) {
4294     Error(ExLoc, "shift amount must be an immediate");
4295     return MatchOperand_ParseFail;
4296   }
4297 
4298   int64_t Val = CE->getValue();
4299   if (isASR) {
4300     // Shift amount must be in [1,32]
4301     if (Val < 1 || Val > 32) {
4302       Error(ExLoc, "'asr' shift amount must be in range [1,32]");
4303       return MatchOperand_ParseFail;
4304     }
4305     // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
4306     if (isThumb() && Val == 32) {
4307       Error(ExLoc, "'asr #32' shift amount not allowed in Thumb mode");
4308       return MatchOperand_ParseFail;
4309     }
4310     if (Val == 32) Val = 0;
4311   } else {
4312     // Shift amount must be in [1,32]
4313     if (Val < 0 || Val > 31) {
4314       Error(ExLoc, "'lsr' shift amount must be in range [0,31]");
4315       return MatchOperand_ParseFail;
4316     }
4317   }
4318 
4319   Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, EndLoc));
4320 
4321   return MatchOperand_Success;
4322 }
4323 
4324 /// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
4325 /// of instructions. Legal values are:
4326 ///     ror #n  'n' in {0, 8, 16, 24}
4327 ARMAsmParser::OperandMatchResultTy
4328 ARMAsmParser::parseRotImm(OperandVector &Operands) {
4329   MCAsmParser &Parser = getParser();
4330   const AsmToken &Tok = Parser.getTok();
4331   SMLoc S = Tok.getLoc();
4332   if (Tok.isNot(AsmToken::Identifier))
4333     return MatchOperand_NoMatch;
4334   StringRef ShiftName = Tok.getString();
4335   if (ShiftName != "ror" && ShiftName != "ROR")
4336     return MatchOperand_NoMatch;
4337   Parser.Lex(); // Eat the operator.
4338 
4339   // A '#' and a rotate amount.
4340   if (Parser.getTok().isNot(AsmToken::Hash) &&
4341       Parser.getTok().isNot(AsmToken::Dollar)) {
4342     Error(Parser.getTok().getLoc(), "'#' expected");
4343     return MatchOperand_ParseFail;
4344   }
4345   Parser.Lex(); // Eat hash token.
4346   SMLoc ExLoc = Parser.getTok().getLoc();
4347 
4348   const MCExpr *ShiftAmount;
4349   SMLoc EndLoc;
4350   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
4351     Error(ExLoc, "malformed rotate expression");
4352     return MatchOperand_ParseFail;
4353   }
4354   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
4355   if (!CE) {
4356     Error(ExLoc, "rotate amount must be an immediate");
4357     return MatchOperand_ParseFail;
4358   }
4359 
4360   int64_t Val = CE->getValue();
4361   // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
4362   // normally, zero is represented in asm by omitting the rotate operand
4363   // entirely.
4364   if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
4365     Error(ExLoc, "'ror' rotate amount must be 8, 16, or 24");
4366     return MatchOperand_ParseFail;
4367   }
4368 
4369   Operands.push_back(ARMOperand::CreateRotImm(Val, S, EndLoc));
4370 
4371   return MatchOperand_Success;
4372 }
4373 
4374 ARMAsmParser::OperandMatchResultTy
4375 ARMAsmParser::parseModImm(OperandVector &Operands) {
4376   MCAsmParser &Parser = getParser();
4377   MCAsmLexer &Lexer = getLexer();
4378   int64_t Imm1, Imm2;
4379 
4380   SMLoc S = Parser.getTok().getLoc();
4381 
4382   // 1) A mod_imm operand can appear in the place of a register name:
4383   //   add r0, #mod_imm
4384   //   add r0, r0, #mod_imm
4385   // to correctly handle the latter, we bail out as soon as we see an
4386   // identifier.
4387   //
4388   // 2) Similarly, we do not want to parse into complex operands:
4389   //   mov r0, #mod_imm
4390   //   mov r0, :lower16:(_foo)
4391   if (Parser.getTok().is(AsmToken::Identifier) ||
4392       Parser.getTok().is(AsmToken::Colon))
4393     return MatchOperand_NoMatch;
4394 
4395   // Hash (dollar) is optional as per the ARMARM
4396   if (Parser.getTok().is(AsmToken::Hash) ||
4397       Parser.getTok().is(AsmToken::Dollar)) {
4398     // Avoid parsing into complex operands (#:)
4399     if (Lexer.peekTok().is(AsmToken::Colon))
4400       return MatchOperand_NoMatch;
4401 
4402     // Eat the hash (dollar)
4403     Parser.Lex();
4404   }
4405 
4406   SMLoc Sx1, Ex1;
4407   Sx1 = Parser.getTok().getLoc();
4408   const MCExpr *Imm1Exp;
4409   if (getParser().parseExpression(Imm1Exp, Ex1)) {
4410     Error(Sx1, "malformed expression");
4411     return MatchOperand_ParseFail;
4412   }
4413 
4414   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm1Exp);
4415 
4416   if (CE) {
4417     // Immediate must fit within 32-bits
4418     Imm1 = CE->getValue();
4419     int Enc = ARM_AM::getSOImmVal(Imm1);
4420     if (Enc != -1 && Parser.getTok().is(AsmToken::EndOfStatement)) {
4421       // We have a match!
4422       Operands.push_back(ARMOperand::CreateModImm((Enc & 0xFF),
4423                                                   (Enc & 0xF00) >> 7,
4424                                                   Sx1, Ex1));
4425       return MatchOperand_Success;
4426     }
4427 
4428     // We have parsed an immediate which is not for us, fallback to a plain
4429     // immediate. This can happen for instruction aliases. For an example,
4430     // ARMInstrInfo.td defines the alias [mov <-> mvn] which can transform
4431     // a mov (mvn) with a mod_imm_neg/mod_imm_not operand into the opposite
4432     // instruction with a mod_imm operand. The alias is defined such that the
4433     // parser method is shared, that's why we have to do this here.
4434     if (Parser.getTok().is(AsmToken::EndOfStatement)) {
4435       Operands.push_back(ARMOperand::CreateImm(Imm1Exp, Sx1, Ex1));
4436       return MatchOperand_Success;
4437     }
4438   } else {
4439     // Operands like #(l1 - l2) can only be evaluated at a later stage (via an
4440     // MCFixup). Fallback to a plain immediate.
4441     Operands.push_back(ARMOperand::CreateImm(Imm1Exp, Sx1, Ex1));
4442     return MatchOperand_Success;
4443   }
4444 
4445   // From this point onward, we expect the input to be a (#bits, #rot) pair
4446   if (Parser.getTok().isNot(AsmToken::Comma)) {
4447     Error(Sx1, "expected modified immediate operand: #[0, 255], #even[0-30]");
4448     return MatchOperand_ParseFail;
4449   }
4450 
4451   if (Imm1 & ~0xFF) {
4452     Error(Sx1, "immediate operand must a number in the range [0, 255]");
4453     return MatchOperand_ParseFail;
4454   }
4455 
4456   // Eat the comma
4457   Parser.Lex();
4458 
4459   // Repeat for #rot
4460   SMLoc Sx2, Ex2;
4461   Sx2 = Parser.getTok().getLoc();
4462 
4463   // Eat the optional hash (dollar)
4464   if (Parser.getTok().is(AsmToken::Hash) ||
4465       Parser.getTok().is(AsmToken::Dollar))
4466     Parser.Lex();
4467 
4468   const MCExpr *Imm2Exp;
4469   if (getParser().parseExpression(Imm2Exp, Ex2)) {
4470     Error(Sx2, "malformed expression");
4471     return MatchOperand_ParseFail;
4472   }
4473 
4474   CE = dyn_cast<MCConstantExpr>(Imm2Exp);
4475 
4476   if (CE) {
4477     Imm2 = CE->getValue();
4478     if (!(Imm2 & ~0x1E)) {
4479       // We have a match!
4480       Operands.push_back(ARMOperand::CreateModImm(Imm1, Imm2, S, Ex2));
4481       return MatchOperand_Success;
4482     }
4483     Error(Sx2, "immediate operand must an even number in the range [0, 30]");
4484     return MatchOperand_ParseFail;
4485   } else {
4486     Error(Sx2, "constant expression expected");
4487     return MatchOperand_ParseFail;
4488   }
4489 }
4490 
4491 ARMAsmParser::OperandMatchResultTy
4492 ARMAsmParser::parseBitfield(OperandVector &Operands) {
4493   MCAsmParser &Parser = getParser();
4494   SMLoc S = Parser.getTok().getLoc();
4495   // The bitfield descriptor is really two operands, the LSB and the width.
4496   if (Parser.getTok().isNot(AsmToken::Hash) &&
4497       Parser.getTok().isNot(AsmToken::Dollar)) {
4498     Error(Parser.getTok().getLoc(), "'#' expected");
4499     return MatchOperand_ParseFail;
4500   }
4501   Parser.Lex(); // Eat hash token.
4502 
4503   const MCExpr *LSBExpr;
4504   SMLoc E = Parser.getTok().getLoc();
4505   if (getParser().parseExpression(LSBExpr)) {
4506     Error(E, "malformed immediate expression");
4507     return MatchOperand_ParseFail;
4508   }
4509   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
4510   if (!CE) {
4511     Error(E, "'lsb' operand must be an immediate");
4512     return MatchOperand_ParseFail;
4513   }
4514 
4515   int64_t LSB = CE->getValue();
4516   // The LSB must be in the range [0,31]
4517   if (LSB < 0 || LSB > 31) {
4518     Error(E, "'lsb' operand must be in the range [0,31]");
4519     return MatchOperand_ParseFail;
4520   }
4521   E = Parser.getTok().getLoc();
4522 
4523   // Expect another immediate operand.
4524   if (Parser.getTok().isNot(AsmToken::Comma)) {
4525     Error(Parser.getTok().getLoc(), "too few operands");
4526     return MatchOperand_ParseFail;
4527   }
4528   Parser.Lex(); // Eat hash token.
4529   if (Parser.getTok().isNot(AsmToken::Hash) &&
4530       Parser.getTok().isNot(AsmToken::Dollar)) {
4531     Error(Parser.getTok().getLoc(), "'#' expected");
4532     return MatchOperand_ParseFail;
4533   }
4534   Parser.Lex(); // Eat hash token.
4535 
4536   const MCExpr *WidthExpr;
4537   SMLoc EndLoc;
4538   if (getParser().parseExpression(WidthExpr, EndLoc)) {
4539     Error(E, "malformed immediate expression");
4540     return MatchOperand_ParseFail;
4541   }
4542   CE = dyn_cast<MCConstantExpr>(WidthExpr);
4543   if (!CE) {
4544     Error(E, "'width' operand must be an immediate");
4545     return MatchOperand_ParseFail;
4546   }
4547 
4548   int64_t Width = CE->getValue();
4549   // The LSB must be in the range [1,32-lsb]
4550   if (Width < 1 || Width > 32 - LSB) {
4551     Error(E, "'width' operand must be in the range [1,32-lsb]");
4552     return MatchOperand_ParseFail;
4553   }
4554 
4555   Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, EndLoc));
4556 
4557   return MatchOperand_Success;
4558 }
4559 
4560 ARMAsmParser::OperandMatchResultTy
4561 ARMAsmParser::parsePostIdxReg(OperandVector &Operands) {
4562   // Check for a post-index addressing register operand. Specifically:
4563   // postidx_reg := '+' register {, shift}
4564   //              | '-' register {, shift}
4565   //              | register {, shift}
4566 
4567   // This method must return MatchOperand_NoMatch without consuming any tokens
4568   // in the case where there is no match, as other alternatives take other
4569   // parse methods.
4570   MCAsmParser &Parser = getParser();
4571   AsmToken Tok = Parser.getTok();
4572   SMLoc S = Tok.getLoc();
4573   bool haveEaten = false;
4574   bool isAdd = true;
4575   if (Tok.is(AsmToken::Plus)) {
4576     Parser.Lex(); // Eat the '+' token.
4577     haveEaten = true;
4578   } else if (Tok.is(AsmToken::Minus)) {
4579     Parser.Lex(); // Eat the '-' token.
4580     isAdd = false;
4581     haveEaten = true;
4582   }
4583 
4584   SMLoc E = Parser.getTok().getEndLoc();
4585   int Reg = tryParseRegister();
4586   if (Reg == -1) {
4587     if (!haveEaten)
4588       return MatchOperand_NoMatch;
4589     Error(Parser.getTok().getLoc(), "register expected");
4590     return MatchOperand_ParseFail;
4591   }
4592 
4593   ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
4594   unsigned ShiftImm = 0;
4595   if (Parser.getTok().is(AsmToken::Comma)) {
4596     Parser.Lex(); // Eat the ','.
4597     if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
4598       return MatchOperand_ParseFail;
4599 
4600     // FIXME: Only approximates end...may include intervening whitespace.
4601     E = Parser.getTok().getLoc();
4602   }
4603 
4604   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
4605                                                   ShiftImm, S, E));
4606 
4607   return MatchOperand_Success;
4608 }
4609 
4610 ARMAsmParser::OperandMatchResultTy
4611 ARMAsmParser::parseAM3Offset(OperandVector &Operands) {
4612   // Check for a post-index addressing register operand. Specifically:
4613   // am3offset := '+' register
4614   //              | '-' register
4615   //              | register
4616   //              | # imm
4617   //              | # + imm
4618   //              | # - imm
4619 
4620   // This method must return MatchOperand_NoMatch without consuming any tokens
4621   // in the case where there is no match, as other alternatives take other
4622   // parse methods.
4623   MCAsmParser &Parser = getParser();
4624   AsmToken Tok = Parser.getTok();
4625   SMLoc S = Tok.getLoc();
4626 
4627   // Do immediates first, as we always parse those if we have a '#'.
4628   if (Parser.getTok().is(AsmToken::Hash) ||
4629       Parser.getTok().is(AsmToken::Dollar)) {
4630     Parser.Lex(); // Eat '#' or '$'.
4631     // Explicitly look for a '-', as we need to encode negative zero
4632     // differently.
4633     bool isNegative = Parser.getTok().is(AsmToken::Minus);
4634     const MCExpr *Offset;
4635     SMLoc E;
4636     if (getParser().parseExpression(Offset, E))
4637       return MatchOperand_ParseFail;
4638     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
4639     if (!CE) {
4640       Error(S, "constant expression expected");
4641       return MatchOperand_ParseFail;
4642     }
4643     // Negative zero is encoded as the flag value INT32_MIN.
4644     int32_t Val = CE->getValue();
4645     if (isNegative && Val == 0)
4646       Val = INT32_MIN;
4647 
4648     Operands.push_back(
4649       ARMOperand::CreateImm(MCConstantExpr::create(Val, getContext()), S, E));
4650 
4651     return MatchOperand_Success;
4652   }
4653 
4654 
4655   bool haveEaten = false;
4656   bool isAdd = true;
4657   if (Tok.is(AsmToken::Plus)) {
4658     Parser.Lex(); // Eat the '+' token.
4659     haveEaten = true;
4660   } else if (Tok.is(AsmToken::Minus)) {
4661     Parser.Lex(); // Eat the '-' token.
4662     isAdd = false;
4663     haveEaten = true;
4664   }
4665 
4666   Tok = Parser.getTok();
4667   int Reg = tryParseRegister();
4668   if (Reg == -1) {
4669     if (!haveEaten)
4670       return MatchOperand_NoMatch;
4671     Error(Tok.getLoc(), "register expected");
4672     return MatchOperand_ParseFail;
4673   }
4674 
4675   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
4676                                                   0, S, Tok.getEndLoc()));
4677 
4678   return MatchOperand_Success;
4679 }
4680 
4681 /// Convert parsed operands to MCInst.  Needed here because this instruction
4682 /// only has two register operands, but multiplication is commutative so
4683 /// assemblers should accept both "mul rD, rN, rD" and "mul rD, rD, rN".
4684 void ARMAsmParser::cvtThumbMultiply(MCInst &Inst,
4685                                     const OperandVector &Operands) {
4686   ((ARMOperand &)*Operands[3]).addRegOperands(Inst, 1);
4687   ((ARMOperand &)*Operands[1]).addCCOutOperands(Inst, 1);
4688   // If we have a three-operand form, make sure to set Rn to be the operand
4689   // that isn't the same as Rd.
4690   unsigned RegOp = 4;
4691   if (Operands.size() == 6 &&
4692       ((ARMOperand &)*Operands[4]).getReg() ==
4693           ((ARMOperand &)*Operands[3]).getReg())
4694     RegOp = 5;
4695   ((ARMOperand &)*Operands[RegOp]).addRegOperands(Inst, 1);
4696   Inst.addOperand(Inst.getOperand(0));
4697   ((ARMOperand &)*Operands[2]).addCondCodeOperands(Inst, 2);
4698 }
4699 
4700 void ARMAsmParser::cvtThumbBranches(MCInst &Inst,
4701                                     const OperandVector &Operands) {
4702   int CondOp = -1, ImmOp = -1;
4703   switch(Inst.getOpcode()) {
4704     case ARM::tB:
4705     case ARM::tBcc:  CondOp = 1; ImmOp = 2; break;
4706 
4707     case ARM::t2B:
4708     case ARM::t2Bcc: CondOp = 1; ImmOp = 3; break;
4709 
4710     default: llvm_unreachable("Unexpected instruction in cvtThumbBranches");
4711   }
4712   // first decide whether or not the branch should be conditional
4713   // by looking at it's location relative to an IT block
4714   if(inITBlock()) {
4715     // inside an IT block we cannot have any conditional branches. any
4716     // such instructions needs to be converted to unconditional form
4717     switch(Inst.getOpcode()) {
4718       case ARM::tBcc: Inst.setOpcode(ARM::tB); break;
4719       case ARM::t2Bcc: Inst.setOpcode(ARM::t2B); break;
4720     }
4721   } else {
4722     // outside IT blocks we can only have unconditional branches with AL
4723     // condition code or conditional branches with non-AL condition code
4724     unsigned Cond = static_cast<ARMOperand &>(*Operands[CondOp]).getCondCode();
4725     switch(Inst.getOpcode()) {
4726       case ARM::tB:
4727       case ARM::tBcc:
4728         Inst.setOpcode(Cond == ARMCC::AL ? ARM::tB : ARM::tBcc);
4729         break;
4730       case ARM::t2B:
4731       case ARM::t2Bcc:
4732         Inst.setOpcode(Cond == ARMCC::AL ? ARM::t2B : ARM::t2Bcc);
4733         break;
4734     }
4735   }
4736 
4737   // now decide on encoding size based on branch target range
4738   switch(Inst.getOpcode()) {
4739     // classify tB as either t2B or t1B based on range of immediate operand
4740     case ARM::tB: {
4741       ARMOperand &op = static_cast<ARMOperand &>(*Operands[ImmOp]);
4742       if (!op.isSignedOffset<11, 1>() && isThumb() && hasV8MBaseline())
4743         Inst.setOpcode(ARM::t2B);
4744       break;
4745     }
4746     // classify tBcc as either t2Bcc or t1Bcc based on range of immediate operand
4747     case ARM::tBcc: {
4748       ARMOperand &op = static_cast<ARMOperand &>(*Operands[ImmOp]);
4749       if (!op.isSignedOffset<8, 1>() && isThumb() && hasV8MBaseline())
4750         Inst.setOpcode(ARM::t2Bcc);
4751       break;
4752     }
4753   }
4754   ((ARMOperand &)*Operands[ImmOp]).addImmOperands(Inst, 1);
4755   ((ARMOperand &)*Operands[CondOp]).addCondCodeOperands(Inst, 2);
4756 }
4757 
4758 /// Parse an ARM memory expression, return false if successful else return true
4759 /// or an error.  The first token must be a '[' when called.
4760 bool ARMAsmParser::parseMemory(OperandVector &Operands) {
4761   MCAsmParser &Parser = getParser();
4762   SMLoc S, E;
4763   assert(Parser.getTok().is(AsmToken::LBrac) &&
4764          "Token is not a Left Bracket");
4765   S = Parser.getTok().getLoc();
4766   Parser.Lex(); // Eat left bracket token.
4767 
4768   const AsmToken &BaseRegTok = Parser.getTok();
4769   int BaseRegNum = tryParseRegister();
4770   if (BaseRegNum == -1)
4771     return Error(BaseRegTok.getLoc(), "register expected");
4772 
4773   // The next token must either be a comma, a colon or a closing bracket.
4774   const AsmToken &Tok = Parser.getTok();
4775   if (!Tok.is(AsmToken::Colon) && !Tok.is(AsmToken::Comma) &&
4776       !Tok.is(AsmToken::RBrac))
4777     return Error(Tok.getLoc(), "malformed memory operand");
4778 
4779   if (Tok.is(AsmToken::RBrac)) {
4780     E = Tok.getEndLoc();
4781     Parser.Lex(); // Eat right bracket token.
4782 
4783     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, 0,
4784                                              ARM_AM::no_shift, 0, 0, false,
4785                                              S, E));
4786 
4787     // If there's a pre-indexing writeback marker, '!', just add it as a token
4788     // operand. It's rather odd, but syntactically valid.
4789     if (Parser.getTok().is(AsmToken::Exclaim)) {
4790       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4791       Parser.Lex(); // Eat the '!'.
4792     }
4793 
4794     return false;
4795   }
4796 
4797   assert((Tok.is(AsmToken::Colon) || Tok.is(AsmToken::Comma)) &&
4798          "Lost colon or comma in memory operand?!");
4799   if (Tok.is(AsmToken::Comma)) {
4800     Parser.Lex(); // Eat the comma.
4801   }
4802 
4803   // If we have a ':', it's an alignment specifier.
4804   if (Parser.getTok().is(AsmToken::Colon)) {
4805     Parser.Lex(); // Eat the ':'.
4806     E = Parser.getTok().getLoc();
4807     SMLoc AlignmentLoc = Tok.getLoc();
4808 
4809     const MCExpr *Expr;
4810     if (getParser().parseExpression(Expr))
4811      return true;
4812 
4813     // The expression has to be a constant. Memory references with relocations
4814     // don't come through here, as they use the <label> forms of the relevant
4815     // instructions.
4816     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
4817     if (!CE)
4818       return Error (E, "constant expression expected");
4819 
4820     unsigned Align = 0;
4821     switch (CE->getValue()) {
4822     default:
4823       return Error(E,
4824                    "alignment specifier must be 16, 32, 64, 128, or 256 bits");
4825     case 16:  Align = 2; break;
4826     case 32:  Align = 4; break;
4827     case 64:  Align = 8; break;
4828     case 128: Align = 16; break;
4829     case 256: Align = 32; break;
4830     }
4831 
4832     // Now we should have the closing ']'
4833     if (Parser.getTok().isNot(AsmToken::RBrac))
4834       return Error(Parser.getTok().getLoc(), "']' expected");
4835     E = Parser.getTok().getEndLoc();
4836     Parser.Lex(); // Eat right bracket token.
4837 
4838     // Don't worry about range checking the value here. That's handled by
4839     // the is*() predicates.
4840     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, 0,
4841                                              ARM_AM::no_shift, 0, Align,
4842                                              false, S, E, AlignmentLoc));
4843 
4844     // If there's a pre-indexing writeback marker, '!', just add it as a token
4845     // operand.
4846     if (Parser.getTok().is(AsmToken::Exclaim)) {
4847       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4848       Parser.Lex(); // Eat the '!'.
4849     }
4850 
4851     return false;
4852   }
4853 
4854   // If we have a '#', it's an immediate offset, else assume it's a register
4855   // offset. Be friendly and also accept a plain integer (without a leading
4856   // hash) for gas compatibility.
4857   if (Parser.getTok().is(AsmToken::Hash) ||
4858       Parser.getTok().is(AsmToken::Dollar) ||
4859       Parser.getTok().is(AsmToken::Integer)) {
4860     if (Parser.getTok().isNot(AsmToken::Integer))
4861       Parser.Lex(); // Eat '#' or '$'.
4862     E = Parser.getTok().getLoc();
4863 
4864     bool isNegative = getParser().getTok().is(AsmToken::Minus);
4865     const MCExpr *Offset;
4866     if (getParser().parseExpression(Offset))
4867      return true;
4868 
4869     // The expression has to be a constant. Memory references with relocations
4870     // don't come through here, as they use the <label> forms of the relevant
4871     // instructions.
4872     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
4873     if (!CE)
4874       return Error (E, "constant expression expected");
4875 
4876     // If the constant was #-0, represent it as INT32_MIN.
4877     int32_t Val = CE->getValue();
4878     if (isNegative && Val == 0)
4879       CE = MCConstantExpr::create(INT32_MIN, getContext());
4880 
4881     // Now we should have the closing ']'
4882     if (Parser.getTok().isNot(AsmToken::RBrac))
4883       return Error(Parser.getTok().getLoc(), "']' expected");
4884     E = Parser.getTok().getEndLoc();
4885     Parser.Lex(); // Eat right bracket token.
4886 
4887     // Don't worry about range checking the value here. That's handled by
4888     // the is*() predicates.
4889     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
4890                                              ARM_AM::no_shift, 0, 0,
4891                                              false, S, E));
4892 
4893     // If there's a pre-indexing writeback marker, '!', just add it as a token
4894     // operand.
4895     if (Parser.getTok().is(AsmToken::Exclaim)) {
4896       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4897       Parser.Lex(); // Eat the '!'.
4898     }
4899 
4900     return false;
4901   }
4902 
4903   // The register offset is optionally preceded by a '+' or '-'
4904   bool isNegative = false;
4905   if (Parser.getTok().is(AsmToken::Minus)) {
4906     isNegative = true;
4907     Parser.Lex(); // Eat the '-'.
4908   } else if (Parser.getTok().is(AsmToken::Plus)) {
4909     // Nothing to do.
4910     Parser.Lex(); // Eat the '+'.
4911   }
4912 
4913   E = Parser.getTok().getLoc();
4914   int OffsetRegNum = tryParseRegister();
4915   if (OffsetRegNum == -1)
4916     return Error(E, "register expected");
4917 
4918   // If there's a shift operator, handle it.
4919   ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
4920   unsigned ShiftImm = 0;
4921   if (Parser.getTok().is(AsmToken::Comma)) {
4922     Parser.Lex(); // Eat the ','.
4923     if (parseMemRegOffsetShift(ShiftType, ShiftImm))
4924       return true;
4925   }
4926 
4927   // Now we should have the closing ']'
4928   if (Parser.getTok().isNot(AsmToken::RBrac))
4929     return Error(Parser.getTok().getLoc(), "']' expected");
4930   E = Parser.getTok().getEndLoc();
4931   Parser.Lex(); // Eat right bracket token.
4932 
4933   Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, OffsetRegNum,
4934                                            ShiftType, ShiftImm, 0, isNegative,
4935                                            S, E));
4936 
4937   // If there's a pre-indexing writeback marker, '!', just add it as a token
4938   // operand.
4939   if (Parser.getTok().is(AsmToken::Exclaim)) {
4940     Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4941     Parser.Lex(); // Eat the '!'.
4942   }
4943 
4944   return false;
4945 }
4946 
4947 /// parseMemRegOffsetShift - one of these two:
4948 ///   ( lsl | lsr | asr | ror ) , # shift_amount
4949 ///   rrx
4950 /// return true if it parses a shift otherwise it returns false.
4951 bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
4952                                           unsigned &Amount) {
4953   MCAsmParser &Parser = getParser();
4954   SMLoc Loc = Parser.getTok().getLoc();
4955   const AsmToken &Tok = Parser.getTok();
4956   if (Tok.isNot(AsmToken::Identifier))
4957     return true;
4958   StringRef ShiftName = Tok.getString();
4959   if (ShiftName == "lsl" || ShiftName == "LSL" ||
4960       ShiftName == "asl" || ShiftName == "ASL")
4961     St = ARM_AM::lsl;
4962   else if (ShiftName == "lsr" || ShiftName == "LSR")
4963     St = ARM_AM::lsr;
4964   else if (ShiftName == "asr" || ShiftName == "ASR")
4965     St = ARM_AM::asr;
4966   else if (ShiftName == "ror" || ShiftName == "ROR")
4967     St = ARM_AM::ror;
4968   else if (ShiftName == "rrx" || ShiftName == "RRX")
4969     St = ARM_AM::rrx;
4970   else
4971     return Error(Loc, "illegal shift operator");
4972   Parser.Lex(); // Eat shift type token.
4973 
4974   // rrx stands alone.
4975   Amount = 0;
4976   if (St != ARM_AM::rrx) {
4977     Loc = Parser.getTok().getLoc();
4978     // A '#' and a shift amount.
4979     const AsmToken &HashTok = Parser.getTok();
4980     if (HashTok.isNot(AsmToken::Hash) &&
4981         HashTok.isNot(AsmToken::Dollar))
4982       return Error(HashTok.getLoc(), "'#' expected");
4983     Parser.Lex(); // Eat hash token.
4984 
4985     const MCExpr *Expr;
4986     if (getParser().parseExpression(Expr))
4987       return true;
4988     // Range check the immediate.
4989     // lsl, ror: 0 <= imm <= 31
4990     // lsr, asr: 0 <= imm <= 32
4991     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
4992     if (!CE)
4993       return Error(Loc, "shift amount must be an immediate");
4994     int64_t Imm = CE->getValue();
4995     if (Imm < 0 ||
4996         ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
4997         ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
4998       return Error(Loc, "immediate shift value out of range");
4999     // If <ShiftTy> #0, turn it into a no_shift.
5000     if (Imm == 0)
5001       St = ARM_AM::lsl;
5002     // For consistency, treat lsr #32 and asr #32 as having immediate value 0.
5003     if (Imm == 32)
5004       Imm = 0;
5005     Amount = Imm;
5006   }
5007 
5008   return false;
5009 }
5010 
5011 /// parseFPImm - A floating point immediate expression operand.
5012 ARMAsmParser::OperandMatchResultTy
5013 ARMAsmParser::parseFPImm(OperandVector &Operands) {
5014   MCAsmParser &Parser = getParser();
5015   // Anything that can accept a floating point constant as an operand
5016   // needs to go through here, as the regular parseExpression is
5017   // integer only.
5018   //
5019   // This routine still creates a generic Immediate operand, containing
5020   // a bitcast of the 64-bit floating point value. The various operands
5021   // that accept floats can check whether the value is valid for them
5022   // via the standard is*() predicates.
5023 
5024   SMLoc S = Parser.getTok().getLoc();
5025 
5026   if (Parser.getTok().isNot(AsmToken::Hash) &&
5027       Parser.getTok().isNot(AsmToken::Dollar))
5028     return MatchOperand_NoMatch;
5029 
5030   // Disambiguate the VMOV forms that can accept an FP immediate.
5031   // vmov.f32 <sreg>, #imm
5032   // vmov.f64 <dreg>, #imm
5033   // vmov.f32 <dreg>, #imm  @ vector f32x2
5034   // vmov.f32 <qreg>, #imm  @ vector f32x4
5035   //
5036   // There are also the NEON VMOV instructions which expect an
5037   // integer constant. Make sure we don't try to parse an FPImm
5038   // for these:
5039   // vmov.i{8|16|32|64} <dreg|qreg>, #imm
5040   ARMOperand &TyOp = static_cast<ARMOperand &>(*Operands[2]);
5041   bool isVmovf = TyOp.isToken() &&
5042                  (TyOp.getToken() == ".f32" || TyOp.getToken() == ".f64" ||
5043                   TyOp.getToken() == ".f16");
5044   ARMOperand &Mnemonic = static_cast<ARMOperand &>(*Operands[0]);
5045   bool isFconst = Mnemonic.isToken() && (Mnemonic.getToken() == "fconstd" ||
5046                                          Mnemonic.getToken() == "fconsts");
5047   if (!(isVmovf || isFconst))
5048     return MatchOperand_NoMatch;
5049 
5050   Parser.Lex(); // Eat '#' or '$'.
5051 
5052   // Handle negation, as that still comes through as a separate token.
5053   bool isNegative = false;
5054   if (Parser.getTok().is(AsmToken::Minus)) {
5055     isNegative = true;
5056     Parser.Lex();
5057   }
5058   const AsmToken &Tok = Parser.getTok();
5059   SMLoc Loc = Tok.getLoc();
5060   if (Tok.is(AsmToken::Real) && isVmovf) {
5061     APFloat RealVal(APFloat::IEEEsingle, Tok.getString());
5062     uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
5063     // If we had a '-' in front, toggle the sign bit.
5064     IntVal ^= (uint64_t)isNegative << 31;
5065     Parser.Lex(); // Eat the token.
5066     Operands.push_back(ARMOperand::CreateImm(
5067           MCConstantExpr::create(IntVal, getContext()),
5068           S, Parser.getTok().getLoc()));
5069     return MatchOperand_Success;
5070   }
5071   // Also handle plain integers. Instructions which allow floating point
5072   // immediates also allow a raw encoded 8-bit value.
5073   if (Tok.is(AsmToken::Integer) && isFconst) {
5074     int64_t Val = Tok.getIntVal();
5075     Parser.Lex(); // Eat the token.
5076     if (Val > 255 || Val < 0) {
5077       Error(Loc, "encoded floating point value out of range");
5078       return MatchOperand_ParseFail;
5079     }
5080     float RealVal = ARM_AM::getFPImmFloat(Val);
5081     Val = APFloat(RealVal).bitcastToAPInt().getZExtValue();
5082 
5083     Operands.push_back(ARMOperand::CreateImm(
5084         MCConstantExpr::create(Val, getContext()), S,
5085         Parser.getTok().getLoc()));
5086     return MatchOperand_Success;
5087   }
5088 
5089   Error(Loc, "invalid floating point immediate");
5090   return MatchOperand_ParseFail;
5091 }
5092 
5093 /// Parse a arm instruction operand.  For now this parses the operand regardless
5094 /// of the mnemonic.
5095 bool ARMAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
5096   MCAsmParser &Parser = getParser();
5097   SMLoc S, E;
5098 
5099   // Check if the current operand has a custom associated parser, if so, try to
5100   // custom parse the operand, or fallback to the general approach.
5101   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
5102   if (ResTy == MatchOperand_Success)
5103     return false;
5104   // If there wasn't a custom match, try the generic matcher below. Otherwise,
5105   // there was a match, but an error occurred, in which case, just return that
5106   // the operand parsing failed.
5107   if (ResTy == MatchOperand_ParseFail)
5108     return true;
5109 
5110   switch (getLexer().getKind()) {
5111   default:
5112     Error(Parser.getTok().getLoc(), "unexpected token in operand");
5113     return true;
5114   case AsmToken::Identifier: {
5115     // If we've seen a branch mnemonic, the next operand must be a label.  This
5116     // is true even if the label is a register name.  So "br r1" means branch to
5117     // label "r1".
5118     bool ExpectLabel = Mnemonic == "b" || Mnemonic == "bl";
5119     if (!ExpectLabel) {
5120       if (!tryParseRegisterWithWriteBack(Operands))
5121         return false;
5122       int Res = tryParseShiftRegister(Operands);
5123       if (Res == 0) // success
5124         return false;
5125       else if (Res == -1) // irrecoverable error
5126         return true;
5127       // If this is VMRS, check for the apsr_nzcv operand.
5128       if (Mnemonic == "vmrs" &&
5129           Parser.getTok().getString().equals_lower("apsr_nzcv")) {
5130         S = Parser.getTok().getLoc();
5131         Parser.Lex();
5132         Operands.push_back(ARMOperand::CreateToken("APSR_nzcv", S));
5133         return false;
5134       }
5135     }
5136 
5137     // Fall though for the Identifier case that is not a register or a
5138     // special name.
5139   }
5140   case AsmToken::LParen:  // parenthesized expressions like (_strcmp-4)
5141   case AsmToken::Integer: // things like 1f and 2b as a branch targets
5142   case AsmToken::String:  // quoted label names.
5143   case AsmToken::Dot: {   // . as a branch target
5144     // This was not a register so parse other operands that start with an
5145     // identifier (like labels) as expressions and create them as immediates.
5146     const MCExpr *IdVal;
5147     S = Parser.getTok().getLoc();
5148     if (getParser().parseExpression(IdVal))
5149       return true;
5150     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5151     Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
5152     return false;
5153   }
5154   case AsmToken::LBrac:
5155     return parseMemory(Operands);
5156   case AsmToken::LCurly:
5157     return parseRegisterList(Operands);
5158   case AsmToken::Dollar:
5159   case AsmToken::Hash: {
5160     // #42 -> immediate.
5161     S = Parser.getTok().getLoc();
5162     Parser.Lex();
5163 
5164     if (Parser.getTok().isNot(AsmToken::Colon)) {
5165       bool isNegative = Parser.getTok().is(AsmToken::Minus);
5166       const MCExpr *ImmVal;
5167       if (getParser().parseExpression(ImmVal))
5168         return true;
5169       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
5170       if (CE) {
5171         int32_t Val = CE->getValue();
5172         if (isNegative && Val == 0)
5173           ImmVal = MCConstantExpr::create(INT32_MIN, getContext());
5174       }
5175       E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5176       Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
5177 
5178       // There can be a trailing '!' on operands that we want as a separate
5179       // '!' Token operand. Handle that here. For example, the compatibility
5180       // alias for 'srsdb sp!, #imm' is 'srsdb #imm!'.
5181       if (Parser.getTok().is(AsmToken::Exclaim)) {
5182         Operands.push_back(ARMOperand::CreateToken(Parser.getTok().getString(),
5183                                                    Parser.getTok().getLoc()));
5184         Parser.Lex(); // Eat exclaim token
5185       }
5186       return false;
5187     }
5188     // w/ a ':' after the '#', it's just like a plain ':'.
5189     // FALLTHROUGH
5190   }
5191   case AsmToken::Colon: {
5192     S = Parser.getTok().getLoc();
5193     // ":lower16:" and ":upper16:" expression prefixes
5194     // FIXME: Check it's an expression prefix,
5195     // e.g. (FOO - :lower16:BAR) isn't legal.
5196     ARMMCExpr::VariantKind RefKind;
5197     if (parsePrefix(RefKind))
5198       return true;
5199 
5200     const MCExpr *SubExprVal;
5201     if (getParser().parseExpression(SubExprVal))
5202       return true;
5203 
5204     const MCExpr *ExprVal = ARMMCExpr::create(RefKind, SubExprVal,
5205                                               getContext());
5206     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5207     Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
5208     return false;
5209   }
5210   case AsmToken::Equal: {
5211     S = Parser.getTok().getLoc();
5212     if (Mnemonic != "ldr") // only parse for ldr pseudo (e.g. ldr r0, =val)
5213       return Error(S, "unexpected token in operand");
5214 
5215     Parser.Lex(); // Eat '='
5216     const MCExpr *SubExprVal;
5217     if (getParser().parseExpression(SubExprVal))
5218       return true;
5219     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5220 
5221     const MCExpr *CPLoc =
5222         getTargetStreamer().addConstantPoolEntry(SubExprVal, S);
5223     Operands.push_back(ARMOperand::CreateImm(CPLoc, S, E));
5224     return false;
5225   }
5226   }
5227 }
5228 
5229 // parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
5230 //  :lower16: and :upper16:.
5231 bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
5232   MCAsmParser &Parser = getParser();
5233   RefKind = ARMMCExpr::VK_ARM_None;
5234 
5235   // consume an optional '#' (GNU compatibility)
5236   if (getLexer().is(AsmToken::Hash))
5237     Parser.Lex();
5238 
5239   // :lower16: and :upper16: modifiers
5240   assert(getLexer().is(AsmToken::Colon) && "expected a :");
5241   Parser.Lex(); // Eat ':'
5242 
5243   if (getLexer().isNot(AsmToken::Identifier)) {
5244     Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
5245     return true;
5246   }
5247 
5248   enum {
5249     COFF = (1 << MCObjectFileInfo::IsCOFF),
5250     ELF = (1 << MCObjectFileInfo::IsELF),
5251     MACHO = (1 << MCObjectFileInfo::IsMachO)
5252   };
5253   static const struct PrefixEntry {
5254     const char *Spelling;
5255     ARMMCExpr::VariantKind VariantKind;
5256     uint8_t SupportedFormats;
5257   } PrefixEntries[] = {
5258     { "lower16", ARMMCExpr::VK_ARM_LO16, COFF | ELF | MACHO },
5259     { "upper16", ARMMCExpr::VK_ARM_HI16, COFF | ELF | MACHO },
5260   };
5261 
5262   StringRef IDVal = Parser.getTok().getIdentifier();
5263 
5264   const auto &Prefix =
5265       std::find_if(std::begin(PrefixEntries), std::end(PrefixEntries),
5266                    [&IDVal](const PrefixEntry &PE) {
5267                       return PE.Spelling == IDVal;
5268                    });
5269   if (Prefix == std::end(PrefixEntries)) {
5270     Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
5271     return true;
5272   }
5273 
5274   uint8_t CurrentFormat;
5275   switch (getContext().getObjectFileInfo()->getObjectFileType()) {
5276   case MCObjectFileInfo::IsMachO:
5277     CurrentFormat = MACHO;
5278     break;
5279   case MCObjectFileInfo::IsELF:
5280     CurrentFormat = ELF;
5281     break;
5282   case MCObjectFileInfo::IsCOFF:
5283     CurrentFormat = COFF;
5284     break;
5285   }
5286 
5287   if (~Prefix->SupportedFormats & CurrentFormat) {
5288     Error(Parser.getTok().getLoc(),
5289           "cannot represent relocation in the current file format");
5290     return true;
5291   }
5292 
5293   RefKind = Prefix->VariantKind;
5294   Parser.Lex();
5295 
5296   if (getLexer().isNot(AsmToken::Colon)) {
5297     Error(Parser.getTok().getLoc(), "unexpected token after prefix");
5298     return true;
5299   }
5300   Parser.Lex(); // Eat the last ':'
5301 
5302   return false;
5303 }
5304 
5305 /// \brief Given a mnemonic, split out possible predication code and carry
5306 /// setting letters to form a canonical mnemonic and flags.
5307 //
5308 // FIXME: Would be nice to autogen this.
5309 // FIXME: This is a bit of a maze of special cases.
5310 StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
5311                                       unsigned &PredicationCode,
5312                                       bool &CarrySetting,
5313                                       unsigned &ProcessorIMod,
5314                                       StringRef &ITMask) {
5315   PredicationCode = ARMCC::AL;
5316   CarrySetting = false;
5317   ProcessorIMod = 0;
5318 
5319   // Ignore some mnemonics we know aren't predicated forms.
5320   //
5321   // FIXME: Would be nice to autogen this.
5322   if ((Mnemonic == "movs" && isThumb()) ||
5323       Mnemonic == "teq"   || Mnemonic == "vceq"   || Mnemonic == "svc"   ||
5324       Mnemonic == "mls"   || Mnemonic == "smmls"  || Mnemonic == "vcls"  ||
5325       Mnemonic == "vmls"  || Mnemonic == "vnmls"  || Mnemonic == "vacge" ||
5326       Mnemonic == "vcge"  || Mnemonic == "vclt"   || Mnemonic == "vacgt" ||
5327       Mnemonic == "vaclt" || Mnemonic == "vacle"  || Mnemonic == "hlt" ||
5328       Mnemonic == "vcgt"  || Mnemonic == "vcle"   || Mnemonic == "smlal" ||
5329       Mnemonic == "umaal" || Mnemonic == "umlal"  || Mnemonic == "vabal" ||
5330       Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal" ||
5331       Mnemonic == "fmuls" || Mnemonic == "vmaxnm" || Mnemonic == "vminnm" ||
5332       Mnemonic == "vcvta" || Mnemonic == "vcvtn"  || Mnemonic == "vcvtp" ||
5333       Mnemonic == "vcvtm" || Mnemonic == "vrinta" || Mnemonic == "vrintn" ||
5334       Mnemonic == "vrintp" || Mnemonic == "vrintm" || Mnemonic == "hvc" ||
5335       Mnemonic.startswith("vsel") || Mnemonic == "vins" || Mnemonic == "vmovx" ||
5336       Mnemonic == "bxns"  || Mnemonic == "blxns")
5337     return Mnemonic;
5338 
5339   // First, split out any predication code. Ignore mnemonics we know aren't
5340   // predicated but do have a carry-set and so weren't caught above.
5341   if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
5342       Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
5343       Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
5344       Mnemonic != "sbcs" && Mnemonic != "rscs") {
5345     unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
5346       .Case("eq", ARMCC::EQ)
5347       .Case("ne", ARMCC::NE)
5348       .Case("hs", ARMCC::HS)
5349       .Case("cs", ARMCC::HS)
5350       .Case("lo", ARMCC::LO)
5351       .Case("cc", ARMCC::LO)
5352       .Case("mi", ARMCC::MI)
5353       .Case("pl", ARMCC::PL)
5354       .Case("vs", ARMCC::VS)
5355       .Case("vc", ARMCC::VC)
5356       .Case("hi", ARMCC::HI)
5357       .Case("ls", ARMCC::LS)
5358       .Case("ge", ARMCC::GE)
5359       .Case("lt", ARMCC::LT)
5360       .Case("gt", ARMCC::GT)
5361       .Case("le", ARMCC::LE)
5362       .Case("al", ARMCC::AL)
5363       .Default(~0U);
5364     if (CC != ~0U) {
5365       Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
5366       PredicationCode = CC;
5367     }
5368   }
5369 
5370   // Next, determine if we have a carry setting bit. We explicitly ignore all
5371   // the instructions we know end in 's'.
5372   if (Mnemonic.endswith("s") &&
5373       !(Mnemonic == "cps" || Mnemonic == "mls" ||
5374         Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
5375         Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
5376         Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
5377         Mnemonic == "vrsqrts" || Mnemonic == "srs" || Mnemonic == "flds" ||
5378         Mnemonic == "fmrs" || Mnemonic == "fsqrts" || Mnemonic == "fsubs" ||
5379         Mnemonic == "fsts" || Mnemonic == "fcpys" || Mnemonic == "fdivs" ||
5380         Mnemonic == "fmuls" || Mnemonic == "fcmps" || Mnemonic == "fcmpzs" ||
5381         Mnemonic == "vfms" || Mnemonic == "vfnms" || Mnemonic == "fconsts" ||
5382         (Mnemonic == "movs" && isThumb()))) {
5383     Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
5384     CarrySetting = true;
5385   }
5386 
5387   // The "cps" instruction can have a interrupt mode operand which is glued into
5388   // the mnemonic. Check if this is the case, split it and parse the imod op
5389   if (Mnemonic.startswith("cps")) {
5390     // Split out any imod code.
5391     unsigned IMod =
5392       StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
5393       .Case("ie", ARM_PROC::IE)
5394       .Case("id", ARM_PROC::ID)
5395       .Default(~0U);
5396     if (IMod != ~0U) {
5397       Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
5398       ProcessorIMod = IMod;
5399     }
5400   }
5401 
5402   // The "it" instruction has the condition mask on the end of the mnemonic.
5403   if (Mnemonic.startswith("it")) {
5404     ITMask = Mnemonic.slice(2, Mnemonic.size());
5405     Mnemonic = Mnemonic.slice(0, 2);
5406   }
5407 
5408   return Mnemonic;
5409 }
5410 
5411 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
5412 /// inclusion of carry set or predication code operands.
5413 //
5414 // FIXME: It would be nice to autogen this.
5415 void ARMAsmParser::getMnemonicAcceptInfo(StringRef Mnemonic, StringRef FullInst,
5416                                          bool &CanAcceptCarrySet,
5417                                          bool &CanAcceptPredicationCode) {
5418   CanAcceptCarrySet =
5419       Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
5420       Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
5421       Mnemonic == "add" || Mnemonic == "adc" || Mnemonic == "mul" ||
5422       Mnemonic == "bic" || Mnemonic == "asr" || Mnemonic == "orr" ||
5423       Mnemonic == "mvn" || Mnemonic == "rsb" || Mnemonic == "rsc" ||
5424       Mnemonic == "orn" || Mnemonic == "sbc" || Mnemonic == "eor" ||
5425       Mnemonic == "neg" || Mnemonic == "vfm" || Mnemonic == "vfnm" ||
5426       (!isThumb() &&
5427        (Mnemonic == "smull" || Mnemonic == "mov" || Mnemonic == "mla" ||
5428         Mnemonic == "smlal" || Mnemonic == "umlal" || Mnemonic == "umull"));
5429 
5430   if (Mnemonic == "bkpt" || Mnemonic == "cbnz" || Mnemonic == "setend" ||
5431       Mnemonic == "cps" || Mnemonic == "it" || Mnemonic == "cbz" ||
5432       Mnemonic == "trap" || Mnemonic == "hlt" || Mnemonic == "udf" ||
5433       Mnemonic.startswith("crc32") || Mnemonic.startswith("cps") ||
5434       Mnemonic.startswith("vsel") || Mnemonic == "vmaxnm" ||
5435       Mnemonic == "vminnm" || Mnemonic == "vcvta" || Mnemonic == "vcvtn" ||
5436       Mnemonic == "vcvtp" || Mnemonic == "vcvtm" || Mnemonic == "vrinta" ||
5437       Mnemonic == "vrintn" || Mnemonic == "vrintp" || Mnemonic == "vrintm" ||
5438       Mnemonic.startswith("aes") || Mnemonic == "hvc" || Mnemonic == "setpan" ||
5439       Mnemonic.startswith("sha1") || Mnemonic.startswith("sha256") ||
5440       (FullInst.startswith("vmull") && FullInst.endswith(".p64")) ||
5441       Mnemonic == "vmovx" || Mnemonic == "vins") {
5442     // These mnemonics are never predicable
5443     CanAcceptPredicationCode = false;
5444   } else if (!isThumb()) {
5445     // Some instructions are only predicable in Thumb mode
5446     CanAcceptPredicationCode =
5447         Mnemonic != "cdp2" && Mnemonic != "clrex" && Mnemonic != "mcr2" &&
5448         Mnemonic != "mcrr2" && Mnemonic != "mrc2" && Mnemonic != "mrrc2" &&
5449         Mnemonic != "dmb" && Mnemonic != "dsb" && Mnemonic != "isb" &&
5450         Mnemonic != "pld" && Mnemonic != "pli" && Mnemonic != "pldw" &&
5451         Mnemonic != "ldc2" && Mnemonic != "ldc2l" && Mnemonic != "stc2" &&
5452         Mnemonic != "stc2l" && !Mnemonic.startswith("rfe") &&
5453         !Mnemonic.startswith("srs");
5454   } else if (isThumbOne()) {
5455     if (hasV6MOps())
5456       CanAcceptPredicationCode = Mnemonic != "movs";
5457     else
5458       CanAcceptPredicationCode = Mnemonic != "nop" && Mnemonic != "movs";
5459   } else
5460     CanAcceptPredicationCode = true;
5461 }
5462 
5463 // \brief Some Thumb instructions have two operand forms that are not
5464 // available as three operand, convert to two operand form if possible.
5465 //
5466 // FIXME: We would really like to be able to tablegen'erate this.
5467 void ARMAsmParser::tryConvertingToTwoOperandForm(StringRef Mnemonic,
5468                                                  bool CarrySetting,
5469                                                  OperandVector &Operands) {
5470   if (Operands.size() != 6)
5471     return;
5472 
5473   const auto &Op3 = static_cast<ARMOperand &>(*Operands[3]);
5474         auto &Op4 = static_cast<ARMOperand &>(*Operands[4]);
5475   if (!Op3.isReg() || !Op4.isReg())
5476     return;
5477 
5478   auto Op3Reg = Op3.getReg();
5479   auto Op4Reg = Op4.getReg();
5480 
5481   // For most Thumb2 cases we just generate the 3 operand form and reduce
5482   // it in processInstruction(), but the 3 operand form of ADD (t2ADDrr)
5483   // won't accept SP or PC so we do the transformation here taking care
5484   // with immediate range in the 'add sp, sp #imm' case.
5485   auto &Op5 = static_cast<ARMOperand &>(*Operands[5]);
5486   if (isThumbTwo()) {
5487     if (Mnemonic != "add")
5488       return;
5489     bool TryTransform = Op3Reg == ARM::PC || Op4Reg == ARM::PC ||
5490                         (Op5.isReg() && Op5.getReg() == ARM::PC);
5491     if (!TryTransform) {
5492       TryTransform = (Op3Reg == ARM::SP || Op4Reg == ARM::SP ||
5493                       (Op5.isReg() && Op5.getReg() == ARM::SP)) &&
5494                      !(Op3Reg == ARM::SP && Op4Reg == ARM::SP &&
5495                        Op5.isImm() && !Op5.isImm0_508s4());
5496     }
5497     if (!TryTransform)
5498       return;
5499   } else if (!isThumbOne())
5500     return;
5501 
5502   if (!(Mnemonic == "add" || Mnemonic == "sub" || Mnemonic == "and" ||
5503         Mnemonic == "eor" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
5504         Mnemonic == "asr" || Mnemonic == "adc" || Mnemonic == "sbc" ||
5505         Mnemonic == "ror" || Mnemonic == "orr" || Mnemonic == "bic"))
5506     return;
5507 
5508   // If first 2 operands of a 3 operand instruction are the same
5509   // then transform to 2 operand version of the same instruction
5510   // e.g. 'adds r0, r0, #1' transforms to 'adds r0, #1'
5511   bool Transform = Op3Reg == Op4Reg;
5512 
5513   // For communtative operations, we might be able to transform if we swap
5514   // Op4 and Op5.  The 'ADD Rdm, SP, Rdm' form is already handled specially
5515   // as tADDrsp.
5516   const ARMOperand *LastOp = &Op5;
5517   bool Swap = false;
5518   if (!Transform && Op5.isReg() && Op3Reg == Op5.getReg() &&
5519       ((Mnemonic == "add" && Op4Reg != ARM::SP) ||
5520        Mnemonic == "and" || Mnemonic == "eor" ||
5521        Mnemonic == "adc" || Mnemonic == "orr")) {
5522     Swap = true;
5523     LastOp = &Op4;
5524     Transform = true;
5525   }
5526 
5527   // If both registers are the same then remove one of them from
5528   // the operand list, with certain exceptions.
5529   if (Transform) {
5530     // Don't transform 'adds Rd, Rd, Rm' or 'sub{s} Rd, Rd, Rm' because the
5531     // 2 operand forms don't exist.
5532     if (((Mnemonic == "add" && CarrySetting) || Mnemonic == "sub") &&
5533         LastOp->isReg())
5534       Transform = false;
5535 
5536     // Don't transform 'add/sub{s} Rd, Rd, #imm' if the immediate fits into
5537     // 3-bits because the ARMARM says not to.
5538     if ((Mnemonic == "add" || Mnemonic == "sub") && LastOp->isImm0_7())
5539       Transform = false;
5540   }
5541 
5542   if (Transform) {
5543     if (Swap)
5544       std::swap(Op4, Op5);
5545     Operands.erase(Operands.begin() + 3);
5546   }
5547 }
5548 
5549 bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
5550                                           OperandVector &Operands) {
5551   // FIXME: This is all horribly hacky. We really need a better way to deal
5552   // with optional operands like this in the matcher table.
5553 
5554   // The 'mov' mnemonic is special. One variant has a cc_out operand, while
5555   // another does not. Specifically, the MOVW instruction does not. So we
5556   // special case it here and remove the defaulted (non-setting) cc_out
5557   // operand if that's the instruction we're trying to match.
5558   //
5559   // We do this as post-processing of the explicit operands rather than just
5560   // conditionally adding the cc_out in the first place because we need
5561   // to check the type of the parsed immediate operand.
5562   if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
5563       !static_cast<ARMOperand &>(*Operands[4]).isModImm() &&
5564       static_cast<ARMOperand &>(*Operands[4]).isImm0_65535Expr() &&
5565       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0)
5566     return true;
5567 
5568   // Register-register 'add' for thumb does not have a cc_out operand
5569   // when there are only two register operands.
5570   if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
5571       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5572       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5573       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0)
5574     return true;
5575   // Register-register 'add' for thumb does not have a cc_out operand
5576   // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
5577   // have to check the immediate range here since Thumb2 has a variant
5578   // that can handle a different range and has a cc_out operand.
5579   if (((isThumb() && Mnemonic == "add") ||
5580        (isThumbTwo() && Mnemonic == "sub")) &&
5581       Operands.size() == 6 && static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5582       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5583       static_cast<ARMOperand &>(*Operands[4]).getReg() == ARM::SP &&
5584       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5585       ((Mnemonic == "add" && static_cast<ARMOperand &>(*Operands[5]).isReg()) ||
5586        static_cast<ARMOperand &>(*Operands[5]).isImm0_1020s4()))
5587     return true;
5588   // For Thumb2, add/sub immediate does not have a cc_out operand for the
5589   // imm0_4095 variant. That's the least-preferred variant when
5590   // selecting via the generic "add" mnemonic, so to know that we
5591   // should remove the cc_out operand, we have to explicitly check that
5592   // it's not one of the other variants. Ugh.
5593   if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
5594       Operands.size() == 6 && static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5595       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5596       static_cast<ARMOperand &>(*Operands[5]).isImm()) {
5597     // Nest conditions rather than one big 'if' statement for readability.
5598     //
5599     // If both registers are low, we're in an IT block, and the immediate is
5600     // in range, we should use encoding T1 instead, which has a cc_out.
5601     if (inITBlock() &&
5602         isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) &&
5603         isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) &&
5604         static_cast<ARMOperand &>(*Operands[5]).isImm0_7())
5605       return false;
5606     // Check against T3. If the second register is the PC, this is an
5607     // alternate form of ADR, which uses encoding T4, so check for that too.
5608     if (static_cast<ARMOperand &>(*Operands[4]).getReg() != ARM::PC &&
5609         static_cast<ARMOperand &>(*Operands[5]).isT2SOImm())
5610       return false;
5611 
5612     // Otherwise, we use encoding T4, which does not have a cc_out
5613     // operand.
5614     return true;
5615   }
5616 
5617   // The thumb2 multiply instruction doesn't have a CCOut register, so
5618   // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
5619   // use the 16-bit encoding or not.
5620   if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
5621       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5622       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5623       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5624       static_cast<ARMOperand &>(*Operands[5]).isReg() &&
5625       // If the registers aren't low regs, the destination reg isn't the
5626       // same as one of the source regs, or the cc_out operand is zero
5627       // outside of an IT block, we have to use the 32-bit encoding, so
5628       // remove the cc_out operand.
5629       (!isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) ||
5630        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) ||
5631        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[5]).getReg()) ||
5632        !inITBlock() || (static_cast<ARMOperand &>(*Operands[3]).getReg() !=
5633                             static_cast<ARMOperand &>(*Operands[5]).getReg() &&
5634                         static_cast<ARMOperand &>(*Operands[3]).getReg() !=
5635                             static_cast<ARMOperand &>(*Operands[4]).getReg())))
5636     return true;
5637 
5638   // Also check the 'mul' syntax variant that doesn't specify an explicit
5639   // destination register.
5640   if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 5 &&
5641       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5642       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5643       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5644       // If the registers aren't low regs  or the cc_out operand is zero
5645       // outside of an IT block, we have to use the 32-bit encoding, so
5646       // remove the cc_out operand.
5647       (!isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) ||
5648        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) ||
5649        !inITBlock()))
5650     return true;
5651 
5652 
5653 
5654   // Register-register 'add/sub' for thumb does not have a cc_out operand
5655   // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
5656   // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
5657   // right, this will result in better diagnostics (which operand is off)
5658   // anyway.
5659   if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
5660       (Operands.size() == 5 || Operands.size() == 6) &&
5661       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5662       static_cast<ARMOperand &>(*Operands[3]).getReg() == ARM::SP &&
5663       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5664       (static_cast<ARMOperand &>(*Operands[4]).isImm() ||
5665        (Operands.size() == 6 &&
5666         static_cast<ARMOperand &>(*Operands[5]).isImm())))
5667     return true;
5668 
5669   return false;
5670 }
5671 
5672 bool ARMAsmParser::shouldOmitPredicateOperand(StringRef Mnemonic,
5673                                               OperandVector &Operands) {
5674   // VRINT{Z, R, X} have a predicate operand in VFP, but not in NEON
5675   unsigned RegIdx = 3;
5676   if ((Mnemonic == "vrintz" || Mnemonic == "vrintx" || Mnemonic == "vrintr") &&
5677       (static_cast<ARMOperand &>(*Operands[2]).getToken() == ".f32" ||
5678        static_cast<ARMOperand &>(*Operands[2]).getToken() == ".f16")) {
5679     if (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
5680         (static_cast<ARMOperand &>(*Operands[3]).getToken() == ".f32" ||
5681          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".f16"))
5682       RegIdx = 4;
5683 
5684     if (static_cast<ARMOperand &>(*Operands[RegIdx]).isReg() &&
5685         (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(
5686              static_cast<ARMOperand &>(*Operands[RegIdx]).getReg()) ||
5687          ARMMCRegisterClasses[ARM::QPRRegClassID].contains(
5688              static_cast<ARMOperand &>(*Operands[RegIdx]).getReg())))
5689       return true;
5690   }
5691   return false;
5692 }
5693 
5694 static bool isDataTypeToken(StringRef Tok) {
5695   return Tok == ".8" || Tok == ".16" || Tok == ".32" || Tok == ".64" ||
5696     Tok == ".i8" || Tok == ".i16" || Tok == ".i32" || Tok == ".i64" ||
5697     Tok == ".u8" || Tok == ".u16" || Tok == ".u32" || Tok == ".u64" ||
5698     Tok == ".s8" || Tok == ".s16" || Tok == ".s32" || Tok == ".s64" ||
5699     Tok == ".p8" || Tok == ".p16" || Tok == ".f32" || Tok == ".f64" ||
5700     Tok == ".f" || Tok == ".d";
5701 }
5702 
5703 // FIXME: This bit should probably be handled via an explicit match class
5704 // in the .td files that matches the suffix instead of having it be
5705 // a literal string token the way it is now.
5706 static bool doesIgnoreDataTypeSuffix(StringRef Mnemonic, StringRef DT) {
5707   return Mnemonic.startswith("vldm") || Mnemonic.startswith("vstm");
5708 }
5709 static void applyMnemonicAliases(StringRef &Mnemonic, uint64_t Features,
5710                                  unsigned VariantID);
5711 
5712 static bool RequiresVFPRegListValidation(StringRef Inst,
5713                                          bool &AcceptSinglePrecisionOnly,
5714                                          bool &AcceptDoublePrecisionOnly) {
5715   if (Inst.size() < 7)
5716     return false;
5717 
5718   if (Inst.startswith("fldm") || Inst.startswith("fstm")) {
5719     StringRef AddressingMode = Inst.substr(4, 2);
5720     if (AddressingMode == "ia" || AddressingMode == "db" ||
5721         AddressingMode == "ea" || AddressingMode == "fd") {
5722       AcceptSinglePrecisionOnly = Inst[6] == 's';
5723       AcceptDoublePrecisionOnly = Inst[6] == 'd' || Inst[6] == 'x';
5724       return true;
5725     }
5726   }
5727 
5728   return false;
5729 }
5730 
5731 /// Parse an arm instruction mnemonic followed by its operands.
5732 bool ARMAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
5733                                     SMLoc NameLoc, OperandVector &Operands) {
5734   MCAsmParser &Parser = getParser();
5735   // FIXME: Can this be done via tablegen in some fashion?
5736   bool RequireVFPRegisterListCheck;
5737   bool AcceptSinglePrecisionOnly;
5738   bool AcceptDoublePrecisionOnly;
5739   RequireVFPRegisterListCheck =
5740     RequiresVFPRegListValidation(Name, AcceptSinglePrecisionOnly,
5741                                  AcceptDoublePrecisionOnly);
5742 
5743   // Apply mnemonic aliases before doing anything else, as the destination
5744   // mnemonic may include suffices and we want to handle them normally.
5745   // The generic tblgen'erated code does this later, at the start of
5746   // MatchInstructionImpl(), but that's too late for aliases that include
5747   // any sort of suffix.
5748   uint64_t AvailableFeatures = getAvailableFeatures();
5749   unsigned AssemblerDialect = getParser().getAssemblerDialect();
5750   applyMnemonicAliases(Name, AvailableFeatures, AssemblerDialect);
5751 
5752   // First check for the ARM-specific .req directive.
5753   if (Parser.getTok().is(AsmToken::Identifier) &&
5754       Parser.getTok().getIdentifier() == ".req") {
5755     parseDirectiveReq(Name, NameLoc);
5756     // We always return 'error' for this, as we're done with this
5757     // statement and don't need to match the 'instruction."
5758     return true;
5759   }
5760 
5761   // Create the leading tokens for the mnemonic, split by '.' characters.
5762   size_t Start = 0, Next = Name.find('.');
5763   StringRef Mnemonic = Name.slice(Start, Next);
5764 
5765   // Split out the predication code and carry setting flag from the mnemonic.
5766   unsigned PredicationCode;
5767   unsigned ProcessorIMod;
5768   bool CarrySetting;
5769   StringRef ITMask;
5770   Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
5771                            ProcessorIMod, ITMask);
5772 
5773   // In Thumb1, only the branch (B) instruction can be predicated.
5774   if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
5775     Parser.eatToEndOfStatement();
5776     return Error(NameLoc, "conditional execution not supported in Thumb1");
5777   }
5778 
5779   Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
5780 
5781   // Handle the IT instruction ITMask. Convert it to a bitmask. This
5782   // is the mask as it will be for the IT encoding if the conditional
5783   // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
5784   // where the conditional bit0 is zero, the instruction post-processing
5785   // will adjust the mask accordingly.
5786   if (Mnemonic == "it") {
5787     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
5788     if (ITMask.size() > 3) {
5789       Parser.eatToEndOfStatement();
5790       return Error(Loc, "too many conditions on IT instruction");
5791     }
5792     unsigned Mask = 8;
5793     for (unsigned i = ITMask.size(); i != 0; --i) {
5794       char pos = ITMask[i - 1];
5795       if (pos != 't' && pos != 'e') {
5796         Parser.eatToEndOfStatement();
5797         return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
5798       }
5799       Mask >>= 1;
5800       if (ITMask[i - 1] == 't')
5801         Mask |= 8;
5802     }
5803     Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
5804   }
5805 
5806   // FIXME: This is all a pretty gross hack. We should automatically handle
5807   // optional operands like this via tblgen.
5808 
5809   // Next, add the CCOut and ConditionCode operands, if needed.
5810   //
5811   // For mnemonics which can ever incorporate a carry setting bit or predication
5812   // code, our matching model involves us always generating CCOut and
5813   // ConditionCode operands to match the mnemonic "as written" and then we let
5814   // the matcher deal with finding the right instruction or generating an
5815   // appropriate error.
5816   bool CanAcceptCarrySet, CanAcceptPredicationCode;
5817   getMnemonicAcceptInfo(Mnemonic, Name, CanAcceptCarrySet, CanAcceptPredicationCode);
5818 
5819   // If we had a carry-set on an instruction that can't do that, issue an
5820   // error.
5821   if (!CanAcceptCarrySet && CarrySetting) {
5822     Parser.eatToEndOfStatement();
5823     return Error(NameLoc, "instruction '" + Mnemonic +
5824                  "' can not set flags, but 's' suffix specified");
5825   }
5826   // If we had a predication code on an instruction that can't do that, issue an
5827   // error.
5828   if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
5829     Parser.eatToEndOfStatement();
5830     return Error(NameLoc, "instruction '" + Mnemonic +
5831                  "' is not predicable, but condition code specified");
5832   }
5833 
5834   // Add the carry setting operand, if necessary.
5835   if (CanAcceptCarrySet) {
5836     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
5837     Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
5838                                                Loc));
5839   }
5840 
5841   // Add the predication code operand, if necessary.
5842   if (CanAcceptPredicationCode) {
5843     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
5844                                       CarrySetting);
5845     Operands.push_back(ARMOperand::CreateCondCode(
5846                          ARMCC::CondCodes(PredicationCode), Loc));
5847   }
5848 
5849   // Add the processor imod operand, if necessary.
5850   if (ProcessorIMod) {
5851     Operands.push_back(ARMOperand::CreateImm(
5852           MCConstantExpr::create(ProcessorIMod, getContext()),
5853                                  NameLoc, NameLoc));
5854   } else if (Mnemonic == "cps" && isMClass()) {
5855     return Error(NameLoc, "instruction 'cps' requires effect for M-class");
5856   }
5857 
5858   // Add the remaining tokens in the mnemonic.
5859   while (Next != StringRef::npos) {
5860     Start = Next;
5861     Next = Name.find('.', Start + 1);
5862     StringRef ExtraToken = Name.slice(Start, Next);
5863 
5864     // Some NEON instructions have an optional datatype suffix that is
5865     // completely ignored. Check for that.
5866     if (isDataTypeToken(ExtraToken) &&
5867         doesIgnoreDataTypeSuffix(Mnemonic, ExtraToken))
5868       continue;
5869 
5870     // For for ARM mode generate an error if the .n qualifier is used.
5871     if (ExtraToken == ".n" && !isThumb()) {
5872       SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
5873       Parser.eatToEndOfStatement();
5874       return Error(Loc, "instruction with .n (narrow) qualifier not allowed in "
5875                    "arm mode");
5876     }
5877 
5878     // The .n qualifier is always discarded as that is what the tables
5879     // and matcher expect.  In ARM mode the .w qualifier has no effect,
5880     // so discard it to avoid errors that can be caused by the matcher.
5881     if (ExtraToken != ".n" && (isThumb() || ExtraToken != ".w")) {
5882       SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
5883       Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
5884     }
5885   }
5886 
5887   // Read the remaining operands.
5888   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5889     // Read the first operand.
5890     if (parseOperand(Operands, Mnemonic)) {
5891       Parser.eatToEndOfStatement();
5892       return true;
5893     }
5894 
5895     while (getLexer().is(AsmToken::Comma)) {
5896       Parser.Lex();  // Eat the comma.
5897 
5898       // Parse and remember the operand.
5899       if (parseOperand(Operands, Mnemonic)) {
5900         Parser.eatToEndOfStatement();
5901         return true;
5902       }
5903     }
5904   }
5905 
5906   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5907     SMLoc Loc = getLexer().getLoc();
5908     Parser.eatToEndOfStatement();
5909     return Error(Loc, "unexpected token in argument list");
5910   }
5911 
5912   Parser.Lex(); // Consume the EndOfStatement
5913 
5914   if (RequireVFPRegisterListCheck) {
5915     ARMOperand &Op = static_cast<ARMOperand &>(*Operands.back());
5916     if (AcceptSinglePrecisionOnly && !Op.isSPRRegList())
5917       return Error(Op.getStartLoc(),
5918                    "VFP/Neon single precision register expected");
5919     if (AcceptDoublePrecisionOnly && !Op.isDPRRegList())
5920       return Error(Op.getStartLoc(),
5921                    "VFP/Neon double precision register expected");
5922   }
5923 
5924   tryConvertingToTwoOperandForm(Mnemonic, CarrySetting, Operands);
5925 
5926   // Some instructions, mostly Thumb, have forms for the same mnemonic that
5927   // do and don't have a cc_out optional-def operand. With some spot-checks
5928   // of the operand list, we can figure out which variant we're trying to
5929   // parse and adjust accordingly before actually matching. We shouldn't ever
5930   // try to remove a cc_out operand that was explicitly set on the
5931   // mnemonic, of course (CarrySetting == true). Reason number #317 the
5932   // table driven matcher doesn't fit well with the ARM instruction set.
5933   if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands))
5934     Operands.erase(Operands.begin() + 1);
5935 
5936   // Some instructions have the same mnemonic, but don't always
5937   // have a predicate. Distinguish them here and delete the
5938   // predicate if needed.
5939   if (shouldOmitPredicateOperand(Mnemonic, Operands))
5940     Operands.erase(Operands.begin() + 1);
5941 
5942   // ARM mode 'blx' need special handling, as the register operand version
5943   // is predicable, but the label operand version is not. So, we can't rely
5944   // on the Mnemonic based checking to correctly figure out when to put
5945   // a k_CondCode operand in the list. If we're trying to match the label
5946   // version, remove the k_CondCode operand here.
5947   if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
5948       static_cast<ARMOperand &>(*Operands[2]).isImm())
5949     Operands.erase(Operands.begin() + 1);
5950 
5951   // Adjust operands of ldrexd/strexd to MCK_GPRPair.
5952   // ldrexd/strexd require even/odd GPR pair. To enforce this constraint,
5953   // a single GPRPair reg operand is used in the .td file to replace the two
5954   // GPRs. However, when parsing from asm, the two GRPs cannot be automatically
5955   // expressed as a GPRPair, so we have to manually merge them.
5956   // FIXME: We would really like to be able to tablegen'erate this.
5957   if (!isThumb() && Operands.size() > 4 &&
5958       (Mnemonic == "ldrexd" || Mnemonic == "strexd" || Mnemonic == "ldaexd" ||
5959        Mnemonic == "stlexd")) {
5960     bool isLoad = (Mnemonic == "ldrexd" || Mnemonic == "ldaexd");
5961     unsigned Idx = isLoad ? 2 : 3;
5962     ARMOperand &Op1 = static_cast<ARMOperand &>(*Operands[Idx]);
5963     ARMOperand &Op2 = static_cast<ARMOperand &>(*Operands[Idx + 1]);
5964 
5965     const MCRegisterClass& MRC = MRI->getRegClass(ARM::GPRRegClassID);
5966     // Adjust only if Op1 and Op2 are GPRs.
5967     if (Op1.isReg() && Op2.isReg() && MRC.contains(Op1.getReg()) &&
5968         MRC.contains(Op2.getReg())) {
5969       unsigned Reg1 = Op1.getReg();
5970       unsigned Reg2 = Op2.getReg();
5971       unsigned Rt = MRI->getEncodingValue(Reg1);
5972       unsigned Rt2 = MRI->getEncodingValue(Reg2);
5973 
5974       // Rt2 must be Rt + 1 and Rt must be even.
5975       if (Rt + 1 != Rt2 || (Rt & 1)) {
5976         Error(Op2.getStartLoc(), isLoad
5977                                      ? "destination operands must be sequential"
5978                                      : "source operands must be sequential");
5979         return true;
5980       }
5981       unsigned NewReg = MRI->getMatchingSuperReg(Reg1, ARM::gsub_0,
5982           &(MRI->getRegClass(ARM::GPRPairRegClassID)));
5983       Operands[Idx] =
5984           ARMOperand::CreateReg(NewReg, Op1.getStartLoc(), Op2.getEndLoc());
5985       Operands.erase(Operands.begin() + Idx + 1);
5986     }
5987   }
5988 
5989   // GNU Assembler extension (compatibility)
5990   if ((Mnemonic == "ldrd" || Mnemonic == "strd")) {
5991     ARMOperand &Op2 = static_cast<ARMOperand &>(*Operands[2]);
5992     ARMOperand &Op3 = static_cast<ARMOperand &>(*Operands[3]);
5993     if (Op3.isMem()) {
5994       assert(Op2.isReg() && "expected register argument");
5995 
5996       unsigned SuperReg = MRI->getMatchingSuperReg(
5997           Op2.getReg(), ARM::gsub_0, &MRI->getRegClass(ARM::GPRPairRegClassID));
5998 
5999       assert(SuperReg && "expected register pair");
6000 
6001       unsigned PairedReg = MRI->getSubReg(SuperReg, ARM::gsub_1);
6002 
6003       Operands.insert(
6004           Operands.begin() + 3,
6005           ARMOperand::CreateReg(PairedReg, Op2.getStartLoc(), Op2.getEndLoc()));
6006     }
6007   }
6008 
6009   // FIXME: As said above, this is all a pretty gross hack.  This instruction
6010   // does not fit with other "subs" and tblgen.
6011   // Adjust operands of B9.3.19 SUBS PC, LR, #imm (Thumb2) system instruction
6012   // so the Mnemonic is the original name "subs" and delete the predicate
6013   // operand so it will match the table entry.
6014   if (isThumbTwo() && Mnemonic == "sub" && Operands.size() == 6 &&
6015       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
6016       static_cast<ARMOperand &>(*Operands[3]).getReg() == ARM::PC &&
6017       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
6018       static_cast<ARMOperand &>(*Operands[4]).getReg() == ARM::LR &&
6019       static_cast<ARMOperand &>(*Operands[5]).isImm()) {
6020     Operands.front() = ARMOperand::CreateToken(Name, NameLoc);
6021     Operands.erase(Operands.begin() + 1);
6022   }
6023   return false;
6024 }
6025 
6026 // Validate context-sensitive operand constraints.
6027 
6028 // return 'true' if register list contains non-low GPR registers,
6029 // 'false' otherwise. If Reg is in the register list or is HiReg, set
6030 // 'containsReg' to true.
6031 static bool checkLowRegisterList(const MCInst &Inst, unsigned OpNo,
6032                                  unsigned Reg, unsigned HiReg,
6033                                  bool &containsReg) {
6034   containsReg = false;
6035   for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
6036     unsigned OpReg = Inst.getOperand(i).getReg();
6037     if (OpReg == Reg)
6038       containsReg = true;
6039     // Anything other than a low register isn't legal here.
6040     if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
6041       return true;
6042   }
6043   return false;
6044 }
6045 
6046 // Check if the specified regisgter is in the register list of the inst,
6047 // starting at the indicated operand number.
6048 static bool listContainsReg(const MCInst &Inst, unsigned OpNo, unsigned Reg) {
6049   for (unsigned i = OpNo, e = Inst.getNumOperands(); i < e; ++i) {
6050     unsigned OpReg = Inst.getOperand(i).getReg();
6051     if (OpReg == Reg)
6052       return true;
6053   }
6054   return false;
6055 }
6056 
6057 // Return true if instruction has the interesting property of being
6058 // allowed in IT blocks, but not being predicable.
6059 static bool instIsBreakpoint(const MCInst &Inst) {
6060     return Inst.getOpcode() == ARM::tBKPT ||
6061            Inst.getOpcode() == ARM::BKPT ||
6062            Inst.getOpcode() == ARM::tHLT ||
6063            Inst.getOpcode() == ARM::HLT;
6064 
6065 }
6066 
6067 bool ARMAsmParser::validatetLDMRegList(const MCInst &Inst,
6068                                        const OperandVector &Operands,
6069                                        unsigned ListNo, bool IsARPop) {
6070   const ARMOperand &Op = static_cast<const ARMOperand &>(*Operands[ListNo]);
6071   bool HasWritebackToken = Op.isToken() && Op.getToken() == "!";
6072 
6073   bool ListContainsSP = listContainsReg(Inst, ListNo, ARM::SP);
6074   bool ListContainsLR = listContainsReg(Inst, ListNo, ARM::LR);
6075   bool ListContainsPC = listContainsReg(Inst, ListNo, ARM::PC);
6076 
6077   if (!IsARPop && ListContainsSP)
6078     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6079                  "SP may not be in the register list");
6080   else if (ListContainsPC && ListContainsLR)
6081     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6082                  "PC and LR may not be in the register list simultaneously");
6083   else if (inITBlock() && !lastInITBlock() && ListContainsPC)
6084     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6085                  "instruction must be outside of IT block or the last "
6086                  "instruction in an IT block");
6087   return false;
6088 }
6089 
6090 bool ARMAsmParser::validatetSTMRegList(const MCInst &Inst,
6091                                        const OperandVector &Operands,
6092                                        unsigned ListNo) {
6093   const ARMOperand &Op = static_cast<const ARMOperand &>(*Operands[ListNo]);
6094   bool HasWritebackToken = Op.isToken() && Op.getToken() == "!";
6095 
6096   bool ListContainsSP = listContainsReg(Inst, ListNo, ARM::SP);
6097   bool ListContainsPC = listContainsReg(Inst, ListNo, ARM::PC);
6098 
6099   if (ListContainsSP && ListContainsPC)
6100     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6101                  "SP and PC may not be in the register list");
6102   else if (ListContainsSP)
6103     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6104                  "SP may not be in the register list");
6105   else if (ListContainsPC)
6106     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6107                  "PC may not be in the register list");
6108   return false;
6109 }
6110 
6111 // FIXME: We would really like to be able to tablegen'erate this.
6112 bool ARMAsmParser::validateInstruction(MCInst &Inst,
6113                                        const OperandVector &Operands) {
6114   const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
6115   SMLoc Loc = Operands[0]->getStartLoc();
6116 
6117   // Check the IT block state first.
6118   // NOTE: BKPT and HLT instructions have the interesting property of being
6119   // allowed in IT blocks, but not being predicable. They just always execute.
6120   if (inITBlock() && !instIsBreakpoint(Inst)) {
6121     unsigned Bit = 1;
6122     if (ITState.FirstCond)
6123       ITState.FirstCond = false;
6124     else
6125       Bit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
6126     // The instruction must be predicable.
6127     if (!MCID.isPredicable())
6128       return Error(Loc, "instructions in IT block must be predicable");
6129     unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
6130     unsigned ITCond = Bit ? ITState.Cond :
6131       ARMCC::getOppositeCondition(ITState.Cond);
6132     if (Cond != ITCond) {
6133       // Find the condition code Operand to get its SMLoc information.
6134       SMLoc CondLoc;
6135       for (unsigned I = 1; I < Operands.size(); ++I)
6136         if (static_cast<ARMOperand &>(*Operands[I]).isCondCode())
6137           CondLoc = Operands[I]->getStartLoc();
6138       return Error(CondLoc, "incorrect condition in IT block; got '" +
6139                    StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
6140                    "', but expected '" +
6141                    ARMCondCodeToString(ARMCC::CondCodes(ITCond)) + "'");
6142     }
6143   // Check for non-'al' condition codes outside of the IT block.
6144   } else if (isThumbTwo() && MCID.isPredicable() &&
6145              Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
6146              ARMCC::AL && Inst.getOpcode() != ARM::tBcc &&
6147              Inst.getOpcode() != ARM::t2Bcc)
6148     return Error(Loc, "predicated instructions must be in IT block");
6149 
6150   const unsigned Opcode = Inst.getOpcode();
6151   switch (Opcode) {
6152   case ARM::LDRD:
6153   case ARM::LDRD_PRE:
6154   case ARM::LDRD_POST: {
6155     const unsigned RtReg = Inst.getOperand(0).getReg();
6156 
6157     // Rt can't be R14.
6158     if (RtReg == ARM::LR)
6159       return Error(Operands[3]->getStartLoc(),
6160                    "Rt can't be R14");
6161 
6162     const unsigned Rt = MRI->getEncodingValue(RtReg);
6163     // Rt must be even-numbered.
6164     if ((Rt & 1) == 1)
6165       return Error(Operands[3]->getStartLoc(),
6166                    "Rt must be even-numbered");
6167 
6168     // Rt2 must be Rt + 1.
6169     const unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6170     if (Rt2 != Rt + 1)
6171       return Error(Operands[3]->getStartLoc(),
6172                    "destination operands must be sequential");
6173 
6174     if (Opcode == ARM::LDRD_PRE || Opcode == ARM::LDRD_POST) {
6175       const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(3).getReg());
6176       // For addressing modes with writeback, the base register needs to be
6177       // different from the destination registers.
6178       if (Rn == Rt || Rn == Rt2)
6179         return Error(Operands[3]->getStartLoc(),
6180                      "base register needs to be different from destination "
6181                      "registers");
6182     }
6183 
6184     return false;
6185   }
6186   case ARM::t2LDRDi8:
6187   case ARM::t2LDRD_PRE:
6188   case ARM::t2LDRD_POST: {
6189     // Rt2 must be different from Rt.
6190     unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
6191     unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6192     if (Rt2 == Rt)
6193       return Error(Operands[3]->getStartLoc(),
6194                    "destination operands can't be identical");
6195     return false;
6196   }
6197   case ARM::t2BXJ: {
6198     const unsigned RmReg = Inst.getOperand(0).getReg();
6199     // Rm = SP is no longer unpredictable in v8-A
6200     if (RmReg == ARM::SP && !hasV8Ops())
6201       return Error(Operands[2]->getStartLoc(),
6202                    "r13 (SP) is an unpredictable operand to BXJ");
6203     return false;
6204   }
6205   case ARM::STRD: {
6206     // Rt2 must be Rt + 1.
6207     unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
6208     unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6209     if (Rt2 != Rt + 1)
6210       return Error(Operands[3]->getStartLoc(),
6211                    "source operands must be sequential");
6212     return false;
6213   }
6214   case ARM::STRD_PRE:
6215   case ARM::STRD_POST: {
6216     // Rt2 must be Rt + 1.
6217     unsigned Rt = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6218     unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(2).getReg());
6219     if (Rt2 != Rt + 1)
6220       return Error(Operands[3]->getStartLoc(),
6221                    "source operands must be sequential");
6222     return false;
6223   }
6224   case ARM::STR_PRE_IMM:
6225   case ARM::STR_PRE_REG:
6226   case ARM::STR_POST_IMM:
6227   case ARM::STR_POST_REG:
6228   case ARM::STRH_PRE:
6229   case ARM::STRH_POST:
6230   case ARM::STRB_PRE_IMM:
6231   case ARM::STRB_PRE_REG:
6232   case ARM::STRB_POST_IMM:
6233   case ARM::STRB_POST_REG: {
6234     // Rt must be different from Rn.
6235     const unsigned Rt = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6236     const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(2).getReg());
6237 
6238     if (Rt == Rn)
6239       return Error(Operands[3]->getStartLoc(),
6240                    "source register and base register can't be identical");
6241     return false;
6242   }
6243   case ARM::LDR_PRE_IMM:
6244   case ARM::LDR_PRE_REG:
6245   case ARM::LDR_POST_IMM:
6246   case ARM::LDR_POST_REG:
6247   case ARM::LDRH_PRE:
6248   case ARM::LDRH_POST:
6249   case ARM::LDRSH_PRE:
6250   case ARM::LDRSH_POST:
6251   case ARM::LDRB_PRE_IMM:
6252   case ARM::LDRB_PRE_REG:
6253   case ARM::LDRB_POST_IMM:
6254   case ARM::LDRB_POST_REG:
6255   case ARM::LDRSB_PRE:
6256   case ARM::LDRSB_POST: {
6257     // Rt must be different from Rn.
6258     const unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
6259     const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(2).getReg());
6260 
6261     if (Rt == Rn)
6262       return Error(Operands[3]->getStartLoc(),
6263                    "destination register and base register can't be identical");
6264     return false;
6265   }
6266   case ARM::SBFX:
6267   case ARM::UBFX: {
6268     // Width must be in range [1, 32-lsb].
6269     unsigned LSB = Inst.getOperand(2).getImm();
6270     unsigned Widthm1 = Inst.getOperand(3).getImm();
6271     if (Widthm1 >= 32 - LSB)
6272       return Error(Operands[5]->getStartLoc(),
6273                    "bitfield width must be in range [1,32-lsb]");
6274     return false;
6275   }
6276   // Notionally handles ARM::tLDMIA_UPD too.
6277   case ARM::tLDMIA: {
6278     // If we're parsing Thumb2, the .w variant is available and handles
6279     // most cases that are normally illegal for a Thumb1 LDM instruction.
6280     // We'll make the transformation in processInstruction() if necessary.
6281     //
6282     // Thumb LDM instructions are writeback iff the base register is not
6283     // in the register list.
6284     unsigned Rn = Inst.getOperand(0).getReg();
6285     bool HasWritebackToken =
6286         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
6287          static_cast<ARMOperand &>(*Operands[3]).getToken() == "!");
6288     bool ListContainsBase;
6289     if (checkLowRegisterList(Inst, 3, Rn, 0, ListContainsBase) && !isThumbTwo())
6290       return Error(Operands[3 + HasWritebackToken]->getStartLoc(),
6291                    "registers must be in range r0-r7");
6292     // If we should have writeback, then there should be a '!' token.
6293     if (!ListContainsBase && !HasWritebackToken && !isThumbTwo())
6294       return Error(Operands[2]->getStartLoc(),
6295                    "writeback operator '!' expected");
6296     // If we should not have writeback, there must not be a '!'. This is
6297     // true even for the 32-bit wide encodings.
6298     if (ListContainsBase && HasWritebackToken)
6299       return Error(Operands[3]->getStartLoc(),
6300                    "writeback operator '!' not allowed when base register "
6301                    "in register list");
6302 
6303     if (validatetLDMRegList(Inst, Operands, 3))
6304       return true;
6305     break;
6306   }
6307   case ARM::LDMIA_UPD:
6308   case ARM::LDMDB_UPD:
6309   case ARM::LDMIB_UPD:
6310   case ARM::LDMDA_UPD:
6311     // ARM variants loading and updating the same register are only officially
6312     // UNPREDICTABLE on v7 upwards. Goodness knows what they did before.
6313     if (!hasV7Ops())
6314       break;
6315     if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
6316       return Error(Operands.back()->getStartLoc(),
6317                    "writeback register not allowed in register list");
6318     break;
6319   case ARM::t2LDMIA:
6320   case ARM::t2LDMDB:
6321     if (validatetLDMRegList(Inst, Operands, 3))
6322       return true;
6323     break;
6324   case ARM::t2STMIA:
6325   case ARM::t2STMDB:
6326     if (validatetSTMRegList(Inst, Operands, 3))
6327       return true;
6328     break;
6329   case ARM::t2LDMIA_UPD:
6330   case ARM::t2LDMDB_UPD:
6331   case ARM::t2STMIA_UPD:
6332   case ARM::t2STMDB_UPD: {
6333     if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
6334       return Error(Operands.back()->getStartLoc(),
6335                    "writeback register not allowed in register list");
6336 
6337     if (Opcode == ARM::t2LDMIA_UPD || Opcode == ARM::t2LDMDB_UPD) {
6338       if (validatetLDMRegList(Inst, Operands, 3))
6339         return true;
6340     } else {
6341       if (validatetSTMRegList(Inst, Operands, 3))
6342         return true;
6343     }
6344     break;
6345   }
6346   case ARM::sysLDMIA_UPD:
6347   case ARM::sysLDMDA_UPD:
6348   case ARM::sysLDMDB_UPD:
6349   case ARM::sysLDMIB_UPD:
6350     if (!listContainsReg(Inst, 3, ARM::PC))
6351       return Error(Operands[4]->getStartLoc(),
6352                    "writeback register only allowed on system LDM "
6353                    "if PC in register-list");
6354     break;
6355   case ARM::sysSTMIA_UPD:
6356   case ARM::sysSTMDA_UPD:
6357   case ARM::sysSTMDB_UPD:
6358   case ARM::sysSTMIB_UPD:
6359     return Error(Operands[2]->getStartLoc(),
6360                  "system STM cannot have writeback register");
6361   case ARM::tMUL: {
6362     // The second source operand must be the same register as the destination
6363     // operand.
6364     //
6365     // In this case, we must directly check the parsed operands because the
6366     // cvtThumbMultiply() function is written in such a way that it guarantees
6367     // this first statement is always true for the new Inst.  Essentially, the
6368     // destination is unconditionally copied into the second source operand
6369     // without checking to see if it matches what we actually parsed.
6370     if (Operands.size() == 6 && (((ARMOperand &)*Operands[3]).getReg() !=
6371                                  ((ARMOperand &)*Operands[5]).getReg()) &&
6372         (((ARMOperand &)*Operands[3]).getReg() !=
6373          ((ARMOperand &)*Operands[4]).getReg())) {
6374       return Error(Operands[3]->getStartLoc(),
6375                    "destination register must match source register");
6376     }
6377     break;
6378   }
6379   // Like for ldm/stm, push and pop have hi-reg handling version in Thumb2,
6380   // so only issue a diagnostic for thumb1. The instructions will be
6381   // switched to the t2 encodings in processInstruction() if necessary.
6382   case ARM::tPOP: {
6383     bool ListContainsBase;
6384     if (checkLowRegisterList(Inst, 2, 0, ARM::PC, ListContainsBase) &&
6385         !isThumbTwo())
6386       return Error(Operands[2]->getStartLoc(),
6387                    "registers must be in range r0-r7 or pc");
6388     if (validatetLDMRegList(Inst, Operands, 2, !isMClass()))
6389       return true;
6390     break;
6391   }
6392   case ARM::tPUSH: {
6393     bool ListContainsBase;
6394     if (checkLowRegisterList(Inst, 2, 0, ARM::LR, ListContainsBase) &&
6395         !isThumbTwo())
6396       return Error(Operands[2]->getStartLoc(),
6397                    "registers must be in range r0-r7 or lr");
6398     if (validatetSTMRegList(Inst, Operands, 2))
6399       return true;
6400     break;
6401   }
6402   case ARM::tSTMIA_UPD: {
6403     bool ListContainsBase, InvalidLowList;
6404     InvalidLowList = checkLowRegisterList(Inst, 4, Inst.getOperand(0).getReg(),
6405                                           0, ListContainsBase);
6406     if (InvalidLowList && !isThumbTwo())
6407       return Error(Operands[4]->getStartLoc(),
6408                    "registers must be in range r0-r7");
6409 
6410     // This would be converted to a 32-bit stm, but that's not valid if the
6411     // writeback register is in the list.
6412     if (InvalidLowList && ListContainsBase)
6413       return Error(Operands[4]->getStartLoc(),
6414                    "writeback operator '!' not allowed when base register "
6415                    "in register list");
6416 
6417     if (validatetSTMRegList(Inst, Operands, 4))
6418       return true;
6419     break;
6420   }
6421   case ARM::tADDrSP: {
6422     // If the non-SP source operand and the destination operand are not the
6423     // same, we need thumb2 (for the wide encoding), or we have an error.
6424     if (!isThumbTwo() &&
6425         Inst.getOperand(0).getReg() != Inst.getOperand(2).getReg()) {
6426       return Error(Operands[4]->getStartLoc(),
6427                    "source register must be the same as destination");
6428     }
6429     break;
6430   }
6431   // Final range checking for Thumb unconditional branch instructions.
6432   case ARM::tB:
6433     if (!(static_cast<ARMOperand &>(*Operands[2])).isSignedOffset<11, 1>())
6434       return Error(Operands[2]->getStartLoc(), "branch target out of range");
6435     break;
6436   case ARM::t2B: {
6437     int op = (Operands[2]->isImm()) ? 2 : 3;
6438     if (!static_cast<ARMOperand &>(*Operands[op]).isSignedOffset<24, 1>())
6439       return Error(Operands[op]->getStartLoc(), "branch target out of range");
6440     break;
6441   }
6442   // Final range checking for Thumb conditional branch instructions.
6443   case ARM::tBcc:
6444     if (!static_cast<ARMOperand &>(*Operands[2]).isSignedOffset<8, 1>())
6445       return Error(Operands[2]->getStartLoc(), "branch target out of range");
6446     break;
6447   case ARM::t2Bcc: {
6448     int Op = (Operands[2]->isImm()) ? 2 : 3;
6449     if (!static_cast<ARMOperand &>(*Operands[Op]).isSignedOffset<20, 1>())
6450       return Error(Operands[Op]->getStartLoc(), "branch target out of range");
6451     break;
6452   }
6453   case ARM::MOVi16:
6454   case ARM::t2MOVi16:
6455   case ARM::t2MOVTi16:
6456     {
6457     // We want to avoid misleadingly allowing something like "mov r0, <symbol>"
6458     // especially when we turn it into a movw and the expression <symbol> does
6459     // not have a :lower16: or :upper16 as part of the expression.  We don't
6460     // want the behavior of silently truncating, which can be unexpected and
6461     // lead to bugs that are difficult to find since this is an easy mistake
6462     // to make.
6463     int i = (Operands[3]->isImm()) ? 3 : 4;
6464     ARMOperand &Op = static_cast<ARMOperand &>(*Operands[i]);
6465     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm());
6466     if (CE) break;
6467     const MCExpr *E = dyn_cast<MCExpr>(Op.getImm());
6468     if (!E) break;
6469     const ARMMCExpr *ARM16Expr = dyn_cast<ARMMCExpr>(E);
6470     if (!ARM16Expr || (ARM16Expr->getKind() != ARMMCExpr::VK_ARM_HI16 &&
6471                        ARM16Expr->getKind() != ARMMCExpr::VK_ARM_LO16))
6472       return Error(
6473           Op.getStartLoc(),
6474           "immediate expression for mov requires :lower16: or :upper16");
6475     break;
6476   }
6477   }
6478 
6479   return false;
6480 }
6481 
6482 static unsigned getRealVSTOpcode(unsigned Opc, unsigned &Spacing) {
6483   switch(Opc) {
6484   default: llvm_unreachable("unexpected opcode!");
6485   // VST1LN
6486   case ARM::VST1LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST1LNd8_UPD;
6487   case ARM::VST1LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST1LNd16_UPD;
6488   case ARM::VST1LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST1LNd32_UPD;
6489   case ARM::VST1LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST1LNd8_UPD;
6490   case ARM::VST1LNdWB_register_Asm_16: Spacing = 1; return ARM::VST1LNd16_UPD;
6491   case ARM::VST1LNdWB_register_Asm_32: Spacing = 1; return ARM::VST1LNd32_UPD;
6492   case ARM::VST1LNdAsm_8:  Spacing = 1; return ARM::VST1LNd8;
6493   case ARM::VST1LNdAsm_16: Spacing = 1; return ARM::VST1LNd16;
6494   case ARM::VST1LNdAsm_32: Spacing = 1; return ARM::VST1LNd32;
6495 
6496   // VST2LN
6497   case ARM::VST2LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST2LNd8_UPD;
6498   case ARM::VST2LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST2LNd16_UPD;
6499   case ARM::VST2LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST2LNd32_UPD;
6500   case ARM::VST2LNqWB_fixed_Asm_16: Spacing = 2; return ARM::VST2LNq16_UPD;
6501   case ARM::VST2LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST2LNq32_UPD;
6502 
6503   case ARM::VST2LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST2LNd8_UPD;
6504   case ARM::VST2LNdWB_register_Asm_16: Spacing = 1; return ARM::VST2LNd16_UPD;
6505   case ARM::VST2LNdWB_register_Asm_32: Spacing = 1; return ARM::VST2LNd32_UPD;
6506   case ARM::VST2LNqWB_register_Asm_16: Spacing = 2; return ARM::VST2LNq16_UPD;
6507   case ARM::VST2LNqWB_register_Asm_32: Spacing = 2; return ARM::VST2LNq32_UPD;
6508 
6509   case ARM::VST2LNdAsm_8:  Spacing = 1; return ARM::VST2LNd8;
6510   case ARM::VST2LNdAsm_16: Spacing = 1; return ARM::VST2LNd16;
6511   case ARM::VST2LNdAsm_32: Spacing = 1; return ARM::VST2LNd32;
6512   case ARM::VST2LNqAsm_16: Spacing = 2; return ARM::VST2LNq16;
6513   case ARM::VST2LNqAsm_32: Spacing = 2; return ARM::VST2LNq32;
6514 
6515   // VST3LN
6516   case ARM::VST3LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST3LNd8_UPD;
6517   case ARM::VST3LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST3LNd16_UPD;
6518   case ARM::VST3LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST3LNd32_UPD;
6519   case ARM::VST3LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VST3LNq16_UPD;
6520   case ARM::VST3LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST3LNq32_UPD;
6521   case ARM::VST3LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST3LNd8_UPD;
6522   case ARM::VST3LNdWB_register_Asm_16: Spacing = 1; return ARM::VST3LNd16_UPD;
6523   case ARM::VST3LNdWB_register_Asm_32: Spacing = 1; return ARM::VST3LNd32_UPD;
6524   case ARM::VST3LNqWB_register_Asm_16: Spacing = 2; return ARM::VST3LNq16_UPD;
6525   case ARM::VST3LNqWB_register_Asm_32: Spacing = 2; return ARM::VST3LNq32_UPD;
6526   case ARM::VST3LNdAsm_8:  Spacing = 1; return ARM::VST3LNd8;
6527   case ARM::VST3LNdAsm_16: Spacing = 1; return ARM::VST3LNd16;
6528   case ARM::VST3LNdAsm_32: Spacing = 1; return ARM::VST3LNd32;
6529   case ARM::VST3LNqAsm_16: Spacing = 2; return ARM::VST3LNq16;
6530   case ARM::VST3LNqAsm_32: Spacing = 2; return ARM::VST3LNq32;
6531 
6532   // VST3
6533   case ARM::VST3dWB_fixed_Asm_8:  Spacing = 1; return ARM::VST3d8_UPD;
6534   case ARM::VST3dWB_fixed_Asm_16: Spacing = 1; return ARM::VST3d16_UPD;
6535   case ARM::VST3dWB_fixed_Asm_32: Spacing = 1; return ARM::VST3d32_UPD;
6536   case ARM::VST3qWB_fixed_Asm_8:  Spacing = 2; return ARM::VST3q8_UPD;
6537   case ARM::VST3qWB_fixed_Asm_16: Spacing = 2; return ARM::VST3q16_UPD;
6538   case ARM::VST3qWB_fixed_Asm_32: Spacing = 2; return ARM::VST3q32_UPD;
6539   case ARM::VST3dWB_register_Asm_8:  Spacing = 1; return ARM::VST3d8_UPD;
6540   case ARM::VST3dWB_register_Asm_16: Spacing = 1; return ARM::VST3d16_UPD;
6541   case ARM::VST3dWB_register_Asm_32: Spacing = 1; return ARM::VST3d32_UPD;
6542   case ARM::VST3qWB_register_Asm_8:  Spacing = 2; return ARM::VST3q8_UPD;
6543   case ARM::VST3qWB_register_Asm_16: Spacing = 2; return ARM::VST3q16_UPD;
6544   case ARM::VST3qWB_register_Asm_32: Spacing = 2; return ARM::VST3q32_UPD;
6545   case ARM::VST3dAsm_8:  Spacing = 1; return ARM::VST3d8;
6546   case ARM::VST3dAsm_16: Spacing = 1; return ARM::VST3d16;
6547   case ARM::VST3dAsm_32: Spacing = 1; return ARM::VST3d32;
6548   case ARM::VST3qAsm_8:  Spacing = 2; return ARM::VST3q8;
6549   case ARM::VST3qAsm_16: Spacing = 2; return ARM::VST3q16;
6550   case ARM::VST3qAsm_32: Spacing = 2; return ARM::VST3q32;
6551 
6552   // VST4LN
6553   case ARM::VST4LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST4LNd8_UPD;
6554   case ARM::VST4LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST4LNd16_UPD;
6555   case ARM::VST4LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST4LNd32_UPD;
6556   case ARM::VST4LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VST4LNq16_UPD;
6557   case ARM::VST4LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST4LNq32_UPD;
6558   case ARM::VST4LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST4LNd8_UPD;
6559   case ARM::VST4LNdWB_register_Asm_16: Spacing = 1; return ARM::VST4LNd16_UPD;
6560   case ARM::VST4LNdWB_register_Asm_32: Spacing = 1; return ARM::VST4LNd32_UPD;
6561   case ARM::VST4LNqWB_register_Asm_16: Spacing = 2; return ARM::VST4LNq16_UPD;
6562   case ARM::VST4LNqWB_register_Asm_32: Spacing = 2; return ARM::VST4LNq32_UPD;
6563   case ARM::VST4LNdAsm_8:  Spacing = 1; return ARM::VST4LNd8;
6564   case ARM::VST4LNdAsm_16: Spacing = 1; return ARM::VST4LNd16;
6565   case ARM::VST4LNdAsm_32: Spacing = 1; return ARM::VST4LNd32;
6566   case ARM::VST4LNqAsm_16: Spacing = 2; return ARM::VST4LNq16;
6567   case ARM::VST4LNqAsm_32: Spacing = 2; return ARM::VST4LNq32;
6568 
6569   // VST4
6570   case ARM::VST4dWB_fixed_Asm_8:  Spacing = 1; return ARM::VST4d8_UPD;
6571   case ARM::VST4dWB_fixed_Asm_16: Spacing = 1; return ARM::VST4d16_UPD;
6572   case ARM::VST4dWB_fixed_Asm_32: Spacing = 1; return ARM::VST4d32_UPD;
6573   case ARM::VST4qWB_fixed_Asm_8:  Spacing = 2; return ARM::VST4q8_UPD;
6574   case ARM::VST4qWB_fixed_Asm_16: Spacing = 2; return ARM::VST4q16_UPD;
6575   case ARM::VST4qWB_fixed_Asm_32: Spacing = 2; return ARM::VST4q32_UPD;
6576   case ARM::VST4dWB_register_Asm_8:  Spacing = 1; return ARM::VST4d8_UPD;
6577   case ARM::VST4dWB_register_Asm_16: Spacing = 1; return ARM::VST4d16_UPD;
6578   case ARM::VST4dWB_register_Asm_32: Spacing = 1; return ARM::VST4d32_UPD;
6579   case ARM::VST4qWB_register_Asm_8:  Spacing = 2; return ARM::VST4q8_UPD;
6580   case ARM::VST4qWB_register_Asm_16: Spacing = 2; return ARM::VST4q16_UPD;
6581   case ARM::VST4qWB_register_Asm_32: Spacing = 2; return ARM::VST4q32_UPD;
6582   case ARM::VST4dAsm_8:  Spacing = 1; return ARM::VST4d8;
6583   case ARM::VST4dAsm_16: Spacing = 1; return ARM::VST4d16;
6584   case ARM::VST4dAsm_32: Spacing = 1; return ARM::VST4d32;
6585   case ARM::VST4qAsm_8:  Spacing = 2; return ARM::VST4q8;
6586   case ARM::VST4qAsm_16: Spacing = 2; return ARM::VST4q16;
6587   case ARM::VST4qAsm_32: Spacing = 2; return ARM::VST4q32;
6588   }
6589 }
6590 
6591 static unsigned getRealVLDOpcode(unsigned Opc, unsigned &Spacing) {
6592   switch(Opc) {
6593   default: llvm_unreachable("unexpected opcode!");
6594   // VLD1LN
6595   case ARM::VLD1LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD1LNd8_UPD;
6596   case ARM::VLD1LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD1LNd16_UPD;
6597   case ARM::VLD1LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD1LNd32_UPD;
6598   case ARM::VLD1LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD1LNd8_UPD;
6599   case ARM::VLD1LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD1LNd16_UPD;
6600   case ARM::VLD1LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD1LNd32_UPD;
6601   case ARM::VLD1LNdAsm_8:  Spacing = 1; return ARM::VLD1LNd8;
6602   case ARM::VLD1LNdAsm_16: Spacing = 1; return ARM::VLD1LNd16;
6603   case ARM::VLD1LNdAsm_32: Spacing = 1; return ARM::VLD1LNd32;
6604 
6605   // VLD2LN
6606   case ARM::VLD2LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD2LNd8_UPD;
6607   case ARM::VLD2LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD2LNd16_UPD;
6608   case ARM::VLD2LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD2LNd32_UPD;
6609   case ARM::VLD2LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD2LNq16_UPD;
6610   case ARM::VLD2LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD2LNq32_UPD;
6611   case ARM::VLD2LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD2LNd8_UPD;
6612   case ARM::VLD2LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD2LNd16_UPD;
6613   case ARM::VLD2LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD2LNd32_UPD;
6614   case ARM::VLD2LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD2LNq16_UPD;
6615   case ARM::VLD2LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD2LNq32_UPD;
6616   case ARM::VLD2LNdAsm_8:  Spacing = 1; return ARM::VLD2LNd8;
6617   case ARM::VLD2LNdAsm_16: Spacing = 1; return ARM::VLD2LNd16;
6618   case ARM::VLD2LNdAsm_32: Spacing = 1; return ARM::VLD2LNd32;
6619   case ARM::VLD2LNqAsm_16: Spacing = 2; return ARM::VLD2LNq16;
6620   case ARM::VLD2LNqAsm_32: Spacing = 2; return ARM::VLD2LNq32;
6621 
6622   // VLD3DUP
6623   case ARM::VLD3DUPdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3DUPd8_UPD;
6624   case ARM::VLD3DUPdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3DUPd16_UPD;
6625   case ARM::VLD3DUPdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3DUPd32_UPD;
6626   case ARM::VLD3DUPqWB_fixed_Asm_8: Spacing = 1; return ARM::VLD3DUPq8_UPD;
6627   case ARM::VLD3DUPqWB_fixed_Asm_16: Spacing = 2; return ARM::VLD3DUPq16_UPD;
6628   case ARM::VLD3DUPqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3DUPq32_UPD;
6629   case ARM::VLD3DUPdWB_register_Asm_8:  Spacing = 1; return ARM::VLD3DUPd8_UPD;
6630   case ARM::VLD3DUPdWB_register_Asm_16: Spacing = 1; return ARM::VLD3DUPd16_UPD;
6631   case ARM::VLD3DUPdWB_register_Asm_32: Spacing = 1; return ARM::VLD3DUPd32_UPD;
6632   case ARM::VLD3DUPqWB_register_Asm_8: Spacing = 2; return ARM::VLD3DUPq8_UPD;
6633   case ARM::VLD3DUPqWB_register_Asm_16: Spacing = 2; return ARM::VLD3DUPq16_UPD;
6634   case ARM::VLD3DUPqWB_register_Asm_32: Spacing = 2; return ARM::VLD3DUPq32_UPD;
6635   case ARM::VLD3DUPdAsm_8:  Spacing = 1; return ARM::VLD3DUPd8;
6636   case ARM::VLD3DUPdAsm_16: Spacing = 1; return ARM::VLD3DUPd16;
6637   case ARM::VLD3DUPdAsm_32: Spacing = 1; return ARM::VLD3DUPd32;
6638   case ARM::VLD3DUPqAsm_8: Spacing = 2; return ARM::VLD3DUPq8;
6639   case ARM::VLD3DUPqAsm_16: Spacing = 2; return ARM::VLD3DUPq16;
6640   case ARM::VLD3DUPqAsm_32: Spacing = 2; return ARM::VLD3DUPq32;
6641 
6642   // VLD3LN
6643   case ARM::VLD3LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3LNd8_UPD;
6644   case ARM::VLD3LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3LNd16_UPD;
6645   case ARM::VLD3LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3LNd32_UPD;
6646   case ARM::VLD3LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3LNq16_UPD;
6647   case ARM::VLD3LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3LNq32_UPD;
6648   case ARM::VLD3LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD3LNd8_UPD;
6649   case ARM::VLD3LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD3LNd16_UPD;
6650   case ARM::VLD3LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD3LNd32_UPD;
6651   case ARM::VLD3LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD3LNq16_UPD;
6652   case ARM::VLD3LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD3LNq32_UPD;
6653   case ARM::VLD3LNdAsm_8:  Spacing = 1; return ARM::VLD3LNd8;
6654   case ARM::VLD3LNdAsm_16: Spacing = 1; return ARM::VLD3LNd16;
6655   case ARM::VLD3LNdAsm_32: Spacing = 1; return ARM::VLD3LNd32;
6656   case ARM::VLD3LNqAsm_16: Spacing = 2; return ARM::VLD3LNq16;
6657   case ARM::VLD3LNqAsm_32: Spacing = 2; return ARM::VLD3LNq32;
6658 
6659   // VLD3
6660   case ARM::VLD3dWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3d8_UPD;
6661   case ARM::VLD3dWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3d16_UPD;
6662   case ARM::VLD3dWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3d32_UPD;
6663   case ARM::VLD3qWB_fixed_Asm_8:  Spacing = 2; return ARM::VLD3q8_UPD;
6664   case ARM::VLD3qWB_fixed_Asm_16: Spacing = 2; return ARM::VLD3q16_UPD;
6665   case ARM::VLD3qWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3q32_UPD;
6666   case ARM::VLD3dWB_register_Asm_8:  Spacing = 1; return ARM::VLD3d8_UPD;
6667   case ARM::VLD3dWB_register_Asm_16: Spacing = 1; return ARM::VLD3d16_UPD;
6668   case ARM::VLD3dWB_register_Asm_32: Spacing = 1; return ARM::VLD3d32_UPD;
6669   case ARM::VLD3qWB_register_Asm_8:  Spacing = 2; return ARM::VLD3q8_UPD;
6670   case ARM::VLD3qWB_register_Asm_16: Spacing = 2; return ARM::VLD3q16_UPD;
6671   case ARM::VLD3qWB_register_Asm_32: Spacing = 2; return ARM::VLD3q32_UPD;
6672   case ARM::VLD3dAsm_8:  Spacing = 1; return ARM::VLD3d8;
6673   case ARM::VLD3dAsm_16: Spacing = 1; return ARM::VLD3d16;
6674   case ARM::VLD3dAsm_32: Spacing = 1; return ARM::VLD3d32;
6675   case ARM::VLD3qAsm_8:  Spacing = 2; return ARM::VLD3q8;
6676   case ARM::VLD3qAsm_16: Spacing = 2; return ARM::VLD3q16;
6677   case ARM::VLD3qAsm_32: Spacing = 2; return ARM::VLD3q32;
6678 
6679   // VLD4LN
6680   case ARM::VLD4LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4LNd8_UPD;
6681   case ARM::VLD4LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4LNd16_UPD;
6682   case ARM::VLD4LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4LNd32_UPD;
6683   case ARM::VLD4LNqWB_fixed_Asm_16: Spacing = 2; return ARM::VLD4LNq16_UPD;
6684   case ARM::VLD4LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4LNq32_UPD;
6685   case ARM::VLD4LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD4LNd8_UPD;
6686   case ARM::VLD4LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD4LNd16_UPD;
6687   case ARM::VLD4LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD4LNd32_UPD;
6688   case ARM::VLD4LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD4LNq16_UPD;
6689   case ARM::VLD4LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD4LNq32_UPD;
6690   case ARM::VLD4LNdAsm_8:  Spacing = 1; return ARM::VLD4LNd8;
6691   case ARM::VLD4LNdAsm_16: Spacing = 1; return ARM::VLD4LNd16;
6692   case ARM::VLD4LNdAsm_32: Spacing = 1; return ARM::VLD4LNd32;
6693   case ARM::VLD4LNqAsm_16: Spacing = 2; return ARM::VLD4LNq16;
6694   case ARM::VLD4LNqAsm_32: Spacing = 2; return ARM::VLD4LNq32;
6695 
6696   // VLD4DUP
6697   case ARM::VLD4DUPdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4DUPd8_UPD;
6698   case ARM::VLD4DUPdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4DUPd16_UPD;
6699   case ARM::VLD4DUPdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4DUPd32_UPD;
6700   case ARM::VLD4DUPqWB_fixed_Asm_8: Spacing = 1; return ARM::VLD4DUPq8_UPD;
6701   case ARM::VLD4DUPqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4DUPq16_UPD;
6702   case ARM::VLD4DUPqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4DUPq32_UPD;
6703   case ARM::VLD4DUPdWB_register_Asm_8:  Spacing = 1; return ARM::VLD4DUPd8_UPD;
6704   case ARM::VLD4DUPdWB_register_Asm_16: Spacing = 1; return ARM::VLD4DUPd16_UPD;
6705   case ARM::VLD4DUPdWB_register_Asm_32: Spacing = 1; return ARM::VLD4DUPd32_UPD;
6706   case ARM::VLD4DUPqWB_register_Asm_8: Spacing = 2; return ARM::VLD4DUPq8_UPD;
6707   case ARM::VLD4DUPqWB_register_Asm_16: Spacing = 2; return ARM::VLD4DUPq16_UPD;
6708   case ARM::VLD4DUPqWB_register_Asm_32: Spacing = 2; return ARM::VLD4DUPq32_UPD;
6709   case ARM::VLD4DUPdAsm_8:  Spacing = 1; return ARM::VLD4DUPd8;
6710   case ARM::VLD4DUPdAsm_16: Spacing = 1; return ARM::VLD4DUPd16;
6711   case ARM::VLD4DUPdAsm_32: Spacing = 1; return ARM::VLD4DUPd32;
6712   case ARM::VLD4DUPqAsm_8: Spacing = 2; return ARM::VLD4DUPq8;
6713   case ARM::VLD4DUPqAsm_16: Spacing = 2; return ARM::VLD4DUPq16;
6714   case ARM::VLD4DUPqAsm_32: Spacing = 2; return ARM::VLD4DUPq32;
6715 
6716   // VLD4
6717   case ARM::VLD4dWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4d8_UPD;
6718   case ARM::VLD4dWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4d16_UPD;
6719   case ARM::VLD4dWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4d32_UPD;
6720   case ARM::VLD4qWB_fixed_Asm_8:  Spacing = 2; return ARM::VLD4q8_UPD;
6721   case ARM::VLD4qWB_fixed_Asm_16: Spacing = 2; return ARM::VLD4q16_UPD;
6722   case ARM::VLD4qWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4q32_UPD;
6723   case ARM::VLD4dWB_register_Asm_8:  Spacing = 1; return ARM::VLD4d8_UPD;
6724   case ARM::VLD4dWB_register_Asm_16: Spacing = 1; return ARM::VLD4d16_UPD;
6725   case ARM::VLD4dWB_register_Asm_32: Spacing = 1; return ARM::VLD4d32_UPD;
6726   case ARM::VLD4qWB_register_Asm_8:  Spacing = 2; return ARM::VLD4q8_UPD;
6727   case ARM::VLD4qWB_register_Asm_16: Spacing = 2; return ARM::VLD4q16_UPD;
6728   case ARM::VLD4qWB_register_Asm_32: Spacing = 2; return ARM::VLD4q32_UPD;
6729   case ARM::VLD4dAsm_8:  Spacing = 1; return ARM::VLD4d8;
6730   case ARM::VLD4dAsm_16: Spacing = 1; return ARM::VLD4d16;
6731   case ARM::VLD4dAsm_32: Spacing = 1; return ARM::VLD4d32;
6732   case ARM::VLD4qAsm_8:  Spacing = 2; return ARM::VLD4q8;
6733   case ARM::VLD4qAsm_16: Spacing = 2; return ARM::VLD4q16;
6734   case ARM::VLD4qAsm_32: Spacing = 2; return ARM::VLD4q32;
6735   }
6736 }
6737 
6738 bool ARMAsmParser::processInstruction(MCInst &Inst,
6739                                       const OperandVector &Operands,
6740                                       MCStreamer &Out) {
6741   switch (Inst.getOpcode()) {
6742   // Alias for alternate form of 'ldr{,b}t Rt, [Rn], #imm' instruction.
6743   case ARM::LDRT_POST:
6744   case ARM::LDRBT_POST: {
6745     const unsigned Opcode =
6746       (Inst.getOpcode() == ARM::LDRT_POST) ? ARM::LDRT_POST_IMM
6747                                            : ARM::LDRBT_POST_IMM;
6748     MCInst TmpInst;
6749     TmpInst.setOpcode(Opcode);
6750     TmpInst.addOperand(Inst.getOperand(0));
6751     TmpInst.addOperand(Inst.getOperand(1));
6752     TmpInst.addOperand(Inst.getOperand(1));
6753     TmpInst.addOperand(MCOperand::createReg(0));
6754     TmpInst.addOperand(MCOperand::createImm(0));
6755     TmpInst.addOperand(Inst.getOperand(2));
6756     TmpInst.addOperand(Inst.getOperand(3));
6757     Inst = TmpInst;
6758     return true;
6759   }
6760   // Alias for alternate form of 'str{,b}t Rt, [Rn], #imm' instruction.
6761   case ARM::STRT_POST:
6762   case ARM::STRBT_POST: {
6763     const unsigned Opcode =
6764       (Inst.getOpcode() == ARM::STRT_POST) ? ARM::STRT_POST_IMM
6765                                            : ARM::STRBT_POST_IMM;
6766     MCInst TmpInst;
6767     TmpInst.setOpcode(Opcode);
6768     TmpInst.addOperand(Inst.getOperand(1));
6769     TmpInst.addOperand(Inst.getOperand(0));
6770     TmpInst.addOperand(Inst.getOperand(1));
6771     TmpInst.addOperand(MCOperand::createReg(0));
6772     TmpInst.addOperand(MCOperand::createImm(0));
6773     TmpInst.addOperand(Inst.getOperand(2));
6774     TmpInst.addOperand(Inst.getOperand(3));
6775     Inst = TmpInst;
6776     return true;
6777   }
6778   // Alias for alternate form of 'ADR Rd, #imm' instruction.
6779   case ARM::ADDri: {
6780     if (Inst.getOperand(1).getReg() != ARM::PC ||
6781         Inst.getOperand(5).getReg() != 0 ||
6782         !(Inst.getOperand(2).isExpr() || Inst.getOperand(2).isImm()))
6783       return false;
6784     MCInst TmpInst;
6785     TmpInst.setOpcode(ARM::ADR);
6786     TmpInst.addOperand(Inst.getOperand(0));
6787     if (Inst.getOperand(2).isImm()) {
6788       // Immediate (mod_imm) will be in its encoded form, we must unencode it
6789       // before passing it to the ADR instruction.
6790       unsigned Enc = Inst.getOperand(2).getImm();
6791       TmpInst.addOperand(MCOperand::createImm(
6792         ARM_AM::rotr32(Enc & 0xFF, (Enc & 0xF00) >> 7)));
6793     } else {
6794       // Turn PC-relative expression into absolute expression.
6795       // Reading PC provides the start of the current instruction + 8 and
6796       // the transform to adr is biased by that.
6797       MCSymbol *Dot = getContext().createTempSymbol();
6798       Out.EmitLabel(Dot);
6799       const MCExpr *OpExpr = Inst.getOperand(2).getExpr();
6800       const MCExpr *InstPC = MCSymbolRefExpr::create(Dot,
6801                                                      MCSymbolRefExpr::VK_None,
6802                                                      getContext());
6803       const MCExpr *Const8 = MCConstantExpr::create(8, getContext());
6804       const MCExpr *ReadPC = MCBinaryExpr::createAdd(InstPC, Const8,
6805                                                      getContext());
6806       const MCExpr *FixupAddr = MCBinaryExpr::createAdd(ReadPC, OpExpr,
6807                                                         getContext());
6808       TmpInst.addOperand(MCOperand::createExpr(FixupAddr));
6809     }
6810     TmpInst.addOperand(Inst.getOperand(3));
6811     TmpInst.addOperand(Inst.getOperand(4));
6812     Inst = TmpInst;
6813     return true;
6814   }
6815   // Aliases for alternate PC+imm syntax of LDR instructions.
6816   case ARM::t2LDRpcrel:
6817     // Select the narrow version if the immediate will fit.
6818     if (Inst.getOperand(1).getImm() > 0 &&
6819         Inst.getOperand(1).getImm() <= 0xff &&
6820         !(static_cast<ARMOperand &>(*Operands[2]).isToken() &&
6821           static_cast<ARMOperand &>(*Operands[2]).getToken() == ".w"))
6822       Inst.setOpcode(ARM::tLDRpci);
6823     else
6824       Inst.setOpcode(ARM::t2LDRpci);
6825     return true;
6826   case ARM::t2LDRBpcrel:
6827     Inst.setOpcode(ARM::t2LDRBpci);
6828     return true;
6829   case ARM::t2LDRHpcrel:
6830     Inst.setOpcode(ARM::t2LDRHpci);
6831     return true;
6832   case ARM::t2LDRSBpcrel:
6833     Inst.setOpcode(ARM::t2LDRSBpci);
6834     return true;
6835   case ARM::t2LDRSHpcrel:
6836     Inst.setOpcode(ARM::t2LDRSHpci);
6837     return true;
6838   // Handle NEON VST complex aliases.
6839   case ARM::VST1LNdWB_register_Asm_8:
6840   case ARM::VST1LNdWB_register_Asm_16:
6841   case ARM::VST1LNdWB_register_Asm_32: {
6842     MCInst TmpInst;
6843     // Shuffle the operands around so the lane index operand is in the
6844     // right place.
6845     unsigned Spacing;
6846     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
6847     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
6848     TmpInst.addOperand(Inst.getOperand(2)); // Rn
6849     TmpInst.addOperand(Inst.getOperand(3)); // alignment
6850     TmpInst.addOperand(Inst.getOperand(4)); // Rm
6851     TmpInst.addOperand(Inst.getOperand(0)); // Vd
6852     TmpInst.addOperand(Inst.getOperand(1)); // lane
6853     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
6854     TmpInst.addOperand(Inst.getOperand(6));
6855     Inst = TmpInst;
6856     return true;
6857   }
6858 
6859   case ARM::VST2LNdWB_register_Asm_8:
6860   case ARM::VST2LNdWB_register_Asm_16:
6861   case ARM::VST2LNdWB_register_Asm_32:
6862   case ARM::VST2LNqWB_register_Asm_16:
6863   case ARM::VST2LNqWB_register_Asm_32: {
6864     MCInst TmpInst;
6865     // Shuffle the operands around so the lane index operand is in the
6866     // right place.
6867     unsigned Spacing;
6868     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
6869     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
6870     TmpInst.addOperand(Inst.getOperand(2)); // Rn
6871     TmpInst.addOperand(Inst.getOperand(3)); // alignment
6872     TmpInst.addOperand(Inst.getOperand(4)); // Rm
6873     TmpInst.addOperand(Inst.getOperand(0)); // Vd
6874     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6875                                             Spacing));
6876     TmpInst.addOperand(Inst.getOperand(1)); // lane
6877     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
6878     TmpInst.addOperand(Inst.getOperand(6));
6879     Inst = TmpInst;
6880     return true;
6881   }
6882 
6883   case ARM::VST3LNdWB_register_Asm_8:
6884   case ARM::VST3LNdWB_register_Asm_16:
6885   case ARM::VST3LNdWB_register_Asm_32:
6886   case ARM::VST3LNqWB_register_Asm_16:
6887   case ARM::VST3LNqWB_register_Asm_32: {
6888     MCInst TmpInst;
6889     // Shuffle the operands around so the lane index operand is in the
6890     // right place.
6891     unsigned Spacing;
6892     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
6893     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
6894     TmpInst.addOperand(Inst.getOperand(2)); // Rn
6895     TmpInst.addOperand(Inst.getOperand(3)); // alignment
6896     TmpInst.addOperand(Inst.getOperand(4)); // Rm
6897     TmpInst.addOperand(Inst.getOperand(0)); // Vd
6898     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6899                                             Spacing));
6900     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6901                                             Spacing * 2));
6902     TmpInst.addOperand(Inst.getOperand(1)); // lane
6903     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
6904     TmpInst.addOperand(Inst.getOperand(6));
6905     Inst = TmpInst;
6906     return true;
6907   }
6908 
6909   case ARM::VST4LNdWB_register_Asm_8:
6910   case ARM::VST4LNdWB_register_Asm_16:
6911   case ARM::VST4LNdWB_register_Asm_32:
6912   case ARM::VST4LNqWB_register_Asm_16:
6913   case ARM::VST4LNqWB_register_Asm_32: {
6914     MCInst TmpInst;
6915     // Shuffle the operands around so the lane index operand is in the
6916     // right place.
6917     unsigned Spacing;
6918     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
6919     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
6920     TmpInst.addOperand(Inst.getOperand(2)); // Rn
6921     TmpInst.addOperand(Inst.getOperand(3)); // alignment
6922     TmpInst.addOperand(Inst.getOperand(4)); // Rm
6923     TmpInst.addOperand(Inst.getOperand(0)); // Vd
6924     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6925                                             Spacing));
6926     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6927                                             Spacing * 2));
6928     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6929                                             Spacing * 3));
6930     TmpInst.addOperand(Inst.getOperand(1)); // lane
6931     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
6932     TmpInst.addOperand(Inst.getOperand(6));
6933     Inst = TmpInst;
6934     return true;
6935   }
6936 
6937   case ARM::VST1LNdWB_fixed_Asm_8:
6938   case ARM::VST1LNdWB_fixed_Asm_16:
6939   case ARM::VST1LNdWB_fixed_Asm_32: {
6940     MCInst TmpInst;
6941     // Shuffle the operands around so the lane index operand is in the
6942     // right place.
6943     unsigned Spacing;
6944     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
6945     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
6946     TmpInst.addOperand(Inst.getOperand(2)); // Rn
6947     TmpInst.addOperand(Inst.getOperand(3)); // alignment
6948     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
6949     TmpInst.addOperand(Inst.getOperand(0)); // Vd
6950     TmpInst.addOperand(Inst.getOperand(1)); // lane
6951     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
6952     TmpInst.addOperand(Inst.getOperand(5));
6953     Inst = TmpInst;
6954     return true;
6955   }
6956 
6957   case ARM::VST2LNdWB_fixed_Asm_8:
6958   case ARM::VST2LNdWB_fixed_Asm_16:
6959   case ARM::VST2LNdWB_fixed_Asm_32:
6960   case ARM::VST2LNqWB_fixed_Asm_16:
6961   case ARM::VST2LNqWB_fixed_Asm_32: {
6962     MCInst TmpInst;
6963     // Shuffle the operands around so the lane index operand is in the
6964     // right place.
6965     unsigned Spacing;
6966     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
6967     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
6968     TmpInst.addOperand(Inst.getOperand(2)); // Rn
6969     TmpInst.addOperand(Inst.getOperand(3)); // alignment
6970     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
6971     TmpInst.addOperand(Inst.getOperand(0)); // Vd
6972     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6973                                             Spacing));
6974     TmpInst.addOperand(Inst.getOperand(1)); // lane
6975     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
6976     TmpInst.addOperand(Inst.getOperand(5));
6977     Inst = TmpInst;
6978     return true;
6979   }
6980 
6981   case ARM::VST3LNdWB_fixed_Asm_8:
6982   case ARM::VST3LNdWB_fixed_Asm_16:
6983   case ARM::VST3LNdWB_fixed_Asm_32:
6984   case ARM::VST3LNqWB_fixed_Asm_16:
6985   case ARM::VST3LNqWB_fixed_Asm_32: {
6986     MCInst TmpInst;
6987     // Shuffle the operands around so the lane index operand is in the
6988     // right place.
6989     unsigned Spacing;
6990     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
6991     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
6992     TmpInst.addOperand(Inst.getOperand(2)); // Rn
6993     TmpInst.addOperand(Inst.getOperand(3)); // alignment
6994     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
6995     TmpInst.addOperand(Inst.getOperand(0)); // Vd
6996     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6997                                             Spacing));
6998     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
6999                                             Spacing * 2));
7000     TmpInst.addOperand(Inst.getOperand(1)); // lane
7001     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7002     TmpInst.addOperand(Inst.getOperand(5));
7003     Inst = TmpInst;
7004     return true;
7005   }
7006 
7007   case ARM::VST4LNdWB_fixed_Asm_8:
7008   case ARM::VST4LNdWB_fixed_Asm_16:
7009   case ARM::VST4LNdWB_fixed_Asm_32:
7010   case ARM::VST4LNqWB_fixed_Asm_16:
7011   case ARM::VST4LNqWB_fixed_Asm_32: {
7012     MCInst TmpInst;
7013     // Shuffle the operands around so the lane index operand is in the
7014     // right place.
7015     unsigned Spacing;
7016     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7017     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7018     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7019     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7020     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7021     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7022     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7023                                             Spacing));
7024     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7025                                             Spacing * 2));
7026     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7027                                             Spacing * 3));
7028     TmpInst.addOperand(Inst.getOperand(1)); // lane
7029     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7030     TmpInst.addOperand(Inst.getOperand(5));
7031     Inst = TmpInst;
7032     return true;
7033   }
7034 
7035   case ARM::VST1LNdAsm_8:
7036   case ARM::VST1LNdAsm_16:
7037   case ARM::VST1LNdAsm_32: {
7038     MCInst TmpInst;
7039     // Shuffle the operands around so the lane index operand is in the
7040     // right place.
7041     unsigned Spacing;
7042     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7043     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7044     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7045     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7046     TmpInst.addOperand(Inst.getOperand(1)); // lane
7047     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7048     TmpInst.addOperand(Inst.getOperand(5));
7049     Inst = TmpInst;
7050     return true;
7051   }
7052 
7053   case ARM::VST2LNdAsm_8:
7054   case ARM::VST2LNdAsm_16:
7055   case ARM::VST2LNdAsm_32:
7056   case ARM::VST2LNqAsm_16:
7057   case ARM::VST2LNqAsm_32: {
7058     MCInst TmpInst;
7059     // Shuffle the operands around so the lane index operand is in the
7060     // right place.
7061     unsigned Spacing;
7062     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7063     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7064     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7065     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7066     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7067                                             Spacing));
7068     TmpInst.addOperand(Inst.getOperand(1)); // lane
7069     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7070     TmpInst.addOperand(Inst.getOperand(5));
7071     Inst = TmpInst;
7072     return true;
7073   }
7074 
7075   case ARM::VST3LNdAsm_8:
7076   case ARM::VST3LNdAsm_16:
7077   case ARM::VST3LNdAsm_32:
7078   case ARM::VST3LNqAsm_16:
7079   case ARM::VST3LNqAsm_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
7086     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7087     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7088     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7089                                             Spacing));
7090     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7091                                             Spacing * 2));
7092     TmpInst.addOperand(Inst.getOperand(1)); // lane
7093     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7094     TmpInst.addOperand(Inst.getOperand(5));
7095     Inst = TmpInst;
7096     return true;
7097   }
7098 
7099   case ARM::VST4LNdAsm_8:
7100   case ARM::VST4LNdAsm_16:
7101   case ARM::VST4LNdAsm_32:
7102   case ARM::VST4LNqAsm_16:
7103   case ARM::VST4LNqAsm_32: {
7104     MCInst TmpInst;
7105     // Shuffle the operands around so the lane index operand is in the
7106     // right place.
7107     unsigned Spacing;
7108     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7109     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7110     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7111     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7112     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7113                                             Spacing));
7114     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7115                                             Spacing * 2));
7116     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7117                                             Spacing * 3));
7118     TmpInst.addOperand(Inst.getOperand(1)); // lane
7119     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7120     TmpInst.addOperand(Inst.getOperand(5));
7121     Inst = TmpInst;
7122     return true;
7123   }
7124 
7125   // Handle NEON VLD complex aliases.
7126   case ARM::VLD1LNdWB_register_Asm_8:
7127   case ARM::VLD1LNdWB_register_Asm_16:
7128   case ARM::VLD1LNdWB_register_Asm_32: {
7129     MCInst TmpInst;
7130     // Shuffle the operands around so the lane index operand is in the
7131     // right place.
7132     unsigned Spacing;
7133     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7134     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7135     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7136     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7137     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7138     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7139     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7140     TmpInst.addOperand(Inst.getOperand(1)); // lane
7141     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7142     TmpInst.addOperand(Inst.getOperand(6));
7143     Inst = TmpInst;
7144     return true;
7145   }
7146 
7147   case ARM::VLD2LNdWB_register_Asm_8:
7148   case ARM::VLD2LNdWB_register_Asm_16:
7149   case ARM::VLD2LNdWB_register_Asm_32:
7150   case ARM::VLD2LNqWB_register_Asm_16:
7151   case ARM::VLD2LNqWB_register_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(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7157     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7158     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7159                                             Spacing));
7160     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7161     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7162     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7163     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7164     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7165     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7166                                             Spacing));
7167     TmpInst.addOperand(Inst.getOperand(1)); // lane
7168     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7169     TmpInst.addOperand(Inst.getOperand(6));
7170     Inst = TmpInst;
7171     return true;
7172   }
7173 
7174   case ARM::VLD3LNdWB_register_Asm_8:
7175   case ARM::VLD3LNdWB_register_Asm_16:
7176   case ARM::VLD3LNdWB_register_Asm_32:
7177   case ARM::VLD3LNqWB_register_Asm_16:
7178   case ARM::VLD3LNqWB_register_Asm_32: {
7179     MCInst TmpInst;
7180     // Shuffle the operands around so the lane index operand is in the
7181     // right place.
7182     unsigned Spacing;
7183     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7184     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7185     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7186                                             Spacing));
7187     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7188                                             Spacing * 2));
7189     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7190     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7191     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7192     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7193     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7194     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7195                                             Spacing));
7196     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7197                                             Spacing * 2));
7198     TmpInst.addOperand(Inst.getOperand(1)); // lane
7199     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7200     TmpInst.addOperand(Inst.getOperand(6));
7201     Inst = TmpInst;
7202     return true;
7203   }
7204 
7205   case ARM::VLD4LNdWB_register_Asm_8:
7206   case ARM::VLD4LNdWB_register_Asm_16:
7207   case ARM::VLD4LNdWB_register_Asm_32:
7208   case ARM::VLD4LNqWB_register_Asm_16:
7209   case ARM::VLD4LNqWB_register_Asm_32: {
7210     MCInst TmpInst;
7211     // Shuffle the operands around so the lane index operand is in the
7212     // right place.
7213     unsigned Spacing;
7214     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7215     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7216     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7217                                             Spacing));
7218     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7219                                             Spacing * 2));
7220     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7221                                             Spacing * 3));
7222     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7223     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7224     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7225     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7226     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7227     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7228                                             Spacing));
7229     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7230                                             Spacing * 2));
7231     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7232                                             Spacing * 3));
7233     TmpInst.addOperand(Inst.getOperand(1)); // lane
7234     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7235     TmpInst.addOperand(Inst.getOperand(6));
7236     Inst = TmpInst;
7237     return true;
7238   }
7239 
7240   case ARM::VLD1LNdWB_fixed_Asm_8:
7241   case ARM::VLD1LNdWB_fixed_Asm_16:
7242   case ARM::VLD1LNdWB_fixed_Asm_32: {
7243     MCInst TmpInst;
7244     // Shuffle the operands around so the lane index operand is in the
7245     // right place.
7246     unsigned Spacing;
7247     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7248     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7249     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7250     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7251     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7252     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7253     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7254     TmpInst.addOperand(Inst.getOperand(1)); // lane
7255     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7256     TmpInst.addOperand(Inst.getOperand(5));
7257     Inst = TmpInst;
7258     return true;
7259   }
7260 
7261   case ARM::VLD2LNdWB_fixed_Asm_8:
7262   case ARM::VLD2LNdWB_fixed_Asm_16:
7263   case ARM::VLD2LNdWB_fixed_Asm_32:
7264   case ARM::VLD2LNqWB_fixed_Asm_16:
7265   case ARM::VLD2LNqWB_fixed_Asm_32: {
7266     MCInst TmpInst;
7267     // Shuffle the operands around so the lane index operand is in the
7268     // right place.
7269     unsigned Spacing;
7270     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7271     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7272     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7273                                             Spacing));
7274     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7275     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7276     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7277     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7278     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7279     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7280                                             Spacing));
7281     TmpInst.addOperand(Inst.getOperand(1)); // lane
7282     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7283     TmpInst.addOperand(Inst.getOperand(5));
7284     Inst = TmpInst;
7285     return true;
7286   }
7287 
7288   case ARM::VLD3LNdWB_fixed_Asm_8:
7289   case ARM::VLD3LNdWB_fixed_Asm_16:
7290   case ARM::VLD3LNdWB_fixed_Asm_32:
7291   case ARM::VLD3LNqWB_fixed_Asm_16:
7292   case ARM::VLD3LNqWB_fixed_Asm_32: {
7293     MCInst TmpInst;
7294     // Shuffle the operands around so the lane index operand is in the
7295     // right place.
7296     unsigned Spacing;
7297     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7298     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7299     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7300                                             Spacing));
7301     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7302                                             Spacing * 2));
7303     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7304     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7305     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7306     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7307     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7308     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7309                                             Spacing));
7310     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7311                                             Spacing * 2));
7312     TmpInst.addOperand(Inst.getOperand(1)); // lane
7313     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7314     TmpInst.addOperand(Inst.getOperand(5));
7315     Inst = TmpInst;
7316     return true;
7317   }
7318 
7319   case ARM::VLD4LNdWB_fixed_Asm_8:
7320   case ARM::VLD4LNdWB_fixed_Asm_16:
7321   case ARM::VLD4LNdWB_fixed_Asm_32:
7322   case ARM::VLD4LNqWB_fixed_Asm_16:
7323   case ARM::VLD4LNqWB_fixed_Asm_32: {
7324     MCInst TmpInst;
7325     // Shuffle the operands around so the lane index operand is in the
7326     // right place.
7327     unsigned Spacing;
7328     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7329     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7330     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7331                                             Spacing));
7332     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7333                                             Spacing * 2));
7334     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7335                                             Spacing * 3));
7336     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7337     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7338     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7339     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7340     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7341     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7342                                             Spacing));
7343     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7344                                             Spacing * 2));
7345     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7346                                             Spacing * 3));
7347     TmpInst.addOperand(Inst.getOperand(1)); // lane
7348     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7349     TmpInst.addOperand(Inst.getOperand(5));
7350     Inst = TmpInst;
7351     return true;
7352   }
7353 
7354   case ARM::VLD1LNdAsm_8:
7355   case ARM::VLD1LNdAsm_16:
7356   case ARM::VLD1LNdAsm_32: {
7357     MCInst TmpInst;
7358     // Shuffle the operands around so the lane index operand is in the
7359     // right place.
7360     unsigned Spacing;
7361     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7362     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7363     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7364     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7365     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7366     TmpInst.addOperand(Inst.getOperand(1)); // lane
7367     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7368     TmpInst.addOperand(Inst.getOperand(5));
7369     Inst = TmpInst;
7370     return true;
7371   }
7372 
7373   case ARM::VLD2LNdAsm_8:
7374   case ARM::VLD2LNdAsm_16:
7375   case ARM::VLD2LNdAsm_32:
7376   case ARM::VLD2LNqAsm_16:
7377   case ARM::VLD2LNqAsm_32: {
7378     MCInst TmpInst;
7379     // Shuffle the operands around so the lane index operand is in the
7380     // right place.
7381     unsigned Spacing;
7382     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7383     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7384     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7385                                             Spacing));
7386     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7387     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7388     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7389     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7390                                             Spacing));
7391     TmpInst.addOperand(Inst.getOperand(1)); // lane
7392     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7393     TmpInst.addOperand(Inst.getOperand(5));
7394     Inst = TmpInst;
7395     return true;
7396   }
7397 
7398   case ARM::VLD3LNdAsm_8:
7399   case ARM::VLD3LNdAsm_16:
7400   case ARM::VLD3LNdAsm_32:
7401   case ARM::VLD3LNqAsm_16:
7402   case ARM::VLD3LNqAsm_32: {
7403     MCInst TmpInst;
7404     // Shuffle the operands around so the lane index operand is in the
7405     // right place.
7406     unsigned Spacing;
7407     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7408     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7409     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7410                                             Spacing));
7411     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7412                                             Spacing * 2));
7413     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7414     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7415     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7416     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7417                                             Spacing));
7418     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7419                                             Spacing * 2));
7420     TmpInst.addOperand(Inst.getOperand(1)); // lane
7421     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7422     TmpInst.addOperand(Inst.getOperand(5));
7423     Inst = TmpInst;
7424     return true;
7425   }
7426 
7427   case ARM::VLD4LNdAsm_8:
7428   case ARM::VLD4LNdAsm_16:
7429   case ARM::VLD4LNdAsm_32:
7430   case ARM::VLD4LNqAsm_16:
7431   case ARM::VLD4LNqAsm_32: {
7432     MCInst TmpInst;
7433     // Shuffle the operands around so the lane index operand is in the
7434     // right place.
7435     unsigned Spacing;
7436     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7437     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7438     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7439                                             Spacing));
7440     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7441                                             Spacing * 2));
7442     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7443                                             Spacing * 3));
7444     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7445     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7446     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7447     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7448                                             Spacing));
7449     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7450                                             Spacing * 2));
7451     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7452                                             Spacing * 3));
7453     TmpInst.addOperand(Inst.getOperand(1)); // lane
7454     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7455     TmpInst.addOperand(Inst.getOperand(5));
7456     Inst = TmpInst;
7457     return true;
7458   }
7459 
7460   // VLD3DUP single 3-element structure to all lanes instructions.
7461   case ARM::VLD3DUPdAsm_8:
7462   case ARM::VLD3DUPdAsm_16:
7463   case ARM::VLD3DUPdAsm_32:
7464   case ARM::VLD3DUPqAsm_8:
7465   case ARM::VLD3DUPqAsm_16:
7466   case ARM::VLD3DUPqAsm_32: {
7467     MCInst TmpInst;
7468     unsigned Spacing;
7469     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7470     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7471     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7472                                             Spacing));
7473     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7474                                             Spacing * 2));
7475     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7476     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7477     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7478     TmpInst.addOperand(Inst.getOperand(4));
7479     Inst = TmpInst;
7480     return true;
7481   }
7482 
7483   case ARM::VLD3DUPdWB_fixed_Asm_8:
7484   case ARM::VLD3DUPdWB_fixed_Asm_16:
7485   case ARM::VLD3DUPdWB_fixed_Asm_32:
7486   case ARM::VLD3DUPqWB_fixed_Asm_8:
7487   case ARM::VLD3DUPqWB_fixed_Asm_16:
7488   case ARM::VLD3DUPqWB_fixed_Asm_32: {
7489     MCInst TmpInst;
7490     unsigned Spacing;
7491     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7492     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7493     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7494                                             Spacing));
7495     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7496                                             Spacing * 2));
7497     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7498     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7499     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7500     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7501     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7502     TmpInst.addOperand(Inst.getOperand(4));
7503     Inst = TmpInst;
7504     return true;
7505   }
7506 
7507   case ARM::VLD3DUPdWB_register_Asm_8:
7508   case ARM::VLD3DUPdWB_register_Asm_16:
7509   case ARM::VLD3DUPdWB_register_Asm_32:
7510   case ARM::VLD3DUPqWB_register_Asm_8:
7511   case ARM::VLD3DUPqWB_register_Asm_16:
7512   case ARM::VLD3DUPqWB_register_Asm_32: {
7513     MCInst TmpInst;
7514     unsigned Spacing;
7515     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7516     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7517     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7518                                             Spacing));
7519     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7520                                             Spacing * 2));
7521     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7522     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7523     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7524     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7525     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7526     TmpInst.addOperand(Inst.getOperand(5));
7527     Inst = TmpInst;
7528     return true;
7529   }
7530 
7531   // VLD3 multiple 3-element structure instructions.
7532   case ARM::VLD3dAsm_8:
7533   case ARM::VLD3dAsm_16:
7534   case ARM::VLD3dAsm_32:
7535   case ARM::VLD3qAsm_8:
7536   case ARM::VLD3qAsm_16:
7537   case ARM::VLD3qAsm_32: {
7538     MCInst TmpInst;
7539     unsigned Spacing;
7540     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7541     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7542     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7543                                             Spacing));
7544     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7545                                             Spacing * 2));
7546     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7547     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7548     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7549     TmpInst.addOperand(Inst.getOperand(4));
7550     Inst = TmpInst;
7551     return true;
7552   }
7553 
7554   case ARM::VLD3dWB_fixed_Asm_8:
7555   case ARM::VLD3dWB_fixed_Asm_16:
7556   case ARM::VLD3dWB_fixed_Asm_32:
7557   case ARM::VLD3qWB_fixed_Asm_8:
7558   case ARM::VLD3qWB_fixed_Asm_16:
7559   case ARM::VLD3qWB_fixed_Asm_32: {
7560     MCInst TmpInst;
7561     unsigned Spacing;
7562     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7563     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7564     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7565                                             Spacing));
7566     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7567                                             Spacing * 2));
7568     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7569     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7570     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7571     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7572     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7573     TmpInst.addOperand(Inst.getOperand(4));
7574     Inst = TmpInst;
7575     return true;
7576   }
7577 
7578   case ARM::VLD3dWB_register_Asm_8:
7579   case ARM::VLD3dWB_register_Asm_16:
7580   case ARM::VLD3dWB_register_Asm_32:
7581   case ARM::VLD3qWB_register_Asm_8:
7582   case ARM::VLD3qWB_register_Asm_16:
7583   case ARM::VLD3qWB_register_Asm_32: {
7584     MCInst TmpInst;
7585     unsigned Spacing;
7586     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7587     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7588     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7589                                             Spacing));
7590     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7591                                             Spacing * 2));
7592     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7593     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7594     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7595     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7596     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7597     TmpInst.addOperand(Inst.getOperand(5));
7598     Inst = TmpInst;
7599     return true;
7600   }
7601 
7602   // VLD4DUP single 3-element structure to all lanes instructions.
7603   case ARM::VLD4DUPdAsm_8:
7604   case ARM::VLD4DUPdAsm_16:
7605   case ARM::VLD4DUPdAsm_32:
7606   case ARM::VLD4DUPqAsm_8:
7607   case ARM::VLD4DUPqAsm_16:
7608   case ARM::VLD4DUPqAsm_32: {
7609     MCInst TmpInst;
7610     unsigned Spacing;
7611     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7612     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7613     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7614                                             Spacing));
7615     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7616                                             Spacing * 2));
7617     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7618                                             Spacing * 3));
7619     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7620     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7621     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7622     TmpInst.addOperand(Inst.getOperand(4));
7623     Inst = TmpInst;
7624     return true;
7625   }
7626 
7627   case ARM::VLD4DUPdWB_fixed_Asm_8:
7628   case ARM::VLD4DUPdWB_fixed_Asm_16:
7629   case ARM::VLD4DUPdWB_fixed_Asm_32:
7630   case ARM::VLD4DUPqWB_fixed_Asm_8:
7631   case ARM::VLD4DUPqWB_fixed_Asm_16:
7632   case ARM::VLD4DUPqWB_fixed_Asm_32: {
7633     MCInst TmpInst;
7634     unsigned Spacing;
7635     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7636     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7637     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7638                                             Spacing));
7639     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7640                                             Spacing * 2));
7641     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7642                                             Spacing * 3));
7643     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7644     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7645     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7646     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7647     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7648     TmpInst.addOperand(Inst.getOperand(4));
7649     Inst = TmpInst;
7650     return true;
7651   }
7652 
7653   case ARM::VLD4DUPdWB_register_Asm_8:
7654   case ARM::VLD4DUPdWB_register_Asm_16:
7655   case ARM::VLD4DUPdWB_register_Asm_32:
7656   case ARM::VLD4DUPqWB_register_Asm_8:
7657   case ARM::VLD4DUPqWB_register_Asm_16:
7658   case ARM::VLD4DUPqWB_register_Asm_32: {
7659     MCInst TmpInst;
7660     unsigned Spacing;
7661     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7662     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7663     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7664                                             Spacing));
7665     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7666                                             Spacing * 2));
7667     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7668                                             Spacing * 3));
7669     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7670     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7671     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7672     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7673     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7674     TmpInst.addOperand(Inst.getOperand(5));
7675     Inst = TmpInst;
7676     return true;
7677   }
7678 
7679   // VLD4 multiple 4-element structure instructions.
7680   case ARM::VLD4dAsm_8:
7681   case ARM::VLD4dAsm_16:
7682   case ARM::VLD4dAsm_32:
7683   case ARM::VLD4qAsm_8:
7684   case ARM::VLD4qAsm_16:
7685   case ARM::VLD4qAsm_32: {
7686     MCInst TmpInst;
7687     unsigned Spacing;
7688     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7689     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7690     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7691                                             Spacing));
7692     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7693                                             Spacing * 2));
7694     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7695                                             Spacing * 3));
7696     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7697     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7698     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7699     TmpInst.addOperand(Inst.getOperand(4));
7700     Inst = TmpInst;
7701     return true;
7702   }
7703 
7704   case ARM::VLD4dWB_fixed_Asm_8:
7705   case ARM::VLD4dWB_fixed_Asm_16:
7706   case ARM::VLD4dWB_fixed_Asm_32:
7707   case ARM::VLD4qWB_fixed_Asm_8:
7708   case ARM::VLD4qWB_fixed_Asm_16:
7709   case ARM::VLD4qWB_fixed_Asm_32: {
7710     MCInst TmpInst;
7711     unsigned Spacing;
7712     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7713     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7714     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7715                                             Spacing));
7716     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7717                                             Spacing * 2));
7718     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7719                                             Spacing * 3));
7720     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7721     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7722     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7723     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7724     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7725     TmpInst.addOperand(Inst.getOperand(4));
7726     Inst = TmpInst;
7727     return true;
7728   }
7729 
7730   case ARM::VLD4dWB_register_Asm_8:
7731   case ARM::VLD4dWB_register_Asm_16:
7732   case ARM::VLD4dWB_register_Asm_32:
7733   case ARM::VLD4qWB_register_Asm_8:
7734   case ARM::VLD4qWB_register_Asm_16:
7735   case ARM::VLD4qWB_register_Asm_32: {
7736     MCInst TmpInst;
7737     unsigned Spacing;
7738     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7739     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7740     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7741                                             Spacing));
7742     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7743                                             Spacing * 2));
7744     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7745                                             Spacing * 3));
7746     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7747     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7748     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7749     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7750     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7751     TmpInst.addOperand(Inst.getOperand(5));
7752     Inst = TmpInst;
7753     return true;
7754   }
7755 
7756   // VST3 multiple 3-element structure instructions.
7757   case ARM::VST3dAsm_8:
7758   case ARM::VST3dAsm_16:
7759   case ARM::VST3dAsm_32:
7760   case ARM::VST3qAsm_8:
7761   case ARM::VST3qAsm_16:
7762   case ARM::VST3qAsm_32: {
7763     MCInst TmpInst;
7764     unsigned Spacing;
7765     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7766     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7767     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7768     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7769     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7770                                             Spacing));
7771     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7772                                             Spacing * 2));
7773     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7774     TmpInst.addOperand(Inst.getOperand(4));
7775     Inst = TmpInst;
7776     return true;
7777   }
7778 
7779   case ARM::VST3dWB_fixed_Asm_8:
7780   case ARM::VST3dWB_fixed_Asm_16:
7781   case ARM::VST3dWB_fixed_Asm_32:
7782   case ARM::VST3qWB_fixed_Asm_8:
7783   case ARM::VST3qWB_fixed_Asm_16:
7784   case ARM::VST3qWB_fixed_Asm_32: {
7785     MCInst TmpInst;
7786     unsigned Spacing;
7787     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7788     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7789     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7790     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7791     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7792     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7793     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7794                                             Spacing));
7795     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7796                                             Spacing * 2));
7797     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7798     TmpInst.addOperand(Inst.getOperand(4));
7799     Inst = TmpInst;
7800     return true;
7801   }
7802 
7803   case ARM::VST3dWB_register_Asm_8:
7804   case ARM::VST3dWB_register_Asm_16:
7805   case ARM::VST3dWB_register_Asm_32:
7806   case ARM::VST3qWB_register_Asm_8:
7807   case ARM::VST3qWB_register_Asm_16:
7808   case ARM::VST3qWB_register_Asm_32: {
7809     MCInst TmpInst;
7810     unsigned Spacing;
7811     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7812     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7813     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7814     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7815     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7816     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7817     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7818                                             Spacing));
7819     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7820                                             Spacing * 2));
7821     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7822     TmpInst.addOperand(Inst.getOperand(5));
7823     Inst = TmpInst;
7824     return true;
7825   }
7826 
7827   // VST4 multiple 3-element structure instructions.
7828   case ARM::VST4dAsm_8:
7829   case ARM::VST4dAsm_16:
7830   case ARM::VST4dAsm_32:
7831   case ARM::VST4qAsm_8:
7832   case ARM::VST4qAsm_16:
7833   case ARM::VST4qAsm_32: {
7834     MCInst TmpInst;
7835     unsigned Spacing;
7836     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7837     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7838     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7839     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7840     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7841                                             Spacing));
7842     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7843                                             Spacing * 2));
7844     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7845                                             Spacing * 3));
7846     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7847     TmpInst.addOperand(Inst.getOperand(4));
7848     Inst = TmpInst;
7849     return true;
7850   }
7851 
7852   case ARM::VST4dWB_fixed_Asm_8:
7853   case ARM::VST4dWB_fixed_Asm_16:
7854   case ARM::VST4dWB_fixed_Asm_32:
7855   case ARM::VST4qWB_fixed_Asm_8:
7856   case ARM::VST4qWB_fixed_Asm_16:
7857   case ARM::VST4qWB_fixed_Asm_32: {
7858     MCInst TmpInst;
7859     unsigned Spacing;
7860     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7861     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7862     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7863     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7864     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7865     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7866     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7867                                             Spacing));
7868     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7869                                             Spacing * 2));
7870     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7871                                             Spacing * 3));
7872     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7873     TmpInst.addOperand(Inst.getOperand(4));
7874     Inst = TmpInst;
7875     return true;
7876   }
7877 
7878   case ARM::VST4dWB_register_Asm_8:
7879   case ARM::VST4dWB_register_Asm_16:
7880   case ARM::VST4dWB_register_Asm_32:
7881   case ARM::VST4qWB_register_Asm_8:
7882   case ARM::VST4qWB_register_Asm_16:
7883   case ARM::VST4qWB_register_Asm_32: {
7884     MCInst TmpInst;
7885     unsigned Spacing;
7886     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7887     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7888     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7889     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7890     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7891     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7892     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7893                                             Spacing));
7894     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7895                                             Spacing * 2));
7896     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7897                                             Spacing * 3));
7898     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7899     TmpInst.addOperand(Inst.getOperand(5));
7900     Inst = TmpInst;
7901     return true;
7902   }
7903 
7904   // Handle encoding choice for the shift-immediate instructions.
7905   case ARM::t2LSLri:
7906   case ARM::t2LSRri:
7907   case ARM::t2ASRri: {
7908     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
7909         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
7910         Inst.getOperand(5).getReg() == (inITBlock() ? 0 : ARM::CPSR) &&
7911         !(static_cast<ARMOperand &>(*Operands[3]).isToken() &&
7912           static_cast<ARMOperand &>(*Operands[3]).getToken() == ".w")) {
7913       unsigned NewOpc;
7914       switch (Inst.getOpcode()) {
7915       default: llvm_unreachable("unexpected opcode");
7916       case ARM::t2LSLri: NewOpc = ARM::tLSLri; break;
7917       case ARM::t2LSRri: NewOpc = ARM::tLSRri; break;
7918       case ARM::t2ASRri: NewOpc = ARM::tASRri; break;
7919       }
7920       // The Thumb1 operands aren't in the same order. Awesome, eh?
7921       MCInst TmpInst;
7922       TmpInst.setOpcode(NewOpc);
7923       TmpInst.addOperand(Inst.getOperand(0));
7924       TmpInst.addOperand(Inst.getOperand(5));
7925       TmpInst.addOperand(Inst.getOperand(1));
7926       TmpInst.addOperand(Inst.getOperand(2));
7927       TmpInst.addOperand(Inst.getOperand(3));
7928       TmpInst.addOperand(Inst.getOperand(4));
7929       Inst = TmpInst;
7930       return true;
7931     }
7932     return false;
7933   }
7934 
7935   // Handle the Thumb2 mode MOV complex aliases.
7936   case ARM::t2MOVsr:
7937   case ARM::t2MOVSsr: {
7938     // Which instruction to expand to depends on the CCOut operand and
7939     // whether we're in an IT block if the register operands are low
7940     // registers.
7941     bool isNarrow = false;
7942     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
7943         isARMLowRegister(Inst.getOperand(1).getReg()) &&
7944         isARMLowRegister(Inst.getOperand(2).getReg()) &&
7945         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
7946         inITBlock() == (Inst.getOpcode() == ARM::t2MOVsr))
7947       isNarrow = true;
7948     MCInst TmpInst;
7949     unsigned newOpc;
7950     switch(ARM_AM::getSORegShOp(Inst.getOperand(3).getImm())) {
7951     default: llvm_unreachable("unexpected opcode!");
7952     case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRrr : ARM::t2ASRrr; break;
7953     case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRrr : ARM::t2LSRrr; break;
7954     case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLrr : ARM::t2LSLrr; break;
7955     case ARM_AM::ror: newOpc = isNarrow ? ARM::tROR   : ARM::t2RORrr; break;
7956     }
7957     TmpInst.setOpcode(newOpc);
7958     TmpInst.addOperand(Inst.getOperand(0)); // Rd
7959     if (isNarrow)
7960       TmpInst.addOperand(MCOperand::createReg(
7961           Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
7962     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7963     TmpInst.addOperand(Inst.getOperand(2)); // Rm
7964     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7965     TmpInst.addOperand(Inst.getOperand(5));
7966     if (!isNarrow)
7967       TmpInst.addOperand(MCOperand::createReg(
7968           Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
7969     Inst = TmpInst;
7970     return true;
7971   }
7972   case ARM::t2MOVsi:
7973   case ARM::t2MOVSsi: {
7974     // Which instruction to expand to depends on the CCOut operand and
7975     // whether we're in an IT block if the register operands are low
7976     // registers.
7977     bool isNarrow = false;
7978     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
7979         isARMLowRegister(Inst.getOperand(1).getReg()) &&
7980         inITBlock() == (Inst.getOpcode() == ARM::t2MOVsi))
7981       isNarrow = true;
7982     MCInst TmpInst;
7983     unsigned newOpc;
7984     switch(ARM_AM::getSORegShOp(Inst.getOperand(2).getImm())) {
7985     default: llvm_unreachable("unexpected opcode!");
7986     case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRri : ARM::t2ASRri; break;
7987     case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRri : ARM::t2LSRri; break;
7988     case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLri : ARM::t2LSLri; break;
7989     case ARM_AM::ror: newOpc = ARM::t2RORri; isNarrow = false; break;
7990     case ARM_AM::rrx: isNarrow = false; newOpc = ARM::t2RRX; break;
7991     }
7992     unsigned Amount = ARM_AM::getSORegOffset(Inst.getOperand(2).getImm());
7993     if (Amount == 32) Amount = 0;
7994     TmpInst.setOpcode(newOpc);
7995     TmpInst.addOperand(Inst.getOperand(0)); // Rd
7996     if (isNarrow)
7997       TmpInst.addOperand(MCOperand::createReg(
7998           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
7999     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8000     if (newOpc != ARM::t2RRX)
8001       TmpInst.addOperand(MCOperand::createImm(Amount));
8002     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8003     TmpInst.addOperand(Inst.getOperand(4));
8004     if (!isNarrow)
8005       TmpInst.addOperand(MCOperand::createReg(
8006           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
8007     Inst = TmpInst;
8008     return true;
8009   }
8010   // Handle the ARM mode MOV complex aliases.
8011   case ARM::ASRr:
8012   case ARM::LSRr:
8013   case ARM::LSLr:
8014   case ARM::RORr: {
8015     ARM_AM::ShiftOpc ShiftTy;
8016     switch(Inst.getOpcode()) {
8017     default: llvm_unreachable("unexpected opcode!");
8018     case ARM::ASRr: ShiftTy = ARM_AM::asr; break;
8019     case ARM::LSRr: ShiftTy = ARM_AM::lsr; break;
8020     case ARM::LSLr: ShiftTy = ARM_AM::lsl; break;
8021     case ARM::RORr: ShiftTy = ARM_AM::ror; break;
8022     }
8023     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, 0);
8024     MCInst TmpInst;
8025     TmpInst.setOpcode(ARM::MOVsr);
8026     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8027     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8028     TmpInst.addOperand(Inst.getOperand(2)); // Rm
8029     TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8030     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8031     TmpInst.addOperand(Inst.getOperand(4));
8032     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
8033     Inst = TmpInst;
8034     return true;
8035   }
8036   case ARM::ASRi:
8037   case ARM::LSRi:
8038   case ARM::LSLi:
8039   case ARM::RORi: {
8040     ARM_AM::ShiftOpc ShiftTy;
8041     switch(Inst.getOpcode()) {
8042     default: llvm_unreachable("unexpected opcode!");
8043     case ARM::ASRi: ShiftTy = ARM_AM::asr; break;
8044     case ARM::LSRi: ShiftTy = ARM_AM::lsr; break;
8045     case ARM::LSLi: ShiftTy = ARM_AM::lsl; break;
8046     case ARM::RORi: ShiftTy = ARM_AM::ror; break;
8047     }
8048     // A shift by zero is a plain MOVr, not a MOVsi.
8049     unsigned Amt = Inst.getOperand(2).getImm();
8050     unsigned Opc = Amt == 0 ? ARM::MOVr : ARM::MOVsi;
8051     // A shift by 32 should be encoded as 0 when permitted
8052     if (Amt == 32 && (ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr))
8053       Amt = 0;
8054     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, Amt);
8055     MCInst TmpInst;
8056     TmpInst.setOpcode(Opc);
8057     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8058     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8059     if (Opc == ARM::MOVsi)
8060       TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8061     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8062     TmpInst.addOperand(Inst.getOperand(4));
8063     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
8064     Inst = TmpInst;
8065     return true;
8066   }
8067   case ARM::RRXi: {
8068     unsigned Shifter = ARM_AM::getSORegOpc(ARM_AM::rrx, 0);
8069     MCInst TmpInst;
8070     TmpInst.setOpcode(ARM::MOVsi);
8071     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8072     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8073     TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8074     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8075     TmpInst.addOperand(Inst.getOperand(3));
8076     TmpInst.addOperand(Inst.getOperand(4)); // cc_out
8077     Inst = TmpInst;
8078     return true;
8079   }
8080   case ARM::t2LDMIA_UPD: {
8081     // If this is a load of a single register, then we should use
8082     // a post-indexed LDR instruction instead, per the ARM ARM.
8083     if (Inst.getNumOperands() != 5)
8084       return false;
8085     MCInst TmpInst;
8086     TmpInst.setOpcode(ARM::t2LDR_POST);
8087     TmpInst.addOperand(Inst.getOperand(4)); // Rt
8088     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8089     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8090     TmpInst.addOperand(MCOperand::createImm(4));
8091     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8092     TmpInst.addOperand(Inst.getOperand(3));
8093     Inst = TmpInst;
8094     return true;
8095   }
8096   case ARM::t2STMDB_UPD: {
8097     // If this is a store of a single register, then we should use
8098     // a pre-indexed STR instruction instead, per the ARM ARM.
8099     if (Inst.getNumOperands() != 5)
8100       return false;
8101     MCInst TmpInst;
8102     TmpInst.setOpcode(ARM::t2STR_PRE);
8103     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8104     TmpInst.addOperand(Inst.getOperand(4)); // Rt
8105     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8106     TmpInst.addOperand(MCOperand::createImm(-4));
8107     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8108     TmpInst.addOperand(Inst.getOperand(3));
8109     Inst = TmpInst;
8110     return true;
8111   }
8112   case ARM::LDMIA_UPD:
8113     // If this is a load of a single register via a 'pop', then we should use
8114     // a post-indexed LDR instruction instead, per the ARM ARM.
8115     if (static_cast<ARMOperand &>(*Operands[0]).getToken() == "pop" &&
8116         Inst.getNumOperands() == 5) {
8117       MCInst TmpInst;
8118       TmpInst.setOpcode(ARM::LDR_POST_IMM);
8119       TmpInst.addOperand(Inst.getOperand(4)); // Rt
8120       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8121       TmpInst.addOperand(Inst.getOperand(1)); // Rn
8122       TmpInst.addOperand(MCOperand::createReg(0));  // am2offset
8123       TmpInst.addOperand(MCOperand::createImm(4));
8124       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8125       TmpInst.addOperand(Inst.getOperand(3));
8126       Inst = TmpInst;
8127       return true;
8128     }
8129     break;
8130   case ARM::STMDB_UPD:
8131     // If this is a store of a single register via a 'push', then we should use
8132     // a pre-indexed STR instruction instead, per the ARM ARM.
8133     if (static_cast<ARMOperand &>(*Operands[0]).getToken() == "push" &&
8134         Inst.getNumOperands() == 5) {
8135       MCInst TmpInst;
8136       TmpInst.setOpcode(ARM::STR_PRE_IMM);
8137       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8138       TmpInst.addOperand(Inst.getOperand(4)); // Rt
8139       TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
8140       TmpInst.addOperand(MCOperand::createImm(-4));
8141       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8142       TmpInst.addOperand(Inst.getOperand(3));
8143       Inst = TmpInst;
8144     }
8145     break;
8146   case ARM::t2ADDri12:
8147     // If the immediate fits for encoding T3 (t2ADDri) and the generic "add"
8148     // mnemonic was used (not "addw"), encoding T3 is preferred.
8149     if (static_cast<ARMOperand &>(*Operands[0]).getToken() != "add" ||
8150         ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
8151       break;
8152     Inst.setOpcode(ARM::t2ADDri);
8153     Inst.addOperand(MCOperand::createReg(0)); // cc_out
8154     break;
8155   case ARM::t2SUBri12:
8156     // If the immediate fits for encoding T3 (t2SUBri) and the generic "sub"
8157     // mnemonic was used (not "subw"), encoding T3 is preferred.
8158     if (static_cast<ARMOperand &>(*Operands[0]).getToken() != "sub" ||
8159         ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
8160       break;
8161     Inst.setOpcode(ARM::t2SUBri);
8162     Inst.addOperand(MCOperand::createReg(0)); // cc_out
8163     break;
8164   case ARM::tADDi8:
8165     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
8166     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
8167     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
8168     // to encoding T1 if <Rd> is omitted."
8169     if ((unsigned)Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
8170       Inst.setOpcode(ARM::tADDi3);
8171       return true;
8172     }
8173     break;
8174   case ARM::tSUBi8:
8175     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
8176     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
8177     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
8178     // to encoding T1 if <Rd> is omitted."
8179     if ((unsigned)Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
8180       Inst.setOpcode(ARM::tSUBi3);
8181       return true;
8182     }
8183     break;
8184   case ARM::t2ADDri:
8185   case ARM::t2SUBri: {
8186     // If the destination and first source operand are the same, and
8187     // the flags are compatible with the current IT status, use encoding T2
8188     // instead of T3. For compatibility with the system 'as'. Make sure the
8189     // wide encoding wasn't explicit.
8190     if (Inst.getOperand(0).getReg() != Inst.getOperand(1).getReg() ||
8191         !isARMLowRegister(Inst.getOperand(0).getReg()) ||
8192         (unsigned)Inst.getOperand(2).getImm() > 255 ||
8193         ((!inITBlock() && Inst.getOperand(5).getReg() != ARM::CPSR) ||
8194          (inITBlock() && Inst.getOperand(5).getReg() != 0)) ||
8195         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8196          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".w"))
8197       break;
8198     MCInst TmpInst;
8199     TmpInst.setOpcode(Inst.getOpcode() == ARM::t2ADDri ?
8200                       ARM::tADDi8 : ARM::tSUBi8);
8201     TmpInst.addOperand(Inst.getOperand(0));
8202     TmpInst.addOperand(Inst.getOperand(5));
8203     TmpInst.addOperand(Inst.getOperand(0));
8204     TmpInst.addOperand(Inst.getOperand(2));
8205     TmpInst.addOperand(Inst.getOperand(3));
8206     TmpInst.addOperand(Inst.getOperand(4));
8207     Inst = TmpInst;
8208     return true;
8209   }
8210   case ARM::t2ADDrr: {
8211     // If the destination and first source operand are the same, and
8212     // there's no setting of the flags, use encoding T2 instead of T3.
8213     // Note that this is only for ADD, not SUB. This mirrors the system
8214     // 'as' behaviour.  Also take advantage of ADD being commutative.
8215     // Make sure the wide encoding wasn't explicit.
8216     bool Swap = false;
8217     auto DestReg = Inst.getOperand(0).getReg();
8218     bool Transform = DestReg == Inst.getOperand(1).getReg();
8219     if (!Transform && DestReg == Inst.getOperand(2).getReg()) {
8220       Transform = true;
8221       Swap = true;
8222     }
8223     if (!Transform ||
8224         Inst.getOperand(5).getReg() != 0 ||
8225         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8226          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".w"))
8227       break;
8228     MCInst TmpInst;
8229     TmpInst.setOpcode(ARM::tADDhirr);
8230     TmpInst.addOperand(Inst.getOperand(0));
8231     TmpInst.addOperand(Inst.getOperand(0));
8232     TmpInst.addOperand(Inst.getOperand(Swap ? 1 : 2));
8233     TmpInst.addOperand(Inst.getOperand(3));
8234     TmpInst.addOperand(Inst.getOperand(4));
8235     Inst = TmpInst;
8236     return true;
8237   }
8238   case ARM::tADDrSP: {
8239     // If the non-SP source operand and the destination operand are not the
8240     // same, we need to use the 32-bit encoding if it's available.
8241     if (Inst.getOperand(0).getReg() != Inst.getOperand(2).getReg()) {
8242       Inst.setOpcode(ARM::t2ADDrr);
8243       Inst.addOperand(MCOperand::createReg(0)); // cc_out
8244       return true;
8245     }
8246     break;
8247   }
8248   case ARM::tB:
8249     // A Thumb conditional branch outside of an IT block is a tBcc.
8250     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()) {
8251       Inst.setOpcode(ARM::tBcc);
8252       return true;
8253     }
8254     break;
8255   case ARM::t2B:
8256     // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
8257     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()){
8258       Inst.setOpcode(ARM::t2Bcc);
8259       return true;
8260     }
8261     break;
8262   case ARM::t2Bcc:
8263     // If the conditional is AL or we're in an IT block, we really want t2B.
8264     if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock()) {
8265       Inst.setOpcode(ARM::t2B);
8266       return true;
8267     }
8268     break;
8269   case ARM::tBcc:
8270     // If the conditional is AL, we really want tB.
8271     if (Inst.getOperand(1).getImm() == ARMCC::AL) {
8272       Inst.setOpcode(ARM::tB);
8273       return true;
8274     }
8275     break;
8276   case ARM::tLDMIA: {
8277     // If the register list contains any high registers, or if the writeback
8278     // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
8279     // instead if we're in Thumb2. Otherwise, this should have generated
8280     // an error in validateInstruction().
8281     unsigned Rn = Inst.getOperand(0).getReg();
8282     bool hasWritebackToken =
8283         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8284          static_cast<ARMOperand &>(*Operands[3]).getToken() == "!");
8285     bool listContainsBase;
8286     if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
8287         (!listContainsBase && !hasWritebackToken) ||
8288         (listContainsBase && hasWritebackToken)) {
8289       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
8290       assert (isThumbTwo());
8291       Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
8292       // If we're switching to the updating version, we need to insert
8293       // the writeback tied operand.
8294       if (hasWritebackToken)
8295         Inst.insert(Inst.begin(),
8296                     MCOperand::createReg(Inst.getOperand(0).getReg()));
8297       return true;
8298     }
8299     break;
8300   }
8301   case ARM::tSTMIA_UPD: {
8302     // If the register list contains any high registers, we need to use
8303     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
8304     // should have generated an error in validateInstruction().
8305     unsigned Rn = Inst.getOperand(0).getReg();
8306     bool listContainsBase;
8307     if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
8308       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
8309       assert (isThumbTwo());
8310       Inst.setOpcode(ARM::t2STMIA_UPD);
8311       return true;
8312     }
8313     break;
8314   }
8315   case ARM::tPOP: {
8316     bool listContainsBase;
8317     // If the register list contains any high registers, we need to use
8318     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
8319     // should have generated an error in validateInstruction().
8320     if (!checkLowRegisterList(Inst, 2, 0, ARM::PC, listContainsBase))
8321       return false;
8322     assert (isThumbTwo());
8323     Inst.setOpcode(ARM::t2LDMIA_UPD);
8324     // Add the base register and writeback operands.
8325     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8326     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8327     return true;
8328   }
8329   case ARM::tPUSH: {
8330     bool listContainsBase;
8331     if (!checkLowRegisterList(Inst, 2, 0, ARM::LR, listContainsBase))
8332       return false;
8333     assert (isThumbTwo());
8334     Inst.setOpcode(ARM::t2STMDB_UPD);
8335     // Add the base register and writeback operands.
8336     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8337     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8338     return true;
8339   }
8340   case ARM::t2MOVi: {
8341     // If we can use the 16-bit encoding and the user didn't explicitly
8342     // request the 32-bit variant, transform it here.
8343     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8344         (unsigned)Inst.getOperand(1).getImm() <= 255 &&
8345         ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
8346           Inst.getOperand(4).getReg() == ARM::CPSR) ||
8347          (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
8348         (!static_cast<ARMOperand &>(*Operands[2]).isToken() ||
8349          static_cast<ARMOperand &>(*Operands[2]).getToken() != ".w")) {
8350       // The operands aren't in the same order for tMOVi8...
8351       MCInst TmpInst;
8352       TmpInst.setOpcode(ARM::tMOVi8);
8353       TmpInst.addOperand(Inst.getOperand(0));
8354       TmpInst.addOperand(Inst.getOperand(4));
8355       TmpInst.addOperand(Inst.getOperand(1));
8356       TmpInst.addOperand(Inst.getOperand(2));
8357       TmpInst.addOperand(Inst.getOperand(3));
8358       Inst = TmpInst;
8359       return true;
8360     }
8361     break;
8362   }
8363   case ARM::t2MOVr: {
8364     // If we can use the 16-bit encoding and the user didn't explicitly
8365     // request the 32-bit variant, transform it here.
8366     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8367         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8368         Inst.getOperand(2).getImm() == ARMCC::AL &&
8369         Inst.getOperand(4).getReg() == ARM::CPSR &&
8370         (!static_cast<ARMOperand &>(*Operands[2]).isToken() ||
8371          static_cast<ARMOperand &>(*Operands[2]).getToken() != ".w")) {
8372       // The operands aren't the same for tMOV[S]r... (no cc_out)
8373       MCInst TmpInst;
8374       TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
8375       TmpInst.addOperand(Inst.getOperand(0));
8376       TmpInst.addOperand(Inst.getOperand(1));
8377       TmpInst.addOperand(Inst.getOperand(2));
8378       TmpInst.addOperand(Inst.getOperand(3));
8379       Inst = TmpInst;
8380       return true;
8381     }
8382     break;
8383   }
8384   case ARM::t2SXTH:
8385   case ARM::t2SXTB:
8386   case ARM::t2UXTH:
8387   case ARM::t2UXTB: {
8388     // If we can use the 16-bit encoding and the user didn't explicitly
8389     // request the 32-bit variant, transform it here.
8390     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8391         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8392         Inst.getOperand(2).getImm() == 0 &&
8393         (!static_cast<ARMOperand &>(*Operands[2]).isToken() ||
8394          static_cast<ARMOperand &>(*Operands[2]).getToken() != ".w")) {
8395       unsigned NewOpc;
8396       switch (Inst.getOpcode()) {
8397       default: llvm_unreachable("Illegal opcode!");
8398       case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
8399       case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
8400       case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
8401       case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
8402       }
8403       // The operands aren't the same for thumb1 (no rotate operand).
8404       MCInst TmpInst;
8405       TmpInst.setOpcode(NewOpc);
8406       TmpInst.addOperand(Inst.getOperand(0));
8407       TmpInst.addOperand(Inst.getOperand(1));
8408       TmpInst.addOperand(Inst.getOperand(3));
8409       TmpInst.addOperand(Inst.getOperand(4));
8410       Inst = TmpInst;
8411       return true;
8412     }
8413     break;
8414   }
8415   case ARM::MOVsi: {
8416     ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(2).getImm());
8417     // rrx shifts and asr/lsr of #32 is encoded as 0
8418     if (SOpc == ARM_AM::rrx || SOpc == ARM_AM::asr || SOpc == ARM_AM::lsr)
8419       return false;
8420     if (ARM_AM::getSORegOffset(Inst.getOperand(2).getImm()) == 0) {
8421       // Shifting by zero is accepted as a vanilla 'MOVr'
8422       MCInst TmpInst;
8423       TmpInst.setOpcode(ARM::MOVr);
8424       TmpInst.addOperand(Inst.getOperand(0));
8425       TmpInst.addOperand(Inst.getOperand(1));
8426       TmpInst.addOperand(Inst.getOperand(3));
8427       TmpInst.addOperand(Inst.getOperand(4));
8428       TmpInst.addOperand(Inst.getOperand(5));
8429       Inst = TmpInst;
8430       return true;
8431     }
8432     return false;
8433   }
8434   case ARM::ANDrsi:
8435   case ARM::ORRrsi:
8436   case ARM::EORrsi:
8437   case ARM::BICrsi:
8438   case ARM::SUBrsi:
8439   case ARM::ADDrsi: {
8440     unsigned newOpc;
8441     ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(3).getImm());
8442     if (SOpc == ARM_AM::rrx) return false;
8443     switch (Inst.getOpcode()) {
8444     default: llvm_unreachable("unexpected opcode!");
8445     case ARM::ANDrsi: newOpc = ARM::ANDrr; break;
8446     case ARM::ORRrsi: newOpc = ARM::ORRrr; break;
8447     case ARM::EORrsi: newOpc = ARM::EORrr; break;
8448     case ARM::BICrsi: newOpc = ARM::BICrr; break;
8449     case ARM::SUBrsi: newOpc = ARM::SUBrr; break;
8450     case ARM::ADDrsi: newOpc = ARM::ADDrr; break;
8451     }
8452     // If the shift is by zero, use the non-shifted instruction definition.
8453     // The exception is for right shifts, where 0 == 32
8454     if (ARM_AM::getSORegOffset(Inst.getOperand(3).getImm()) == 0 &&
8455         !(SOpc == ARM_AM::lsr || SOpc == ARM_AM::asr)) {
8456       MCInst TmpInst;
8457       TmpInst.setOpcode(newOpc);
8458       TmpInst.addOperand(Inst.getOperand(0));
8459       TmpInst.addOperand(Inst.getOperand(1));
8460       TmpInst.addOperand(Inst.getOperand(2));
8461       TmpInst.addOperand(Inst.getOperand(4));
8462       TmpInst.addOperand(Inst.getOperand(5));
8463       TmpInst.addOperand(Inst.getOperand(6));
8464       Inst = TmpInst;
8465       return true;
8466     }
8467     return false;
8468   }
8469   case ARM::ITasm:
8470   case ARM::t2IT: {
8471     // The mask bits for all but the first condition are represented as
8472     // the low bit of the condition code value implies 't'. We currently
8473     // always have 1 implies 't', so XOR toggle the bits if the low bit
8474     // of the condition code is zero.
8475     MCOperand &MO = Inst.getOperand(1);
8476     unsigned Mask = MO.getImm();
8477     unsigned OrigMask = Mask;
8478     unsigned TZ = countTrailingZeros(Mask);
8479     if ((Inst.getOperand(0).getImm() & 1) == 0) {
8480       assert(Mask && TZ <= 3 && "illegal IT mask value!");
8481       Mask ^= (0xE << TZ) & 0xF;
8482     }
8483     MO.setImm(Mask);
8484 
8485     // Set up the IT block state according to the IT instruction we just
8486     // matched.
8487     assert(!inITBlock() && "nested IT blocks?!");
8488     ITState.Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
8489     ITState.Mask = OrigMask; // Use the original mask, not the updated one.
8490     ITState.CurPosition = 0;
8491     ITState.FirstCond = true;
8492     break;
8493   }
8494   case ARM::t2LSLrr:
8495   case ARM::t2LSRrr:
8496   case ARM::t2ASRrr:
8497   case ARM::t2SBCrr:
8498   case ARM::t2RORrr:
8499   case ARM::t2BICrr:
8500   {
8501     // Assemblers should use the narrow encodings of these instructions when permissible.
8502     if ((isARMLowRegister(Inst.getOperand(1).getReg()) &&
8503          isARMLowRegister(Inst.getOperand(2).getReg())) &&
8504         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
8505         ((!inITBlock() && Inst.getOperand(5).getReg() == ARM::CPSR) ||
8506          (inITBlock() && Inst.getOperand(5).getReg() != ARM::CPSR)) &&
8507         (!static_cast<ARMOperand &>(*Operands[3]).isToken() ||
8508          !static_cast<ARMOperand &>(*Operands[3]).getToken().equals_lower(
8509              ".w"))) {
8510       unsigned NewOpc;
8511       switch (Inst.getOpcode()) {
8512         default: llvm_unreachable("unexpected opcode");
8513         case ARM::t2LSLrr: NewOpc = ARM::tLSLrr; break;
8514         case ARM::t2LSRrr: NewOpc = ARM::tLSRrr; break;
8515         case ARM::t2ASRrr: NewOpc = ARM::tASRrr; break;
8516         case ARM::t2SBCrr: NewOpc = ARM::tSBC; break;
8517         case ARM::t2RORrr: NewOpc = ARM::tROR; break;
8518         case ARM::t2BICrr: NewOpc = ARM::tBIC; break;
8519       }
8520       MCInst TmpInst;
8521       TmpInst.setOpcode(NewOpc);
8522       TmpInst.addOperand(Inst.getOperand(0));
8523       TmpInst.addOperand(Inst.getOperand(5));
8524       TmpInst.addOperand(Inst.getOperand(1));
8525       TmpInst.addOperand(Inst.getOperand(2));
8526       TmpInst.addOperand(Inst.getOperand(3));
8527       TmpInst.addOperand(Inst.getOperand(4));
8528       Inst = TmpInst;
8529       return true;
8530     }
8531     return false;
8532   }
8533   case ARM::t2ANDrr:
8534   case ARM::t2EORrr:
8535   case ARM::t2ADCrr:
8536   case ARM::t2ORRrr:
8537   {
8538     // Assemblers should use the narrow encodings of these instructions when permissible.
8539     // These instructions are special in that they are commutable, so shorter encodings
8540     // are available more often.
8541     if ((isARMLowRegister(Inst.getOperand(1).getReg()) &&
8542          isARMLowRegister(Inst.getOperand(2).getReg())) &&
8543         (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() ||
8544          Inst.getOperand(0).getReg() == Inst.getOperand(2).getReg()) &&
8545         ((!inITBlock() && Inst.getOperand(5).getReg() == ARM::CPSR) ||
8546          (inITBlock() && Inst.getOperand(5).getReg() != ARM::CPSR)) &&
8547         (!static_cast<ARMOperand &>(*Operands[3]).isToken() ||
8548          !static_cast<ARMOperand &>(*Operands[3]).getToken().equals_lower(
8549              ".w"))) {
8550       unsigned NewOpc;
8551       switch (Inst.getOpcode()) {
8552         default: llvm_unreachable("unexpected opcode");
8553         case ARM::t2ADCrr: NewOpc = ARM::tADC; break;
8554         case ARM::t2ANDrr: NewOpc = ARM::tAND; break;
8555         case ARM::t2EORrr: NewOpc = ARM::tEOR; break;
8556         case ARM::t2ORRrr: NewOpc = ARM::tORR; break;
8557       }
8558       MCInst TmpInst;
8559       TmpInst.setOpcode(NewOpc);
8560       TmpInst.addOperand(Inst.getOperand(0));
8561       TmpInst.addOperand(Inst.getOperand(5));
8562       if (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()) {
8563         TmpInst.addOperand(Inst.getOperand(1));
8564         TmpInst.addOperand(Inst.getOperand(2));
8565       } else {
8566         TmpInst.addOperand(Inst.getOperand(2));
8567         TmpInst.addOperand(Inst.getOperand(1));
8568       }
8569       TmpInst.addOperand(Inst.getOperand(3));
8570       TmpInst.addOperand(Inst.getOperand(4));
8571       Inst = TmpInst;
8572       return true;
8573     }
8574     return false;
8575   }
8576   }
8577   return false;
8578 }
8579 
8580 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
8581   // 16-bit thumb arithmetic instructions either require or preclude the 'S'
8582   // suffix depending on whether they're in an IT block or not.
8583   unsigned Opc = Inst.getOpcode();
8584   const MCInstrDesc &MCID = MII.get(Opc);
8585   if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
8586     assert(MCID.hasOptionalDef() &&
8587            "optionally flag setting instruction missing optional def operand");
8588     assert(MCID.NumOperands == Inst.getNumOperands() &&
8589            "operand count mismatch!");
8590     // Find the optional-def operand (cc_out).
8591     unsigned OpNo;
8592     for (OpNo = 0;
8593          !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
8594          ++OpNo)
8595       ;
8596     // If we're parsing Thumb1, reject it completely.
8597     if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
8598       return Match_MnemonicFail;
8599     // If we're parsing Thumb2, which form is legal depends on whether we're
8600     // in an IT block.
8601     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
8602         !inITBlock())
8603       return Match_RequiresITBlock;
8604     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
8605         inITBlock())
8606       return Match_RequiresNotITBlock;
8607   } else if (isThumbOne()) {
8608     // Some high-register supporting Thumb1 encodings only allow both registers
8609     // to be from r0-r7 when in Thumb2.
8610     if (Opc == ARM::tADDhirr && !hasV6MOps() &&
8611         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8612         isARMLowRegister(Inst.getOperand(2).getReg()))
8613       return Match_RequiresThumb2;
8614     // Others only require ARMv6 or later.
8615     else if (Opc == ARM::tMOVr && !hasV6Ops() &&
8616              isARMLowRegister(Inst.getOperand(0).getReg()) &&
8617              isARMLowRegister(Inst.getOperand(1).getReg()))
8618       return Match_RequiresV6;
8619   }
8620 
8621   for (unsigned I = 0; I < MCID.NumOperands; ++I)
8622     if (MCID.OpInfo[I].RegClass == ARM::rGPRRegClassID) {
8623       // rGPRRegClass excludes PC, and also excluded SP before ARMv8
8624       if ((Inst.getOperand(I).getReg() == ARM::SP) && !hasV8Ops())
8625         return Match_RequiresV8;
8626       else if (Inst.getOperand(I).getReg() == ARM::PC)
8627         return Match_InvalidOperand;
8628     }
8629 
8630   return Match_Success;
8631 }
8632 
8633 namespace llvm {
8634 template <> inline bool IsCPSRDead<MCInst>(MCInst *Instr) {
8635   return true; // In an assembly source, no need to second-guess
8636 }
8637 }
8638 
8639 static const char *getSubtargetFeatureName(uint64_t Val);
8640 bool ARMAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
8641                                            OperandVector &Operands,
8642                                            MCStreamer &Out, uint64_t &ErrorInfo,
8643                                            bool MatchingInlineAsm) {
8644   MCInst Inst;
8645   unsigned MatchResult;
8646 
8647   MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
8648                                      MatchingInlineAsm);
8649   switch (MatchResult) {
8650   case Match_Success:
8651     // Context sensitive operand constraints aren't handled by the matcher,
8652     // so check them here.
8653     if (validateInstruction(Inst, Operands)) {
8654       // Still progress the IT block, otherwise one wrong condition causes
8655       // nasty cascading errors.
8656       forwardITPosition();
8657       return true;
8658     }
8659 
8660     { // processInstruction() updates inITBlock state, we need to save it away
8661       bool wasInITBlock = inITBlock();
8662 
8663       // Some instructions need post-processing to, for example, tweak which
8664       // encoding is selected. Loop on it while changes happen so the
8665       // individual transformations can chain off each other. E.g.,
8666       // tPOP(r8)->t2LDMIA_UPD(sp,r8)->t2STR_POST(sp,r8)
8667       while (processInstruction(Inst, Operands, Out))
8668         ;
8669 
8670       // Only after the instruction is fully processed, we can validate it
8671       if (wasInITBlock && hasV8Ops() && isThumb() &&
8672           !isV8EligibleForIT(&Inst)) {
8673         Warning(IDLoc, "deprecated instruction in IT block");
8674       }
8675     }
8676 
8677     // Only move forward at the very end so that everything in validate
8678     // and process gets a consistent answer about whether we're in an IT
8679     // block.
8680     forwardITPosition();
8681 
8682     // ITasm is an ARM mode pseudo-instruction that just sets the ITblock and
8683     // doesn't actually encode.
8684     if (Inst.getOpcode() == ARM::ITasm)
8685       return false;
8686 
8687     Inst.setLoc(IDLoc);
8688     Out.EmitInstruction(Inst, getSTI());
8689     return false;
8690   case Match_MissingFeature: {
8691     assert(ErrorInfo && "Unknown missing feature!");
8692     // Special case the error message for the very common case where only
8693     // a single subtarget feature is missing (Thumb vs. ARM, e.g.).
8694     std::string Msg = "instruction requires:";
8695     uint64_t Mask = 1;
8696     for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
8697       if (ErrorInfo & Mask) {
8698         Msg += " ";
8699         Msg += getSubtargetFeatureName(ErrorInfo & Mask);
8700       }
8701       Mask <<= 1;
8702     }
8703     return Error(IDLoc, Msg);
8704   }
8705   case Match_InvalidOperand: {
8706     SMLoc ErrorLoc = IDLoc;
8707     if (ErrorInfo != ~0ULL) {
8708       if (ErrorInfo >= Operands.size())
8709         return Error(IDLoc, "too few operands for instruction");
8710 
8711       ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
8712       if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
8713     }
8714 
8715     return Error(ErrorLoc, "invalid operand for instruction");
8716   }
8717   case Match_MnemonicFail:
8718     return Error(IDLoc, "invalid instruction",
8719                  ((ARMOperand &)*Operands[0]).getLocRange());
8720   case Match_RequiresNotITBlock:
8721     return Error(IDLoc, "flag setting instruction only valid outside IT block");
8722   case Match_RequiresITBlock:
8723     return Error(IDLoc, "instruction only valid inside IT block");
8724   case Match_RequiresV6:
8725     return Error(IDLoc, "instruction variant requires ARMv6 or later");
8726   case Match_RequiresThumb2:
8727     return Error(IDLoc, "instruction variant requires Thumb2");
8728   case Match_RequiresV8:
8729     return Error(IDLoc, "instruction variant requires ARMv8 or later");
8730   case Match_ImmRange0_15: {
8731     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
8732     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
8733     return Error(ErrorLoc, "immediate operand must be in the range [0,15]");
8734   }
8735   case Match_ImmRange0_239: {
8736     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
8737     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
8738     return Error(ErrorLoc, "immediate operand must be in the range [0,239]");
8739   }
8740   case Match_AlignedMemoryRequiresNone:
8741   case Match_DupAlignedMemoryRequiresNone:
8742   case Match_AlignedMemoryRequires16:
8743   case Match_DupAlignedMemoryRequires16:
8744   case Match_AlignedMemoryRequires32:
8745   case Match_DupAlignedMemoryRequires32:
8746   case Match_AlignedMemoryRequires64:
8747   case Match_DupAlignedMemoryRequires64:
8748   case Match_AlignedMemoryRequires64or128:
8749   case Match_DupAlignedMemoryRequires64or128:
8750   case Match_AlignedMemoryRequires64or128or256:
8751   {
8752     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getAlignmentLoc();
8753     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
8754     switch (MatchResult) {
8755       default:
8756         llvm_unreachable("Missing Match_Aligned type");
8757       case Match_AlignedMemoryRequiresNone:
8758       case Match_DupAlignedMemoryRequiresNone:
8759         return Error(ErrorLoc, "alignment must be omitted");
8760       case Match_AlignedMemoryRequires16:
8761       case Match_DupAlignedMemoryRequires16:
8762         return Error(ErrorLoc, "alignment must be 16 or omitted");
8763       case Match_AlignedMemoryRequires32:
8764       case Match_DupAlignedMemoryRequires32:
8765         return Error(ErrorLoc, "alignment must be 32 or omitted");
8766       case Match_AlignedMemoryRequires64:
8767       case Match_DupAlignedMemoryRequires64:
8768         return Error(ErrorLoc, "alignment must be 64 or omitted");
8769       case Match_AlignedMemoryRequires64or128:
8770       case Match_DupAlignedMemoryRequires64or128:
8771         return Error(ErrorLoc, "alignment must be 64, 128 or omitted");
8772       case Match_AlignedMemoryRequires64or128or256:
8773         return Error(ErrorLoc, "alignment must be 64, 128, 256 or omitted");
8774     }
8775   }
8776   }
8777 
8778   llvm_unreachable("Implement any new match types added!");
8779 }
8780 
8781 /// parseDirective parses the arm specific directives
8782 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
8783   const MCObjectFileInfo::Environment Format =
8784     getContext().getObjectFileInfo()->getObjectFileType();
8785   bool IsMachO = Format == MCObjectFileInfo::IsMachO;
8786   bool IsCOFF = Format == MCObjectFileInfo::IsCOFF;
8787 
8788   StringRef IDVal = DirectiveID.getIdentifier();
8789   if (IDVal == ".word")
8790     return parseLiteralValues(4, DirectiveID.getLoc());
8791   else if (IDVal == ".short" || IDVal == ".hword")
8792     return parseLiteralValues(2, DirectiveID.getLoc());
8793   else if (IDVal == ".thumb")
8794     return parseDirectiveThumb(DirectiveID.getLoc());
8795   else if (IDVal == ".arm")
8796     return parseDirectiveARM(DirectiveID.getLoc());
8797   else if (IDVal == ".thumb_func")
8798     return parseDirectiveThumbFunc(DirectiveID.getLoc());
8799   else if (IDVal == ".code")
8800     return parseDirectiveCode(DirectiveID.getLoc());
8801   else if (IDVal == ".syntax")
8802     return parseDirectiveSyntax(DirectiveID.getLoc());
8803   else if (IDVal == ".unreq")
8804     return parseDirectiveUnreq(DirectiveID.getLoc());
8805   else if (IDVal == ".fnend")
8806     return parseDirectiveFnEnd(DirectiveID.getLoc());
8807   else if (IDVal == ".cantunwind")
8808     return parseDirectiveCantUnwind(DirectiveID.getLoc());
8809   else if (IDVal == ".personality")
8810     return parseDirectivePersonality(DirectiveID.getLoc());
8811   else if (IDVal == ".handlerdata")
8812     return parseDirectiveHandlerData(DirectiveID.getLoc());
8813   else if (IDVal == ".setfp")
8814     return parseDirectiveSetFP(DirectiveID.getLoc());
8815   else if (IDVal == ".pad")
8816     return parseDirectivePad(DirectiveID.getLoc());
8817   else if (IDVal == ".save")
8818     return parseDirectiveRegSave(DirectiveID.getLoc(), false);
8819   else if (IDVal == ".vsave")
8820     return parseDirectiveRegSave(DirectiveID.getLoc(), true);
8821   else if (IDVal == ".ltorg" || IDVal == ".pool")
8822     return parseDirectiveLtorg(DirectiveID.getLoc());
8823   else if (IDVal == ".even")
8824     return parseDirectiveEven(DirectiveID.getLoc());
8825   else if (IDVal == ".personalityindex")
8826     return parseDirectivePersonalityIndex(DirectiveID.getLoc());
8827   else if (IDVal == ".unwind_raw")
8828     return parseDirectiveUnwindRaw(DirectiveID.getLoc());
8829   else if (IDVal == ".movsp")
8830     return parseDirectiveMovSP(DirectiveID.getLoc());
8831   else if (IDVal == ".arch_extension")
8832     return parseDirectiveArchExtension(DirectiveID.getLoc());
8833   else if (IDVal == ".align")
8834     return parseDirectiveAlign(DirectiveID.getLoc());
8835   else if (IDVal == ".thumb_set")
8836     return parseDirectiveThumbSet(DirectiveID.getLoc());
8837 
8838   if (!IsMachO && !IsCOFF) {
8839     if (IDVal == ".arch")
8840       return parseDirectiveArch(DirectiveID.getLoc());
8841     else if (IDVal == ".cpu")
8842       return parseDirectiveCPU(DirectiveID.getLoc());
8843     else if (IDVal == ".eabi_attribute")
8844       return parseDirectiveEabiAttr(DirectiveID.getLoc());
8845     else if (IDVal == ".fpu")
8846       return parseDirectiveFPU(DirectiveID.getLoc());
8847     else if (IDVal == ".fnstart")
8848       return parseDirectiveFnStart(DirectiveID.getLoc());
8849     else if (IDVal == ".inst")
8850       return parseDirectiveInst(DirectiveID.getLoc());
8851     else if (IDVal == ".inst.n")
8852       return parseDirectiveInst(DirectiveID.getLoc(), 'n');
8853     else if (IDVal == ".inst.w")
8854       return parseDirectiveInst(DirectiveID.getLoc(), 'w');
8855     else if (IDVal == ".object_arch")
8856       return parseDirectiveObjectArch(DirectiveID.getLoc());
8857     else if (IDVal == ".tlsdescseq")
8858       return parseDirectiveTLSDescSeq(DirectiveID.getLoc());
8859   }
8860 
8861   return true;
8862 }
8863 
8864 /// parseLiteralValues
8865 ///  ::= .hword expression [, expression]*
8866 ///  ::= .short expression [, expression]*
8867 ///  ::= .word expression [, expression]*
8868 bool ARMAsmParser::parseLiteralValues(unsigned Size, SMLoc L) {
8869   MCAsmParser &Parser = getParser();
8870   if (getLexer().isNot(AsmToken::EndOfStatement)) {
8871     for (;;) {
8872       const MCExpr *Value;
8873       if (getParser().parseExpression(Value)) {
8874         Parser.eatToEndOfStatement();
8875         return false;
8876       }
8877 
8878       getParser().getStreamer().EmitValue(Value, Size, L);
8879 
8880       if (getLexer().is(AsmToken::EndOfStatement))
8881         break;
8882 
8883       // FIXME: Improve diagnostic.
8884       if (getLexer().isNot(AsmToken::Comma)) {
8885         Error(L, "unexpected token in directive");
8886         return false;
8887       }
8888       Parser.Lex();
8889     }
8890   }
8891 
8892   Parser.Lex();
8893   return false;
8894 }
8895 
8896 /// parseDirectiveThumb
8897 ///  ::= .thumb
8898 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
8899   MCAsmParser &Parser = getParser();
8900   if (getLexer().isNot(AsmToken::EndOfStatement)) {
8901     Error(L, "unexpected token in directive");
8902     return false;
8903   }
8904   Parser.Lex();
8905 
8906   if (!hasThumb()) {
8907     Error(L, "target does not support Thumb mode");
8908     return false;
8909   }
8910 
8911   if (!isThumb())
8912     SwitchMode();
8913 
8914   getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
8915   return false;
8916 }
8917 
8918 /// parseDirectiveARM
8919 ///  ::= .arm
8920 bool ARMAsmParser::parseDirectiveARM(SMLoc L) {
8921   MCAsmParser &Parser = getParser();
8922   if (getLexer().isNot(AsmToken::EndOfStatement)) {
8923     Error(L, "unexpected token in directive");
8924     return false;
8925   }
8926   Parser.Lex();
8927 
8928   if (!hasARM()) {
8929     Error(L, "target does not support ARM mode");
8930     return false;
8931   }
8932 
8933   if (isThumb())
8934     SwitchMode();
8935 
8936   getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
8937   return false;
8938 }
8939 
8940 void ARMAsmParser::onLabelParsed(MCSymbol *Symbol) {
8941   if (NextSymbolIsThumb) {
8942     getParser().getStreamer().EmitThumbFunc(Symbol);
8943     NextSymbolIsThumb = false;
8944   }
8945 }
8946 
8947 /// parseDirectiveThumbFunc
8948 ///  ::= .thumbfunc symbol_name
8949 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
8950   MCAsmParser &Parser = getParser();
8951   const auto Format = getContext().getObjectFileInfo()->getObjectFileType();
8952   bool IsMachO = Format == MCObjectFileInfo::IsMachO;
8953 
8954   // Darwin asm has (optionally) function name after .thumb_func direction
8955   // ELF doesn't
8956   if (IsMachO) {
8957     const AsmToken &Tok = Parser.getTok();
8958     if (Tok.isNot(AsmToken::EndOfStatement)) {
8959       if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String)) {
8960         Error(L, "unexpected token in .thumb_func directive");
8961         return false;
8962       }
8963 
8964       MCSymbol *Func =
8965           getParser().getContext().getOrCreateSymbol(Tok.getIdentifier());
8966       getParser().getStreamer().EmitThumbFunc(Func);
8967       Parser.Lex(); // Consume the identifier token.
8968       return false;
8969     }
8970   }
8971 
8972   if (getLexer().isNot(AsmToken::EndOfStatement)) {
8973     Error(Parser.getTok().getLoc(), "unexpected token in directive");
8974     Parser.eatToEndOfStatement();
8975     return false;
8976   }
8977 
8978   NextSymbolIsThumb = true;
8979   return false;
8980 }
8981 
8982 /// parseDirectiveSyntax
8983 ///  ::= .syntax unified | divided
8984 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
8985   MCAsmParser &Parser = getParser();
8986   const AsmToken &Tok = Parser.getTok();
8987   if (Tok.isNot(AsmToken::Identifier)) {
8988     Error(L, "unexpected token in .syntax directive");
8989     return false;
8990   }
8991 
8992   StringRef Mode = Tok.getString();
8993   if (Mode == "unified" || Mode == "UNIFIED") {
8994     Parser.Lex();
8995   } else if (Mode == "divided" || Mode == "DIVIDED") {
8996     Error(L, "'.syntax divided' arm asssembly not supported");
8997     return false;
8998   } else {
8999     Error(L, "unrecognized syntax mode in .syntax directive");
9000     return false;
9001   }
9002 
9003   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9004     Error(Parser.getTok().getLoc(), "unexpected token in directive");
9005     return false;
9006   }
9007   Parser.Lex();
9008 
9009   // TODO tell the MC streamer the mode
9010   // getParser().getStreamer().Emit???();
9011   return false;
9012 }
9013 
9014 /// parseDirectiveCode
9015 ///  ::= .code 16 | 32
9016 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
9017   MCAsmParser &Parser = getParser();
9018   const AsmToken &Tok = Parser.getTok();
9019   if (Tok.isNot(AsmToken::Integer)) {
9020     Error(L, "unexpected token in .code directive");
9021     return false;
9022   }
9023   int64_t Val = Parser.getTok().getIntVal();
9024   if (Val != 16 && Val != 32) {
9025     Error(L, "invalid operand to .code directive");
9026     return false;
9027   }
9028   Parser.Lex();
9029 
9030   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9031     Error(Parser.getTok().getLoc(), "unexpected token in directive");
9032     return false;
9033   }
9034   Parser.Lex();
9035 
9036   if (Val == 16) {
9037     if (!hasThumb()) {
9038       Error(L, "target does not support Thumb mode");
9039       return false;
9040     }
9041 
9042     if (!isThumb())
9043       SwitchMode();
9044     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
9045   } else {
9046     if (!hasARM()) {
9047       Error(L, "target does not support ARM mode");
9048       return false;
9049     }
9050 
9051     if (isThumb())
9052       SwitchMode();
9053     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
9054   }
9055 
9056   return false;
9057 }
9058 
9059 /// parseDirectiveReq
9060 ///  ::= name .req registername
9061 bool ARMAsmParser::parseDirectiveReq(StringRef Name, SMLoc L) {
9062   MCAsmParser &Parser = getParser();
9063   Parser.Lex(); // Eat the '.req' token.
9064   unsigned Reg;
9065   SMLoc SRegLoc, ERegLoc;
9066   if (ParseRegister(Reg, SRegLoc, ERegLoc)) {
9067     Parser.eatToEndOfStatement();
9068     Error(SRegLoc, "register name expected");
9069     return false;
9070   }
9071 
9072   // Shouldn't be anything else.
9073   if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
9074     Parser.eatToEndOfStatement();
9075     Error(Parser.getTok().getLoc(), "unexpected input in .req directive.");
9076     return false;
9077   }
9078 
9079   Parser.Lex(); // Consume the EndOfStatement
9080 
9081   if (RegisterReqs.insert(std::make_pair(Name, Reg)).first->second != Reg) {
9082     Error(SRegLoc, "redefinition of '" + Name + "' does not match original.");
9083     return false;
9084   }
9085 
9086   return false;
9087 }
9088 
9089 /// parseDirectiveUneq
9090 ///  ::= .unreq registername
9091 bool ARMAsmParser::parseDirectiveUnreq(SMLoc L) {
9092   MCAsmParser &Parser = getParser();
9093   if (Parser.getTok().isNot(AsmToken::Identifier)) {
9094     Parser.eatToEndOfStatement();
9095     Error(L, "unexpected input in .unreq directive.");
9096     return false;
9097   }
9098   RegisterReqs.erase(Parser.getTok().getIdentifier().lower());
9099   Parser.Lex(); // Eat the identifier.
9100   return false;
9101 }
9102 
9103 // After changing arch/CPU, try to put the ARM/Thumb mode back to what it was
9104 // before, if supported by the new target, or emit mapping symbols for the mode
9105 // switch.
9106 void ARMAsmParser::FixModeAfterArchChange(bool WasThumb, SMLoc Loc) {
9107   if (WasThumb != isThumb()) {
9108     if (WasThumb && hasThumb()) {
9109       // Stay in Thumb mode
9110       SwitchMode();
9111     } else if (!WasThumb && hasARM()) {
9112       // Stay in ARM mode
9113       SwitchMode();
9114     } else {
9115       // Mode switch forced, because the new arch doesn't support the old mode.
9116       getParser().getStreamer().EmitAssemblerFlag(isThumb() ? MCAF_Code16
9117                                                             : MCAF_Code32);
9118       // Warn about the implcit mode switch. GAS does not switch modes here,
9119       // but instead stays in the old mode, reporting an error on any following
9120       // instructions as the mode does not exist on the target.
9121       Warning(Loc, Twine("new target does not support ") +
9122                        (WasThumb ? "thumb" : "arm") + " mode, switching to " +
9123                        (!WasThumb ? "thumb" : "arm") + " mode");
9124     }
9125   }
9126 }
9127 
9128 /// parseDirectiveArch
9129 ///  ::= .arch token
9130 bool ARMAsmParser::parseDirectiveArch(SMLoc L) {
9131   StringRef Arch = getParser().parseStringToEndOfStatement().trim();
9132 
9133   unsigned ID = ARM::parseArch(Arch);
9134 
9135   if (ID == ARM::AK_INVALID) {
9136     Error(L, "Unknown arch name");
9137     return false;
9138   }
9139 
9140   bool WasThumb = isThumb();
9141   Triple T;
9142   MCSubtargetInfo &STI = copySTI();
9143   STI.setDefaultFeatures("", ("+" + ARM::getArchName(ID)).str());
9144   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9145   FixModeAfterArchChange(WasThumb, L);
9146 
9147   getTargetStreamer().emitArch(ID);
9148   return false;
9149 }
9150 
9151 /// parseDirectiveEabiAttr
9152 ///  ::= .eabi_attribute int, int [, "str"]
9153 ///  ::= .eabi_attribute Tag_name, int [, "str"]
9154 bool ARMAsmParser::parseDirectiveEabiAttr(SMLoc L) {
9155   MCAsmParser &Parser = getParser();
9156   int64_t Tag;
9157   SMLoc TagLoc;
9158   TagLoc = Parser.getTok().getLoc();
9159   if (Parser.getTok().is(AsmToken::Identifier)) {
9160     StringRef Name = Parser.getTok().getIdentifier();
9161     Tag = ARMBuildAttrs::AttrTypeFromString(Name);
9162     if (Tag == -1) {
9163       Error(TagLoc, "attribute name not recognised: " + Name);
9164       Parser.eatToEndOfStatement();
9165       return false;
9166     }
9167     Parser.Lex();
9168   } else {
9169     const MCExpr *AttrExpr;
9170 
9171     TagLoc = Parser.getTok().getLoc();
9172     if (Parser.parseExpression(AttrExpr)) {
9173       Parser.eatToEndOfStatement();
9174       return false;
9175     }
9176 
9177     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(AttrExpr);
9178     if (!CE) {
9179       Error(TagLoc, "expected numeric constant");
9180       Parser.eatToEndOfStatement();
9181       return false;
9182     }
9183 
9184     Tag = CE->getValue();
9185   }
9186 
9187   if (Parser.getTok().isNot(AsmToken::Comma)) {
9188     Error(Parser.getTok().getLoc(), "comma expected");
9189     Parser.eatToEndOfStatement();
9190     return false;
9191   }
9192   Parser.Lex(); // skip comma
9193 
9194   StringRef StringValue = "";
9195   bool IsStringValue = false;
9196 
9197   int64_t IntegerValue = 0;
9198   bool IsIntegerValue = false;
9199 
9200   if (Tag == ARMBuildAttrs::CPU_raw_name || Tag == ARMBuildAttrs::CPU_name)
9201     IsStringValue = true;
9202   else if (Tag == ARMBuildAttrs::compatibility) {
9203     IsStringValue = true;
9204     IsIntegerValue = true;
9205   } else if (Tag < 32 || Tag % 2 == 0)
9206     IsIntegerValue = true;
9207   else if (Tag % 2 == 1)
9208     IsStringValue = true;
9209   else
9210     llvm_unreachable("invalid tag type");
9211 
9212   if (IsIntegerValue) {
9213     const MCExpr *ValueExpr;
9214     SMLoc ValueExprLoc = Parser.getTok().getLoc();
9215     if (Parser.parseExpression(ValueExpr)) {
9216       Parser.eatToEndOfStatement();
9217       return false;
9218     }
9219 
9220     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ValueExpr);
9221     if (!CE) {
9222       Error(ValueExprLoc, "expected numeric constant");
9223       Parser.eatToEndOfStatement();
9224       return false;
9225     }
9226 
9227     IntegerValue = CE->getValue();
9228   }
9229 
9230   if (Tag == ARMBuildAttrs::compatibility) {
9231     if (Parser.getTok().isNot(AsmToken::Comma))
9232       IsStringValue = false;
9233     if (Parser.getTok().isNot(AsmToken::Comma)) {
9234       Error(Parser.getTok().getLoc(), "comma expected");
9235       Parser.eatToEndOfStatement();
9236       return false;
9237     } else {
9238        Parser.Lex();
9239     }
9240   }
9241 
9242   if (IsStringValue) {
9243     if (Parser.getTok().isNot(AsmToken::String)) {
9244       Error(Parser.getTok().getLoc(), "bad string constant");
9245       Parser.eatToEndOfStatement();
9246       return false;
9247     }
9248 
9249     StringValue = Parser.getTok().getStringContents();
9250     Parser.Lex();
9251   }
9252 
9253   if (IsIntegerValue && IsStringValue) {
9254     assert(Tag == ARMBuildAttrs::compatibility);
9255     getTargetStreamer().emitIntTextAttribute(Tag, IntegerValue, StringValue);
9256   } else if (IsIntegerValue)
9257     getTargetStreamer().emitAttribute(Tag, IntegerValue);
9258   else if (IsStringValue)
9259     getTargetStreamer().emitTextAttribute(Tag, StringValue);
9260   return false;
9261 }
9262 
9263 /// parseDirectiveCPU
9264 ///  ::= .cpu str
9265 bool ARMAsmParser::parseDirectiveCPU(SMLoc L) {
9266   StringRef CPU = getParser().parseStringToEndOfStatement().trim();
9267   getTargetStreamer().emitTextAttribute(ARMBuildAttrs::CPU_name, CPU);
9268 
9269   // FIXME: This is using table-gen data, but should be moved to
9270   // ARMTargetParser once that is table-gen'd.
9271   if (!getSTI().isCPUStringValid(CPU)) {
9272     Error(L, "Unknown CPU name");
9273     return false;
9274   }
9275 
9276   bool WasThumb = isThumb();
9277   MCSubtargetInfo &STI = copySTI();
9278   STI.setDefaultFeatures(CPU, "");
9279   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9280   FixModeAfterArchChange(WasThumb, L);
9281 
9282   return false;
9283 }
9284 /// parseDirectiveFPU
9285 ///  ::= .fpu str
9286 bool ARMAsmParser::parseDirectiveFPU(SMLoc L) {
9287   SMLoc FPUNameLoc = getTok().getLoc();
9288   StringRef FPU = getParser().parseStringToEndOfStatement().trim();
9289 
9290   unsigned ID = ARM::parseFPU(FPU);
9291   std::vector<const char *> Features;
9292   if (!ARM::getFPUFeatures(ID, Features)) {
9293     Error(FPUNameLoc, "Unknown FPU name");
9294     return false;
9295   }
9296 
9297   MCSubtargetInfo &STI = copySTI();
9298   for (auto Feature : Features)
9299     STI.ApplyFeatureFlag(Feature);
9300   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9301 
9302   getTargetStreamer().emitFPU(ID);
9303   return false;
9304 }
9305 
9306 /// parseDirectiveFnStart
9307 ///  ::= .fnstart
9308 bool ARMAsmParser::parseDirectiveFnStart(SMLoc L) {
9309   if (UC.hasFnStart()) {
9310     Error(L, ".fnstart starts before the end of previous one");
9311     UC.emitFnStartLocNotes();
9312     return false;
9313   }
9314 
9315   // Reset the unwind directives parser state
9316   UC.reset();
9317 
9318   getTargetStreamer().emitFnStart();
9319 
9320   UC.recordFnStart(L);
9321   return false;
9322 }
9323 
9324 /// parseDirectiveFnEnd
9325 ///  ::= .fnend
9326 bool ARMAsmParser::parseDirectiveFnEnd(SMLoc L) {
9327   // Check the ordering of unwind directives
9328   if (!UC.hasFnStart()) {
9329     Error(L, ".fnstart must precede .fnend directive");
9330     return false;
9331   }
9332 
9333   // Reset the unwind directives parser state
9334   getTargetStreamer().emitFnEnd();
9335 
9336   UC.reset();
9337   return false;
9338 }
9339 
9340 /// parseDirectiveCantUnwind
9341 ///  ::= .cantunwind
9342 bool ARMAsmParser::parseDirectiveCantUnwind(SMLoc L) {
9343   UC.recordCantUnwind(L);
9344 
9345   // Check the ordering of unwind directives
9346   if (!UC.hasFnStart()) {
9347     Error(L, ".fnstart must precede .cantunwind directive");
9348     return false;
9349   }
9350   if (UC.hasHandlerData()) {
9351     Error(L, ".cantunwind can't be used with .handlerdata directive");
9352     UC.emitHandlerDataLocNotes();
9353     return false;
9354   }
9355   if (UC.hasPersonality()) {
9356     Error(L, ".cantunwind can't be used with .personality directive");
9357     UC.emitPersonalityLocNotes();
9358     return false;
9359   }
9360 
9361   getTargetStreamer().emitCantUnwind();
9362   return false;
9363 }
9364 
9365 /// parseDirectivePersonality
9366 ///  ::= .personality name
9367 bool ARMAsmParser::parseDirectivePersonality(SMLoc L) {
9368   MCAsmParser &Parser = getParser();
9369   bool HasExistingPersonality = UC.hasPersonality();
9370 
9371   UC.recordPersonality(L);
9372 
9373   // Check the ordering of unwind directives
9374   if (!UC.hasFnStart()) {
9375     Error(L, ".fnstart must precede .personality directive");
9376     return false;
9377   }
9378   if (UC.cantUnwind()) {
9379     Error(L, ".personality can't be used with .cantunwind directive");
9380     UC.emitCantUnwindLocNotes();
9381     return false;
9382   }
9383   if (UC.hasHandlerData()) {
9384     Error(L, ".personality must precede .handlerdata directive");
9385     UC.emitHandlerDataLocNotes();
9386     return false;
9387   }
9388   if (HasExistingPersonality) {
9389     Parser.eatToEndOfStatement();
9390     Error(L, "multiple personality directives");
9391     UC.emitPersonalityLocNotes();
9392     return false;
9393   }
9394 
9395   // Parse the name of the personality routine
9396   if (Parser.getTok().isNot(AsmToken::Identifier)) {
9397     Parser.eatToEndOfStatement();
9398     Error(L, "unexpected input in .personality directive.");
9399     return false;
9400   }
9401   StringRef Name(Parser.getTok().getIdentifier());
9402   Parser.Lex();
9403 
9404   MCSymbol *PR = getParser().getContext().getOrCreateSymbol(Name);
9405   getTargetStreamer().emitPersonality(PR);
9406   return false;
9407 }
9408 
9409 /// parseDirectiveHandlerData
9410 ///  ::= .handlerdata
9411 bool ARMAsmParser::parseDirectiveHandlerData(SMLoc L) {
9412   UC.recordHandlerData(L);
9413 
9414   // Check the ordering of unwind directives
9415   if (!UC.hasFnStart()) {
9416     Error(L, ".fnstart must precede .personality directive");
9417     return false;
9418   }
9419   if (UC.cantUnwind()) {
9420     Error(L, ".handlerdata can't be used with .cantunwind directive");
9421     UC.emitCantUnwindLocNotes();
9422     return false;
9423   }
9424 
9425   getTargetStreamer().emitHandlerData();
9426   return false;
9427 }
9428 
9429 /// parseDirectiveSetFP
9430 ///  ::= .setfp fpreg, spreg [, offset]
9431 bool ARMAsmParser::parseDirectiveSetFP(SMLoc L) {
9432   MCAsmParser &Parser = getParser();
9433   // Check the ordering of unwind directives
9434   if (!UC.hasFnStart()) {
9435     Error(L, ".fnstart must precede .setfp directive");
9436     return false;
9437   }
9438   if (UC.hasHandlerData()) {
9439     Error(L, ".setfp must precede .handlerdata directive");
9440     return false;
9441   }
9442 
9443   // Parse fpreg
9444   SMLoc FPRegLoc = Parser.getTok().getLoc();
9445   int FPReg = tryParseRegister();
9446   if (FPReg == -1) {
9447     Error(FPRegLoc, "frame pointer register expected");
9448     return false;
9449   }
9450 
9451   // Consume comma
9452   if (Parser.getTok().isNot(AsmToken::Comma)) {
9453     Error(Parser.getTok().getLoc(), "comma expected");
9454     return false;
9455   }
9456   Parser.Lex(); // skip comma
9457 
9458   // Parse spreg
9459   SMLoc SPRegLoc = Parser.getTok().getLoc();
9460   int SPReg = tryParseRegister();
9461   if (SPReg == -1) {
9462     Error(SPRegLoc, "stack pointer register expected");
9463     return false;
9464   }
9465 
9466   if (SPReg != ARM::SP && SPReg != UC.getFPReg()) {
9467     Error(SPRegLoc, "register should be either $sp or the latest fp register");
9468     return false;
9469   }
9470 
9471   // Update the frame pointer register
9472   UC.saveFPReg(FPReg);
9473 
9474   // Parse offset
9475   int64_t Offset = 0;
9476   if (Parser.getTok().is(AsmToken::Comma)) {
9477     Parser.Lex(); // skip comma
9478 
9479     if (Parser.getTok().isNot(AsmToken::Hash) &&
9480         Parser.getTok().isNot(AsmToken::Dollar)) {
9481       Error(Parser.getTok().getLoc(), "'#' expected");
9482       return false;
9483     }
9484     Parser.Lex(); // skip hash token.
9485 
9486     const MCExpr *OffsetExpr;
9487     SMLoc ExLoc = Parser.getTok().getLoc();
9488     SMLoc EndLoc;
9489     if (getParser().parseExpression(OffsetExpr, EndLoc)) {
9490       Error(ExLoc, "malformed setfp offset");
9491       return false;
9492     }
9493     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9494     if (!CE) {
9495       Error(ExLoc, "setfp offset must be an immediate");
9496       return false;
9497     }
9498 
9499     Offset = CE->getValue();
9500   }
9501 
9502   getTargetStreamer().emitSetFP(static_cast<unsigned>(FPReg),
9503                                 static_cast<unsigned>(SPReg), Offset);
9504   return false;
9505 }
9506 
9507 /// parseDirective
9508 ///  ::= .pad offset
9509 bool ARMAsmParser::parseDirectivePad(SMLoc L) {
9510   MCAsmParser &Parser = getParser();
9511   // Check the ordering of unwind directives
9512   if (!UC.hasFnStart()) {
9513     Error(L, ".fnstart must precede .pad directive");
9514     return false;
9515   }
9516   if (UC.hasHandlerData()) {
9517     Error(L, ".pad must precede .handlerdata directive");
9518     return false;
9519   }
9520 
9521   // Parse the offset
9522   if (Parser.getTok().isNot(AsmToken::Hash) &&
9523       Parser.getTok().isNot(AsmToken::Dollar)) {
9524     Error(Parser.getTok().getLoc(), "'#' expected");
9525     return false;
9526   }
9527   Parser.Lex(); // skip hash token.
9528 
9529   const MCExpr *OffsetExpr;
9530   SMLoc ExLoc = Parser.getTok().getLoc();
9531   SMLoc EndLoc;
9532   if (getParser().parseExpression(OffsetExpr, EndLoc)) {
9533     Error(ExLoc, "malformed pad offset");
9534     return false;
9535   }
9536   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9537   if (!CE) {
9538     Error(ExLoc, "pad offset must be an immediate");
9539     return false;
9540   }
9541 
9542   getTargetStreamer().emitPad(CE->getValue());
9543   return false;
9544 }
9545 
9546 /// parseDirectiveRegSave
9547 ///  ::= .save  { registers }
9548 ///  ::= .vsave { registers }
9549 bool ARMAsmParser::parseDirectiveRegSave(SMLoc L, bool IsVector) {
9550   // Check the ordering of unwind directives
9551   if (!UC.hasFnStart()) {
9552     Error(L, ".fnstart must precede .save or .vsave directives");
9553     return false;
9554   }
9555   if (UC.hasHandlerData()) {
9556     Error(L, ".save or .vsave must precede .handlerdata directive");
9557     return false;
9558   }
9559 
9560   // RAII object to make sure parsed operands are deleted.
9561   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Operands;
9562 
9563   // Parse the register list
9564   if (parseRegisterList(Operands))
9565     return false;
9566   ARMOperand &Op = (ARMOperand &)*Operands[0];
9567   if (!IsVector && !Op.isRegList()) {
9568     Error(L, ".save expects GPR registers");
9569     return false;
9570   }
9571   if (IsVector && !Op.isDPRRegList()) {
9572     Error(L, ".vsave expects DPR registers");
9573     return false;
9574   }
9575 
9576   getTargetStreamer().emitRegSave(Op.getRegList(), IsVector);
9577   return false;
9578 }
9579 
9580 /// parseDirectiveInst
9581 ///  ::= .inst opcode [, ...]
9582 ///  ::= .inst.n opcode [, ...]
9583 ///  ::= .inst.w opcode [, ...]
9584 bool ARMAsmParser::parseDirectiveInst(SMLoc Loc, char Suffix) {
9585   MCAsmParser &Parser = getParser();
9586   int Width;
9587 
9588   if (isThumb()) {
9589     switch (Suffix) {
9590     case 'n':
9591       Width = 2;
9592       break;
9593     case 'w':
9594       Width = 4;
9595       break;
9596     default:
9597       Parser.eatToEndOfStatement();
9598       Error(Loc, "cannot determine Thumb instruction size, "
9599                  "use inst.n/inst.w instead");
9600       return false;
9601     }
9602   } else {
9603     if (Suffix) {
9604       Parser.eatToEndOfStatement();
9605       Error(Loc, "width suffixes are invalid in ARM mode");
9606       return false;
9607     }
9608     Width = 4;
9609   }
9610 
9611   if (getLexer().is(AsmToken::EndOfStatement)) {
9612     Parser.eatToEndOfStatement();
9613     Error(Loc, "expected expression following directive");
9614     return false;
9615   }
9616 
9617   for (;;) {
9618     const MCExpr *Expr;
9619 
9620     if (getParser().parseExpression(Expr)) {
9621       Error(Loc, "expected expression");
9622       return false;
9623     }
9624 
9625     const MCConstantExpr *Value = dyn_cast_or_null<MCConstantExpr>(Expr);
9626     if (!Value) {
9627       Error(Loc, "expected constant expression");
9628       return false;
9629     }
9630 
9631     switch (Width) {
9632     case 2:
9633       if (Value->getValue() > 0xffff) {
9634         Error(Loc, "inst.n operand is too big, use inst.w instead");
9635         return false;
9636       }
9637       break;
9638     case 4:
9639       if (Value->getValue() > 0xffffffff) {
9640         Error(Loc,
9641               StringRef(Suffix ? "inst.w" : "inst") + " operand is too big");
9642         return false;
9643       }
9644       break;
9645     default:
9646       llvm_unreachable("only supported widths are 2 and 4");
9647     }
9648 
9649     getTargetStreamer().emitInst(Value->getValue(), Suffix);
9650 
9651     if (getLexer().is(AsmToken::EndOfStatement))
9652       break;
9653 
9654     if (getLexer().isNot(AsmToken::Comma)) {
9655       Error(Loc, "unexpected token in directive");
9656       return false;
9657     }
9658 
9659     Parser.Lex();
9660   }
9661 
9662   Parser.Lex();
9663   return false;
9664 }
9665 
9666 /// parseDirectiveLtorg
9667 ///  ::= .ltorg | .pool
9668 bool ARMAsmParser::parseDirectiveLtorg(SMLoc L) {
9669   getTargetStreamer().emitCurrentConstantPool();
9670   return false;
9671 }
9672 
9673 bool ARMAsmParser::parseDirectiveEven(SMLoc L) {
9674   const MCSection *Section = getStreamer().getCurrentSection().first;
9675 
9676   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9677     TokError("unexpected token in directive");
9678     return false;
9679   }
9680 
9681   if (!Section) {
9682     getStreamer().InitSections(false);
9683     Section = getStreamer().getCurrentSection().first;
9684   }
9685 
9686   assert(Section && "must have section to emit alignment");
9687   if (Section->UseCodeAlign())
9688     getStreamer().EmitCodeAlignment(2);
9689   else
9690     getStreamer().EmitValueToAlignment(2);
9691 
9692   return false;
9693 }
9694 
9695 /// parseDirectivePersonalityIndex
9696 ///   ::= .personalityindex index
9697 bool ARMAsmParser::parseDirectivePersonalityIndex(SMLoc L) {
9698   MCAsmParser &Parser = getParser();
9699   bool HasExistingPersonality = UC.hasPersonality();
9700 
9701   UC.recordPersonalityIndex(L);
9702 
9703   if (!UC.hasFnStart()) {
9704     Parser.eatToEndOfStatement();
9705     Error(L, ".fnstart must precede .personalityindex directive");
9706     return false;
9707   }
9708   if (UC.cantUnwind()) {
9709     Parser.eatToEndOfStatement();
9710     Error(L, ".personalityindex cannot be used with .cantunwind");
9711     UC.emitCantUnwindLocNotes();
9712     return false;
9713   }
9714   if (UC.hasHandlerData()) {
9715     Parser.eatToEndOfStatement();
9716     Error(L, ".personalityindex must precede .handlerdata directive");
9717     UC.emitHandlerDataLocNotes();
9718     return false;
9719   }
9720   if (HasExistingPersonality) {
9721     Parser.eatToEndOfStatement();
9722     Error(L, "multiple personality directives");
9723     UC.emitPersonalityLocNotes();
9724     return false;
9725   }
9726 
9727   const MCExpr *IndexExpression;
9728   SMLoc IndexLoc = Parser.getTok().getLoc();
9729   if (Parser.parseExpression(IndexExpression)) {
9730     Parser.eatToEndOfStatement();
9731     return false;
9732   }
9733 
9734   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(IndexExpression);
9735   if (!CE) {
9736     Parser.eatToEndOfStatement();
9737     Error(IndexLoc, "index must be a constant number");
9738     return false;
9739   }
9740   if (CE->getValue() < 0 ||
9741       CE->getValue() >= ARM::EHABI::NUM_PERSONALITY_INDEX) {
9742     Parser.eatToEndOfStatement();
9743     Error(IndexLoc, "personality routine index should be in range [0-3]");
9744     return false;
9745   }
9746 
9747   getTargetStreamer().emitPersonalityIndex(CE->getValue());
9748   return false;
9749 }
9750 
9751 /// parseDirectiveUnwindRaw
9752 ///   ::= .unwind_raw offset, opcode [, opcode...]
9753 bool ARMAsmParser::parseDirectiveUnwindRaw(SMLoc L) {
9754   MCAsmParser &Parser = getParser();
9755   if (!UC.hasFnStart()) {
9756     Parser.eatToEndOfStatement();
9757     Error(L, ".fnstart must precede .unwind_raw directives");
9758     return false;
9759   }
9760 
9761   int64_t StackOffset;
9762 
9763   const MCExpr *OffsetExpr;
9764   SMLoc OffsetLoc = getLexer().getLoc();
9765   if (getLexer().is(AsmToken::EndOfStatement) ||
9766       getParser().parseExpression(OffsetExpr)) {
9767     Error(OffsetLoc, "expected expression");
9768     Parser.eatToEndOfStatement();
9769     return false;
9770   }
9771 
9772   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9773   if (!CE) {
9774     Error(OffsetLoc, "offset must be a constant");
9775     Parser.eatToEndOfStatement();
9776     return false;
9777   }
9778 
9779   StackOffset = CE->getValue();
9780 
9781   if (getLexer().isNot(AsmToken::Comma)) {
9782     Error(getLexer().getLoc(), "expected comma");
9783     Parser.eatToEndOfStatement();
9784     return false;
9785   }
9786   Parser.Lex();
9787 
9788   SmallVector<uint8_t, 16> Opcodes;
9789   for (;;) {
9790     const MCExpr *OE;
9791 
9792     SMLoc OpcodeLoc = getLexer().getLoc();
9793     if (getLexer().is(AsmToken::EndOfStatement) || Parser.parseExpression(OE)) {
9794       Error(OpcodeLoc, "expected opcode expression");
9795       Parser.eatToEndOfStatement();
9796       return false;
9797     }
9798 
9799     const MCConstantExpr *OC = dyn_cast<MCConstantExpr>(OE);
9800     if (!OC) {
9801       Error(OpcodeLoc, "opcode value must be a constant");
9802       Parser.eatToEndOfStatement();
9803       return false;
9804     }
9805 
9806     const int64_t Opcode = OC->getValue();
9807     if (Opcode & ~0xff) {
9808       Error(OpcodeLoc, "invalid opcode");
9809       Parser.eatToEndOfStatement();
9810       return false;
9811     }
9812 
9813     Opcodes.push_back(uint8_t(Opcode));
9814 
9815     if (getLexer().is(AsmToken::EndOfStatement))
9816       break;
9817 
9818     if (getLexer().isNot(AsmToken::Comma)) {
9819       Error(getLexer().getLoc(), "unexpected token in directive");
9820       Parser.eatToEndOfStatement();
9821       return false;
9822     }
9823 
9824     Parser.Lex();
9825   }
9826 
9827   getTargetStreamer().emitUnwindRaw(StackOffset, Opcodes);
9828 
9829   Parser.Lex();
9830   return false;
9831 }
9832 
9833 /// parseDirectiveTLSDescSeq
9834 ///   ::= .tlsdescseq tls-variable
9835 bool ARMAsmParser::parseDirectiveTLSDescSeq(SMLoc L) {
9836   MCAsmParser &Parser = getParser();
9837 
9838   if (getLexer().isNot(AsmToken::Identifier)) {
9839     TokError("expected variable after '.tlsdescseq' directive");
9840     Parser.eatToEndOfStatement();
9841     return false;
9842   }
9843 
9844   const MCSymbolRefExpr *SRE =
9845     MCSymbolRefExpr::create(Parser.getTok().getIdentifier(),
9846                             MCSymbolRefExpr::VK_ARM_TLSDESCSEQ, getContext());
9847   Lex();
9848 
9849   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9850     Error(Parser.getTok().getLoc(), "unexpected token");
9851     Parser.eatToEndOfStatement();
9852     return false;
9853   }
9854 
9855   getTargetStreamer().AnnotateTLSDescriptorSequence(SRE);
9856   return false;
9857 }
9858 
9859 /// parseDirectiveMovSP
9860 ///  ::= .movsp reg [, #offset]
9861 bool ARMAsmParser::parseDirectiveMovSP(SMLoc L) {
9862   MCAsmParser &Parser = getParser();
9863   if (!UC.hasFnStart()) {
9864     Parser.eatToEndOfStatement();
9865     Error(L, ".fnstart must precede .movsp directives");
9866     return false;
9867   }
9868   if (UC.getFPReg() != ARM::SP) {
9869     Parser.eatToEndOfStatement();
9870     Error(L, "unexpected .movsp directive");
9871     return false;
9872   }
9873 
9874   SMLoc SPRegLoc = Parser.getTok().getLoc();
9875   int SPReg = tryParseRegister();
9876   if (SPReg == -1) {
9877     Parser.eatToEndOfStatement();
9878     Error(SPRegLoc, "register expected");
9879     return false;
9880   }
9881 
9882   if (SPReg == ARM::SP || SPReg == ARM::PC) {
9883     Parser.eatToEndOfStatement();
9884     Error(SPRegLoc, "sp and pc are not permitted in .movsp directive");
9885     return false;
9886   }
9887 
9888   int64_t Offset = 0;
9889   if (Parser.getTok().is(AsmToken::Comma)) {
9890     Parser.Lex();
9891 
9892     if (Parser.getTok().isNot(AsmToken::Hash)) {
9893       Error(Parser.getTok().getLoc(), "expected #constant");
9894       Parser.eatToEndOfStatement();
9895       return false;
9896     }
9897     Parser.Lex();
9898 
9899     const MCExpr *OffsetExpr;
9900     SMLoc OffsetLoc = Parser.getTok().getLoc();
9901     if (Parser.parseExpression(OffsetExpr)) {
9902       Parser.eatToEndOfStatement();
9903       Error(OffsetLoc, "malformed offset expression");
9904       return false;
9905     }
9906 
9907     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9908     if (!CE) {
9909       Parser.eatToEndOfStatement();
9910       Error(OffsetLoc, "offset must be an immediate constant");
9911       return false;
9912     }
9913 
9914     Offset = CE->getValue();
9915   }
9916 
9917   getTargetStreamer().emitMovSP(SPReg, Offset);
9918   UC.saveFPReg(SPReg);
9919 
9920   return false;
9921 }
9922 
9923 /// parseDirectiveObjectArch
9924 ///   ::= .object_arch name
9925 bool ARMAsmParser::parseDirectiveObjectArch(SMLoc L) {
9926   MCAsmParser &Parser = getParser();
9927   if (getLexer().isNot(AsmToken::Identifier)) {
9928     Error(getLexer().getLoc(), "unexpected token");
9929     Parser.eatToEndOfStatement();
9930     return false;
9931   }
9932 
9933   StringRef Arch = Parser.getTok().getString();
9934   SMLoc ArchLoc = Parser.getTok().getLoc();
9935   getLexer().Lex();
9936 
9937   unsigned ID = ARM::parseArch(Arch);
9938 
9939   if (ID == ARM::AK_INVALID) {
9940     Error(ArchLoc, "unknown architecture '" + Arch + "'");
9941     Parser.eatToEndOfStatement();
9942     return false;
9943   }
9944 
9945   getTargetStreamer().emitObjectArch(ID);
9946 
9947   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9948     Error(getLexer().getLoc(), "unexpected token");
9949     Parser.eatToEndOfStatement();
9950   }
9951 
9952   return false;
9953 }
9954 
9955 /// parseDirectiveAlign
9956 ///   ::= .align
9957 bool ARMAsmParser::parseDirectiveAlign(SMLoc L) {
9958   // NOTE: if this is not the end of the statement, fall back to the target
9959   // agnostic handling for this directive which will correctly handle this.
9960   if (getLexer().isNot(AsmToken::EndOfStatement))
9961     return true;
9962 
9963   // '.align' is target specifically handled to mean 2**2 byte alignment.
9964   if (getStreamer().getCurrentSection().first->UseCodeAlign())
9965     getStreamer().EmitCodeAlignment(4, 0);
9966   else
9967     getStreamer().EmitValueToAlignment(4, 0, 1, 0);
9968 
9969   return false;
9970 }
9971 
9972 /// parseDirectiveThumbSet
9973 ///  ::= .thumb_set name, value
9974 bool ARMAsmParser::parseDirectiveThumbSet(SMLoc L) {
9975   MCAsmParser &Parser = getParser();
9976 
9977   StringRef Name;
9978   if (Parser.parseIdentifier(Name)) {
9979     TokError("expected identifier after '.thumb_set'");
9980     Parser.eatToEndOfStatement();
9981     return false;
9982   }
9983 
9984   if (getLexer().isNot(AsmToken::Comma)) {
9985     TokError("expected comma after name '" + Name + "'");
9986     Parser.eatToEndOfStatement();
9987     return false;
9988   }
9989   Lex();
9990 
9991   MCSymbol *Sym;
9992   const MCExpr *Value;
9993   if (MCParserUtils::parseAssignmentExpression(Name, /* allow_redef */ true,
9994                                                Parser, Sym, Value))
9995     return true;
9996 
9997   getTargetStreamer().emitThumbSet(Sym, Value);
9998   return false;
9999 }
10000 
10001 /// Force static initialization.
10002 extern "C" void LLVMInitializeARMAsmParser() {
10003   RegisterMCAsmParser<ARMAsmParser> X(TheARMLETarget);
10004   RegisterMCAsmParser<ARMAsmParser> Y(TheARMBETarget);
10005   RegisterMCAsmParser<ARMAsmParser> A(TheThumbLETarget);
10006   RegisterMCAsmParser<ARMAsmParser> B(TheThumbBETarget);
10007 }
10008 
10009 #define GET_REGISTER_MATCHER
10010 #define GET_SUBTARGET_FEATURE_NAME
10011 #define GET_MATCHER_IMPLEMENTATION
10012 #include "ARMGenAsmMatcher.inc"
10013 
10014 // FIXME: This structure should be moved inside ARMTargetParser
10015 // when we start to table-generate them, and we can use the ARM
10016 // flags below, that were generated by table-gen.
10017 static const struct {
10018   const unsigned Kind;
10019   const uint64_t ArchCheck;
10020   const FeatureBitset Features;
10021 } Extensions[] = {
10022   { ARM::AEK_CRC, Feature_HasV8, {ARM::FeatureCRC} },
10023   { ARM::AEK_CRYPTO,  Feature_HasV8,
10024     {ARM::FeatureCrypto, ARM::FeatureNEON, ARM::FeatureFPARMv8} },
10025   { ARM::AEK_FP, Feature_HasV8, {ARM::FeatureFPARMv8} },
10026   { (ARM::AEK_HWDIV | ARM::AEK_HWDIVARM), Feature_HasV7 | Feature_IsNotMClass,
10027     {ARM::FeatureHWDiv, ARM::FeatureHWDivARM} },
10028   { ARM::AEK_MP, Feature_HasV7 | Feature_IsNotMClass, {ARM::FeatureMP} },
10029   { ARM::AEK_SIMD, Feature_HasV8, {ARM::FeatureNEON, ARM::FeatureFPARMv8} },
10030   { ARM::AEK_SEC, Feature_HasV6K, {ARM::FeatureTrustZone} },
10031   // FIXME: Only available in A-class, isel not predicated
10032   { ARM::AEK_VIRT, Feature_HasV7, {ARM::FeatureVirtualization} },
10033   { ARM::AEK_FP16, Feature_HasV8_2a, {ARM::FeatureFPARMv8, ARM::FeatureFullFP16} },
10034   // FIXME: Unsupported extensions.
10035   { ARM::AEK_OS, Feature_None, {} },
10036   { ARM::AEK_IWMMXT, Feature_None, {} },
10037   { ARM::AEK_IWMMXT2, Feature_None, {} },
10038   { ARM::AEK_MAVERICK, Feature_None, {} },
10039   { ARM::AEK_XSCALE, Feature_None, {} },
10040 };
10041 
10042 /// parseDirectiveArchExtension
10043 ///   ::= .arch_extension [no]feature
10044 bool ARMAsmParser::parseDirectiveArchExtension(SMLoc L) {
10045   MCAsmParser &Parser = getParser();
10046 
10047   if (getLexer().isNot(AsmToken::Identifier)) {
10048     Error(getLexer().getLoc(), "unexpected token");
10049     Parser.eatToEndOfStatement();
10050     return false;
10051   }
10052 
10053   StringRef Name = Parser.getTok().getString();
10054   SMLoc ExtLoc = Parser.getTok().getLoc();
10055   getLexer().Lex();
10056 
10057   bool EnableFeature = true;
10058   if (Name.startswith_lower("no")) {
10059     EnableFeature = false;
10060     Name = Name.substr(2);
10061   }
10062   unsigned FeatureKind = ARM::parseArchExt(Name);
10063   if (FeatureKind == ARM::AEK_INVALID)
10064     Error(ExtLoc, "unknown architectural extension: " + Name);
10065 
10066   for (const auto &Extension : Extensions) {
10067     if (Extension.Kind != FeatureKind)
10068       continue;
10069 
10070     if (Extension.Features.none())
10071       report_fatal_error("unsupported architectural extension: " + Name);
10072 
10073     if ((getAvailableFeatures() & Extension.ArchCheck) != Extension.ArchCheck) {
10074       Error(ExtLoc, "architectural extension '" + Name + "' is not "
10075             "allowed for the current base architecture");
10076       return false;
10077     }
10078 
10079     MCSubtargetInfo &STI = copySTI();
10080     FeatureBitset ToggleFeatures = EnableFeature
10081       ? (~STI.getFeatureBits() & Extension.Features)
10082       : ( STI.getFeatureBits() & Extension.Features);
10083 
10084     uint64_t Features =
10085         ComputeAvailableFeatures(STI.ToggleFeature(ToggleFeatures));
10086     setAvailableFeatures(Features);
10087     return false;
10088   }
10089 
10090   Error(ExtLoc, "unknown architectural extension: " + Name);
10091   Parser.eatToEndOfStatement();
10092   return false;
10093 }
10094 
10095 // Define this matcher function after the auto-generated include so we
10096 // have the match class enum definitions.
10097 unsigned ARMAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
10098                                                   unsigned Kind) {
10099   ARMOperand &Op = static_cast<ARMOperand &>(AsmOp);
10100   // If the kind is a token for a literal immediate, check if our asm
10101   // operand matches. This is for InstAliases which have a fixed-value
10102   // immediate in the syntax.
10103   switch (Kind) {
10104   default: break;
10105   case MCK__35_0:
10106     if (Op.isImm())
10107       if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm()))
10108         if (CE->getValue() == 0)
10109           return Match_Success;
10110     break;
10111   case MCK_ModImm:
10112     if (Op.isImm()) {
10113       const MCExpr *SOExpr = Op.getImm();
10114       int64_t Value;
10115       if (!SOExpr->evaluateAsAbsolute(Value))
10116         return Match_Success;
10117       assert((Value >= INT32_MIN && Value <= UINT32_MAX) &&
10118              "expression value must be representable in 32 bits");
10119     }
10120     break;
10121   case MCK_rGPR:
10122     if (hasV8Ops() && Op.isReg() && Op.getReg() == ARM::SP)
10123       return Match_Success;
10124     break;
10125   case MCK_GPRPair:
10126     if (Op.isReg() &&
10127         MRI->getRegClass(ARM::GPRRegClassID).contains(Op.getReg()))
10128       return Match_Success;
10129     break;
10130   }
10131   return Match_InvalidOperand;
10132 }
10133