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