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 "ARMFeatures.h"
11 #include "MCTargetDesc/ARMAddressingModes.h"
12 #include "MCTargetDesc/ARMBaseInfo.h"
13 #include "MCTargetDesc/ARMMCExpr.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/ADT/Triple.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/MCDisassembler/MCDisassembler.h"
24 #include "llvm/MC/MCELFStreamer.h"
25 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCInstrDesc.h"
28 #include "llvm/MC/MCInstrInfo.h"
29 #include "llvm/MC/MCObjectFileInfo.h"
30 #include "llvm/MC/MCParser/MCAsmLexer.h"
31 #include "llvm/MC/MCParser/MCAsmParser.h"
32 #include "llvm/MC/MCParser/MCAsmParserUtils.h"
33 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
34 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
35 #include "llvm/MC/MCRegisterInfo.h"
36 #include "llvm/MC/MCSection.h"
37 #include "llvm/MC/MCStreamer.h"
38 #include "llvm/MC/MCSubtargetInfo.h"
39 #include "llvm/MC/MCSymbol.h"
40 #include "llvm/Support/ARMBuildAttributes.h"
41 #include "llvm/Support/ARMEHABI.h"
42 #include "llvm/Support/COFF.h"
43 #include "llvm/Support/CommandLine.h"
44 #include "llvm/Support/Debug.h"
45 #include "llvm/Support/ELF.h"
46 #include "llvm/Support/MathExtras.h"
47 #include "llvm/Support/SourceMgr.h"
48 #include "llvm/Support/TargetParser.h"
49 #include "llvm/Support/TargetRegistry.h"
50 #include "llvm/Support/raw_ostream.h"
51 
52 using namespace llvm;
53 
54 namespace {
55 
56 enum class ImplicitItModeTy { Always, Never, ARMOnly, ThumbOnly };
57 
58 static cl::opt<ImplicitItModeTy> ImplicitItMode(
59     "arm-implicit-it", cl::init(ImplicitItModeTy::ARMOnly),
60     cl::desc("Allow conditional instructions outdside of an IT block"),
61     cl::values(clEnumValN(ImplicitItModeTy::Always, "always",
62                           "Accept in both ISAs, emit implicit ITs in Thumb"),
63                clEnumValN(ImplicitItModeTy::Never, "never",
64                           "Warn in ARM, reject in Thumb"),
65                clEnumValN(ImplicitItModeTy::ARMOnly, "arm",
66                           "Accept in ARM, reject in Thumb"),
67                clEnumValN(ImplicitItModeTy::ThumbOnly, "thumb",
68                           "Warn in ARM, emit implicit ITs in Thumb"),
69                clEnumValEnd));
70 
71 class ARMOperand;
72 
73 enum VectorLaneTy { NoLanes, AllLanes, IndexedLane };
74 
75 class UnwindContext {
76   MCAsmParser &Parser;
77 
78   typedef SmallVector<SMLoc, 4> Locs;
79 
80   Locs FnStartLocs;
81   Locs CantUnwindLocs;
82   Locs PersonalityLocs;
83   Locs PersonalityIndexLocs;
84   Locs HandlerDataLocs;
85   int FPReg;
86 
87 public:
88   UnwindContext(MCAsmParser &P) : Parser(P), FPReg(ARM::SP) {}
89 
90   bool hasFnStart() const { return !FnStartLocs.empty(); }
91   bool cantUnwind() const { return !CantUnwindLocs.empty(); }
92   bool hasHandlerData() const { return !HandlerDataLocs.empty(); }
93   bool hasPersonality() const {
94     return !(PersonalityLocs.empty() && PersonalityIndexLocs.empty());
95   }
96 
97   void recordFnStart(SMLoc L) { FnStartLocs.push_back(L); }
98   void recordCantUnwind(SMLoc L) { CantUnwindLocs.push_back(L); }
99   void recordPersonality(SMLoc L) { PersonalityLocs.push_back(L); }
100   void recordHandlerData(SMLoc L) { HandlerDataLocs.push_back(L); }
101   void recordPersonalityIndex(SMLoc L) { PersonalityIndexLocs.push_back(L); }
102 
103   void saveFPReg(int Reg) { FPReg = Reg; }
104   int getFPReg() const { return FPReg; }
105 
106   void emitFnStartLocNotes() const {
107     for (Locs::const_iterator FI = FnStartLocs.begin(), FE = FnStartLocs.end();
108          FI != FE; ++FI)
109       Parser.Note(*FI, ".fnstart was specified here");
110   }
111   void emitCantUnwindLocNotes() const {
112     for (Locs::const_iterator UI = CantUnwindLocs.begin(),
113                               UE = CantUnwindLocs.end(); UI != UE; ++UI)
114       Parser.Note(*UI, ".cantunwind was specified here");
115   }
116   void emitHandlerDataLocNotes() const {
117     for (Locs::const_iterator HI = HandlerDataLocs.begin(),
118                               HE = HandlerDataLocs.end(); HI != HE; ++HI)
119       Parser.Note(*HI, ".handlerdata was specified here");
120   }
121   void emitPersonalityLocNotes() const {
122     for (Locs::const_iterator PI = PersonalityLocs.begin(),
123                               PE = PersonalityLocs.end(),
124                               PII = PersonalityIndexLocs.begin(),
125                               PIE = PersonalityIndexLocs.end();
126          PI != PE || PII != PIE;) {
127       if (PI != PE && (PII == PIE || PI->getPointer() < PII->getPointer()))
128         Parser.Note(*PI++, ".personality was specified here");
129       else if (PII != PIE && (PI == PE || PII->getPointer() < PI->getPointer()))
130         Parser.Note(*PII++, ".personalityindex was specified here");
131       else
132         llvm_unreachable(".personality and .personalityindex cannot be "
133                          "at the same location");
134     }
135   }
136 
137   void reset() {
138     FnStartLocs = Locs();
139     CantUnwindLocs = Locs();
140     PersonalityLocs = Locs();
141     HandlerDataLocs = Locs();
142     PersonalityIndexLocs = Locs();
143     FPReg = ARM::SP;
144   }
145 };
146 
147 class ARMAsmParser : public MCTargetAsmParser {
148   const MCInstrInfo &MII;
149   const MCRegisterInfo *MRI;
150   UnwindContext UC;
151 
152   ARMTargetStreamer &getTargetStreamer() {
153     assert(getParser().getStreamer().getTargetStreamer() &&
154            "do not have a target streamer");
155     MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
156     return static_cast<ARMTargetStreamer &>(TS);
157   }
158 
159   // Map of register aliases registers via the .req directive.
160   StringMap<unsigned> RegisterReqs;
161 
162   bool NextSymbolIsThumb;
163 
164   bool useImplicitITThumb() const {
165     return ImplicitItMode == ImplicitItModeTy::Always ||
166            ImplicitItMode == ImplicitItModeTy::ThumbOnly;
167   }
168 
169   bool useImplicitITARM() const {
170     return ImplicitItMode == ImplicitItModeTy::Always ||
171            ImplicitItMode == ImplicitItModeTy::ARMOnly;
172   }
173 
174   struct {
175     ARMCC::CondCodes Cond;    // Condition for IT block.
176     unsigned Mask:4;          // Condition mask for instructions.
177                               // Starting at first 1 (from lsb).
178                               //   '1'  condition as indicated in IT.
179                               //   '0'  inverse of condition (else).
180                               // Count of instructions in IT block is
181                               // 4 - trailingzeroes(mask)
182                               // Note that this does not have the same encoding
183                               // as in the IT instruction, which also depends
184                               // on the low bit of the condition code.
185 
186     unsigned CurPosition;     // Current position in parsing of IT
187                               // block. In range [0,4], with 0 being the IT
188                               // instruction itself. Initialized according to
189                               // count of instructions in block.  ~0U if no
190                               // active IT block.
191 
192     bool IsExplicit;          // true  - The IT instruction was present in the
193                               //         input, we should not modify it.
194                               // false - The IT instruction was added
195                               //         implicitly, we can extend it if that
196                               //         would be legal.
197   } ITState;
198 
199   llvm::SmallVector<MCInst, 4> PendingConditionalInsts;
200 
201   void flushPendingInstructions(MCStreamer &Out) override {
202     if (!inImplicitITBlock()) {
203       assert(PendingConditionalInsts.size() == 0);
204       return;
205     }
206 
207     // Emit the IT instruction
208     unsigned Mask = getITMaskEncoding();
209     MCInst ITInst;
210     ITInst.setOpcode(ARM::t2IT);
211     ITInst.addOperand(MCOperand::createImm(ITState.Cond));
212     ITInst.addOperand(MCOperand::createImm(Mask));
213     Out.EmitInstruction(ITInst, getSTI());
214 
215     // Emit the conditonal instructions
216     assert(PendingConditionalInsts.size() <= 4);
217     for (const MCInst &Inst : PendingConditionalInsts) {
218       Out.EmitInstruction(Inst, getSTI());
219     }
220     PendingConditionalInsts.clear();
221 
222     // Clear the IT state
223     ITState.Mask = 0;
224     ITState.CurPosition = ~0U;
225   }
226 
227   bool inITBlock() { return ITState.CurPosition != ~0U; }
228   bool inExplicitITBlock() { return inITBlock() && ITState.IsExplicit; }
229   bool inImplicitITBlock() { return inITBlock() && !ITState.IsExplicit; }
230   bool lastInITBlock() {
231     return ITState.CurPosition == 4 - countTrailingZeros(ITState.Mask);
232   }
233   void forwardITPosition() {
234     if (!inITBlock()) return;
235     // Move to the next instruction in the IT block, if there is one. If not,
236     // mark the block as done, except for implicit IT blocks, which we leave
237     // open until we find an instruction that can't be added to it.
238     unsigned TZ = countTrailingZeros(ITState.Mask);
239     if (++ITState.CurPosition == 5 - TZ && ITState.IsExplicit)
240       ITState.CurPosition = ~0U; // Done with the IT block after this.
241   }
242 
243   // Rewind the state of the current IT block, removing the last slot from it.
244   void rewindImplicitITPosition() {
245     assert(inImplicitITBlock());
246     assert(ITState.CurPosition > 1);
247     ITState.CurPosition--;
248     unsigned TZ = countTrailingZeros(ITState.Mask);
249     unsigned NewMask = 0;
250     NewMask |= ITState.Mask & (0xC << TZ);
251     NewMask |= 0x2 << TZ;
252     ITState.Mask = NewMask;
253   }
254 
255   // Rewind the state of the current IT block, removing the last slot from it.
256   // If we were at the first slot, this closes the IT block.
257   void discardImplicitITBlock() {
258     assert(inImplicitITBlock());
259     assert(ITState.CurPosition == 1);
260     ITState.CurPosition = ~0U;
261     return;
262   }
263 
264   // Get the encoding of the IT mask, as it will appear in an IT instruction.
265   unsigned getITMaskEncoding() {
266     assert(inITBlock());
267     unsigned Mask = ITState.Mask;
268     unsigned TZ = countTrailingZeros(Mask);
269     if ((ITState.Cond & 1) == 0) {
270       assert(Mask && TZ <= 3 && "illegal IT mask value!");
271       Mask ^= (0xE << TZ) & 0xF;
272     }
273     return Mask;
274   }
275 
276   // Get the condition code corresponding to the current IT block slot.
277   ARMCC::CondCodes currentITCond() {
278     unsigned MaskBit;
279     if (ITState.CurPosition == 1)
280       MaskBit = 1;
281     else
282       MaskBit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
283 
284     return MaskBit ? ITState.Cond : ARMCC::getOppositeCondition(ITState.Cond);
285   }
286 
287   // Invert the condition of the current IT block slot without changing any
288   // other slots in the same block.
289   void invertCurrentITCondition() {
290     if (ITState.CurPosition == 1) {
291       ITState.Cond = ARMCC::getOppositeCondition(ITState.Cond);
292     } else {
293       ITState.Mask ^= 1 << (5 - ITState.CurPosition);
294     }
295   }
296 
297   // Returns true if the current IT block is full (all 4 slots used).
298   bool isITBlockFull() {
299     return inITBlock() && (ITState.Mask & 1);
300   }
301 
302   // Extend the current implicit IT block to have one more slot with the given
303   // condition code.
304   void extendImplicitITBlock(ARMCC::CondCodes Cond) {
305     assert(inImplicitITBlock());
306     assert(!isITBlockFull());
307     assert(Cond == ITState.Cond ||
308            Cond == ARMCC::getOppositeCondition(ITState.Cond));
309     unsigned TZ = countTrailingZeros(ITState.Mask);
310     unsigned NewMask = 0;
311     // Keep any existing condition bits.
312     NewMask |= ITState.Mask & (0xE << TZ);
313     // Insert the new condition bit.
314     NewMask |= (Cond == ITState.Cond) << TZ;
315     // Move the trailing 1 down one bit.
316     NewMask |= 1 << (TZ - 1);
317     ITState.Mask = NewMask;
318   }
319 
320   // Create a new implicit IT block with a dummy condition code.
321   void startImplicitITBlock() {
322     assert(!inITBlock());
323     ITState.Cond = ARMCC::AL;
324     ITState.Mask = 8;
325     ITState.CurPosition = 1;
326     ITState.IsExplicit = false;
327     return;
328   }
329 
330   // Create a new explicit IT block with the given condition and mask. The mask
331   // should be in the parsed format, with a 1 implying 't', regardless of the
332   // low bit of the condition.
333   void startExplicitITBlock(ARMCC::CondCodes Cond, unsigned Mask) {
334     assert(!inITBlock());
335     ITState.Cond = Cond;
336     ITState.Mask = Mask;
337     ITState.CurPosition = 0;
338     ITState.IsExplicit = true;
339     return;
340   }
341 
342   void Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges = None) {
343     return getParser().Note(L, Msg, Ranges);
344   }
345   bool Warning(SMLoc L, const Twine &Msg,
346                ArrayRef<SMRange> Ranges = None) {
347     return getParser().Warning(L, Msg, Ranges);
348   }
349   bool Error(SMLoc L, const Twine &Msg,
350              ArrayRef<SMRange> Ranges = None) {
351     return getParser().Error(L, Msg, Ranges);
352   }
353 
354   bool validatetLDMRegList(const MCInst &Inst, const OperandVector &Operands,
355                            unsigned ListNo, bool IsARPop = false);
356   bool validatetSTMRegList(const MCInst &Inst, const OperandVector &Operands,
357                            unsigned ListNo);
358 
359   int tryParseRegister();
360   bool tryParseRegisterWithWriteBack(OperandVector &);
361   int tryParseShiftRegister(OperandVector &);
362   bool parseRegisterList(OperandVector &);
363   bool parseMemory(OperandVector &);
364   bool parseOperand(OperandVector &, StringRef Mnemonic);
365   bool parsePrefix(ARMMCExpr::VariantKind &RefKind);
366   bool parseMemRegOffsetShift(ARM_AM::ShiftOpc &ShiftType,
367                               unsigned &ShiftAmount);
368   bool parseLiteralValues(unsigned Size, SMLoc L);
369   bool parseDirectiveThumb(SMLoc L);
370   bool parseDirectiveARM(SMLoc L);
371   bool parseDirectiveThumbFunc(SMLoc L);
372   bool parseDirectiveCode(SMLoc L);
373   bool parseDirectiveSyntax(SMLoc L);
374   bool parseDirectiveReq(StringRef Name, SMLoc L);
375   bool parseDirectiveUnreq(SMLoc L);
376   bool parseDirectiveArch(SMLoc L);
377   bool parseDirectiveEabiAttr(SMLoc L);
378   bool parseDirectiveCPU(SMLoc L);
379   bool parseDirectiveFPU(SMLoc L);
380   bool parseDirectiveFnStart(SMLoc L);
381   bool parseDirectiveFnEnd(SMLoc L);
382   bool parseDirectiveCantUnwind(SMLoc L);
383   bool parseDirectivePersonality(SMLoc L);
384   bool parseDirectiveHandlerData(SMLoc L);
385   bool parseDirectiveSetFP(SMLoc L);
386   bool parseDirectivePad(SMLoc L);
387   bool parseDirectiveRegSave(SMLoc L, bool IsVector);
388   bool parseDirectiveInst(SMLoc L, char Suffix = '\0');
389   bool parseDirectiveLtorg(SMLoc L);
390   bool parseDirectiveEven(SMLoc L);
391   bool parseDirectivePersonalityIndex(SMLoc L);
392   bool parseDirectiveUnwindRaw(SMLoc L);
393   bool parseDirectiveTLSDescSeq(SMLoc L);
394   bool parseDirectiveMovSP(SMLoc L);
395   bool parseDirectiveObjectArch(SMLoc L);
396   bool parseDirectiveArchExtension(SMLoc L);
397   bool parseDirectiveAlign(SMLoc L);
398   bool parseDirectiveThumbSet(SMLoc L);
399 
400   StringRef splitMnemonic(StringRef Mnemonic, unsigned &PredicationCode,
401                           bool &CarrySetting, unsigned &ProcessorIMod,
402                           StringRef &ITMask);
403   void getMnemonicAcceptInfo(StringRef Mnemonic, StringRef FullInst,
404                              bool &CanAcceptCarrySet,
405                              bool &CanAcceptPredicationCode);
406 
407   void tryConvertingToTwoOperandForm(StringRef Mnemonic, bool CarrySetting,
408                                      OperandVector &Operands);
409   bool isThumb() const {
410     // FIXME: Can tablegen auto-generate this?
411     return getSTI().getFeatureBits()[ARM::ModeThumb];
412   }
413   bool isThumbOne() const {
414     return isThumb() && !getSTI().getFeatureBits()[ARM::FeatureThumb2];
415   }
416   bool isThumbTwo() const {
417     return isThumb() && getSTI().getFeatureBits()[ARM::FeatureThumb2];
418   }
419   bool hasThumb() const {
420     return getSTI().getFeatureBits()[ARM::HasV4TOps];
421   }
422   bool hasThumb2() const {
423     return getSTI().getFeatureBits()[ARM::FeatureThumb2];
424   }
425   bool hasV6Ops() const {
426     return getSTI().getFeatureBits()[ARM::HasV6Ops];
427   }
428   bool hasV6T2Ops() const {
429     return getSTI().getFeatureBits()[ARM::HasV6T2Ops];
430   }
431   bool hasV6MOps() const {
432     return getSTI().getFeatureBits()[ARM::HasV6MOps];
433   }
434   bool hasV7Ops() const {
435     return getSTI().getFeatureBits()[ARM::HasV7Ops];
436   }
437   bool hasV8Ops() const {
438     return getSTI().getFeatureBits()[ARM::HasV8Ops];
439   }
440   bool hasV8MBaseline() const {
441     return getSTI().getFeatureBits()[ARM::HasV8MBaselineOps];
442   }
443   bool hasV8MMainline() const {
444     return getSTI().getFeatureBits()[ARM::HasV8MMainlineOps];
445   }
446   bool has8MSecExt() const {
447     return getSTI().getFeatureBits()[ARM::Feature8MSecExt];
448   }
449   bool hasARM() const {
450     return !getSTI().getFeatureBits()[ARM::FeatureNoARM];
451   }
452   bool hasDSP() const {
453     return getSTI().getFeatureBits()[ARM::FeatureDSP];
454   }
455   bool hasD16() const {
456     return getSTI().getFeatureBits()[ARM::FeatureD16];
457   }
458   bool hasV8_1aOps() const {
459     return getSTI().getFeatureBits()[ARM::HasV8_1aOps];
460   }
461   bool hasRAS() const {
462     return getSTI().getFeatureBits()[ARM::FeatureRAS];
463   }
464 
465   void SwitchMode() {
466     MCSubtargetInfo &STI = copySTI();
467     uint64_t FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
468     setAvailableFeatures(FB);
469   }
470   void FixModeAfterArchChange(bool WasThumb, SMLoc Loc);
471   bool isMClass() const {
472     return getSTI().getFeatureBits()[ARM::FeatureMClass];
473   }
474 
475   /// @name Auto-generated Match Functions
476   /// {
477 
478 #define GET_ASSEMBLER_HEADER
479 #include "ARMGenAsmMatcher.inc"
480 
481   /// }
482 
483   OperandMatchResultTy parseITCondCode(OperandVector &);
484   OperandMatchResultTy parseCoprocNumOperand(OperandVector &);
485   OperandMatchResultTy parseCoprocRegOperand(OperandVector &);
486   OperandMatchResultTy parseCoprocOptionOperand(OperandVector &);
487   OperandMatchResultTy parseMemBarrierOptOperand(OperandVector &);
488   OperandMatchResultTy parseInstSyncBarrierOptOperand(OperandVector &);
489   OperandMatchResultTy parseProcIFlagsOperand(OperandVector &);
490   OperandMatchResultTy parseMSRMaskOperand(OperandVector &);
491   OperandMatchResultTy parseBankedRegOperand(OperandVector &);
492   OperandMatchResultTy parsePKHImm(OperandVector &O, StringRef Op, int Low,
493                                    int High);
494   OperandMatchResultTy parsePKHLSLImm(OperandVector &O) {
495     return parsePKHImm(O, "lsl", 0, 31);
496   }
497   OperandMatchResultTy parsePKHASRImm(OperandVector &O) {
498     return parsePKHImm(O, "asr", 1, 32);
499   }
500   OperandMatchResultTy parseSetEndImm(OperandVector &);
501   OperandMatchResultTy parseShifterImm(OperandVector &);
502   OperandMatchResultTy parseRotImm(OperandVector &);
503   OperandMatchResultTy parseModImm(OperandVector &);
504   OperandMatchResultTy parseBitfield(OperandVector &);
505   OperandMatchResultTy parsePostIdxReg(OperandVector &);
506   OperandMatchResultTy parseAM3Offset(OperandVector &);
507   OperandMatchResultTy parseFPImm(OperandVector &);
508   OperandMatchResultTy parseVectorList(OperandVector &);
509   OperandMatchResultTy parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index,
510                                        SMLoc &EndLoc);
511 
512   // Asm Match Converter Methods
513   void cvtThumbMultiply(MCInst &Inst, const OperandVector &);
514   void cvtThumbBranches(MCInst &Inst, const OperandVector &);
515 
516   bool validateInstruction(MCInst &Inst, const OperandVector &Ops);
517   bool processInstruction(MCInst &Inst, const OperandVector &Ops, MCStreamer &Out);
518   bool shouldOmitCCOutOperand(StringRef Mnemonic, OperandVector &Operands);
519   bool shouldOmitPredicateOperand(StringRef Mnemonic, OperandVector &Operands);
520   bool isITBlockTerminator(MCInst &Inst) const;
521 
522 public:
523   enum ARMMatchResultTy {
524     Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
525     Match_RequiresNotITBlock,
526     Match_RequiresV6,
527     Match_RequiresThumb2,
528     Match_RequiresV8,
529 #define GET_OPERAND_DIAGNOSTIC_TYPES
530 #include "ARMGenAsmMatcher.inc"
531 
532   };
533 
534   ARMAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
535                const MCInstrInfo &MII, const MCTargetOptions &Options)
536     : MCTargetAsmParser(Options, STI), MII(MII), UC(Parser) {
537     MCAsmParserExtension::Initialize(Parser);
538 
539     // Cache the MCRegisterInfo.
540     MRI = getContext().getRegisterInfo();
541 
542     // Initialize the set of available features.
543     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
544 
545     // Not in an ITBlock to start with.
546     ITState.CurPosition = ~0U;
547 
548     NextSymbolIsThumb = false;
549   }
550 
551   // Implementation of the MCTargetAsmParser interface:
552   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
553   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
554                         SMLoc NameLoc, OperandVector &Operands) override;
555   bool ParseDirective(AsmToken DirectiveID) override;
556 
557   unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
558                                       unsigned Kind) override;
559   unsigned checkTargetMatchPredicate(MCInst &Inst) override;
560 
561   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
562                                OperandVector &Operands, MCStreamer &Out,
563                                uint64_t &ErrorInfo,
564                                bool MatchingInlineAsm) override;
565   unsigned MatchInstruction(OperandVector &Operands, MCInst &Inst,
566                             uint64_t &ErrorInfo, bool MatchingInlineAsm,
567                             bool &EmitInITBlock, MCStreamer &Out);
568   void onLabelParsed(MCSymbol *Symbol) override;
569 };
570 } // end anonymous namespace
571 
572 namespace {
573 
574 /// ARMOperand - Instances of this class represent a parsed ARM machine
575 /// operand.
576 class ARMOperand : public MCParsedAsmOperand {
577   enum KindTy {
578     k_CondCode,
579     k_CCOut,
580     k_ITCondMask,
581     k_CoprocNum,
582     k_CoprocReg,
583     k_CoprocOption,
584     k_Immediate,
585     k_MemBarrierOpt,
586     k_InstSyncBarrierOpt,
587     k_Memory,
588     k_PostIndexRegister,
589     k_MSRMask,
590     k_BankedReg,
591     k_ProcIFlags,
592     k_VectorIndex,
593     k_Register,
594     k_RegisterList,
595     k_DPRRegisterList,
596     k_SPRRegisterList,
597     k_VectorList,
598     k_VectorListAllLanes,
599     k_VectorListIndexed,
600     k_ShiftedRegister,
601     k_ShiftedImmediate,
602     k_ShifterImmediate,
603     k_RotateImmediate,
604     k_ModifiedImmediate,
605     k_ConstantPoolImmediate,
606     k_BitfieldDescriptor,
607     k_Token,
608   } Kind;
609 
610   SMLoc StartLoc, EndLoc, AlignmentLoc;
611   SmallVector<unsigned, 8> Registers;
612 
613   struct CCOp {
614     ARMCC::CondCodes Val;
615   };
616 
617   struct CopOp {
618     unsigned Val;
619   };
620 
621   struct CoprocOptionOp {
622     unsigned Val;
623   };
624 
625   struct ITMaskOp {
626     unsigned Mask:4;
627   };
628 
629   struct MBOptOp {
630     ARM_MB::MemBOpt Val;
631   };
632 
633   struct ISBOptOp {
634     ARM_ISB::InstSyncBOpt Val;
635   };
636 
637   struct IFlagsOp {
638     ARM_PROC::IFlags Val;
639   };
640 
641   struct MMaskOp {
642     unsigned Val;
643   };
644 
645   struct BankedRegOp {
646     unsigned Val;
647   };
648 
649   struct TokOp {
650     const char *Data;
651     unsigned Length;
652   };
653 
654   struct RegOp {
655     unsigned RegNum;
656   };
657 
658   // A vector register list is a sequential list of 1 to 4 registers.
659   struct VectorListOp {
660     unsigned RegNum;
661     unsigned Count;
662     unsigned LaneIndex;
663     bool isDoubleSpaced;
664   };
665 
666   struct VectorIndexOp {
667     unsigned Val;
668   };
669 
670   struct ImmOp {
671     const MCExpr *Val;
672   };
673 
674   /// Combined record for all forms of ARM address expressions.
675   struct MemoryOp {
676     unsigned BaseRegNum;
677     // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
678     // was specified.
679     const MCConstantExpr *OffsetImm;  // Offset immediate value
680     unsigned OffsetRegNum;    // Offset register num, when OffsetImm == NULL
681     ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
682     unsigned ShiftImm;        // shift for OffsetReg.
683     unsigned Alignment;       // 0 = no alignment specified
684     // n = alignment in bytes (2, 4, 8, 16, or 32)
685     unsigned isNegative : 1;  // Negated OffsetReg? (~'U' bit)
686   };
687 
688   struct PostIdxRegOp {
689     unsigned RegNum;
690     bool isAdd;
691     ARM_AM::ShiftOpc ShiftTy;
692     unsigned ShiftImm;
693   };
694 
695   struct ShifterImmOp {
696     bool isASR;
697     unsigned Imm;
698   };
699 
700   struct RegShiftedRegOp {
701     ARM_AM::ShiftOpc ShiftTy;
702     unsigned SrcReg;
703     unsigned ShiftReg;
704     unsigned ShiftImm;
705   };
706 
707   struct RegShiftedImmOp {
708     ARM_AM::ShiftOpc ShiftTy;
709     unsigned SrcReg;
710     unsigned ShiftImm;
711   };
712 
713   struct RotImmOp {
714     unsigned Imm;
715   };
716 
717   struct ModImmOp {
718     unsigned Bits;
719     unsigned Rot;
720   };
721 
722   struct BitfieldOp {
723     unsigned LSB;
724     unsigned Width;
725   };
726 
727   union {
728     struct CCOp CC;
729     struct CopOp Cop;
730     struct CoprocOptionOp CoprocOption;
731     struct MBOptOp MBOpt;
732     struct ISBOptOp ISBOpt;
733     struct ITMaskOp ITMask;
734     struct IFlagsOp IFlags;
735     struct MMaskOp MMask;
736     struct BankedRegOp BankedReg;
737     struct TokOp Tok;
738     struct RegOp Reg;
739     struct VectorListOp VectorList;
740     struct VectorIndexOp VectorIndex;
741     struct ImmOp Imm;
742     struct MemoryOp Memory;
743     struct PostIdxRegOp PostIdxReg;
744     struct ShifterImmOp ShifterImm;
745     struct RegShiftedRegOp RegShiftedReg;
746     struct RegShiftedImmOp RegShiftedImm;
747     struct RotImmOp RotImm;
748     struct ModImmOp ModImm;
749     struct BitfieldOp Bitfield;
750   };
751 
752 public:
753   ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
754 
755   /// getStartLoc - Get the location of the first token of this operand.
756   SMLoc getStartLoc() const override { return StartLoc; }
757   /// getEndLoc - Get the location of the last token of this operand.
758   SMLoc getEndLoc() const override { return EndLoc; }
759   /// getLocRange - Get the range between the first and last token of this
760   /// operand.
761   SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
762 
763   /// getAlignmentLoc - Get the location of the Alignment token of this operand.
764   SMLoc getAlignmentLoc() const {
765     assert(Kind == k_Memory && "Invalid access!");
766     return AlignmentLoc;
767   }
768 
769   ARMCC::CondCodes getCondCode() const {
770     assert(Kind == k_CondCode && "Invalid access!");
771     return CC.Val;
772   }
773 
774   unsigned getCoproc() const {
775     assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
776     return Cop.Val;
777   }
778 
779   StringRef getToken() const {
780     assert(Kind == k_Token && "Invalid access!");
781     return StringRef(Tok.Data, Tok.Length);
782   }
783 
784   unsigned getReg() const override {
785     assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
786     return Reg.RegNum;
787   }
788 
789   const SmallVectorImpl<unsigned> &getRegList() const {
790     assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
791             Kind == k_SPRRegisterList) && "Invalid access!");
792     return Registers;
793   }
794 
795   const MCExpr *getImm() const {
796     assert(isImm() && "Invalid access!");
797     return Imm.Val;
798   }
799 
800   const MCExpr *getConstantPoolImm() const {
801     assert(isConstantPoolImm() && "Invalid access!");
802     return Imm.Val;
803   }
804 
805   unsigned getVectorIndex() const {
806     assert(Kind == k_VectorIndex && "Invalid access!");
807     return VectorIndex.Val;
808   }
809 
810   ARM_MB::MemBOpt getMemBarrierOpt() const {
811     assert(Kind == k_MemBarrierOpt && "Invalid access!");
812     return MBOpt.Val;
813   }
814 
815   ARM_ISB::InstSyncBOpt getInstSyncBarrierOpt() const {
816     assert(Kind == k_InstSyncBarrierOpt && "Invalid access!");
817     return ISBOpt.Val;
818   }
819 
820   ARM_PROC::IFlags getProcIFlags() const {
821     assert(Kind == k_ProcIFlags && "Invalid access!");
822     return IFlags.Val;
823   }
824 
825   unsigned getMSRMask() const {
826     assert(Kind == k_MSRMask && "Invalid access!");
827     return MMask.Val;
828   }
829 
830   unsigned getBankedReg() const {
831     assert(Kind == k_BankedReg && "Invalid access!");
832     return BankedReg.Val;
833   }
834 
835   bool isCoprocNum() const { return Kind == k_CoprocNum; }
836   bool isCoprocReg() const { return Kind == k_CoprocReg; }
837   bool isCoprocOption() const { return Kind == k_CoprocOption; }
838   bool isCondCode() const { return Kind == k_CondCode; }
839   bool isCCOut() const { return Kind == k_CCOut; }
840   bool isITMask() const { return Kind == k_ITCondMask; }
841   bool isITCondCode() const { return Kind == k_CondCode; }
842   bool isImm() const override {
843     return Kind == k_Immediate;
844   }
845 
846   bool isARMBranchTarget() const {
847     if (!isImm()) return false;
848 
849     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()))
850       return CE->getValue() % 4 == 0;
851     return true;
852   }
853 
854 
855   bool isThumbBranchTarget() const {
856     if (!isImm()) return false;
857 
858     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()))
859       return CE->getValue() % 2 == 0;
860     return true;
861   }
862 
863   // checks whether this operand is an unsigned offset which fits is a field
864   // of specified width and scaled by a specific number of bits
865   template<unsigned width, unsigned scale>
866   bool isUnsignedOffset() const {
867     if (!isImm()) return false;
868     if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
869     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
870       int64_t Val = CE->getValue();
871       int64_t Align = 1LL << scale;
872       int64_t Max = Align * ((1LL << width) - 1);
873       return ((Val % Align) == 0) && (Val >= 0) && (Val <= Max);
874     }
875     return false;
876   }
877   // checks whether this operand is an signed offset which fits is a field
878   // of specified width and scaled by a specific number of bits
879   template<unsigned width, unsigned scale>
880   bool isSignedOffset() const {
881     if (!isImm()) return false;
882     if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
883     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
884       int64_t Val = CE->getValue();
885       int64_t Align = 1LL << scale;
886       int64_t Max = Align * ((1LL << (width-1)) - 1);
887       int64_t Min = -Align * (1LL << (width-1));
888       return ((Val % Align) == 0) && (Val >= Min) && (Val <= Max);
889     }
890     return false;
891   }
892 
893   // checks whether this operand is a memory operand computed as an offset
894   // applied to PC. the offset may have 8 bits of magnitude and is represented
895   // with two bits of shift. textually it may be either [pc, #imm], #imm or
896   // relocable expression...
897   bool isThumbMemPC() const {
898     int64_t Val = 0;
899     if (isImm()) {
900       if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
901       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val);
902       if (!CE) return false;
903       Val = CE->getValue();
904     }
905     else if (isMem()) {
906       if(!Memory.OffsetImm || Memory.OffsetRegNum) return false;
907       if(Memory.BaseRegNum != ARM::PC) return false;
908       Val = Memory.OffsetImm->getValue();
909     }
910     else return false;
911     return ((Val % 4) == 0) && (Val >= 0) && (Val <= 1020);
912   }
913   bool isFPImm() const {
914     if (!isImm()) return false;
915     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
916     if (!CE) return false;
917     int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
918     return Val != -1;
919   }
920   bool isFBits16() const {
921     if (!isImm()) return false;
922     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
923     if (!CE) return false;
924     int64_t Value = CE->getValue();
925     return Value >= 0 && Value <= 16;
926   }
927   bool isFBits32() const {
928     if (!isImm()) return false;
929     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
930     if (!CE) return false;
931     int64_t Value = CE->getValue();
932     return Value >= 1 && Value <= 32;
933   }
934   bool isImm8s4() const {
935     if (!isImm()) return false;
936     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
937     if (!CE) return false;
938     int64_t Value = CE->getValue();
939     return ((Value & 3) == 0) && Value >= -1020 && Value <= 1020;
940   }
941   bool isImm0_1020s4() const {
942     if (!isImm()) return false;
943     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
944     if (!CE) return false;
945     int64_t Value = CE->getValue();
946     return ((Value & 3) == 0) && Value >= 0 && Value <= 1020;
947   }
948   bool isImm0_508s4() const {
949     if (!isImm()) return false;
950     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
951     if (!CE) return false;
952     int64_t Value = CE->getValue();
953     return ((Value & 3) == 0) && Value >= 0 && Value <= 508;
954   }
955   bool isImm0_508s4Neg() const {
956     if (!isImm()) return false;
957     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
958     if (!CE) return false;
959     int64_t Value = -CE->getValue();
960     // explicitly exclude zero. we want that to use the normal 0_508 version.
961     return ((Value & 3) == 0) && Value > 0 && Value <= 508;
962   }
963   bool isImm0_239() const {
964     if (!isImm()) return false;
965     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
966     if (!CE) return false;
967     int64_t Value = CE->getValue();
968     return Value >= 0 && Value < 240;
969   }
970   bool isImm0_255() const {
971     if (!isImm()) return false;
972     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
973     if (!CE) return false;
974     int64_t Value = CE->getValue();
975     return Value >= 0 && Value < 256;
976   }
977   bool isImm0_4095() const {
978     if (!isImm()) return false;
979     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
980     if (!CE) return false;
981     int64_t Value = CE->getValue();
982     return Value >= 0 && Value < 4096;
983   }
984   bool isImm0_4095Neg() const {
985     if (!isImm()) return false;
986     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
987     if (!CE) return false;
988     int64_t Value = -CE->getValue();
989     return Value > 0 && Value < 4096;
990   }
991   bool isImm0_1() const {
992     if (!isImm()) return false;
993     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
994     if (!CE) return false;
995     int64_t Value = CE->getValue();
996     return Value >= 0 && Value < 2;
997   }
998   bool isImm0_3() const {
999     if (!isImm()) return false;
1000     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1001     if (!CE) return false;
1002     int64_t Value = CE->getValue();
1003     return Value >= 0 && Value < 4;
1004   }
1005   bool isImm0_7() const {
1006     if (!isImm()) return false;
1007     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1008     if (!CE) return false;
1009     int64_t Value = CE->getValue();
1010     return Value >= 0 && Value < 8;
1011   }
1012   bool isImm0_15() const {
1013     if (!isImm()) return false;
1014     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1015     if (!CE) return false;
1016     int64_t Value = CE->getValue();
1017     return Value >= 0 && Value < 16;
1018   }
1019   bool isImm0_31() const {
1020     if (!isImm()) return false;
1021     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1022     if (!CE) return false;
1023     int64_t Value = CE->getValue();
1024     return Value >= 0 && Value < 32;
1025   }
1026   bool isImm0_63() const {
1027     if (!isImm()) return false;
1028     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1029     if (!CE) return false;
1030     int64_t Value = CE->getValue();
1031     return Value >= 0 && Value < 64;
1032   }
1033   bool isImm8() const {
1034     if (!isImm()) return false;
1035     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1036     if (!CE) return false;
1037     int64_t Value = CE->getValue();
1038     return Value == 8;
1039   }
1040   bool isImm16() const {
1041     if (!isImm()) return false;
1042     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1043     if (!CE) return false;
1044     int64_t Value = CE->getValue();
1045     return Value == 16;
1046   }
1047   bool isImm32() const {
1048     if (!isImm()) return false;
1049     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1050     if (!CE) return false;
1051     int64_t Value = CE->getValue();
1052     return Value == 32;
1053   }
1054   bool isShrImm8() const {
1055     if (!isImm()) return false;
1056     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1057     if (!CE) return false;
1058     int64_t Value = CE->getValue();
1059     return Value > 0 && Value <= 8;
1060   }
1061   bool isShrImm16() const {
1062     if (!isImm()) return false;
1063     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1064     if (!CE) return false;
1065     int64_t Value = CE->getValue();
1066     return Value > 0 && Value <= 16;
1067   }
1068   bool isShrImm32() const {
1069     if (!isImm()) return false;
1070     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1071     if (!CE) return false;
1072     int64_t Value = CE->getValue();
1073     return Value > 0 && Value <= 32;
1074   }
1075   bool isShrImm64() const {
1076     if (!isImm()) return false;
1077     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1078     if (!CE) return false;
1079     int64_t Value = CE->getValue();
1080     return Value > 0 && Value <= 64;
1081   }
1082   bool isImm1_7() const {
1083     if (!isImm()) return false;
1084     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1085     if (!CE) return false;
1086     int64_t Value = CE->getValue();
1087     return Value > 0 && Value < 8;
1088   }
1089   bool isImm1_15() const {
1090     if (!isImm()) return false;
1091     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1092     if (!CE) return false;
1093     int64_t Value = CE->getValue();
1094     return Value > 0 && Value < 16;
1095   }
1096   bool isImm1_31() const {
1097     if (!isImm()) return false;
1098     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1099     if (!CE) return false;
1100     int64_t Value = CE->getValue();
1101     return Value > 0 && Value < 32;
1102   }
1103   bool isImm1_16() const {
1104     if (!isImm()) return false;
1105     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1106     if (!CE) return false;
1107     int64_t Value = CE->getValue();
1108     return Value > 0 && Value < 17;
1109   }
1110   bool isImm1_32() const {
1111     if (!isImm()) return false;
1112     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1113     if (!CE) return false;
1114     int64_t Value = CE->getValue();
1115     return Value > 0 && Value < 33;
1116   }
1117   bool isImm0_32() const {
1118     if (!isImm()) return false;
1119     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1120     if (!CE) return false;
1121     int64_t Value = CE->getValue();
1122     return Value >= 0 && Value < 33;
1123   }
1124   bool isImm0_65535() const {
1125     if (!isImm()) return false;
1126     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1127     if (!CE) return false;
1128     int64_t Value = CE->getValue();
1129     return Value >= 0 && Value < 65536;
1130   }
1131   bool isImm256_65535Expr() const {
1132     if (!isImm()) return false;
1133     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1134     // If it's not a constant expression, it'll generate a fixup and be
1135     // handled later.
1136     if (!CE) return true;
1137     int64_t Value = CE->getValue();
1138     return Value >= 256 && Value < 65536;
1139   }
1140   bool isImm0_65535Expr() const {
1141     if (!isImm()) return false;
1142     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1143     // If it's not a constant expression, it'll generate a fixup and be
1144     // handled later.
1145     if (!CE) return true;
1146     int64_t Value = CE->getValue();
1147     return Value >= 0 && Value < 65536;
1148   }
1149   bool isImm24bit() const {
1150     if (!isImm()) return false;
1151     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1152     if (!CE) return false;
1153     int64_t Value = CE->getValue();
1154     return Value >= 0 && Value <= 0xffffff;
1155   }
1156   bool isImmThumbSR() const {
1157     if (!isImm()) return false;
1158     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1159     if (!CE) return false;
1160     int64_t Value = CE->getValue();
1161     return Value > 0 && Value < 33;
1162   }
1163   bool isPKHLSLImm() const {
1164     if (!isImm()) return false;
1165     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1166     if (!CE) return false;
1167     int64_t Value = CE->getValue();
1168     return Value >= 0 && Value < 32;
1169   }
1170   bool isPKHASRImm() const {
1171     if (!isImm()) return false;
1172     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1173     if (!CE) return false;
1174     int64_t Value = CE->getValue();
1175     return Value > 0 && Value <= 32;
1176   }
1177   bool isAdrLabel() const {
1178     // If we have an immediate that's not a constant, treat it as a label
1179     // reference needing a fixup.
1180     if (isImm() && !isa<MCConstantExpr>(getImm()))
1181       return true;
1182 
1183     // If it is a constant, it must fit into a modified immediate encoding.
1184     if (!isImm()) return false;
1185     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1186     if (!CE) return false;
1187     int64_t Value = CE->getValue();
1188     return (ARM_AM::getSOImmVal(Value) != -1 ||
1189             ARM_AM::getSOImmVal(-Value) != -1);
1190   }
1191   bool isT2SOImm() const {
1192     if (!isImm()) return false;
1193     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1194     if (!CE) return false;
1195     int64_t Value = CE->getValue();
1196     return ARM_AM::getT2SOImmVal(Value) != -1;
1197   }
1198   bool isT2SOImmNot() const {
1199     if (!isImm()) return false;
1200     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1201     if (!CE) return false;
1202     int64_t Value = CE->getValue();
1203     return ARM_AM::getT2SOImmVal(Value) == -1 &&
1204       ARM_AM::getT2SOImmVal(~Value) != -1;
1205   }
1206   bool isT2SOImmNeg() const {
1207     if (!isImm()) return false;
1208     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1209     if (!CE) return false;
1210     int64_t Value = CE->getValue();
1211     // Only use this when not representable as a plain so_imm.
1212     return ARM_AM::getT2SOImmVal(Value) == -1 &&
1213       ARM_AM::getT2SOImmVal(-Value) != -1;
1214   }
1215   bool isSetEndImm() const {
1216     if (!isImm()) return false;
1217     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1218     if (!CE) return false;
1219     int64_t Value = CE->getValue();
1220     return Value == 1 || Value == 0;
1221   }
1222   bool isReg() const override { return Kind == k_Register; }
1223   bool isRegList() const { return Kind == k_RegisterList; }
1224   bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
1225   bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
1226   bool isToken() const override { return Kind == k_Token; }
1227   bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
1228   bool isInstSyncBarrierOpt() const { return Kind == k_InstSyncBarrierOpt; }
1229   bool isMem() const override { return Kind == k_Memory; }
1230   bool isShifterImm() const { return Kind == k_ShifterImmediate; }
1231   bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
1232   bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
1233   bool isRotImm() const { return Kind == k_RotateImmediate; }
1234   bool isModImm() const { return Kind == k_ModifiedImmediate; }
1235   bool isModImmNot() const {
1236     if (!isImm()) return false;
1237     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1238     if (!CE) return false;
1239     int64_t Value = CE->getValue();
1240     return ARM_AM::getSOImmVal(~Value) != -1;
1241   }
1242   bool isModImmNeg() const {
1243     if (!isImm()) return false;
1244     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1245     if (!CE) return false;
1246     int64_t Value = CE->getValue();
1247     return ARM_AM::getSOImmVal(Value) == -1 &&
1248       ARM_AM::getSOImmVal(-Value) != -1;
1249   }
1250   bool isConstantPoolImm() const { return Kind == k_ConstantPoolImmediate; }
1251   bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
1252   bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
1253   bool isPostIdxReg() const {
1254     return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy ==ARM_AM::no_shift;
1255   }
1256   bool isMemNoOffset(bool alignOK = false, unsigned Alignment = 0) const {
1257     if (!isMem())
1258       return false;
1259     // No offset of any kind.
1260     return Memory.OffsetRegNum == 0 && Memory.OffsetImm == nullptr &&
1261      (alignOK || Memory.Alignment == Alignment);
1262   }
1263   bool isMemPCRelImm12() const {
1264     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1265       return false;
1266     // Base register must be PC.
1267     if (Memory.BaseRegNum != ARM::PC)
1268       return false;
1269     // Immediate offset in range [-4095, 4095].
1270     if (!Memory.OffsetImm) return true;
1271     int64_t Val = Memory.OffsetImm->getValue();
1272     return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
1273   }
1274   bool isAlignedMemory() const {
1275     return isMemNoOffset(true);
1276   }
1277   bool isAlignedMemoryNone() const {
1278     return isMemNoOffset(false, 0);
1279   }
1280   bool isDupAlignedMemoryNone() const {
1281     return isMemNoOffset(false, 0);
1282   }
1283   bool isAlignedMemory16() const {
1284     if (isMemNoOffset(false, 2)) // alignment in bytes for 16-bits is 2.
1285       return true;
1286     return isMemNoOffset(false, 0);
1287   }
1288   bool isDupAlignedMemory16() const {
1289     if (isMemNoOffset(false, 2)) // alignment in bytes for 16-bits is 2.
1290       return true;
1291     return isMemNoOffset(false, 0);
1292   }
1293   bool isAlignedMemory32() const {
1294     if (isMemNoOffset(false, 4)) // alignment in bytes for 32-bits is 4.
1295       return true;
1296     return isMemNoOffset(false, 0);
1297   }
1298   bool isDupAlignedMemory32() const {
1299     if (isMemNoOffset(false, 4)) // alignment in bytes for 32-bits is 4.
1300       return true;
1301     return isMemNoOffset(false, 0);
1302   }
1303   bool isAlignedMemory64() const {
1304     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1305       return true;
1306     return isMemNoOffset(false, 0);
1307   }
1308   bool isDupAlignedMemory64() const {
1309     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1310       return true;
1311     return isMemNoOffset(false, 0);
1312   }
1313   bool isAlignedMemory64or128() const {
1314     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1315       return true;
1316     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1317       return true;
1318     return isMemNoOffset(false, 0);
1319   }
1320   bool isDupAlignedMemory64or128() const {
1321     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1322       return true;
1323     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1324       return true;
1325     return isMemNoOffset(false, 0);
1326   }
1327   bool isAlignedMemory64or128or256() const {
1328     if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1329       return true;
1330     if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1331       return true;
1332     if (isMemNoOffset(false, 32)) // alignment in bytes for 256-bits is 32.
1333       return true;
1334     return isMemNoOffset(false, 0);
1335   }
1336   bool isAddrMode2() const {
1337     if (!isMem() || Memory.Alignment != 0) return false;
1338     // Check for register offset.
1339     if (Memory.OffsetRegNum) return true;
1340     // Immediate offset in range [-4095, 4095].
1341     if (!Memory.OffsetImm) return true;
1342     int64_t Val = Memory.OffsetImm->getValue();
1343     return Val > -4096 && Val < 4096;
1344   }
1345   bool isAM2OffsetImm() const {
1346     if (!isImm()) return false;
1347     // Immediate offset in range [-4095, 4095].
1348     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1349     if (!CE) return false;
1350     int64_t Val = CE->getValue();
1351     return (Val == INT32_MIN) || (Val > -4096 && Val < 4096);
1352   }
1353   bool isAddrMode3() const {
1354     // If we have an immediate that's not a constant, treat it as a label
1355     // reference needing a fixup. If it is a constant, it's something else
1356     // and we reject it.
1357     if (isImm() && !isa<MCConstantExpr>(getImm()))
1358       return true;
1359     if (!isMem() || Memory.Alignment != 0) return false;
1360     // No shifts are legal for AM3.
1361     if (Memory.ShiftType != ARM_AM::no_shift) return false;
1362     // Check for register offset.
1363     if (Memory.OffsetRegNum) return true;
1364     // Immediate offset in range [-255, 255].
1365     if (!Memory.OffsetImm) return true;
1366     int64_t Val = Memory.OffsetImm->getValue();
1367     // The #-0 offset is encoded as INT32_MIN, and we have to check
1368     // for this too.
1369     return (Val > -256 && Val < 256) || Val == INT32_MIN;
1370   }
1371   bool isAM3Offset() const {
1372     if (Kind != k_Immediate && Kind != k_PostIndexRegister)
1373       return false;
1374     if (Kind == k_PostIndexRegister)
1375       return PostIdxReg.ShiftTy == ARM_AM::no_shift;
1376     // Immediate offset in range [-255, 255].
1377     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1378     if (!CE) return false;
1379     int64_t Val = CE->getValue();
1380     // Special case, #-0 is INT32_MIN.
1381     return (Val > -256 && Val < 256) || Val == INT32_MIN;
1382   }
1383   bool isAddrMode5() const {
1384     // If we have an immediate that's not a constant, treat it as a label
1385     // reference needing a fixup. If it is a constant, it's something else
1386     // and we reject it.
1387     if (isImm() && !isa<MCConstantExpr>(getImm()))
1388       return true;
1389     if (!isMem() || Memory.Alignment != 0) return false;
1390     // Check for register offset.
1391     if (Memory.OffsetRegNum) return false;
1392     // Immediate offset in range [-1020, 1020] and a multiple of 4.
1393     if (!Memory.OffsetImm) return true;
1394     int64_t Val = Memory.OffsetImm->getValue();
1395     return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
1396       Val == INT32_MIN;
1397   }
1398   bool isAddrMode5FP16() const {
1399     // If we have an immediate that's not a constant, treat it as a label
1400     // reference needing a fixup. If it is a constant, it's something else
1401     // and we reject it.
1402     if (isImm() && !isa<MCConstantExpr>(getImm()))
1403       return true;
1404     if (!isMem() || Memory.Alignment != 0) return false;
1405     // Check for register offset.
1406     if (Memory.OffsetRegNum) return false;
1407     // Immediate offset in range [-510, 510] and a multiple of 2.
1408     if (!Memory.OffsetImm) return true;
1409     int64_t Val = Memory.OffsetImm->getValue();
1410     return (Val >= -510 && Val <= 510 && ((Val & 1) == 0)) || Val == INT32_MIN;
1411   }
1412   bool isMemTBB() const {
1413     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1414         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
1415       return false;
1416     return true;
1417   }
1418   bool isMemTBH() const {
1419     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1420         Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
1421         Memory.Alignment != 0 )
1422       return false;
1423     return true;
1424   }
1425   bool isMemRegOffset() const {
1426     if (!isMem() || !Memory.OffsetRegNum || Memory.Alignment != 0)
1427       return false;
1428     return true;
1429   }
1430   bool isT2MemRegOffset() const {
1431     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1432         Memory.Alignment != 0 || Memory.BaseRegNum == ARM::PC)
1433       return false;
1434     // Only lsl #{0, 1, 2, 3} allowed.
1435     if (Memory.ShiftType == ARM_AM::no_shift)
1436       return true;
1437     if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
1438       return false;
1439     return true;
1440   }
1441   bool isMemThumbRR() const {
1442     // Thumb reg+reg addressing is simple. Just two registers, a base and
1443     // an offset. No shifts, negations or any other complicating factors.
1444     if (!isMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1445         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
1446       return false;
1447     return isARMLowRegister(Memory.BaseRegNum) &&
1448       (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
1449   }
1450   bool isMemThumbRIs4() const {
1451     if (!isMem() || Memory.OffsetRegNum != 0 ||
1452         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1453       return false;
1454     // Immediate offset, multiple of 4 in range [0, 124].
1455     if (!Memory.OffsetImm) return true;
1456     int64_t Val = Memory.OffsetImm->getValue();
1457     return Val >= 0 && Val <= 124 && (Val % 4) == 0;
1458   }
1459   bool isMemThumbRIs2() const {
1460     if (!isMem() || Memory.OffsetRegNum != 0 ||
1461         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1462       return false;
1463     // Immediate offset, multiple of 4 in range [0, 62].
1464     if (!Memory.OffsetImm) return true;
1465     int64_t Val = Memory.OffsetImm->getValue();
1466     return Val >= 0 && Val <= 62 && (Val % 2) == 0;
1467   }
1468   bool isMemThumbRIs1() const {
1469     if (!isMem() || Memory.OffsetRegNum != 0 ||
1470         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1471       return false;
1472     // Immediate offset in range [0, 31].
1473     if (!Memory.OffsetImm) return true;
1474     int64_t Val = Memory.OffsetImm->getValue();
1475     return Val >= 0 && Val <= 31;
1476   }
1477   bool isMemThumbSPI() const {
1478     if (!isMem() || Memory.OffsetRegNum != 0 ||
1479         Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
1480       return false;
1481     // Immediate offset, multiple of 4 in range [0, 1020].
1482     if (!Memory.OffsetImm) return true;
1483     int64_t Val = Memory.OffsetImm->getValue();
1484     return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
1485   }
1486   bool isMemImm8s4Offset() const {
1487     // If we have an immediate that's not a constant, treat it as a label
1488     // reference needing a fixup. If it is a constant, it's something else
1489     // and we reject it.
1490     if (isImm() && !isa<MCConstantExpr>(getImm()))
1491       return true;
1492     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1493       return false;
1494     // Immediate offset a multiple of 4 in range [-1020, 1020].
1495     if (!Memory.OffsetImm) return true;
1496     int64_t Val = Memory.OffsetImm->getValue();
1497     // Special case, #-0 is INT32_MIN.
1498     return (Val >= -1020 && Val <= 1020 && (Val & 3) == 0) || Val == INT32_MIN;
1499   }
1500   bool isMemImm0_1020s4Offset() const {
1501     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1502       return false;
1503     // Immediate offset a multiple of 4 in range [0, 1020].
1504     if (!Memory.OffsetImm) return true;
1505     int64_t Val = Memory.OffsetImm->getValue();
1506     return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
1507   }
1508   bool isMemImm8Offset() const {
1509     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1510       return false;
1511     // Base reg of PC isn't allowed for these encodings.
1512     if (Memory.BaseRegNum == ARM::PC) return false;
1513     // Immediate offset in range [-255, 255].
1514     if (!Memory.OffsetImm) return true;
1515     int64_t Val = Memory.OffsetImm->getValue();
1516     return (Val == INT32_MIN) || (Val > -256 && Val < 256);
1517   }
1518   bool isMemPosImm8Offset() const {
1519     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1520       return false;
1521     // Immediate offset in range [0, 255].
1522     if (!Memory.OffsetImm) return true;
1523     int64_t Val = Memory.OffsetImm->getValue();
1524     return Val >= 0 && Val < 256;
1525   }
1526   bool isMemNegImm8Offset() const {
1527     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1528       return false;
1529     // Base reg of PC isn't allowed for these encodings.
1530     if (Memory.BaseRegNum == ARM::PC) return false;
1531     // Immediate offset in range [-255, -1].
1532     if (!Memory.OffsetImm) return false;
1533     int64_t Val = Memory.OffsetImm->getValue();
1534     return (Val == INT32_MIN) || (Val > -256 && Val < 0);
1535   }
1536   bool isMemUImm12Offset() const {
1537     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1538       return false;
1539     // Immediate offset in range [0, 4095].
1540     if (!Memory.OffsetImm) return true;
1541     int64_t Val = Memory.OffsetImm->getValue();
1542     return (Val >= 0 && Val < 4096);
1543   }
1544   bool isMemImm12Offset() const {
1545     // If we have an immediate that's not a constant, treat it as a label
1546     // reference needing a fixup. If it is a constant, it's something else
1547     // and we reject it.
1548 
1549     if (isImm() && !isa<MCConstantExpr>(getImm()))
1550       return true;
1551 
1552     if (!isMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1553       return false;
1554     // Immediate offset in range [-4095, 4095].
1555     if (!Memory.OffsetImm) return true;
1556     int64_t Val = Memory.OffsetImm->getValue();
1557     return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
1558   }
1559   bool isConstPoolAsmImm() const {
1560     // Delay processing of Constant Pool Immediate, this will turn into
1561     // a constant. Match no other operand
1562     return (isConstantPoolImm());
1563   }
1564   bool isPostIdxImm8() const {
1565     if (!isImm()) return false;
1566     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1567     if (!CE) return false;
1568     int64_t Val = CE->getValue();
1569     return (Val > -256 && Val < 256) || (Val == INT32_MIN);
1570   }
1571   bool isPostIdxImm8s4() const {
1572     if (!isImm()) return false;
1573     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1574     if (!CE) return false;
1575     int64_t Val = CE->getValue();
1576     return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
1577       (Val == INT32_MIN);
1578   }
1579 
1580   bool isMSRMask() const { return Kind == k_MSRMask; }
1581   bool isBankedReg() const { return Kind == k_BankedReg; }
1582   bool isProcIFlags() const { return Kind == k_ProcIFlags; }
1583 
1584   // NEON operands.
1585   bool isSingleSpacedVectorList() const {
1586     return Kind == k_VectorList && !VectorList.isDoubleSpaced;
1587   }
1588   bool isDoubleSpacedVectorList() const {
1589     return Kind == k_VectorList && VectorList.isDoubleSpaced;
1590   }
1591   bool isVecListOneD() const {
1592     if (!isSingleSpacedVectorList()) return false;
1593     return VectorList.Count == 1;
1594   }
1595 
1596   bool isVecListDPair() const {
1597     if (!isSingleSpacedVectorList()) return false;
1598     return (ARMMCRegisterClasses[ARM::DPairRegClassID]
1599               .contains(VectorList.RegNum));
1600   }
1601 
1602   bool isVecListThreeD() const {
1603     if (!isSingleSpacedVectorList()) return false;
1604     return VectorList.Count == 3;
1605   }
1606 
1607   bool isVecListFourD() const {
1608     if (!isSingleSpacedVectorList()) return false;
1609     return VectorList.Count == 4;
1610   }
1611 
1612   bool isVecListDPairSpaced() const {
1613     if (Kind != k_VectorList) return false;
1614     if (isSingleSpacedVectorList()) return false;
1615     return (ARMMCRegisterClasses[ARM::DPairSpcRegClassID]
1616               .contains(VectorList.RegNum));
1617   }
1618 
1619   bool isVecListThreeQ() const {
1620     if (!isDoubleSpacedVectorList()) return false;
1621     return VectorList.Count == 3;
1622   }
1623 
1624   bool isVecListFourQ() const {
1625     if (!isDoubleSpacedVectorList()) return false;
1626     return VectorList.Count == 4;
1627   }
1628 
1629   bool isSingleSpacedVectorAllLanes() const {
1630     return Kind == k_VectorListAllLanes && !VectorList.isDoubleSpaced;
1631   }
1632   bool isDoubleSpacedVectorAllLanes() const {
1633     return Kind == k_VectorListAllLanes && VectorList.isDoubleSpaced;
1634   }
1635   bool isVecListOneDAllLanes() const {
1636     if (!isSingleSpacedVectorAllLanes()) return false;
1637     return VectorList.Count == 1;
1638   }
1639 
1640   bool isVecListDPairAllLanes() const {
1641     if (!isSingleSpacedVectorAllLanes()) return false;
1642     return (ARMMCRegisterClasses[ARM::DPairRegClassID]
1643               .contains(VectorList.RegNum));
1644   }
1645 
1646   bool isVecListDPairSpacedAllLanes() const {
1647     if (!isDoubleSpacedVectorAllLanes()) return false;
1648     return VectorList.Count == 2;
1649   }
1650 
1651   bool isVecListThreeDAllLanes() const {
1652     if (!isSingleSpacedVectorAllLanes()) return false;
1653     return VectorList.Count == 3;
1654   }
1655 
1656   bool isVecListThreeQAllLanes() const {
1657     if (!isDoubleSpacedVectorAllLanes()) return false;
1658     return VectorList.Count == 3;
1659   }
1660 
1661   bool isVecListFourDAllLanes() const {
1662     if (!isSingleSpacedVectorAllLanes()) return false;
1663     return VectorList.Count == 4;
1664   }
1665 
1666   bool isVecListFourQAllLanes() const {
1667     if (!isDoubleSpacedVectorAllLanes()) return false;
1668     return VectorList.Count == 4;
1669   }
1670 
1671   bool isSingleSpacedVectorIndexed() const {
1672     return Kind == k_VectorListIndexed && !VectorList.isDoubleSpaced;
1673   }
1674   bool isDoubleSpacedVectorIndexed() const {
1675     return Kind == k_VectorListIndexed && VectorList.isDoubleSpaced;
1676   }
1677   bool isVecListOneDByteIndexed() const {
1678     if (!isSingleSpacedVectorIndexed()) return false;
1679     return VectorList.Count == 1 && VectorList.LaneIndex <= 7;
1680   }
1681 
1682   bool isVecListOneDHWordIndexed() const {
1683     if (!isSingleSpacedVectorIndexed()) return false;
1684     return VectorList.Count == 1 && VectorList.LaneIndex <= 3;
1685   }
1686 
1687   bool isVecListOneDWordIndexed() const {
1688     if (!isSingleSpacedVectorIndexed()) return false;
1689     return VectorList.Count == 1 && VectorList.LaneIndex <= 1;
1690   }
1691 
1692   bool isVecListTwoDByteIndexed() const {
1693     if (!isSingleSpacedVectorIndexed()) return false;
1694     return VectorList.Count == 2 && VectorList.LaneIndex <= 7;
1695   }
1696 
1697   bool isVecListTwoDHWordIndexed() const {
1698     if (!isSingleSpacedVectorIndexed()) return false;
1699     return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
1700   }
1701 
1702   bool isVecListTwoQWordIndexed() const {
1703     if (!isDoubleSpacedVectorIndexed()) return false;
1704     return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
1705   }
1706 
1707   bool isVecListTwoQHWordIndexed() const {
1708     if (!isDoubleSpacedVectorIndexed()) return false;
1709     return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
1710   }
1711 
1712   bool isVecListTwoDWordIndexed() const {
1713     if (!isSingleSpacedVectorIndexed()) return false;
1714     return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
1715   }
1716 
1717   bool isVecListThreeDByteIndexed() const {
1718     if (!isSingleSpacedVectorIndexed()) return false;
1719     return VectorList.Count == 3 && VectorList.LaneIndex <= 7;
1720   }
1721 
1722   bool isVecListThreeDHWordIndexed() const {
1723     if (!isSingleSpacedVectorIndexed()) return false;
1724     return VectorList.Count == 3 && VectorList.LaneIndex <= 3;
1725   }
1726 
1727   bool isVecListThreeQWordIndexed() const {
1728     if (!isDoubleSpacedVectorIndexed()) return false;
1729     return VectorList.Count == 3 && VectorList.LaneIndex <= 1;
1730   }
1731 
1732   bool isVecListThreeQHWordIndexed() const {
1733     if (!isDoubleSpacedVectorIndexed()) return false;
1734     return VectorList.Count == 3 && VectorList.LaneIndex <= 3;
1735   }
1736 
1737   bool isVecListThreeDWordIndexed() const {
1738     if (!isSingleSpacedVectorIndexed()) return false;
1739     return VectorList.Count == 3 && VectorList.LaneIndex <= 1;
1740   }
1741 
1742   bool isVecListFourDByteIndexed() const {
1743     if (!isSingleSpacedVectorIndexed()) return false;
1744     return VectorList.Count == 4 && VectorList.LaneIndex <= 7;
1745   }
1746 
1747   bool isVecListFourDHWordIndexed() const {
1748     if (!isSingleSpacedVectorIndexed()) return false;
1749     return VectorList.Count == 4 && VectorList.LaneIndex <= 3;
1750   }
1751 
1752   bool isVecListFourQWordIndexed() const {
1753     if (!isDoubleSpacedVectorIndexed()) return false;
1754     return VectorList.Count == 4 && VectorList.LaneIndex <= 1;
1755   }
1756 
1757   bool isVecListFourQHWordIndexed() const {
1758     if (!isDoubleSpacedVectorIndexed()) return false;
1759     return VectorList.Count == 4 && VectorList.LaneIndex <= 3;
1760   }
1761 
1762   bool isVecListFourDWordIndexed() const {
1763     if (!isSingleSpacedVectorIndexed()) return false;
1764     return VectorList.Count == 4 && VectorList.LaneIndex <= 1;
1765   }
1766 
1767   bool isVectorIndex8() const {
1768     if (Kind != k_VectorIndex) return false;
1769     return VectorIndex.Val < 8;
1770   }
1771   bool isVectorIndex16() const {
1772     if (Kind != k_VectorIndex) return false;
1773     return VectorIndex.Val < 4;
1774   }
1775   bool isVectorIndex32() const {
1776     if (Kind != k_VectorIndex) return false;
1777     return VectorIndex.Val < 2;
1778   }
1779 
1780   bool isNEONi8splat() const {
1781     if (!isImm()) return false;
1782     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1783     // Must be a constant.
1784     if (!CE) return false;
1785     int64_t Value = CE->getValue();
1786     // i8 value splatted across 8 bytes. The immediate is just the 8 byte
1787     // value.
1788     return Value >= 0 && Value < 256;
1789   }
1790 
1791   bool isNEONi16splat() const {
1792     if (isNEONByteReplicate(2))
1793       return false; // Leave that for bytes replication and forbid by default.
1794     if (!isImm())
1795       return false;
1796     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1797     // Must be a constant.
1798     if (!CE) return false;
1799     unsigned Value = CE->getValue();
1800     return ARM_AM::isNEONi16splat(Value);
1801   }
1802 
1803   bool isNEONi16splatNot() const {
1804     if (!isImm())
1805       return false;
1806     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1807     // Must be a constant.
1808     if (!CE) return false;
1809     unsigned Value = CE->getValue();
1810     return ARM_AM::isNEONi16splat(~Value & 0xffff);
1811   }
1812 
1813   bool isNEONi32splat() const {
1814     if (isNEONByteReplicate(4))
1815       return false; // Leave that for bytes replication and forbid by default.
1816     if (!isImm())
1817       return false;
1818     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1819     // Must be a constant.
1820     if (!CE) return false;
1821     unsigned Value = CE->getValue();
1822     return ARM_AM::isNEONi32splat(Value);
1823   }
1824 
1825   bool isNEONi32splatNot() const {
1826     if (!isImm())
1827       return false;
1828     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1829     // Must be a constant.
1830     if (!CE) return false;
1831     unsigned Value = CE->getValue();
1832     return ARM_AM::isNEONi32splat(~Value);
1833   }
1834 
1835   bool isNEONByteReplicate(unsigned NumBytes) const {
1836     if (!isImm())
1837       return false;
1838     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1839     // Must be a constant.
1840     if (!CE)
1841       return false;
1842     int64_t Value = CE->getValue();
1843     if (!Value)
1844       return false; // Don't bother with zero.
1845 
1846     unsigned char B = Value & 0xff;
1847     for (unsigned i = 1; i < NumBytes; ++i) {
1848       Value >>= 8;
1849       if ((Value & 0xff) != B)
1850         return false;
1851     }
1852     return true;
1853   }
1854   bool isNEONi16ByteReplicate() const { return isNEONByteReplicate(2); }
1855   bool isNEONi32ByteReplicate() const { return isNEONByteReplicate(4); }
1856   bool isNEONi32vmov() const {
1857     if (isNEONByteReplicate(4))
1858       return false; // Let it to be classified as byte-replicate case.
1859     if (!isImm())
1860       return false;
1861     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1862     // Must be a constant.
1863     if (!CE)
1864       return false;
1865     int64_t Value = CE->getValue();
1866     // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1867     // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1868     // FIXME: This is probably wrong and a copy and paste from previous example
1869     return (Value >= 0 && Value < 256) ||
1870       (Value >= 0x0100 && Value <= 0xff00) ||
1871       (Value >= 0x010000 && Value <= 0xff0000) ||
1872       (Value >= 0x01000000 && Value <= 0xff000000) ||
1873       (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1874       (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1875   }
1876   bool isNEONi32vmovNeg() const {
1877     if (!isImm()) return false;
1878     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1879     // Must be a constant.
1880     if (!CE) return false;
1881     int64_t Value = ~CE->getValue();
1882     // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1883     // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1884     // FIXME: This is probably wrong and a copy and paste from previous example
1885     return (Value >= 0 && Value < 256) ||
1886       (Value >= 0x0100 && Value <= 0xff00) ||
1887       (Value >= 0x010000 && Value <= 0xff0000) ||
1888       (Value >= 0x01000000 && Value <= 0xff000000) ||
1889       (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1890       (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1891   }
1892 
1893   bool isNEONi64splat() const {
1894     if (!isImm()) return false;
1895     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1896     // Must be a constant.
1897     if (!CE) return false;
1898     uint64_t Value = CE->getValue();
1899     // i64 value with each byte being either 0 or 0xff.
1900     for (unsigned i = 0; i < 8; ++i, Value >>= 8)
1901       if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
1902     return true;
1903   }
1904 
1905   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
1906     // Add as immediates when possible.  Null MCExpr = 0.
1907     if (!Expr)
1908       Inst.addOperand(MCOperand::createImm(0));
1909     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
1910       Inst.addOperand(MCOperand::createImm(CE->getValue()));
1911     else
1912       Inst.addOperand(MCOperand::createExpr(Expr));
1913   }
1914 
1915   void addARMBranchTargetOperands(MCInst &Inst, unsigned N) const {
1916     assert(N == 1 && "Invalid number of operands!");
1917     addExpr(Inst, getImm());
1918   }
1919 
1920   void addThumbBranchTargetOperands(MCInst &Inst, unsigned N) const {
1921     assert(N == 1 && "Invalid number of operands!");
1922     addExpr(Inst, getImm());
1923   }
1924 
1925   void addCondCodeOperands(MCInst &Inst, unsigned N) const {
1926     assert(N == 2 && "Invalid number of operands!");
1927     Inst.addOperand(MCOperand::createImm(unsigned(getCondCode())));
1928     unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
1929     Inst.addOperand(MCOperand::createReg(RegNum));
1930   }
1931 
1932   void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
1933     assert(N == 1 && "Invalid number of operands!");
1934     Inst.addOperand(MCOperand::createImm(getCoproc()));
1935   }
1936 
1937   void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
1938     assert(N == 1 && "Invalid number of operands!");
1939     Inst.addOperand(MCOperand::createImm(getCoproc()));
1940   }
1941 
1942   void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
1943     assert(N == 1 && "Invalid number of operands!");
1944     Inst.addOperand(MCOperand::createImm(CoprocOption.Val));
1945   }
1946 
1947   void addITMaskOperands(MCInst &Inst, unsigned N) const {
1948     assert(N == 1 && "Invalid number of operands!");
1949     Inst.addOperand(MCOperand::createImm(ITMask.Mask));
1950   }
1951 
1952   void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
1953     assert(N == 1 && "Invalid number of operands!");
1954     Inst.addOperand(MCOperand::createImm(unsigned(getCondCode())));
1955   }
1956 
1957   void addCCOutOperands(MCInst &Inst, unsigned N) const {
1958     assert(N == 1 && "Invalid number of operands!");
1959     Inst.addOperand(MCOperand::createReg(getReg()));
1960   }
1961 
1962   void addRegOperands(MCInst &Inst, unsigned N) const {
1963     assert(N == 1 && "Invalid number of operands!");
1964     Inst.addOperand(MCOperand::createReg(getReg()));
1965   }
1966 
1967   void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
1968     assert(N == 3 && "Invalid number of operands!");
1969     assert(isRegShiftedReg() &&
1970            "addRegShiftedRegOperands() on non-RegShiftedReg!");
1971     Inst.addOperand(MCOperand::createReg(RegShiftedReg.SrcReg));
1972     Inst.addOperand(MCOperand::createReg(RegShiftedReg.ShiftReg));
1973     Inst.addOperand(MCOperand::createImm(
1974       ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
1975   }
1976 
1977   void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
1978     assert(N == 2 && "Invalid number of operands!");
1979     assert(isRegShiftedImm() &&
1980            "addRegShiftedImmOperands() on non-RegShiftedImm!");
1981     Inst.addOperand(MCOperand::createReg(RegShiftedImm.SrcReg));
1982     // Shift of #32 is encoded as 0 where permitted
1983     unsigned Imm = (RegShiftedImm.ShiftImm == 32 ? 0 : RegShiftedImm.ShiftImm);
1984     Inst.addOperand(MCOperand::createImm(
1985       ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, Imm)));
1986   }
1987 
1988   void addShifterImmOperands(MCInst &Inst, unsigned N) const {
1989     assert(N == 1 && "Invalid number of operands!");
1990     Inst.addOperand(MCOperand::createImm((ShifterImm.isASR << 5) |
1991                                          ShifterImm.Imm));
1992   }
1993 
1994   void addRegListOperands(MCInst &Inst, unsigned N) const {
1995     assert(N == 1 && "Invalid number of operands!");
1996     const SmallVectorImpl<unsigned> &RegList = getRegList();
1997     for (SmallVectorImpl<unsigned>::const_iterator
1998            I = RegList.begin(), E = RegList.end(); I != E; ++I)
1999       Inst.addOperand(MCOperand::createReg(*I));
2000   }
2001 
2002   void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
2003     addRegListOperands(Inst, N);
2004   }
2005 
2006   void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
2007     addRegListOperands(Inst, N);
2008   }
2009 
2010   void addRotImmOperands(MCInst &Inst, unsigned N) const {
2011     assert(N == 1 && "Invalid number of operands!");
2012     // Encoded as val>>3. The printer handles display as 8, 16, 24.
2013     Inst.addOperand(MCOperand::createImm(RotImm.Imm >> 3));
2014   }
2015 
2016   void addModImmOperands(MCInst &Inst, unsigned N) const {
2017     assert(N == 1 && "Invalid number of operands!");
2018 
2019     // Support for fixups (MCFixup)
2020     if (isImm())
2021       return addImmOperands(Inst, N);
2022 
2023     Inst.addOperand(MCOperand::createImm(ModImm.Bits | (ModImm.Rot << 7)));
2024   }
2025 
2026   void addModImmNotOperands(MCInst &Inst, unsigned N) const {
2027     assert(N == 1 && "Invalid number of operands!");
2028     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2029     uint32_t Enc = ARM_AM::getSOImmVal(~CE->getValue());
2030     Inst.addOperand(MCOperand::createImm(Enc));
2031   }
2032 
2033   void addModImmNegOperands(MCInst &Inst, unsigned N) const {
2034     assert(N == 1 && "Invalid number of operands!");
2035     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2036     uint32_t Enc = ARM_AM::getSOImmVal(-CE->getValue());
2037     Inst.addOperand(MCOperand::createImm(Enc));
2038   }
2039 
2040   void addBitfieldOperands(MCInst &Inst, unsigned N) const {
2041     assert(N == 1 && "Invalid number of operands!");
2042     // Munge the lsb/width into a bitfield mask.
2043     unsigned lsb = Bitfield.LSB;
2044     unsigned width = Bitfield.Width;
2045     // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
2046     uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
2047                       (32 - (lsb + width)));
2048     Inst.addOperand(MCOperand::createImm(Mask));
2049   }
2050 
2051   void addImmOperands(MCInst &Inst, unsigned N) const {
2052     assert(N == 1 && "Invalid number of operands!");
2053     addExpr(Inst, getImm());
2054   }
2055 
2056   void addFBits16Operands(MCInst &Inst, unsigned N) const {
2057     assert(N == 1 && "Invalid number of operands!");
2058     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2059     Inst.addOperand(MCOperand::createImm(16 - CE->getValue()));
2060   }
2061 
2062   void addFBits32Operands(MCInst &Inst, unsigned N) const {
2063     assert(N == 1 && "Invalid number of operands!");
2064     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2065     Inst.addOperand(MCOperand::createImm(32 - CE->getValue()));
2066   }
2067 
2068   void addFPImmOperands(MCInst &Inst, unsigned N) const {
2069     assert(N == 1 && "Invalid number of operands!");
2070     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2071     int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
2072     Inst.addOperand(MCOperand::createImm(Val));
2073   }
2074 
2075   void addImm8s4Operands(MCInst &Inst, unsigned N) const {
2076     assert(N == 1 && "Invalid number of operands!");
2077     // FIXME: We really want to scale the value here, but the LDRD/STRD
2078     // instruction don't encode operands that way yet.
2079     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2080     Inst.addOperand(MCOperand::createImm(CE->getValue()));
2081   }
2082 
2083   void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
2084     assert(N == 1 && "Invalid number of operands!");
2085     // The immediate is scaled by four in the encoding and is stored
2086     // in the MCInst as such. Lop off the low two bits here.
2087     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2088     Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
2089   }
2090 
2091   void addImm0_508s4NegOperands(MCInst &Inst, unsigned N) const {
2092     assert(N == 1 && "Invalid number of operands!");
2093     // The immediate is scaled by four in the encoding and is stored
2094     // in the MCInst as such. Lop off the low two bits here.
2095     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2096     Inst.addOperand(MCOperand::createImm(-(CE->getValue() / 4)));
2097   }
2098 
2099   void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
2100     assert(N == 1 && "Invalid number of operands!");
2101     // The immediate is scaled by four in the encoding and is stored
2102     // in the MCInst as such. Lop off the low two bits here.
2103     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2104     Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
2105   }
2106 
2107   void addImm1_16Operands(MCInst &Inst, unsigned N) const {
2108     assert(N == 1 && "Invalid number of operands!");
2109     // The constant encodes as the immediate-1, and we store in the instruction
2110     // the bits as encoded, so subtract off one here.
2111     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2112     Inst.addOperand(MCOperand::createImm(CE->getValue() - 1));
2113   }
2114 
2115   void addImm1_32Operands(MCInst &Inst, unsigned N) const {
2116     assert(N == 1 && "Invalid number of operands!");
2117     // The constant encodes as the immediate-1, and we store in the instruction
2118     // the bits as encoded, so subtract off one here.
2119     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2120     Inst.addOperand(MCOperand::createImm(CE->getValue() - 1));
2121   }
2122 
2123   void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
2124     assert(N == 1 && "Invalid number of operands!");
2125     // The constant encodes as the immediate, except for 32, which encodes as
2126     // zero.
2127     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2128     unsigned Imm = CE->getValue();
2129     Inst.addOperand(MCOperand::createImm((Imm == 32 ? 0 : Imm)));
2130   }
2131 
2132   void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
2133     assert(N == 1 && "Invalid number of operands!");
2134     // An ASR value of 32 encodes as 0, so that's how we want to add it to
2135     // the instruction as well.
2136     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2137     int Val = CE->getValue();
2138     Inst.addOperand(MCOperand::createImm(Val == 32 ? 0 : Val));
2139   }
2140 
2141   void addT2SOImmNotOperands(MCInst &Inst, unsigned N) const {
2142     assert(N == 1 && "Invalid number of operands!");
2143     // The operand is actually a t2_so_imm, but we have its bitwise
2144     // negation in the assembly source, so twiddle it here.
2145     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2146     Inst.addOperand(MCOperand::createImm(~CE->getValue()));
2147   }
2148 
2149   void addT2SOImmNegOperands(MCInst &Inst, unsigned N) const {
2150     assert(N == 1 && "Invalid number of operands!");
2151     // The operand is actually a t2_so_imm, but we have its
2152     // negation in the assembly source, so twiddle it here.
2153     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2154     Inst.addOperand(MCOperand::createImm(-CE->getValue()));
2155   }
2156 
2157   void addImm0_4095NegOperands(MCInst &Inst, unsigned N) const {
2158     assert(N == 1 && "Invalid number of operands!");
2159     // The operand is actually an imm0_4095, but we have its
2160     // negation in the assembly source, so twiddle it here.
2161     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2162     Inst.addOperand(MCOperand::createImm(-CE->getValue()));
2163   }
2164 
2165   void addUnsignedOffset_b8s2Operands(MCInst &Inst, unsigned N) const {
2166     if(const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
2167       Inst.addOperand(MCOperand::createImm(CE->getValue() >> 2));
2168       return;
2169     }
2170 
2171     const MCSymbolRefExpr *SR = dyn_cast<MCSymbolRefExpr>(Imm.Val);
2172     assert(SR && "Unknown value type!");
2173     Inst.addOperand(MCOperand::createExpr(SR));
2174   }
2175 
2176   void addThumbMemPCOperands(MCInst &Inst, unsigned N) const {
2177     assert(N == 1 && "Invalid number of operands!");
2178     if (isImm()) {
2179       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2180       if (CE) {
2181         Inst.addOperand(MCOperand::createImm(CE->getValue()));
2182         return;
2183       }
2184 
2185       const MCSymbolRefExpr *SR = dyn_cast<MCSymbolRefExpr>(Imm.Val);
2186 
2187       assert(SR && "Unknown value type!");
2188       Inst.addOperand(MCOperand::createExpr(SR));
2189       return;
2190     }
2191 
2192     assert(isMem()  && "Unknown value type!");
2193     assert(isa<MCConstantExpr>(Memory.OffsetImm) && "Unknown value type!");
2194     Inst.addOperand(MCOperand::createImm(Memory.OffsetImm->getValue()));
2195   }
2196 
2197   void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
2198     assert(N == 1 && "Invalid number of operands!");
2199     Inst.addOperand(MCOperand::createImm(unsigned(getMemBarrierOpt())));
2200   }
2201 
2202   void addInstSyncBarrierOptOperands(MCInst &Inst, unsigned N) const {
2203     assert(N == 1 && "Invalid number of operands!");
2204     Inst.addOperand(MCOperand::createImm(unsigned(getInstSyncBarrierOpt())));
2205   }
2206 
2207   void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
2208     assert(N == 1 && "Invalid number of operands!");
2209     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2210   }
2211 
2212   void addMemPCRelImm12Operands(MCInst &Inst, unsigned N) const {
2213     assert(N == 1 && "Invalid number of operands!");
2214     int32_t Imm = Memory.OffsetImm->getValue();
2215     Inst.addOperand(MCOperand::createImm(Imm));
2216   }
2217 
2218   void addAdrLabelOperands(MCInst &Inst, unsigned N) const {
2219     assert(N == 1 && "Invalid number of operands!");
2220     assert(isImm() && "Not an immediate!");
2221 
2222     // If we have an immediate that's not a constant, treat it as a label
2223     // reference needing a fixup.
2224     if (!isa<MCConstantExpr>(getImm())) {
2225       Inst.addOperand(MCOperand::createExpr(getImm()));
2226       return;
2227     }
2228 
2229     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2230     int Val = CE->getValue();
2231     Inst.addOperand(MCOperand::createImm(Val));
2232   }
2233 
2234   void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
2235     assert(N == 2 && "Invalid number of operands!");
2236     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2237     Inst.addOperand(MCOperand::createImm(Memory.Alignment));
2238   }
2239 
2240   void addDupAlignedMemoryNoneOperands(MCInst &Inst, unsigned N) const {
2241     addAlignedMemoryOperands(Inst, N);
2242   }
2243 
2244   void addAlignedMemoryNoneOperands(MCInst &Inst, unsigned N) const {
2245     addAlignedMemoryOperands(Inst, N);
2246   }
2247 
2248   void addAlignedMemory16Operands(MCInst &Inst, unsigned N) const {
2249     addAlignedMemoryOperands(Inst, N);
2250   }
2251 
2252   void addDupAlignedMemory16Operands(MCInst &Inst, unsigned N) const {
2253     addAlignedMemoryOperands(Inst, N);
2254   }
2255 
2256   void addAlignedMemory32Operands(MCInst &Inst, unsigned N) const {
2257     addAlignedMemoryOperands(Inst, N);
2258   }
2259 
2260   void addDupAlignedMemory32Operands(MCInst &Inst, unsigned N) const {
2261     addAlignedMemoryOperands(Inst, N);
2262   }
2263 
2264   void addAlignedMemory64Operands(MCInst &Inst, unsigned N) const {
2265     addAlignedMemoryOperands(Inst, N);
2266   }
2267 
2268   void addDupAlignedMemory64Operands(MCInst &Inst, unsigned N) const {
2269     addAlignedMemoryOperands(Inst, N);
2270   }
2271 
2272   void addAlignedMemory64or128Operands(MCInst &Inst, unsigned N) const {
2273     addAlignedMemoryOperands(Inst, N);
2274   }
2275 
2276   void addDupAlignedMemory64or128Operands(MCInst &Inst, unsigned N) const {
2277     addAlignedMemoryOperands(Inst, N);
2278   }
2279 
2280   void addAlignedMemory64or128or256Operands(MCInst &Inst, unsigned N) const {
2281     addAlignedMemoryOperands(Inst, N);
2282   }
2283 
2284   void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
2285     assert(N == 3 && "Invalid number of operands!");
2286     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2287     if (!Memory.OffsetRegNum) {
2288       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2289       // Special case for #-0
2290       if (Val == INT32_MIN) Val = 0;
2291       if (Val < 0) Val = -Val;
2292       Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
2293     } else {
2294       // For register offset, we encode the shift type and negation flag
2295       // here.
2296       Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
2297                               Memory.ShiftImm, Memory.ShiftType);
2298     }
2299     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2300     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2301     Inst.addOperand(MCOperand::createImm(Val));
2302   }
2303 
2304   void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
2305     assert(N == 2 && "Invalid number of operands!");
2306     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2307     assert(CE && "non-constant AM2OffsetImm operand!");
2308     int32_t Val = CE->getValue();
2309     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2310     // Special case for #-0
2311     if (Val == INT32_MIN) Val = 0;
2312     if (Val < 0) Val = -Val;
2313     Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
2314     Inst.addOperand(MCOperand::createReg(0));
2315     Inst.addOperand(MCOperand::createImm(Val));
2316   }
2317 
2318   void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
2319     assert(N == 3 && "Invalid number of operands!");
2320     // If we have an immediate that's not a constant, treat it as a label
2321     // reference needing a fixup. If it is a constant, it's something else
2322     // and we reject it.
2323     if (isImm()) {
2324       Inst.addOperand(MCOperand::createExpr(getImm()));
2325       Inst.addOperand(MCOperand::createReg(0));
2326       Inst.addOperand(MCOperand::createImm(0));
2327       return;
2328     }
2329 
2330     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2331     if (!Memory.OffsetRegNum) {
2332       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2333       // Special case for #-0
2334       if (Val == INT32_MIN) Val = 0;
2335       if (Val < 0) Val = -Val;
2336       Val = ARM_AM::getAM3Opc(AddSub, Val);
2337     } else {
2338       // For register offset, we encode the shift type and negation flag
2339       // here.
2340       Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
2341     }
2342     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2343     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2344     Inst.addOperand(MCOperand::createImm(Val));
2345   }
2346 
2347   void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
2348     assert(N == 2 && "Invalid number of operands!");
2349     if (Kind == k_PostIndexRegister) {
2350       int32_t Val =
2351         ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
2352       Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
2353       Inst.addOperand(MCOperand::createImm(Val));
2354       return;
2355     }
2356 
2357     // Constant offset.
2358     const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
2359     int32_t Val = CE->getValue();
2360     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2361     // Special case for #-0
2362     if (Val == INT32_MIN) Val = 0;
2363     if (Val < 0) Val = -Val;
2364     Val = ARM_AM::getAM3Opc(AddSub, Val);
2365     Inst.addOperand(MCOperand::createReg(0));
2366     Inst.addOperand(MCOperand::createImm(Val));
2367   }
2368 
2369   void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
2370     assert(N == 2 && "Invalid number of operands!");
2371     // If we have an immediate that's not a constant, treat it as a label
2372     // reference needing a fixup. If it is a constant, it's something else
2373     // and we reject it.
2374     if (isImm()) {
2375       Inst.addOperand(MCOperand::createExpr(getImm()));
2376       Inst.addOperand(MCOperand::createImm(0));
2377       return;
2378     }
2379 
2380     // The lower two bits are always zero and as such are not encoded.
2381     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
2382     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2383     // Special case for #-0
2384     if (Val == INT32_MIN) Val = 0;
2385     if (Val < 0) Val = -Val;
2386     Val = ARM_AM::getAM5Opc(AddSub, Val);
2387     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2388     Inst.addOperand(MCOperand::createImm(Val));
2389   }
2390 
2391   void addAddrMode5FP16Operands(MCInst &Inst, unsigned N) const {
2392     assert(N == 2 && "Invalid number of operands!");
2393     // If we have an immediate that's not a constant, treat it as a label
2394     // reference needing a fixup. If it is a constant, it's something else
2395     // and we reject it.
2396     if (isImm()) {
2397       Inst.addOperand(MCOperand::createExpr(getImm()));
2398       Inst.addOperand(MCOperand::createImm(0));
2399       return;
2400     }
2401 
2402     // The lower bit is always zero and as such is not encoded.
2403     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 2 : 0;
2404     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
2405     // Special case for #-0
2406     if (Val == INT32_MIN) Val = 0;
2407     if (Val < 0) Val = -Val;
2408     Val = ARM_AM::getAM5FP16Opc(AddSub, Val);
2409     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2410     Inst.addOperand(MCOperand::createImm(Val));
2411   }
2412 
2413   void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
2414     assert(N == 2 && "Invalid number of operands!");
2415     // If we have an immediate that's not a constant, treat it as a label
2416     // reference needing a fixup. If it is a constant, it's something else
2417     // and we reject it.
2418     if (isImm()) {
2419       Inst.addOperand(MCOperand::createExpr(getImm()));
2420       Inst.addOperand(MCOperand::createImm(0));
2421       return;
2422     }
2423 
2424     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2425     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2426     Inst.addOperand(MCOperand::createImm(Val));
2427   }
2428 
2429   void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
2430     assert(N == 2 && "Invalid number of operands!");
2431     // The lower two bits are always zero and as such are not encoded.
2432     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
2433     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2434     Inst.addOperand(MCOperand::createImm(Val));
2435   }
2436 
2437   void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
2438     assert(N == 2 && "Invalid number of operands!");
2439     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2440     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2441     Inst.addOperand(MCOperand::createImm(Val));
2442   }
2443 
2444   void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
2445     addMemImm8OffsetOperands(Inst, N);
2446   }
2447 
2448   void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
2449     addMemImm8OffsetOperands(Inst, N);
2450   }
2451 
2452   void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
2453     assert(N == 2 && "Invalid number of operands!");
2454     // If this is an immediate, it's a label reference.
2455     if (isImm()) {
2456       addExpr(Inst, getImm());
2457       Inst.addOperand(MCOperand::createImm(0));
2458       return;
2459     }
2460 
2461     // Otherwise, it's a normal memory reg+offset.
2462     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2463     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2464     Inst.addOperand(MCOperand::createImm(Val));
2465   }
2466 
2467   void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
2468     assert(N == 2 && "Invalid number of operands!");
2469     // If this is an immediate, it's a label reference.
2470     if (isImm()) {
2471       addExpr(Inst, getImm());
2472       Inst.addOperand(MCOperand::createImm(0));
2473       return;
2474     }
2475 
2476     // Otherwise, it's a normal memory reg+offset.
2477     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
2478     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2479     Inst.addOperand(MCOperand::createImm(Val));
2480   }
2481 
2482   void addConstPoolAsmImmOperands(MCInst &Inst, unsigned N) const {
2483     assert(N == 1 && "Invalid number of operands!");
2484     // This is container for the immediate that we will create the constant
2485     // pool from
2486     addExpr(Inst, getConstantPoolImm());
2487     return;
2488   }
2489 
2490   void addMemTBBOperands(MCInst &Inst, unsigned N) const {
2491     assert(N == 2 && "Invalid number of operands!");
2492     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2493     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2494   }
2495 
2496   void addMemTBHOperands(MCInst &Inst, unsigned N) const {
2497     assert(N == 2 && "Invalid number of operands!");
2498     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2499     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2500   }
2501 
2502   void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
2503     assert(N == 3 && "Invalid number of operands!");
2504     unsigned Val =
2505       ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
2506                         Memory.ShiftImm, Memory.ShiftType);
2507     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2508     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2509     Inst.addOperand(MCOperand::createImm(Val));
2510   }
2511 
2512   void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
2513     assert(N == 3 && "Invalid number of operands!");
2514     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2515     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2516     Inst.addOperand(MCOperand::createImm(Memory.ShiftImm));
2517   }
2518 
2519   void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
2520     assert(N == 2 && "Invalid number of operands!");
2521     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2522     Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2523   }
2524 
2525   void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
2526     assert(N == 2 && "Invalid number of operands!");
2527     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
2528     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2529     Inst.addOperand(MCOperand::createImm(Val));
2530   }
2531 
2532   void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
2533     assert(N == 2 && "Invalid number of operands!");
2534     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
2535     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2536     Inst.addOperand(MCOperand::createImm(Val));
2537   }
2538 
2539   void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
2540     assert(N == 2 && "Invalid number of operands!");
2541     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
2542     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2543     Inst.addOperand(MCOperand::createImm(Val));
2544   }
2545 
2546   void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
2547     assert(N == 2 && "Invalid number of operands!");
2548     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
2549     Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2550     Inst.addOperand(MCOperand::createImm(Val));
2551   }
2552 
2553   void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
2554     assert(N == 1 && "Invalid number of operands!");
2555     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2556     assert(CE && "non-constant post-idx-imm8 operand!");
2557     int Imm = CE->getValue();
2558     bool isAdd = Imm >= 0;
2559     if (Imm == INT32_MIN) Imm = 0;
2560     Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
2561     Inst.addOperand(MCOperand::createImm(Imm));
2562   }
2563 
2564   void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
2565     assert(N == 1 && "Invalid number of operands!");
2566     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2567     assert(CE && "non-constant post-idx-imm8s4 operand!");
2568     int Imm = CE->getValue();
2569     bool isAdd = Imm >= 0;
2570     if (Imm == INT32_MIN) Imm = 0;
2571     // Immediate is scaled by 4.
2572     Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
2573     Inst.addOperand(MCOperand::createImm(Imm));
2574   }
2575 
2576   void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
2577     assert(N == 2 && "Invalid number of operands!");
2578     Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
2579     Inst.addOperand(MCOperand::createImm(PostIdxReg.isAdd));
2580   }
2581 
2582   void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
2583     assert(N == 2 && "Invalid number of operands!");
2584     Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
2585     // The sign, shift type, and shift amount are encoded in a single operand
2586     // using the AM2 encoding helpers.
2587     ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
2588     unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
2589                                      PostIdxReg.ShiftTy);
2590     Inst.addOperand(MCOperand::createImm(Imm));
2591   }
2592 
2593   void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
2594     assert(N == 1 && "Invalid number of operands!");
2595     Inst.addOperand(MCOperand::createImm(unsigned(getMSRMask())));
2596   }
2597 
2598   void addBankedRegOperands(MCInst &Inst, unsigned N) const {
2599     assert(N == 1 && "Invalid number of operands!");
2600     Inst.addOperand(MCOperand::createImm(unsigned(getBankedReg())));
2601   }
2602 
2603   void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
2604     assert(N == 1 && "Invalid number of operands!");
2605     Inst.addOperand(MCOperand::createImm(unsigned(getProcIFlags())));
2606   }
2607 
2608   void addVecListOperands(MCInst &Inst, unsigned N) const {
2609     assert(N == 1 && "Invalid number of operands!");
2610     Inst.addOperand(MCOperand::createReg(VectorList.RegNum));
2611   }
2612 
2613   void addVecListIndexedOperands(MCInst &Inst, unsigned N) const {
2614     assert(N == 2 && "Invalid number of operands!");
2615     Inst.addOperand(MCOperand::createReg(VectorList.RegNum));
2616     Inst.addOperand(MCOperand::createImm(VectorList.LaneIndex));
2617   }
2618 
2619   void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
2620     assert(N == 1 && "Invalid number of operands!");
2621     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
2622   }
2623 
2624   void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
2625     assert(N == 1 && "Invalid number of operands!");
2626     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
2627   }
2628 
2629   void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
2630     assert(N == 1 && "Invalid number of operands!");
2631     Inst.addOperand(MCOperand::createImm(getVectorIndex()));
2632   }
2633 
2634   void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
2635     assert(N == 1 && "Invalid number of operands!");
2636     // The immediate encodes the type of constant as well as the value.
2637     // Mask in that this is an i8 splat.
2638     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2639     Inst.addOperand(MCOperand::createImm(CE->getValue() | 0xe00));
2640   }
2641 
2642   void addNEONi16splatOperands(MCInst &Inst, unsigned N) const {
2643     assert(N == 1 && "Invalid number of operands!");
2644     // The immediate encodes the type of constant as well as the value.
2645     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2646     unsigned Value = CE->getValue();
2647     Value = ARM_AM::encodeNEONi16splat(Value);
2648     Inst.addOperand(MCOperand::createImm(Value));
2649   }
2650 
2651   void addNEONi16splatNotOperands(MCInst &Inst, unsigned N) const {
2652     assert(N == 1 && "Invalid number of operands!");
2653     // The immediate encodes the type of constant as well as the value.
2654     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2655     unsigned Value = CE->getValue();
2656     Value = ARM_AM::encodeNEONi16splat(~Value & 0xffff);
2657     Inst.addOperand(MCOperand::createImm(Value));
2658   }
2659 
2660   void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
2661     assert(N == 1 && "Invalid number of operands!");
2662     // The immediate encodes the type of constant as well as the value.
2663     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2664     unsigned Value = CE->getValue();
2665     Value = ARM_AM::encodeNEONi32splat(Value);
2666     Inst.addOperand(MCOperand::createImm(Value));
2667   }
2668 
2669   void addNEONi32splatNotOperands(MCInst &Inst, unsigned N) const {
2670     assert(N == 1 && "Invalid number of operands!");
2671     // The immediate encodes the type of constant as well as the value.
2672     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2673     unsigned Value = CE->getValue();
2674     Value = ARM_AM::encodeNEONi32splat(~Value);
2675     Inst.addOperand(MCOperand::createImm(Value));
2676   }
2677 
2678   void addNEONinvByteReplicateOperands(MCInst &Inst, unsigned N) const {
2679     assert(N == 1 && "Invalid number of operands!");
2680     // The immediate encodes the type of constant as well as the value.
2681     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2682     unsigned Value = CE->getValue();
2683     assert((Inst.getOpcode() == ARM::VMOVv8i8 ||
2684             Inst.getOpcode() == ARM::VMOVv16i8) &&
2685            "All vmvn instructions that wants to replicate non-zero byte "
2686            "always must be replaced with VMOVv8i8 or VMOVv16i8.");
2687     unsigned B = ((~Value) & 0xff);
2688     B |= 0xe00; // cmode = 0b1110
2689     Inst.addOperand(MCOperand::createImm(B));
2690   }
2691   void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
2692     assert(N == 1 && "Invalid number of operands!");
2693     // The immediate encodes the type of constant as well as the value.
2694     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2695     unsigned Value = CE->getValue();
2696     if (Value >= 256 && Value <= 0xffff)
2697       Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
2698     else if (Value > 0xffff && Value <= 0xffffff)
2699       Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
2700     else if (Value > 0xffffff)
2701       Value = (Value >> 24) | 0x600;
2702     Inst.addOperand(MCOperand::createImm(Value));
2703   }
2704 
2705   void addNEONvmovByteReplicateOperands(MCInst &Inst, unsigned N) const {
2706     assert(N == 1 && "Invalid number of operands!");
2707     // The immediate encodes the type of constant as well as the value.
2708     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2709     unsigned Value = CE->getValue();
2710     assert((Inst.getOpcode() == ARM::VMOVv8i8 ||
2711             Inst.getOpcode() == ARM::VMOVv16i8) &&
2712            "All instructions that wants to replicate non-zero byte "
2713            "always must be replaced with VMOVv8i8 or VMOVv16i8.");
2714     unsigned B = Value & 0xff;
2715     B |= 0xe00; // cmode = 0b1110
2716     Inst.addOperand(MCOperand::createImm(B));
2717   }
2718   void addNEONi32vmovNegOperands(MCInst &Inst, unsigned N) const {
2719     assert(N == 1 && "Invalid number of operands!");
2720     // The immediate encodes the type of constant as well as the value.
2721     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2722     unsigned Value = ~CE->getValue();
2723     if (Value >= 256 && Value <= 0xffff)
2724       Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
2725     else if (Value > 0xffff && Value <= 0xffffff)
2726       Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
2727     else if (Value > 0xffffff)
2728       Value = (Value >> 24) | 0x600;
2729     Inst.addOperand(MCOperand::createImm(Value));
2730   }
2731 
2732   void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
2733     assert(N == 1 && "Invalid number of operands!");
2734     // The immediate encodes the type of constant as well as the value.
2735     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2736     uint64_t Value = CE->getValue();
2737     unsigned Imm = 0;
2738     for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
2739       Imm |= (Value & 1) << i;
2740     }
2741     Inst.addOperand(MCOperand::createImm(Imm | 0x1e00));
2742   }
2743 
2744   void print(raw_ostream &OS) const override;
2745 
2746   static std::unique_ptr<ARMOperand> CreateITMask(unsigned Mask, SMLoc S) {
2747     auto Op = make_unique<ARMOperand>(k_ITCondMask);
2748     Op->ITMask.Mask = Mask;
2749     Op->StartLoc = S;
2750     Op->EndLoc = S;
2751     return Op;
2752   }
2753 
2754   static std::unique_ptr<ARMOperand> CreateCondCode(ARMCC::CondCodes CC,
2755                                                     SMLoc S) {
2756     auto Op = make_unique<ARMOperand>(k_CondCode);
2757     Op->CC.Val = CC;
2758     Op->StartLoc = S;
2759     Op->EndLoc = S;
2760     return Op;
2761   }
2762 
2763   static std::unique_ptr<ARMOperand> CreateCoprocNum(unsigned CopVal, SMLoc S) {
2764     auto Op = make_unique<ARMOperand>(k_CoprocNum);
2765     Op->Cop.Val = CopVal;
2766     Op->StartLoc = S;
2767     Op->EndLoc = S;
2768     return Op;
2769   }
2770 
2771   static std::unique_ptr<ARMOperand> CreateCoprocReg(unsigned CopVal, SMLoc S) {
2772     auto Op = make_unique<ARMOperand>(k_CoprocReg);
2773     Op->Cop.Val = CopVal;
2774     Op->StartLoc = S;
2775     Op->EndLoc = S;
2776     return Op;
2777   }
2778 
2779   static std::unique_ptr<ARMOperand> CreateCoprocOption(unsigned Val, SMLoc S,
2780                                                         SMLoc E) {
2781     auto Op = make_unique<ARMOperand>(k_CoprocOption);
2782     Op->Cop.Val = Val;
2783     Op->StartLoc = S;
2784     Op->EndLoc = E;
2785     return Op;
2786   }
2787 
2788   static std::unique_ptr<ARMOperand> CreateCCOut(unsigned RegNum, SMLoc S) {
2789     auto Op = make_unique<ARMOperand>(k_CCOut);
2790     Op->Reg.RegNum = RegNum;
2791     Op->StartLoc = S;
2792     Op->EndLoc = S;
2793     return Op;
2794   }
2795 
2796   static std::unique_ptr<ARMOperand> CreateToken(StringRef Str, SMLoc S) {
2797     auto Op = make_unique<ARMOperand>(k_Token);
2798     Op->Tok.Data = Str.data();
2799     Op->Tok.Length = Str.size();
2800     Op->StartLoc = S;
2801     Op->EndLoc = S;
2802     return Op;
2803   }
2804 
2805   static std::unique_ptr<ARMOperand> CreateReg(unsigned RegNum, SMLoc S,
2806                                                SMLoc E) {
2807     auto Op = make_unique<ARMOperand>(k_Register);
2808     Op->Reg.RegNum = RegNum;
2809     Op->StartLoc = S;
2810     Op->EndLoc = E;
2811     return Op;
2812   }
2813 
2814   static std::unique_ptr<ARMOperand>
2815   CreateShiftedRegister(ARM_AM::ShiftOpc ShTy, unsigned SrcReg,
2816                         unsigned ShiftReg, unsigned ShiftImm, SMLoc S,
2817                         SMLoc E) {
2818     auto Op = make_unique<ARMOperand>(k_ShiftedRegister);
2819     Op->RegShiftedReg.ShiftTy = ShTy;
2820     Op->RegShiftedReg.SrcReg = SrcReg;
2821     Op->RegShiftedReg.ShiftReg = ShiftReg;
2822     Op->RegShiftedReg.ShiftImm = ShiftImm;
2823     Op->StartLoc = S;
2824     Op->EndLoc = E;
2825     return Op;
2826   }
2827 
2828   static std::unique_ptr<ARMOperand>
2829   CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy, unsigned SrcReg,
2830                          unsigned ShiftImm, SMLoc S, SMLoc E) {
2831     auto Op = make_unique<ARMOperand>(k_ShiftedImmediate);
2832     Op->RegShiftedImm.ShiftTy = ShTy;
2833     Op->RegShiftedImm.SrcReg = SrcReg;
2834     Op->RegShiftedImm.ShiftImm = ShiftImm;
2835     Op->StartLoc = S;
2836     Op->EndLoc = E;
2837     return Op;
2838   }
2839 
2840   static std::unique_ptr<ARMOperand> CreateShifterImm(bool isASR, unsigned Imm,
2841                                                       SMLoc S, SMLoc E) {
2842     auto Op = make_unique<ARMOperand>(k_ShifterImmediate);
2843     Op->ShifterImm.isASR = isASR;
2844     Op->ShifterImm.Imm = Imm;
2845     Op->StartLoc = S;
2846     Op->EndLoc = E;
2847     return Op;
2848   }
2849 
2850   static std::unique_ptr<ARMOperand> CreateRotImm(unsigned Imm, SMLoc S,
2851                                                   SMLoc E) {
2852     auto Op = make_unique<ARMOperand>(k_RotateImmediate);
2853     Op->RotImm.Imm = Imm;
2854     Op->StartLoc = S;
2855     Op->EndLoc = E;
2856     return Op;
2857   }
2858 
2859   static std::unique_ptr<ARMOperand> CreateModImm(unsigned Bits, unsigned Rot,
2860                                                   SMLoc S, SMLoc E) {
2861     auto Op = make_unique<ARMOperand>(k_ModifiedImmediate);
2862     Op->ModImm.Bits = Bits;
2863     Op->ModImm.Rot = Rot;
2864     Op->StartLoc = S;
2865     Op->EndLoc = E;
2866     return Op;
2867   }
2868 
2869   static std::unique_ptr<ARMOperand>
2870   CreateConstantPoolImm(const MCExpr *Val, SMLoc S, SMLoc E) {
2871     auto Op = make_unique<ARMOperand>(k_ConstantPoolImmediate);
2872     Op->Imm.Val = Val;
2873     Op->StartLoc = S;
2874     Op->EndLoc = E;
2875     return Op;
2876   }
2877 
2878   static std::unique_ptr<ARMOperand>
2879   CreateBitfield(unsigned LSB, unsigned Width, SMLoc S, SMLoc E) {
2880     auto Op = make_unique<ARMOperand>(k_BitfieldDescriptor);
2881     Op->Bitfield.LSB = LSB;
2882     Op->Bitfield.Width = Width;
2883     Op->StartLoc = S;
2884     Op->EndLoc = E;
2885     return Op;
2886   }
2887 
2888   static std::unique_ptr<ARMOperand>
2889   CreateRegList(SmallVectorImpl<std::pair<unsigned, unsigned>> &Regs,
2890                 SMLoc StartLoc, SMLoc EndLoc) {
2891     assert (Regs.size() > 0 && "RegList contains no registers?");
2892     KindTy Kind = k_RegisterList;
2893 
2894     if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().second))
2895       Kind = k_DPRRegisterList;
2896     else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
2897              contains(Regs.front().second))
2898       Kind = k_SPRRegisterList;
2899 
2900     // Sort based on the register encoding values.
2901     array_pod_sort(Regs.begin(), Regs.end());
2902 
2903     auto Op = make_unique<ARMOperand>(Kind);
2904     for (SmallVectorImpl<std::pair<unsigned, unsigned> >::const_iterator
2905            I = Regs.begin(), E = Regs.end(); I != E; ++I)
2906       Op->Registers.push_back(I->second);
2907     Op->StartLoc = StartLoc;
2908     Op->EndLoc = EndLoc;
2909     return Op;
2910   }
2911 
2912   static std::unique_ptr<ARMOperand> CreateVectorList(unsigned RegNum,
2913                                                       unsigned Count,
2914                                                       bool isDoubleSpaced,
2915                                                       SMLoc S, SMLoc E) {
2916     auto Op = make_unique<ARMOperand>(k_VectorList);
2917     Op->VectorList.RegNum = RegNum;
2918     Op->VectorList.Count = Count;
2919     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2920     Op->StartLoc = S;
2921     Op->EndLoc = E;
2922     return Op;
2923   }
2924 
2925   static std::unique_ptr<ARMOperand>
2926   CreateVectorListAllLanes(unsigned RegNum, unsigned Count, bool isDoubleSpaced,
2927                            SMLoc S, SMLoc E) {
2928     auto Op = make_unique<ARMOperand>(k_VectorListAllLanes);
2929     Op->VectorList.RegNum = RegNum;
2930     Op->VectorList.Count = Count;
2931     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2932     Op->StartLoc = S;
2933     Op->EndLoc = E;
2934     return Op;
2935   }
2936 
2937   static std::unique_ptr<ARMOperand>
2938   CreateVectorListIndexed(unsigned RegNum, unsigned Count, unsigned Index,
2939                           bool isDoubleSpaced, SMLoc S, SMLoc E) {
2940     auto Op = make_unique<ARMOperand>(k_VectorListIndexed);
2941     Op->VectorList.RegNum = RegNum;
2942     Op->VectorList.Count = Count;
2943     Op->VectorList.LaneIndex = Index;
2944     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2945     Op->StartLoc = S;
2946     Op->EndLoc = E;
2947     return Op;
2948   }
2949 
2950   static std::unique_ptr<ARMOperand>
2951   CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E, MCContext &Ctx) {
2952     auto Op = make_unique<ARMOperand>(k_VectorIndex);
2953     Op->VectorIndex.Val = Idx;
2954     Op->StartLoc = S;
2955     Op->EndLoc = E;
2956     return Op;
2957   }
2958 
2959   static std::unique_ptr<ARMOperand> CreateImm(const MCExpr *Val, SMLoc S,
2960                                                SMLoc E) {
2961     auto Op = make_unique<ARMOperand>(k_Immediate);
2962     Op->Imm.Val = Val;
2963     Op->StartLoc = S;
2964     Op->EndLoc = E;
2965     return Op;
2966   }
2967 
2968   static std::unique_ptr<ARMOperand>
2969   CreateMem(unsigned BaseRegNum, const MCConstantExpr *OffsetImm,
2970             unsigned OffsetRegNum, ARM_AM::ShiftOpc ShiftType,
2971             unsigned ShiftImm, unsigned Alignment, bool isNegative, SMLoc S,
2972             SMLoc E, SMLoc AlignmentLoc = SMLoc()) {
2973     auto Op = make_unique<ARMOperand>(k_Memory);
2974     Op->Memory.BaseRegNum = BaseRegNum;
2975     Op->Memory.OffsetImm = OffsetImm;
2976     Op->Memory.OffsetRegNum = OffsetRegNum;
2977     Op->Memory.ShiftType = ShiftType;
2978     Op->Memory.ShiftImm = ShiftImm;
2979     Op->Memory.Alignment = Alignment;
2980     Op->Memory.isNegative = isNegative;
2981     Op->StartLoc = S;
2982     Op->EndLoc = E;
2983     Op->AlignmentLoc = AlignmentLoc;
2984     return Op;
2985   }
2986 
2987   static std::unique_ptr<ARMOperand>
2988   CreatePostIdxReg(unsigned RegNum, bool isAdd, ARM_AM::ShiftOpc ShiftTy,
2989                    unsigned ShiftImm, SMLoc S, SMLoc E) {
2990     auto Op = make_unique<ARMOperand>(k_PostIndexRegister);
2991     Op->PostIdxReg.RegNum = RegNum;
2992     Op->PostIdxReg.isAdd = isAdd;
2993     Op->PostIdxReg.ShiftTy = ShiftTy;
2994     Op->PostIdxReg.ShiftImm = ShiftImm;
2995     Op->StartLoc = S;
2996     Op->EndLoc = E;
2997     return Op;
2998   }
2999 
3000   static std::unique_ptr<ARMOperand> CreateMemBarrierOpt(ARM_MB::MemBOpt Opt,
3001                                                          SMLoc S) {
3002     auto Op = make_unique<ARMOperand>(k_MemBarrierOpt);
3003     Op->MBOpt.Val = Opt;
3004     Op->StartLoc = S;
3005     Op->EndLoc = S;
3006     return Op;
3007   }
3008 
3009   static std::unique_ptr<ARMOperand>
3010   CreateInstSyncBarrierOpt(ARM_ISB::InstSyncBOpt Opt, SMLoc S) {
3011     auto Op = make_unique<ARMOperand>(k_InstSyncBarrierOpt);
3012     Op->ISBOpt.Val = Opt;
3013     Op->StartLoc = S;
3014     Op->EndLoc = S;
3015     return Op;
3016   }
3017 
3018   static std::unique_ptr<ARMOperand> CreateProcIFlags(ARM_PROC::IFlags IFlags,
3019                                                       SMLoc S) {
3020     auto Op = make_unique<ARMOperand>(k_ProcIFlags);
3021     Op->IFlags.Val = IFlags;
3022     Op->StartLoc = S;
3023     Op->EndLoc = S;
3024     return Op;
3025   }
3026 
3027   static std::unique_ptr<ARMOperand> CreateMSRMask(unsigned MMask, SMLoc S) {
3028     auto Op = make_unique<ARMOperand>(k_MSRMask);
3029     Op->MMask.Val = MMask;
3030     Op->StartLoc = S;
3031     Op->EndLoc = S;
3032     return Op;
3033   }
3034 
3035   static std::unique_ptr<ARMOperand> CreateBankedReg(unsigned Reg, SMLoc S) {
3036     auto Op = make_unique<ARMOperand>(k_BankedReg);
3037     Op->BankedReg.Val = Reg;
3038     Op->StartLoc = S;
3039     Op->EndLoc = S;
3040     return Op;
3041   }
3042 };
3043 
3044 } // end anonymous namespace.
3045 
3046 void ARMOperand::print(raw_ostream &OS) const {
3047   switch (Kind) {
3048   case k_CondCode:
3049     OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
3050     break;
3051   case k_CCOut:
3052     OS << "<ccout " << getReg() << ">";
3053     break;
3054   case k_ITCondMask: {
3055     static const char *const MaskStr[] = {
3056       "()", "(t)", "(e)", "(tt)", "(et)", "(te)", "(ee)", "(ttt)", "(ett)",
3057       "(tet)", "(eet)", "(tte)", "(ete)", "(tee)", "(eee)"
3058     };
3059     assert((ITMask.Mask & 0xf) == ITMask.Mask);
3060     OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
3061     break;
3062   }
3063   case k_CoprocNum:
3064     OS << "<coprocessor number: " << getCoproc() << ">";
3065     break;
3066   case k_CoprocReg:
3067     OS << "<coprocessor register: " << getCoproc() << ">";
3068     break;
3069   case k_CoprocOption:
3070     OS << "<coprocessor option: " << CoprocOption.Val << ">";
3071     break;
3072   case k_MSRMask:
3073     OS << "<mask: " << getMSRMask() << ">";
3074     break;
3075   case k_BankedReg:
3076     OS << "<banked reg: " << getBankedReg() << ">";
3077     break;
3078   case k_Immediate:
3079     OS << *getImm();
3080     break;
3081   case k_MemBarrierOpt:
3082     OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt(), false) << ">";
3083     break;
3084   case k_InstSyncBarrierOpt:
3085     OS << "<ARM_ISB::" << InstSyncBOptToString(getInstSyncBarrierOpt()) << ">";
3086     break;
3087   case k_Memory:
3088     OS << "<memory "
3089        << " base:" << Memory.BaseRegNum;
3090     OS << ">";
3091     break;
3092   case k_PostIndexRegister:
3093     OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
3094        << PostIdxReg.RegNum;
3095     if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
3096       OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
3097          << PostIdxReg.ShiftImm;
3098     OS << ">";
3099     break;
3100   case k_ProcIFlags: {
3101     OS << "<ARM_PROC::";
3102     unsigned IFlags = getProcIFlags();
3103     for (int i=2; i >= 0; --i)
3104       if (IFlags & (1 << i))
3105         OS << ARM_PROC::IFlagsToString(1 << i);
3106     OS << ">";
3107     break;
3108   }
3109   case k_Register:
3110     OS << "<register " << getReg() << ">";
3111     break;
3112   case k_ShifterImmediate:
3113     OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
3114        << " #" << ShifterImm.Imm << ">";
3115     break;
3116   case k_ShiftedRegister:
3117     OS << "<so_reg_reg "
3118        << RegShiftedReg.SrcReg << " "
3119        << ARM_AM::getShiftOpcStr(RegShiftedReg.ShiftTy)
3120        << " " << RegShiftedReg.ShiftReg << ">";
3121     break;
3122   case k_ShiftedImmediate:
3123     OS << "<so_reg_imm "
3124        << RegShiftedImm.SrcReg << " "
3125        << ARM_AM::getShiftOpcStr(RegShiftedImm.ShiftTy)
3126        << " #" << RegShiftedImm.ShiftImm << ">";
3127     break;
3128   case k_RotateImmediate:
3129     OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
3130     break;
3131   case k_ModifiedImmediate:
3132     OS << "<mod_imm #" << ModImm.Bits << ", #"
3133        <<  ModImm.Rot << ")>";
3134     break;
3135   case k_ConstantPoolImmediate:
3136     OS << "<constant_pool_imm #" << *getConstantPoolImm();
3137     break;
3138   case k_BitfieldDescriptor:
3139     OS << "<bitfield " << "lsb: " << Bitfield.LSB
3140        << ", width: " << Bitfield.Width << ">";
3141     break;
3142   case k_RegisterList:
3143   case k_DPRRegisterList:
3144   case k_SPRRegisterList: {
3145     OS << "<register_list ";
3146 
3147     const SmallVectorImpl<unsigned> &RegList = getRegList();
3148     for (SmallVectorImpl<unsigned>::const_iterator
3149            I = RegList.begin(), E = RegList.end(); I != E; ) {
3150       OS << *I;
3151       if (++I < E) OS << ", ";
3152     }
3153 
3154     OS << ">";
3155     break;
3156   }
3157   case k_VectorList:
3158     OS << "<vector_list " << VectorList.Count << " * "
3159        << VectorList.RegNum << ">";
3160     break;
3161   case k_VectorListAllLanes:
3162     OS << "<vector_list(all lanes) " << VectorList.Count << " * "
3163        << VectorList.RegNum << ">";
3164     break;
3165   case k_VectorListIndexed:
3166     OS << "<vector_list(lane " << VectorList.LaneIndex << ") "
3167        << VectorList.Count << " * " << VectorList.RegNum << ">";
3168     break;
3169   case k_Token:
3170     OS << "'" << getToken() << "'";
3171     break;
3172   case k_VectorIndex:
3173     OS << "<vectorindex " << getVectorIndex() << ">";
3174     break;
3175   }
3176 }
3177 
3178 /// @name Auto-generated Match Functions
3179 /// {
3180 
3181 static unsigned MatchRegisterName(StringRef Name);
3182 
3183 /// }
3184 
3185 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
3186                                  SMLoc &StartLoc, SMLoc &EndLoc) {
3187   const AsmToken &Tok = getParser().getTok();
3188   StartLoc = Tok.getLoc();
3189   EndLoc = Tok.getEndLoc();
3190   RegNo = tryParseRegister();
3191 
3192   return (RegNo == (unsigned)-1);
3193 }
3194 
3195 /// Try to parse a register name.  The token must be an Identifier when called,
3196 /// and if it is a register name the token is eaten and the register number is
3197 /// returned.  Otherwise return -1.
3198 ///
3199 int ARMAsmParser::tryParseRegister() {
3200   MCAsmParser &Parser = getParser();
3201   const AsmToken &Tok = Parser.getTok();
3202   if (Tok.isNot(AsmToken::Identifier)) return -1;
3203 
3204   std::string lowerCase = Tok.getString().lower();
3205   unsigned RegNum = MatchRegisterName(lowerCase);
3206   if (!RegNum) {
3207     RegNum = StringSwitch<unsigned>(lowerCase)
3208       .Case("r13", ARM::SP)
3209       .Case("r14", ARM::LR)
3210       .Case("r15", ARM::PC)
3211       .Case("ip", ARM::R12)
3212       // Additional register name aliases for 'gas' compatibility.
3213       .Case("a1", ARM::R0)
3214       .Case("a2", ARM::R1)
3215       .Case("a3", ARM::R2)
3216       .Case("a4", ARM::R3)
3217       .Case("v1", ARM::R4)
3218       .Case("v2", ARM::R5)
3219       .Case("v3", ARM::R6)
3220       .Case("v4", ARM::R7)
3221       .Case("v5", ARM::R8)
3222       .Case("v6", ARM::R9)
3223       .Case("v7", ARM::R10)
3224       .Case("v8", ARM::R11)
3225       .Case("sb", ARM::R9)
3226       .Case("sl", ARM::R10)
3227       .Case("fp", ARM::R11)
3228       .Default(0);
3229   }
3230   if (!RegNum) {
3231     // Check for aliases registered via .req. Canonicalize to lower case.
3232     // That's more consistent since register names are case insensitive, and
3233     // it's how the original entry was passed in from MC/MCParser/AsmParser.
3234     StringMap<unsigned>::const_iterator Entry = RegisterReqs.find(lowerCase);
3235     // If no match, return failure.
3236     if (Entry == RegisterReqs.end())
3237       return -1;
3238     Parser.Lex(); // Eat identifier token.
3239     return Entry->getValue();
3240   }
3241 
3242   // Some FPUs only have 16 D registers, so D16-D31 are invalid
3243   if (hasD16() && RegNum >= ARM::D16 && RegNum <= ARM::D31)
3244     return -1;
3245 
3246   Parser.Lex(); // Eat identifier token.
3247 
3248   return RegNum;
3249 }
3250 
3251 // Try to parse a shifter  (e.g., "lsl <amt>"). On success, return 0.
3252 // If a recoverable error occurs, return 1. If an irrecoverable error
3253 // occurs, return -1. An irrecoverable error is one where tokens have been
3254 // consumed in the process of trying to parse the shifter (i.e., when it is
3255 // indeed a shifter operand, but malformed).
3256 int ARMAsmParser::tryParseShiftRegister(OperandVector &Operands) {
3257   MCAsmParser &Parser = getParser();
3258   SMLoc S = Parser.getTok().getLoc();
3259   const AsmToken &Tok = Parser.getTok();
3260   if (Tok.isNot(AsmToken::Identifier))
3261     return -1;
3262 
3263   std::string lowerCase = Tok.getString().lower();
3264   ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
3265       .Case("asl", ARM_AM::lsl)
3266       .Case("lsl", ARM_AM::lsl)
3267       .Case("lsr", ARM_AM::lsr)
3268       .Case("asr", ARM_AM::asr)
3269       .Case("ror", ARM_AM::ror)
3270       .Case("rrx", ARM_AM::rrx)
3271       .Default(ARM_AM::no_shift);
3272 
3273   if (ShiftTy == ARM_AM::no_shift)
3274     return 1;
3275 
3276   Parser.Lex(); // Eat the operator.
3277 
3278   // The source register for the shift has already been added to the
3279   // operand list, so we need to pop it off and combine it into the shifted
3280   // register operand instead.
3281   std::unique_ptr<ARMOperand> PrevOp(
3282       (ARMOperand *)Operands.pop_back_val().release());
3283   if (!PrevOp->isReg())
3284     return Error(PrevOp->getStartLoc(), "shift must be of a register");
3285   int SrcReg = PrevOp->getReg();
3286 
3287   SMLoc EndLoc;
3288   int64_t Imm = 0;
3289   int ShiftReg = 0;
3290   if (ShiftTy == ARM_AM::rrx) {
3291     // RRX Doesn't have an explicit shift amount. The encoder expects
3292     // the shift register to be the same as the source register. Seems odd,
3293     // but OK.
3294     ShiftReg = SrcReg;
3295   } else {
3296     // Figure out if this is shifted by a constant or a register (for non-RRX).
3297     if (Parser.getTok().is(AsmToken::Hash) ||
3298         Parser.getTok().is(AsmToken::Dollar)) {
3299       Parser.Lex(); // Eat hash.
3300       SMLoc ImmLoc = Parser.getTok().getLoc();
3301       const MCExpr *ShiftExpr = nullptr;
3302       if (getParser().parseExpression(ShiftExpr, EndLoc)) {
3303         Error(ImmLoc, "invalid immediate shift value");
3304         return -1;
3305       }
3306       // The expression must be evaluatable as an immediate.
3307       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
3308       if (!CE) {
3309         Error(ImmLoc, "invalid immediate shift value");
3310         return -1;
3311       }
3312       // Range check the immediate.
3313       // lsl, ror: 0 <= imm <= 31
3314       // lsr, asr: 0 <= imm <= 32
3315       Imm = CE->getValue();
3316       if (Imm < 0 ||
3317           ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
3318           ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
3319         Error(ImmLoc, "immediate shift value out of range");
3320         return -1;
3321       }
3322       // shift by zero is a nop. Always send it through as lsl.
3323       // ('as' compatibility)
3324       if (Imm == 0)
3325         ShiftTy = ARM_AM::lsl;
3326     } else if (Parser.getTok().is(AsmToken::Identifier)) {
3327       SMLoc L = Parser.getTok().getLoc();
3328       EndLoc = Parser.getTok().getEndLoc();
3329       ShiftReg = tryParseRegister();
3330       if (ShiftReg == -1) {
3331         Error(L, "expected immediate or register in shift operand");
3332         return -1;
3333       }
3334     } else {
3335       Error(Parser.getTok().getLoc(),
3336             "expected immediate or register in shift operand");
3337       return -1;
3338     }
3339   }
3340 
3341   if (ShiftReg && ShiftTy != ARM_AM::rrx)
3342     Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
3343                                                          ShiftReg, Imm,
3344                                                          S, EndLoc));
3345   else
3346     Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
3347                                                           S, EndLoc));
3348 
3349   return 0;
3350 }
3351 
3352 
3353 /// Try to parse a register name.  The token must be an Identifier when called.
3354 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
3355 /// if there is a "writeback". 'true' if it's not a register.
3356 ///
3357 /// TODO this is likely to change to allow different register types and or to
3358 /// parse for a specific register type.
3359 bool ARMAsmParser::tryParseRegisterWithWriteBack(OperandVector &Operands) {
3360   MCAsmParser &Parser = getParser();
3361   const AsmToken &RegTok = Parser.getTok();
3362   int RegNo = tryParseRegister();
3363   if (RegNo == -1)
3364     return true;
3365 
3366   Operands.push_back(ARMOperand::CreateReg(RegNo, RegTok.getLoc(),
3367                                            RegTok.getEndLoc()));
3368 
3369   const AsmToken &ExclaimTok = Parser.getTok();
3370   if (ExclaimTok.is(AsmToken::Exclaim)) {
3371     Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
3372                                                ExclaimTok.getLoc()));
3373     Parser.Lex(); // Eat exclaim token
3374     return false;
3375   }
3376 
3377   // Also check for an index operand. This is only legal for vector registers,
3378   // but that'll get caught OK in operand matching, so we don't need to
3379   // explicitly filter everything else out here.
3380   if (Parser.getTok().is(AsmToken::LBrac)) {
3381     SMLoc SIdx = Parser.getTok().getLoc();
3382     Parser.Lex(); // Eat left bracket token.
3383 
3384     const MCExpr *ImmVal;
3385     if (getParser().parseExpression(ImmVal))
3386       return true;
3387     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
3388     if (!MCE)
3389       return TokError("immediate value expected for vector index");
3390 
3391     if (Parser.getTok().isNot(AsmToken::RBrac))
3392       return Error(Parser.getTok().getLoc(), "']' expected");
3393 
3394     SMLoc E = Parser.getTok().getEndLoc();
3395     Parser.Lex(); // Eat right bracket token.
3396 
3397     Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
3398                                                      SIdx, E,
3399                                                      getContext()));
3400   }
3401 
3402   return false;
3403 }
3404 
3405 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
3406 /// instruction with a symbolic operand name.
3407 /// We accept "crN" syntax for GAS compatibility.
3408 /// <operand-name> ::= <prefix><number>
3409 /// If CoprocOp is 'c', then:
3410 ///   <prefix> ::= c | cr
3411 /// If CoprocOp is 'p', then :
3412 ///   <prefix> ::= p
3413 /// <number> ::= integer in range [0, 15]
3414 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
3415   // Use the same layout as the tablegen'erated register name matcher. Ugly,
3416   // but efficient.
3417   if (Name.size() < 2 || Name[0] != CoprocOp)
3418     return -1;
3419   Name = (Name[1] == 'r') ? Name.drop_front(2) : Name.drop_front();
3420 
3421   switch (Name.size()) {
3422   default: return -1;
3423   case 1:
3424     switch (Name[0]) {
3425     default:  return -1;
3426     case '0': return 0;
3427     case '1': return 1;
3428     case '2': return 2;
3429     case '3': return 3;
3430     case '4': return 4;
3431     case '5': return 5;
3432     case '6': return 6;
3433     case '7': return 7;
3434     case '8': return 8;
3435     case '9': return 9;
3436     }
3437   case 2:
3438     if (Name[0] != '1')
3439       return -1;
3440     switch (Name[1]) {
3441     default:  return -1;
3442     // CP10 and CP11 are VFP/NEON and so vector instructions should be used.
3443     // However, old cores (v5/v6) did use them in that way.
3444     case '0': return 10;
3445     case '1': return 11;
3446     case '2': return 12;
3447     case '3': return 13;
3448     case '4': return 14;
3449     case '5': return 15;
3450     }
3451   }
3452 }
3453 
3454 /// parseITCondCode - Try to parse a condition code for an IT instruction.
3455 ARMAsmParser::OperandMatchResultTy
3456 ARMAsmParser::parseITCondCode(OperandVector &Operands) {
3457   MCAsmParser &Parser = getParser();
3458   SMLoc S = Parser.getTok().getLoc();
3459   const AsmToken &Tok = Parser.getTok();
3460   if (!Tok.is(AsmToken::Identifier))
3461     return MatchOperand_NoMatch;
3462   unsigned CC = StringSwitch<unsigned>(Tok.getString().lower())
3463     .Case("eq", ARMCC::EQ)
3464     .Case("ne", ARMCC::NE)
3465     .Case("hs", ARMCC::HS)
3466     .Case("cs", ARMCC::HS)
3467     .Case("lo", ARMCC::LO)
3468     .Case("cc", ARMCC::LO)
3469     .Case("mi", ARMCC::MI)
3470     .Case("pl", ARMCC::PL)
3471     .Case("vs", ARMCC::VS)
3472     .Case("vc", ARMCC::VC)
3473     .Case("hi", ARMCC::HI)
3474     .Case("ls", ARMCC::LS)
3475     .Case("ge", ARMCC::GE)
3476     .Case("lt", ARMCC::LT)
3477     .Case("gt", ARMCC::GT)
3478     .Case("le", ARMCC::LE)
3479     .Case("al", ARMCC::AL)
3480     .Default(~0U);
3481   if (CC == ~0U)
3482     return MatchOperand_NoMatch;
3483   Parser.Lex(); // Eat the token.
3484 
3485   Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
3486 
3487   return MatchOperand_Success;
3488 }
3489 
3490 /// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
3491 /// token must be an Identifier when called, and if it is a coprocessor
3492 /// number, the token is eaten and the operand is added to the operand list.
3493 ARMAsmParser::OperandMatchResultTy
3494 ARMAsmParser::parseCoprocNumOperand(OperandVector &Operands) {
3495   MCAsmParser &Parser = getParser();
3496   SMLoc S = Parser.getTok().getLoc();
3497   const AsmToken &Tok = Parser.getTok();
3498   if (Tok.isNot(AsmToken::Identifier))
3499     return MatchOperand_NoMatch;
3500 
3501   int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
3502   if (Num == -1)
3503     return MatchOperand_NoMatch;
3504   // ARMv7 and v8 don't allow cp10/cp11 due to VFP/NEON specific instructions
3505   if ((hasV7Ops() || hasV8Ops()) && (Num == 10 || Num == 11))
3506     return MatchOperand_NoMatch;
3507 
3508   Parser.Lex(); // Eat identifier token.
3509   Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
3510   return MatchOperand_Success;
3511 }
3512 
3513 /// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
3514 /// token must be an Identifier when called, and if it is a coprocessor
3515 /// number, the token is eaten and the operand is added to the operand list.
3516 ARMAsmParser::OperandMatchResultTy
3517 ARMAsmParser::parseCoprocRegOperand(OperandVector &Operands) {
3518   MCAsmParser &Parser = getParser();
3519   SMLoc S = Parser.getTok().getLoc();
3520   const AsmToken &Tok = Parser.getTok();
3521   if (Tok.isNot(AsmToken::Identifier))
3522     return MatchOperand_NoMatch;
3523 
3524   int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
3525   if (Reg == -1)
3526     return MatchOperand_NoMatch;
3527 
3528   Parser.Lex(); // Eat identifier token.
3529   Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
3530   return MatchOperand_Success;
3531 }
3532 
3533 /// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
3534 /// coproc_option : '{' imm0_255 '}'
3535 ARMAsmParser::OperandMatchResultTy
3536 ARMAsmParser::parseCoprocOptionOperand(OperandVector &Operands) {
3537   MCAsmParser &Parser = getParser();
3538   SMLoc S = Parser.getTok().getLoc();
3539 
3540   // If this isn't a '{', this isn't a coprocessor immediate operand.
3541   if (Parser.getTok().isNot(AsmToken::LCurly))
3542     return MatchOperand_NoMatch;
3543   Parser.Lex(); // Eat the '{'
3544 
3545   const MCExpr *Expr;
3546   SMLoc Loc = Parser.getTok().getLoc();
3547   if (getParser().parseExpression(Expr)) {
3548     Error(Loc, "illegal expression");
3549     return MatchOperand_ParseFail;
3550   }
3551   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3552   if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
3553     Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
3554     return MatchOperand_ParseFail;
3555   }
3556   int Val = CE->getValue();
3557 
3558   // Check for and consume the closing '}'
3559   if (Parser.getTok().isNot(AsmToken::RCurly))
3560     return MatchOperand_ParseFail;
3561   SMLoc E = Parser.getTok().getEndLoc();
3562   Parser.Lex(); // Eat the '}'
3563 
3564   Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
3565   return MatchOperand_Success;
3566 }
3567 
3568 // For register list parsing, we need to map from raw GPR register numbering
3569 // to the enumeration values. The enumeration values aren't sorted by
3570 // register number due to our using "sp", "lr" and "pc" as canonical names.
3571 static unsigned getNextRegister(unsigned Reg) {
3572   // If this is a GPR, we need to do it manually, otherwise we can rely
3573   // on the sort ordering of the enumeration since the other reg-classes
3574   // are sane.
3575   if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
3576     return Reg + 1;
3577   switch(Reg) {
3578   default: llvm_unreachable("Invalid GPR number!");
3579   case ARM::R0:  return ARM::R1;  case ARM::R1:  return ARM::R2;
3580   case ARM::R2:  return ARM::R3;  case ARM::R3:  return ARM::R4;
3581   case ARM::R4:  return ARM::R5;  case ARM::R5:  return ARM::R6;
3582   case ARM::R6:  return ARM::R7;  case ARM::R7:  return ARM::R8;
3583   case ARM::R8:  return ARM::R9;  case ARM::R9:  return ARM::R10;
3584   case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
3585   case ARM::R12: return ARM::SP;  case ARM::SP:  return ARM::LR;
3586   case ARM::LR:  return ARM::PC;  case ARM::PC:  return ARM::R0;
3587   }
3588 }
3589 
3590 // Return the low-subreg of a given Q register.
3591 static unsigned getDRegFromQReg(unsigned QReg) {
3592   switch (QReg) {
3593   default: llvm_unreachable("expected a Q register!");
3594   case ARM::Q0:  return ARM::D0;
3595   case ARM::Q1:  return ARM::D2;
3596   case ARM::Q2:  return ARM::D4;
3597   case ARM::Q3:  return ARM::D6;
3598   case ARM::Q4:  return ARM::D8;
3599   case ARM::Q5:  return ARM::D10;
3600   case ARM::Q6:  return ARM::D12;
3601   case ARM::Q7:  return ARM::D14;
3602   case ARM::Q8:  return ARM::D16;
3603   case ARM::Q9:  return ARM::D18;
3604   case ARM::Q10: return ARM::D20;
3605   case ARM::Q11: return ARM::D22;
3606   case ARM::Q12: return ARM::D24;
3607   case ARM::Q13: return ARM::D26;
3608   case ARM::Q14: return ARM::D28;
3609   case ARM::Q15: return ARM::D30;
3610   }
3611 }
3612 
3613 /// Parse a register list.
3614 bool ARMAsmParser::parseRegisterList(OperandVector &Operands) {
3615   MCAsmParser &Parser = getParser();
3616   assert(Parser.getTok().is(AsmToken::LCurly) &&
3617          "Token is not a Left Curly Brace");
3618   SMLoc S = Parser.getTok().getLoc();
3619   Parser.Lex(); // Eat '{' token.
3620   SMLoc RegLoc = Parser.getTok().getLoc();
3621 
3622   // Check the first register in the list to see what register class
3623   // this is a list of.
3624   int Reg = tryParseRegister();
3625   if (Reg == -1)
3626     return Error(RegLoc, "register expected");
3627 
3628   // The reglist instructions have at most 16 registers, so reserve
3629   // space for that many.
3630   int EReg = 0;
3631   SmallVector<std::pair<unsigned, unsigned>, 16> Registers;
3632 
3633   // Allow Q regs and just interpret them as the two D sub-registers.
3634   if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3635     Reg = getDRegFromQReg(Reg);
3636     EReg = MRI->getEncodingValue(Reg);
3637     Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3638     ++Reg;
3639   }
3640   const MCRegisterClass *RC;
3641   if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
3642     RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
3643   else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
3644     RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
3645   else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
3646     RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
3647   else
3648     return Error(RegLoc, "invalid register in register list");
3649 
3650   // Store the register.
3651   EReg = MRI->getEncodingValue(Reg);
3652   Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3653 
3654   // This starts immediately after the first register token in the list,
3655   // so we can see either a comma or a minus (range separator) as a legal
3656   // next token.
3657   while (Parser.getTok().is(AsmToken::Comma) ||
3658          Parser.getTok().is(AsmToken::Minus)) {
3659     if (Parser.getTok().is(AsmToken::Minus)) {
3660       Parser.Lex(); // Eat the minus.
3661       SMLoc AfterMinusLoc = Parser.getTok().getLoc();
3662       int EndReg = tryParseRegister();
3663       if (EndReg == -1)
3664         return Error(AfterMinusLoc, "register expected");
3665       // Allow Q regs and just interpret them as the two D sub-registers.
3666       if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
3667         EndReg = getDRegFromQReg(EndReg) + 1;
3668       // If the register is the same as the start reg, there's nothing
3669       // more to do.
3670       if (Reg == EndReg)
3671         continue;
3672       // The register must be in the same register class as the first.
3673       if (!RC->contains(EndReg))
3674         return Error(AfterMinusLoc, "invalid register in register list");
3675       // Ranges must go from low to high.
3676       if (MRI->getEncodingValue(Reg) > MRI->getEncodingValue(EndReg))
3677         return Error(AfterMinusLoc, "bad range in register list");
3678 
3679       // Add all the registers in the range to the register list.
3680       while (Reg != EndReg) {
3681         Reg = getNextRegister(Reg);
3682         EReg = MRI->getEncodingValue(Reg);
3683         Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3684       }
3685       continue;
3686     }
3687     Parser.Lex(); // Eat the comma.
3688     RegLoc = Parser.getTok().getLoc();
3689     int OldReg = Reg;
3690     const AsmToken RegTok = Parser.getTok();
3691     Reg = tryParseRegister();
3692     if (Reg == -1)
3693       return Error(RegLoc, "register expected");
3694     // Allow Q regs and just interpret them as the two D sub-registers.
3695     bool isQReg = false;
3696     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3697       Reg = getDRegFromQReg(Reg);
3698       isQReg = true;
3699     }
3700     // The register must be in the same register class as the first.
3701     if (!RC->contains(Reg))
3702       return Error(RegLoc, "invalid register in register list");
3703     // List must be monotonically increasing.
3704     if (MRI->getEncodingValue(Reg) < MRI->getEncodingValue(OldReg)) {
3705       if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
3706         Warning(RegLoc, "register list not in ascending order");
3707       else
3708         return Error(RegLoc, "register list not in ascending order");
3709     }
3710     if (MRI->getEncodingValue(Reg) == MRI->getEncodingValue(OldReg)) {
3711       Warning(RegLoc, "duplicated register (" + RegTok.getString() +
3712               ") in register list");
3713       continue;
3714     }
3715     // VFP register lists must also be contiguous.
3716     if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
3717         Reg != OldReg + 1)
3718       return Error(RegLoc, "non-contiguous register range");
3719     EReg = MRI->getEncodingValue(Reg);
3720     Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3721     if (isQReg) {
3722       EReg = MRI->getEncodingValue(++Reg);
3723       Registers.push_back(std::pair<unsigned, unsigned>(EReg, Reg));
3724     }
3725   }
3726 
3727   if (Parser.getTok().isNot(AsmToken::RCurly))
3728     return Error(Parser.getTok().getLoc(), "'}' expected");
3729   SMLoc E = Parser.getTok().getEndLoc();
3730   Parser.Lex(); // Eat '}' token.
3731 
3732   // Push the register list operand.
3733   Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
3734 
3735   // The ARM system instruction variants for LDM/STM have a '^' token here.
3736   if (Parser.getTok().is(AsmToken::Caret)) {
3737     Operands.push_back(ARMOperand::CreateToken("^",Parser.getTok().getLoc()));
3738     Parser.Lex(); // Eat '^' token.
3739   }
3740 
3741   return false;
3742 }
3743 
3744 // Helper function to parse the lane index for vector lists.
3745 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3746 parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index, SMLoc &EndLoc) {
3747   MCAsmParser &Parser = getParser();
3748   Index = 0; // Always return a defined index value.
3749   if (Parser.getTok().is(AsmToken::LBrac)) {
3750     Parser.Lex(); // Eat the '['.
3751     if (Parser.getTok().is(AsmToken::RBrac)) {
3752       // "Dn[]" is the 'all lanes' syntax.
3753       LaneKind = AllLanes;
3754       EndLoc = Parser.getTok().getEndLoc();
3755       Parser.Lex(); // Eat the ']'.
3756       return MatchOperand_Success;
3757     }
3758 
3759     // There's an optional '#' token here. Normally there wouldn't be, but
3760     // inline assemble puts one in, and it's friendly to accept that.
3761     if (Parser.getTok().is(AsmToken::Hash))
3762       Parser.Lex(); // Eat '#' or '$'.
3763 
3764     const MCExpr *LaneIndex;
3765     SMLoc Loc = Parser.getTok().getLoc();
3766     if (getParser().parseExpression(LaneIndex)) {
3767       Error(Loc, "illegal expression");
3768       return MatchOperand_ParseFail;
3769     }
3770     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LaneIndex);
3771     if (!CE) {
3772       Error(Loc, "lane index must be empty or an integer");
3773       return MatchOperand_ParseFail;
3774     }
3775     if (Parser.getTok().isNot(AsmToken::RBrac)) {
3776       Error(Parser.getTok().getLoc(), "']' expected");
3777       return MatchOperand_ParseFail;
3778     }
3779     EndLoc = Parser.getTok().getEndLoc();
3780     Parser.Lex(); // Eat the ']'.
3781     int64_t Val = CE->getValue();
3782 
3783     // FIXME: Make this range check context sensitive for .8, .16, .32.
3784     if (Val < 0 || Val > 7) {
3785       Error(Parser.getTok().getLoc(), "lane index out of range");
3786       return MatchOperand_ParseFail;
3787     }
3788     Index = Val;
3789     LaneKind = IndexedLane;
3790     return MatchOperand_Success;
3791   }
3792   LaneKind = NoLanes;
3793   return MatchOperand_Success;
3794 }
3795 
3796 // parse a vector register list
3797 ARMAsmParser::OperandMatchResultTy
3798 ARMAsmParser::parseVectorList(OperandVector &Operands) {
3799   MCAsmParser &Parser = getParser();
3800   VectorLaneTy LaneKind;
3801   unsigned LaneIndex;
3802   SMLoc S = Parser.getTok().getLoc();
3803   // As an extension (to match gas), support a plain D register or Q register
3804   // (without encosing curly braces) as a single or double entry list,
3805   // respectively.
3806   if (Parser.getTok().is(AsmToken::Identifier)) {
3807     SMLoc E = Parser.getTok().getEndLoc();
3808     int Reg = tryParseRegister();
3809     if (Reg == -1)
3810       return MatchOperand_NoMatch;
3811     if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg)) {
3812       OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex, E);
3813       if (Res != MatchOperand_Success)
3814         return Res;
3815       switch (LaneKind) {
3816       case NoLanes:
3817         Operands.push_back(ARMOperand::CreateVectorList(Reg, 1, false, S, E));
3818         break;
3819       case AllLanes:
3820         Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 1, false,
3821                                                                 S, E));
3822         break;
3823       case IndexedLane:
3824         Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 1,
3825                                                                LaneIndex,
3826                                                                false, S, E));
3827         break;
3828       }
3829       return MatchOperand_Success;
3830     }
3831     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3832       Reg = getDRegFromQReg(Reg);
3833       OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex, E);
3834       if (Res != MatchOperand_Success)
3835         return Res;
3836       switch (LaneKind) {
3837       case NoLanes:
3838         Reg = MRI->getMatchingSuperReg(Reg, ARM::dsub_0,
3839                                    &ARMMCRegisterClasses[ARM::DPairRegClassID]);
3840         Operands.push_back(ARMOperand::CreateVectorList(Reg, 2, false, S, E));
3841         break;
3842       case AllLanes:
3843         Reg = MRI->getMatchingSuperReg(Reg, ARM::dsub_0,
3844                                    &ARMMCRegisterClasses[ARM::DPairRegClassID]);
3845         Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 2, false,
3846                                                                 S, E));
3847         break;
3848       case IndexedLane:
3849         Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 2,
3850                                                                LaneIndex,
3851                                                                false, S, E));
3852         break;
3853       }
3854       return MatchOperand_Success;
3855     }
3856     Error(S, "vector register expected");
3857     return MatchOperand_ParseFail;
3858   }
3859 
3860   if (Parser.getTok().isNot(AsmToken::LCurly))
3861     return MatchOperand_NoMatch;
3862 
3863   Parser.Lex(); // Eat '{' token.
3864   SMLoc RegLoc = Parser.getTok().getLoc();
3865 
3866   int Reg = tryParseRegister();
3867   if (Reg == -1) {
3868     Error(RegLoc, "register expected");
3869     return MatchOperand_ParseFail;
3870   }
3871   unsigned Count = 1;
3872   int Spacing = 0;
3873   unsigned FirstReg = Reg;
3874   // The list is of D registers, but we also allow Q regs and just interpret
3875   // them as the two D sub-registers.
3876   if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3877     FirstReg = Reg = getDRegFromQReg(Reg);
3878     Spacing = 1; // double-spacing requires explicit D registers, otherwise
3879                  // it's ambiguous with four-register single spaced.
3880     ++Reg;
3881     ++Count;
3882   }
3883 
3884   SMLoc E;
3885   if (parseVectorLane(LaneKind, LaneIndex, E) != MatchOperand_Success)
3886     return MatchOperand_ParseFail;
3887 
3888   while (Parser.getTok().is(AsmToken::Comma) ||
3889          Parser.getTok().is(AsmToken::Minus)) {
3890     if (Parser.getTok().is(AsmToken::Minus)) {
3891       if (!Spacing)
3892         Spacing = 1; // Register range implies a single spaced list.
3893       else if (Spacing == 2) {
3894         Error(Parser.getTok().getLoc(),
3895               "sequential registers in double spaced list");
3896         return MatchOperand_ParseFail;
3897       }
3898       Parser.Lex(); // Eat the minus.
3899       SMLoc AfterMinusLoc = Parser.getTok().getLoc();
3900       int EndReg = tryParseRegister();
3901       if (EndReg == -1) {
3902         Error(AfterMinusLoc, "register expected");
3903         return MatchOperand_ParseFail;
3904       }
3905       // Allow Q regs and just interpret them as the two D sub-registers.
3906       if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
3907         EndReg = getDRegFromQReg(EndReg) + 1;
3908       // If the register is the same as the start reg, there's nothing
3909       // more to do.
3910       if (Reg == EndReg)
3911         continue;
3912       // The register must be in the same register class as the first.
3913       if (!ARMMCRegisterClasses[ARM::DPRRegClassID].contains(EndReg)) {
3914         Error(AfterMinusLoc, "invalid register in register list");
3915         return MatchOperand_ParseFail;
3916       }
3917       // Ranges must go from low to high.
3918       if (Reg > EndReg) {
3919         Error(AfterMinusLoc, "bad range in register list");
3920         return MatchOperand_ParseFail;
3921       }
3922       // Parse the lane specifier if present.
3923       VectorLaneTy NextLaneKind;
3924       unsigned NextLaneIndex;
3925       if (parseVectorLane(NextLaneKind, NextLaneIndex, E) !=
3926           MatchOperand_Success)
3927         return MatchOperand_ParseFail;
3928       if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3929         Error(AfterMinusLoc, "mismatched lane index in register list");
3930         return MatchOperand_ParseFail;
3931       }
3932 
3933       // Add all the registers in the range to the register list.
3934       Count += EndReg - Reg;
3935       Reg = EndReg;
3936       continue;
3937     }
3938     Parser.Lex(); // Eat the comma.
3939     RegLoc = Parser.getTok().getLoc();
3940     int OldReg = Reg;
3941     Reg = tryParseRegister();
3942     if (Reg == -1) {
3943       Error(RegLoc, "register expected");
3944       return MatchOperand_ParseFail;
3945     }
3946     // vector register lists must be contiguous.
3947     // It's OK to use the enumeration values directly here rather, as the
3948     // VFP register classes have the enum sorted properly.
3949     //
3950     // The list is of D registers, but we also allow Q regs and just interpret
3951     // them as the two D sub-registers.
3952     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3953       if (!Spacing)
3954         Spacing = 1; // Register range implies a single spaced list.
3955       else if (Spacing == 2) {
3956         Error(RegLoc,
3957               "invalid register in double-spaced list (must be 'D' register')");
3958         return MatchOperand_ParseFail;
3959       }
3960       Reg = getDRegFromQReg(Reg);
3961       if (Reg != OldReg + 1) {
3962         Error(RegLoc, "non-contiguous register range");
3963         return MatchOperand_ParseFail;
3964       }
3965       ++Reg;
3966       Count += 2;
3967       // Parse the lane specifier if present.
3968       VectorLaneTy NextLaneKind;
3969       unsigned NextLaneIndex;
3970       SMLoc LaneLoc = Parser.getTok().getLoc();
3971       if (parseVectorLane(NextLaneKind, NextLaneIndex, E) !=
3972           MatchOperand_Success)
3973         return MatchOperand_ParseFail;
3974       if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3975         Error(LaneLoc, "mismatched lane index in register list");
3976         return MatchOperand_ParseFail;
3977       }
3978       continue;
3979     }
3980     // Normal D register.
3981     // Figure out the register spacing (single or double) of the list if
3982     // we don't know it already.
3983     if (!Spacing)
3984       Spacing = 1 + (Reg == OldReg + 2);
3985 
3986     // Just check that it's contiguous and keep going.
3987     if (Reg != OldReg + Spacing) {
3988       Error(RegLoc, "non-contiguous register range");
3989       return MatchOperand_ParseFail;
3990     }
3991     ++Count;
3992     // Parse the lane specifier if present.
3993     VectorLaneTy NextLaneKind;
3994     unsigned NextLaneIndex;
3995     SMLoc EndLoc = Parser.getTok().getLoc();
3996     if (parseVectorLane(NextLaneKind, NextLaneIndex, E) != MatchOperand_Success)
3997       return MatchOperand_ParseFail;
3998     if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3999       Error(EndLoc, "mismatched lane index in register list");
4000       return MatchOperand_ParseFail;
4001     }
4002   }
4003 
4004   if (Parser.getTok().isNot(AsmToken::RCurly)) {
4005     Error(Parser.getTok().getLoc(), "'}' expected");
4006     return MatchOperand_ParseFail;
4007   }
4008   E = Parser.getTok().getEndLoc();
4009   Parser.Lex(); // Eat '}' token.
4010 
4011   switch (LaneKind) {
4012   case NoLanes:
4013     // Two-register operands have been converted to the
4014     // composite register classes.
4015     if (Count == 2) {
4016       const MCRegisterClass *RC = (Spacing == 1) ?
4017         &ARMMCRegisterClasses[ARM::DPairRegClassID] :
4018         &ARMMCRegisterClasses[ARM::DPairSpcRegClassID];
4019       FirstReg = MRI->getMatchingSuperReg(FirstReg, ARM::dsub_0, RC);
4020     }
4021 
4022     Operands.push_back(ARMOperand::CreateVectorList(FirstReg, Count,
4023                                                     (Spacing == 2), S, E));
4024     break;
4025   case AllLanes:
4026     // Two-register operands have been converted to the
4027     // composite register classes.
4028     if (Count == 2) {
4029       const MCRegisterClass *RC = (Spacing == 1) ?
4030         &ARMMCRegisterClasses[ARM::DPairRegClassID] :
4031         &ARMMCRegisterClasses[ARM::DPairSpcRegClassID];
4032       FirstReg = MRI->getMatchingSuperReg(FirstReg, ARM::dsub_0, RC);
4033     }
4034     Operands.push_back(ARMOperand::CreateVectorListAllLanes(FirstReg, Count,
4035                                                             (Spacing == 2),
4036                                                             S, E));
4037     break;
4038   case IndexedLane:
4039     Operands.push_back(ARMOperand::CreateVectorListIndexed(FirstReg, Count,
4040                                                            LaneIndex,
4041                                                            (Spacing == 2),
4042                                                            S, E));
4043     break;
4044   }
4045   return MatchOperand_Success;
4046 }
4047 
4048 /// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
4049 ARMAsmParser::OperandMatchResultTy
4050 ARMAsmParser::parseMemBarrierOptOperand(OperandVector &Operands) {
4051   MCAsmParser &Parser = getParser();
4052   SMLoc S = Parser.getTok().getLoc();
4053   const AsmToken &Tok = Parser.getTok();
4054   unsigned Opt;
4055 
4056   if (Tok.is(AsmToken::Identifier)) {
4057     StringRef OptStr = Tok.getString();
4058 
4059     Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()).lower())
4060       .Case("sy",    ARM_MB::SY)
4061       .Case("st",    ARM_MB::ST)
4062       .Case("ld",    ARM_MB::LD)
4063       .Case("sh",    ARM_MB::ISH)
4064       .Case("ish",   ARM_MB::ISH)
4065       .Case("shst",  ARM_MB::ISHST)
4066       .Case("ishst", ARM_MB::ISHST)
4067       .Case("ishld", ARM_MB::ISHLD)
4068       .Case("nsh",   ARM_MB::NSH)
4069       .Case("un",    ARM_MB::NSH)
4070       .Case("nshst", ARM_MB::NSHST)
4071       .Case("nshld", ARM_MB::NSHLD)
4072       .Case("unst",  ARM_MB::NSHST)
4073       .Case("osh",   ARM_MB::OSH)
4074       .Case("oshst", ARM_MB::OSHST)
4075       .Case("oshld", ARM_MB::OSHLD)
4076       .Default(~0U);
4077 
4078     // ishld, oshld, nshld and ld are only available from ARMv8.
4079     if (!hasV8Ops() && (Opt == ARM_MB::ISHLD || Opt == ARM_MB::OSHLD ||
4080                         Opt == ARM_MB::NSHLD || Opt == ARM_MB::LD))
4081       Opt = ~0U;
4082 
4083     if (Opt == ~0U)
4084       return MatchOperand_NoMatch;
4085 
4086     Parser.Lex(); // Eat identifier token.
4087   } else if (Tok.is(AsmToken::Hash) ||
4088              Tok.is(AsmToken::Dollar) ||
4089              Tok.is(AsmToken::Integer)) {
4090     if (Parser.getTok().isNot(AsmToken::Integer))
4091       Parser.Lex(); // Eat '#' or '$'.
4092     SMLoc Loc = Parser.getTok().getLoc();
4093 
4094     const MCExpr *MemBarrierID;
4095     if (getParser().parseExpression(MemBarrierID)) {
4096       Error(Loc, "illegal expression");
4097       return MatchOperand_ParseFail;
4098     }
4099 
4100     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(MemBarrierID);
4101     if (!CE) {
4102       Error(Loc, "constant expression expected");
4103       return MatchOperand_ParseFail;
4104     }
4105 
4106     int Val = CE->getValue();
4107     if (Val & ~0xf) {
4108       Error(Loc, "immediate value out of range");
4109       return MatchOperand_ParseFail;
4110     }
4111 
4112     Opt = ARM_MB::RESERVED_0 + Val;
4113   } else
4114     return MatchOperand_ParseFail;
4115 
4116   Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
4117   return MatchOperand_Success;
4118 }
4119 
4120 /// parseInstSyncBarrierOptOperand - Try to parse ISB inst sync barrier options.
4121 ARMAsmParser::OperandMatchResultTy
4122 ARMAsmParser::parseInstSyncBarrierOptOperand(OperandVector &Operands) {
4123   MCAsmParser &Parser = getParser();
4124   SMLoc S = Parser.getTok().getLoc();
4125   const AsmToken &Tok = Parser.getTok();
4126   unsigned Opt;
4127 
4128   if (Tok.is(AsmToken::Identifier)) {
4129     StringRef OptStr = Tok.getString();
4130 
4131     if (OptStr.equals_lower("sy"))
4132       Opt = ARM_ISB::SY;
4133     else
4134       return MatchOperand_NoMatch;
4135 
4136     Parser.Lex(); // Eat identifier token.
4137   } else if (Tok.is(AsmToken::Hash) ||
4138              Tok.is(AsmToken::Dollar) ||
4139              Tok.is(AsmToken::Integer)) {
4140     if (Parser.getTok().isNot(AsmToken::Integer))
4141       Parser.Lex(); // Eat '#' or '$'.
4142     SMLoc Loc = Parser.getTok().getLoc();
4143 
4144     const MCExpr *ISBarrierID;
4145     if (getParser().parseExpression(ISBarrierID)) {
4146       Error(Loc, "illegal expression");
4147       return MatchOperand_ParseFail;
4148     }
4149 
4150     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ISBarrierID);
4151     if (!CE) {
4152       Error(Loc, "constant expression expected");
4153       return MatchOperand_ParseFail;
4154     }
4155 
4156     int Val = CE->getValue();
4157     if (Val & ~0xf) {
4158       Error(Loc, "immediate value out of range");
4159       return MatchOperand_ParseFail;
4160     }
4161 
4162     Opt = ARM_ISB::RESERVED_0 + Val;
4163   } else
4164     return MatchOperand_ParseFail;
4165 
4166   Operands.push_back(ARMOperand::CreateInstSyncBarrierOpt(
4167           (ARM_ISB::InstSyncBOpt)Opt, S));
4168   return MatchOperand_Success;
4169 }
4170 
4171 
4172 /// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
4173 ARMAsmParser::OperandMatchResultTy
4174 ARMAsmParser::parseProcIFlagsOperand(OperandVector &Operands) {
4175   MCAsmParser &Parser = getParser();
4176   SMLoc S = Parser.getTok().getLoc();
4177   const AsmToken &Tok = Parser.getTok();
4178   if (!Tok.is(AsmToken::Identifier))
4179     return MatchOperand_NoMatch;
4180   StringRef IFlagsStr = Tok.getString();
4181 
4182   // An iflags string of "none" is interpreted to mean that none of the AIF
4183   // bits are set.  Not a terribly useful instruction, but a valid encoding.
4184   unsigned IFlags = 0;
4185   if (IFlagsStr != "none") {
4186         for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
4187       unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
4188         .Case("a", ARM_PROC::A)
4189         .Case("i", ARM_PROC::I)
4190         .Case("f", ARM_PROC::F)
4191         .Default(~0U);
4192 
4193       // If some specific iflag is already set, it means that some letter is
4194       // present more than once, this is not acceptable.
4195       if (Flag == ~0U || (IFlags & Flag))
4196         return MatchOperand_NoMatch;
4197 
4198       IFlags |= Flag;
4199     }
4200   }
4201 
4202   Parser.Lex(); // Eat identifier token.
4203   Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
4204   return MatchOperand_Success;
4205 }
4206 
4207 /// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
4208 ARMAsmParser::OperandMatchResultTy
4209 ARMAsmParser::parseMSRMaskOperand(OperandVector &Operands) {
4210   MCAsmParser &Parser = getParser();
4211   SMLoc S = Parser.getTok().getLoc();
4212   const AsmToken &Tok = Parser.getTok();
4213   if (!Tok.is(AsmToken::Identifier))
4214     return MatchOperand_NoMatch;
4215   StringRef Mask = Tok.getString();
4216 
4217   if (isMClass()) {
4218     // See ARMv6-M 10.1.1
4219     std::string Name = Mask.lower();
4220     unsigned FlagsVal = StringSwitch<unsigned>(Name)
4221       // Note: in the documentation:
4222       //  ARM deprecates using MSR APSR without a _<bits> qualifier as an alias
4223       //  for MSR APSR_nzcvq.
4224       // but we do make it an alias here.  This is so to get the "mask encoding"
4225       // bits correct on MSR APSR writes.
4226       //
4227       // FIXME: Note the 0xc00 "mask encoding" bits version of the registers
4228       // should really only be allowed when writing a special register.  Note
4229       // they get dropped in the MRS instruction reading a special register as
4230       // the SYSm field is only 8 bits.
4231       .Case("apsr", 0x800)
4232       .Case("apsr_nzcvq", 0x800)
4233       .Case("apsr_g", 0x400)
4234       .Case("apsr_nzcvqg", 0xc00)
4235       .Case("iapsr", 0x801)
4236       .Case("iapsr_nzcvq", 0x801)
4237       .Case("iapsr_g", 0x401)
4238       .Case("iapsr_nzcvqg", 0xc01)
4239       .Case("eapsr", 0x802)
4240       .Case("eapsr_nzcvq", 0x802)
4241       .Case("eapsr_g", 0x402)
4242       .Case("eapsr_nzcvqg", 0xc02)
4243       .Case("xpsr", 0x803)
4244       .Case("xpsr_nzcvq", 0x803)
4245       .Case("xpsr_g", 0x403)
4246       .Case("xpsr_nzcvqg", 0xc03)
4247       .Case("ipsr", 0x805)
4248       .Case("epsr", 0x806)
4249       .Case("iepsr", 0x807)
4250       .Case("msp", 0x808)
4251       .Case("psp", 0x809)
4252       .Case("primask", 0x810)
4253       .Case("basepri", 0x811)
4254       .Case("basepri_max", 0x812)
4255       .Case("faultmask", 0x813)
4256       .Case("control", 0x814)
4257       .Case("msplim", 0x80a)
4258       .Case("psplim", 0x80b)
4259       .Case("msp_ns", 0x888)
4260       .Case("psp_ns", 0x889)
4261       .Case("msplim_ns", 0x88a)
4262       .Case("psplim_ns", 0x88b)
4263       .Case("primask_ns", 0x890)
4264       .Case("basepri_ns", 0x891)
4265       .Case("basepri_max_ns", 0x892)
4266       .Case("faultmask_ns", 0x893)
4267       .Case("control_ns", 0x894)
4268       .Case("sp_ns", 0x898)
4269       .Default(~0U);
4270 
4271     if (FlagsVal == ~0U)
4272       return MatchOperand_NoMatch;
4273 
4274     if (!hasDSP() && (FlagsVal & 0x400))
4275       // The _g and _nzcvqg versions are only valid if the DSP extension is
4276       // available.
4277       return MatchOperand_NoMatch;
4278 
4279     if (!hasV7Ops() && FlagsVal >= 0x811 && FlagsVal <= 0x813)
4280       // basepri, basepri_max and faultmask only valid for V7m.
4281       return MatchOperand_NoMatch;
4282 
4283     if (!has8MSecExt() && (FlagsVal == 0x80a || FlagsVal == 0x80b ||
4284                              (FlagsVal > 0x814 && FlagsVal < 0xc00)))
4285       return MatchOperand_NoMatch;
4286 
4287     if (!hasV8MMainline() && (FlagsVal == 0x88a || FlagsVal == 0x88b ||
4288                               (FlagsVal > 0x890 && FlagsVal <= 0x893)))
4289       return MatchOperand_NoMatch;
4290 
4291     Parser.Lex(); // Eat identifier token.
4292     Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
4293     return MatchOperand_Success;
4294   }
4295 
4296   // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
4297   size_t Start = 0, Next = Mask.find('_');
4298   StringRef Flags = "";
4299   std::string SpecReg = Mask.slice(Start, Next).lower();
4300   if (Next != StringRef::npos)
4301     Flags = Mask.slice(Next+1, Mask.size());
4302 
4303   // FlagsVal contains the complete mask:
4304   // 3-0: Mask
4305   // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
4306   unsigned FlagsVal = 0;
4307 
4308   if (SpecReg == "apsr") {
4309     FlagsVal = StringSwitch<unsigned>(Flags)
4310     .Case("nzcvq",  0x8) // same as CPSR_f
4311     .Case("g",      0x4) // same as CPSR_s
4312     .Case("nzcvqg", 0xc) // same as CPSR_fs
4313     .Default(~0U);
4314 
4315     if (FlagsVal == ~0U) {
4316       if (!Flags.empty())
4317         return MatchOperand_NoMatch;
4318       else
4319         FlagsVal = 8; // No flag
4320     }
4321   } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
4322     // cpsr_all is an alias for cpsr_fc, as is plain cpsr.
4323     if (Flags == "all" || Flags == "")
4324       Flags = "fc";
4325     for (int i = 0, e = Flags.size(); i != e; ++i) {
4326       unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
4327       .Case("c", 1)
4328       .Case("x", 2)
4329       .Case("s", 4)
4330       .Case("f", 8)
4331       .Default(~0U);
4332 
4333       // If some specific flag is already set, it means that some letter is
4334       // present more than once, this is not acceptable.
4335       if (FlagsVal == ~0U || (FlagsVal & Flag))
4336         return MatchOperand_NoMatch;
4337       FlagsVal |= Flag;
4338     }
4339   } else // No match for special register.
4340     return MatchOperand_NoMatch;
4341 
4342   // Special register without flags is NOT equivalent to "fc" flags.
4343   // NOTE: This is a divergence from gas' behavior.  Uncommenting the following
4344   // two lines would enable gas compatibility at the expense of breaking
4345   // round-tripping.
4346   //
4347   // if (!FlagsVal)
4348   //  FlagsVal = 0x9;
4349 
4350   // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
4351   if (SpecReg == "spsr")
4352     FlagsVal |= 16;
4353 
4354   Parser.Lex(); // Eat identifier token.
4355   Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
4356   return MatchOperand_Success;
4357 }
4358 
4359 /// parseBankedRegOperand - Try to parse a banked register (e.g. "lr_irq") for
4360 /// use in the MRS/MSR instructions added to support virtualization.
4361 ARMAsmParser::OperandMatchResultTy
4362 ARMAsmParser::parseBankedRegOperand(OperandVector &Operands) {
4363   MCAsmParser &Parser = getParser();
4364   SMLoc S = Parser.getTok().getLoc();
4365   const AsmToken &Tok = Parser.getTok();
4366   if (!Tok.is(AsmToken::Identifier))
4367     return MatchOperand_NoMatch;
4368   StringRef RegName = Tok.getString();
4369 
4370   // The values here come from B9.2.3 of the ARM ARM, where bits 4-0 are SysM
4371   // and bit 5 is R.
4372   unsigned Encoding = StringSwitch<unsigned>(RegName.lower())
4373                           .Case("r8_usr", 0x00)
4374                           .Case("r9_usr", 0x01)
4375                           .Case("r10_usr", 0x02)
4376                           .Case("r11_usr", 0x03)
4377                           .Case("r12_usr", 0x04)
4378                           .Case("sp_usr", 0x05)
4379                           .Case("lr_usr", 0x06)
4380                           .Case("r8_fiq", 0x08)
4381                           .Case("r9_fiq", 0x09)
4382                           .Case("r10_fiq", 0x0a)
4383                           .Case("r11_fiq", 0x0b)
4384                           .Case("r12_fiq", 0x0c)
4385                           .Case("sp_fiq", 0x0d)
4386                           .Case("lr_fiq", 0x0e)
4387                           .Case("lr_irq", 0x10)
4388                           .Case("sp_irq", 0x11)
4389                           .Case("lr_svc", 0x12)
4390                           .Case("sp_svc", 0x13)
4391                           .Case("lr_abt", 0x14)
4392                           .Case("sp_abt", 0x15)
4393                           .Case("lr_und", 0x16)
4394                           .Case("sp_und", 0x17)
4395                           .Case("lr_mon", 0x1c)
4396                           .Case("sp_mon", 0x1d)
4397                           .Case("elr_hyp", 0x1e)
4398                           .Case("sp_hyp", 0x1f)
4399                           .Case("spsr_fiq", 0x2e)
4400                           .Case("spsr_irq", 0x30)
4401                           .Case("spsr_svc", 0x32)
4402                           .Case("spsr_abt", 0x34)
4403                           .Case("spsr_und", 0x36)
4404                           .Case("spsr_mon", 0x3c)
4405                           .Case("spsr_hyp", 0x3e)
4406                           .Default(~0U);
4407 
4408   if (Encoding == ~0U)
4409     return MatchOperand_NoMatch;
4410 
4411   Parser.Lex(); // Eat identifier token.
4412   Operands.push_back(ARMOperand::CreateBankedReg(Encoding, S));
4413   return MatchOperand_Success;
4414 }
4415 
4416 ARMAsmParser::OperandMatchResultTy
4417 ARMAsmParser::parsePKHImm(OperandVector &Operands, StringRef Op, int Low,
4418                           int High) {
4419   MCAsmParser &Parser = getParser();
4420   const AsmToken &Tok = Parser.getTok();
4421   if (Tok.isNot(AsmToken::Identifier)) {
4422     Error(Parser.getTok().getLoc(), Op + " operand expected.");
4423     return MatchOperand_ParseFail;
4424   }
4425   StringRef ShiftName = Tok.getString();
4426   std::string LowerOp = Op.lower();
4427   std::string UpperOp = Op.upper();
4428   if (ShiftName != LowerOp && ShiftName != UpperOp) {
4429     Error(Parser.getTok().getLoc(), Op + " operand expected.");
4430     return MatchOperand_ParseFail;
4431   }
4432   Parser.Lex(); // Eat shift type token.
4433 
4434   // There must be a '#' and a shift amount.
4435   if (Parser.getTok().isNot(AsmToken::Hash) &&
4436       Parser.getTok().isNot(AsmToken::Dollar)) {
4437     Error(Parser.getTok().getLoc(), "'#' expected");
4438     return MatchOperand_ParseFail;
4439   }
4440   Parser.Lex(); // Eat hash token.
4441 
4442   const MCExpr *ShiftAmount;
4443   SMLoc Loc = Parser.getTok().getLoc();
4444   SMLoc EndLoc;
4445   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
4446     Error(Loc, "illegal expression");
4447     return MatchOperand_ParseFail;
4448   }
4449   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
4450   if (!CE) {
4451     Error(Loc, "constant expression expected");
4452     return MatchOperand_ParseFail;
4453   }
4454   int Val = CE->getValue();
4455   if (Val < Low || Val > High) {
4456     Error(Loc, "immediate value out of range");
4457     return MatchOperand_ParseFail;
4458   }
4459 
4460   Operands.push_back(ARMOperand::CreateImm(CE, Loc, EndLoc));
4461 
4462   return MatchOperand_Success;
4463 }
4464 
4465 ARMAsmParser::OperandMatchResultTy
4466 ARMAsmParser::parseSetEndImm(OperandVector &Operands) {
4467   MCAsmParser &Parser = getParser();
4468   const AsmToken &Tok = Parser.getTok();
4469   SMLoc S = Tok.getLoc();
4470   if (Tok.isNot(AsmToken::Identifier)) {
4471     Error(S, "'be' or 'le' operand expected");
4472     return MatchOperand_ParseFail;
4473   }
4474   int Val = StringSwitch<int>(Tok.getString().lower())
4475     .Case("be", 1)
4476     .Case("le", 0)
4477     .Default(-1);
4478   Parser.Lex(); // Eat the token.
4479 
4480   if (Val == -1) {
4481     Error(S, "'be' or 'le' operand expected");
4482     return MatchOperand_ParseFail;
4483   }
4484   Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::create(Val,
4485                                                                   getContext()),
4486                                            S, Tok.getEndLoc()));
4487   return MatchOperand_Success;
4488 }
4489 
4490 /// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
4491 /// instructions. Legal values are:
4492 ///     lsl #n  'n' in [0,31]
4493 ///     asr #n  'n' in [1,32]
4494 ///             n == 32 encoded as n == 0.
4495 ARMAsmParser::OperandMatchResultTy
4496 ARMAsmParser::parseShifterImm(OperandVector &Operands) {
4497   MCAsmParser &Parser = getParser();
4498   const AsmToken &Tok = Parser.getTok();
4499   SMLoc S = Tok.getLoc();
4500   if (Tok.isNot(AsmToken::Identifier)) {
4501     Error(S, "shift operator 'asr' or 'lsl' expected");
4502     return MatchOperand_ParseFail;
4503   }
4504   StringRef ShiftName = Tok.getString();
4505   bool isASR;
4506   if (ShiftName == "lsl" || ShiftName == "LSL")
4507     isASR = false;
4508   else if (ShiftName == "asr" || ShiftName == "ASR")
4509     isASR = true;
4510   else {
4511     Error(S, "shift operator 'asr' or 'lsl' expected");
4512     return MatchOperand_ParseFail;
4513   }
4514   Parser.Lex(); // Eat the operator.
4515 
4516   // A '#' and a shift amount.
4517   if (Parser.getTok().isNot(AsmToken::Hash) &&
4518       Parser.getTok().isNot(AsmToken::Dollar)) {
4519     Error(Parser.getTok().getLoc(), "'#' expected");
4520     return MatchOperand_ParseFail;
4521   }
4522   Parser.Lex(); // Eat hash token.
4523   SMLoc ExLoc = Parser.getTok().getLoc();
4524 
4525   const MCExpr *ShiftAmount;
4526   SMLoc EndLoc;
4527   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
4528     Error(ExLoc, "malformed shift expression");
4529     return MatchOperand_ParseFail;
4530   }
4531   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
4532   if (!CE) {
4533     Error(ExLoc, "shift amount must be an immediate");
4534     return MatchOperand_ParseFail;
4535   }
4536 
4537   int64_t Val = CE->getValue();
4538   if (isASR) {
4539     // Shift amount must be in [1,32]
4540     if (Val < 1 || Val > 32) {
4541       Error(ExLoc, "'asr' shift amount must be in range [1,32]");
4542       return MatchOperand_ParseFail;
4543     }
4544     // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
4545     if (isThumb() && Val == 32) {
4546       Error(ExLoc, "'asr #32' shift amount not allowed in Thumb mode");
4547       return MatchOperand_ParseFail;
4548     }
4549     if (Val == 32) Val = 0;
4550   } else {
4551     // Shift amount must be in [1,32]
4552     if (Val < 0 || Val > 31) {
4553       Error(ExLoc, "'lsr' shift amount must be in range [0,31]");
4554       return MatchOperand_ParseFail;
4555     }
4556   }
4557 
4558   Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, EndLoc));
4559 
4560   return MatchOperand_Success;
4561 }
4562 
4563 /// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
4564 /// of instructions. Legal values are:
4565 ///     ror #n  'n' in {0, 8, 16, 24}
4566 ARMAsmParser::OperandMatchResultTy
4567 ARMAsmParser::parseRotImm(OperandVector &Operands) {
4568   MCAsmParser &Parser = getParser();
4569   const AsmToken &Tok = Parser.getTok();
4570   SMLoc S = Tok.getLoc();
4571   if (Tok.isNot(AsmToken::Identifier))
4572     return MatchOperand_NoMatch;
4573   StringRef ShiftName = Tok.getString();
4574   if (ShiftName != "ror" && ShiftName != "ROR")
4575     return MatchOperand_NoMatch;
4576   Parser.Lex(); // Eat the operator.
4577 
4578   // A '#' and a rotate amount.
4579   if (Parser.getTok().isNot(AsmToken::Hash) &&
4580       Parser.getTok().isNot(AsmToken::Dollar)) {
4581     Error(Parser.getTok().getLoc(), "'#' expected");
4582     return MatchOperand_ParseFail;
4583   }
4584   Parser.Lex(); // Eat hash token.
4585   SMLoc ExLoc = Parser.getTok().getLoc();
4586 
4587   const MCExpr *ShiftAmount;
4588   SMLoc EndLoc;
4589   if (getParser().parseExpression(ShiftAmount, EndLoc)) {
4590     Error(ExLoc, "malformed rotate expression");
4591     return MatchOperand_ParseFail;
4592   }
4593   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
4594   if (!CE) {
4595     Error(ExLoc, "rotate amount must be an immediate");
4596     return MatchOperand_ParseFail;
4597   }
4598 
4599   int64_t Val = CE->getValue();
4600   // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
4601   // normally, zero is represented in asm by omitting the rotate operand
4602   // entirely.
4603   if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
4604     Error(ExLoc, "'ror' rotate amount must be 8, 16, or 24");
4605     return MatchOperand_ParseFail;
4606   }
4607 
4608   Operands.push_back(ARMOperand::CreateRotImm(Val, S, EndLoc));
4609 
4610   return MatchOperand_Success;
4611 }
4612 
4613 ARMAsmParser::OperandMatchResultTy
4614 ARMAsmParser::parseModImm(OperandVector &Operands) {
4615   MCAsmParser &Parser = getParser();
4616   MCAsmLexer &Lexer = getLexer();
4617   int64_t Imm1, Imm2;
4618 
4619   SMLoc S = Parser.getTok().getLoc();
4620 
4621   // 1) A mod_imm operand can appear in the place of a register name:
4622   //   add r0, #mod_imm
4623   //   add r0, r0, #mod_imm
4624   // to correctly handle the latter, we bail out as soon as we see an
4625   // identifier.
4626   //
4627   // 2) Similarly, we do not want to parse into complex operands:
4628   //   mov r0, #mod_imm
4629   //   mov r0, :lower16:(_foo)
4630   if (Parser.getTok().is(AsmToken::Identifier) ||
4631       Parser.getTok().is(AsmToken::Colon))
4632     return MatchOperand_NoMatch;
4633 
4634   // Hash (dollar) is optional as per the ARMARM
4635   if (Parser.getTok().is(AsmToken::Hash) ||
4636       Parser.getTok().is(AsmToken::Dollar)) {
4637     // Avoid parsing into complex operands (#:)
4638     if (Lexer.peekTok().is(AsmToken::Colon))
4639       return MatchOperand_NoMatch;
4640 
4641     // Eat the hash (dollar)
4642     Parser.Lex();
4643   }
4644 
4645   SMLoc Sx1, Ex1;
4646   Sx1 = Parser.getTok().getLoc();
4647   const MCExpr *Imm1Exp;
4648   if (getParser().parseExpression(Imm1Exp, Ex1)) {
4649     Error(Sx1, "malformed expression");
4650     return MatchOperand_ParseFail;
4651   }
4652 
4653   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm1Exp);
4654 
4655   if (CE) {
4656     // Immediate must fit within 32-bits
4657     Imm1 = CE->getValue();
4658     int Enc = ARM_AM::getSOImmVal(Imm1);
4659     if (Enc != -1 && Parser.getTok().is(AsmToken::EndOfStatement)) {
4660       // We have a match!
4661       Operands.push_back(ARMOperand::CreateModImm((Enc & 0xFF),
4662                                                   (Enc & 0xF00) >> 7,
4663                                                   Sx1, Ex1));
4664       return MatchOperand_Success;
4665     }
4666 
4667     // We have parsed an immediate which is not for us, fallback to a plain
4668     // immediate. This can happen for instruction aliases. For an example,
4669     // ARMInstrInfo.td defines the alias [mov <-> mvn] which can transform
4670     // a mov (mvn) with a mod_imm_neg/mod_imm_not operand into the opposite
4671     // instruction with a mod_imm operand. The alias is defined such that the
4672     // parser method is shared, that's why we have to do this here.
4673     if (Parser.getTok().is(AsmToken::EndOfStatement)) {
4674       Operands.push_back(ARMOperand::CreateImm(Imm1Exp, Sx1, Ex1));
4675       return MatchOperand_Success;
4676     }
4677   } else {
4678     // Operands like #(l1 - l2) can only be evaluated at a later stage (via an
4679     // MCFixup). Fallback to a plain immediate.
4680     Operands.push_back(ARMOperand::CreateImm(Imm1Exp, Sx1, Ex1));
4681     return MatchOperand_Success;
4682   }
4683 
4684   // From this point onward, we expect the input to be a (#bits, #rot) pair
4685   if (Parser.getTok().isNot(AsmToken::Comma)) {
4686     Error(Sx1, "expected modified immediate operand: #[0, 255], #even[0-30]");
4687     return MatchOperand_ParseFail;
4688   }
4689 
4690   if (Imm1 & ~0xFF) {
4691     Error(Sx1, "immediate operand must a number in the range [0, 255]");
4692     return MatchOperand_ParseFail;
4693   }
4694 
4695   // Eat the comma
4696   Parser.Lex();
4697 
4698   // Repeat for #rot
4699   SMLoc Sx2, Ex2;
4700   Sx2 = Parser.getTok().getLoc();
4701 
4702   // Eat the optional hash (dollar)
4703   if (Parser.getTok().is(AsmToken::Hash) ||
4704       Parser.getTok().is(AsmToken::Dollar))
4705     Parser.Lex();
4706 
4707   const MCExpr *Imm2Exp;
4708   if (getParser().parseExpression(Imm2Exp, Ex2)) {
4709     Error(Sx2, "malformed expression");
4710     return MatchOperand_ParseFail;
4711   }
4712 
4713   CE = dyn_cast<MCConstantExpr>(Imm2Exp);
4714 
4715   if (CE) {
4716     Imm2 = CE->getValue();
4717     if (!(Imm2 & ~0x1E)) {
4718       // We have a match!
4719       Operands.push_back(ARMOperand::CreateModImm(Imm1, Imm2, S, Ex2));
4720       return MatchOperand_Success;
4721     }
4722     Error(Sx2, "immediate operand must an even number in the range [0, 30]");
4723     return MatchOperand_ParseFail;
4724   } else {
4725     Error(Sx2, "constant expression expected");
4726     return MatchOperand_ParseFail;
4727   }
4728 }
4729 
4730 ARMAsmParser::OperandMatchResultTy
4731 ARMAsmParser::parseBitfield(OperandVector &Operands) {
4732   MCAsmParser &Parser = getParser();
4733   SMLoc S = Parser.getTok().getLoc();
4734   // The bitfield descriptor is really two operands, the LSB and the width.
4735   if (Parser.getTok().isNot(AsmToken::Hash) &&
4736       Parser.getTok().isNot(AsmToken::Dollar)) {
4737     Error(Parser.getTok().getLoc(), "'#' expected");
4738     return MatchOperand_ParseFail;
4739   }
4740   Parser.Lex(); // Eat hash token.
4741 
4742   const MCExpr *LSBExpr;
4743   SMLoc E = Parser.getTok().getLoc();
4744   if (getParser().parseExpression(LSBExpr)) {
4745     Error(E, "malformed immediate expression");
4746     return MatchOperand_ParseFail;
4747   }
4748   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
4749   if (!CE) {
4750     Error(E, "'lsb' operand must be an immediate");
4751     return MatchOperand_ParseFail;
4752   }
4753 
4754   int64_t LSB = CE->getValue();
4755   // The LSB must be in the range [0,31]
4756   if (LSB < 0 || LSB > 31) {
4757     Error(E, "'lsb' operand must be in the range [0,31]");
4758     return MatchOperand_ParseFail;
4759   }
4760   E = Parser.getTok().getLoc();
4761 
4762   // Expect another immediate operand.
4763   if (Parser.getTok().isNot(AsmToken::Comma)) {
4764     Error(Parser.getTok().getLoc(), "too few operands");
4765     return MatchOperand_ParseFail;
4766   }
4767   Parser.Lex(); // Eat hash token.
4768   if (Parser.getTok().isNot(AsmToken::Hash) &&
4769       Parser.getTok().isNot(AsmToken::Dollar)) {
4770     Error(Parser.getTok().getLoc(), "'#' expected");
4771     return MatchOperand_ParseFail;
4772   }
4773   Parser.Lex(); // Eat hash token.
4774 
4775   const MCExpr *WidthExpr;
4776   SMLoc EndLoc;
4777   if (getParser().parseExpression(WidthExpr, EndLoc)) {
4778     Error(E, "malformed immediate expression");
4779     return MatchOperand_ParseFail;
4780   }
4781   CE = dyn_cast<MCConstantExpr>(WidthExpr);
4782   if (!CE) {
4783     Error(E, "'width' operand must be an immediate");
4784     return MatchOperand_ParseFail;
4785   }
4786 
4787   int64_t Width = CE->getValue();
4788   // The LSB must be in the range [1,32-lsb]
4789   if (Width < 1 || Width > 32 - LSB) {
4790     Error(E, "'width' operand must be in the range [1,32-lsb]");
4791     return MatchOperand_ParseFail;
4792   }
4793 
4794   Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, EndLoc));
4795 
4796   return MatchOperand_Success;
4797 }
4798 
4799 ARMAsmParser::OperandMatchResultTy
4800 ARMAsmParser::parsePostIdxReg(OperandVector &Operands) {
4801   // Check for a post-index addressing register operand. Specifically:
4802   // postidx_reg := '+' register {, shift}
4803   //              | '-' register {, shift}
4804   //              | register {, shift}
4805 
4806   // This method must return MatchOperand_NoMatch without consuming any tokens
4807   // in the case where there is no match, as other alternatives take other
4808   // parse methods.
4809   MCAsmParser &Parser = getParser();
4810   AsmToken Tok = Parser.getTok();
4811   SMLoc S = Tok.getLoc();
4812   bool haveEaten = false;
4813   bool isAdd = true;
4814   if (Tok.is(AsmToken::Plus)) {
4815     Parser.Lex(); // Eat the '+' token.
4816     haveEaten = true;
4817   } else if (Tok.is(AsmToken::Minus)) {
4818     Parser.Lex(); // Eat the '-' token.
4819     isAdd = false;
4820     haveEaten = true;
4821   }
4822 
4823   SMLoc E = Parser.getTok().getEndLoc();
4824   int Reg = tryParseRegister();
4825   if (Reg == -1) {
4826     if (!haveEaten)
4827       return MatchOperand_NoMatch;
4828     Error(Parser.getTok().getLoc(), "register expected");
4829     return MatchOperand_ParseFail;
4830   }
4831 
4832   ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
4833   unsigned ShiftImm = 0;
4834   if (Parser.getTok().is(AsmToken::Comma)) {
4835     Parser.Lex(); // Eat the ','.
4836     if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
4837       return MatchOperand_ParseFail;
4838 
4839     // FIXME: Only approximates end...may include intervening whitespace.
4840     E = Parser.getTok().getLoc();
4841   }
4842 
4843   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
4844                                                   ShiftImm, S, E));
4845 
4846   return MatchOperand_Success;
4847 }
4848 
4849 ARMAsmParser::OperandMatchResultTy
4850 ARMAsmParser::parseAM3Offset(OperandVector &Operands) {
4851   // Check for a post-index addressing register operand. Specifically:
4852   // am3offset := '+' register
4853   //              | '-' register
4854   //              | register
4855   //              | # imm
4856   //              | # + imm
4857   //              | # - imm
4858 
4859   // This method must return MatchOperand_NoMatch without consuming any tokens
4860   // in the case where there is no match, as other alternatives take other
4861   // parse methods.
4862   MCAsmParser &Parser = getParser();
4863   AsmToken Tok = Parser.getTok();
4864   SMLoc S = Tok.getLoc();
4865 
4866   // Do immediates first, as we always parse those if we have a '#'.
4867   if (Parser.getTok().is(AsmToken::Hash) ||
4868       Parser.getTok().is(AsmToken::Dollar)) {
4869     Parser.Lex(); // Eat '#' or '$'.
4870     // Explicitly look for a '-', as we need to encode negative zero
4871     // differently.
4872     bool isNegative = Parser.getTok().is(AsmToken::Minus);
4873     const MCExpr *Offset;
4874     SMLoc E;
4875     if (getParser().parseExpression(Offset, E))
4876       return MatchOperand_ParseFail;
4877     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
4878     if (!CE) {
4879       Error(S, "constant expression expected");
4880       return MatchOperand_ParseFail;
4881     }
4882     // Negative zero is encoded as the flag value INT32_MIN.
4883     int32_t Val = CE->getValue();
4884     if (isNegative && Val == 0)
4885       Val = INT32_MIN;
4886 
4887     Operands.push_back(
4888       ARMOperand::CreateImm(MCConstantExpr::create(Val, getContext()), S, E));
4889 
4890     return MatchOperand_Success;
4891   }
4892 
4893 
4894   bool haveEaten = false;
4895   bool isAdd = true;
4896   if (Tok.is(AsmToken::Plus)) {
4897     Parser.Lex(); // Eat the '+' token.
4898     haveEaten = true;
4899   } else if (Tok.is(AsmToken::Minus)) {
4900     Parser.Lex(); // Eat the '-' token.
4901     isAdd = false;
4902     haveEaten = true;
4903   }
4904 
4905   Tok = Parser.getTok();
4906   int Reg = tryParseRegister();
4907   if (Reg == -1) {
4908     if (!haveEaten)
4909       return MatchOperand_NoMatch;
4910     Error(Tok.getLoc(), "register expected");
4911     return MatchOperand_ParseFail;
4912   }
4913 
4914   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
4915                                                   0, S, Tok.getEndLoc()));
4916 
4917   return MatchOperand_Success;
4918 }
4919 
4920 /// Convert parsed operands to MCInst.  Needed here because this instruction
4921 /// only has two register operands, but multiplication is commutative so
4922 /// assemblers should accept both "mul rD, rN, rD" and "mul rD, rD, rN".
4923 void ARMAsmParser::cvtThumbMultiply(MCInst &Inst,
4924                                     const OperandVector &Operands) {
4925   ((ARMOperand &)*Operands[3]).addRegOperands(Inst, 1);
4926   ((ARMOperand &)*Operands[1]).addCCOutOperands(Inst, 1);
4927   // If we have a three-operand form, make sure to set Rn to be the operand
4928   // that isn't the same as Rd.
4929   unsigned RegOp = 4;
4930   if (Operands.size() == 6 &&
4931       ((ARMOperand &)*Operands[4]).getReg() ==
4932           ((ARMOperand &)*Operands[3]).getReg())
4933     RegOp = 5;
4934   ((ARMOperand &)*Operands[RegOp]).addRegOperands(Inst, 1);
4935   Inst.addOperand(Inst.getOperand(0));
4936   ((ARMOperand &)*Operands[2]).addCondCodeOperands(Inst, 2);
4937 }
4938 
4939 void ARMAsmParser::cvtThumbBranches(MCInst &Inst,
4940                                     const OperandVector &Operands) {
4941   int CondOp = -1, ImmOp = -1;
4942   switch(Inst.getOpcode()) {
4943     case ARM::tB:
4944     case ARM::tBcc:  CondOp = 1; ImmOp = 2; break;
4945 
4946     case ARM::t2B:
4947     case ARM::t2Bcc: CondOp = 1; ImmOp = 3; break;
4948 
4949     default: llvm_unreachable("Unexpected instruction in cvtThumbBranches");
4950   }
4951   // first decide whether or not the branch should be conditional
4952   // by looking at it's location relative to an IT block
4953   if(inITBlock()) {
4954     // inside an IT block we cannot have any conditional branches. any
4955     // such instructions needs to be converted to unconditional form
4956     switch(Inst.getOpcode()) {
4957       case ARM::tBcc: Inst.setOpcode(ARM::tB); break;
4958       case ARM::t2Bcc: Inst.setOpcode(ARM::t2B); break;
4959     }
4960   } else {
4961     // outside IT blocks we can only have unconditional branches with AL
4962     // condition code or conditional branches with non-AL condition code
4963     unsigned Cond = static_cast<ARMOperand &>(*Operands[CondOp]).getCondCode();
4964     switch(Inst.getOpcode()) {
4965       case ARM::tB:
4966       case ARM::tBcc:
4967         Inst.setOpcode(Cond == ARMCC::AL ? ARM::tB : ARM::tBcc);
4968         break;
4969       case ARM::t2B:
4970       case ARM::t2Bcc:
4971         Inst.setOpcode(Cond == ARMCC::AL ? ARM::t2B : ARM::t2Bcc);
4972         break;
4973     }
4974   }
4975 
4976   // now decide on encoding size based on branch target range
4977   switch(Inst.getOpcode()) {
4978     // classify tB as either t2B or t1B based on range of immediate operand
4979     case ARM::tB: {
4980       ARMOperand &op = static_cast<ARMOperand &>(*Operands[ImmOp]);
4981       if (!op.isSignedOffset<11, 1>() && isThumb() && hasV8MBaseline())
4982         Inst.setOpcode(ARM::t2B);
4983       break;
4984     }
4985     // classify tBcc as either t2Bcc or t1Bcc based on range of immediate operand
4986     case ARM::tBcc: {
4987       ARMOperand &op = static_cast<ARMOperand &>(*Operands[ImmOp]);
4988       if (!op.isSignedOffset<8, 1>() && isThumb() && hasV8MBaseline())
4989         Inst.setOpcode(ARM::t2Bcc);
4990       break;
4991     }
4992   }
4993   ((ARMOperand &)*Operands[ImmOp]).addImmOperands(Inst, 1);
4994   ((ARMOperand &)*Operands[CondOp]).addCondCodeOperands(Inst, 2);
4995 }
4996 
4997 /// Parse an ARM memory expression, return false if successful else return true
4998 /// or an error.  The first token must be a '[' when called.
4999 bool ARMAsmParser::parseMemory(OperandVector &Operands) {
5000   MCAsmParser &Parser = getParser();
5001   SMLoc S, E;
5002   assert(Parser.getTok().is(AsmToken::LBrac) &&
5003          "Token is not a Left Bracket");
5004   S = Parser.getTok().getLoc();
5005   Parser.Lex(); // Eat left bracket token.
5006 
5007   const AsmToken &BaseRegTok = Parser.getTok();
5008   int BaseRegNum = tryParseRegister();
5009   if (BaseRegNum == -1)
5010     return Error(BaseRegTok.getLoc(), "register expected");
5011 
5012   // The next token must either be a comma, a colon or a closing bracket.
5013   const AsmToken &Tok = Parser.getTok();
5014   if (!Tok.is(AsmToken::Colon) && !Tok.is(AsmToken::Comma) &&
5015       !Tok.is(AsmToken::RBrac))
5016     return Error(Tok.getLoc(), "malformed memory operand");
5017 
5018   if (Tok.is(AsmToken::RBrac)) {
5019     E = Tok.getEndLoc();
5020     Parser.Lex(); // Eat right bracket token.
5021 
5022     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, 0,
5023                                              ARM_AM::no_shift, 0, 0, false,
5024                                              S, E));
5025 
5026     // If there's a pre-indexing writeback marker, '!', just add it as a token
5027     // operand. It's rather odd, but syntactically valid.
5028     if (Parser.getTok().is(AsmToken::Exclaim)) {
5029       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
5030       Parser.Lex(); // Eat the '!'.
5031     }
5032 
5033     return false;
5034   }
5035 
5036   assert((Tok.is(AsmToken::Colon) || Tok.is(AsmToken::Comma)) &&
5037          "Lost colon or comma in memory operand?!");
5038   if (Tok.is(AsmToken::Comma)) {
5039     Parser.Lex(); // Eat the comma.
5040   }
5041 
5042   // If we have a ':', it's an alignment specifier.
5043   if (Parser.getTok().is(AsmToken::Colon)) {
5044     Parser.Lex(); // Eat the ':'.
5045     E = Parser.getTok().getLoc();
5046     SMLoc AlignmentLoc = Tok.getLoc();
5047 
5048     const MCExpr *Expr;
5049     if (getParser().parseExpression(Expr))
5050      return true;
5051 
5052     // The expression has to be a constant. Memory references with relocations
5053     // don't come through here, as they use the <label> forms of the relevant
5054     // instructions.
5055     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
5056     if (!CE)
5057       return Error (E, "constant expression expected");
5058 
5059     unsigned Align = 0;
5060     switch (CE->getValue()) {
5061     default:
5062       return Error(E,
5063                    "alignment specifier must be 16, 32, 64, 128, or 256 bits");
5064     case 16:  Align = 2; break;
5065     case 32:  Align = 4; break;
5066     case 64:  Align = 8; break;
5067     case 128: Align = 16; break;
5068     case 256: Align = 32; break;
5069     }
5070 
5071     // Now we should have the closing ']'
5072     if (Parser.getTok().isNot(AsmToken::RBrac))
5073       return Error(Parser.getTok().getLoc(), "']' expected");
5074     E = Parser.getTok().getEndLoc();
5075     Parser.Lex(); // Eat right bracket token.
5076 
5077     // Don't worry about range checking the value here. That's handled by
5078     // the is*() predicates.
5079     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, 0,
5080                                              ARM_AM::no_shift, 0, Align,
5081                                              false, S, E, AlignmentLoc));
5082 
5083     // If there's a pre-indexing writeback marker, '!', just add it as a token
5084     // operand.
5085     if (Parser.getTok().is(AsmToken::Exclaim)) {
5086       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
5087       Parser.Lex(); // Eat the '!'.
5088     }
5089 
5090     return false;
5091   }
5092 
5093   // If we have a '#', it's an immediate offset, else assume it's a register
5094   // offset. Be friendly and also accept a plain integer (without a leading
5095   // hash) for gas compatibility.
5096   if (Parser.getTok().is(AsmToken::Hash) ||
5097       Parser.getTok().is(AsmToken::Dollar) ||
5098       Parser.getTok().is(AsmToken::Integer)) {
5099     if (Parser.getTok().isNot(AsmToken::Integer))
5100       Parser.Lex(); // Eat '#' or '$'.
5101     E = Parser.getTok().getLoc();
5102 
5103     bool isNegative = getParser().getTok().is(AsmToken::Minus);
5104     const MCExpr *Offset;
5105     if (getParser().parseExpression(Offset))
5106      return true;
5107 
5108     // The expression has to be a constant. Memory references with relocations
5109     // don't come through here, as they use the <label> forms of the relevant
5110     // instructions.
5111     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
5112     if (!CE)
5113       return Error (E, "constant expression expected");
5114 
5115     // If the constant was #-0, represent it as INT32_MIN.
5116     int32_t Val = CE->getValue();
5117     if (isNegative && Val == 0)
5118       CE = MCConstantExpr::create(INT32_MIN, getContext());
5119 
5120     // Now we should have the closing ']'
5121     if (Parser.getTok().isNot(AsmToken::RBrac))
5122       return Error(Parser.getTok().getLoc(), "']' expected");
5123     E = Parser.getTok().getEndLoc();
5124     Parser.Lex(); // Eat right bracket token.
5125 
5126     // Don't worry about range checking the value here. That's handled by
5127     // the is*() predicates.
5128     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
5129                                              ARM_AM::no_shift, 0, 0,
5130                                              false, S, E));
5131 
5132     // If there's a pre-indexing writeback marker, '!', just add it as a token
5133     // operand.
5134     if (Parser.getTok().is(AsmToken::Exclaim)) {
5135       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
5136       Parser.Lex(); // Eat the '!'.
5137     }
5138 
5139     return false;
5140   }
5141 
5142   // The register offset is optionally preceded by a '+' or '-'
5143   bool isNegative = false;
5144   if (Parser.getTok().is(AsmToken::Minus)) {
5145     isNegative = true;
5146     Parser.Lex(); // Eat the '-'.
5147   } else if (Parser.getTok().is(AsmToken::Plus)) {
5148     // Nothing to do.
5149     Parser.Lex(); // Eat the '+'.
5150   }
5151 
5152   E = Parser.getTok().getLoc();
5153   int OffsetRegNum = tryParseRegister();
5154   if (OffsetRegNum == -1)
5155     return Error(E, "register expected");
5156 
5157   // If there's a shift operator, handle it.
5158   ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
5159   unsigned ShiftImm = 0;
5160   if (Parser.getTok().is(AsmToken::Comma)) {
5161     Parser.Lex(); // Eat the ','.
5162     if (parseMemRegOffsetShift(ShiftType, ShiftImm))
5163       return true;
5164   }
5165 
5166   // Now we should have the closing ']'
5167   if (Parser.getTok().isNot(AsmToken::RBrac))
5168     return Error(Parser.getTok().getLoc(), "']' expected");
5169   E = Parser.getTok().getEndLoc();
5170   Parser.Lex(); // Eat right bracket token.
5171 
5172   Operands.push_back(ARMOperand::CreateMem(BaseRegNum, nullptr, OffsetRegNum,
5173                                            ShiftType, ShiftImm, 0, isNegative,
5174                                            S, E));
5175 
5176   // If there's a pre-indexing writeback marker, '!', just add it as a token
5177   // operand.
5178   if (Parser.getTok().is(AsmToken::Exclaim)) {
5179     Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
5180     Parser.Lex(); // Eat the '!'.
5181   }
5182 
5183   return false;
5184 }
5185 
5186 /// parseMemRegOffsetShift - one of these two:
5187 ///   ( lsl | lsr | asr | ror ) , # shift_amount
5188 ///   rrx
5189 /// return true if it parses a shift otherwise it returns false.
5190 bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
5191                                           unsigned &Amount) {
5192   MCAsmParser &Parser = getParser();
5193   SMLoc Loc = Parser.getTok().getLoc();
5194   const AsmToken &Tok = Parser.getTok();
5195   if (Tok.isNot(AsmToken::Identifier))
5196     return true;
5197   StringRef ShiftName = Tok.getString();
5198   if (ShiftName == "lsl" || ShiftName == "LSL" ||
5199       ShiftName == "asl" || ShiftName == "ASL")
5200     St = ARM_AM::lsl;
5201   else if (ShiftName == "lsr" || ShiftName == "LSR")
5202     St = ARM_AM::lsr;
5203   else if (ShiftName == "asr" || ShiftName == "ASR")
5204     St = ARM_AM::asr;
5205   else if (ShiftName == "ror" || ShiftName == "ROR")
5206     St = ARM_AM::ror;
5207   else if (ShiftName == "rrx" || ShiftName == "RRX")
5208     St = ARM_AM::rrx;
5209   else
5210     return Error(Loc, "illegal shift operator");
5211   Parser.Lex(); // Eat shift type token.
5212 
5213   // rrx stands alone.
5214   Amount = 0;
5215   if (St != ARM_AM::rrx) {
5216     Loc = Parser.getTok().getLoc();
5217     // A '#' and a shift amount.
5218     const AsmToken &HashTok = Parser.getTok();
5219     if (HashTok.isNot(AsmToken::Hash) &&
5220         HashTok.isNot(AsmToken::Dollar))
5221       return Error(HashTok.getLoc(), "'#' expected");
5222     Parser.Lex(); // Eat hash token.
5223 
5224     const MCExpr *Expr;
5225     if (getParser().parseExpression(Expr))
5226       return true;
5227     // Range check the immediate.
5228     // lsl, ror: 0 <= imm <= 31
5229     // lsr, asr: 0 <= imm <= 32
5230     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
5231     if (!CE)
5232       return Error(Loc, "shift amount must be an immediate");
5233     int64_t Imm = CE->getValue();
5234     if (Imm < 0 ||
5235         ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
5236         ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
5237       return Error(Loc, "immediate shift value out of range");
5238     // If <ShiftTy> #0, turn it into a no_shift.
5239     if (Imm == 0)
5240       St = ARM_AM::lsl;
5241     // For consistency, treat lsr #32 and asr #32 as having immediate value 0.
5242     if (Imm == 32)
5243       Imm = 0;
5244     Amount = Imm;
5245   }
5246 
5247   return false;
5248 }
5249 
5250 /// parseFPImm - A floating point immediate expression operand.
5251 ARMAsmParser::OperandMatchResultTy
5252 ARMAsmParser::parseFPImm(OperandVector &Operands) {
5253   MCAsmParser &Parser = getParser();
5254   // Anything that can accept a floating point constant as an operand
5255   // needs to go through here, as the regular parseExpression is
5256   // integer only.
5257   //
5258   // This routine still creates a generic Immediate operand, containing
5259   // a bitcast of the 64-bit floating point value. The various operands
5260   // that accept floats can check whether the value is valid for them
5261   // via the standard is*() predicates.
5262 
5263   SMLoc S = Parser.getTok().getLoc();
5264 
5265   if (Parser.getTok().isNot(AsmToken::Hash) &&
5266       Parser.getTok().isNot(AsmToken::Dollar))
5267     return MatchOperand_NoMatch;
5268 
5269   // Disambiguate the VMOV forms that can accept an FP immediate.
5270   // vmov.f32 <sreg>, #imm
5271   // vmov.f64 <dreg>, #imm
5272   // vmov.f32 <dreg>, #imm  @ vector f32x2
5273   // vmov.f32 <qreg>, #imm  @ vector f32x4
5274   //
5275   // There are also the NEON VMOV instructions which expect an
5276   // integer constant. Make sure we don't try to parse an FPImm
5277   // for these:
5278   // vmov.i{8|16|32|64} <dreg|qreg>, #imm
5279   ARMOperand &TyOp = static_cast<ARMOperand &>(*Operands[2]);
5280   bool isVmovf = TyOp.isToken() &&
5281                  (TyOp.getToken() == ".f32" || TyOp.getToken() == ".f64" ||
5282                   TyOp.getToken() == ".f16");
5283   ARMOperand &Mnemonic = static_cast<ARMOperand &>(*Operands[0]);
5284   bool isFconst = Mnemonic.isToken() && (Mnemonic.getToken() == "fconstd" ||
5285                                          Mnemonic.getToken() == "fconsts");
5286   if (!(isVmovf || isFconst))
5287     return MatchOperand_NoMatch;
5288 
5289   Parser.Lex(); // Eat '#' or '$'.
5290 
5291   // Handle negation, as that still comes through as a separate token.
5292   bool isNegative = false;
5293   if (Parser.getTok().is(AsmToken::Minus)) {
5294     isNegative = true;
5295     Parser.Lex();
5296   }
5297   const AsmToken &Tok = Parser.getTok();
5298   SMLoc Loc = Tok.getLoc();
5299   if (Tok.is(AsmToken::Real) && isVmovf) {
5300     APFloat RealVal(APFloat::IEEEsingle, Tok.getString());
5301     uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
5302     // If we had a '-' in front, toggle the sign bit.
5303     IntVal ^= (uint64_t)isNegative << 31;
5304     Parser.Lex(); // Eat the token.
5305     Operands.push_back(ARMOperand::CreateImm(
5306           MCConstantExpr::create(IntVal, getContext()),
5307           S, Parser.getTok().getLoc()));
5308     return MatchOperand_Success;
5309   }
5310   // Also handle plain integers. Instructions which allow floating point
5311   // immediates also allow a raw encoded 8-bit value.
5312   if (Tok.is(AsmToken::Integer) && isFconst) {
5313     int64_t Val = Tok.getIntVal();
5314     Parser.Lex(); // Eat the token.
5315     if (Val > 255 || Val < 0) {
5316       Error(Loc, "encoded floating point value out of range");
5317       return MatchOperand_ParseFail;
5318     }
5319     float RealVal = ARM_AM::getFPImmFloat(Val);
5320     Val = APFloat(RealVal).bitcastToAPInt().getZExtValue();
5321 
5322     Operands.push_back(ARMOperand::CreateImm(
5323         MCConstantExpr::create(Val, getContext()), S,
5324         Parser.getTok().getLoc()));
5325     return MatchOperand_Success;
5326   }
5327 
5328   Error(Loc, "invalid floating point immediate");
5329   return MatchOperand_ParseFail;
5330 }
5331 
5332 /// Parse a arm instruction operand.  For now this parses the operand regardless
5333 /// of the mnemonic.
5334 bool ARMAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
5335   MCAsmParser &Parser = getParser();
5336   SMLoc S, E;
5337 
5338   // Check if the current operand has a custom associated parser, if so, try to
5339   // custom parse the operand, or fallback to the general approach.
5340   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
5341   if (ResTy == MatchOperand_Success)
5342     return false;
5343   // If there wasn't a custom match, try the generic matcher below. Otherwise,
5344   // there was a match, but an error occurred, in which case, just return that
5345   // the operand parsing failed.
5346   if (ResTy == MatchOperand_ParseFail)
5347     return true;
5348 
5349   switch (getLexer().getKind()) {
5350   default:
5351     Error(Parser.getTok().getLoc(), "unexpected token in operand");
5352     return true;
5353   case AsmToken::Identifier: {
5354     // If we've seen a branch mnemonic, the next operand must be a label.  This
5355     // is true even if the label is a register name.  So "br r1" means branch to
5356     // label "r1".
5357     bool ExpectLabel = Mnemonic == "b" || Mnemonic == "bl";
5358     if (!ExpectLabel) {
5359       if (!tryParseRegisterWithWriteBack(Operands))
5360         return false;
5361       int Res = tryParseShiftRegister(Operands);
5362       if (Res == 0) // success
5363         return false;
5364       else if (Res == -1) // irrecoverable error
5365         return true;
5366       // If this is VMRS, check for the apsr_nzcv operand.
5367       if (Mnemonic == "vmrs" &&
5368           Parser.getTok().getString().equals_lower("apsr_nzcv")) {
5369         S = Parser.getTok().getLoc();
5370         Parser.Lex();
5371         Operands.push_back(ARMOperand::CreateToken("APSR_nzcv", S));
5372         return false;
5373       }
5374     }
5375 
5376     // Fall though for the Identifier case that is not a register or a
5377     // special name.
5378   }
5379   case AsmToken::LParen:  // parenthesized expressions like (_strcmp-4)
5380   case AsmToken::Integer: // things like 1f and 2b as a branch targets
5381   case AsmToken::String:  // quoted label names.
5382   case AsmToken::Dot: {   // . as a branch target
5383     // This was not a register so parse other operands that start with an
5384     // identifier (like labels) as expressions and create them as immediates.
5385     const MCExpr *IdVal;
5386     S = Parser.getTok().getLoc();
5387     if (getParser().parseExpression(IdVal))
5388       return true;
5389     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5390     Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
5391     return false;
5392   }
5393   case AsmToken::LBrac:
5394     return parseMemory(Operands);
5395   case AsmToken::LCurly:
5396     return parseRegisterList(Operands);
5397   case AsmToken::Dollar:
5398   case AsmToken::Hash: {
5399     // #42 -> immediate.
5400     S = Parser.getTok().getLoc();
5401     Parser.Lex();
5402 
5403     if (Parser.getTok().isNot(AsmToken::Colon)) {
5404       bool isNegative = Parser.getTok().is(AsmToken::Minus);
5405       const MCExpr *ImmVal;
5406       if (getParser().parseExpression(ImmVal))
5407         return true;
5408       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
5409       if (CE) {
5410         int32_t Val = CE->getValue();
5411         if (isNegative && Val == 0)
5412           ImmVal = MCConstantExpr::create(INT32_MIN, getContext());
5413       }
5414       E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5415       Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
5416 
5417       // There can be a trailing '!' on operands that we want as a separate
5418       // '!' Token operand. Handle that here. For example, the compatibility
5419       // alias for 'srsdb sp!, #imm' is 'srsdb #imm!'.
5420       if (Parser.getTok().is(AsmToken::Exclaim)) {
5421         Operands.push_back(ARMOperand::CreateToken(Parser.getTok().getString(),
5422                                                    Parser.getTok().getLoc()));
5423         Parser.Lex(); // Eat exclaim token
5424       }
5425       return false;
5426     }
5427     // w/ a ':' after the '#', it's just like a plain ':'.
5428     LLVM_FALLTHROUGH;
5429   }
5430   case AsmToken::Colon: {
5431     S = Parser.getTok().getLoc();
5432     // ":lower16:" and ":upper16:" expression prefixes
5433     // FIXME: Check it's an expression prefix,
5434     // e.g. (FOO - :lower16:BAR) isn't legal.
5435     ARMMCExpr::VariantKind RefKind;
5436     if (parsePrefix(RefKind))
5437       return true;
5438 
5439     const MCExpr *SubExprVal;
5440     if (getParser().parseExpression(SubExprVal))
5441       return true;
5442 
5443     const MCExpr *ExprVal = ARMMCExpr::create(RefKind, SubExprVal,
5444                                               getContext());
5445     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5446     Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
5447     return false;
5448   }
5449   case AsmToken::Equal: {
5450     S = Parser.getTok().getLoc();
5451     if (Mnemonic != "ldr") // only parse for ldr pseudo (e.g. ldr r0, =val)
5452       return Error(S, "unexpected token in operand");
5453     Parser.Lex(); // Eat '='
5454     const MCExpr *SubExprVal;
5455     if (getParser().parseExpression(SubExprVal))
5456       return true;
5457     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
5458     Operands.push_back(ARMOperand::CreateConstantPoolImm(SubExprVal, S, E));
5459     return false;
5460   }
5461   }
5462 }
5463 
5464 // parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
5465 //  :lower16: and :upper16:.
5466 bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
5467   MCAsmParser &Parser = getParser();
5468   RefKind = ARMMCExpr::VK_ARM_None;
5469 
5470   // consume an optional '#' (GNU compatibility)
5471   if (getLexer().is(AsmToken::Hash))
5472     Parser.Lex();
5473 
5474   // :lower16: and :upper16: modifiers
5475   assert(getLexer().is(AsmToken::Colon) && "expected a :");
5476   Parser.Lex(); // Eat ':'
5477 
5478   if (getLexer().isNot(AsmToken::Identifier)) {
5479     Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
5480     return true;
5481   }
5482 
5483   enum {
5484     COFF = (1 << MCObjectFileInfo::IsCOFF),
5485     ELF = (1 << MCObjectFileInfo::IsELF),
5486     MACHO = (1 << MCObjectFileInfo::IsMachO)
5487   };
5488   static const struct PrefixEntry {
5489     const char *Spelling;
5490     ARMMCExpr::VariantKind VariantKind;
5491     uint8_t SupportedFormats;
5492   } PrefixEntries[] = {
5493     { "lower16", ARMMCExpr::VK_ARM_LO16, COFF | ELF | MACHO },
5494     { "upper16", ARMMCExpr::VK_ARM_HI16, COFF | ELF | MACHO },
5495   };
5496 
5497   StringRef IDVal = Parser.getTok().getIdentifier();
5498 
5499   const auto &Prefix =
5500       std::find_if(std::begin(PrefixEntries), std::end(PrefixEntries),
5501                    [&IDVal](const PrefixEntry &PE) {
5502                       return PE.Spelling == IDVal;
5503                    });
5504   if (Prefix == std::end(PrefixEntries)) {
5505     Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
5506     return true;
5507   }
5508 
5509   uint8_t CurrentFormat;
5510   switch (getContext().getObjectFileInfo()->getObjectFileType()) {
5511   case MCObjectFileInfo::IsMachO:
5512     CurrentFormat = MACHO;
5513     break;
5514   case MCObjectFileInfo::IsELF:
5515     CurrentFormat = ELF;
5516     break;
5517   case MCObjectFileInfo::IsCOFF:
5518     CurrentFormat = COFF;
5519     break;
5520   }
5521 
5522   if (~Prefix->SupportedFormats & CurrentFormat) {
5523     Error(Parser.getTok().getLoc(),
5524           "cannot represent relocation in the current file format");
5525     return true;
5526   }
5527 
5528   RefKind = Prefix->VariantKind;
5529   Parser.Lex();
5530 
5531   if (getLexer().isNot(AsmToken::Colon)) {
5532     Error(Parser.getTok().getLoc(), "unexpected token after prefix");
5533     return true;
5534   }
5535   Parser.Lex(); // Eat the last ':'
5536 
5537   return false;
5538 }
5539 
5540 /// \brief Given a mnemonic, split out possible predication code and carry
5541 /// setting letters to form a canonical mnemonic and flags.
5542 //
5543 // FIXME: Would be nice to autogen this.
5544 // FIXME: This is a bit of a maze of special cases.
5545 StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
5546                                       unsigned &PredicationCode,
5547                                       bool &CarrySetting,
5548                                       unsigned &ProcessorIMod,
5549                                       StringRef &ITMask) {
5550   PredicationCode = ARMCC::AL;
5551   CarrySetting = false;
5552   ProcessorIMod = 0;
5553 
5554   // Ignore some mnemonics we know aren't predicated forms.
5555   //
5556   // FIXME: Would be nice to autogen this.
5557   if ((Mnemonic == "movs" && isThumb()) ||
5558       Mnemonic == "teq"   || Mnemonic == "vceq"   || Mnemonic == "svc"   ||
5559       Mnemonic == "mls"   || Mnemonic == "smmls"  || Mnemonic == "vcls"  ||
5560       Mnemonic == "vmls"  || Mnemonic == "vnmls"  || Mnemonic == "vacge" ||
5561       Mnemonic == "vcge"  || Mnemonic == "vclt"   || Mnemonic == "vacgt" ||
5562       Mnemonic == "vaclt" || Mnemonic == "vacle"  || Mnemonic == "hlt" ||
5563       Mnemonic == "vcgt"  || Mnemonic == "vcle"   || Mnemonic == "smlal" ||
5564       Mnemonic == "umaal" || Mnemonic == "umlal"  || Mnemonic == "vabal" ||
5565       Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal" ||
5566       Mnemonic == "fmuls" || Mnemonic == "vmaxnm" || Mnemonic == "vminnm" ||
5567       Mnemonic == "vcvta" || Mnemonic == "vcvtn"  || Mnemonic == "vcvtp" ||
5568       Mnemonic == "vcvtm" || Mnemonic == "vrinta" || Mnemonic == "vrintn" ||
5569       Mnemonic == "vrintp" || Mnemonic == "vrintm" || Mnemonic == "hvc" ||
5570       Mnemonic.startswith("vsel") || Mnemonic == "vins" || Mnemonic == "vmovx" ||
5571       Mnemonic == "bxns"  || Mnemonic == "blxns")
5572     return Mnemonic;
5573 
5574   // First, split out any predication code. Ignore mnemonics we know aren't
5575   // predicated but do have a carry-set and so weren't caught above.
5576   if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
5577       Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
5578       Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
5579       Mnemonic != "sbcs" && Mnemonic != "rscs") {
5580     unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
5581       .Case("eq", ARMCC::EQ)
5582       .Case("ne", ARMCC::NE)
5583       .Case("hs", ARMCC::HS)
5584       .Case("cs", ARMCC::HS)
5585       .Case("lo", ARMCC::LO)
5586       .Case("cc", ARMCC::LO)
5587       .Case("mi", ARMCC::MI)
5588       .Case("pl", ARMCC::PL)
5589       .Case("vs", ARMCC::VS)
5590       .Case("vc", ARMCC::VC)
5591       .Case("hi", ARMCC::HI)
5592       .Case("ls", ARMCC::LS)
5593       .Case("ge", ARMCC::GE)
5594       .Case("lt", ARMCC::LT)
5595       .Case("gt", ARMCC::GT)
5596       .Case("le", ARMCC::LE)
5597       .Case("al", ARMCC::AL)
5598       .Default(~0U);
5599     if (CC != ~0U) {
5600       Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
5601       PredicationCode = CC;
5602     }
5603   }
5604 
5605   // Next, determine if we have a carry setting bit. We explicitly ignore all
5606   // the instructions we know end in 's'.
5607   if (Mnemonic.endswith("s") &&
5608       !(Mnemonic == "cps" || Mnemonic == "mls" ||
5609         Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
5610         Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
5611         Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
5612         Mnemonic == "vrsqrts" || Mnemonic == "srs" || Mnemonic == "flds" ||
5613         Mnemonic == "fmrs" || Mnemonic == "fsqrts" || Mnemonic == "fsubs" ||
5614         Mnemonic == "fsts" || Mnemonic == "fcpys" || Mnemonic == "fdivs" ||
5615         Mnemonic == "fmuls" || Mnemonic == "fcmps" || Mnemonic == "fcmpzs" ||
5616         Mnemonic == "vfms" || Mnemonic == "vfnms" || Mnemonic == "fconsts" ||
5617         Mnemonic == "bxns" || Mnemonic == "blxns" ||
5618         (Mnemonic == "movs" && isThumb()))) {
5619     Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
5620     CarrySetting = true;
5621   }
5622 
5623   // The "cps" instruction can have a interrupt mode operand which is glued into
5624   // the mnemonic. Check if this is the case, split it and parse the imod op
5625   if (Mnemonic.startswith("cps")) {
5626     // Split out any imod code.
5627     unsigned IMod =
5628       StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
5629       .Case("ie", ARM_PROC::IE)
5630       .Case("id", ARM_PROC::ID)
5631       .Default(~0U);
5632     if (IMod != ~0U) {
5633       Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
5634       ProcessorIMod = IMod;
5635     }
5636   }
5637 
5638   // The "it" instruction has the condition mask on the end of the mnemonic.
5639   if (Mnemonic.startswith("it")) {
5640     ITMask = Mnemonic.slice(2, Mnemonic.size());
5641     Mnemonic = Mnemonic.slice(0, 2);
5642   }
5643 
5644   return Mnemonic;
5645 }
5646 
5647 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
5648 /// inclusion of carry set or predication code operands.
5649 //
5650 // FIXME: It would be nice to autogen this.
5651 void ARMAsmParser::getMnemonicAcceptInfo(StringRef Mnemonic, StringRef FullInst,
5652                                          bool &CanAcceptCarrySet,
5653                                          bool &CanAcceptPredicationCode) {
5654   CanAcceptCarrySet =
5655       Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
5656       Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
5657       Mnemonic == "add" || Mnemonic == "adc" || Mnemonic == "mul" ||
5658       Mnemonic == "bic" || Mnemonic == "asr" || Mnemonic == "orr" ||
5659       Mnemonic == "mvn" || Mnemonic == "rsb" || Mnemonic == "rsc" ||
5660       Mnemonic == "orn" || Mnemonic == "sbc" || Mnemonic == "eor" ||
5661       Mnemonic == "neg" || Mnemonic == "vfm" || Mnemonic == "vfnm" ||
5662       (!isThumb() &&
5663        (Mnemonic == "smull" || Mnemonic == "mov" || Mnemonic == "mla" ||
5664         Mnemonic == "smlal" || Mnemonic == "umlal" || Mnemonic == "umull"));
5665 
5666   if (Mnemonic == "bkpt" || Mnemonic == "cbnz" || Mnemonic == "setend" ||
5667       Mnemonic == "cps" || Mnemonic == "it" || Mnemonic == "cbz" ||
5668       Mnemonic == "trap" || Mnemonic == "hlt" || Mnemonic == "udf" ||
5669       Mnemonic.startswith("crc32") || Mnemonic.startswith("cps") ||
5670       Mnemonic.startswith("vsel") || Mnemonic == "vmaxnm" ||
5671       Mnemonic == "vminnm" || Mnemonic == "vcvta" || Mnemonic == "vcvtn" ||
5672       Mnemonic == "vcvtp" || Mnemonic == "vcvtm" || Mnemonic == "vrinta" ||
5673       Mnemonic == "vrintn" || Mnemonic == "vrintp" || Mnemonic == "vrintm" ||
5674       Mnemonic.startswith("aes") || Mnemonic == "hvc" || Mnemonic == "setpan" ||
5675       Mnemonic.startswith("sha1") || Mnemonic.startswith("sha256") ||
5676       (FullInst.startswith("vmull") && FullInst.endswith(".p64")) ||
5677       Mnemonic == "vmovx" || Mnemonic == "vins") {
5678     // These mnemonics are never predicable
5679     CanAcceptPredicationCode = false;
5680   } else if (!isThumb()) {
5681     // Some instructions are only predicable in Thumb mode
5682     CanAcceptPredicationCode =
5683         Mnemonic != "cdp2" && Mnemonic != "clrex" && Mnemonic != "mcr2" &&
5684         Mnemonic != "mcrr2" && Mnemonic != "mrc2" && Mnemonic != "mrrc2" &&
5685         Mnemonic != "dmb" && Mnemonic != "dsb" && Mnemonic != "isb" &&
5686         Mnemonic != "pld" && Mnemonic != "pli" && Mnemonic != "pldw" &&
5687         Mnemonic != "ldc2" && Mnemonic != "ldc2l" && Mnemonic != "stc2" &&
5688         Mnemonic != "stc2l" && !Mnemonic.startswith("rfe") &&
5689         !Mnemonic.startswith("srs");
5690   } else if (isThumbOne()) {
5691     if (hasV6MOps())
5692       CanAcceptPredicationCode = Mnemonic != "movs";
5693     else
5694       CanAcceptPredicationCode = Mnemonic != "nop" && Mnemonic != "movs";
5695   } else
5696     CanAcceptPredicationCode = true;
5697 }
5698 
5699 // \brief Some Thumb instructions have two operand forms that are not
5700 // available as three operand, convert to two operand form if possible.
5701 //
5702 // FIXME: We would really like to be able to tablegen'erate this.
5703 void ARMAsmParser::tryConvertingToTwoOperandForm(StringRef Mnemonic,
5704                                                  bool CarrySetting,
5705                                                  OperandVector &Operands) {
5706   if (Operands.size() != 6)
5707     return;
5708 
5709   const auto &Op3 = static_cast<ARMOperand &>(*Operands[3]);
5710         auto &Op4 = static_cast<ARMOperand &>(*Operands[4]);
5711   if (!Op3.isReg() || !Op4.isReg())
5712     return;
5713 
5714   auto Op3Reg = Op3.getReg();
5715   auto Op4Reg = Op4.getReg();
5716 
5717   // For most Thumb2 cases we just generate the 3 operand form and reduce
5718   // it in processInstruction(), but the 3 operand form of ADD (t2ADDrr)
5719   // won't accept SP or PC so we do the transformation here taking care
5720   // with immediate range in the 'add sp, sp #imm' case.
5721   auto &Op5 = static_cast<ARMOperand &>(*Operands[5]);
5722   if (isThumbTwo()) {
5723     if (Mnemonic != "add")
5724       return;
5725     bool TryTransform = Op3Reg == ARM::PC || Op4Reg == ARM::PC ||
5726                         (Op5.isReg() && Op5.getReg() == ARM::PC);
5727     if (!TryTransform) {
5728       TryTransform = (Op3Reg == ARM::SP || Op4Reg == ARM::SP ||
5729                       (Op5.isReg() && Op5.getReg() == ARM::SP)) &&
5730                      !(Op3Reg == ARM::SP && Op4Reg == ARM::SP &&
5731                        Op5.isImm() && !Op5.isImm0_508s4());
5732     }
5733     if (!TryTransform)
5734       return;
5735   } else if (!isThumbOne())
5736     return;
5737 
5738   if (!(Mnemonic == "add" || Mnemonic == "sub" || Mnemonic == "and" ||
5739         Mnemonic == "eor" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
5740         Mnemonic == "asr" || Mnemonic == "adc" || Mnemonic == "sbc" ||
5741         Mnemonic == "ror" || Mnemonic == "orr" || Mnemonic == "bic"))
5742     return;
5743 
5744   // If first 2 operands of a 3 operand instruction are the same
5745   // then transform to 2 operand version of the same instruction
5746   // e.g. 'adds r0, r0, #1' transforms to 'adds r0, #1'
5747   bool Transform = Op3Reg == Op4Reg;
5748 
5749   // For communtative operations, we might be able to transform if we swap
5750   // Op4 and Op5.  The 'ADD Rdm, SP, Rdm' form is already handled specially
5751   // as tADDrsp.
5752   const ARMOperand *LastOp = &Op5;
5753   bool Swap = false;
5754   if (!Transform && Op5.isReg() && Op3Reg == Op5.getReg() &&
5755       ((Mnemonic == "add" && Op4Reg != ARM::SP) ||
5756        Mnemonic == "and" || Mnemonic == "eor" ||
5757        Mnemonic == "adc" || Mnemonic == "orr")) {
5758     Swap = true;
5759     LastOp = &Op4;
5760     Transform = true;
5761   }
5762 
5763   // If both registers are the same then remove one of them from
5764   // the operand list, with certain exceptions.
5765   if (Transform) {
5766     // Don't transform 'adds Rd, Rd, Rm' or 'sub{s} Rd, Rd, Rm' because the
5767     // 2 operand forms don't exist.
5768     if (((Mnemonic == "add" && CarrySetting) || Mnemonic == "sub") &&
5769         LastOp->isReg())
5770       Transform = false;
5771 
5772     // Don't transform 'add/sub{s} Rd, Rd, #imm' if the immediate fits into
5773     // 3-bits because the ARMARM says not to.
5774     if ((Mnemonic == "add" || Mnemonic == "sub") && LastOp->isImm0_7())
5775       Transform = false;
5776   }
5777 
5778   if (Transform) {
5779     if (Swap)
5780       std::swap(Op4, Op5);
5781     Operands.erase(Operands.begin() + 3);
5782   }
5783 }
5784 
5785 bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
5786                                           OperandVector &Operands) {
5787   // FIXME: This is all horribly hacky. We really need a better way to deal
5788   // with optional operands like this in the matcher table.
5789 
5790   // The 'mov' mnemonic is special. One variant has a cc_out operand, while
5791   // another does not. Specifically, the MOVW instruction does not. So we
5792   // special case it here and remove the defaulted (non-setting) cc_out
5793   // operand if that's the instruction we're trying to match.
5794   //
5795   // We do this as post-processing of the explicit operands rather than just
5796   // conditionally adding the cc_out in the first place because we need
5797   // to check the type of the parsed immediate operand.
5798   if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
5799       !static_cast<ARMOperand &>(*Operands[4]).isModImm() &&
5800       static_cast<ARMOperand &>(*Operands[4]).isImm0_65535Expr() &&
5801       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0)
5802     return true;
5803 
5804   // Register-register 'add' for thumb does not have a cc_out operand
5805   // when there are only two register operands.
5806   if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
5807       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5808       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5809       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0)
5810     return true;
5811   // Register-register 'add' for thumb does not have a cc_out operand
5812   // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
5813   // have to check the immediate range here since Thumb2 has a variant
5814   // that can handle a different range and has a cc_out operand.
5815   if (((isThumb() && Mnemonic == "add") ||
5816        (isThumbTwo() && Mnemonic == "sub")) &&
5817       Operands.size() == 6 && static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5818       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5819       static_cast<ARMOperand &>(*Operands[4]).getReg() == ARM::SP &&
5820       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5821       ((Mnemonic == "add" && static_cast<ARMOperand &>(*Operands[5]).isReg()) ||
5822        static_cast<ARMOperand &>(*Operands[5]).isImm0_1020s4()))
5823     return true;
5824   // For Thumb2, add/sub immediate does not have a cc_out operand for the
5825   // imm0_4095 variant. That's the least-preferred variant when
5826   // selecting via the generic "add" mnemonic, so to know that we
5827   // should remove the cc_out operand, we have to explicitly check that
5828   // it's not one of the other variants. Ugh.
5829   if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
5830       Operands.size() == 6 && static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5831       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5832       static_cast<ARMOperand &>(*Operands[5]).isImm()) {
5833     // Nest conditions rather than one big 'if' statement for readability.
5834     //
5835     // If both registers are low, we're in an IT block, and the immediate is
5836     // in range, we should use encoding T1 instead, which has a cc_out.
5837     if (inITBlock() &&
5838         isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) &&
5839         isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) &&
5840         static_cast<ARMOperand &>(*Operands[5]).isImm0_7())
5841       return false;
5842     // Check against T3. If the second register is the PC, this is an
5843     // alternate form of ADR, which uses encoding T4, so check for that too.
5844     if (static_cast<ARMOperand &>(*Operands[4]).getReg() != ARM::PC &&
5845         static_cast<ARMOperand &>(*Operands[5]).isT2SOImm())
5846       return false;
5847 
5848     // Otherwise, we use encoding T4, which does not have a cc_out
5849     // operand.
5850     return true;
5851   }
5852 
5853   // The thumb2 multiply instruction doesn't have a CCOut register, so
5854   // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
5855   // use the 16-bit encoding or not.
5856   if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
5857       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5858       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5859       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5860       static_cast<ARMOperand &>(*Operands[5]).isReg() &&
5861       // If the registers aren't low regs, the destination reg isn't the
5862       // same as one of the source regs, or the cc_out operand is zero
5863       // outside of an IT block, we have to use the 32-bit encoding, so
5864       // remove the cc_out operand.
5865       (!isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) ||
5866        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) ||
5867        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[5]).getReg()) ||
5868        !inITBlock() || (static_cast<ARMOperand &>(*Operands[3]).getReg() !=
5869                             static_cast<ARMOperand &>(*Operands[5]).getReg() &&
5870                         static_cast<ARMOperand &>(*Operands[3]).getReg() !=
5871                             static_cast<ARMOperand &>(*Operands[4]).getReg())))
5872     return true;
5873 
5874   // Also check the 'mul' syntax variant that doesn't specify an explicit
5875   // destination register.
5876   if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 5 &&
5877       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5878       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5879       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
5880       // If the registers aren't low regs  or the cc_out operand is zero
5881       // outside of an IT block, we have to use the 32-bit encoding, so
5882       // remove the cc_out operand.
5883       (!isARMLowRegister(static_cast<ARMOperand &>(*Operands[3]).getReg()) ||
5884        !isARMLowRegister(static_cast<ARMOperand &>(*Operands[4]).getReg()) ||
5885        !inITBlock()))
5886     return true;
5887 
5888 
5889 
5890   // Register-register 'add/sub' for thumb does not have a cc_out operand
5891   // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
5892   // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
5893   // right, this will result in better diagnostics (which operand is off)
5894   // anyway.
5895   if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
5896       (Operands.size() == 5 || Operands.size() == 6) &&
5897       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
5898       static_cast<ARMOperand &>(*Operands[3]).getReg() == ARM::SP &&
5899       static_cast<ARMOperand &>(*Operands[1]).getReg() == 0 &&
5900       (static_cast<ARMOperand &>(*Operands[4]).isImm() ||
5901        (Operands.size() == 6 &&
5902         static_cast<ARMOperand &>(*Operands[5]).isImm())))
5903     return true;
5904 
5905   return false;
5906 }
5907 
5908 bool ARMAsmParser::shouldOmitPredicateOperand(StringRef Mnemonic,
5909                                               OperandVector &Operands) {
5910   // VRINT{Z, R, X} have a predicate operand in VFP, but not in NEON
5911   unsigned RegIdx = 3;
5912   if ((Mnemonic == "vrintz" || Mnemonic == "vrintx" || Mnemonic == "vrintr") &&
5913       (static_cast<ARMOperand &>(*Operands[2]).getToken() == ".f32" ||
5914        static_cast<ARMOperand &>(*Operands[2]).getToken() == ".f16")) {
5915     if (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
5916         (static_cast<ARMOperand &>(*Operands[3]).getToken() == ".f32" ||
5917          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".f16"))
5918       RegIdx = 4;
5919 
5920     if (static_cast<ARMOperand &>(*Operands[RegIdx]).isReg() &&
5921         (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(
5922              static_cast<ARMOperand &>(*Operands[RegIdx]).getReg()) ||
5923          ARMMCRegisterClasses[ARM::QPRRegClassID].contains(
5924              static_cast<ARMOperand &>(*Operands[RegIdx]).getReg())))
5925       return true;
5926   }
5927   return false;
5928 }
5929 
5930 static bool isDataTypeToken(StringRef Tok) {
5931   return Tok == ".8" || Tok == ".16" || Tok == ".32" || Tok == ".64" ||
5932     Tok == ".i8" || Tok == ".i16" || Tok == ".i32" || Tok == ".i64" ||
5933     Tok == ".u8" || Tok == ".u16" || Tok == ".u32" || Tok == ".u64" ||
5934     Tok == ".s8" || Tok == ".s16" || Tok == ".s32" || Tok == ".s64" ||
5935     Tok == ".p8" || Tok == ".p16" || Tok == ".f32" || Tok == ".f64" ||
5936     Tok == ".f" || Tok == ".d";
5937 }
5938 
5939 // FIXME: This bit should probably be handled via an explicit match class
5940 // in the .td files that matches the suffix instead of having it be
5941 // a literal string token the way it is now.
5942 static bool doesIgnoreDataTypeSuffix(StringRef Mnemonic, StringRef DT) {
5943   return Mnemonic.startswith("vldm") || Mnemonic.startswith("vstm");
5944 }
5945 static void applyMnemonicAliases(StringRef &Mnemonic, uint64_t Features,
5946                                  unsigned VariantID);
5947 
5948 static bool RequiresVFPRegListValidation(StringRef Inst,
5949                                          bool &AcceptSinglePrecisionOnly,
5950                                          bool &AcceptDoublePrecisionOnly) {
5951   if (Inst.size() < 7)
5952     return false;
5953 
5954   if (Inst.startswith("fldm") || Inst.startswith("fstm")) {
5955     StringRef AddressingMode = Inst.substr(4, 2);
5956     if (AddressingMode == "ia" || AddressingMode == "db" ||
5957         AddressingMode == "ea" || AddressingMode == "fd") {
5958       AcceptSinglePrecisionOnly = Inst[6] == 's';
5959       AcceptDoublePrecisionOnly = Inst[6] == 'd' || Inst[6] == 'x';
5960       return true;
5961     }
5962   }
5963 
5964   return false;
5965 }
5966 
5967 /// Parse an arm instruction mnemonic followed by its operands.
5968 bool ARMAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
5969                                     SMLoc NameLoc, OperandVector &Operands) {
5970   MCAsmParser &Parser = getParser();
5971   // FIXME: Can this be done via tablegen in some fashion?
5972   bool RequireVFPRegisterListCheck;
5973   bool AcceptSinglePrecisionOnly;
5974   bool AcceptDoublePrecisionOnly;
5975   RequireVFPRegisterListCheck =
5976     RequiresVFPRegListValidation(Name, AcceptSinglePrecisionOnly,
5977                                  AcceptDoublePrecisionOnly);
5978 
5979   // Apply mnemonic aliases before doing anything else, as the destination
5980   // mnemonic may include suffices and we want to handle them normally.
5981   // The generic tblgen'erated code does this later, at the start of
5982   // MatchInstructionImpl(), but that's too late for aliases that include
5983   // any sort of suffix.
5984   uint64_t AvailableFeatures = getAvailableFeatures();
5985   unsigned AssemblerDialect = getParser().getAssemblerDialect();
5986   applyMnemonicAliases(Name, AvailableFeatures, AssemblerDialect);
5987 
5988   // First check for the ARM-specific .req directive.
5989   if (Parser.getTok().is(AsmToken::Identifier) &&
5990       Parser.getTok().getIdentifier() == ".req") {
5991     parseDirectiveReq(Name, NameLoc);
5992     // We always return 'error' for this, as we're done with this
5993     // statement and don't need to match the 'instruction."
5994     return true;
5995   }
5996 
5997   // Create the leading tokens for the mnemonic, split by '.' characters.
5998   size_t Start = 0, Next = Name.find('.');
5999   StringRef Mnemonic = Name.slice(Start, Next);
6000 
6001   // Split out the predication code and carry setting flag from the mnemonic.
6002   unsigned PredicationCode;
6003   unsigned ProcessorIMod;
6004   bool CarrySetting;
6005   StringRef ITMask;
6006   Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
6007                            ProcessorIMod, ITMask);
6008 
6009   // In Thumb1, only the branch (B) instruction can be predicated.
6010   if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
6011     Parser.eatToEndOfStatement();
6012     return Error(NameLoc, "conditional execution not supported in Thumb1");
6013   }
6014 
6015   Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
6016 
6017   // Handle the IT instruction ITMask. Convert it to a bitmask. This
6018   // is the mask as it will be for the IT encoding if the conditional
6019   // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
6020   // where the conditional bit0 is zero, the instruction post-processing
6021   // will adjust the mask accordingly.
6022   if (Mnemonic == "it") {
6023     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
6024     if (ITMask.size() > 3) {
6025       Parser.eatToEndOfStatement();
6026       return Error(Loc, "too many conditions on IT instruction");
6027     }
6028     unsigned Mask = 8;
6029     for (unsigned i = ITMask.size(); i != 0; --i) {
6030       char pos = ITMask[i - 1];
6031       if (pos != 't' && pos != 'e') {
6032         Parser.eatToEndOfStatement();
6033         return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
6034       }
6035       Mask >>= 1;
6036       if (ITMask[i - 1] == 't')
6037         Mask |= 8;
6038     }
6039     Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
6040   }
6041 
6042   // FIXME: This is all a pretty gross hack. We should automatically handle
6043   // optional operands like this via tblgen.
6044 
6045   // Next, add the CCOut and ConditionCode operands, if needed.
6046   //
6047   // For mnemonics which can ever incorporate a carry setting bit or predication
6048   // code, our matching model involves us always generating CCOut and
6049   // ConditionCode operands to match the mnemonic "as written" and then we let
6050   // the matcher deal with finding the right instruction or generating an
6051   // appropriate error.
6052   bool CanAcceptCarrySet, CanAcceptPredicationCode;
6053   getMnemonicAcceptInfo(Mnemonic, Name, CanAcceptCarrySet, CanAcceptPredicationCode);
6054 
6055   // If we had a carry-set on an instruction that can't do that, issue an
6056   // error.
6057   if (!CanAcceptCarrySet && CarrySetting) {
6058     Parser.eatToEndOfStatement();
6059     return Error(NameLoc, "instruction '" + Mnemonic +
6060                  "' can not set flags, but 's' suffix specified");
6061   }
6062   // If we had a predication code on an instruction that can't do that, issue an
6063   // error.
6064   if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
6065     Parser.eatToEndOfStatement();
6066     return Error(NameLoc, "instruction '" + Mnemonic +
6067                  "' is not predicable, but condition code specified");
6068   }
6069 
6070   // Add the carry setting operand, if necessary.
6071   if (CanAcceptCarrySet) {
6072     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
6073     Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
6074                                                Loc));
6075   }
6076 
6077   // Add the predication code operand, if necessary.
6078   if (CanAcceptPredicationCode) {
6079     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
6080                                       CarrySetting);
6081     Operands.push_back(ARMOperand::CreateCondCode(
6082                          ARMCC::CondCodes(PredicationCode), Loc));
6083   }
6084 
6085   // Add the processor imod operand, if necessary.
6086   if (ProcessorIMod) {
6087     Operands.push_back(ARMOperand::CreateImm(
6088           MCConstantExpr::create(ProcessorIMod, getContext()),
6089                                  NameLoc, NameLoc));
6090   } else if (Mnemonic == "cps" && isMClass()) {
6091     return Error(NameLoc, "instruction 'cps' requires effect for M-class");
6092   }
6093 
6094   // Add the remaining tokens in the mnemonic.
6095   while (Next != StringRef::npos) {
6096     Start = Next;
6097     Next = Name.find('.', Start + 1);
6098     StringRef ExtraToken = Name.slice(Start, Next);
6099 
6100     // Some NEON instructions have an optional datatype suffix that is
6101     // completely ignored. Check for that.
6102     if (isDataTypeToken(ExtraToken) &&
6103         doesIgnoreDataTypeSuffix(Mnemonic, ExtraToken))
6104       continue;
6105 
6106     // For for ARM mode generate an error if the .n qualifier is used.
6107     if (ExtraToken == ".n" && !isThumb()) {
6108       SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
6109       Parser.eatToEndOfStatement();
6110       return Error(Loc, "instruction with .n (narrow) qualifier not allowed in "
6111                    "arm mode");
6112     }
6113 
6114     // The .n qualifier is always discarded as that is what the tables
6115     // and matcher expect.  In ARM mode the .w qualifier has no effect,
6116     // so discard it to avoid errors that can be caused by the matcher.
6117     if (ExtraToken != ".n" && (isThumb() || ExtraToken != ".w")) {
6118       SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
6119       Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
6120     }
6121   }
6122 
6123   // Read the remaining operands.
6124   if (getLexer().isNot(AsmToken::EndOfStatement)) {
6125     // Read the first operand.
6126     if (parseOperand(Operands, Mnemonic)) {
6127       Parser.eatToEndOfStatement();
6128       return true;
6129     }
6130 
6131     while (getLexer().is(AsmToken::Comma)) {
6132       Parser.Lex();  // Eat the comma.
6133 
6134       // Parse and remember the operand.
6135       if (parseOperand(Operands, Mnemonic)) {
6136         Parser.eatToEndOfStatement();
6137         return true;
6138       }
6139     }
6140   }
6141 
6142   if (getLexer().isNot(AsmToken::EndOfStatement)) {
6143     SMLoc Loc = getLexer().getLoc();
6144     Parser.eatToEndOfStatement();
6145     return Error(Loc, "unexpected token in argument list");
6146   }
6147 
6148   Parser.Lex(); // Consume the EndOfStatement
6149 
6150   if (RequireVFPRegisterListCheck) {
6151     ARMOperand &Op = static_cast<ARMOperand &>(*Operands.back());
6152     if (AcceptSinglePrecisionOnly && !Op.isSPRRegList())
6153       return Error(Op.getStartLoc(),
6154                    "VFP/Neon single precision register expected");
6155     if (AcceptDoublePrecisionOnly && !Op.isDPRRegList())
6156       return Error(Op.getStartLoc(),
6157                    "VFP/Neon double precision register expected");
6158   }
6159 
6160   tryConvertingToTwoOperandForm(Mnemonic, CarrySetting, Operands);
6161 
6162   // Some instructions, mostly Thumb, have forms for the same mnemonic that
6163   // do and don't have a cc_out optional-def operand. With some spot-checks
6164   // of the operand list, we can figure out which variant we're trying to
6165   // parse and adjust accordingly before actually matching. We shouldn't ever
6166   // try to remove a cc_out operand that was explicitly set on the
6167   // mnemonic, of course (CarrySetting == true). Reason number #317 the
6168   // table driven matcher doesn't fit well with the ARM instruction set.
6169   if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands))
6170     Operands.erase(Operands.begin() + 1);
6171 
6172   // Some instructions have the same mnemonic, but don't always
6173   // have a predicate. Distinguish them here and delete the
6174   // predicate if needed.
6175   if (shouldOmitPredicateOperand(Mnemonic, Operands))
6176     Operands.erase(Operands.begin() + 1);
6177 
6178   // ARM mode 'blx' need special handling, as the register operand version
6179   // is predicable, but the label operand version is not. So, we can't rely
6180   // on the Mnemonic based checking to correctly figure out when to put
6181   // a k_CondCode operand in the list. If we're trying to match the label
6182   // version, remove the k_CondCode operand here.
6183   if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
6184       static_cast<ARMOperand &>(*Operands[2]).isImm())
6185     Operands.erase(Operands.begin() + 1);
6186 
6187   // Adjust operands of ldrexd/strexd to MCK_GPRPair.
6188   // ldrexd/strexd require even/odd GPR pair. To enforce this constraint,
6189   // a single GPRPair reg operand is used in the .td file to replace the two
6190   // GPRs. However, when parsing from asm, the two GRPs cannot be automatically
6191   // expressed as a GPRPair, so we have to manually merge them.
6192   // FIXME: We would really like to be able to tablegen'erate this.
6193   if (!isThumb() && Operands.size() > 4 &&
6194       (Mnemonic == "ldrexd" || Mnemonic == "strexd" || Mnemonic == "ldaexd" ||
6195        Mnemonic == "stlexd")) {
6196     bool isLoad = (Mnemonic == "ldrexd" || Mnemonic == "ldaexd");
6197     unsigned Idx = isLoad ? 2 : 3;
6198     ARMOperand &Op1 = static_cast<ARMOperand &>(*Operands[Idx]);
6199     ARMOperand &Op2 = static_cast<ARMOperand &>(*Operands[Idx + 1]);
6200 
6201     const MCRegisterClass& MRC = MRI->getRegClass(ARM::GPRRegClassID);
6202     // Adjust only if Op1 and Op2 are GPRs.
6203     if (Op1.isReg() && Op2.isReg() && MRC.contains(Op1.getReg()) &&
6204         MRC.contains(Op2.getReg())) {
6205       unsigned Reg1 = Op1.getReg();
6206       unsigned Reg2 = Op2.getReg();
6207       unsigned Rt = MRI->getEncodingValue(Reg1);
6208       unsigned Rt2 = MRI->getEncodingValue(Reg2);
6209 
6210       // Rt2 must be Rt + 1 and Rt must be even.
6211       if (Rt + 1 != Rt2 || (Rt & 1)) {
6212         Error(Op2.getStartLoc(), isLoad
6213                                      ? "destination operands must be sequential"
6214                                      : "source operands must be sequential");
6215         return true;
6216       }
6217       unsigned NewReg = MRI->getMatchingSuperReg(Reg1, ARM::gsub_0,
6218           &(MRI->getRegClass(ARM::GPRPairRegClassID)));
6219       Operands[Idx] =
6220           ARMOperand::CreateReg(NewReg, Op1.getStartLoc(), Op2.getEndLoc());
6221       Operands.erase(Operands.begin() + Idx + 1);
6222     }
6223   }
6224 
6225   // GNU Assembler extension (compatibility)
6226   if ((Mnemonic == "ldrd" || Mnemonic == "strd")) {
6227     ARMOperand &Op2 = static_cast<ARMOperand &>(*Operands[2]);
6228     ARMOperand &Op3 = static_cast<ARMOperand &>(*Operands[3]);
6229     if (Op3.isMem()) {
6230       assert(Op2.isReg() && "expected register argument");
6231 
6232       unsigned SuperReg = MRI->getMatchingSuperReg(
6233           Op2.getReg(), ARM::gsub_0, &MRI->getRegClass(ARM::GPRPairRegClassID));
6234 
6235       assert(SuperReg && "expected register pair");
6236 
6237       unsigned PairedReg = MRI->getSubReg(SuperReg, ARM::gsub_1);
6238 
6239       Operands.insert(
6240           Operands.begin() + 3,
6241           ARMOperand::CreateReg(PairedReg, Op2.getStartLoc(), Op2.getEndLoc()));
6242     }
6243   }
6244 
6245   // FIXME: As said above, this is all a pretty gross hack.  This instruction
6246   // does not fit with other "subs" and tblgen.
6247   // Adjust operands of B9.3.19 SUBS PC, LR, #imm (Thumb2) system instruction
6248   // so the Mnemonic is the original name "subs" and delete the predicate
6249   // operand so it will match the table entry.
6250   if (isThumbTwo() && Mnemonic == "sub" && Operands.size() == 6 &&
6251       static_cast<ARMOperand &>(*Operands[3]).isReg() &&
6252       static_cast<ARMOperand &>(*Operands[3]).getReg() == ARM::PC &&
6253       static_cast<ARMOperand &>(*Operands[4]).isReg() &&
6254       static_cast<ARMOperand &>(*Operands[4]).getReg() == ARM::LR &&
6255       static_cast<ARMOperand &>(*Operands[5]).isImm()) {
6256     Operands.front() = ARMOperand::CreateToken(Name, NameLoc);
6257     Operands.erase(Operands.begin() + 1);
6258   }
6259   return false;
6260 }
6261 
6262 // Validate context-sensitive operand constraints.
6263 
6264 // return 'true' if register list contains non-low GPR registers,
6265 // 'false' otherwise. If Reg is in the register list or is HiReg, set
6266 // 'containsReg' to true.
6267 static bool checkLowRegisterList(const MCInst &Inst, unsigned OpNo,
6268                                  unsigned Reg, unsigned HiReg,
6269                                  bool &containsReg) {
6270   containsReg = false;
6271   for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
6272     unsigned OpReg = Inst.getOperand(i).getReg();
6273     if (OpReg == Reg)
6274       containsReg = true;
6275     // Anything other than a low register isn't legal here.
6276     if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
6277       return true;
6278   }
6279   return false;
6280 }
6281 
6282 // Check if the specified regisgter is in the register list of the inst,
6283 // starting at the indicated operand number.
6284 static bool listContainsReg(const MCInst &Inst, unsigned OpNo, unsigned Reg) {
6285   for (unsigned i = OpNo, e = Inst.getNumOperands(); i < e; ++i) {
6286     unsigned OpReg = Inst.getOperand(i).getReg();
6287     if (OpReg == Reg)
6288       return true;
6289   }
6290   return false;
6291 }
6292 
6293 // Return true if instruction has the interesting property of being
6294 // allowed in IT blocks, but not being predicable.
6295 static bool instIsBreakpoint(const MCInst &Inst) {
6296     return Inst.getOpcode() == ARM::tBKPT ||
6297            Inst.getOpcode() == ARM::BKPT ||
6298            Inst.getOpcode() == ARM::tHLT ||
6299            Inst.getOpcode() == ARM::HLT;
6300 
6301 }
6302 
6303 bool ARMAsmParser::validatetLDMRegList(const MCInst &Inst,
6304                                        const OperandVector &Operands,
6305                                        unsigned ListNo, bool IsARPop) {
6306   const ARMOperand &Op = static_cast<const ARMOperand &>(*Operands[ListNo]);
6307   bool HasWritebackToken = Op.isToken() && Op.getToken() == "!";
6308 
6309   bool ListContainsSP = listContainsReg(Inst, ListNo, ARM::SP);
6310   bool ListContainsLR = listContainsReg(Inst, ListNo, ARM::LR);
6311   bool ListContainsPC = listContainsReg(Inst, ListNo, ARM::PC);
6312 
6313   if (!IsARPop && ListContainsSP)
6314     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6315                  "SP may not be in the register list");
6316   else if (ListContainsPC && ListContainsLR)
6317     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6318                  "PC and LR may not be in the register list simultaneously");
6319   else if (inITBlock() && !lastInITBlock() && ListContainsPC)
6320     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6321                  "instruction must be outside of IT block or the last "
6322                  "instruction in an IT block");
6323   return false;
6324 }
6325 
6326 bool ARMAsmParser::validatetSTMRegList(const MCInst &Inst,
6327                                        const OperandVector &Operands,
6328                                        unsigned ListNo) {
6329   const ARMOperand &Op = static_cast<const ARMOperand &>(*Operands[ListNo]);
6330   bool HasWritebackToken = Op.isToken() && Op.getToken() == "!";
6331 
6332   bool ListContainsSP = listContainsReg(Inst, ListNo, ARM::SP);
6333   bool ListContainsPC = listContainsReg(Inst, ListNo, ARM::PC);
6334 
6335   if (ListContainsSP && ListContainsPC)
6336     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6337                  "SP and PC may not be in the register list");
6338   else if (ListContainsSP)
6339     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6340                  "SP may not be in the register list");
6341   else if (ListContainsPC)
6342     return Error(Operands[ListNo + HasWritebackToken]->getStartLoc(),
6343                  "PC may not be in the register list");
6344   return false;
6345 }
6346 
6347 // FIXME: We would really like to be able to tablegen'erate this.
6348 bool ARMAsmParser::validateInstruction(MCInst &Inst,
6349                                        const OperandVector &Operands) {
6350   const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
6351   SMLoc Loc = Operands[0]->getStartLoc();
6352 
6353   // Check the IT block state first.
6354   // NOTE: BKPT and HLT instructions have the interesting property of being
6355   // allowed in IT blocks, but not being predicable. They just always execute.
6356   if (inITBlock() && !instIsBreakpoint(Inst)) {
6357     // The instruction must be predicable.
6358     if (!MCID.isPredicable())
6359       return Error(Loc, "instructions in IT block must be predicable");
6360     unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
6361     if (Cond != currentITCond()) {
6362       // Find the condition code Operand to get its SMLoc information.
6363       SMLoc CondLoc;
6364       for (unsigned I = 1; I < Operands.size(); ++I)
6365         if (static_cast<ARMOperand &>(*Operands[I]).isCondCode())
6366           CondLoc = Operands[I]->getStartLoc();
6367       return Error(CondLoc, "incorrect condition in IT block; got '" +
6368                    StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
6369                    "', but expected '" +
6370                    ARMCondCodeToString(ARMCC::CondCodes(currentITCond())) + "'");
6371     }
6372   // Check for non-'al' condition codes outside of the IT block.
6373   } else if (isThumbTwo() && MCID.isPredicable() &&
6374              Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
6375              ARMCC::AL && Inst.getOpcode() != ARM::tBcc &&
6376              Inst.getOpcode() != ARM::t2Bcc) {
6377     return Error(Loc, "predicated instructions must be in IT block");
6378   } else if (!isThumb() && !useImplicitITARM() && MCID.isPredicable() &&
6379              Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
6380                  ARMCC::AL) {
6381     return Warning(Loc, "predicated instructions should be in IT block");
6382   }
6383 
6384   const unsigned Opcode = Inst.getOpcode();
6385   switch (Opcode) {
6386   case ARM::LDRD:
6387   case ARM::LDRD_PRE:
6388   case ARM::LDRD_POST: {
6389     const unsigned RtReg = Inst.getOperand(0).getReg();
6390 
6391     // Rt can't be R14.
6392     if (RtReg == ARM::LR)
6393       return Error(Operands[3]->getStartLoc(),
6394                    "Rt can't be R14");
6395 
6396     const unsigned Rt = MRI->getEncodingValue(RtReg);
6397     // Rt must be even-numbered.
6398     if ((Rt & 1) == 1)
6399       return Error(Operands[3]->getStartLoc(),
6400                    "Rt must be even-numbered");
6401 
6402     // Rt2 must be Rt + 1.
6403     const unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6404     if (Rt2 != Rt + 1)
6405       return Error(Operands[3]->getStartLoc(),
6406                    "destination operands must be sequential");
6407 
6408     if (Opcode == ARM::LDRD_PRE || Opcode == ARM::LDRD_POST) {
6409       const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(3).getReg());
6410       // For addressing modes with writeback, the base register needs to be
6411       // different from the destination registers.
6412       if (Rn == Rt || Rn == Rt2)
6413         return Error(Operands[3]->getStartLoc(),
6414                      "base register needs to be different from destination "
6415                      "registers");
6416     }
6417 
6418     return false;
6419   }
6420   case ARM::t2LDRDi8:
6421   case ARM::t2LDRD_PRE:
6422   case ARM::t2LDRD_POST: {
6423     // Rt2 must be different from Rt.
6424     unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
6425     unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6426     if (Rt2 == Rt)
6427       return Error(Operands[3]->getStartLoc(),
6428                    "destination operands can't be identical");
6429     return false;
6430   }
6431   case ARM::t2BXJ: {
6432     const unsigned RmReg = Inst.getOperand(0).getReg();
6433     // Rm = SP is no longer unpredictable in v8-A
6434     if (RmReg == ARM::SP && !hasV8Ops())
6435       return Error(Operands[2]->getStartLoc(),
6436                    "r13 (SP) is an unpredictable operand to BXJ");
6437     return false;
6438   }
6439   case ARM::STRD: {
6440     // Rt2 must be Rt + 1.
6441     unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
6442     unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6443     if (Rt2 != Rt + 1)
6444       return Error(Operands[3]->getStartLoc(),
6445                    "source operands must be sequential");
6446     return false;
6447   }
6448   case ARM::STRD_PRE:
6449   case ARM::STRD_POST: {
6450     // Rt2 must be Rt + 1.
6451     unsigned Rt = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6452     unsigned Rt2 = MRI->getEncodingValue(Inst.getOperand(2).getReg());
6453     if (Rt2 != Rt + 1)
6454       return Error(Operands[3]->getStartLoc(),
6455                    "source operands must be sequential");
6456     return false;
6457   }
6458   case ARM::STR_PRE_IMM:
6459   case ARM::STR_PRE_REG:
6460   case ARM::STR_POST_IMM:
6461   case ARM::STR_POST_REG:
6462   case ARM::STRH_PRE:
6463   case ARM::STRH_POST:
6464   case ARM::STRB_PRE_IMM:
6465   case ARM::STRB_PRE_REG:
6466   case ARM::STRB_POST_IMM:
6467   case ARM::STRB_POST_REG: {
6468     // Rt must be different from Rn.
6469     const unsigned Rt = MRI->getEncodingValue(Inst.getOperand(1).getReg());
6470     const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(2).getReg());
6471 
6472     if (Rt == Rn)
6473       return Error(Operands[3]->getStartLoc(),
6474                    "source register and base register can't be identical");
6475     return false;
6476   }
6477   case ARM::LDR_PRE_IMM:
6478   case ARM::LDR_PRE_REG:
6479   case ARM::LDR_POST_IMM:
6480   case ARM::LDR_POST_REG:
6481   case ARM::LDRH_PRE:
6482   case ARM::LDRH_POST:
6483   case ARM::LDRSH_PRE:
6484   case ARM::LDRSH_POST:
6485   case ARM::LDRB_PRE_IMM:
6486   case ARM::LDRB_PRE_REG:
6487   case ARM::LDRB_POST_IMM:
6488   case ARM::LDRB_POST_REG:
6489   case ARM::LDRSB_PRE:
6490   case ARM::LDRSB_POST: {
6491     // Rt must be different from Rn.
6492     const unsigned Rt = MRI->getEncodingValue(Inst.getOperand(0).getReg());
6493     const unsigned Rn = MRI->getEncodingValue(Inst.getOperand(2).getReg());
6494 
6495     if (Rt == Rn)
6496       return Error(Operands[3]->getStartLoc(),
6497                    "destination register and base register can't be identical");
6498     return false;
6499   }
6500   case ARM::SBFX:
6501   case ARM::UBFX: {
6502     // Width must be in range [1, 32-lsb].
6503     unsigned LSB = Inst.getOperand(2).getImm();
6504     unsigned Widthm1 = Inst.getOperand(3).getImm();
6505     if (Widthm1 >= 32 - LSB)
6506       return Error(Operands[5]->getStartLoc(),
6507                    "bitfield width must be in range [1,32-lsb]");
6508     return false;
6509   }
6510   // Notionally handles ARM::tLDMIA_UPD too.
6511   case ARM::tLDMIA: {
6512     // If we're parsing Thumb2, the .w variant is available and handles
6513     // most cases that are normally illegal for a Thumb1 LDM instruction.
6514     // We'll make the transformation in processInstruction() if necessary.
6515     //
6516     // Thumb LDM instructions are writeback iff the base register is not
6517     // in the register list.
6518     unsigned Rn = Inst.getOperand(0).getReg();
6519     bool HasWritebackToken =
6520         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
6521          static_cast<ARMOperand &>(*Operands[3]).getToken() == "!");
6522     bool ListContainsBase;
6523     if (checkLowRegisterList(Inst, 3, Rn, 0, ListContainsBase) && !isThumbTwo())
6524       return Error(Operands[3 + HasWritebackToken]->getStartLoc(),
6525                    "registers must be in range r0-r7");
6526     // If we should have writeback, then there should be a '!' token.
6527     if (!ListContainsBase && !HasWritebackToken && !isThumbTwo())
6528       return Error(Operands[2]->getStartLoc(),
6529                    "writeback operator '!' expected");
6530     // If we should not have writeback, there must not be a '!'. This is
6531     // true even for the 32-bit wide encodings.
6532     if (ListContainsBase && HasWritebackToken)
6533       return Error(Operands[3]->getStartLoc(),
6534                    "writeback operator '!' not allowed when base register "
6535                    "in register list");
6536 
6537     if (validatetLDMRegList(Inst, Operands, 3))
6538       return true;
6539     break;
6540   }
6541   case ARM::LDMIA_UPD:
6542   case ARM::LDMDB_UPD:
6543   case ARM::LDMIB_UPD:
6544   case ARM::LDMDA_UPD:
6545     // ARM variants loading and updating the same register are only officially
6546     // UNPREDICTABLE on v7 upwards. Goodness knows what they did before.
6547     if (!hasV7Ops())
6548       break;
6549     if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
6550       return Error(Operands.back()->getStartLoc(),
6551                    "writeback register not allowed in register list");
6552     break;
6553   case ARM::t2LDMIA:
6554   case ARM::t2LDMDB:
6555     if (validatetLDMRegList(Inst, Operands, 3))
6556       return true;
6557     break;
6558   case ARM::t2STMIA:
6559   case ARM::t2STMDB:
6560     if (validatetSTMRegList(Inst, Operands, 3))
6561       return true;
6562     break;
6563   case ARM::t2LDMIA_UPD:
6564   case ARM::t2LDMDB_UPD:
6565   case ARM::t2STMIA_UPD:
6566   case ARM::t2STMDB_UPD: {
6567     if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
6568       return Error(Operands.back()->getStartLoc(),
6569                    "writeback register not allowed in register list");
6570 
6571     if (Opcode == ARM::t2LDMIA_UPD || Opcode == ARM::t2LDMDB_UPD) {
6572       if (validatetLDMRegList(Inst, Operands, 3))
6573         return true;
6574     } else {
6575       if (validatetSTMRegList(Inst, Operands, 3))
6576         return true;
6577     }
6578     break;
6579   }
6580   case ARM::sysLDMIA_UPD:
6581   case ARM::sysLDMDA_UPD:
6582   case ARM::sysLDMDB_UPD:
6583   case ARM::sysLDMIB_UPD:
6584     if (!listContainsReg(Inst, 3, ARM::PC))
6585       return Error(Operands[4]->getStartLoc(),
6586                    "writeback register only allowed on system LDM "
6587                    "if PC in register-list");
6588     break;
6589   case ARM::sysSTMIA_UPD:
6590   case ARM::sysSTMDA_UPD:
6591   case ARM::sysSTMDB_UPD:
6592   case ARM::sysSTMIB_UPD:
6593     return Error(Operands[2]->getStartLoc(),
6594                  "system STM cannot have writeback register");
6595   case ARM::tMUL: {
6596     // The second source operand must be the same register as the destination
6597     // operand.
6598     //
6599     // In this case, we must directly check the parsed operands because the
6600     // cvtThumbMultiply() function is written in such a way that it guarantees
6601     // this first statement is always true for the new Inst.  Essentially, the
6602     // destination is unconditionally copied into the second source operand
6603     // without checking to see if it matches what we actually parsed.
6604     if (Operands.size() == 6 && (((ARMOperand &)*Operands[3]).getReg() !=
6605                                  ((ARMOperand &)*Operands[5]).getReg()) &&
6606         (((ARMOperand &)*Operands[3]).getReg() !=
6607          ((ARMOperand &)*Operands[4]).getReg())) {
6608       return Error(Operands[3]->getStartLoc(),
6609                    "destination register must match source register");
6610     }
6611     break;
6612   }
6613   // Like for ldm/stm, push and pop have hi-reg handling version in Thumb2,
6614   // so only issue a diagnostic for thumb1. The instructions will be
6615   // switched to the t2 encodings in processInstruction() if necessary.
6616   case ARM::tPOP: {
6617     bool ListContainsBase;
6618     if (checkLowRegisterList(Inst, 2, 0, ARM::PC, ListContainsBase) &&
6619         !isThumbTwo())
6620       return Error(Operands[2]->getStartLoc(),
6621                    "registers must be in range r0-r7 or pc");
6622     if (validatetLDMRegList(Inst, Operands, 2, !isMClass()))
6623       return true;
6624     break;
6625   }
6626   case ARM::tPUSH: {
6627     bool ListContainsBase;
6628     if (checkLowRegisterList(Inst, 2, 0, ARM::LR, ListContainsBase) &&
6629         !isThumbTwo())
6630       return Error(Operands[2]->getStartLoc(),
6631                    "registers must be in range r0-r7 or lr");
6632     if (validatetSTMRegList(Inst, Operands, 2))
6633       return true;
6634     break;
6635   }
6636   case ARM::tSTMIA_UPD: {
6637     bool ListContainsBase, InvalidLowList;
6638     InvalidLowList = checkLowRegisterList(Inst, 4, Inst.getOperand(0).getReg(),
6639                                           0, ListContainsBase);
6640     if (InvalidLowList && !isThumbTwo())
6641       return Error(Operands[4]->getStartLoc(),
6642                    "registers must be in range r0-r7");
6643 
6644     // This would be converted to a 32-bit stm, but that's not valid if the
6645     // writeback register is in the list.
6646     if (InvalidLowList && ListContainsBase)
6647       return Error(Operands[4]->getStartLoc(),
6648                    "writeback operator '!' not allowed when base register "
6649                    "in register list");
6650 
6651     if (validatetSTMRegList(Inst, Operands, 4))
6652       return true;
6653     break;
6654   }
6655   case ARM::tADDrSP: {
6656     // If the non-SP source operand and the destination operand are not the
6657     // same, we need thumb2 (for the wide encoding), or we have an error.
6658     if (!isThumbTwo() &&
6659         Inst.getOperand(0).getReg() != Inst.getOperand(2).getReg()) {
6660       return Error(Operands[4]->getStartLoc(),
6661                    "source register must be the same as destination");
6662     }
6663     break;
6664   }
6665   // Final range checking for Thumb unconditional branch instructions.
6666   case ARM::tB:
6667     if (!(static_cast<ARMOperand &>(*Operands[2])).isSignedOffset<11, 1>())
6668       return Error(Operands[2]->getStartLoc(), "branch target out of range");
6669     break;
6670   case ARM::t2B: {
6671     int op = (Operands[2]->isImm()) ? 2 : 3;
6672     if (!static_cast<ARMOperand &>(*Operands[op]).isSignedOffset<24, 1>())
6673       return Error(Operands[op]->getStartLoc(), "branch target out of range");
6674     break;
6675   }
6676   // Final range checking for Thumb conditional branch instructions.
6677   case ARM::tBcc:
6678     if (!static_cast<ARMOperand &>(*Operands[2]).isSignedOffset<8, 1>())
6679       return Error(Operands[2]->getStartLoc(), "branch target out of range");
6680     break;
6681   case ARM::t2Bcc: {
6682     int Op = (Operands[2]->isImm()) ? 2 : 3;
6683     if (!static_cast<ARMOperand &>(*Operands[Op]).isSignedOffset<20, 1>())
6684       return Error(Operands[Op]->getStartLoc(), "branch target out of range");
6685     break;
6686   }
6687   case ARM::tCBZ:
6688   case ARM::tCBNZ: {
6689     if (!static_cast<ARMOperand &>(*Operands[2]).isUnsignedOffset<6, 1>())
6690       return Error(Operands[2]->getStartLoc(), "branch target out of range");
6691     break;
6692   }
6693   case ARM::MOVi16:
6694   case ARM::t2MOVi16:
6695   case ARM::t2MOVTi16:
6696     {
6697     // We want to avoid misleadingly allowing something like "mov r0, <symbol>"
6698     // especially when we turn it into a movw and the expression <symbol> does
6699     // not have a :lower16: or :upper16 as part of the expression.  We don't
6700     // want the behavior of silently truncating, which can be unexpected and
6701     // lead to bugs that are difficult to find since this is an easy mistake
6702     // to make.
6703     int i = (Operands[3]->isImm()) ? 3 : 4;
6704     ARMOperand &Op = static_cast<ARMOperand &>(*Operands[i]);
6705     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm());
6706     if (CE) break;
6707     const MCExpr *E = dyn_cast<MCExpr>(Op.getImm());
6708     if (!E) break;
6709     const ARMMCExpr *ARM16Expr = dyn_cast<ARMMCExpr>(E);
6710     if (!ARM16Expr || (ARM16Expr->getKind() != ARMMCExpr::VK_ARM_HI16 &&
6711                        ARM16Expr->getKind() != ARMMCExpr::VK_ARM_LO16))
6712       return Error(
6713           Op.getStartLoc(),
6714           "immediate expression for mov requires :lower16: or :upper16");
6715     break;
6716   }
6717   case ARM::HINT:
6718   case ARM::t2HINT: {
6719     if (hasRAS()) {
6720       // ESB is not predicable (pred must be AL)
6721       unsigned Imm8 = Inst.getOperand(0).getImm();
6722       unsigned Pred = Inst.getOperand(1).getImm();
6723       if (Imm8 == 0x10 && Pred != ARMCC::AL)
6724         return Error(Operands[1]->getStartLoc(), "instruction 'esb' is not "
6725                                                  "predicable, but condition "
6726                                                  "code specified");
6727     }
6728     // Without the RAS extension, this behaves as any other unallocated hint.
6729     break;
6730   }
6731   }
6732 
6733   return false;
6734 }
6735 
6736 static unsigned getRealVSTOpcode(unsigned Opc, unsigned &Spacing) {
6737   switch(Opc) {
6738   default: llvm_unreachable("unexpected opcode!");
6739   // VST1LN
6740   case ARM::VST1LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST1LNd8_UPD;
6741   case ARM::VST1LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST1LNd16_UPD;
6742   case ARM::VST1LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST1LNd32_UPD;
6743   case ARM::VST1LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST1LNd8_UPD;
6744   case ARM::VST1LNdWB_register_Asm_16: Spacing = 1; return ARM::VST1LNd16_UPD;
6745   case ARM::VST1LNdWB_register_Asm_32: Spacing = 1; return ARM::VST1LNd32_UPD;
6746   case ARM::VST1LNdAsm_8:  Spacing = 1; return ARM::VST1LNd8;
6747   case ARM::VST1LNdAsm_16: Spacing = 1; return ARM::VST1LNd16;
6748   case ARM::VST1LNdAsm_32: Spacing = 1; return ARM::VST1LNd32;
6749 
6750   // VST2LN
6751   case ARM::VST2LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST2LNd8_UPD;
6752   case ARM::VST2LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST2LNd16_UPD;
6753   case ARM::VST2LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST2LNd32_UPD;
6754   case ARM::VST2LNqWB_fixed_Asm_16: Spacing = 2; return ARM::VST2LNq16_UPD;
6755   case ARM::VST2LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST2LNq32_UPD;
6756 
6757   case ARM::VST2LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST2LNd8_UPD;
6758   case ARM::VST2LNdWB_register_Asm_16: Spacing = 1; return ARM::VST2LNd16_UPD;
6759   case ARM::VST2LNdWB_register_Asm_32: Spacing = 1; return ARM::VST2LNd32_UPD;
6760   case ARM::VST2LNqWB_register_Asm_16: Spacing = 2; return ARM::VST2LNq16_UPD;
6761   case ARM::VST2LNqWB_register_Asm_32: Spacing = 2; return ARM::VST2LNq32_UPD;
6762 
6763   case ARM::VST2LNdAsm_8:  Spacing = 1; return ARM::VST2LNd8;
6764   case ARM::VST2LNdAsm_16: Spacing = 1; return ARM::VST2LNd16;
6765   case ARM::VST2LNdAsm_32: Spacing = 1; return ARM::VST2LNd32;
6766   case ARM::VST2LNqAsm_16: Spacing = 2; return ARM::VST2LNq16;
6767   case ARM::VST2LNqAsm_32: Spacing = 2; return ARM::VST2LNq32;
6768 
6769   // VST3LN
6770   case ARM::VST3LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST3LNd8_UPD;
6771   case ARM::VST3LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST3LNd16_UPD;
6772   case ARM::VST3LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST3LNd32_UPD;
6773   case ARM::VST3LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VST3LNq16_UPD;
6774   case ARM::VST3LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST3LNq32_UPD;
6775   case ARM::VST3LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST3LNd8_UPD;
6776   case ARM::VST3LNdWB_register_Asm_16: Spacing = 1; return ARM::VST3LNd16_UPD;
6777   case ARM::VST3LNdWB_register_Asm_32: Spacing = 1; return ARM::VST3LNd32_UPD;
6778   case ARM::VST3LNqWB_register_Asm_16: Spacing = 2; return ARM::VST3LNq16_UPD;
6779   case ARM::VST3LNqWB_register_Asm_32: Spacing = 2; return ARM::VST3LNq32_UPD;
6780   case ARM::VST3LNdAsm_8:  Spacing = 1; return ARM::VST3LNd8;
6781   case ARM::VST3LNdAsm_16: Spacing = 1; return ARM::VST3LNd16;
6782   case ARM::VST3LNdAsm_32: Spacing = 1; return ARM::VST3LNd32;
6783   case ARM::VST3LNqAsm_16: Spacing = 2; return ARM::VST3LNq16;
6784   case ARM::VST3LNqAsm_32: Spacing = 2; return ARM::VST3LNq32;
6785 
6786   // VST3
6787   case ARM::VST3dWB_fixed_Asm_8:  Spacing = 1; return ARM::VST3d8_UPD;
6788   case ARM::VST3dWB_fixed_Asm_16: Spacing = 1; return ARM::VST3d16_UPD;
6789   case ARM::VST3dWB_fixed_Asm_32: Spacing = 1; return ARM::VST3d32_UPD;
6790   case ARM::VST3qWB_fixed_Asm_8:  Spacing = 2; return ARM::VST3q8_UPD;
6791   case ARM::VST3qWB_fixed_Asm_16: Spacing = 2; return ARM::VST3q16_UPD;
6792   case ARM::VST3qWB_fixed_Asm_32: Spacing = 2; return ARM::VST3q32_UPD;
6793   case ARM::VST3dWB_register_Asm_8:  Spacing = 1; return ARM::VST3d8_UPD;
6794   case ARM::VST3dWB_register_Asm_16: Spacing = 1; return ARM::VST3d16_UPD;
6795   case ARM::VST3dWB_register_Asm_32: Spacing = 1; return ARM::VST3d32_UPD;
6796   case ARM::VST3qWB_register_Asm_8:  Spacing = 2; return ARM::VST3q8_UPD;
6797   case ARM::VST3qWB_register_Asm_16: Spacing = 2; return ARM::VST3q16_UPD;
6798   case ARM::VST3qWB_register_Asm_32: Spacing = 2; return ARM::VST3q32_UPD;
6799   case ARM::VST3dAsm_8:  Spacing = 1; return ARM::VST3d8;
6800   case ARM::VST3dAsm_16: Spacing = 1; return ARM::VST3d16;
6801   case ARM::VST3dAsm_32: Spacing = 1; return ARM::VST3d32;
6802   case ARM::VST3qAsm_8:  Spacing = 2; return ARM::VST3q8;
6803   case ARM::VST3qAsm_16: Spacing = 2; return ARM::VST3q16;
6804   case ARM::VST3qAsm_32: Spacing = 2; return ARM::VST3q32;
6805 
6806   // VST4LN
6807   case ARM::VST4LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST4LNd8_UPD;
6808   case ARM::VST4LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST4LNd16_UPD;
6809   case ARM::VST4LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST4LNd32_UPD;
6810   case ARM::VST4LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VST4LNq16_UPD;
6811   case ARM::VST4LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST4LNq32_UPD;
6812   case ARM::VST4LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST4LNd8_UPD;
6813   case ARM::VST4LNdWB_register_Asm_16: Spacing = 1; return ARM::VST4LNd16_UPD;
6814   case ARM::VST4LNdWB_register_Asm_32: Spacing = 1; return ARM::VST4LNd32_UPD;
6815   case ARM::VST4LNqWB_register_Asm_16: Spacing = 2; return ARM::VST4LNq16_UPD;
6816   case ARM::VST4LNqWB_register_Asm_32: Spacing = 2; return ARM::VST4LNq32_UPD;
6817   case ARM::VST4LNdAsm_8:  Spacing = 1; return ARM::VST4LNd8;
6818   case ARM::VST4LNdAsm_16: Spacing = 1; return ARM::VST4LNd16;
6819   case ARM::VST4LNdAsm_32: Spacing = 1; return ARM::VST4LNd32;
6820   case ARM::VST4LNqAsm_16: Spacing = 2; return ARM::VST4LNq16;
6821   case ARM::VST4LNqAsm_32: Spacing = 2; return ARM::VST4LNq32;
6822 
6823   // VST4
6824   case ARM::VST4dWB_fixed_Asm_8:  Spacing = 1; return ARM::VST4d8_UPD;
6825   case ARM::VST4dWB_fixed_Asm_16: Spacing = 1; return ARM::VST4d16_UPD;
6826   case ARM::VST4dWB_fixed_Asm_32: Spacing = 1; return ARM::VST4d32_UPD;
6827   case ARM::VST4qWB_fixed_Asm_8:  Spacing = 2; return ARM::VST4q8_UPD;
6828   case ARM::VST4qWB_fixed_Asm_16: Spacing = 2; return ARM::VST4q16_UPD;
6829   case ARM::VST4qWB_fixed_Asm_32: Spacing = 2; return ARM::VST4q32_UPD;
6830   case ARM::VST4dWB_register_Asm_8:  Spacing = 1; return ARM::VST4d8_UPD;
6831   case ARM::VST4dWB_register_Asm_16: Spacing = 1; return ARM::VST4d16_UPD;
6832   case ARM::VST4dWB_register_Asm_32: Spacing = 1; return ARM::VST4d32_UPD;
6833   case ARM::VST4qWB_register_Asm_8:  Spacing = 2; return ARM::VST4q8_UPD;
6834   case ARM::VST4qWB_register_Asm_16: Spacing = 2; return ARM::VST4q16_UPD;
6835   case ARM::VST4qWB_register_Asm_32: Spacing = 2; return ARM::VST4q32_UPD;
6836   case ARM::VST4dAsm_8:  Spacing = 1; return ARM::VST4d8;
6837   case ARM::VST4dAsm_16: Spacing = 1; return ARM::VST4d16;
6838   case ARM::VST4dAsm_32: Spacing = 1; return ARM::VST4d32;
6839   case ARM::VST4qAsm_8:  Spacing = 2; return ARM::VST4q8;
6840   case ARM::VST4qAsm_16: Spacing = 2; return ARM::VST4q16;
6841   case ARM::VST4qAsm_32: Spacing = 2; return ARM::VST4q32;
6842   }
6843 }
6844 
6845 static unsigned getRealVLDOpcode(unsigned Opc, unsigned &Spacing) {
6846   switch(Opc) {
6847   default: llvm_unreachable("unexpected opcode!");
6848   // VLD1LN
6849   case ARM::VLD1LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD1LNd8_UPD;
6850   case ARM::VLD1LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD1LNd16_UPD;
6851   case ARM::VLD1LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD1LNd32_UPD;
6852   case ARM::VLD1LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD1LNd8_UPD;
6853   case ARM::VLD1LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD1LNd16_UPD;
6854   case ARM::VLD1LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD1LNd32_UPD;
6855   case ARM::VLD1LNdAsm_8:  Spacing = 1; return ARM::VLD1LNd8;
6856   case ARM::VLD1LNdAsm_16: Spacing = 1; return ARM::VLD1LNd16;
6857   case ARM::VLD1LNdAsm_32: Spacing = 1; return ARM::VLD1LNd32;
6858 
6859   // VLD2LN
6860   case ARM::VLD2LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD2LNd8_UPD;
6861   case ARM::VLD2LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD2LNd16_UPD;
6862   case ARM::VLD2LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD2LNd32_UPD;
6863   case ARM::VLD2LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD2LNq16_UPD;
6864   case ARM::VLD2LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD2LNq32_UPD;
6865   case ARM::VLD2LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD2LNd8_UPD;
6866   case ARM::VLD2LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD2LNd16_UPD;
6867   case ARM::VLD2LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD2LNd32_UPD;
6868   case ARM::VLD2LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD2LNq16_UPD;
6869   case ARM::VLD2LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD2LNq32_UPD;
6870   case ARM::VLD2LNdAsm_8:  Spacing = 1; return ARM::VLD2LNd8;
6871   case ARM::VLD2LNdAsm_16: Spacing = 1; return ARM::VLD2LNd16;
6872   case ARM::VLD2LNdAsm_32: Spacing = 1; return ARM::VLD2LNd32;
6873   case ARM::VLD2LNqAsm_16: Spacing = 2; return ARM::VLD2LNq16;
6874   case ARM::VLD2LNqAsm_32: Spacing = 2; return ARM::VLD2LNq32;
6875 
6876   // VLD3DUP
6877   case ARM::VLD3DUPdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3DUPd8_UPD;
6878   case ARM::VLD3DUPdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3DUPd16_UPD;
6879   case ARM::VLD3DUPdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3DUPd32_UPD;
6880   case ARM::VLD3DUPqWB_fixed_Asm_8: Spacing = 1; return ARM::VLD3DUPq8_UPD;
6881   case ARM::VLD3DUPqWB_fixed_Asm_16: Spacing = 2; return ARM::VLD3DUPq16_UPD;
6882   case ARM::VLD3DUPqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3DUPq32_UPD;
6883   case ARM::VLD3DUPdWB_register_Asm_8:  Spacing = 1; return ARM::VLD3DUPd8_UPD;
6884   case ARM::VLD3DUPdWB_register_Asm_16: Spacing = 1; return ARM::VLD3DUPd16_UPD;
6885   case ARM::VLD3DUPdWB_register_Asm_32: Spacing = 1; return ARM::VLD3DUPd32_UPD;
6886   case ARM::VLD3DUPqWB_register_Asm_8: Spacing = 2; return ARM::VLD3DUPq8_UPD;
6887   case ARM::VLD3DUPqWB_register_Asm_16: Spacing = 2; return ARM::VLD3DUPq16_UPD;
6888   case ARM::VLD3DUPqWB_register_Asm_32: Spacing = 2; return ARM::VLD3DUPq32_UPD;
6889   case ARM::VLD3DUPdAsm_8:  Spacing = 1; return ARM::VLD3DUPd8;
6890   case ARM::VLD3DUPdAsm_16: Spacing = 1; return ARM::VLD3DUPd16;
6891   case ARM::VLD3DUPdAsm_32: Spacing = 1; return ARM::VLD3DUPd32;
6892   case ARM::VLD3DUPqAsm_8: Spacing = 2; return ARM::VLD3DUPq8;
6893   case ARM::VLD3DUPqAsm_16: Spacing = 2; return ARM::VLD3DUPq16;
6894   case ARM::VLD3DUPqAsm_32: Spacing = 2; return ARM::VLD3DUPq32;
6895 
6896   // VLD3LN
6897   case ARM::VLD3LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3LNd8_UPD;
6898   case ARM::VLD3LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3LNd16_UPD;
6899   case ARM::VLD3LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3LNd32_UPD;
6900   case ARM::VLD3LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3LNq16_UPD;
6901   case ARM::VLD3LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3LNq32_UPD;
6902   case ARM::VLD3LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD3LNd8_UPD;
6903   case ARM::VLD3LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD3LNd16_UPD;
6904   case ARM::VLD3LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD3LNd32_UPD;
6905   case ARM::VLD3LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD3LNq16_UPD;
6906   case ARM::VLD3LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD3LNq32_UPD;
6907   case ARM::VLD3LNdAsm_8:  Spacing = 1; return ARM::VLD3LNd8;
6908   case ARM::VLD3LNdAsm_16: Spacing = 1; return ARM::VLD3LNd16;
6909   case ARM::VLD3LNdAsm_32: Spacing = 1; return ARM::VLD3LNd32;
6910   case ARM::VLD3LNqAsm_16: Spacing = 2; return ARM::VLD3LNq16;
6911   case ARM::VLD3LNqAsm_32: Spacing = 2; return ARM::VLD3LNq32;
6912 
6913   // VLD3
6914   case ARM::VLD3dWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3d8_UPD;
6915   case ARM::VLD3dWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3d16_UPD;
6916   case ARM::VLD3dWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3d32_UPD;
6917   case ARM::VLD3qWB_fixed_Asm_8:  Spacing = 2; return ARM::VLD3q8_UPD;
6918   case ARM::VLD3qWB_fixed_Asm_16: Spacing = 2; return ARM::VLD3q16_UPD;
6919   case ARM::VLD3qWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3q32_UPD;
6920   case ARM::VLD3dWB_register_Asm_8:  Spacing = 1; return ARM::VLD3d8_UPD;
6921   case ARM::VLD3dWB_register_Asm_16: Spacing = 1; return ARM::VLD3d16_UPD;
6922   case ARM::VLD3dWB_register_Asm_32: Spacing = 1; return ARM::VLD3d32_UPD;
6923   case ARM::VLD3qWB_register_Asm_8:  Spacing = 2; return ARM::VLD3q8_UPD;
6924   case ARM::VLD3qWB_register_Asm_16: Spacing = 2; return ARM::VLD3q16_UPD;
6925   case ARM::VLD3qWB_register_Asm_32: Spacing = 2; return ARM::VLD3q32_UPD;
6926   case ARM::VLD3dAsm_8:  Spacing = 1; return ARM::VLD3d8;
6927   case ARM::VLD3dAsm_16: Spacing = 1; return ARM::VLD3d16;
6928   case ARM::VLD3dAsm_32: Spacing = 1; return ARM::VLD3d32;
6929   case ARM::VLD3qAsm_8:  Spacing = 2; return ARM::VLD3q8;
6930   case ARM::VLD3qAsm_16: Spacing = 2; return ARM::VLD3q16;
6931   case ARM::VLD3qAsm_32: Spacing = 2; return ARM::VLD3q32;
6932 
6933   // VLD4LN
6934   case ARM::VLD4LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4LNd8_UPD;
6935   case ARM::VLD4LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4LNd16_UPD;
6936   case ARM::VLD4LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4LNd32_UPD;
6937   case ARM::VLD4LNqWB_fixed_Asm_16: Spacing = 2; return ARM::VLD4LNq16_UPD;
6938   case ARM::VLD4LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4LNq32_UPD;
6939   case ARM::VLD4LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD4LNd8_UPD;
6940   case ARM::VLD4LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD4LNd16_UPD;
6941   case ARM::VLD4LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD4LNd32_UPD;
6942   case ARM::VLD4LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD4LNq16_UPD;
6943   case ARM::VLD4LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD4LNq32_UPD;
6944   case ARM::VLD4LNdAsm_8:  Spacing = 1; return ARM::VLD4LNd8;
6945   case ARM::VLD4LNdAsm_16: Spacing = 1; return ARM::VLD4LNd16;
6946   case ARM::VLD4LNdAsm_32: Spacing = 1; return ARM::VLD4LNd32;
6947   case ARM::VLD4LNqAsm_16: Spacing = 2; return ARM::VLD4LNq16;
6948   case ARM::VLD4LNqAsm_32: Spacing = 2; return ARM::VLD4LNq32;
6949 
6950   // VLD4DUP
6951   case ARM::VLD4DUPdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4DUPd8_UPD;
6952   case ARM::VLD4DUPdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4DUPd16_UPD;
6953   case ARM::VLD4DUPdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4DUPd32_UPD;
6954   case ARM::VLD4DUPqWB_fixed_Asm_8: Spacing = 1; return ARM::VLD4DUPq8_UPD;
6955   case ARM::VLD4DUPqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4DUPq16_UPD;
6956   case ARM::VLD4DUPqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4DUPq32_UPD;
6957   case ARM::VLD4DUPdWB_register_Asm_8:  Spacing = 1; return ARM::VLD4DUPd8_UPD;
6958   case ARM::VLD4DUPdWB_register_Asm_16: Spacing = 1; return ARM::VLD4DUPd16_UPD;
6959   case ARM::VLD4DUPdWB_register_Asm_32: Spacing = 1; return ARM::VLD4DUPd32_UPD;
6960   case ARM::VLD4DUPqWB_register_Asm_8: Spacing = 2; return ARM::VLD4DUPq8_UPD;
6961   case ARM::VLD4DUPqWB_register_Asm_16: Spacing = 2; return ARM::VLD4DUPq16_UPD;
6962   case ARM::VLD4DUPqWB_register_Asm_32: Spacing = 2; return ARM::VLD4DUPq32_UPD;
6963   case ARM::VLD4DUPdAsm_8:  Spacing = 1; return ARM::VLD4DUPd8;
6964   case ARM::VLD4DUPdAsm_16: Spacing = 1; return ARM::VLD4DUPd16;
6965   case ARM::VLD4DUPdAsm_32: Spacing = 1; return ARM::VLD4DUPd32;
6966   case ARM::VLD4DUPqAsm_8: Spacing = 2; return ARM::VLD4DUPq8;
6967   case ARM::VLD4DUPqAsm_16: Spacing = 2; return ARM::VLD4DUPq16;
6968   case ARM::VLD4DUPqAsm_32: Spacing = 2; return ARM::VLD4DUPq32;
6969 
6970   // VLD4
6971   case ARM::VLD4dWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4d8_UPD;
6972   case ARM::VLD4dWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4d16_UPD;
6973   case ARM::VLD4dWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4d32_UPD;
6974   case ARM::VLD4qWB_fixed_Asm_8:  Spacing = 2; return ARM::VLD4q8_UPD;
6975   case ARM::VLD4qWB_fixed_Asm_16: Spacing = 2; return ARM::VLD4q16_UPD;
6976   case ARM::VLD4qWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4q32_UPD;
6977   case ARM::VLD4dWB_register_Asm_8:  Spacing = 1; return ARM::VLD4d8_UPD;
6978   case ARM::VLD4dWB_register_Asm_16: Spacing = 1; return ARM::VLD4d16_UPD;
6979   case ARM::VLD4dWB_register_Asm_32: Spacing = 1; return ARM::VLD4d32_UPD;
6980   case ARM::VLD4qWB_register_Asm_8:  Spacing = 2; return ARM::VLD4q8_UPD;
6981   case ARM::VLD4qWB_register_Asm_16: Spacing = 2; return ARM::VLD4q16_UPD;
6982   case ARM::VLD4qWB_register_Asm_32: Spacing = 2; return ARM::VLD4q32_UPD;
6983   case ARM::VLD4dAsm_8:  Spacing = 1; return ARM::VLD4d8;
6984   case ARM::VLD4dAsm_16: Spacing = 1; return ARM::VLD4d16;
6985   case ARM::VLD4dAsm_32: Spacing = 1; return ARM::VLD4d32;
6986   case ARM::VLD4qAsm_8:  Spacing = 2; return ARM::VLD4q8;
6987   case ARM::VLD4qAsm_16: Spacing = 2; return ARM::VLD4q16;
6988   case ARM::VLD4qAsm_32: Spacing = 2; return ARM::VLD4q32;
6989   }
6990 }
6991 
6992 bool ARMAsmParser::processInstruction(MCInst &Inst,
6993                                       const OperandVector &Operands,
6994                                       MCStreamer &Out) {
6995   switch (Inst.getOpcode()) {
6996   // Alias for alternate form of 'ldr{,b}t Rt, [Rn], #imm' instruction.
6997   case ARM::LDRT_POST:
6998   case ARM::LDRBT_POST: {
6999     const unsigned Opcode =
7000       (Inst.getOpcode() == ARM::LDRT_POST) ? ARM::LDRT_POST_IMM
7001                                            : ARM::LDRBT_POST_IMM;
7002     MCInst TmpInst;
7003     TmpInst.setOpcode(Opcode);
7004     TmpInst.addOperand(Inst.getOperand(0));
7005     TmpInst.addOperand(Inst.getOperand(1));
7006     TmpInst.addOperand(Inst.getOperand(1));
7007     TmpInst.addOperand(MCOperand::createReg(0));
7008     TmpInst.addOperand(MCOperand::createImm(0));
7009     TmpInst.addOperand(Inst.getOperand(2));
7010     TmpInst.addOperand(Inst.getOperand(3));
7011     Inst = TmpInst;
7012     return true;
7013   }
7014   // Alias for alternate form of 'str{,b}t Rt, [Rn], #imm' instruction.
7015   case ARM::STRT_POST:
7016   case ARM::STRBT_POST: {
7017     const unsigned Opcode =
7018       (Inst.getOpcode() == ARM::STRT_POST) ? ARM::STRT_POST_IMM
7019                                            : ARM::STRBT_POST_IMM;
7020     MCInst TmpInst;
7021     TmpInst.setOpcode(Opcode);
7022     TmpInst.addOperand(Inst.getOperand(1));
7023     TmpInst.addOperand(Inst.getOperand(0));
7024     TmpInst.addOperand(Inst.getOperand(1));
7025     TmpInst.addOperand(MCOperand::createReg(0));
7026     TmpInst.addOperand(MCOperand::createImm(0));
7027     TmpInst.addOperand(Inst.getOperand(2));
7028     TmpInst.addOperand(Inst.getOperand(3));
7029     Inst = TmpInst;
7030     return true;
7031   }
7032   // Alias for alternate form of 'ADR Rd, #imm' instruction.
7033   case ARM::ADDri: {
7034     if (Inst.getOperand(1).getReg() != ARM::PC ||
7035         Inst.getOperand(5).getReg() != 0 ||
7036         !(Inst.getOperand(2).isExpr() || Inst.getOperand(2).isImm()))
7037       return false;
7038     MCInst TmpInst;
7039     TmpInst.setOpcode(ARM::ADR);
7040     TmpInst.addOperand(Inst.getOperand(0));
7041     if (Inst.getOperand(2).isImm()) {
7042       // Immediate (mod_imm) will be in its encoded form, we must unencode it
7043       // before passing it to the ADR instruction.
7044       unsigned Enc = Inst.getOperand(2).getImm();
7045       TmpInst.addOperand(MCOperand::createImm(
7046         ARM_AM::rotr32(Enc & 0xFF, (Enc & 0xF00) >> 7)));
7047     } else {
7048       // Turn PC-relative expression into absolute expression.
7049       // Reading PC provides the start of the current instruction + 8 and
7050       // the transform to adr is biased by that.
7051       MCSymbol *Dot = getContext().createTempSymbol();
7052       Out.EmitLabel(Dot);
7053       const MCExpr *OpExpr = Inst.getOperand(2).getExpr();
7054       const MCExpr *InstPC = MCSymbolRefExpr::create(Dot,
7055                                                      MCSymbolRefExpr::VK_None,
7056                                                      getContext());
7057       const MCExpr *Const8 = MCConstantExpr::create(8, getContext());
7058       const MCExpr *ReadPC = MCBinaryExpr::createAdd(InstPC, Const8,
7059                                                      getContext());
7060       const MCExpr *FixupAddr = MCBinaryExpr::createAdd(ReadPC, OpExpr,
7061                                                         getContext());
7062       TmpInst.addOperand(MCOperand::createExpr(FixupAddr));
7063     }
7064     TmpInst.addOperand(Inst.getOperand(3));
7065     TmpInst.addOperand(Inst.getOperand(4));
7066     Inst = TmpInst;
7067     return true;
7068   }
7069   // Aliases for alternate PC+imm syntax of LDR instructions.
7070   case ARM::t2LDRpcrel:
7071     // Select the narrow version if the immediate will fit.
7072     if (Inst.getOperand(1).getImm() > 0 &&
7073         Inst.getOperand(1).getImm() <= 0xff &&
7074         !(static_cast<ARMOperand &>(*Operands[2]).isToken() &&
7075           static_cast<ARMOperand &>(*Operands[2]).getToken() == ".w"))
7076       Inst.setOpcode(ARM::tLDRpci);
7077     else
7078       Inst.setOpcode(ARM::t2LDRpci);
7079     return true;
7080   case ARM::t2LDRBpcrel:
7081     Inst.setOpcode(ARM::t2LDRBpci);
7082     return true;
7083   case ARM::t2LDRHpcrel:
7084     Inst.setOpcode(ARM::t2LDRHpci);
7085     return true;
7086   case ARM::t2LDRSBpcrel:
7087     Inst.setOpcode(ARM::t2LDRSBpci);
7088     return true;
7089   case ARM::t2LDRSHpcrel:
7090     Inst.setOpcode(ARM::t2LDRSHpci);
7091     return true;
7092   case ARM::LDRConstPool:
7093   case ARM::tLDRConstPool:
7094   case ARM::t2LDRConstPool: {
7095     // Pseudo instruction ldr rt, =immediate is converted to a
7096     // MOV rt, immediate if immediate is known and representable
7097     // otherwise we create a constant pool entry that we load from.
7098     MCInst TmpInst;
7099     if (Inst.getOpcode() == ARM::LDRConstPool)
7100       TmpInst.setOpcode(ARM::LDRi12);
7101     else if (Inst.getOpcode() == ARM::tLDRConstPool)
7102       TmpInst.setOpcode(ARM::tLDRpci);
7103     else if (Inst.getOpcode() == ARM::t2LDRConstPool)
7104       TmpInst.setOpcode(ARM::t2LDRpci);
7105     const ARMOperand &PoolOperand =
7106       static_cast<ARMOperand &>(*Operands[3]);
7107     const MCExpr *SubExprVal = PoolOperand.getConstantPoolImm();
7108     // If SubExprVal is a constant we may be able to use a MOV
7109     if (isa<MCConstantExpr>(SubExprVal) &&
7110         Inst.getOperand(0).getReg() != ARM::PC &&
7111         Inst.getOperand(0).getReg() != ARM::SP) {
7112       int64_t Value =
7113         (int64_t) (cast<MCConstantExpr>(SubExprVal))->getValue();
7114       bool UseMov  = true;
7115       bool MovHasS = true;
7116       if (Inst.getOpcode() == ARM::LDRConstPool) {
7117         // ARM Constant
7118         if (ARM_AM::getSOImmVal(Value) != -1) {
7119           Value = ARM_AM::getSOImmVal(Value);
7120           TmpInst.setOpcode(ARM::MOVi);
7121         }
7122         else if (ARM_AM::getSOImmVal(~Value) != -1) {
7123           Value = ARM_AM::getSOImmVal(~Value);
7124           TmpInst.setOpcode(ARM::MVNi);
7125         }
7126         else if (hasV6T2Ops() &&
7127                  Value >=0 && Value < 65536) {
7128           TmpInst.setOpcode(ARM::MOVi16);
7129           MovHasS = false;
7130         }
7131         else
7132           UseMov = false;
7133       }
7134       else {
7135         // Thumb/Thumb2 Constant
7136         if (hasThumb2() &&
7137             ARM_AM::getT2SOImmVal(Value) != -1)
7138           TmpInst.setOpcode(ARM::t2MOVi);
7139         else if (hasThumb2() &&
7140                  ARM_AM::getT2SOImmVal(~Value) != -1) {
7141           TmpInst.setOpcode(ARM::t2MVNi);
7142           Value = ~Value;
7143         }
7144         else if (hasV8MBaseline() &&
7145                  Value >=0 && Value < 65536) {
7146           TmpInst.setOpcode(ARM::t2MOVi16);
7147           MovHasS = false;
7148         }
7149         else
7150           UseMov = false;
7151       }
7152       if (UseMov) {
7153         TmpInst.addOperand(Inst.getOperand(0));           // Rt
7154         TmpInst.addOperand(MCOperand::createImm(Value));  // Immediate
7155         TmpInst.addOperand(Inst.getOperand(2));           // CondCode
7156         TmpInst.addOperand(Inst.getOperand(3));           // CondCode
7157         if (MovHasS)
7158           TmpInst.addOperand(MCOperand::createReg(0));    // S
7159         Inst = TmpInst;
7160         return true;
7161       }
7162     }
7163     // No opportunity to use MOV/MVN create constant pool
7164     const MCExpr *CPLoc =
7165       getTargetStreamer().addConstantPoolEntry(SubExprVal,
7166                                                PoolOperand.getStartLoc());
7167     TmpInst.addOperand(Inst.getOperand(0));           // Rt
7168     TmpInst.addOperand(MCOperand::createExpr(CPLoc)); // offset to constpool
7169     if (TmpInst.getOpcode() == ARM::LDRi12)
7170       TmpInst.addOperand(MCOperand::createImm(0));    // unused offset
7171     TmpInst.addOperand(Inst.getOperand(2));           // CondCode
7172     TmpInst.addOperand(Inst.getOperand(3));           // CondCode
7173     Inst = TmpInst;
7174     return true;
7175   }
7176   // Handle NEON VST complex aliases.
7177   case ARM::VST1LNdWB_register_Asm_8:
7178   case ARM::VST1LNdWB_register_Asm_16:
7179   case ARM::VST1LNdWB_register_Asm_32: {
7180     MCInst TmpInst;
7181     // Shuffle the operands around so the lane index operand is in the
7182     // right place.
7183     unsigned Spacing;
7184     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7185     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7186     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7187     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7188     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7189     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7190     TmpInst.addOperand(Inst.getOperand(1)); // lane
7191     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7192     TmpInst.addOperand(Inst.getOperand(6));
7193     Inst = TmpInst;
7194     return true;
7195   }
7196 
7197   case ARM::VST2LNdWB_register_Asm_8:
7198   case ARM::VST2LNdWB_register_Asm_16:
7199   case ARM::VST2LNdWB_register_Asm_32:
7200   case ARM::VST2LNqWB_register_Asm_16:
7201   case ARM::VST2LNqWB_register_Asm_32: {
7202     MCInst TmpInst;
7203     // Shuffle the operands around so the lane index operand is in the
7204     // right place.
7205     unsigned Spacing;
7206     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7207     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7208     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7209     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7210     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7211     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7212     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7213                                             Spacing));
7214     TmpInst.addOperand(Inst.getOperand(1)); // lane
7215     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7216     TmpInst.addOperand(Inst.getOperand(6));
7217     Inst = TmpInst;
7218     return true;
7219   }
7220 
7221   case ARM::VST3LNdWB_register_Asm_8:
7222   case ARM::VST3LNdWB_register_Asm_16:
7223   case ARM::VST3LNdWB_register_Asm_32:
7224   case ARM::VST3LNqWB_register_Asm_16:
7225   case ARM::VST3LNqWB_register_Asm_32: {
7226     MCInst TmpInst;
7227     // Shuffle the operands around so the lane index operand is in the
7228     // right place.
7229     unsigned Spacing;
7230     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7231     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7232     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7233     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7234     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7235     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7236     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7237                                             Spacing));
7238     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7239                                             Spacing * 2));
7240     TmpInst.addOperand(Inst.getOperand(1)); // lane
7241     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7242     TmpInst.addOperand(Inst.getOperand(6));
7243     Inst = TmpInst;
7244     return true;
7245   }
7246 
7247   case ARM::VST4LNdWB_register_Asm_8:
7248   case ARM::VST4LNdWB_register_Asm_16:
7249   case ARM::VST4LNdWB_register_Asm_32:
7250   case ARM::VST4LNqWB_register_Asm_16:
7251   case ARM::VST4LNqWB_register_Asm_32: {
7252     MCInst TmpInst;
7253     // Shuffle the operands around so the lane index operand is in the
7254     // right place.
7255     unsigned Spacing;
7256     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7257     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7258     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7259     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7260     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7261     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7262     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7263                                             Spacing));
7264     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7265                                             Spacing * 2));
7266     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7267                                             Spacing * 3));
7268     TmpInst.addOperand(Inst.getOperand(1)); // lane
7269     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7270     TmpInst.addOperand(Inst.getOperand(6));
7271     Inst = TmpInst;
7272     return true;
7273   }
7274 
7275   case ARM::VST1LNdWB_fixed_Asm_8:
7276   case ARM::VST1LNdWB_fixed_Asm_16:
7277   case ARM::VST1LNdWB_fixed_Asm_32: {
7278     MCInst TmpInst;
7279     // Shuffle the operands around so the lane index operand is in the
7280     // right place.
7281     unsigned Spacing;
7282     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7283     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7284     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7285     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7286     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7287     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7288     TmpInst.addOperand(Inst.getOperand(1)); // lane
7289     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7290     TmpInst.addOperand(Inst.getOperand(5));
7291     Inst = TmpInst;
7292     return true;
7293   }
7294 
7295   case ARM::VST2LNdWB_fixed_Asm_8:
7296   case ARM::VST2LNdWB_fixed_Asm_16:
7297   case ARM::VST2LNdWB_fixed_Asm_32:
7298   case ARM::VST2LNqWB_fixed_Asm_16:
7299   case ARM::VST2LNqWB_fixed_Asm_32: {
7300     MCInst TmpInst;
7301     // Shuffle the operands around so the lane index operand is in the
7302     // right place.
7303     unsigned Spacing;
7304     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7305     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7306     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7307     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7308     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7309     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7310     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7311                                             Spacing));
7312     TmpInst.addOperand(Inst.getOperand(1)); // lane
7313     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7314     TmpInst.addOperand(Inst.getOperand(5));
7315     Inst = TmpInst;
7316     return true;
7317   }
7318 
7319   case ARM::VST3LNdWB_fixed_Asm_8:
7320   case ARM::VST3LNdWB_fixed_Asm_16:
7321   case ARM::VST3LNdWB_fixed_Asm_32:
7322   case ARM::VST3LNqWB_fixed_Asm_16:
7323   case ARM::VST3LNqWB_fixed_Asm_32: {
7324     MCInst TmpInst;
7325     // Shuffle the operands around so the lane index operand is in the
7326     // right place.
7327     unsigned Spacing;
7328     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7329     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7330     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7331     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7332     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7333     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7334     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7335                                             Spacing));
7336     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7337                                             Spacing * 2));
7338     TmpInst.addOperand(Inst.getOperand(1)); // lane
7339     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7340     TmpInst.addOperand(Inst.getOperand(5));
7341     Inst = TmpInst;
7342     return true;
7343   }
7344 
7345   case ARM::VST4LNdWB_fixed_Asm_8:
7346   case ARM::VST4LNdWB_fixed_Asm_16:
7347   case ARM::VST4LNdWB_fixed_Asm_32:
7348   case ARM::VST4LNqWB_fixed_Asm_16:
7349   case ARM::VST4LNqWB_fixed_Asm_32: {
7350     MCInst TmpInst;
7351     // Shuffle the operands around so the lane index operand is in the
7352     // right place.
7353     unsigned Spacing;
7354     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7355     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7356     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7357     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7358     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7359     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7360     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7361                                             Spacing));
7362     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7363                                             Spacing * 2));
7364     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7365                                             Spacing * 3));
7366     TmpInst.addOperand(Inst.getOperand(1)); // lane
7367     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7368     TmpInst.addOperand(Inst.getOperand(5));
7369     Inst = TmpInst;
7370     return true;
7371   }
7372 
7373   case ARM::VST1LNdAsm_8:
7374   case ARM::VST1LNdAsm_16:
7375   case ARM::VST1LNdAsm_32: {
7376     MCInst TmpInst;
7377     // Shuffle the operands around so the lane index operand is in the
7378     // right place.
7379     unsigned Spacing;
7380     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7381     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7382     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7383     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7384     TmpInst.addOperand(Inst.getOperand(1)); // lane
7385     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7386     TmpInst.addOperand(Inst.getOperand(5));
7387     Inst = TmpInst;
7388     return true;
7389   }
7390 
7391   case ARM::VST2LNdAsm_8:
7392   case ARM::VST2LNdAsm_16:
7393   case ARM::VST2LNdAsm_32:
7394   case ARM::VST2LNqAsm_16:
7395   case ARM::VST2LNqAsm_32: {
7396     MCInst TmpInst;
7397     // Shuffle the operands around so the lane index operand is in the
7398     // right place.
7399     unsigned Spacing;
7400     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7401     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7402     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7403     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7404     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7405                                             Spacing));
7406     TmpInst.addOperand(Inst.getOperand(1)); // lane
7407     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7408     TmpInst.addOperand(Inst.getOperand(5));
7409     Inst = TmpInst;
7410     return true;
7411   }
7412 
7413   case ARM::VST3LNdAsm_8:
7414   case ARM::VST3LNdAsm_16:
7415   case ARM::VST3LNdAsm_32:
7416   case ARM::VST3LNqAsm_16:
7417   case ARM::VST3LNqAsm_32: {
7418     MCInst TmpInst;
7419     // Shuffle the operands around so the lane index operand is in the
7420     // right place.
7421     unsigned Spacing;
7422     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7423     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7424     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7425     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7426     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7427                                             Spacing));
7428     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7429                                             Spacing * 2));
7430     TmpInst.addOperand(Inst.getOperand(1)); // lane
7431     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7432     TmpInst.addOperand(Inst.getOperand(5));
7433     Inst = TmpInst;
7434     return true;
7435   }
7436 
7437   case ARM::VST4LNdAsm_8:
7438   case ARM::VST4LNdAsm_16:
7439   case ARM::VST4LNdAsm_32:
7440   case ARM::VST4LNqAsm_16:
7441   case ARM::VST4LNqAsm_32: {
7442     MCInst TmpInst;
7443     // Shuffle the operands around so the lane index operand is in the
7444     // right place.
7445     unsigned Spacing;
7446     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7447     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7448     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7449     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7450     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7451                                             Spacing));
7452     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7453                                             Spacing * 2));
7454     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7455                                             Spacing * 3));
7456     TmpInst.addOperand(Inst.getOperand(1)); // lane
7457     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7458     TmpInst.addOperand(Inst.getOperand(5));
7459     Inst = TmpInst;
7460     return true;
7461   }
7462 
7463   // Handle NEON VLD complex aliases.
7464   case ARM::VLD1LNdWB_register_Asm_8:
7465   case ARM::VLD1LNdWB_register_Asm_16:
7466   case ARM::VLD1LNdWB_register_Asm_32: {
7467     MCInst TmpInst;
7468     // Shuffle the operands around so the lane index operand is in the
7469     // right place.
7470     unsigned Spacing;
7471     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7472     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7473     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7474     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7475     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7476     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7477     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7478     TmpInst.addOperand(Inst.getOperand(1)); // lane
7479     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7480     TmpInst.addOperand(Inst.getOperand(6));
7481     Inst = TmpInst;
7482     return true;
7483   }
7484 
7485   case ARM::VLD2LNdWB_register_Asm_8:
7486   case ARM::VLD2LNdWB_register_Asm_16:
7487   case ARM::VLD2LNdWB_register_Asm_32:
7488   case ARM::VLD2LNqWB_register_Asm_16:
7489   case ARM::VLD2LNqWB_register_Asm_32: {
7490     MCInst TmpInst;
7491     // Shuffle the operands around so the lane index operand is in the
7492     // right place.
7493     unsigned Spacing;
7494     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7495     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7496     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7497                                             Spacing));
7498     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7499     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7500     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7501     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7502     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7503     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7504                                             Spacing));
7505     TmpInst.addOperand(Inst.getOperand(1)); // lane
7506     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7507     TmpInst.addOperand(Inst.getOperand(6));
7508     Inst = TmpInst;
7509     return true;
7510   }
7511 
7512   case ARM::VLD3LNdWB_register_Asm_8:
7513   case ARM::VLD3LNdWB_register_Asm_16:
7514   case ARM::VLD3LNdWB_register_Asm_32:
7515   case ARM::VLD3LNqWB_register_Asm_16:
7516   case ARM::VLD3LNqWB_register_Asm_32: {
7517     MCInst TmpInst;
7518     // Shuffle the operands around so the lane index operand is in the
7519     // right place.
7520     unsigned Spacing;
7521     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7522     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7523     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7524                                             Spacing));
7525     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7526                                             Spacing * 2));
7527     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7528     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7529     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7530     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7531     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7532     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7533                                             Spacing));
7534     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7535                                             Spacing * 2));
7536     TmpInst.addOperand(Inst.getOperand(1)); // lane
7537     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7538     TmpInst.addOperand(Inst.getOperand(6));
7539     Inst = TmpInst;
7540     return true;
7541   }
7542 
7543   case ARM::VLD4LNdWB_register_Asm_8:
7544   case ARM::VLD4LNdWB_register_Asm_16:
7545   case ARM::VLD4LNdWB_register_Asm_32:
7546   case ARM::VLD4LNqWB_register_Asm_16:
7547   case ARM::VLD4LNqWB_register_Asm_32: {
7548     MCInst TmpInst;
7549     // Shuffle the operands around so the lane index operand is in the
7550     // right place.
7551     unsigned Spacing;
7552     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7553     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7554     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7555                                             Spacing));
7556     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7557                                             Spacing * 2));
7558     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7559                                             Spacing * 3));
7560     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7561     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7562     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7563     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7564     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7565     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7566                                             Spacing));
7567     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7568                                             Spacing * 2));
7569     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7570                                             Spacing * 3));
7571     TmpInst.addOperand(Inst.getOperand(1)); // lane
7572     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7573     TmpInst.addOperand(Inst.getOperand(6));
7574     Inst = TmpInst;
7575     return true;
7576   }
7577 
7578   case ARM::VLD1LNdWB_fixed_Asm_8:
7579   case ARM::VLD1LNdWB_fixed_Asm_16:
7580   case ARM::VLD1LNdWB_fixed_Asm_32: {
7581     MCInst TmpInst;
7582     // Shuffle the operands around so the lane index operand is in the
7583     // right place.
7584     unsigned Spacing;
7585     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7586     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7587     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7588     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7589     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7590     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7591     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7592     TmpInst.addOperand(Inst.getOperand(1)); // lane
7593     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7594     TmpInst.addOperand(Inst.getOperand(5));
7595     Inst = TmpInst;
7596     return true;
7597   }
7598 
7599   case ARM::VLD2LNdWB_fixed_Asm_8:
7600   case ARM::VLD2LNdWB_fixed_Asm_16:
7601   case ARM::VLD2LNdWB_fixed_Asm_32:
7602   case ARM::VLD2LNqWB_fixed_Asm_16:
7603   case ARM::VLD2LNqWB_fixed_Asm_32: {
7604     MCInst TmpInst;
7605     // Shuffle the operands around so the lane index operand is in the
7606     // right place.
7607     unsigned Spacing;
7608     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7609     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7610     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7611                                             Spacing));
7612     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7613     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7614     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7615     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7616     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7617     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7618                                             Spacing));
7619     TmpInst.addOperand(Inst.getOperand(1)); // lane
7620     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7621     TmpInst.addOperand(Inst.getOperand(5));
7622     Inst = TmpInst;
7623     return true;
7624   }
7625 
7626   case ARM::VLD3LNdWB_fixed_Asm_8:
7627   case ARM::VLD3LNdWB_fixed_Asm_16:
7628   case ARM::VLD3LNdWB_fixed_Asm_32:
7629   case ARM::VLD3LNqWB_fixed_Asm_16:
7630   case ARM::VLD3LNqWB_fixed_Asm_32: {
7631     MCInst TmpInst;
7632     // Shuffle the operands around so the lane index operand is in the
7633     // right place.
7634     unsigned Spacing;
7635     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7636     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7637     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7638                                             Spacing));
7639     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7640                                             Spacing * 2));
7641     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7642     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7643     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7644     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7645     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7646     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7647                                             Spacing));
7648     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7649                                             Spacing * 2));
7650     TmpInst.addOperand(Inst.getOperand(1)); // lane
7651     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7652     TmpInst.addOperand(Inst.getOperand(5));
7653     Inst = TmpInst;
7654     return true;
7655   }
7656 
7657   case ARM::VLD4LNdWB_fixed_Asm_8:
7658   case ARM::VLD4LNdWB_fixed_Asm_16:
7659   case ARM::VLD4LNdWB_fixed_Asm_32:
7660   case ARM::VLD4LNqWB_fixed_Asm_16:
7661   case ARM::VLD4LNqWB_fixed_Asm_32: {
7662     MCInst TmpInst;
7663     // Shuffle the operands around so the lane index operand is in the
7664     // right place.
7665     unsigned Spacing;
7666     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7667     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7668     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7669                                             Spacing));
7670     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7671                                             Spacing * 2));
7672     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7673                                             Spacing * 3));
7674     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7675     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7676     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7677     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7678     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7679     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7680                                             Spacing));
7681     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7682                                             Spacing * 2));
7683     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7684                                             Spacing * 3));
7685     TmpInst.addOperand(Inst.getOperand(1)); // lane
7686     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7687     TmpInst.addOperand(Inst.getOperand(5));
7688     Inst = TmpInst;
7689     return true;
7690   }
7691 
7692   case ARM::VLD1LNdAsm_8:
7693   case ARM::VLD1LNdAsm_16:
7694   case ARM::VLD1LNdAsm_32: {
7695     MCInst TmpInst;
7696     // Shuffle the operands around so the lane index operand is in the
7697     // right place.
7698     unsigned Spacing;
7699     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7700     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7701     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7702     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7703     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7704     TmpInst.addOperand(Inst.getOperand(1)); // lane
7705     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7706     TmpInst.addOperand(Inst.getOperand(5));
7707     Inst = TmpInst;
7708     return true;
7709   }
7710 
7711   case ARM::VLD2LNdAsm_8:
7712   case ARM::VLD2LNdAsm_16:
7713   case ARM::VLD2LNdAsm_32:
7714   case ARM::VLD2LNqAsm_16:
7715   case ARM::VLD2LNqAsm_32: {
7716     MCInst TmpInst;
7717     // Shuffle the operands around so the lane index operand is in the
7718     // right place.
7719     unsigned Spacing;
7720     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7721     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7722     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7723                                             Spacing));
7724     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7725     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7726     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7727     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7728                                             Spacing));
7729     TmpInst.addOperand(Inst.getOperand(1)); // lane
7730     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7731     TmpInst.addOperand(Inst.getOperand(5));
7732     Inst = TmpInst;
7733     return true;
7734   }
7735 
7736   case ARM::VLD3LNdAsm_8:
7737   case ARM::VLD3LNdAsm_16:
7738   case ARM::VLD3LNdAsm_32:
7739   case ARM::VLD3LNqAsm_16:
7740   case ARM::VLD3LNqAsm_32: {
7741     MCInst TmpInst;
7742     // Shuffle the operands around so the lane index operand is in the
7743     // right place.
7744     unsigned Spacing;
7745     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7746     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7747     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7748                                             Spacing));
7749     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7750                                             Spacing * 2));
7751     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7752     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7753     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7754     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7755                                             Spacing));
7756     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7757                                             Spacing * 2));
7758     TmpInst.addOperand(Inst.getOperand(1)); // lane
7759     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7760     TmpInst.addOperand(Inst.getOperand(5));
7761     Inst = TmpInst;
7762     return true;
7763   }
7764 
7765   case ARM::VLD4LNdAsm_8:
7766   case ARM::VLD4LNdAsm_16:
7767   case ARM::VLD4LNdAsm_32:
7768   case ARM::VLD4LNqAsm_16:
7769   case ARM::VLD4LNqAsm_32: {
7770     MCInst TmpInst;
7771     // Shuffle the operands around so the lane index operand is in the
7772     // right place.
7773     unsigned Spacing;
7774     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7775     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7776     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7777                                             Spacing));
7778     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7779                                             Spacing * 2));
7780     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7781                                             Spacing * 3));
7782     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7783     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7784     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7785     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7786                                             Spacing));
7787     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7788                                             Spacing * 2));
7789     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7790                                             Spacing * 3));
7791     TmpInst.addOperand(Inst.getOperand(1)); // lane
7792     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7793     TmpInst.addOperand(Inst.getOperand(5));
7794     Inst = TmpInst;
7795     return true;
7796   }
7797 
7798   // VLD3DUP single 3-element structure to all lanes instructions.
7799   case ARM::VLD3DUPdAsm_8:
7800   case ARM::VLD3DUPdAsm_16:
7801   case ARM::VLD3DUPdAsm_32:
7802   case ARM::VLD3DUPqAsm_8:
7803   case ARM::VLD3DUPqAsm_16:
7804   case ARM::VLD3DUPqAsm_32: {
7805     MCInst TmpInst;
7806     unsigned Spacing;
7807     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7808     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7809     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7810                                             Spacing));
7811     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7812                                             Spacing * 2));
7813     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7814     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7815     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7816     TmpInst.addOperand(Inst.getOperand(4));
7817     Inst = TmpInst;
7818     return true;
7819   }
7820 
7821   case ARM::VLD3DUPdWB_fixed_Asm_8:
7822   case ARM::VLD3DUPdWB_fixed_Asm_16:
7823   case ARM::VLD3DUPdWB_fixed_Asm_32:
7824   case ARM::VLD3DUPqWB_fixed_Asm_8:
7825   case ARM::VLD3DUPqWB_fixed_Asm_16:
7826   case ARM::VLD3DUPqWB_fixed_Asm_32: {
7827     MCInst TmpInst;
7828     unsigned Spacing;
7829     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7830     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7831     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7832                                             Spacing));
7833     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7834                                             Spacing * 2));
7835     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7836     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7837     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7838     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7839     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7840     TmpInst.addOperand(Inst.getOperand(4));
7841     Inst = TmpInst;
7842     return true;
7843   }
7844 
7845   case ARM::VLD3DUPdWB_register_Asm_8:
7846   case ARM::VLD3DUPdWB_register_Asm_16:
7847   case ARM::VLD3DUPdWB_register_Asm_32:
7848   case ARM::VLD3DUPqWB_register_Asm_8:
7849   case ARM::VLD3DUPqWB_register_Asm_16:
7850   case ARM::VLD3DUPqWB_register_Asm_32: {
7851     MCInst TmpInst;
7852     unsigned Spacing;
7853     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7854     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7855     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7856                                             Spacing));
7857     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7858                                             Spacing * 2));
7859     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7860     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7861     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7862     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7863     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7864     TmpInst.addOperand(Inst.getOperand(5));
7865     Inst = TmpInst;
7866     return true;
7867   }
7868 
7869   // VLD3 multiple 3-element structure instructions.
7870   case ARM::VLD3dAsm_8:
7871   case ARM::VLD3dAsm_16:
7872   case ARM::VLD3dAsm_32:
7873   case ARM::VLD3qAsm_8:
7874   case ARM::VLD3qAsm_16:
7875   case ARM::VLD3qAsm_32: {
7876     MCInst TmpInst;
7877     unsigned Spacing;
7878     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7879     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7880     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7881                                             Spacing));
7882     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7883                                             Spacing * 2));
7884     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7885     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7886     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7887     TmpInst.addOperand(Inst.getOperand(4));
7888     Inst = TmpInst;
7889     return true;
7890   }
7891 
7892   case ARM::VLD3dWB_fixed_Asm_8:
7893   case ARM::VLD3dWB_fixed_Asm_16:
7894   case ARM::VLD3dWB_fixed_Asm_32:
7895   case ARM::VLD3qWB_fixed_Asm_8:
7896   case ARM::VLD3qWB_fixed_Asm_16:
7897   case ARM::VLD3qWB_fixed_Asm_32: {
7898     MCInst TmpInst;
7899     unsigned Spacing;
7900     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7901     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7902     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7903                                             Spacing));
7904     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7905                                             Spacing * 2));
7906     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7907     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7908     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7909     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7910     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7911     TmpInst.addOperand(Inst.getOperand(4));
7912     Inst = TmpInst;
7913     return true;
7914   }
7915 
7916   case ARM::VLD3dWB_register_Asm_8:
7917   case ARM::VLD3dWB_register_Asm_16:
7918   case ARM::VLD3dWB_register_Asm_32:
7919   case ARM::VLD3qWB_register_Asm_8:
7920   case ARM::VLD3qWB_register_Asm_16:
7921   case ARM::VLD3qWB_register_Asm_32: {
7922     MCInst TmpInst;
7923     unsigned Spacing;
7924     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7925     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7926     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7927                                             Spacing));
7928     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7929                                             Spacing * 2));
7930     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7931     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7932     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7933     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7934     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7935     TmpInst.addOperand(Inst.getOperand(5));
7936     Inst = TmpInst;
7937     return true;
7938   }
7939 
7940   // VLD4DUP single 3-element structure to all lanes instructions.
7941   case ARM::VLD4DUPdAsm_8:
7942   case ARM::VLD4DUPdAsm_16:
7943   case ARM::VLD4DUPdAsm_32:
7944   case ARM::VLD4DUPqAsm_8:
7945   case ARM::VLD4DUPqAsm_16:
7946   case ARM::VLD4DUPqAsm_32: {
7947     MCInst TmpInst;
7948     unsigned Spacing;
7949     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7950     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7951     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7952                                             Spacing));
7953     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7954                                             Spacing * 2));
7955     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7956                                             Spacing * 3));
7957     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7958     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7959     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7960     TmpInst.addOperand(Inst.getOperand(4));
7961     Inst = TmpInst;
7962     return true;
7963   }
7964 
7965   case ARM::VLD4DUPdWB_fixed_Asm_8:
7966   case ARM::VLD4DUPdWB_fixed_Asm_16:
7967   case ARM::VLD4DUPdWB_fixed_Asm_32:
7968   case ARM::VLD4DUPqWB_fixed_Asm_8:
7969   case ARM::VLD4DUPqWB_fixed_Asm_16:
7970   case ARM::VLD4DUPqWB_fixed_Asm_32: {
7971     MCInst TmpInst;
7972     unsigned Spacing;
7973     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7974     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7975     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7976                                             Spacing));
7977     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7978                                             Spacing * 2));
7979     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7980                                             Spacing * 3));
7981     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7982     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7983     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7984     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7985     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7986     TmpInst.addOperand(Inst.getOperand(4));
7987     Inst = TmpInst;
7988     return true;
7989   }
7990 
7991   case ARM::VLD4DUPdWB_register_Asm_8:
7992   case ARM::VLD4DUPdWB_register_Asm_16:
7993   case ARM::VLD4DUPdWB_register_Asm_32:
7994   case ARM::VLD4DUPqWB_register_Asm_8:
7995   case ARM::VLD4DUPqWB_register_Asm_16:
7996   case ARM::VLD4DUPqWB_register_Asm_32: {
7997     MCInst TmpInst;
7998     unsigned Spacing;
7999     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
8000     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8001     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8002                                             Spacing));
8003     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8004                                             Spacing * 2));
8005     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8006                                             Spacing * 3));
8007     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8008     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8009     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8010     TmpInst.addOperand(Inst.getOperand(3)); // Rm
8011     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
8012     TmpInst.addOperand(Inst.getOperand(5));
8013     Inst = TmpInst;
8014     return true;
8015   }
8016 
8017   // VLD4 multiple 4-element structure instructions.
8018   case ARM::VLD4dAsm_8:
8019   case ARM::VLD4dAsm_16:
8020   case ARM::VLD4dAsm_32:
8021   case ARM::VLD4qAsm_8:
8022   case ARM::VLD4qAsm_16:
8023   case ARM::VLD4qAsm_32: {
8024     MCInst TmpInst;
8025     unsigned Spacing;
8026     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
8027     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8028     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8029                                             Spacing));
8030     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8031                                             Spacing * 2));
8032     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8033                                             Spacing * 3));
8034     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8035     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8036     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8037     TmpInst.addOperand(Inst.getOperand(4));
8038     Inst = TmpInst;
8039     return true;
8040   }
8041 
8042   case ARM::VLD4dWB_fixed_Asm_8:
8043   case ARM::VLD4dWB_fixed_Asm_16:
8044   case ARM::VLD4dWB_fixed_Asm_32:
8045   case ARM::VLD4qWB_fixed_Asm_8:
8046   case ARM::VLD4qWB_fixed_Asm_16:
8047   case ARM::VLD4qWB_fixed_Asm_32: {
8048     MCInst TmpInst;
8049     unsigned Spacing;
8050     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
8051     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8052     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8053                                             Spacing));
8054     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8055                                             Spacing * 2));
8056     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8057                                             Spacing * 3));
8058     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8059     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8060     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8061     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
8062     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8063     TmpInst.addOperand(Inst.getOperand(4));
8064     Inst = TmpInst;
8065     return true;
8066   }
8067 
8068   case ARM::VLD4dWB_register_Asm_8:
8069   case ARM::VLD4dWB_register_Asm_16:
8070   case ARM::VLD4dWB_register_Asm_32:
8071   case ARM::VLD4qWB_register_Asm_8:
8072   case ARM::VLD4qWB_register_Asm_16:
8073   case ARM::VLD4qWB_register_Asm_32: {
8074     MCInst TmpInst;
8075     unsigned Spacing;
8076     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
8077     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8078     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8079                                             Spacing));
8080     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8081                                             Spacing * 2));
8082     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8083                                             Spacing * 3));
8084     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8085     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8086     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8087     TmpInst.addOperand(Inst.getOperand(3)); // Rm
8088     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
8089     TmpInst.addOperand(Inst.getOperand(5));
8090     Inst = TmpInst;
8091     return true;
8092   }
8093 
8094   // VST3 multiple 3-element structure instructions.
8095   case ARM::VST3dAsm_8:
8096   case ARM::VST3dAsm_16:
8097   case ARM::VST3dAsm_32:
8098   case ARM::VST3qAsm_8:
8099   case ARM::VST3qAsm_16:
8100   case ARM::VST3qAsm_32: {
8101     MCInst TmpInst;
8102     unsigned Spacing;
8103     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8104     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8105     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8106     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8107     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8108                                             Spacing));
8109     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8110                                             Spacing * 2));
8111     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8112     TmpInst.addOperand(Inst.getOperand(4));
8113     Inst = TmpInst;
8114     return true;
8115   }
8116 
8117   case ARM::VST3dWB_fixed_Asm_8:
8118   case ARM::VST3dWB_fixed_Asm_16:
8119   case ARM::VST3dWB_fixed_Asm_32:
8120   case ARM::VST3qWB_fixed_Asm_8:
8121   case ARM::VST3qWB_fixed_Asm_16:
8122   case ARM::VST3qWB_fixed_Asm_32: {
8123     MCInst TmpInst;
8124     unsigned Spacing;
8125     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8126     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8127     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8128     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8129     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
8130     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8131     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8132                                             Spacing));
8133     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8134                                             Spacing * 2));
8135     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8136     TmpInst.addOperand(Inst.getOperand(4));
8137     Inst = TmpInst;
8138     return true;
8139   }
8140 
8141   case ARM::VST3dWB_register_Asm_8:
8142   case ARM::VST3dWB_register_Asm_16:
8143   case ARM::VST3dWB_register_Asm_32:
8144   case ARM::VST3qWB_register_Asm_8:
8145   case ARM::VST3qWB_register_Asm_16:
8146   case ARM::VST3qWB_register_Asm_32: {
8147     MCInst TmpInst;
8148     unsigned Spacing;
8149     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8150     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8151     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8152     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8153     TmpInst.addOperand(Inst.getOperand(3)); // Rm
8154     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8155     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8156                                             Spacing));
8157     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8158                                             Spacing * 2));
8159     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
8160     TmpInst.addOperand(Inst.getOperand(5));
8161     Inst = TmpInst;
8162     return true;
8163   }
8164 
8165   // VST4 multiple 3-element structure instructions.
8166   case ARM::VST4dAsm_8:
8167   case ARM::VST4dAsm_16:
8168   case ARM::VST4dAsm_32:
8169   case ARM::VST4qAsm_8:
8170   case ARM::VST4qAsm_16:
8171   case ARM::VST4qAsm_32: {
8172     MCInst TmpInst;
8173     unsigned Spacing;
8174     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8175     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8176     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8177     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8178     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8179                                             Spacing));
8180     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8181                                             Spacing * 2));
8182     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8183                                             Spacing * 3));
8184     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8185     TmpInst.addOperand(Inst.getOperand(4));
8186     Inst = TmpInst;
8187     return true;
8188   }
8189 
8190   case ARM::VST4dWB_fixed_Asm_8:
8191   case ARM::VST4dWB_fixed_Asm_16:
8192   case ARM::VST4dWB_fixed_Asm_32:
8193   case ARM::VST4qWB_fixed_Asm_8:
8194   case ARM::VST4qWB_fixed_Asm_16:
8195   case ARM::VST4qWB_fixed_Asm_32: {
8196     MCInst TmpInst;
8197     unsigned Spacing;
8198     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8199     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8200     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8201     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8202     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
8203     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8204     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8205                                             Spacing));
8206     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8207                                             Spacing * 2));
8208     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8209                                             Spacing * 3));
8210     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8211     TmpInst.addOperand(Inst.getOperand(4));
8212     Inst = TmpInst;
8213     return true;
8214   }
8215 
8216   case ARM::VST4dWB_register_Asm_8:
8217   case ARM::VST4dWB_register_Asm_16:
8218   case ARM::VST4dWB_register_Asm_32:
8219   case ARM::VST4qWB_register_Asm_8:
8220   case ARM::VST4qWB_register_Asm_16:
8221   case ARM::VST4qWB_register_Asm_32: {
8222     MCInst TmpInst;
8223     unsigned Spacing;
8224     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8225     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8226     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8227     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8228     TmpInst.addOperand(Inst.getOperand(3)); // Rm
8229     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8230     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8231                                             Spacing));
8232     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8233                                             Spacing * 2));
8234     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8235                                             Spacing * 3));
8236     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
8237     TmpInst.addOperand(Inst.getOperand(5));
8238     Inst = TmpInst;
8239     return true;
8240   }
8241 
8242   // Handle encoding choice for the shift-immediate instructions.
8243   case ARM::t2LSLri:
8244   case ARM::t2LSRri:
8245   case ARM::t2ASRri: {
8246     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8247         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
8248         Inst.getOperand(5).getReg() == (inITBlock() ? 0 : ARM::CPSR) &&
8249         !(static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8250           static_cast<ARMOperand &>(*Operands[3]).getToken() == ".w")) {
8251       unsigned NewOpc;
8252       switch (Inst.getOpcode()) {
8253       default: llvm_unreachable("unexpected opcode");
8254       case ARM::t2LSLri: NewOpc = ARM::tLSLri; break;
8255       case ARM::t2LSRri: NewOpc = ARM::tLSRri; break;
8256       case ARM::t2ASRri: NewOpc = ARM::tASRri; break;
8257       }
8258       // The Thumb1 operands aren't in the same order. Awesome, eh?
8259       MCInst TmpInst;
8260       TmpInst.setOpcode(NewOpc);
8261       TmpInst.addOperand(Inst.getOperand(0));
8262       TmpInst.addOperand(Inst.getOperand(5));
8263       TmpInst.addOperand(Inst.getOperand(1));
8264       TmpInst.addOperand(Inst.getOperand(2));
8265       TmpInst.addOperand(Inst.getOperand(3));
8266       TmpInst.addOperand(Inst.getOperand(4));
8267       Inst = TmpInst;
8268       return true;
8269     }
8270     return false;
8271   }
8272 
8273   // Handle the Thumb2 mode MOV complex aliases.
8274   case ARM::t2MOVsr:
8275   case ARM::t2MOVSsr: {
8276     // Which instruction to expand to depends on the CCOut operand and
8277     // whether we're in an IT block if the register operands are low
8278     // registers.
8279     bool isNarrow = false;
8280     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8281         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8282         isARMLowRegister(Inst.getOperand(2).getReg()) &&
8283         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
8284         inITBlock() == (Inst.getOpcode() == ARM::t2MOVsr))
8285       isNarrow = true;
8286     MCInst TmpInst;
8287     unsigned newOpc;
8288     switch(ARM_AM::getSORegShOp(Inst.getOperand(3).getImm())) {
8289     default: llvm_unreachable("unexpected opcode!");
8290     case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRrr : ARM::t2ASRrr; break;
8291     case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRrr : ARM::t2LSRrr; break;
8292     case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLrr : ARM::t2LSLrr; break;
8293     case ARM_AM::ror: newOpc = isNarrow ? ARM::tROR   : ARM::t2RORrr; break;
8294     }
8295     TmpInst.setOpcode(newOpc);
8296     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8297     if (isNarrow)
8298       TmpInst.addOperand(MCOperand::createReg(
8299           Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
8300     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8301     TmpInst.addOperand(Inst.getOperand(2)); // Rm
8302     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
8303     TmpInst.addOperand(Inst.getOperand(5));
8304     if (!isNarrow)
8305       TmpInst.addOperand(MCOperand::createReg(
8306           Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
8307     Inst = TmpInst;
8308     return true;
8309   }
8310   case ARM::t2MOVsi:
8311   case ARM::t2MOVSsi: {
8312     // Which instruction to expand to depends on the CCOut operand and
8313     // whether we're in an IT block if the register operands are low
8314     // registers.
8315     bool isNarrow = false;
8316     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8317         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8318         inITBlock() == (Inst.getOpcode() == ARM::t2MOVsi))
8319       isNarrow = true;
8320     MCInst TmpInst;
8321     unsigned newOpc;
8322     switch(ARM_AM::getSORegShOp(Inst.getOperand(2).getImm())) {
8323     default: llvm_unreachable("unexpected opcode!");
8324     case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRri : ARM::t2ASRri; break;
8325     case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRri : ARM::t2LSRri; break;
8326     case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLri : ARM::t2LSLri; break;
8327     case ARM_AM::ror: newOpc = ARM::t2RORri; isNarrow = false; break;
8328     case ARM_AM::rrx: isNarrow = false; newOpc = ARM::t2RRX; break;
8329     }
8330     unsigned Amount = ARM_AM::getSORegOffset(Inst.getOperand(2).getImm());
8331     if (Amount == 32) Amount = 0;
8332     TmpInst.setOpcode(newOpc);
8333     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8334     if (isNarrow)
8335       TmpInst.addOperand(MCOperand::createReg(
8336           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
8337     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8338     if (newOpc != ARM::t2RRX)
8339       TmpInst.addOperand(MCOperand::createImm(Amount));
8340     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8341     TmpInst.addOperand(Inst.getOperand(4));
8342     if (!isNarrow)
8343       TmpInst.addOperand(MCOperand::createReg(
8344           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
8345     Inst = TmpInst;
8346     return true;
8347   }
8348   // Handle the ARM mode MOV complex aliases.
8349   case ARM::ASRr:
8350   case ARM::LSRr:
8351   case ARM::LSLr:
8352   case ARM::RORr: {
8353     ARM_AM::ShiftOpc ShiftTy;
8354     switch(Inst.getOpcode()) {
8355     default: llvm_unreachable("unexpected opcode!");
8356     case ARM::ASRr: ShiftTy = ARM_AM::asr; break;
8357     case ARM::LSRr: ShiftTy = ARM_AM::lsr; break;
8358     case ARM::LSLr: ShiftTy = ARM_AM::lsl; break;
8359     case ARM::RORr: ShiftTy = ARM_AM::ror; break;
8360     }
8361     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, 0);
8362     MCInst TmpInst;
8363     TmpInst.setOpcode(ARM::MOVsr);
8364     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8365     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8366     TmpInst.addOperand(Inst.getOperand(2)); // Rm
8367     TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8368     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8369     TmpInst.addOperand(Inst.getOperand(4));
8370     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
8371     Inst = TmpInst;
8372     return true;
8373   }
8374   case ARM::ASRi:
8375   case ARM::LSRi:
8376   case ARM::LSLi:
8377   case ARM::RORi: {
8378     ARM_AM::ShiftOpc ShiftTy;
8379     switch(Inst.getOpcode()) {
8380     default: llvm_unreachable("unexpected opcode!");
8381     case ARM::ASRi: ShiftTy = ARM_AM::asr; break;
8382     case ARM::LSRi: ShiftTy = ARM_AM::lsr; break;
8383     case ARM::LSLi: ShiftTy = ARM_AM::lsl; break;
8384     case ARM::RORi: ShiftTy = ARM_AM::ror; break;
8385     }
8386     // A shift by zero is a plain MOVr, not a MOVsi.
8387     unsigned Amt = Inst.getOperand(2).getImm();
8388     unsigned Opc = Amt == 0 ? ARM::MOVr : ARM::MOVsi;
8389     // A shift by 32 should be encoded as 0 when permitted
8390     if (Amt == 32 && (ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr))
8391       Amt = 0;
8392     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, Amt);
8393     MCInst TmpInst;
8394     TmpInst.setOpcode(Opc);
8395     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8396     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8397     if (Opc == ARM::MOVsi)
8398       TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8399     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8400     TmpInst.addOperand(Inst.getOperand(4));
8401     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
8402     Inst = TmpInst;
8403     return true;
8404   }
8405   case ARM::RRXi: {
8406     unsigned Shifter = ARM_AM::getSORegOpc(ARM_AM::rrx, 0);
8407     MCInst TmpInst;
8408     TmpInst.setOpcode(ARM::MOVsi);
8409     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8410     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8411     TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8412     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8413     TmpInst.addOperand(Inst.getOperand(3));
8414     TmpInst.addOperand(Inst.getOperand(4)); // cc_out
8415     Inst = TmpInst;
8416     return true;
8417   }
8418   case ARM::t2LDMIA_UPD: {
8419     // If this is a load of a single register, then we should use
8420     // a post-indexed LDR instruction instead, per the ARM ARM.
8421     if (Inst.getNumOperands() != 5)
8422       return false;
8423     MCInst TmpInst;
8424     TmpInst.setOpcode(ARM::t2LDR_POST);
8425     TmpInst.addOperand(Inst.getOperand(4)); // Rt
8426     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8427     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8428     TmpInst.addOperand(MCOperand::createImm(4));
8429     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8430     TmpInst.addOperand(Inst.getOperand(3));
8431     Inst = TmpInst;
8432     return true;
8433   }
8434   case ARM::t2STMDB_UPD: {
8435     // If this is a store of a single register, then we should use
8436     // a pre-indexed STR instruction instead, per the ARM ARM.
8437     if (Inst.getNumOperands() != 5)
8438       return false;
8439     MCInst TmpInst;
8440     TmpInst.setOpcode(ARM::t2STR_PRE);
8441     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8442     TmpInst.addOperand(Inst.getOperand(4)); // Rt
8443     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8444     TmpInst.addOperand(MCOperand::createImm(-4));
8445     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8446     TmpInst.addOperand(Inst.getOperand(3));
8447     Inst = TmpInst;
8448     return true;
8449   }
8450   case ARM::LDMIA_UPD:
8451     // If this is a load of a single register via a 'pop', then we should use
8452     // a post-indexed LDR instruction instead, per the ARM ARM.
8453     if (static_cast<ARMOperand &>(*Operands[0]).getToken() == "pop" &&
8454         Inst.getNumOperands() == 5) {
8455       MCInst TmpInst;
8456       TmpInst.setOpcode(ARM::LDR_POST_IMM);
8457       TmpInst.addOperand(Inst.getOperand(4)); // Rt
8458       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8459       TmpInst.addOperand(Inst.getOperand(1)); // Rn
8460       TmpInst.addOperand(MCOperand::createReg(0));  // am2offset
8461       TmpInst.addOperand(MCOperand::createImm(4));
8462       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8463       TmpInst.addOperand(Inst.getOperand(3));
8464       Inst = TmpInst;
8465       return true;
8466     }
8467     break;
8468   case ARM::STMDB_UPD:
8469     // If this is a store of a single register via a 'push', then we should use
8470     // a pre-indexed STR instruction instead, per the ARM ARM.
8471     if (static_cast<ARMOperand &>(*Operands[0]).getToken() == "push" &&
8472         Inst.getNumOperands() == 5) {
8473       MCInst TmpInst;
8474       TmpInst.setOpcode(ARM::STR_PRE_IMM);
8475       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8476       TmpInst.addOperand(Inst.getOperand(4)); // Rt
8477       TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
8478       TmpInst.addOperand(MCOperand::createImm(-4));
8479       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8480       TmpInst.addOperand(Inst.getOperand(3));
8481       Inst = TmpInst;
8482     }
8483     break;
8484   case ARM::t2ADDri12:
8485     // If the immediate fits for encoding T3 (t2ADDri) and the generic "add"
8486     // mnemonic was used (not "addw"), encoding T3 is preferred.
8487     if (static_cast<ARMOperand &>(*Operands[0]).getToken() != "add" ||
8488         ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
8489       break;
8490     Inst.setOpcode(ARM::t2ADDri);
8491     Inst.addOperand(MCOperand::createReg(0)); // cc_out
8492     break;
8493   case ARM::t2SUBri12:
8494     // If the immediate fits for encoding T3 (t2SUBri) and the generic "sub"
8495     // mnemonic was used (not "subw"), encoding T3 is preferred.
8496     if (static_cast<ARMOperand &>(*Operands[0]).getToken() != "sub" ||
8497         ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
8498       break;
8499     Inst.setOpcode(ARM::t2SUBri);
8500     Inst.addOperand(MCOperand::createReg(0)); // cc_out
8501     break;
8502   case ARM::tADDi8:
8503     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
8504     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
8505     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
8506     // to encoding T1 if <Rd> is omitted."
8507     if ((unsigned)Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
8508       Inst.setOpcode(ARM::tADDi3);
8509       return true;
8510     }
8511     break;
8512   case ARM::tSUBi8:
8513     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
8514     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
8515     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
8516     // to encoding T1 if <Rd> is omitted."
8517     if ((unsigned)Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
8518       Inst.setOpcode(ARM::tSUBi3);
8519       return true;
8520     }
8521     break;
8522   case ARM::t2ADDri:
8523   case ARM::t2SUBri: {
8524     // If the destination and first source operand are the same, and
8525     // the flags are compatible with the current IT status, use encoding T2
8526     // instead of T3. For compatibility with the system 'as'. Make sure the
8527     // wide encoding wasn't explicit.
8528     if (Inst.getOperand(0).getReg() != Inst.getOperand(1).getReg() ||
8529         !isARMLowRegister(Inst.getOperand(0).getReg()) ||
8530         (unsigned)Inst.getOperand(2).getImm() > 255 ||
8531         ((!inITBlock() && Inst.getOperand(5).getReg() != ARM::CPSR) ||
8532          (inITBlock() && Inst.getOperand(5).getReg() != 0)) ||
8533         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8534          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".w"))
8535       break;
8536     MCInst TmpInst;
8537     TmpInst.setOpcode(Inst.getOpcode() == ARM::t2ADDri ?
8538                       ARM::tADDi8 : ARM::tSUBi8);
8539     TmpInst.addOperand(Inst.getOperand(0));
8540     TmpInst.addOperand(Inst.getOperand(5));
8541     TmpInst.addOperand(Inst.getOperand(0));
8542     TmpInst.addOperand(Inst.getOperand(2));
8543     TmpInst.addOperand(Inst.getOperand(3));
8544     TmpInst.addOperand(Inst.getOperand(4));
8545     Inst = TmpInst;
8546     return true;
8547   }
8548   case ARM::t2ADDrr: {
8549     // If the destination and first source operand are the same, and
8550     // there's no setting of the flags, use encoding T2 instead of T3.
8551     // Note that this is only for ADD, not SUB. This mirrors the system
8552     // 'as' behaviour.  Also take advantage of ADD being commutative.
8553     // Make sure the wide encoding wasn't explicit.
8554     bool Swap = false;
8555     auto DestReg = Inst.getOperand(0).getReg();
8556     bool Transform = DestReg == Inst.getOperand(1).getReg();
8557     if (!Transform && DestReg == Inst.getOperand(2).getReg()) {
8558       Transform = true;
8559       Swap = true;
8560     }
8561     if (!Transform ||
8562         Inst.getOperand(5).getReg() != 0 ||
8563         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8564          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".w"))
8565       break;
8566     MCInst TmpInst;
8567     TmpInst.setOpcode(ARM::tADDhirr);
8568     TmpInst.addOperand(Inst.getOperand(0));
8569     TmpInst.addOperand(Inst.getOperand(0));
8570     TmpInst.addOperand(Inst.getOperand(Swap ? 1 : 2));
8571     TmpInst.addOperand(Inst.getOperand(3));
8572     TmpInst.addOperand(Inst.getOperand(4));
8573     Inst = TmpInst;
8574     return true;
8575   }
8576   case ARM::tADDrSP: {
8577     // If the non-SP source operand and the destination operand are not the
8578     // same, we need to use the 32-bit encoding if it's available.
8579     if (Inst.getOperand(0).getReg() != Inst.getOperand(2).getReg()) {
8580       Inst.setOpcode(ARM::t2ADDrr);
8581       Inst.addOperand(MCOperand::createReg(0)); // cc_out
8582       return true;
8583     }
8584     break;
8585   }
8586   case ARM::tB:
8587     // A Thumb conditional branch outside of an IT block is a tBcc.
8588     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()) {
8589       Inst.setOpcode(ARM::tBcc);
8590       return true;
8591     }
8592     break;
8593   case ARM::t2B:
8594     // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
8595     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()){
8596       Inst.setOpcode(ARM::t2Bcc);
8597       return true;
8598     }
8599     break;
8600   case ARM::t2Bcc:
8601     // If the conditional is AL or we're in an IT block, we really want t2B.
8602     if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock()) {
8603       Inst.setOpcode(ARM::t2B);
8604       return true;
8605     }
8606     break;
8607   case ARM::tBcc:
8608     // If the conditional is AL, we really want tB.
8609     if (Inst.getOperand(1).getImm() == ARMCC::AL) {
8610       Inst.setOpcode(ARM::tB);
8611       return true;
8612     }
8613     break;
8614   case ARM::tLDMIA: {
8615     // If the register list contains any high registers, or if the writeback
8616     // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
8617     // instead if we're in Thumb2. Otherwise, this should have generated
8618     // an error in validateInstruction().
8619     unsigned Rn = Inst.getOperand(0).getReg();
8620     bool hasWritebackToken =
8621         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8622          static_cast<ARMOperand &>(*Operands[3]).getToken() == "!");
8623     bool listContainsBase;
8624     if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
8625         (!listContainsBase && !hasWritebackToken) ||
8626         (listContainsBase && hasWritebackToken)) {
8627       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
8628       assert (isThumbTwo());
8629       Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
8630       // If we're switching to the updating version, we need to insert
8631       // the writeback tied operand.
8632       if (hasWritebackToken)
8633         Inst.insert(Inst.begin(),
8634                     MCOperand::createReg(Inst.getOperand(0).getReg()));
8635       return true;
8636     }
8637     break;
8638   }
8639   case ARM::tSTMIA_UPD: {
8640     // If the register list contains any high registers, we need to use
8641     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
8642     // should have generated an error in validateInstruction().
8643     unsigned Rn = Inst.getOperand(0).getReg();
8644     bool listContainsBase;
8645     if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
8646       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
8647       assert (isThumbTwo());
8648       Inst.setOpcode(ARM::t2STMIA_UPD);
8649       return true;
8650     }
8651     break;
8652   }
8653   case ARM::tPOP: {
8654     bool listContainsBase;
8655     // If the register list contains any high registers, we need to use
8656     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
8657     // should have generated an error in validateInstruction().
8658     if (!checkLowRegisterList(Inst, 2, 0, ARM::PC, listContainsBase))
8659       return false;
8660     assert (isThumbTwo());
8661     Inst.setOpcode(ARM::t2LDMIA_UPD);
8662     // Add the base register and writeback operands.
8663     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8664     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8665     return true;
8666   }
8667   case ARM::tPUSH: {
8668     bool listContainsBase;
8669     if (!checkLowRegisterList(Inst, 2, 0, ARM::LR, listContainsBase))
8670       return false;
8671     assert (isThumbTwo());
8672     Inst.setOpcode(ARM::t2STMDB_UPD);
8673     // Add the base register and writeback operands.
8674     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8675     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8676     return true;
8677   }
8678   case ARM::t2MOVi: {
8679     // If we can use the 16-bit encoding and the user didn't explicitly
8680     // request the 32-bit variant, transform it here.
8681     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8682         (unsigned)Inst.getOperand(1).getImm() <= 255 &&
8683         ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
8684           Inst.getOperand(4).getReg() == ARM::CPSR) ||
8685          (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
8686         (!static_cast<ARMOperand &>(*Operands[2]).isToken() ||
8687          static_cast<ARMOperand &>(*Operands[2]).getToken() != ".w")) {
8688       // The operands aren't in the same order for tMOVi8...
8689       MCInst TmpInst;
8690       TmpInst.setOpcode(ARM::tMOVi8);
8691       TmpInst.addOperand(Inst.getOperand(0));
8692       TmpInst.addOperand(Inst.getOperand(4));
8693       TmpInst.addOperand(Inst.getOperand(1));
8694       TmpInst.addOperand(Inst.getOperand(2));
8695       TmpInst.addOperand(Inst.getOperand(3));
8696       Inst = TmpInst;
8697       return true;
8698     }
8699     break;
8700   }
8701   case ARM::t2MOVr: {
8702     // If we can use the 16-bit encoding and the user didn't explicitly
8703     // request the 32-bit variant, transform it here.
8704     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8705         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8706         Inst.getOperand(2).getImm() == ARMCC::AL &&
8707         Inst.getOperand(4).getReg() == ARM::CPSR &&
8708         (!static_cast<ARMOperand &>(*Operands[2]).isToken() ||
8709          static_cast<ARMOperand &>(*Operands[2]).getToken() != ".w")) {
8710       // The operands aren't the same for tMOV[S]r... (no cc_out)
8711       MCInst TmpInst;
8712       TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
8713       TmpInst.addOperand(Inst.getOperand(0));
8714       TmpInst.addOperand(Inst.getOperand(1));
8715       TmpInst.addOperand(Inst.getOperand(2));
8716       TmpInst.addOperand(Inst.getOperand(3));
8717       Inst = TmpInst;
8718       return true;
8719     }
8720     break;
8721   }
8722   case ARM::t2SXTH:
8723   case ARM::t2SXTB:
8724   case ARM::t2UXTH:
8725   case ARM::t2UXTB: {
8726     // If we can use the 16-bit encoding and the user didn't explicitly
8727     // request the 32-bit variant, transform it here.
8728     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8729         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8730         Inst.getOperand(2).getImm() == 0 &&
8731         (!static_cast<ARMOperand &>(*Operands[2]).isToken() ||
8732          static_cast<ARMOperand &>(*Operands[2]).getToken() != ".w")) {
8733       unsigned NewOpc;
8734       switch (Inst.getOpcode()) {
8735       default: llvm_unreachable("Illegal opcode!");
8736       case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
8737       case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
8738       case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
8739       case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
8740       }
8741       // The operands aren't the same for thumb1 (no rotate operand).
8742       MCInst TmpInst;
8743       TmpInst.setOpcode(NewOpc);
8744       TmpInst.addOperand(Inst.getOperand(0));
8745       TmpInst.addOperand(Inst.getOperand(1));
8746       TmpInst.addOperand(Inst.getOperand(3));
8747       TmpInst.addOperand(Inst.getOperand(4));
8748       Inst = TmpInst;
8749       return true;
8750     }
8751     break;
8752   }
8753   case ARM::MOVsi: {
8754     ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(2).getImm());
8755     // rrx shifts and asr/lsr of #32 is encoded as 0
8756     if (SOpc == ARM_AM::rrx || SOpc == ARM_AM::asr || SOpc == ARM_AM::lsr)
8757       return false;
8758     if (ARM_AM::getSORegOffset(Inst.getOperand(2).getImm()) == 0) {
8759       // Shifting by zero is accepted as a vanilla 'MOVr'
8760       MCInst TmpInst;
8761       TmpInst.setOpcode(ARM::MOVr);
8762       TmpInst.addOperand(Inst.getOperand(0));
8763       TmpInst.addOperand(Inst.getOperand(1));
8764       TmpInst.addOperand(Inst.getOperand(3));
8765       TmpInst.addOperand(Inst.getOperand(4));
8766       TmpInst.addOperand(Inst.getOperand(5));
8767       Inst = TmpInst;
8768       return true;
8769     }
8770     return false;
8771   }
8772   case ARM::ANDrsi:
8773   case ARM::ORRrsi:
8774   case ARM::EORrsi:
8775   case ARM::BICrsi:
8776   case ARM::SUBrsi:
8777   case ARM::ADDrsi: {
8778     unsigned newOpc;
8779     ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(3).getImm());
8780     if (SOpc == ARM_AM::rrx) return false;
8781     switch (Inst.getOpcode()) {
8782     default: llvm_unreachable("unexpected opcode!");
8783     case ARM::ANDrsi: newOpc = ARM::ANDrr; break;
8784     case ARM::ORRrsi: newOpc = ARM::ORRrr; break;
8785     case ARM::EORrsi: newOpc = ARM::EORrr; break;
8786     case ARM::BICrsi: newOpc = ARM::BICrr; break;
8787     case ARM::SUBrsi: newOpc = ARM::SUBrr; break;
8788     case ARM::ADDrsi: newOpc = ARM::ADDrr; break;
8789     }
8790     // If the shift is by zero, use the non-shifted instruction definition.
8791     // The exception is for right shifts, where 0 == 32
8792     if (ARM_AM::getSORegOffset(Inst.getOperand(3).getImm()) == 0 &&
8793         !(SOpc == ARM_AM::lsr || SOpc == ARM_AM::asr)) {
8794       MCInst TmpInst;
8795       TmpInst.setOpcode(newOpc);
8796       TmpInst.addOperand(Inst.getOperand(0));
8797       TmpInst.addOperand(Inst.getOperand(1));
8798       TmpInst.addOperand(Inst.getOperand(2));
8799       TmpInst.addOperand(Inst.getOperand(4));
8800       TmpInst.addOperand(Inst.getOperand(5));
8801       TmpInst.addOperand(Inst.getOperand(6));
8802       Inst = TmpInst;
8803       return true;
8804     }
8805     return false;
8806   }
8807   case ARM::ITasm:
8808   case ARM::t2IT: {
8809     MCOperand &MO = Inst.getOperand(1);
8810     unsigned Mask = MO.getImm();
8811     ARMCC::CondCodes Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
8812 
8813     // Set up the IT block state according to the IT instruction we just
8814     // matched.
8815     assert(!inITBlock() && "nested IT blocks?!");
8816     startExplicitITBlock(Cond, Mask);
8817     MO.setImm(getITMaskEncoding());
8818     break;
8819   }
8820   case ARM::t2LSLrr:
8821   case ARM::t2LSRrr:
8822   case ARM::t2ASRrr:
8823   case ARM::t2SBCrr:
8824   case ARM::t2RORrr:
8825   case ARM::t2BICrr:
8826   {
8827     // Assemblers should use the narrow encodings of these instructions when permissible.
8828     if ((isARMLowRegister(Inst.getOperand(1).getReg()) &&
8829          isARMLowRegister(Inst.getOperand(2).getReg())) &&
8830         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
8831         ((!inITBlock() && Inst.getOperand(5).getReg() == ARM::CPSR) ||
8832          (inITBlock() && Inst.getOperand(5).getReg() != ARM::CPSR)) &&
8833         (!static_cast<ARMOperand &>(*Operands[3]).isToken() ||
8834          !static_cast<ARMOperand &>(*Operands[3]).getToken().equals_lower(
8835              ".w"))) {
8836       unsigned NewOpc;
8837       switch (Inst.getOpcode()) {
8838         default: llvm_unreachable("unexpected opcode");
8839         case ARM::t2LSLrr: NewOpc = ARM::tLSLrr; break;
8840         case ARM::t2LSRrr: NewOpc = ARM::tLSRrr; break;
8841         case ARM::t2ASRrr: NewOpc = ARM::tASRrr; break;
8842         case ARM::t2SBCrr: NewOpc = ARM::tSBC; break;
8843         case ARM::t2RORrr: NewOpc = ARM::tROR; break;
8844         case ARM::t2BICrr: NewOpc = ARM::tBIC; break;
8845       }
8846       MCInst TmpInst;
8847       TmpInst.setOpcode(NewOpc);
8848       TmpInst.addOperand(Inst.getOperand(0));
8849       TmpInst.addOperand(Inst.getOperand(5));
8850       TmpInst.addOperand(Inst.getOperand(1));
8851       TmpInst.addOperand(Inst.getOperand(2));
8852       TmpInst.addOperand(Inst.getOperand(3));
8853       TmpInst.addOperand(Inst.getOperand(4));
8854       Inst = TmpInst;
8855       return true;
8856     }
8857     return false;
8858   }
8859   case ARM::t2ANDrr:
8860   case ARM::t2EORrr:
8861   case ARM::t2ADCrr:
8862   case ARM::t2ORRrr:
8863   {
8864     // Assemblers should use the narrow encodings of these instructions when permissible.
8865     // These instructions are special in that they are commutable, so shorter encodings
8866     // are available more often.
8867     if ((isARMLowRegister(Inst.getOperand(1).getReg()) &&
8868          isARMLowRegister(Inst.getOperand(2).getReg())) &&
8869         (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() ||
8870          Inst.getOperand(0).getReg() == Inst.getOperand(2).getReg()) &&
8871         ((!inITBlock() && Inst.getOperand(5).getReg() == ARM::CPSR) ||
8872          (inITBlock() && Inst.getOperand(5).getReg() != ARM::CPSR)) &&
8873         (!static_cast<ARMOperand &>(*Operands[3]).isToken() ||
8874          !static_cast<ARMOperand &>(*Operands[3]).getToken().equals_lower(
8875              ".w"))) {
8876       unsigned NewOpc;
8877       switch (Inst.getOpcode()) {
8878         default: llvm_unreachable("unexpected opcode");
8879         case ARM::t2ADCrr: NewOpc = ARM::tADC; break;
8880         case ARM::t2ANDrr: NewOpc = ARM::tAND; break;
8881         case ARM::t2EORrr: NewOpc = ARM::tEOR; break;
8882         case ARM::t2ORRrr: NewOpc = ARM::tORR; break;
8883       }
8884       MCInst TmpInst;
8885       TmpInst.setOpcode(NewOpc);
8886       TmpInst.addOperand(Inst.getOperand(0));
8887       TmpInst.addOperand(Inst.getOperand(5));
8888       if (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()) {
8889         TmpInst.addOperand(Inst.getOperand(1));
8890         TmpInst.addOperand(Inst.getOperand(2));
8891       } else {
8892         TmpInst.addOperand(Inst.getOperand(2));
8893         TmpInst.addOperand(Inst.getOperand(1));
8894       }
8895       TmpInst.addOperand(Inst.getOperand(3));
8896       TmpInst.addOperand(Inst.getOperand(4));
8897       Inst = TmpInst;
8898       return true;
8899     }
8900     return false;
8901   }
8902   }
8903   return false;
8904 }
8905 
8906 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
8907   // 16-bit thumb arithmetic instructions either require or preclude the 'S'
8908   // suffix depending on whether they're in an IT block or not.
8909   unsigned Opc = Inst.getOpcode();
8910   const MCInstrDesc &MCID = MII.get(Opc);
8911   if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
8912     assert(MCID.hasOptionalDef() &&
8913            "optionally flag setting instruction missing optional def operand");
8914     assert(MCID.NumOperands == Inst.getNumOperands() &&
8915            "operand count mismatch!");
8916     // Find the optional-def operand (cc_out).
8917     unsigned OpNo;
8918     for (OpNo = 0;
8919          !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
8920          ++OpNo)
8921       ;
8922     // If we're parsing Thumb1, reject it completely.
8923     if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
8924       return Match_MnemonicFail;
8925     // If we're parsing Thumb2, which form is legal depends on whether we're
8926     // in an IT block.
8927     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
8928         !inITBlock())
8929       return Match_RequiresITBlock;
8930     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
8931         inITBlock())
8932       return Match_RequiresNotITBlock;
8933   } else if (isThumbOne()) {
8934     // Some high-register supporting Thumb1 encodings only allow both registers
8935     // to be from r0-r7 when in Thumb2.
8936     if (Opc == ARM::tADDhirr && !hasV6MOps() &&
8937         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8938         isARMLowRegister(Inst.getOperand(2).getReg()))
8939       return Match_RequiresThumb2;
8940     // Others only require ARMv6 or later.
8941     else if (Opc == ARM::tMOVr && !hasV6Ops() &&
8942              isARMLowRegister(Inst.getOperand(0).getReg()) &&
8943              isARMLowRegister(Inst.getOperand(1).getReg()))
8944       return Match_RequiresV6;
8945   }
8946 
8947   for (unsigned I = 0; I < MCID.NumOperands; ++I)
8948     if (MCID.OpInfo[I].RegClass == ARM::rGPRRegClassID) {
8949       // rGPRRegClass excludes PC, and also excluded SP before ARMv8
8950       if ((Inst.getOperand(I).getReg() == ARM::SP) && !hasV8Ops())
8951         return Match_RequiresV8;
8952       else if (Inst.getOperand(I).getReg() == ARM::PC)
8953         return Match_InvalidOperand;
8954     }
8955 
8956   return Match_Success;
8957 }
8958 
8959 namespace llvm {
8960 template <> inline bool IsCPSRDead<MCInst>(MCInst *Instr) {
8961   return true; // In an assembly source, no need to second-guess
8962 }
8963 }
8964 
8965 // Returns true if Inst is unpredictable if it is in and IT block, but is not
8966 // the last instruction in the block.
8967 bool ARMAsmParser::isITBlockTerminator(MCInst &Inst) const {
8968   const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
8969 
8970   // All branch & call instructions terminate IT blocks.
8971   if (MCID.isTerminator() || MCID.isCall() || MCID.isReturn() ||
8972       MCID.isBranch() || MCID.isIndirectBranch())
8973     return true;
8974 
8975   // Any arithmetic instruction which writes to the PC also terminates the IT
8976   // block.
8977   for (unsigned OpIdx = 0; OpIdx < MCID.getNumDefs(); ++OpIdx) {
8978     MCOperand &Op = Inst.getOperand(OpIdx);
8979     if (Op.isReg() && Op.getReg() == ARM::PC)
8980       return true;
8981   }
8982 
8983   if (MCID.hasImplicitDefOfPhysReg(ARM::PC, MRI))
8984     return true;
8985 
8986   // Instructions with variable operand lists, which write to the variable
8987   // operands. We only care about Thumb instructions here, as ARM instructions
8988   // obviously can't be in an IT block.
8989   switch (Inst.getOpcode()) {
8990   case ARM::t2LDMIA:
8991   case ARM::t2LDMIA_UPD:
8992   case ARM::t2LDMDB:
8993   case ARM::t2LDMDB_UPD:
8994     if (listContainsReg(Inst, 3, ARM::PC))
8995       return true;
8996     break;
8997   case ARM::tPOP:
8998     if (listContainsReg(Inst, 2, ARM::PC))
8999       return true;
9000     break;
9001   }
9002 
9003   return false;
9004 }
9005 
9006 unsigned ARMAsmParser::MatchInstruction(OperandVector &Operands, MCInst &Inst,
9007                                           uint64_t &ErrorInfo,
9008                                           bool MatchingInlineAsm,
9009                                           bool &EmitInITBlock,
9010                                           MCStreamer &Out) {
9011   // If we can't use an implicit IT block here, just match as normal.
9012   if (inExplicitITBlock() || !isThumbTwo() || !useImplicitITThumb())
9013     return MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
9014 
9015   // Try to match the instruction in an extension of the current IT block (if
9016   // there is one).
9017   if (inImplicitITBlock()) {
9018     extendImplicitITBlock(ITState.Cond);
9019     if (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm) ==
9020             Match_Success) {
9021       // The match succeded, but we still have to check that the instruction is
9022       // valid in this implicit IT block.
9023       const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
9024       if (MCID.isPredicable()) {
9025         ARMCC::CondCodes InstCond =
9026             (ARMCC::CondCodes)Inst.getOperand(MCID.findFirstPredOperandIdx())
9027                 .getImm();
9028         ARMCC::CondCodes ITCond = currentITCond();
9029         if (InstCond == ITCond) {
9030           EmitInITBlock = true;
9031           return Match_Success;
9032         } else if (InstCond == ARMCC::getOppositeCondition(ITCond)) {
9033           invertCurrentITCondition();
9034           EmitInITBlock = true;
9035           return Match_Success;
9036         }
9037       }
9038     }
9039     rewindImplicitITPosition();
9040   }
9041 
9042   // Finish the current IT block, and try to match outside any IT block.
9043   flushPendingInstructions(Out);
9044   unsigned PlainMatchResult =
9045       MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
9046   if (PlainMatchResult == Match_Success) {
9047     const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
9048     if (MCID.isPredicable()) {
9049       ARMCC::CondCodes InstCond =
9050           (ARMCC::CondCodes)Inst.getOperand(MCID.findFirstPredOperandIdx())
9051               .getImm();
9052       // Some forms of the branch instruction have their own condition code
9053       // fields, so can be conditionally executed without an IT block.
9054       if (Inst.getOpcode() == ARM::tBcc || Inst.getOpcode() == ARM::t2Bcc) {
9055         EmitInITBlock = false;
9056         return Match_Success;
9057       }
9058       if (InstCond == ARMCC::AL) {
9059         EmitInITBlock = false;
9060         return Match_Success;
9061       }
9062     } else {
9063       EmitInITBlock = false;
9064       return Match_Success;
9065     }
9066   }
9067 
9068   // Try to match in a new IT block. The matcher doesn't check the actual
9069   // condition, so we create an IT block with a dummy condition, and fix it up
9070   // once we know the actual condition.
9071   startImplicitITBlock();
9072   if (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm) ==
9073       Match_Success) {
9074     const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
9075     if (MCID.isPredicable()) {
9076       ITState.Cond =
9077           (ARMCC::CondCodes)Inst.getOperand(MCID.findFirstPredOperandIdx())
9078               .getImm();
9079       EmitInITBlock = true;
9080       return Match_Success;
9081     }
9082   }
9083   discardImplicitITBlock();
9084 
9085   // If none of these succeed, return the error we got when trying to match
9086   // outside any IT blocks.
9087   EmitInITBlock = false;
9088   return PlainMatchResult;
9089 }
9090 
9091 static const char *getSubtargetFeatureName(uint64_t Val);
9092 bool ARMAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
9093                                            OperandVector &Operands,
9094                                            MCStreamer &Out, uint64_t &ErrorInfo,
9095                                            bool MatchingInlineAsm) {
9096   MCInst Inst;
9097   unsigned MatchResult;
9098   bool PendConditionalInstruction = false;
9099 
9100   MatchResult = MatchInstruction(Operands, Inst, ErrorInfo, MatchingInlineAsm,
9101                                  PendConditionalInstruction, Out);
9102 
9103   switch (MatchResult) {
9104   case Match_Success:
9105     // Context sensitive operand constraints aren't handled by the matcher,
9106     // so check them here.
9107     if (validateInstruction(Inst, Operands)) {
9108       // Still progress the IT block, otherwise one wrong condition causes
9109       // nasty cascading errors.
9110       forwardITPosition();
9111       return true;
9112     }
9113 
9114     { // processInstruction() updates inITBlock state, we need to save it away
9115       bool wasInITBlock = inITBlock();
9116 
9117       // Some instructions need post-processing to, for example, tweak which
9118       // encoding is selected. Loop on it while changes happen so the
9119       // individual transformations can chain off each other. E.g.,
9120       // tPOP(r8)->t2LDMIA_UPD(sp,r8)->t2STR_POST(sp,r8)
9121       while (processInstruction(Inst, Operands, Out))
9122         ;
9123 
9124       // Only after the instruction is fully processed, we can validate it
9125       if (wasInITBlock && hasV8Ops() && isThumb() &&
9126           !isV8EligibleForIT(&Inst)) {
9127         Warning(IDLoc, "deprecated instruction in IT block");
9128       }
9129     }
9130 
9131     // Only move forward at the very end so that everything in validate
9132     // and process gets a consistent answer about whether we're in an IT
9133     // block.
9134     forwardITPosition();
9135 
9136     // ITasm is an ARM mode pseudo-instruction that just sets the ITblock and
9137     // doesn't actually encode.
9138     if (Inst.getOpcode() == ARM::ITasm)
9139       return false;
9140 
9141     Inst.setLoc(IDLoc);
9142     if (PendConditionalInstruction) {
9143       PendingConditionalInsts.push_back(Inst);
9144       if (isITBlockFull() || isITBlockTerminator(Inst))
9145         flushPendingInstructions(Out);
9146     } else {
9147       Out.EmitInstruction(Inst, getSTI());
9148     }
9149     return false;
9150   case Match_MissingFeature: {
9151     assert(ErrorInfo && "Unknown missing feature!");
9152     // Special case the error message for the very common case where only
9153     // a single subtarget feature is missing (Thumb vs. ARM, e.g.).
9154     std::string Msg = "instruction requires:";
9155     uint64_t Mask = 1;
9156     for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
9157       if (ErrorInfo & Mask) {
9158         Msg += " ";
9159         Msg += getSubtargetFeatureName(ErrorInfo & Mask);
9160       }
9161       Mask <<= 1;
9162     }
9163     return Error(IDLoc, Msg);
9164   }
9165   case Match_InvalidOperand: {
9166     SMLoc ErrorLoc = IDLoc;
9167     if (ErrorInfo != ~0ULL) {
9168       if (ErrorInfo >= Operands.size())
9169         return Error(IDLoc, "too few operands for instruction");
9170 
9171       ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
9172       if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
9173     }
9174 
9175     return Error(ErrorLoc, "invalid operand for instruction");
9176   }
9177   case Match_MnemonicFail:
9178     return Error(IDLoc, "invalid instruction",
9179                  ((ARMOperand &)*Operands[0]).getLocRange());
9180   case Match_RequiresNotITBlock:
9181     return Error(IDLoc, "flag setting instruction only valid outside IT block");
9182   case Match_RequiresITBlock:
9183     return Error(IDLoc, "instruction only valid inside IT block");
9184   case Match_RequiresV6:
9185     return Error(IDLoc, "instruction variant requires ARMv6 or later");
9186   case Match_RequiresThumb2:
9187     return Error(IDLoc, "instruction variant requires Thumb2");
9188   case Match_RequiresV8:
9189     return Error(IDLoc, "instruction variant requires ARMv8 or later");
9190   case Match_ImmRange0_15: {
9191     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
9192     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
9193     return Error(ErrorLoc, "immediate operand must be in the range [0,15]");
9194   }
9195   case Match_ImmRange0_239: {
9196     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
9197     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
9198     return Error(ErrorLoc, "immediate operand must be in the range [0,239]");
9199   }
9200   case Match_AlignedMemoryRequiresNone:
9201   case Match_DupAlignedMemoryRequiresNone:
9202   case Match_AlignedMemoryRequires16:
9203   case Match_DupAlignedMemoryRequires16:
9204   case Match_AlignedMemoryRequires32:
9205   case Match_DupAlignedMemoryRequires32:
9206   case Match_AlignedMemoryRequires64:
9207   case Match_DupAlignedMemoryRequires64:
9208   case Match_AlignedMemoryRequires64or128:
9209   case Match_DupAlignedMemoryRequires64or128:
9210   case Match_AlignedMemoryRequires64or128or256:
9211   {
9212     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getAlignmentLoc();
9213     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
9214     switch (MatchResult) {
9215       default:
9216         llvm_unreachable("Missing Match_Aligned type");
9217       case Match_AlignedMemoryRequiresNone:
9218       case Match_DupAlignedMemoryRequiresNone:
9219         return Error(ErrorLoc, "alignment must be omitted");
9220       case Match_AlignedMemoryRequires16:
9221       case Match_DupAlignedMemoryRequires16:
9222         return Error(ErrorLoc, "alignment must be 16 or omitted");
9223       case Match_AlignedMemoryRequires32:
9224       case Match_DupAlignedMemoryRequires32:
9225         return Error(ErrorLoc, "alignment must be 32 or omitted");
9226       case Match_AlignedMemoryRequires64:
9227       case Match_DupAlignedMemoryRequires64:
9228         return Error(ErrorLoc, "alignment must be 64 or omitted");
9229       case Match_AlignedMemoryRequires64or128:
9230       case Match_DupAlignedMemoryRequires64or128:
9231         return Error(ErrorLoc, "alignment must be 64, 128 or omitted");
9232       case Match_AlignedMemoryRequires64or128or256:
9233         return Error(ErrorLoc, "alignment must be 64, 128, 256 or omitted");
9234     }
9235   }
9236   }
9237 
9238   llvm_unreachable("Implement any new match types added!");
9239 }
9240 
9241 /// parseDirective parses the arm specific directives
9242 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
9243   const MCObjectFileInfo::Environment Format =
9244     getContext().getObjectFileInfo()->getObjectFileType();
9245   bool IsMachO = Format == MCObjectFileInfo::IsMachO;
9246   bool IsCOFF = Format == MCObjectFileInfo::IsCOFF;
9247 
9248   StringRef IDVal = DirectiveID.getIdentifier();
9249   if (IDVal == ".word")
9250     return parseLiteralValues(4, DirectiveID.getLoc());
9251   else if (IDVal == ".short" || IDVal == ".hword")
9252     return parseLiteralValues(2, DirectiveID.getLoc());
9253   else if (IDVal == ".thumb")
9254     return parseDirectiveThumb(DirectiveID.getLoc());
9255   else if (IDVal == ".arm")
9256     return parseDirectiveARM(DirectiveID.getLoc());
9257   else if (IDVal == ".thumb_func")
9258     return parseDirectiveThumbFunc(DirectiveID.getLoc());
9259   else if (IDVal == ".code")
9260     return parseDirectiveCode(DirectiveID.getLoc());
9261   else if (IDVal == ".syntax")
9262     return parseDirectiveSyntax(DirectiveID.getLoc());
9263   else if (IDVal == ".unreq")
9264     return parseDirectiveUnreq(DirectiveID.getLoc());
9265   else if (IDVal == ".fnend")
9266     return parseDirectiveFnEnd(DirectiveID.getLoc());
9267   else if (IDVal == ".cantunwind")
9268     return parseDirectiveCantUnwind(DirectiveID.getLoc());
9269   else if (IDVal == ".personality")
9270     return parseDirectivePersonality(DirectiveID.getLoc());
9271   else if (IDVal == ".handlerdata")
9272     return parseDirectiveHandlerData(DirectiveID.getLoc());
9273   else if (IDVal == ".setfp")
9274     return parseDirectiveSetFP(DirectiveID.getLoc());
9275   else if (IDVal == ".pad")
9276     return parseDirectivePad(DirectiveID.getLoc());
9277   else if (IDVal == ".save")
9278     return parseDirectiveRegSave(DirectiveID.getLoc(), false);
9279   else if (IDVal == ".vsave")
9280     return parseDirectiveRegSave(DirectiveID.getLoc(), true);
9281   else if (IDVal == ".ltorg" || IDVal == ".pool")
9282     return parseDirectiveLtorg(DirectiveID.getLoc());
9283   else if (IDVal == ".even")
9284     return parseDirectiveEven(DirectiveID.getLoc());
9285   else if (IDVal == ".personalityindex")
9286     return parseDirectivePersonalityIndex(DirectiveID.getLoc());
9287   else if (IDVal == ".unwind_raw")
9288     return parseDirectiveUnwindRaw(DirectiveID.getLoc());
9289   else if (IDVal == ".movsp")
9290     return parseDirectiveMovSP(DirectiveID.getLoc());
9291   else if (IDVal == ".arch_extension")
9292     return parseDirectiveArchExtension(DirectiveID.getLoc());
9293   else if (IDVal == ".align")
9294     return parseDirectiveAlign(DirectiveID.getLoc());
9295   else if (IDVal == ".thumb_set")
9296     return parseDirectiveThumbSet(DirectiveID.getLoc());
9297 
9298   if (!IsMachO && !IsCOFF) {
9299     if (IDVal == ".arch")
9300       return parseDirectiveArch(DirectiveID.getLoc());
9301     else if (IDVal == ".cpu")
9302       return parseDirectiveCPU(DirectiveID.getLoc());
9303     else if (IDVal == ".eabi_attribute")
9304       return parseDirectiveEabiAttr(DirectiveID.getLoc());
9305     else if (IDVal == ".fpu")
9306       return parseDirectiveFPU(DirectiveID.getLoc());
9307     else if (IDVal == ".fnstart")
9308       return parseDirectiveFnStart(DirectiveID.getLoc());
9309     else if (IDVal == ".inst")
9310       return parseDirectiveInst(DirectiveID.getLoc());
9311     else if (IDVal == ".inst.n")
9312       return parseDirectiveInst(DirectiveID.getLoc(), 'n');
9313     else if (IDVal == ".inst.w")
9314       return parseDirectiveInst(DirectiveID.getLoc(), 'w');
9315     else if (IDVal == ".object_arch")
9316       return parseDirectiveObjectArch(DirectiveID.getLoc());
9317     else if (IDVal == ".tlsdescseq")
9318       return parseDirectiveTLSDescSeq(DirectiveID.getLoc());
9319   }
9320 
9321   return true;
9322 }
9323 
9324 /// parseLiteralValues
9325 ///  ::= .hword expression [, expression]*
9326 ///  ::= .short expression [, expression]*
9327 ///  ::= .word expression [, expression]*
9328 bool ARMAsmParser::parseLiteralValues(unsigned Size, SMLoc L) {
9329   MCAsmParser &Parser = getParser();
9330   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9331     for (;;) {
9332       const MCExpr *Value;
9333       if (getParser().parseExpression(Value)) {
9334         Parser.eatToEndOfStatement();
9335         return false;
9336       }
9337 
9338       getParser().getStreamer().EmitValue(Value, Size, L);
9339 
9340       if (getLexer().is(AsmToken::EndOfStatement))
9341         break;
9342 
9343       // FIXME: Improve diagnostic.
9344       if (getLexer().isNot(AsmToken::Comma)) {
9345         Error(L, "unexpected token in directive");
9346         return false;
9347       }
9348       Parser.Lex();
9349     }
9350   }
9351 
9352   Parser.Lex();
9353   return false;
9354 }
9355 
9356 /// parseDirectiveThumb
9357 ///  ::= .thumb
9358 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
9359   MCAsmParser &Parser = getParser();
9360   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9361     Error(L, "unexpected token in directive");
9362     return false;
9363   }
9364   Parser.Lex();
9365 
9366   if (!hasThumb()) {
9367     Error(L, "target does not support Thumb mode");
9368     return false;
9369   }
9370 
9371   if (!isThumb())
9372     SwitchMode();
9373 
9374   getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
9375   return false;
9376 }
9377 
9378 /// parseDirectiveARM
9379 ///  ::= .arm
9380 bool ARMAsmParser::parseDirectiveARM(SMLoc L) {
9381   MCAsmParser &Parser = getParser();
9382   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9383     Error(L, "unexpected token in directive");
9384     return false;
9385   }
9386   Parser.Lex();
9387 
9388   if (!hasARM()) {
9389     Error(L, "target does not support ARM mode");
9390     return false;
9391   }
9392 
9393   if (isThumb())
9394     SwitchMode();
9395 
9396   getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
9397   return false;
9398 }
9399 
9400 void ARMAsmParser::onLabelParsed(MCSymbol *Symbol) {
9401   // We need to flush the current implicit IT block on a label, because it is
9402   // not legal to branch into an IT block.
9403   flushPendingInstructions(getStreamer());
9404   if (NextSymbolIsThumb) {
9405     getParser().getStreamer().EmitThumbFunc(Symbol);
9406     NextSymbolIsThumb = false;
9407   }
9408 }
9409 
9410 /// parseDirectiveThumbFunc
9411 ///  ::= .thumbfunc symbol_name
9412 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
9413   MCAsmParser &Parser = getParser();
9414   const auto Format = getContext().getObjectFileInfo()->getObjectFileType();
9415   bool IsMachO = Format == MCObjectFileInfo::IsMachO;
9416 
9417   // Darwin asm has (optionally) function name after .thumb_func direction
9418   // ELF doesn't
9419   if (IsMachO) {
9420     const AsmToken &Tok = Parser.getTok();
9421     if (Tok.isNot(AsmToken::EndOfStatement)) {
9422       if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String)) {
9423         Error(L, "unexpected token in .thumb_func directive");
9424         return false;
9425       }
9426 
9427       MCSymbol *Func =
9428           getParser().getContext().getOrCreateSymbol(Tok.getIdentifier());
9429       getParser().getStreamer().EmitThumbFunc(Func);
9430       Parser.Lex(); // Consume the identifier token.
9431       return false;
9432     }
9433   }
9434 
9435   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9436     Error(Parser.getTok().getLoc(), "unexpected token in directive");
9437     Parser.eatToEndOfStatement();
9438     return false;
9439   }
9440 
9441   NextSymbolIsThumb = true;
9442   return false;
9443 }
9444 
9445 /// parseDirectiveSyntax
9446 ///  ::= .syntax unified | divided
9447 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
9448   MCAsmParser &Parser = getParser();
9449   const AsmToken &Tok = Parser.getTok();
9450   if (Tok.isNot(AsmToken::Identifier)) {
9451     Error(L, "unexpected token in .syntax directive");
9452     return false;
9453   }
9454 
9455   StringRef Mode = Tok.getString();
9456   if (Mode == "unified" || Mode == "UNIFIED") {
9457     Parser.Lex();
9458   } else if (Mode == "divided" || Mode == "DIVIDED") {
9459     Error(L, "'.syntax divided' arm asssembly not supported");
9460     return false;
9461   } else {
9462     Error(L, "unrecognized syntax mode in .syntax directive");
9463     return false;
9464   }
9465 
9466   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9467     Error(Parser.getTok().getLoc(), "unexpected token in directive");
9468     return false;
9469   }
9470   Parser.Lex();
9471 
9472   // TODO tell the MC streamer the mode
9473   // getParser().getStreamer().Emit???();
9474   return false;
9475 }
9476 
9477 /// parseDirectiveCode
9478 ///  ::= .code 16 | 32
9479 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
9480   MCAsmParser &Parser = getParser();
9481   const AsmToken &Tok = Parser.getTok();
9482   if (Tok.isNot(AsmToken::Integer)) {
9483     Error(L, "unexpected token in .code directive");
9484     return false;
9485   }
9486   int64_t Val = Parser.getTok().getIntVal();
9487   if (Val != 16 && Val != 32) {
9488     Error(L, "invalid operand to .code directive");
9489     return false;
9490   }
9491   Parser.Lex();
9492 
9493   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9494     Error(Parser.getTok().getLoc(), "unexpected token in directive");
9495     return false;
9496   }
9497   Parser.Lex();
9498 
9499   if (Val == 16) {
9500     if (!hasThumb()) {
9501       Error(L, "target does not support Thumb mode");
9502       return false;
9503     }
9504 
9505     if (!isThumb())
9506       SwitchMode();
9507     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
9508   } else {
9509     if (!hasARM()) {
9510       Error(L, "target does not support ARM mode");
9511       return false;
9512     }
9513 
9514     if (isThumb())
9515       SwitchMode();
9516     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
9517   }
9518 
9519   return false;
9520 }
9521 
9522 /// parseDirectiveReq
9523 ///  ::= name .req registername
9524 bool ARMAsmParser::parseDirectiveReq(StringRef Name, SMLoc L) {
9525   MCAsmParser &Parser = getParser();
9526   Parser.Lex(); // Eat the '.req' token.
9527   unsigned Reg;
9528   SMLoc SRegLoc, ERegLoc;
9529   if (ParseRegister(Reg, SRegLoc, ERegLoc)) {
9530     Parser.eatToEndOfStatement();
9531     Error(SRegLoc, "register name expected");
9532     return false;
9533   }
9534 
9535   // Shouldn't be anything else.
9536   if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
9537     Parser.eatToEndOfStatement();
9538     Error(Parser.getTok().getLoc(), "unexpected input in .req directive.");
9539     return false;
9540   }
9541 
9542   Parser.Lex(); // Consume the EndOfStatement
9543 
9544   if (RegisterReqs.insert(std::make_pair(Name, Reg)).first->second != Reg) {
9545     Error(SRegLoc, "redefinition of '" + Name + "' does not match original.");
9546     return false;
9547   }
9548 
9549   return false;
9550 }
9551 
9552 /// parseDirectiveUneq
9553 ///  ::= .unreq registername
9554 bool ARMAsmParser::parseDirectiveUnreq(SMLoc L) {
9555   MCAsmParser &Parser = getParser();
9556   if (Parser.getTok().isNot(AsmToken::Identifier)) {
9557     Parser.eatToEndOfStatement();
9558     Error(L, "unexpected input in .unreq directive.");
9559     return false;
9560   }
9561   RegisterReqs.erase(Parser.getTok().getIdentifier().lower());
9562   Parser.Lex(); // Eat the identifier.
9563   return false;
9564 }
9565 
9566 // After changing arch/CPU, try to put the ARM/Thumb mode back to what it was
9567 // before, if supported by the new target, or emit mapping symbols for the mode
9568 // switch.
9569 void ARMAsmParser::FixModeAfterArchChange(bool WasThumb, SMLoc Loc) {
9570   if (WasThumb != isThumb()) {
9571     if (WasThumb && hasThumb()) {
9572       // Stay in Thumb mode
9573       SwitchMode();
9574     } else if (!WasThumb && hasARM()) {
9575       // Stay in ARM mode
9576       SwitchMode();
9577     } else {
9578       // Mode switch forced, because the new arch doesn't support the old mode.
9579       getParser().getStreamer().EmitAssemblerFlag(isThumb() ? MCAF_Code16
9580                                                             : MCAF_Code32);
9581       // Warn about the implcit mode switch. GAS does not switch modes here,
9582       // but instead stays in the old mode, reporting an error on any following
9583       // instructions as the mode does not exist on the target.
9584       Warning(Loc, Twine("new target does not support ") +
9585                        (WasThumb ? "thumb" : "arm") + " mode, switching to " +
9586                        (!WasThumb ? "thumb" : "arm") + " mode");
9587     }
9588   }
9589 }
9590 
9591 /// parseDirectiveArch
9592 ///  ::= .arch token
9593 bool ARMAsmParser::parseDirectiveArch(SMLoc L) {
9594   StringRef Arch = getParser().parseStringToEndOfStatement().trim();
9595 
9596   unsigned ID = ARM::parseArch(Arch);
9597 
9598   if (ID == ARM::AK_INVALID) {
9599     Error(L, "Unknown arch name");
9600     return false;
9601   }
9602 
9603   bool WasThumb = isThumb();
9604   Triple T;
9605   MCSubtargetInfo &STI = copySTI();
9606   STI.setDefaultFeatures("", ("+" + ARM::getArchName(ID)).str());
9607   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9608   FixModeAfterArchChange(WasThumb, L);
9609 
9610   getTargetStreamer().emitArch(ID);
9611   return false;
9612 }
9613 
9614 /// parseDirectiveEabiAttr
9615 ///  ::= .eabi_attribute int, int [, "str"]
9616 ///  ::= .eabi_attribute Tag_name, int [, "str"]
9617 bool ARMAsmParser::parseDirectiveEabiAttr(SMLoc L) {
9618   MCAsmParser &Parser = getParser();
9619   int64_t Tag;
9620   SMLoc TagLoc;
9621   TagLoc = Parser.getTok().getLoc();
9622   if (Parser.getTok().is(AsmToken::Identifier)) {
9623     StringRef Name = Parser.getTok().getIdentifier();
9624     Tag = ARMBuildAttrs::AttrTypeFromString(Name);
9625     if (Tag == -1) {
9626       Error(TagLoc, "attribute name not recognised: " + Name);
9627       Parser.eatToEndOfStatement();
9628       return false;
9629     }
9630     Parser.Lex();
9631   } else {
9632     const MCExpr *AttrExpr;
9633 
9634     TagLoc = Parser.getTok().getLoc();
9635     if (Parser.parseExpression(AttrExpr)) {
9636       Parser.eatToEndOfStatement();
9637       return false;
9638     }
9639 
9640     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(AttrExpr);
9641     if (!CE) {
9642       Error(TagLoc, "expected numeric constant");
9643       Parser.eatToEndOfStatement();
9644       return false;
9645     }
9646 
9647     Tag = CE->getValue();
9648   }
9649 
9650   if (Parser.getTok().isNot(AsmToken::Comma)) {
9651     Error(Parser.getTok().getLoc(), "comma expected");
9652     Parser.eatToEndOfStatement();
9653     return false;
9654   }
9655   Parser.Lex(); // skip comma
9656 
9657   StringRef StringValue = "";
9658   bool IsStringValue = false;
9659 
9660   int64_t IntegerValue = 0;
9661   bool IsIntegerValue = false;
9662 
9663   if (Tag == ARMBuildAttrs::CPU_raw_name || Tag == ARMBuildAttrs::CPU_name)
9664     IsStringValue = true;
9665   else if (Tag == ARMBuildAttrs::compatibility) {
9666     IsStringValue = true;
9667     IsIntegerValue = true;
9668   } else if (Tag < 32 || Tag % 2 == 0)
9669     IsIntegerValue = true;
9670   else if (Tag % 2 == 1)
9671     IsStringValue = true;
9672   else
9673     llvm_unreachable("invalid tag type");
9674 
9675   if (IsIntegerValue) {
9676     const MCExpr *ValueExpr;
9677     SMLoc ValueExprLoc = Parser.getTok().getLoc();
9678     if (Parser.parseExpression(ValueExpr)) {
9679       Parser.eatToEndOfStatement();
9680       return false;
9681     }
9682 
9683     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ValueExpr);
9684     if (!CE) {
9685       Error(ValueExprLoc, "expected numeric constant");
9686       Parser.eatToEndOfStatement();
9687       return false;
9688     }
9689 
9690     IntegerValue = CE->getValue();
9691   }
9692 
9693   if (Tag == ARMBuildAttrs::compatibility) {
9694     if (Parser.getTok().isNot(AsmToken::Comma))
9695       IsStringValue = false;
9696     if (Parser.getTok().isNot(AsmToken::Comma)) {
9697       Error(Parser.getTok().getLoc(), "comma expected");
9698       Parser.eatToEndOfStatement();
9699       return false;
9700     } else {
9701        Parser.Lex();
9702     }
9703   }
9704 
9705   if (IsStringValue) {
9706     if (Parser.getTok().isNot(AsmToken::String)) {
9707       Error(Parser.getTok().getLoc(), "bad string constant");
9708       Parser.eatToEndOfStatement();
9709       return false;
9710     }
9711 
9712     StringValue = Parser.getTok().getStringContents();
9713     Parser.Lex();
9714   }
9715 
9716   if (IsIntegerValue && IsStringValue) {
9717     assert(Tag == ARMBuildAttrs::compatibility);
9718     getTargetStreamer().emitIntTextAttribute(Tag, IntegerValue, StringValue);
9719   } else if (IsIntegerValue)
9720     getTargetStreamer().emitAttribute(Tag, IntegerValue);
9721   else if (IsStringValue)
9722     getTargetStreamer().emitTextAttribute(Tag, StringValue);
9723   return false;
9724 }
9725 
9726 /// parseDirectiveCPU
9727 ///  ::= .cpu str
9728 bool ARMAsmParser::parseDirectiveCPU(SMLoc L) {
9729   StringRef CPU = getParser().parseStringToEndOfStatement().trim();
9730   getTargetStreamer().emitTextAttribute(ARMBuildAttrs::CPU_name, CPU);
9731 
9732   // FIXME: This is using table-gen data, but should be moved to
9733   // ARMTargetParser once that is table-gen'd.
9734   if (!getSTI().isCPUStringValid(CPU)) {
9735     Error(L, "Unknown CPU name");
9736     return false;
9737   }
9738 
9739   bool WasThumb = isThumb();
9740   MCSubtargetInfo &STI = copySTI();
9741   STI.setDefaultFeatures(CPU, "");
9742   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9743   FixModeAfterArchChange(WasThumb, L);
9744 
9745   return false;
9746 }
9747 /// parseDirectiveFPU
9748 ///  ::= .fpu str
9749 bool ARMAsmParser::parseDirectiveFPU(SMLoc L) {
9750   SMLoc FPUNameLoc = getTok().getLoc();
9751   StringRef FPU = getParser().parseStringToEndOfStatement().trim();
9752 
9753   unsigned ID = ARM::parseFPU(FPU);
9754   std::vector<const char *> Features;
9755   if (!ARM::getFPUFeatures(ID, Features)) {
9756     Error(FPUNameLoc, "Unknown FPU name");
9757     return false;
9758   }
9759 
9760   MCSubtargetInfo &STI = copySTI();
9761   for (auto Feature : Features)
9762     STI.ApplyFeatureFlag(Feature);
9763   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9764 
9765   getTargetStreamer().emitFPU(ID);
9766   return false;
9767 }
9768 
9769 /// parseDirectiveFnStart
9770 ///  ::= .fnstart
9771 bool ARMAsmParser::parseDirectiveFnStart(SMLoc L) {
9772   if (UC.hasFnStart()) {
9773     Error(L, ".fnstart starts before the end of previous one");
9774     UC.emitFnStartLocNotes();
9775     return false;
9776   }
9777 
9778   // Reset the unwind directives parser state
9779   UC.reset();
9780 
9781   getTargetStreamer().emitFnStart();
9782 
9783   UC.recordFnStart(L);
9784   return false;
9785 }
9786 
9787 /// parseDirectiveFnEnd
9788 ///  ::= .fnend
9789 bool ARMAsmParser::parseDirectiveFnEnd(SMLoc L) {
9790   // Check the ordering of unwind directives
9791   if (!UC.hasFnStart()) {
9792     Error(L, ".fnstart must precede .fnend directive");
9793     return false;
9794   }
9795 
9796   // Reset the unwind directives parser state
9797   getTargetStreamer().emitFnEnd();
9798 
9799   UC.reset();
9800   return false;
9801 }
9802 
9803 /// parseDirectiveCantUnwind
9804 ///  ::= .cantunwind
9805 bool ARMAsmParser::parseDirectiveCantUnwind(SMLoc L) {
9806   UC.recordCantUnwind(L);
9807 
9808   // Check the ordering of unwind directives
9809   if (!UC.hasFnStart()) {
9810     Error(L, ".fnstart must precede .cantunwind directive");
9811     return false;
9812   }
9813   if (UC.hasHandlerData()) {
9814     Error(L, ".cantunwind can't be used with .handlerdata directive");
9815     UC.emitHandlerDataLocNotes();
9816     return false;
9817   }
9818   if (UC.hasPersonality()) {
9819     Error(L, ".cantunwind can't be used with .personality directive");
9820     UC.emitPersonalityLocNotes();
9821     return false;
9822   }
9823 
9824   getTargetStreamer().emitCantUnwind();
9825   return false;
9826 }
9827 
9828 /// parseDirectivePersonality
9829 ///  ::= .personality name
9830 bool ARMAsmParser::parseDirectivePersonality(SMLoc L) {
9831   MCAsmParser &Parser = getParser();
9832   bool HasExistingPersonality = UC.hasPersonality();
9833 
9834   UC.recordPersonality(L);
9835 
9836   // Check the ordering of unwind directives
9837   if (!UC.hasFnStart()) {
9838     Error(L, ".fnstart must precede .personality directive");
9839     return false;
9840   }
9841   if (UC.cantUnwind()) {
9842     Error(L, ".personality can't be used with .cantunwind directive");
9843     UC.emitCantUnwindLocNotes();
9844     return false;
9845   }
9846   if (UC.hasHandlerData()) {
9847     Error(L, ".personality must precede .handlerdata directive");
9848     UC.emitHandlerDataLocNotes();
9849     return false;
9850   }
9851   if (HasExistingPersonality) {
9852     Parser.eatToEndOfStatement();
9853     Error(L, "multiple personality directives");
9854     UC.emitPersonalityLocNotes();
9855     return false;
9856   }
9857 
9858   // Parse the name of the personality routine
9859   if (Parser.getTok().isNot(AsmToken::Identifier)) {
9860     Parser.eatToEndOfStatement();
9861     Error(L, "unexpected input in .personality directive.");
9862     return false;
9863   }
9864   StringRef Name(Parser.getTok().getIdentifier());
9865   Parser.Lex();
9866 
9867   MCSymbol *PR = getParser().getContext().getOrCreateSymbol(Name);
9868   getTargetStreamer().emitPersonality(PR);
9869   return false;
9870 }
9871 
9872 /// parseDirectiveHandlerData
9873 ///  ::= .handlerdata
9874 bool ARMAsmParser::parseDirectiveHandlerData(SMLoc L) {
9875   UC.recordHandlerData(L);
9876 
9877   // Check the ordering of unwind directives
9878   if (!UC.hasFnStart()) {
9879     Error(L, ".fnstart must precede .personality directive");
9880     return false;
9881   }
9882   if (UC.cantUnwind()) {
9883     Error(L, ".handlerdata can't be used with .cantunwind directive");
9884     UC.emitCantUnwindLocNotes();
9885     return false;
9886   }
9887 
9888   getTargetStreamer().emitHandlerData();
9889   return false;
9890 }
9891 
9892 /// parseDirectiveSetFP
9893 ///  ::= .setfp fpreg, spreg [, offset]
9894 bool ARMAsmParser::parseDirectiveSetFP(SMLoc L) {
9895   MCAsmParser &Parser = getParser();
9896   // Check the ordering of unwind directives
9897   if (!UC.hasFnStart()) {
9898     Error(L, ".fnstart must precede .setfp directive");
9899     return false;
9900   }
9901   if (UC.hasHandlerData()) {
9902     Error(L, ".setfp must precede .handlerdata directive");
9903     return false;
9904   }
9905 
9906   // Parse fpreg
9907   SMLoc FPRegLoc = Parser.getTok().getLoc();
9908   int FPReg = tryParseRegister();
9909   if (FPReg == -1) {
9910     Error(FPRegLoc, "frame pointer register expected");
9911     return false;
9912   }
9913 
9914   // Consume comma
9915   if (Parser.getTok().isNot(AsmToken::Comma)) {
9916     Error(Parser.getTok().getLoc(), "comma expected");
9917     return false;
9918   }
9919   Parser.Lex(); // skip comma
9920 
9921   // Parse spreg
9922   SMLoc SPRegLoc = Parser.getTok().getLoc();
9923   int SPReg = tryParseRegister();
9924   if (SPReg == -1) {
9925     Error(SPRegLoc, "stack pointer register expected");
9926     return false;
9927   }
9928 
9929   if (SPReg != ARM::SP && SPReg != UC.getFPReg()) {
9930     Error(SPRegLoc, "register should be either $sp or the latest fp register");
9931     return false;
9932   }
9933 
9934   // Update the frame pointer register
9935   UC.saveFPReg(FPReg);
9936 
9937   // Parse offset
9938   int64_t Offset = 0;
9939   if (Parser.getTok().is(AsmToken::Comma)) {
9940     Parser.Lex(); // skip comma
9941 
9942     if (Parser.getTok().isNot(AsmToken::Hash) &&
9943         Parser.getTok().isNot(AsmToken::Dollar)) {
9944       Error(Parser.getTok().getLoc(), "'#' expected");
9945       return false;
9946     }
9947     Parser.Lex(); // skip hash token.
9948 
9949     const MCExpr *OffsetExpr;
9950     SMLoc ExLoc = Parser.getTok().getLoc();
9951     SMLoc EndLoc;
9952     if (getParser().parseExpression(OffsetExpr, EndLoc)) {
9953       Error(ExLoc, "malformed setfp offset");
9954       return false;
9955     }
9956     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9957     if (!CE) {
9958       Error(ExLoc, "setfp offset must be an immediate");
9959       return false;
9960     }
9961 
9962     Offset = CE->getValue();
9963   }
9964 
9965   getTargetStreamer().emitSetFP(static_cast<unsigned>(FPReg),
9966                                 static_cast<unsigned>(SPReg), Offset);
9967   return false;
9968 }
9969 
9970 /// parseDirective
9971 ///  ::= .pad offset
9972 bool ARMAsmParser::parseDirectivePad(SMLoc L) {
9973   MCAsmParser &Parser = getParser();
9974   // Check the ordering of unwind directives
9975   if (!UC.hasFnStart()) {
9976     Error(L, ".fnstart must precede .pad directive");
9977     return false;
9978   }
9979   if (UC.hasHandlerData()) {
9980     Error(L, ".pad must precede .handlerdata directive");
9981     return false;
9982   }
9983 
9984   // Parse the offset
9985   if (Parser.getTok().isNot(AsmToken::Hash) &&
9986       Parser.getTok().isNot(AsmToken::Dollar)) {
9987     Error(Parser.getTok().getLoc(), "'#' expected");
9988     return false;
9989   }
9990   Parser.Lex(); // skip hash token.
9991 
9992   const MCExpr *OffsetExpr;
9993   SMLoc ExLoc = Parser.getTok().getLoc();
9994   SMLoc EndLoc;
9995   if (getParser().parseExpression(OffsetExpr, EndLoc)) {
9996     Error(ExLoc, "malformed pad offset");
9997     return false;
9998   }
9999   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
10000   if (!CE) {
10001     Error(ExLoc, "pad offset must be an immediate");
10002     return false;
10003   }
10004 
10005   getTargetStreamer().emitPad(CE->getValue());
10006   return false;
10007 }
10008 
10009 /// parseDirectiveRegSave
10010 ///  ::= .save  { registers }
10011 ///  ::= .vsave { registers }
10012 bool ARMAsmParser::parseDirectiveRegSave(SMLoc L, bool IsVector) {
10013   // Check the ordering of unwind directives
10014   if (!UC.hasFnStart()) {
10015     Error(L, ".fnstart must precede .save or .vsave directives");
10016     return false;
10017   }
10018   if (UC.hasHandlerData()) {
10019     Error(L, ".save or .vsave must precede .handlerdata directive");
10020     return false;
10021   }
10022 
10023   // RAII object to make sure parsed operands are deleted.
10024   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Operands;
10025 
10026   // Parse the register list
10027   if (parseRegisterList(Operands))
10028     return false;
10029   ARMOperand &Op = (ARMOperand &)*Operands[0];
10030   if (!IsVector && !Op.isRegList()) {
10031     Error(L, ".save expects GPR registers");
10032     return false;
10033   }
10034   if (IsVector && !Op.isDPRRegList()) {
10035     Error(L, ".vsave expects DPR registers");
10036     return false;
10037   }
10038 
10039   getTargetStreamer().emitRegSave(Op.getRegList(), IsVector);
10040   return false;
10041 }
10042 
10043 /// parseDirectiveInst
10044 ///  ::= .inst opcode [, ...]
10045 ///  ::= .inst.n opcode [, ...]
10046 ///  ::= .inst.w opcode [, ...]
10047 bool ARMAsmParser::parseDirectiveInst(SMLoc Loc, char Suffix) {
10048   MCAsmParser &Parser = getParser();
10049   int Width;
10050 
10051   if (isThumb()) {
10052     switch (Suffix) {
10053     case 'n':
10054       Width = 2;
10055       break;
10056     case 'w':
10057       Width = 4;
10058       break;
10059     default:
10060       Parser.eatToEndOfStatement();
10061       Error(Loc, "cannot determine Thumb instruction size, "
10062                  "use inst.n/inst.w instead");
10063       return false;
10064     }
10065   } else {
10066     if (Suffix) {
10067       Parser.eatToEndOfStatement();
10068       Error(Loc, "width suffixes are invalid in ARM mode");
10069       return false;
10070     }
10071     Width = 4;
10072   }
10073 
10074   if (getLexer().is(AsmToken::EndOfStatement)) {
10075     Parser.eatToEndOfStatement();
10076     Error(Loc, "expected expression following directive");
10077     return false;
10078   }
10079 
10080   for (;;) {
10081     const MCExpr *Expr;
10082 
10083     if (getParser().parseExpression(Expr)) {
10084       Error(Loc, "expected expression");
10085       return false;
10086     }
10087 
10088     const MCConstantExpr *Value = dyn_cast_or_null<MCConstantExpr>(Expr);
10089     if (!Value) {
10090       Error(Loc, "expected constant expression");
10091       return false;
10092     }
10093 
10094     switch (Width) {
10095     case 2:
10096       if (Value->getValue() > 0xffff) {
10097         Error(Loc, "inst.n operand is too big, use inst.w instead");
10098         return false;
10099       }
10100       break;
10101     case 4:
10102       if (Value->getValue() > 0xffffffff) {
10103         Error(Loc,
10104               StringRef(Suffix ? "inst.w" : "inst") + " operand is too big");
10105         return false;
10106       }
10107       break;
10108     default:
10109       llvm_unreachable("only supported widths are 2 and 4");
10110     }
10111 
10112     getTargetStreamer().emitInst(Value->getValue(), Suffix);
10113 
10114     if (getLexer().is(AsmToken::EndOfStatement))
10115       break;
10116 
10117     if (getLexer().isNot(AsmToken::Comma)) {
10118       Error(Loc, "unexpected token in directive");
10119       return false;
10120     }
10121 
10122     Parser.Lex();
10123   }
10124 
10125   Parser.Lex();
10126   return false;
10127 }
10128 
10129 /// parseDirectiveLtorg
10130 ///  ::= .ltorg | .pool
10131 bool ARMAsmParser::parseDirectiveLtorg(SMLoc L) {
10132   getTargetStreamer().emitCurrentConstantPool();
10133   return false;
10134 }
10135 
10136 bool ARMAsmParser::parseDirectiveEven(SMLoc L) {
10137   const MCSection *Section = getStreamer().getCurrentSection().first;
10138 
10139   if (getLexer().isNot(AsmToken::EndOfStatement)) {
10140     TokError("unexpected token in directive");
10141     return false;
10142   }
10143 
10144   if (!Section) {
10145     getStreamer().InitSections(false);
10146     Section = getStreamer().getCurrentSection().first;
10147   }
10148 
10149   assert(Section && "must have section to emit alignment");
10150   if (Section->UseCodeAlign())
10151     getStreamer().EmitCodeAlignment(2);
10152   else
10153     getStreamer().EmitValueToAlignment(2);
10154 
10155   return false;
10156 }
10157 
10158 /// parseDirectivePersonalityIndex
10159 ///   ::= .personalityindex index
10160 bool ARMAsmParser::parseDirectivePersonalityIndex(SMLoc L) {
10161   MCAsmParser &Parser = getParser();
10162   bool HasExistingPersonality = UC.hasPersonality();
10163 
10164   UC.recordPersonalityIndex(L);
10165 
10166   if (!UC.hasFnStart()) {
10167     Parser.eatToEndOfStatement();
10168     Error(L, ".fnstart must precede .personalityindex directive");
10169     return false;
10170   }
10171   if (UC.cantUnwind()) {
10172     Parser.eatToEndOfStatement();
10173     Error(L, ".personalityindex cannot be used with .cantunwind");
10174     UC.emitCantUnwindLocNotes();
10175     return false;
10176   }
10177   if (UC.hasHandlerData()) {
10178     Parser.eatToEndOfStatement();
10179     Error(L, ".personalityindex must precede .handlerdata directive");
10180     UC.emitHandlerDataLocNotes();
10181     return false;
10182   }
10183   if (HasExistingPersonality) {
10184     Parser.eatToEndOfStatement();
10185     Error(L, "multiple personality directives");
10186     UC.emitPersonalityLocNotes();
10187     return false;
10188   }
10189 
10190   const MCExpr *IndexExpression;
10191   SMLoc IndexLoc = Parser.getTok().getLoc();
10192   if (Parser.parseExpression(IndexExpression)) {
10193     Parser.eatToEndOfStatement();
10194     return false;
10195   }
10196 
10197   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(IndexExpression);
10198   if (!CE) {
10199     Parser.eatToEndOfStatement();
10200     Error(IndexLoc, "index must be a constant number");
10201     return false;
10202   }
10203   if (CE->getValue() < 0 ||
10204       CE->getValue() >= ARM::EHABI::NUM_PERSONALITY_INDEX) {
10205     Parser.eatToEndOfStatement();
10206     Error(IndexLoc, "personality routine index should be in range [0-3]");
10207     return false;
10208   }
10209 
10210   getTargetStreamer().emitPersonalityIndex(CE->getValue());
10211   return false;
10212 }
10213 
10214 /// parseDirectiveUnwindRaw
10215 ///   ::= .unwind_raw offset, opcode [, opcode...]
10216 bool ARMAsmParser::parseDirectiveUnwindRaw(SMLoc L) {
10217   MCAsmParser &Parser = getParser();
10218   if (!UC.hasFnStart()) {
10219     Parser.eatToEndOfStatement();
10220     Error(L, ".fnstart must precede .unwind_raw directives");
10221     return false;
10222   }
10223 
10224   int64_t StackOffset;
10225 
10226   const MCExpr *OffsetExpr;
10227   SMLoc OffsetLoc = getLexer().getLoc();
10228   if (getLexer().is(AsmToken::EndOfStatement) ||
10229       getParser().parseExpression(OffsetExpr)) {
10230     Error(OffsetLoc, "expected expression");
10231     Parser.eatToEndOfStatement();
10232     return false;
10233   }
10234 
10235   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
10236   if (!CE) {
10237     Error(OffsetLoc, "offset must be a constant");
10238     Parser.eatToEndOfStatement();
10239     return false;
10240   }
10241 
10242   StackOffset = CE->getValue();
10243 
10244   if (getLexer().isNot(AsmToken::Comma)) {
10245     Error(getLexer().getLoc(), "expected comma");
10246     Parser.eatToEndOfStatement();
10247     return false;
10248   }
10249   Parser.Lex();
10250 
10251   SmallVector<uint8_t, 16> Opcodes;
10252   for (;;) {
10253     const MCExpr *OE;
10254 
10255     SMLoc OpcodeLoc = getLexer().getLoc();
10256     if (getLexer().is(AsmToken::EndOfStatement) || Parser.parseExpression(OE)) {
10257       Error(OpcodeLoc, "expected opcode expression");
10258       Parser.eatToEndOfStatement();
10259       return false;
10260     }
10261 
10262     const MCConstantExpr *OC = dyn_cast<MCConstantExpr>(OE);
10263     if (!OC) {
10264       Error(OpcodeLoc, "opcode value must be a constant");
10265       Parser.eatToEndOfStatement();
10266       return false;
10267     }
10268 
10269     const int64_t Opcode = OC->getValue();
10270     if (Opcode & ~0xff) {
10271       Error(OpcodeLoc, "invalid opcode");
10272       Parser.eatToEndOfStatement();
10273       return false;
10274     }
10275 
10276     Opcodes.push_back(uint8_t(Opcode));
10277 
10278     if (getLexer().is(AsmToken::EndOfStatement))
10279       break;
10280 
10281     if (getLexer().isNot(AsmToken::Comma)) {
10282       Error(getLexer().getLoc(), "unexpected token in directive");
10283       Parser.eatToEndOfStatement();
10284       return false;
10285     }
10286 
10287     Parser.Lex();
10288   }
10289 
10290   getTargetStreamer().emitUnwindRaw(StackOffset, Opcodes);
10291 
10292   Parser.Lex();
10293   return false;
10294 }
10295 
10296 /// parseDirectiveTLSDescSeq
10297 ///   ::= .tlsdescseq tls-variable
10298 bool ARMAsmParser::parseDirectiveTLSDescSeq(SMLoc L) {
10299   MCAsmParser &Parser = getParser();
10300 
10301   if (getLexer().isNot(AsmToken::Identifier)) {
10302     TokError("expected variable after '.tlsdescseq' directive");
10303     Parser.eatToEndOfStatement();
10304     return false;
10305   }
10306 
10307   const MCSymbolRefExpr *SRE =
10308     MCSymbolRefExpr::create(Parser.getTok().getIdentifier(),
10309                             MCSymbolRefExpr::VK_ARM_TLSDESCSEQ, getContext());
10310   Lex();
10311 
10312   if (getLexer().isNot(AsmToken::EndOfStatement)) {
10313     Error(Parser.getTok().getLoc(), "unexpected token");
10314     Parser.eatToEndOfStatement();
10315     return false;
10316   }
10317 
10318   getTargetStreamer().AnnotateTLSDescriptorSequence(SRE);
10319   return false;
10320 }
10321 
10322 /// parseDirectiveMovSP
10323 ///  ::= .movsp reg [, #offset]
10324 bool ARMAsmParser::parseDirectiveMovSP(SMLoc L) {
10325   MCAsmParser &Parser = getParser();
10326   if (!UC.hasFnStart()) {
10327     Parser.eatToEndOfStatement();
10328     Error(L, ".fnstart must precede .movsp directives");
10329     return false;
10330   }
10331   if (UC.getFPReg() != ARM::SP) {
10332     Parser.eatToEndOfStatement();
10333     Error(L, "unexpected .movsp directive");
10334     return false;
10335   }
10336 
10337   SMLoc SPRegLoc = Parser.getTok().getLoc();
10338   int SPReg = tryParseRegister();
10339   if (SPReg == -1) {
10340     Parser.eatToEndOfStatement();
10341     Error(SPRegLoc, "register expected");
10342     return false;
10343   }
10344 
10345   if (SPReg == ARM::SP || SPReg == ARM::PC) {
10346     Parser.eatToEndOfStatement();
10347     Error(SPRegLoc, "sp and pc are not permitted in .movsp directive");
10348     return false;
10349   }
10350 
10351   int64_t Offset = 0;
10352   if (Parser.getTok().is(AsmToken::Comma)) {
10353     Parser.Lex();
10354 
10355     if (Parser.getTok().isNot(AsmToken::Hash)) {
10356       Error(Parser.getTok().getLoc(), "expected #constant");
10357       Parser.eatToEndOfStatement();
10358       return false;
10359     }
10360     Parser.Lex();
10361 
10362     const MCExpr *OffsetExpr;
10363     SMLoc OffsetLoc = Parser.getTok().getLoc();
10364     if (Parser.parseExpression(OffsetExpr)) {
10365       Parser.eatToEndOfStatement();
10366       Error(OffsetLoc, "malformed offset expression");
10367       return false;
10368     }
10369 
10370     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
10371     if (!CE) {
10372       Parser.eatToEndOfStatement();
10373       Error(OffsetLoc, "offset must be an immediate constant");
10374       return false;
10375     }
10376 
10377     Offset = CE->getValue();
10378   }
10379 
10380   getTargetStreamer().emitMovSP(SPReg, Offset);
10381   UC.saveFPReg(SPReg);
10382 
10383   return false;
10384 }
10385 
10386 /// parseDirectiveObjectArch
10387 ///   ::= .object_arch name
10388 bool ARMAsmParser::parseDirectiveObjectArch(SMLoc L) {
10389   MCAsmParser &Parser = getParser();
10390   if (getLexer().isNot(AsmToken::Identifier)) {
10391     Error(getLexer().getLoc(), "unexpected token");
10392     Parser.eatToEndOfStatement();
10393     return false;
10394   }
10395 
10396   StringRef Arch = Parser.getTok().getString();
10397   SMLoc ArchLoc = Parser.getTok().getLoc();
10398   Lex();
10399 
10400   unsigned ID = ARM::parseArch(Arch);
10401 
10402   if (ID == ARM::AK_INVALID) {
10403     Error(ArchLoc, "unknown architecture '" + Arch + "'");
10404     Parser.eatToEndOfStatement();
10405     return false;
10406   }
10407 
10408   getTargetStreamer().emitObjectArch(ID);
10409 
10410   if (getLexer().isNot(AsmToken::EndOfStatement)) {
10411     Error(getLexer().getLoc(), "unexpected token");
10412     Parser.eatToEndOfStatement();
10413   }
10414 
10415   return false;
10416 }
10417 
10418 /// parseDirectiveAlign
10419 ///   ::= .align
10420 bool ARMAsmParser::parseDirectiveAlign(SMLoc L) {
10421   // NOTE: if this is not the end of the statement, fall back to the target
10422   // agnostic handling for this directive which will correctly handle this.
10423   if (getLexer().isNot(AsmToken::EndOfStatement))
10424     return true;
10425 
10426   // '.align' is target specifically handled to mean 2**2 byte alignment.
10427   const MCSection *Section = getStreamer().getCurrentSection().first;
10428   assert(Section && "must have section to emit alignment");
10429   if (Section->UseCodeAlign())
10430     getStreamer().EmitCodeAlignment(4, 0);
10431   else
10432     getStreamer().EmitValueToAlignment(4, 0, 1, 0);
10433 
10434   return false;
10435 }
10436 
10437 /// parseDirectiveThumbSet
10438 ///  ::= .thumb_set name, value
10439 bool ARMAsmParser::parseDirectiveThumbSet(SMLoc L) {
10440   MCAsmParser &Parser = getParser();
10441 
10442   StringRef Name;
10443   if (Parser.parseIdentifier(Name)) {
10444     TokError("expected identifier after '.thumb_set'");
10445     Parser.eatToEndOfStatement();
10446     return false;
10447   }
10448 
10449   if (getLexer().isNot(AsmToken::Comma)) {
10450     TokError("expected comma after name '" + Name + "'");
10451     Parser.eatToEndOfStatement();
10452     return false;
10453   }
10454   Lex();
10455 
10456   MCSymbol *Sym;
10457   const MCExpr *Value;
10458   if (MCParserUtils::parseAssignmentExpression(Name, /* allow_redef */ true,
10459                                                Parser, Sym, Value))
10460     return true;
10461 
10462   getTargetStreamer().emitThumbSet(Sym, Value);
10463   return false;
10464 }
10465 
10466 /// Force static initialization.
10467 extern "C" void LLVMInitializeARMAsmParser() {
10468   RegisterMCAsmParser<ARMAsmParser> X(TheARMLETarget);
10469   RegisterMCAsmParser<ARMAsmParser> Y(TheARMBETarget);
10470   RegisterMCAsmParser<ARMAsmParser> A(TheThumbLETarget);
10471   RegisterMCAsmParser<ARMAsmParser> B(TheThumbBETarget);
10472 }
10473 
10474 #define GET_REGISTER_MATCHER
10475 #define GET_SUBTARGET_FEATURE_NAME
10476 #define GET_MATCHER_IMPLEMENTATION
10477 #include "ARMGenAsmMatcher.inc"
10478 
10479 // FIXME: This structure should be moved inside ARMTargetParser
10480 // when we start to table-generate them, and we can use the ARM
10481 // flags below, that were generated by table-gen.
10482 static const struct {
10483   const unsigned Kind;
10484   const uint64_t ArchCheck;
10485   const FeatureBitset Features;
10486 } Extensions[] = {
10487   { ARM::AEK_CRC, Feature_HasV8, {ARM::FeatureCRC} },
10488   { ARM::AEK_CRYPTO,  Feature_HasV8,
10489     {ARM::FeatureCrypto, ARM::FeatureNEON, ARM::FeatureFPARMv8} },
10490   { ARM::AEK_FP, Feature_HasV8, {ARM::FeatureFPARMv8} },
10491   { (ARM::AEK_HWDIV | ARM::AEK_HWDIVARM), Feature_HasV7 | Feature_IsNotMClass,
10492     {ARM::FeatureHWDiv, ARM::FeatureHWDivARM} },
10493   { ARM::AEK_MP, Feature_HasV7 | Feature_IsNotMClass, {ARM::FeatureMP} },
10494   { ARM::AEK_SIMD, Feature_HasV8, {ARM::FeatureNEON, ARM::FeatureFPARMv8} },
10495   { ARM::AEK_SEC, Feature_HasV6K, {ARM::FeatureTrustZone} },
10496   // FIXME: Only available in A-class, isel not predicated
10497   { ARM::AEK_VIRT, Feature_HasV7, {ARM::FeatureVirtualization} },
10498   { ARM::AEK_FP16, Feature_HasV8_2a, {ARM::FeatureFPARMv8, ARM::FeatureFullFP16} },
10499   { ARM::AEK_RAS, Feature_HasV8, {ARM::FeatureRAS} },
10500   // FIXME: Unsupported extensions.
10501   { ARM::AEK_OS, Feature_None, {} },
10502   { ARM::AEK_IWMMXT, Feature_None, {} },
10503   { ARM::AEK_IWMMXT2, Feature_None, {} },
10504   { ARM::AEK_MAVERICK, Feature_None, {} },
10505   { ARM::AEK_XSCALE, Feature_None, {} },
10506 };
10507 
10508 /// parseDirectiveArchExtension
10509 ///   ::= .arch_extension [no]feature
10510 bool ARMAsmParser::parseDirectiveArchExtension(SMLoc L) {
10511   MCAsmParser &Parser = getParser();
10512 
10513   if (getLexer().isNot(AsmToken::Identifier)) {
10514     Error(getLexer().getLoc(), "expected architecture extension name");
10515     Parser.eatToEndOfStatement();
10516     return false;
10517   }
10518 
10519   StringRef Name = Parser.getTok().getString();
10520   SMLoc ExtLoc = Parser.getTok().getLoc();
10521   Lex();
10522 
10523   bool EnableFeature = true;
10524   if (Name.startswith_lower("no")) {
10525     EnableFeature = false;
10526     Name = Name.substr(2);
10527   }
10528   unsigned FeatureKind = ARM::parseArchExt(Name);
10529   if (FeatureKind == ARM::AEK_INVALID) {
10530     Error(ExtLoc, "unknown architectural extension: " + Name);
10531     return false;
10532   }
10533 
10534   for (const auto &Extension : Extensions) {
10535     if (Extension.Kind != FeatureKind)
10536       continue;
10537 
10538     if (Extension.Features.none()) {
10539       Error(ExtLoc, "unsupported architectural extension: " + Name);
10540       return false;
10541     }
10542 
10543     if ((getAvailableFeatures() & Extension.ArchCheck) != Extension.ArchCheck) {
10544       Error(ExtLoc, "architectural extension '" + Name + "' is not "
10545             "allowed for the current base architecture");
10546       return false;
10547     }
10548 
10549     MCSubtargetInfo &STI = copySTI();
10550     FeatureBitset ToggleFeatures = EnableFeature
10551       ? (~STI.getFeatureBits() & Extension.Features)
10552       : ( STI.getFeatureBits() & Extension.Features);
10553 
10554     uint64_t Features =
10555         ComputeAvailableFeatures(STI.ToggleFeature(ToggleFeatures));
10556     setAvailableFeatures(Features);
10557     return false;
10558   }
10559 
10560   Error(ExtLoc, "unknown architectural extension: " + Name);
10561   Parser.eatToEndOfStatement();
10562   return false;
10563 }
10564 
10565 // Define this matcher function after the auto-generated include so we
10566 // have the match class enum definitions.
10567 unsigned ARMAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
10568                                                   unsigned Kind) {
10569   ARMOperand &Op = static_cast<ARMOperand &>(AsmOp);
10570   // If the kind is a token for a literal immediate, check if our asm
10571   // operand matches. This is for InstAliases which have a fixed-value
10572   // immediate in the syntax.
10573   switch (Kind) {
10574   default: break;
10575   case MCK__35_0:
10576     if (Op.isImm())
10577       if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm()))
10578         if (CE->getValue() == 0)
10579           return Match_Success;
10580     break;
10581   case MCK_ModImm:
10582     if (Op.isImm()) {
10583       const MCExpr *SOExpr = Op.getImm();
10584       int64_t Value;
10585       if (!SOExpr->evaluateAsAbsolute(Value))
10586         return Match_Success;
10587       assert((Value >= INT32_MIN && Value <= UINT32_MAX) &&
10588              "expression value must be representable in 32 bits");
10589     }
10590     break;
10591   case MCK_rGPR:
10592     if (hasV8Ops() && Op.isReg() && Op.getReg() == ARM::SP)
10593       return Match_Success;
10594     break;
10595   case MCK_GPRPair:
10596     if (Op.isReg() &&
10597         MRI->getRegClass(ARM::GPRRegClassID).contains(Op.getReg()))
10598       return Match_Success;
10599     break;
10600   }
10601   return Match_InvalidOperand;
10602 }
10603