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