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