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