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