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