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