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