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