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