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