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