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