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