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