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