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