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