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