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