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