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