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