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     // 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::MOVi16:
6688   case ARM::t2MOVi16:
6689   case ARM::t2MOVTi16:
6690     {
6691     // We want to avoid misleadingly allowing something like "mov r0, <symbol>"
6692     // especially when we turn it into a movw and the expression <symbol> does
6693     // not have a :lower16: or :upper16 as part of the expression.  We don't
6694     // want the behavior of silently truncating, which can be unexpected and
6695     // lead to bugs that are difficult to find since this is an easy mistake
6696     // to make.
6697     int i = (Operands[3]->isImm()) ? 3 : 4;
6698     ARMOperand &Op = static_cast<ARMOperand &>(*Operands[i]);
6699     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm());
6700     if (CE) break;
6701     const MCExpr *E = dyn_cast<MCExpr>(Op.getImm());
6702     if (!E) break;
6703     const ARMMCExpr *ARM16Expr = dyn_cast<ARMMCExpr>(E);
6704     if (!ARM16Expr || (ARM16Expr->getKind() != ARMMCExpr::VK_ARM_HI16 &&
6705                        ARM16Expr->getKind() != ARMMCExpr::VK_ARM_LO16))
6706       return Error(
6707           Op.getStartLoc(),
6708           "immediate expression for mov requires :lower16: or :upper16");
6709     break;
6710   }
6711   case ARM::HINT:
6712   case ARM::t2HINT: {
6713     if (hasRAS()) {
6714       // ESB is not predicable (pred must be AL)
6715       unsigned Imm8 = Inst.getOperand(0).getImm();
6716       unsigned Pred = Inst.getOperand(1).getImm();
6717       if (Imm8 == 0x10 && Pred != ARMCC::AL)
6718         return Error(Operands[1]->getStartLoc(), "instruction 'esb' is not "
6719                                                  "predicable, but condition "
6720                                                  "code specified");
6721     }
6722     // Without the RAS extension, this behaves as any other unallocated hint.
6723     break;
6724   }
6725   }
6726 
6727   return false;
6728 }
6729 
6730 static unsigned getRealVSTOpcode(unsigned Opc, unsigned &Spacing) {
6731   switch(Opc) {
6732   default: llvm_unreachable("unexpected opcode!");
6733   // VST1LN
6734   case ARM::VST1LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST1LNd8_UPD;
6735   case ARM::VST1LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST1LNd16_UPD;
6736   case ARM::VST1LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST1LNd32_UPD;
6737   case ARM::VST1LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST1LNd8_UPD;
6738   case ARM::VST1LNdWB_register_Asm_16: Spacing = 1; return ARM::VST1LNd16_UPD;
6739   case ARM::VST1LNdWB_register_Asm_32: Spacing = 1; return ARM::VST1LNd32_UPD;
6740   case ARM::VST1LNdAsm_8:  Spacing = 1; return ARM::VST1LNd8;
6741   case ARM::VST1LNdAsm_16: Spacing = 1; return ARM::VST1LNd16;
6742   case ARM::VST1LNdAsm_32: Spacing = 1; return ARM::VST1LNd32;
6743 
6744   // VST2LN
6745   case ARM::VST2LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST2LNd8_UPD;
6746   case ARM::VST2LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST2LNd16_UPD;
6747   case ARM::VST2LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST2LNd32_UPD;
6748   case ARM::VST2LNqWB_fixed_Asm_16: Spacing = 2; return ARM::VST2LNq16_UPD;
6749   case ARM::VST2LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST2LNq32_UPD;
6750 
6751   case ARM::VST2LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST2LNd8_UPD;
6752   case ARM::VST2LNdWB_register_Asm_16: Spacing = 1; return ARM::VST2LNd16_UPD;
6753   case ARM::VST2LNdWB_register_Asm_32: Spacing = 1; return ARM::VST2LNd32_UPD;
6754   case ARM::VST2LNqWB_register_Asm_16: Spacing = 2; return ARM::VST2LNq16_UPD;
6755   case ARM::VST2LNqWB_register_Asm_32: Spacing = 2; return ARM::VST2LNq32_UPD;
6756 
6757   case ARM::VST2LNdAsm_8:  Spacing = 1; return ARM::VST2LNd8;
6758   case ARM::VST2LNdAsm_16: Spacing = 1; return ARM::VST2LNd16;
6759   case ARM::VST2LNdAsm_32: Spacing = 1; return ARM::VST2LNd32;
6760   case ARM::VST2LNqAsm_16: Spacing = 2; return ARM::VST2LNq16;
6761   case ARM::VST2LNqAsm_32: Spacing = 2; return ARM::VST2LNq32;
6762 
6763   // VST3LN
6764   case ARM::VST3LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST3LNd8_UPD;
6765   case ARM::VST3LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST3LNd16_UPD;
6766   case ARM::VST3LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST3LNd32_UPD;
6767   case ARM::VST3LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VST3LNq16_UPD;
6768   case ARM::VST3LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST3LNq32_UPD;
6769   case ARM::VST3LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST3LNd8_UPD;
6770   case ARM::VST3LNdWB_register_Asm_16: Spacing = 1; return ARM::VST3LNd16_UPD;
6771   case ARM::VST3LNdWB_register_Asm_32: Spacing = 1; return ARM::VST3LNd32_UPD;
6772   case ARM::VST3LNqWB_register_Asm_16: Spacing = 2; return ARM::VST3LNq16_UPD;
6773   case ARM::VST3LNqWB_register_Asm_32: Spacing = 2; return ARM::VST3LNq32_UPD;
6774   case ARM::VST3LNdAsm_8:  Spacing = 1; return ARM::VST3LNd8;
6775   case ARM::VST3LNdAsm_16: Spacing = 1; return ARM::VST3LNd16;
6776   case ARM::VST3LNdAsm_32: Spacing = 1; return ARM::VST3LNd32;
6777   case ARM::VST3LNqAsm_16: Spacing = 2; return ARM::VST3LNq16;
6778   case ARM::VST3LNqAsm_32: Spacing = 2; return ARM::VST3LNq32;
6779 
6780   // VST3
6781   case ARM::VST3dWB_fixed_Asm_8:  Spacing = 1; return ARM::VST3d8_UPD;
6782   case ARM::VST3dWB_fixed_Asm_16: Spacing = 1; return ARM::VST3d16_UPD;
6783   case ARM::VST3dWB_fixed_Asm_32: Spacing = 1; return ARM::VST3d32_UPD;
6784   case ARM::VST3qWB_fixed_Asm_8:  Spacing = 2; return ARM::VST3q8_UPD;
6785   case ARM::VST3qWB_fixed_Asm_16: Spacing = 2; return ARM::VST3q16_UPD;
6786   case ARM::VST3qWB_fixed_Asm_32: Spacing = 2; return ARM::VST3q32_UPD;
6787   case ARM::VST3dWB_register_Asm_8:  Spacing = 1; return ARM::VST3d8_UPD;
6788   case ARM::VST3dWB_register_Asm_16: Spacing = 1; return ARM::VST3d16_UPD;
6789   case ARM::VST3dWB_register_Asm_32: Spacing = 1; return ARM::VST3d32_UPD;
6790   case ARM::VST3qWB_register_Asm_8:  Spacing = 2; return ARM::VST3q8_UPD;
6791   case ARM::VST3qWB_register_Asm_16: Spacing = 2; return ARM::VST3q16_UPD;
6792   case ARM::VST3qWB_register_Asm_32: Spacing = 2; return ARM::VST3q32_UPD;
6793   case ARM::VST3dAsm_8:  Spacing = 1; return ARM::VST3d8;
6794   case ARM::VST3dAsm_16: Spacing = 1; return ARM::VST3d16;
6795   case ARM::VST3dAsm_32: Spacing = 1; return ARM::VST3d32;
6796   case ARM::VST3qAsm_8:  Spacing = 2; return ARM::VST3q8;
6797   case ARM::VST3qAsm_16: Spacing = 2; return ARM::VST3q16;
6798   case ARM::VST3qAsm_32: Spacing = 2; return ARM::VST3q32;
6799 
6800   // VST4LN
6801   case ARM::VST4LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VST4LNd8_UPD;
6802   case ARM::VST4LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VST4LNd16_UPD;
6803   case ARM::VST4LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VST4LNd32_UPD;
6804   case ARM::VST4LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VST4LNq16_UPD;
6805   case ARM::VST4LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VST4LNq32_UPD;
6806   case ARM::VST4LNdWB_register_Asm_8:  Spacing = 1; return ARM::VST4LNd8_UPD;
6807   case ARM::VST4LNdWB_register_Asm_16: Spacing = 1; return ARM::VST4LNd16_UPD;
6808   case ARM::VST4LNdWB_register_Asm_32: Spacing = 1; return ARM::VST4LNd32_UPD;
6809   case ARM::VST4LNqWB_register_Asm_16: Spacing = 2; return ARM::VST4LNq16_UPD;
6810   case ARM::VST4LNqWB_register_Asm_32: Spacing = 2; return ARM::VST4LNq32_UPD;
6811   case ARM::VST4LNdAsm_8:  Spacing = 1; return ARM::VST4LNd8;
6812   case ARM::VST4LNdAsm_16: Spacing = 1; return ARM::VST4LNd16;
6813   case ARM::VST4LNdAsm_32: Spacing = 1; return ARM::VST4LNd32;
6814   case ARM::VST4LNqAsm_16: Spacing = 2; return ARM::VST4LNq16;
6815   case ARM::VST4LNqAsm_32: Spacing = 2; return ARM::VST4LNq32;
6816 
6817   // VST4
6818   case ARM::VST4dWB_fixed_Asm_8:  Spacing = 1; return ARM::VST4d8_UPD;
6819   case ARM::VST4dWB_fixed_Asm_16: Spacing = 1; return ARM::VST4d16_UPD;
6820   case ARM::VST4dWB_fixed_Asm_32: Spacing = 1; return ARM::VST4d32_UPD;
6821   case ARM::VST4qWB_fixed_Asm_8:  Spacing = 2; return ARM::VST4q8_UPD;
6822   case ARM::VST4qWB_fixed_Asm_16: Spacing = 2; return ARM::VST4q16_UPD;
6823   case ARM::VST4qWB_fixed_Asm_32: Spacing = 2; return ARM::VST4q32_UPD;
6824   case ARM::VST4dWB_register_Asm_8:  Spacing = 1; return ARM::VST4d8_UPD;
6825   case ARM::VST4dWB_register_Asm_16: Spacing = 1; return ARM::VST4d16_UPD;
6826   case ARM::VST4dWB_register_Asm_32: Spacing = 1; return ARM::VST4d32_UPD;
6827   case ARM::VST4qWB_register_Asm_8:  Spacing = 2; return ARM::VST4q8_UPD;
6828   case ARM::VST4qWB_register_Asm_16: Spacing = 2; return ARM::VST4q16_UPD;
6829   case ARM::VST4qWB_register_Asm_32: Spacing = 2; return ARM::VST4q32_UPD;
6830   case ARM::VST4dAsm_8:  Spacing = 1; return ARM::VST4d8;
6831   case ARM::VST4dAsm_16: Spacing = 1; return ARM::VST4d16;
6832   case ARM::VST4dAsm_32: Spacing = 1; return ARM::VST4d32;
6833   case ARM::VST4qAsm_8:  Spacing = 2; return ARM::VST4q8;
6834   case ARM::VST4qAsm_16: Spacing = 2; return ARM::VST4q16;
6835   case ARM::VST4qAsm_32: Spacing = 2; return ARM::VST4q32;
6836   }
6837 }
6838 
6839 static unsigned getRealVLDOpcode(unsigned Opc, unsigned &Spacing) {
6840   switch(Opc) {
6841   default: llvm_unreachable("unexpected opcode!");
6842   // VLD1LN
6843   case ARM::VLD1LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD1LNd8_UPD;
6844   case ARM::VLD1LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD1LNd16_UPD;
6845   case ARM::VLD1LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD1LNd32_UPD;
6846   case ARM::VLD1LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD1LNd8_UPD;
6847   case ARM::VLD1LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD1LNd16_UPD;
6848   case ARM::VLD1LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD1LNd32_UPD;
6849   case ARM::VLD1LNdAsm_8:  Spacing = 1; return ARM::VLD1LNd8;
6850   case ARM::VLD1LNdAsm_16: Spacing = 1; return ARM::VLD1LNd16;
6851   case ARM::VLD1LNdAsm_32: Spacing = 1; return ARM::VLD1LNd32;
6852 
6853   // VLD2LN
6854   case ARM::VLD2LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD2LNd8_UPD;
6855   case ARM::VLD2LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD2LNd16_UPD;
6856   case ARM::VLD2LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD2LNd32_UPD;
6857   case ARM::VLD2LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD2LNq16_UPD;
6858   case ARM::VLD2LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD2LNq32_UPD;
6859   case ARM::VLD2LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD2LNd8_UPD;
6860   case ARM::VLD2LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD2LNd16_UPD;
6861   case ARM::VLD2LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD2LNd32_UPD;
6862   case ARM::VLD2LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD2LNq16_UPD;
6863   case ARM::VLD2LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD2LNq32_UPD;
6864   case ARM::VLD2LNdAsm_8:  Spacing = 1; return ARM::VLD2LNd8;
6865   case ARM::VLD2LNdAsm_16: Spacing = 1; return ARM::VLD2LNd16;
6866   case ARM::VLD2LNdAsm_32: Spacing = 1; return ARM::VLD2LNd32;
6867   case ARM::VLD2LNqAsm_16: Spacing = 2; return ARM::VLD2LNq16;
6868   case ARM::VLD2LNqAsm_32: Spacing = 2; return ARM::VLD2LNq32;
6869 
6870   // VLD3DUP
6871   case ARM::VLD3DUPdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3DUPd8_UPD;
6872   case ARM::VLD3DUPdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3DUPd16_UPD;
6873   case ARM::VLD3DUPdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3DUPd32_UPD;
6874   case ARM::VLD3DUPqWB_fixed_Asm_8: Spacing = 1; return ARM::VLD3DUPq8_UPD;
6875   case ARM::VLD3DUPqWB_fixed_Asm_16: Spacing = 2; return ARM::VLD3DUPq16_UPD;
6876   case ARM::VLD3DUPqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3DUPq32_UPD;
6877   case ARM::VLD3DUPdWB_register_Asm_8:  Spacing = 1; return ARM::VLD3DUPd8_UPD;
6878   case ARM::VLD3DUPdWB_register_Asm_16: Spacing = 1; return ARM::VLD3DUPd16_UPD;
6879   case ARM::VLD3DUPdWB_register_Asm_32: Spacing = 1; return ARM::VLD3DUPd32_UPD;
6880   case ARM::VLD3DUPqWB_register_Asm_8: Spacing = 2; return ARM::VLD3DUPq8_UPD;
6881   case ARM::VLD3DUPqWB_register_Asm_16: Spacing = 2; return ARM::VLD3DUPq16_UPD;
6882   case ARM::VLD3DUPqWB_register_Asm_32: Spacing = 2; return ARM::VLD3DUPq32_UPD;
6883   case ARM::VLD3DUPdAsm_8:  Spacing = 1; return ARM::VLD3DUPd8;
6884   case ARM::VLD3DUPdAsm_16: Spacing = 1; return ARM::VLD3DUPd16;
6885   case ARM::VLD3DUPdAsm_32: Spacing = 1; return ARM::VLD3DUPd32;
6886   case ARM::VLD3DUPqAsm_8: Spacing = 2; return ARM::VLD3DUPq8;
6887   case ARM::VLD3DUPqAsm_16: Spacing = 2; return ARM::VLD3DUPq16;
6888   case ARM::VLD3DUPqAsm_32: Spacing = 2; return ARM::VLD3DUPq32;
6889 
6890   // VLD3LN
6891   case ARM::VLD3LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3LNd8_UPD;
6892   case ARM::VLD3LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3LNd16_UPD;
6893   case ARM::VLD3LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3LNd32_UPD;
6894   case ARM::VLD3LNqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3LNq16_UPD;
6895   case ARM::VLD3LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3LNq32_UPD;
6896   case ARM::VLD3LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD3LNd8_UPD;
6897   case ARM::VLD3LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD3LNd16_UPD;
6898   case ARM::VLD3LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD3LNd32_UPD;
6899   case ARM::VLD3LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD3LNq16_UPD;
6900   case ARM::VLD3LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD3LNq32_UPD;
6901   case ARM::VLD3LNdAsm_8:  Spacing = 1; return ARM::VLD3LNd8;
6902   case ARM::VLD3LNdAsm_16: Spacing = 1; return ARM::VLD3LNd16;
6903   case ARM::VLD3LNdAsm_32: Spacing = 1; return ARM::VLD3LNd32;
6904   case ARM::VLD3LNqAsm_16: Spacing = 2; return ARM::VLD3LNq16;
6905   case ARM::VLD3LNqAsm_32: Spacing = 2; return ARM::VLD3LNq32;
6906 
6907   // VLD3
6908   case ARM::VLD3dWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD3d8_UPD;
6909   case ARM::VLD3dWB_fixed_Asm_16: Spacing = 1; return ARM::VLD3d16_UPD;
6910   case ARM::VLD3dWB_fixed_Asm_32: Spacing = 1; return ARM::VLD3d32_UPD;
6911   case ARM::VLD3qWB_fixed_Asm_8:  Spacing = 2; return ARM::VLD3q8_UPD;
6912   case ARM::VLD3qWB_fixed_Asm_16: Spacing = 2; return ARM::VLD3q16_UPD;
6913   case ARM::VLD3qWB_fixed_Asm_32: Spacing = 2; return ARM::VLD3q32_UPD;
6914   case ARM::VLD3dWB_register_Asm_8:  Spacing = 1; return ARM::VLD3d8_UPD;
6915   case ARM::VLD3dWB_register_Asm_16: Spacing = 1; return ARM::VLD3d16_UPD;
6916   case ARM::VLD3dWB_register_Asm_32: Spacing = 1; return ARM::VLD3d32_UPD;
6917   case ARM::VLD3qWB_register_Asm_8:  Spacing = 2; return ARM::VLD3q8_UPD;
6918   case ARM::VLD3qWB_register_Asm_16: Spacing = 2; return ARM::VLD3q16_UPD;
6919   case ARM::VLD3qWB_register_Asm_32: Spacing = 2; return ARM::VLD3q32_UPD;
6920   case ARM::VLD3dAsm_8:  Spacing = 1; return ARM::VLD3d8;
6921   case ARM::VLD3dAsm_16: Spacing = 1; return ARM::VLD3d16;
6922   case ARM::VLD3dAsm_32: Spacing = 1; return ARM::VLD3d32;
6923   case ARM::VLD3qAsm_8:  Spacing = 2; return ARM::VLD3q8;
6924   case ARM::VLD3qAsm_16: Spacing = 2; return ARM::VLD3q16;
6925   case ARM::VLD3qAsm_32: Spacing = 2; return ARM::VLD3q32;
6926 
6927   // VLD4LN
6928   case ARM::VLD4LNdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4LNd8_UPD;
6929   case ARM::VLD4LNdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4LNd16_UPD;
6930   case ARM::VLD4LNdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4LNd32_UPD;
6931   case ARM::VLD4LNqWB_fixed_Asm_16: Spacing = 2; return ARM::VLD4LNq16_UPD;
6932   case ARM::VLD4LNqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4LNq32_UPD;
6933   case ARM::VLD4LNdWB_register_Asm_8:  Spacing = 1; return ARM::VLD4LNd8_UPD;
6934   case ARM::VLD4LNdWB_register_Asm_16: Spacing = 1; return ARM::VLD4LNd16_UPD;
6935   case ARM::VLD4LNdWB_register_Asm_32: Spacing = 1; return ARM::VLD4LNd32_UPD;
6936   case ARM::VLD4LNqWB_register_Asm_16: Spacing = 2; return ARM::VLD4LNq16_UPD;
6937   case ARM::VLD4LNqWB_register_Asm_32: Spacing = 2; return ARM::VLD4LNq32_UPD;
6938   case ARM::VLD4LNdAsm_8:  Spacing = 1; return ARM::VLD4LNd8;
6939   case ARM::VLD4LNdAsm_16: Spacing = 1; return ARM::VLD4LNd16;
6940   case ARM::VLD4LNdAsm_32: Spacing = 1; return ARM::VLD4LNd32;
6941   case ARM::VLD4LNqAsm_16: Spacing = 2; return ARM::VLD4LNq16;
6942   case ARM::VLD4LNqAsm_32: Spacing = 2; return ARM::VLD4LNq32;
6943 
6944   // VLD4DUP
6945   case ARM::VLD4DUPdWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4DUPd8_UPD;
6946   case ARM::VLD4DUPdWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4DUPd16_UPD;
6947   case ARM::VLD4DUPdWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4DUPd32_UPD;
6948   case ARM::VLD4DUPqWB_fixed_Asm_8: Spacing = 1; return ARM::VLD4DUPq8_UPD;
6949   case ARM::VLD4DUPqWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4DUPq16_UPD;
6950   case ARM::VLD4DUPqWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4DUPq32_UPD;
6951   case ARM::VLD4DUPdWB_register_Asm_8:  Spacing = 1; return ARM::VLD4DUPd8_UPD;
6952   case ARM::VLD4DUPdWB_register_Asm_16: Spacing = 1; return ARM::VLD4DUPd16_UPD;
6953   case ARM::VLD4DUPdWB_register_Asm_32: Spacing = 1; return ARM::VLD4DUPd32_UPD;
6954   case ARM::VLD4DUPqWB_register_Asm_8: Spacing = 2; return ARM::VLD4DUPq8_UPD;
6955   case ARM::VLD4DUPqWB_register_Asm_16: Spacing = 2; return ARM::VLD4DUPq16_UPD;
6956   case ARM::VLD4DUPqWB_register_Asm_32: Spacing = 2; return ARM::VLD4DUPq32_UPD;
6957   case ARM::VLD4DUPdAsm_8:  Spacing = 1; return ARM::VLD4DUPd8;
6958   case ARM::VLD4DUPdAsm_16: Spacing = 1; return ARM::VLD4DUPd16;
6959   case ARM::VLD4DUPdAsm_32: Spacing = 1; return ARM::VLD4DUPd32;
6960   case ARM::VLD4DUPqAsm_8: Spacing = 2; return ARM::VLD4DUPq8;
6961   case ARM::VLD4DUPqAsm_16: Spacing = 2; return ARM::VLD4DUPq16;
6962   case ARM::VLD4DUPqAsm_32: Spacing = 2; return ARM::VLD4DUPq32;
6963 
6964   // VLD4
6965   case ARM::VLD4dWB_fixed_Asm_8:  Spacing = 1; return ARM::VLD4d8_UPD;
6966   case ARM::VLD4dWB_fixed_Asm_16: Spacing = 1; return ARM::VLD4d16_UPD;
6967   case ARM::VLD4dWB_fixed_Asm_32: Spacing = 1; return ARM::VLD4d32_UPD;
6968   case ARM::VLD4qWB_fixed_Asm_8:  Spacing = 2; return ARM::VLD4q8_UPD;
6969   case ARM::VLD4qWB_fixed_Asm_16: Spacing = 2; return ARM::VLD4q16_UPD;
6970   case ARM::VLD4qWB_fixed_Asm_32: Spacing = 2; return ARM::VLD4q32_UPD;
6971   case ARM::VLD4dWB_register_Asm_8:  Spacing = 1; return ARM::VLD4d8_UPD;
6972   case ARM::VLD4dWB_register_Asm_16: Spacing = 1; return ARM::VLD4d16_UPD;
6973   case ARM::VLD4dWB_register_Asm_32: Spacing = 1; return ARM::VLD4d32_UPD;
6974   case ARM::VLD4qWB_register_Asm_8:  Spacing = 2; return ARM::VLD4q8_UPD;
6975   case ARM::VLD4qWB_register_Asm_16: Spacing = 2; return ARM::VLD4q16_UPD;
6976   case ARM::VLD4qWB_register_Asm_32: Spacing = 2; return ARM::VLD4q32_UPD;
6977   case ARM::VLD4dAsm_8:  Spacing = 1; return ARM::VLD4d8;
6978   case ARM::VLD4dAsm_16: Spacing = 1; return ARM::VLD4d16;
6979   case ARM::VLD4dAsm_32: Spacing = 1; return ARM::VLD4d32;
6980   case ARM::VLD4qAsm_8:  Spacing = 2; return ARM::VLD4q8;
6981   case ARM::VLD4qAsm_16: Spacing = 2; return ARM::VLD4q16;
6982   case ARM::VLD4qAsm_32: Spacing = 2; return ARM::VLD4q32;
6983   }
6984 }
6985 
6986 bool ARMAsmParser::processInstruction(MCInst &Inst,
6987                                       const OperandVector &Operands,
6988                                       MCStreamer &Out) {
6989   switch (Inst.getOpcode()) {
6990   // Alias for alternate form of 'ldr{,b}t Rt, [Rn], #imm' instruction.
6991   case ARM::LDRT_POST:
6992   case ARM::LDRBT_POST: {
6993     const unsigned Opcode =
6994       (Inst.getOpcode() == ARM::LDRT_POST) ? ARM::LDRT_POST_IMM
6995                                            : ARM::LDRBT_POST_IMM;
6996     MCInst TmpInst;
6997     TmpInst.setOpcode(Opcode);
6998     TmpInst.addOperand(Inst.getOperand(0));
6999     TmpInst.addOperand(Inst.getOperand(1));
7000     TmpInst.addOperand(Inst.getOperand(1));
7001     TmpInst.addOperand(MCOperand::createReg(0));
7002     TmpInst.addOperand(MCOperand::createImm(0));
7003     TmpInst.addOperand(Inst.getOperand(2));
7004     TmpInst.addOperand(Inst.getOperand(3));
7005     Inst = TmpInst;
7006     return true;
7007   }
7008   // Alias for alternate form of 'str{,b}t Rt, [Rn], #imm' instruction.
7009   case ARM::STRT_POST:
7010   case ARM::STRBT_POST: {
7011     const unsigned Opcode =
7012       (Inst.getOpcode() == ARM::STRT_POST) ? ARM::STRT_POST_IMM
7013                                            : ARM::STRBT_POST_IMM;
7014     MCInst TmpInst;
7015     TmpInst.setOpcode(Opcode);
7016     TmpInst.addOperand(Inst.getOperand(1));
7017     TmpInst.addOperand(Inst.getOperand(0));
7018     TmpInst.addOperand(Inst.getOperand(1));
7019     TmpInst.addOperand(MCOperand::createReg(0));
7020     TmpInst.addOperand(MCOperand::createImm(0));
7021     TmpInst.addOperand(Inst.getOperand(2));
7022     TmpInst.addOperand(Inst.getOperand(3));
7023     Inst = TmpInst;
7024     return true;
7025   }
7026   // Alias for alternate form of 'ADR Rd, #imm' instruction.
7027   case ARM::ADDri: {
7028     if (Inst.getOperand(1).getReg() != ARM::PC ||
7029         Inst.getOperand(5).getReg() != 0 ||
7030         !(Inst.getOperand(2).isExpr() || Inst.getOperand(2).isImm()))
7031       return false;
7032     MCInst TmpInst;
7033     TmpInst.setOpcode(ARM::ADR);
7034     TmpInst.addOperand(Inst.getOperand(0));
7035     if (Inst.getOperand(2).isImm()) {
7036       // Immediate (mod_imm) will be in its encoded form, we must unencode it
7037       // before passing it to the ADR instruction.
7038       unsigned Enc = Inst.getOperand(2).getImm();
7039       TmpInst.addOperand(MCOperand::createImm(
7040         ARM_AM::rotr32(Enc & 0xFF, (Enc & 0xF00) >> 7)));
7041     } else {
7042       // Turn PC-relative expression into absolute expression.
7043       // Reading PC provides the start of the current instruction + 8 and
7044       // the transform to adr is biased by that.
7045       MCSymbol *Dot = getContext().createTempSymbol();
7046       Out.EmitLabel(Dot);
7047       const MCExpr *OpExpr = Inst.getOperand(2).getExpr();
7048       const MCExpr *InstPC = MCSymbolRefExpr::create(Dot,
7049                                                      MCSymbolRefExpr::VK_None,
7050                                                      getContext());
7051       const MCExpr *Const8 = MCConstantExpr::create(8, getContext());
7052       const MCExpr *ReadPC = MCBinaryExpr::createAdd(InstPC, Const8,
7053                                                      getContext());
7054       const MCExpr *FixupAddr = MCBinaryExpr::createAdd(ReadPC, OpExpr,
7055                                                         getContext());
7056       TmpInst.addOperand(MCOperand::createExpr(FixupAddr));
7057     }
7058     TmpInst.addOperand(Inst.getOperand(3));
7059     TmpInst.addOperand(Inst.getOperand(4));
7060     Inst = TmpInst;
7061     return true;
7062   }
7063   // Aliases for alternate PC+imm syntax of LDR instructions.
7064   case ARM::t2LDRpcrel:
7065     // Select the narrow version if the immediate will fit.
7066     if (Inst.getOperand(1).getImm() > 0 &&
7067         Inst.getOperand(1).getImm() <= 0xff &&
7068         !(static_cast<ARMOperand &>(*Operands[2]).isToken() &&
7069           static_cast<ARMOperand &>(*Operands[2]).getToken() == ".w"))
7070       Inst.setOpcode(ARM::tLDRpci);
7071     else
7072       Inst.setOpcode(ARM::t2LDRpci);
7073     return true;
7074   case ARM::t2LDRBpcrel:
7075     Inst.setOpcode(ARM::t2LDRBpci);
7076     return true;
7077   case ARM::t2LDRHpcrel:
7078     Inst.setOpcode(ARM::t2LDRHpci);
7079     return true;
7080   case ARM::t2LDRSBpcrel:
7081     Inst.setOpcode(ARM::t2LDRSBpci);
7082     return true;
7083   case ARM::t2LDRSHpcrel:
7084     Inst.setOpcode(ARM::t2LDRSHpci);
7085     return true;
7086   case ARM::LDRConstPool:
7087   case ARM::tLDRConstPool:
7088   case ARM::t2LDRConstPool: {
7089     // Pseudo instruction ldr rt, =immediate is converted to a
7090     // MOV rt, immediate if immediate is known and representable
7091     // otherwise we create a constant pool entry that we load from.
7092     MCInst TmpInst;
7093     if (Inst.getOpcode() == ARM::LDRConstPool)
7094       TmpInst.setOpcode(ARM::LDRi12);
7095     else if (Inst.getOpcode() == ARM::tLDRConstPool)
7096       TmpInst.setOpcode(ARM::tLDRpci);
7097     else if (Inst.getOpcode() == ARM::t2LDRConstPool)
7098       TmpInst.setOpcode(ARM::t2LDRpci);
7099     const ARMOperand &PoolOperand =
7100       static_cast<ARMOperand &>(*Operands[3]);
7101     const MCExpr *SubExprVal = PoolOperand.getConstantPoolImm();
7102     // If SubExprVal is a constant we may be able to use a MOV
7103     if (isa<MCConstantExpr>(SubExprVal) &&
7104         Inst.getOperand(0).getReg() != ARM::PC &&
7105         Inst.getOperand(0).getReg() != ARM::SP) {
7106       int64_t Value =
7107         (int64_t) (cast<MCConstantExpr>(SubExprVal))->getValue();
7108       bool UseMov  = true;
7109       bool MovHasS = true;
7110       if (Inst.getOpcode() == ARM::LDRConstPool) {
7111         // ARM Constant
7112         if (ARM_AM::getSOImmVal(Value) != -1) {
7113           Value = ARM_AM::getSOImmVal(Value);
7114           TmpInst.setOpcode(ARM::MOVi);
7115         }
7116         else if (ARM_AM::getSOImmVal(~Value) != -1) {
7117           Value = ARM_AM::getSOImmVal(~Value);
7118           TmpInst.setOpcode(ARM::MVNi);
7119         }
7120         else if (hasV6T2Ops() &&
7121                  Value >=0 && Value < 65536) {
7122           TmpInst.setOpcode(ARM::MOVi16);
7123           MovHasS = false;
7124         }
7125         else
7126           UseMov = false;
7127       }
7128       else {
7129         // Thumb/Thumb2 Constant
7130         if (hasThumb2() &&
7131             ARM_AM::getT2SOImmVal(Value) != -1)
7132           TmpInst.setOpcode(ARM::t2MOVi);
7133         else if (hasThumb2() &&
7134                  ARM_AM::getT2SOImmVal(~Value) != -1) {
7135           TmpInst.setOpcode(ARM::t2MVNi);
7136           Value = ~Value;
7137         }
7138         else if (hasV8MBaseline() &&
7139                  Value >=0 && Value < 65536) {
7140           TmpInst.setOpcode(ARM::t2MOVi16);
7141           MovHasS = false;
7142         }
7143         else
7144           UseMov = false;
7145       }
7146       if (UseMov) {
7147         TmpInst.addOperand(Inst.getOperand(0));           // Rt
7148         TmpInst.addOperand(MCOperand::createImm(Value));  // Immediate
7149         TmpInst.addOperand(Inst.getOperand(2));           // CondCode
7150         TmpInst.addOperand(Inst.getOperand(3));           // CondCode
7151         if (MovHasS)
7152           TmpInst.addOperand(MCOperand::createReg(0));    // S
7153         Inst = TmpInst;
7154         return true;
7155       }
7156     }
7157     // No opportunity to use MOV/MVN create constant pool
7158     const MCExpr *CPLoc =
7159       getTargetStreamer().addConstantPoolEntry(SubExprVal,
7160                                                PoolOperand.getStartLoc());
7161     TmpInst.addOperand(Inst.getOperand(0));           // Rt
7162     TmpInst.addOperand(MCOperand::createExpr(CPLoc)); // offset to constpool
7163     if (TmpInst.getOpcode() == ARM::LDRi12)
7164       TmpInst.addOperand(MCOperand::createImm(0));    // unused offset
7165     TmpInst.addOperand(Inst.getOperand(2));           // CondCode
7166     TmpInst.addOperand(Inst.getOperand(3));           // CondCode
7167     Inst = TmpInst;
7168     return true;
7169   }
7170   // Handle NEON VST complex aliases.
7171   case ARM::VST1LNdWB_register_Asm_8:
7172   case ARM::VST1LNdWB_register_Asm_16:
7173   case ARM::VST1LNdWB_register_Asm_32: {
7174     MCInst TmpInst;
7175     // Shuffle the operands around so the lane index operand is in the
7176     // right place.
7177     unsigned Spacing;
7178     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7179     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7180     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7181     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7182     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7183     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7184     TmpInst.addOperand(Inst.getOperand(1)); // lane
7185     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7186     TmpInst.addOperand(Inst.getOperand(6));
7187     Inst = TmpInst;
7188     return true;
7189   }
7190 
7191   case ARM::VST2LNdWB_register_Asm_8:
7192   case ARM::VST2LNdWB_register_Asm_16:
7193   case ARM::VST2LNdWB_register_Asm_32:
7194   case ARM::VST2LNqWB_register_Asm_16:
7195   case ARM::VST2LNqWB_register_Asm_32: {
7196     MCInst TmpInst;
7197     // Shuffle the operands around so the lane index operand is in the
7198     // right place.
7199     unsigned Spacing;
7200     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7201     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7202     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7203     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7204     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7205     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7206     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7207                                             Spacing));
7208     TmpInst.addOperand(Inst.getOperand(1)); // lane
7209     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7210     TmpInst.addOperand(Inst.getOperand(6));
7211     Inst = TmpInst;
7212     return true;
7213   }
7214 
7215   case ARM::VST3LNdWB_register_Asm_8:
7216   case ARM::VST3LNdWB_register_Asm_16:
7217   case ARM::VST3LNdWB_register_Asm_32:
7218   case ARM::VST3LNqWB_register_Asm_16:
7219   case ARM::VST3LNqWB_register_Asm_32: {
7220     MCInst TmpInst;
7221     // Shuffle the operands around so the lane index operand is in the
7222     // right place.
7223     unsigned Spacing;
7224     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7225     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7226     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7227     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7228     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7229     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7230     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7231                                             Spacing));
7232     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7233                                             Spacing * 2));
7234     TmpInst.addOperand(Inst.getOperand(1)); // lane
7235     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7236     TmpInst.addOperand(Inst.getOperand(6));
7237     Inst = TmpInst;
7238     return true;
7239   }
7240 
7241   case ARM::VST4LNdWB_register_Asm_8:
7242   case ARM::VST4LNdWB_register_Asm_16:
7243   case ARM::VST4LNdWB_register_Asm_32:
7244   case ARM::VST4LNqWB_register_Asm_16:
7245   case ARM::VST4LNqWB_register_Asm_32: {
7246     MCInst TmpInst;
7247     // Shuffle the operands around so the lane index operand is in the
7248     // right place.
7249     unsigned Spacing;
7250     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7251     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7252     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7253     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7254     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7255     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7256     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7257                                             Spacing));
7258     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7259                                             Spacing * 2));
7260     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7261                                             Spacing * 3));
7262     TmpInst.addOperand(Inst.getOperand(1)); // lane
7263     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7264     TmpInst.addOperand(Inst.getOperand(6));
7265     Inst = TmpInst;
7266     return true;
7267   }
7268 
7269   case ARM::VST1LNdWB_fixed_Asm_8:
7270   case ARM::VST1LNdWB_fixed_Asm_16:
7271   case ARM::VST1LNdWB_fixed_Asm_32: {
7272     MCInst TmpInst;
7273     // Shuffle the operands around so the lane index operand is in the
7274     // right place.
7275     unsigned Spacing;
7276     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7277     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7278     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7279     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7280     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7281     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7282     TmpInst.addOperand(Inst.getOperand(1)); // lane
7283     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7284     TmpInst.addOperand(Inst.getOperand(5));
7285     Inst = TmpInst;
7286     return true;
7287   }
7288 
7289   case ARM::VST2LNdWB_fixed_Asm_8:
7290   case ARM::VST2LNdWB_fixed_Asm_16:
7291   case ARM::VST2LNdWB_fixed_Asm_32:
7292   case ARM::VST2LNqWB_fixed_Asm_16:
7293   case ARM::VST2LNqWB_fixed_Asm_32: {
7294     MCInst TmpInst;
7295     // Shuffle the operands around so the lane index operand is in the
7296     // right place.
7297     unsigned Spacing;
7298     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7299     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7300     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7301     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7302     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7303     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7304     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7305                                             Spacing));
7306     TmpInst.addOperand(Inst.getOperand(1)); // lane
7307     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7308     TmpInst.addOperand(Inst.getOperand(5));
7309     Inst = TmpInst;
7310     return true;
7311   }
7312 
7313   case ARM::VST3LNdWB_fixed_Asm_8:
7314   case ARM::VST3LNdWB_fixed_Asm_16:
7315   case ARM::VST3LNdWB_fixed_Asm_32:
7316   case ARM::VST3LNqWB_fixed_Asm_16:
7317   case ARM::VST3LNqWB_fixed_Asm_32: {
7318     MCInst TmpInst;
7319     // Shuffle the operands around so the lane index operand is in the
7320     // right place.
7321     unsigned Spacing;
7322     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7323     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7324     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7325     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7326     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7327     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7328     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7329                                             Spacing));
7330     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7331                                             Spacing * 2));
7332     TmpInst.addOperand(Inst.getOperand(1)); // lane
7333     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7334     TmpInst.addOperand(Inst.getOperand(5));
7335     Inst = TmpInst;
7336     return true;
7337   }
7338 
7339   case ARM::VST4LNdWB_fixed_Asm_8:
7340   case ARM::VST4LNdWB_fixed_Asm_16:
7341   case ARM::VST4LNdWB_fixed_Asm_32:
7342   case ARM::VST4LNqWB_fixed_Asm_16:
7343   case ARM::VST4LNqWB_fixed_Asm_32: {
7344     MCInst TmpInst;
7345     // Shuffle the operands around so the lane index operand is in the
7346     // right place.
7347     unsigned Spacing;
7348     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7349     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7350     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7351     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7352     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7353     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7354     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7355                                             Spacing));
7356     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7357                                             Spacing * 2));
7358     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7359                                             Spacing * 3));
7360     TmpInst.addOperand(Inst.getOperand(1)); // lane
7361     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7362     TmpInst.addOperand(Inst.getOperand(5));
7363     Inst = TmpInst;
7364     return true;
7365   }
7366 
7367   case ARM::VST1LNdAsm_8:
7368   case ARM::VST1LNdAsm_16:
7369   case ARM::VST1LNdAsm_32: {
7370     MCInst TmpInst;
7371     // Shuffle the operands around so the lane index operand is in the
7372     // right place.
7373     unsigned Spacing;
7374     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7375     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7376     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7377     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7378     TmpInst.addOperand(Inst.getOperand(1)); // lane
7379     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7380     TmpInst.addOperand(Inst.getOperand(5));
7381     Inst = TmpInst;
7382     return true;
7383   }
7384 
7385   case ARM::VST2LNdAsm_8:
7386   case ARM::VST2LNdAsm_16:
7387   case ARM::VST2LNdAsm_32:
7388   case ARM::VST2LNqAsm_16:
7389   case ARM::VST2LNqAsm_32: {
7390     MCInst TmpInst;
7391     // Shuffle the operands around so the lane index operand is in the
7392     // right place.
7393     unsigned Spacing;
7394     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7395     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7396     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7397     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7398     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7399                                             Spacing));
7400     TmpInst.addOperand(Inst.getOperand(1)); // lane
7401     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7402     TmpInst.addOperand(Inst.getOperand(5));
7403     Inst = TmpInst;
7404     return true;
7405   }
7406 
7407   case ARM::VST3LNdAsm_8:
7408   case ARM::VST3LNdAsm_16:
7409   case ARM::VST3LNdAsm_32:
7410   case ARM::VST3LNqAsm_16:
7411   case ARM::VST3LNqAsm_32: {
7412     MCInst TmpInst;
7413     // Shuffle the operands around so the lane index operand is in the
7414     // right place.
7415     unsigned Spacing;
7416     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7417     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7418     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7419     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7420     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7421                                             Spacing));
7422     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7423                                             Spacing * 2));
7424     TmpInst.addOperand(Inst.getOperand(1)); // lane
7425     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7426     TmpInst.addOperand(Inst.getOperand(5));
7427     Inst = TmpInst;
7428     return true;
7429   }
7430 
7431   case ARM::VST4LNdAsm_8:
7432   case ARM::VST4LNdAsm_16:
7433   case ARM::VST4LNdAsm_32:
7434   case ARM::VST4LNqAsm_16:
7435   case ARM::VST4LNqAsm_32: {
7436     MCInst TmpInst;
7437     // Shuffle the operands around so the lane index operand is in the
7438     // right place.
7439     unsigned Spacing;
7440     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
7441     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7442     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7443     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7444     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7445                                             Spacing));
7446     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7447                                             Spacing * 2));
7448     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7449                                             Spacing * 3));
7450     TmpInst.addOperand(Inst.getOperand(1)); // lane
7451     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7452     TmpInst.addOperand(Inst.getOperand(5));
7453     Inst = TmpInst;
7454     return true;
7455   }
7456 
7457   // Handle NEON VLD complex aliases.
7458   case ARM::VLD1LNdWB_register_Asm_8:
7459   case ARM::VLD1LNdWB_register_Asm_16:
7460   case ARM::VLD1LNdWB_register_Asm_32: {
7461     MCInst TmpInst;
7462     // Shuffle the operands around so the lane index operand is in the
7463     // right place.
7464     unsigned Spacing;
7465     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7466     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7467     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7468     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7469     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7470     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7471     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7472     TmpInst.addOperand(Inst.getOperand(1)); // lane
7473     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7474     TmpInst.addOperand(Inst.getOperand(6));
7475     Inst = TmpInst;
7476     return true;
7477   }
7478 
7479   case ARM::VLD2LNdWB_register_Asm_8:
7480   case ARM::VLD2LNdWB_register_Asm_16:
7481   case ARM::VLD2LNdWB_register_Asm_32:
7482   case ARM::VLD2LNqWB_register_Asm_16:
7483   case ARM::VLD2LNqWB_register_Asm_32: {
7484     MCInst TmpInst;
7485     // Shuffle the operands around so the lane index operand is in the
7486     // right place.
7487     unsigned Spacing;
7488     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7489     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7490     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7491                                             Spacing));
7492     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7493     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7494     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7495     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7496     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7497     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7498                                             Spacing));
7499     TmpInst.addOperand(Inst.getOperand(1)); // lane
7500     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7501     TmpInst.addOperand(Inst.getOperand(6));
7502     Inst = TmpInst;
7503     return true;
7504   }
7505 
7506   case ARM::VLD3LNdWB_register_Asm_8:
7507   case ARM::VLD3LNdWB_register_Asm_16:
7508   case ARM::VLD3LNdWB_register_Asm_32:
7509   case ARM::VLD3LNqWB_register_Asm_16:
7510   case ARM::VLD3LNqWB_register_Asm_32: {
7511     MCInst TmpInst;
7512     // Shuffle the operands around so the lane index operand is in the
7513     // right place.
7514     unsigned Spacing;
7515     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7516     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7517     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7518                                             Spacing));
7519     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7520                                             Spacing * 2));
7521     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7522     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7523     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7524     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7525     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7526     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7527                                             Spacing));
7528     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7529                                             Spacing * 2));
7530     TmpInst.addOperand(Inst.getOperand(1)); // lane
7531     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7532     TmpInst.addOperand(Inst.getOperand(6));
7533     Inst = TmpInst;
7534     return true;
7535   }
7536 
7537   case ARM::VLD4LNdWB_register_Asm_8:
7538   case ARM::VLD4LNdWB_register_Asm_16:
7539   case ARM::VLD4LNdWB_register_Asm_32:
7540   case ARM::VLD4LNqWB_register_Asm_16:
7541   case ARM::VLD4LNqWB_register_Asm_32: {
7542     MCInst TmpInst;
7543     // Shuffle the operands around so the lane index operand is in the
7544     // right place.
7545     unsigned Spacing;
7546     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7547     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7548     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7549                                             Spacing));
7550     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7551                                             Spacing * 2));
7552     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7553                                             Spacing * 3));
7554     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7555     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7556     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7557     TmpInst.addOperand(Inst.getOperand(4)); // Rm
7558     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7559     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7560                                             Spacing));
7561     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7562                                             Spacing * 2));
7563     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7564                                             Spacing * 3));
7565     TmpInst.addOperand(Inst.getOperand(1)); // lane
7566     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
7567     TmpInst.addOperand(Inst.getOperand(6));
7568     Inst = TmpInst;
7569     return true;
7570   }
7571 
7572   case ARM::VLD1LNdWB_fixed_Asm_8:
7573   case ARM::VLD1LNdWB_fixed_Asm_16:
7574   case ARM::VLD1LNdWB_fixed_Asm_32: {
7575     MCInst TmpInst;
7576     // Shuffle the operands around so the lane index operand is in the
7577     // right place.
7578     unsigned Spacing;
7579     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7580     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7581     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7582     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7583     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7584     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7585     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7586     TmpInst.addOperand(Inst.getOperand(1)); // lane
7587     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7588     TmpInst.addOperand(Inst.getOperand(5));
7589     Inst = TmpInst;
7590     return true;
7591   }
7592 
7593   case ARM::VLD2LNdWB_fixed_Asm_8:
7594   case ARM::VLD2LNdWB_fixed_Asm_16:
7595   case ARM::VLD2LNdWB_fixed_Asm_32:
7596   case ARM::VLD2LNqWB_fixed_Asm_16:
7597   case ARM::VLD2LNqWB_fixed_Asm_32: {
7598     MCInst TmpInst;
7599     // Shuffle the operands around so the lane index operand is in the
7600     // right place.
7601     unsigned Spacing;
7602     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7603     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7604     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7605                                             Spacing));
7606     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7607     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7608     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7609     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7610     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7611     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7612                                             Spacing));
7613     TmpInst.addOperand(Inst.getOperand(1)); // lane
7614     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7615     TmpInst.addOperand(Inst.getOperand(5));
7616     Inst = TmpInst;
7617     return true;
7618   }
7619 
7620   case ARM::VLD3LNdWB_fixed_Asm_8:
7621   case ARM::VLD3LNdWB_fixed_Asm_16:
7622   case ARM::VLD3LNdWB_fixed_Asm_32:
7623   case ARM::VLD3LNqWB_fixed_Asm_16:
7624   case ARM::VLD3LNqWB_fixed_Asm_32: {
7625     MCInst TmpInst;
7626     // Shuffle the operands around so the lane index operand is in the
7627     // right place.
7628     unsigned Spacing;
7629     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7630     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7631     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7632                                             Spacing));
7633     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7634                                             Spacing * 2));
7635     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7636     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7637     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7638     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7639     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7640     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7641                                             Spacing));
7642     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7643                                             Spacing * 2));
7644     TmpInst.addOperand(Inst.getOperand(1)); // lane
7645     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7646     TmpInst.addOperand(Inst.getOperand(5));
7647     Inst = TmpInst;
7648     return true;
7649   }
7650 
7651   case ARM::VLD4LNdWB_fixed_Asm_8:
7652   case ARM::VLD4LNdWB_fixed_Asm_16:
7653   case ARM::VLD4LNdWB_fixed_Asm_32:
7654   case ARM::VLD4LNqWB_fixed_Asm_16:
7655   case ARM::VLD4LNqWB_fixed_Asm_32: {
7656     MCInst TmpInst;
7657     // Shuffle the operands around so the lane index operand is in the
7658     // right place.
7659     unsigned Spacing;
7660     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7661     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7662     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7663                                             Spacing));
7664     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7665                                             Spacing * 2));
7666     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7667                                             Spacing * 3));
7668     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
7669     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7670     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7671     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7672     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7673     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7674                                             Spacing));
7675     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7676                                             Spacing * 2));
7677     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7678                                             Spacing * 3));
7679     TmpInst.addOperand(Inst.getOperand(1)); // lane
7680     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7681     TmpInst.addOperand(Inst.getOperand(5));
7682     Inst = TmpInst;
7683     return true;
7684   }
7685 
7686   case ARM::VLD1LNdAsm_8:
7687   case ARM::VLD1LNdAsm_16:
7688   case ARM::VLD1LNdAsm_32: {
7689     MCInst TmpInst;
7690     // Shuffle the operands around so the lane index operand is in the
7691     // right place.
7692     unsigned Spacing;
7693     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7694     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7695     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7696     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7697     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7698     TmpInst.addOperand(Inst.getOperand(1)); // lane
7699     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7700     TmpInst.addOperand(Inst.getOperand(5));
7701     Inst = TmpInst;
7702     return true;
7703   }
7704 
7705   case ARM::VLD2LNdAsm_8:
7706   case ARM::VLD2LNdAsm_16:
7707   case ARM::VLD2LNdAsm_32:
7708   case ARM::VLD2LNqAsm_16:
7709   case ARM::VLD2LNqAsm_32: {
7710     MCInst TmpInst;
7711     // Shuffle the operands around so the lane index operand is in the
7712     // right place.
7713     unsigned Spacing;
7714     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7715     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7716     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7717                                             Spacing));
7718     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7719     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7720     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7721     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7722                                             Spacing));
7723     TmpInst.addOperand(Inst.getOperand(1)); // lane
7724     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7725     TmpInst.addOperand(Inst.getOperand(5));
7726     Inst = TmpInst;
7727     return true;
7728   }
7729 
7730   case ARM::VLD3LNdAsm_8:
7731   case ARM::VLD3LNdAsm_16:
7732   case ARM::VLD3LNdAsm_32:
7733   case ARM::VLD3LNqAsm_16:
7734   case ARM::VLD3LNqAsm_32: {
7735     MCInst TmpInst;
7736     // Shuffle the operands around so the lane index operand is in the
7737     // right place.
7738     unsigned Spacing;
7739     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7740     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7741     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7742                                             Spacing));
7743     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7744                                             Spacing * 2));
7745     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7746     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7747     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7748     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7749                                             Spacing));
7750     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7751                                             Spacing * 2));
7752     TmpInst.addOperand(Inst.getOperand(1)); // lane
7753     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7754     TmpInst.addOperand(Inst.getOperand(5));
7755     Inst = TmpInst;
7756     return true;
7757   }
7758 
7759   case ARM::VLD4LNdAsm_8:
7760   case ARM::VLD4LNdAsm_16:
7761   case ARM::VLD4LNdAsm_32:
7762   case ARM::VLD4LNqAsm_16:
7763   case ARM::VLD4LNqAsm_32: {
7764     MCInst TmpInst;
7765     // Shuffle the operands around so the lane index operand is in the
7766     // right place.
7767     unsigned Spacing;
7768     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7769     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7770     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7771                                             Spacing));
7772     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7773                                             Spacing * 2));
7774     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7775                                             Spacing * 3));
7776     TmpInst.addOperand(Inst.getOperand(2)); // Rn
7777     TmpInst.addOperand(Inst.getOperand(3)); // alignment
7778     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
7779     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7780                                             Spacing));
7781     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7782                                             Spacing * 2));
7783     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7784                                             Spacing * 3));
7785     TmpInst.addOperand(Inst.getOperand(1)); // lane
7786     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7787     TmpInst.addOperand(Inst.getOperand(5));
7788     Inst = TmpInst;
7789     return true;
7790   }
7791 
7792   // VLD3DUP single 3-element structure to all lanes instructions.
7793   case ARM::VLD3DUPdAsm_8:
7794   case ARM::VLD3DUPdAsm_16:
7795   case ARM::VLD3DUPdAsm_32:
7796   case ARM::VLD3DUPqAsm_8:
7797   case ARM::VLD3DUPqAsm_16:
7798   case ARM::VLD3DUPqAsm_32: {
7799     MCInst TmpInst;
7800     unsigned Spacing;
7801     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7802     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7803     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7804                                             Spacing));
7805     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7806                                             Spacing * 2));
7807     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7808     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7809     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7810     TmpInst.addOperand(Inst.getOperand(4));
7811     Inst = TmpInst;
7812     return true;
7813   }
7814 
7815   case ARM::VLD3DUPdWB_fixed_Asm_8:
7816   case ARM::VLD3DUPdWB_fixed_Asm_16:
7817   case ARM::VLD3DUPdWB_fixed_Asm_32:
7818   case ARM::VLD3DUPqWB_fixed_Asm_8:
7819   case ARM::VLD3DUPqWB_fixed_Asm_16:
7820   case ARM::VLD3DUPqWB_fixed_Asm_32: {
7821     MCInst TmpInst;
7822     unsigned Spacing;
7823     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7824     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7825     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7826                                             Spacing));
7827     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7828                                             Spacing * 2));
7829     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7830     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7831     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7832     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7833     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7834     TmpInst.addOperand(Inst.getOperand(4));
7835     Inst = TmpInst;
7836     return true;
7837   }
7838 
7839   case ARM::VLD3DUPdWB_register_Asm_8:
7840   case ARM::VLD3DUPdWB_register_Asm_16:
7841   case ARM::VLD3DUPdWB_register_Asm_32:
7842   case ARM::VLD3DUPqWB_register_Asm_8:
7843   case ARM::VLD3DUPqWB_register_Asm_16:
7844   case ARM::VLD3DUPqWB_register_Asm_32: {
7845     MCInst TmpInst;
7846     unsigned Spacing;
7847     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7848     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7849     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7850                                             Spacing));
7851     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7852                                             Spacing * 2));
7853     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7854     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7855     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7856     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7857     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7858     TmpInst.addOperand(Inst.getOperand(5));
7859     Inst = TmpInst;
7860     return true;
7861   }
7862 
7863   // VLD3 multiple 3-element structure instructions.
7864   case ARM::VLD3dAsm_8:
7865   case ARM::VLD3dAsm_16:
7866   case ARM::VLD3dAsm_32:
7867   case ARM::VLD3qAsm_8:
7868   case ARM::VLD3qAsm_16:
7869   case ARM::VLD3qAsm_32: {
7870     MCInst TmpInst;
7871     unsigned Spacing;
7872     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7873     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7874     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7875                                             Spacing));
7876     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7877                                             Spacing * 2));
7878     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7879     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7880     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7881     TmpInst.addOperand(Inst.getOperand(4));
7882     Inst = TmpInst;
7883     return true;
7884   }
7885 
7886   case ARM::VLD3dWB_fixed_Asm_8:
7887   case ARM::VLD3dWB_fixed_Asm_16:
7888   case ARM::VLD3dWB_fixed_Asm_32:
7889   case ARM::VLD3qWB_fixed_Asm_8:
7890   case ARM::VLD3qWB_fixed_Asm_16:
7891   case ARM::VLD3qWB_fixed_Asm_32: {
7892     MCInst TmpInst;
7893     unsigned Spacing;
7894     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7895     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7896     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7897                                             Spacing));
7898     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7899                                             Spacing * 2));
7900     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7901     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7902     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7903     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7904     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7905     TmpInst.addOperand(Inst.getOperand(4));
7906     Inst = TmpInst;
7907     return true;
7908   }
7909 
7910   case ARM::VLD3dWB_register_Asm_8:
7911   case ARM::VLD3dWB_register_Asm_16:
7912   case ARM::VLD3dWB_register_Asm_32:
7913   case ARM::VLD3qWB_register_Asm_8:
7914   case ARM::VLD3qWB_register_Asm_16:
7915   case ARM::VLD3qWB_register_Asm_32: {
7916     MCInst TmpInst;
7917     unsigned Spacing;
7918     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7919     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7920     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7921                                             Spacing));
7922     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7923                                             Spacing * 2));
7924     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7925     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7926     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7927     TmpInst.addOperand(Inst.getOperand(3)); // Rm
7928     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
7929     TmpInst.addOperand(Inst.getOperand(5));
7930     Inst = TmpInst;
7931     return true;
7932   }
7933 
7934   // VLD4DUP single 3-element structure to all lanes instructions.
7935   case ARM::VLD4DUPdAsm_8:
7936   case ARM::VLD4DUPdAsm_16:
7937   case ARM::VLD4DUPdAsm_32:
7938   case ARM::VLD4DUPqAsm_8:
7939   case ARM::VLD4DUPqAsm_16:
7940   case ARM::VLD4DUPqAsm_32: {
7941     MCInst TmpInst;
7942     unsigned Spacing;
7943     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7944     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7945     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7946                                             Spacing));
7947     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7948                                             Spacing * 2));
7949     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7950                                             Spacing * 3));
7951     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7952     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7953     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7954     TmpInst.addOperand(Inst.getOperand(4));
7955     Inst = TmpInst;
7956     return true;
7957   }
7958 
7959   case ARM::VLD4DUPdWB_fixed_Asm_8:
7960   case ARM::VLD4DUPdWB_fixed_Asm_16:
7961   case ARM::VLD4DUPdWB_fixed_Asm_32:
7962   case ARM::VLD4DUPqWB_fixed_Asm_8:
7963   case ARM::VLD4DUPqWB_fixed_Asm_16:
7964   case ARM::VLD4DUPqWB_fixed_Asm_32: {
7965     MCInst TmpInst;
7966     unsigned Spacing;
7967     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7968     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7969     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7970                                             Spacing));
7971     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7972                                             Spacing * 2));
7973     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7974                                             Spacing * 3));
7975     TmpInst.addOperand(Inst.getOperand(1)); // Rn
7976     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
7977     TmpInst.addOperand(Inst.getOperand(2)); // alignment
7978     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
7979     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
7980     TmpInst.addOperand(Inst.getOperand(4));
7981     Inst = TmpInst;
7982     return true;
7983   }
7984 
7985   case ARM::VLD4DUPdWB_register_Asm_8:
7986   case ARM::VLD4DUPdWB_register_Asm_16:
7987   case ARM::VLD4DUPdWB_register_Asm_32:
7988   case ARM::VLD4DUPqWB_register_Asm_8:
7989   case ARM::VLD4DUPqWB_register_Asm_16:
7990   case ARM::VLD4DUPqWB_register_Asm_32: {
7991     MCInst TmpInst;
7992     unsigned Spacing;
7993     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
7994     TmpInst.addOperand(Inst.getOperand(0)); // Vd
7995     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7996                                             Spacing));
7997     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
7998                                             Spacing * 2));
7999     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8000                                             Spacing * 3));
8001     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8002     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8003     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8004     TmpInst.addOperand(Inst.getOperand(3)); // Rm
8005     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
8006     TmpInst.addOperand(Inst.getOperand(5));
8007     Inst = TmpInst;
8008     return true;
8009   }
8010 
8011   // VLD4 multiple 4-element structure instructions.
8012   case ARM::VLD4dAsm_8:
8013   case ARM::VLD4dAsm_16:
8014   case ARM::VLD4dAsm_32:
8015   case ARM::VLD4qAsm_8:
8016   case ARM::VLD4qAsm_16:
8017   case ARM::VLD4qAsm_32: {
8018     MCInst TmpInst;
8019     unsigned Spacing;
8020     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
8021     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8022     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8023                                             Spacing));
8024     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8025                                             Spacing * 2));
8026     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8027                                             Spacing * 3));
8028     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8029     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8030     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8031     TmpInst.addOperand(Inst.getOperand(4));
8032     Inst = TmpInst;
8033     return true;
8034   }
8035 
8036   case ARM::VLD4dWB_fixed_Asm_8:
8037   case ARM::VLD4dWB_fixed_Asm_16:
8038   case ARM::VLD4dWB_fixed_Asm_32:
8039   case ARM::VLD4qWB_fixed_Asm_8:
8040   case ARM::VLD4qWB_fixed_Asm_16:
8041   case ARM::VLD4qWB_fixed_Asm_32: {
8042     MCInst TmpInst;
8043     unsigned Spacing;
8044     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
8045     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8046     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8047                                             Spacing));
8048     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8049                                             Spacing * 2));
8050     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8051                                             Spacing * 3));
8052     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8053     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8054     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8055     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
8056     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8057     TmpInst.addOperand(Inst.getOperand(4));
8058     Inst = TmpInst;
8059     return true;
8060   }
8061 
8062   case ARM::VLD4dWB_register_Asm_8:
8063   case ARM::VLD4dWB_register_Asm_16:
8064   case ARM::VLD4dWB_register_Asm_32:
8065   case ARM::VLD4qWB_register_Asm_8:
8066   case ARM::VLD4qWB_register_Asm_16:
8067   case ARM::VLD4qWB_register_Asm_32: {
8068     MCInst TmpInst;
8069     unsigned Spacing;
8070     TmpInst.setOpcode(getRealVLDOpcode(Inst.getOpcode(), Spacing));
8071     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8072     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8073                                             Spacing));
8074     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8075                                             Spacing * 2));
8076     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8077                                             Spacing * 3));
8078     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8079     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8080     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8081     TmpInst.addOperand(Inst.getOperand(3)); // Rm
8082     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
8083     TmpInst.addOperand(Inst.getOperand(5));
8084     Inst = TmpInst;
8085     return true;
8086   }
8087 
8088   // VST3 multiple 3-element structure instructions.
8089   case ARM::VST3dAsm_8:
8090   case ARM::VST3dAsm_16:
8091   case ARM::VST3dAsm_32:
8092   case ARM::VST3qAsm_8:
8093   case ARM::VST3qAsm_16:
8094   case ARM::VST3qAsm_32: {
8095     MCInst TmpInst;
8096     unsigned Spacing;
8097     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8098     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8099     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8100     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8101     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8102                                             Spacing));
8103     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8104                                             Spacing * 2));
8105     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8106     TmpInst.addOperand(Inst.getOperand(4));
8107     Inst = TmpInst;
8108     return true;
8109   }
8110 
8111   case ARM::VST3dWB_fixed_Asm_8:
8112   case ARM::VST3dWB_fixed_Asm_16:
8113   case ARM::VST3dWB_fixed_Asm_32:
8114   case ARM::VST3qWB_fixed_Asm_8:
8115   case ARM::VST3qWB_fixed_Asm_16:
8116   case ARM::VST3qWB_fixed_Asm_32: {
8117     MCInst TmpInst;
8118     unsigned Spacing;
8119     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8120     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8121     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8122     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8123     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
8124     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8125     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8126                                             Spacing));
8127     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8128                                             Spacing * 2));
8129     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8130     TmpInst.addOperand(Inst.getOperand(4));
8131     Inst = TmpInst;
8132     return true;
8133   }
8134 
8135   case ARM::VST3dWB_register_Asm_8:
8136   case ARM::VST3dWB_register_Asm_16:
8137   case ARM::VST3dWB_register_Asm_32:
8138   case ARM::VST3qWB_register_Asm_8:
8139   case ARM::VST3qWB_register_Asm_16:
8140   case ARM::VST3qWB_register_Asm_32: {
8141     MCInst TmpInst;
8142     unsigned Spacing;
8143     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8144     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8145     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8146     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8147     TmpInst.addOperand(Inst.getOperand(3)); // Rm
8148     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8149     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8150                                             Spacing));
8151     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8152                                             Spacing * 2));
8153     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
8154     TmpInst.addOperand(Inst.getOperand(5));
8155     Inst = TmpInst;
8156     return true;
8157   }
8158 
8159   // VST4 multiple 3-element structure instructions.
8160   case ARM::VST4dAsm_8:
8161   case ARM::VST4dAsm_16:
8162   case ARM::VST4dAsm_32:
8163   case ARM::VST4qAsm_8:
8164   case ARM::VST4qAsm_16:
8165   case ARM::VST4qAsm_32: {
8166     MCInst TmpInst;
8167     unsigned Spacing;
8168     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8169     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8170     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8171     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8172     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8173                                             Spacing));
8174     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8175                                             Spacing * 2));
8176     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8177                                             Spacing * 3));
8178     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8179     TmpInst.addOperand(Inst.getOperand(4));
8180     Inst = TmpInst;
8181     return true;
8182   }
8183 
8184   case ARM::VST4dWB_fixed_Asm_8:
8185   case ARM::VST4dWB_fixed_Asm_16:
8186   case ARM::VST4dWB_fixed_Asm_32:
8187   case ARM::VST4qWB_fixed_Asm_8:
8188   case ARM::VST4qWB_fixed_Asm_16:
8189   case ARM::VST4qWB_fixed_Asm_32: {
8190     MCInst TmpInst;
8191     unsigned Spacing;
8192     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8193     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8194     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8195     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8196     TmpInst.addOperand(MCOperand::createReg(0)); // Rm
8197     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8198     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8199                                             Spacing));
8200     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8201                                             Spacing * 2));
8202     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8203                                             Spacing * 3));
8204     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8205     TmpInst.addOperand(Inst.getOperand(4));
8206     Inst = TmpInst;
8207     return true;
8208   }
8209 
8210   case ARM::VST4dWB_register_Asm_8:
8211   case ARM::VST4dWB_register_Asm_16:
8212   case ARM::VST4dWB_register_Asm_32:
8213   case ARM::VST4qWB_register_Asm_8:
8214   case ARM::VST4qWB_register_Asm_16:
8215   case ARM::VST4qWB_register_Asm_32: {
8216     MCInst TmpInst;
8217     unsigned Spacing;
8218     TmpInst.setOpcode(getRealVSTOpcode(Inst.getOpcode(), Spacing));
8219     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8220     TmpInst.addOperand(Inst.getOperand(1)); // Rn_wb == tied Rn
8221     TmpInst.addOperand(Inst.getOperand(2)); // alignment
8222     TmpInst.addOperand(Inst.getOperand(3)); // Rm
8223     TmpInst.addOperand(Inst.getOperand(0)); // Vd
8224     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8225                                             Spacing));
8226     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8227                                             Spacing * 2));
8228     TmpInst.addOperand(MCOperand::createReg(Inst.getOperand(0).getReg() +
8229                                             Spacing * 3));
8230     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
8231     TmpInst.addOperand(Inst.getOperand(5));
8232     Inst = TmpInst;
8233     return true;
8234   }
8235 
8236   // Handle encoding choice for the shift-immediate instructions.
8237   case ARM::t2LSLri:
8238   case ARM::t2LSRri:
8239   case ARM::t2ASRri: {
8240     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8241         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
8242         Inst.getOperand(5).getReg() == (inITBlock() ? 0 : ARM::CPSR) &&
8243         !(static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8244           static_cast<ARMOperand &>(*Operands[3]).getToken() == ".w")) {
8245       unsigned NewOpc;
8246       switch (Inst.getOpcode()) {
8247       default: llvm_unreachable("unexpected opcode");
8248       case ARM::t2LSLri: NewOpc = ARM::tLSLri; break;
8249       case ARM::t2LSRri: NewOpc = ARM::tLSRri; break;
8250       case ARM::t2ASRri: NewOpc = ARM::tASRri; break;
8251       }
8252       // The Thumb1 operands aren't in the same order. Awesome, eh?
8253       MCInst TmpInst;
8254       TmpInst.setOpcode(NewOpc);
8255       TmpInst.addOperand(Inst.getOperand(0));
8256       TmpInst.addOperand(Inst.getOperand(5));
8257       TmpInst.addOperand(Inst.getOperand(1));
8258       TmpInst.addOperand(Inst.getOperand(2));
8259       TmpInst.addOperand(Inst.getOperand(3));
8260       TmpInst.addOperand(Inst.getOperand(4));
8261       Inst = TmpInst;
8262       return true;
8263     }
8264     return false;
8265   }
8266 
8267   // Handle the Thumb2 mode MOV complex aliases.
8268   case ARM::t2MOVsr:
8269   case ARM::t2MOVSsr: {
8270     // Which instruction to expand to depends on the CCOut operand and
8271     // whether we're in an IT block if the register operands are low
8272     // registers.
8273     bool isNarrow = false;
8274     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8275         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8276         isARMLowRegister(Inst.getOperand(2).getReg()) &&
8277         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
8278         inITBlock() == (Inst.getOpcode() == ARM::t2MOVsr))
8279       isNarrow = true;
8280     MCInst TmpInst;
8281     unsigned newOpc;
8282     switch(ARM_AM::getSORegShOp(Inst.getOperand(3).getImm())) {
8283     default: llvm_unreachable("unexpected opcode!");
8284     case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRrr : ARM::t2ASRrr; break;
8285     case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRrr : ARM::t2LSRrr; break;
8286     case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLrr : ARM::t2LSLrr; break;
8287     case ARM_AM::ror: newOpc = isNarrow ? ARM::tROR   : ARM::t2RORrr; break;
8288     }
8289     TmpInst.setOpcode(newOpc);
8290     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8291     if (isNarrow)
8292       TmpInst.addOperand(MCOperand::createReg(
8293           Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
8294     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8295     TmpInst.addOperand(Inst.getOperand(2)); // Rm
8296     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
8297     TmpInst.addOperand(Inst.getOperand(5));
8298     if (!isNarrow)
8299       TmpInst.addOperand(MCOperand::createReg(
8300           Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
8301     Inst = TmpInst;
8302     return true;
8303   }
8304   case ARM::t2MOVsi:
8305   case ARM::t2MOVSsi: {
8306     // Which instruction to expand to depends on the CCOut operand and
8307     // whether we're in an IT block if the register operands are low
8308     // registers.
8309     bool isNarrow = false;
8310     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8311         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8312         inITBlock() == (Inst.getOpcode() == ARM::t2MOVsi))
8313       isNarrow = true;
8314     MCInst TmpInst;
8315     unsigned newOpc;
8316     switch(ARM_AM::getSORegShOp(Inst.getOperand(2).getImm())) {
8317     default: llvm_unreachable("unexpected opcode!");
8318     case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRri : ARM::t2ASRri; break;
8319     case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRri : ARM::t2LSRri; break;
8320     case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLri : ARM::t2LSLri; break;
8321     case ARM_AM::ror: newOpc = ARM::t2RORri; isNarrow = false; break;
8322     case ARM_AM::rrx: isNarrow = false; newOpc = ARM::t2RRX; break;
8323     }
8324     unsigned Amount = ARM_AM::getSORegOffset(Inst.getOperand(2).getImm());
8325     if (Amount == 32) Amount = 0;
8326     TmpInst.setOpcode(newOpc);
8327     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8328     if (isNarrow)
8329       TmpInst.addOperand(MCOperand::createReg(
8330           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
8331     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8332     if (newOpc != ARM::t2RRX)
8333       TmpInst.addOperand(MCOperand::createImm(Amount));
8334     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8335     TmpInst.addOperand(Inst.getOperand(4));
8336     if (!isNarrow)
8337       TmpInst.addOperand(MCOperand::createReg(
8338           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
8339     Inst = TmpInst;
8340     return true;
8341   }
8342   // Handle the ARM mode MOV complex aliases.
8343   case ARM::ASRr:
8344   case ARM::LSRr:
8345   case ARM::LSLr:
8346   case ARM::RORr: {
8347     ARM_AM::ShiftOpc ShiftTy;
8348     switch(Inst.getOpcode()) {
8349     default: llvm_unreachable("unexpected opcode!");
8350     case ARM::ASRr: ShiftTy = ARM_AM::asr; break;
8351     case ARM::LSRr: ShiftTy = ARM_AM::lsr; break;
8352     case ARM::LSLr: ShiftTy = ARM_AM::lsl; break;
8353     case ARM::RORr: ShiftTy = ARM_AM::ror; break;
8354     }
8355     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, 0);
8356     MCInst TmpInst;
8357     TmpInst.setOpcode(ARM::MOVsr);
8358     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8359     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8360     TmpInst.addOperand(Inst.getOperand(2)); // Rm
8361     TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8362     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8363     TmpInst.addOperand(Inst.getOperand(4));
8364     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
8365     Inst = TmpInst;
8366     return true;
8367   }
8368   case ARM::ASRi:
8369   case ARM::LSRi:
8370   case ARM::LSLi:
8371   case ARM::RORi: {
8372     ARM_AM::ShiftOpc ShiftTy;
8373     switch(Inst.getOpcode()) {
8374     default: llvm_unreachable("unexpected opcode!");
8375     case ARM::ASRi: ShiftTy = ARM_AM::asr; break;
8376     case ARM::LSRi: ShiftTy = ARM_AM::lsr; break;
8377     case ARM::LSLi: ShiftTy = ARM_AM::lsl; break;
8378     case ARM::RORi: ShiftTy = ARM_AM::ror; break;
8379     }
8380     // A shift by zero is a plain MOVr, not a MOVsi.
8381     unsigned Amt = Inst.getOperand(2).getImm();
8382     unsigned Opc = Amt == 0 ? ARM::MOVr : ARM::MOVsi;
8383     // A shift by 32 should be encoded as 0 when permitted
8384     if (Amt == 32 && (ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr))
8385       Amt = 0;
8386     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, Amt);
8387     MCInst TmpInst;
8388     TmpInst.setOpcode(Opc);
8389     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8390     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8391     if (Opc == ARM::MOVsi)
8392       TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8393     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
8394     TmpInst.addOperand(Inst.getOperand(4));
8395     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
8396     Inst = TmpInst;
8397     return true;
8398   }
8399   case ARM::RRXi: {
8400     unsigned Shifter = ARM_AM::getSORegOpc(ARM_AM::rrx, 0);
8401     MCInst TmpInst;
8402     TmpInst.setOpcode(ARM::MOVsi);
8403     TmpInst.addOperand(Inst.getOperand(0)); // Rd
8404     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8405     TmpInst.addOperand(MCOperand::createImm(Shifter)); // Shift value and ty
8406     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8407     TmpInst.addOperand(Inst.getOperand(3));
8408     TmpInst.addOperand(Inst.getOperand(4)); // cc_out
8409     Inst = TmpInst;
8410     return true;
8411   }
8412   case ARM::t2LDMIA_UPD: {
8413     // If this is a load of a single register, then we should use
8414     // a post-indexed LDR instruction instead, per the ARM ARM.
8415     if (Inst.getNumOperands() != 5)
8416       return false;
8417     MCInst TmpInst;
8418     TmpInst.setOpcode(ARM::t2LDR_POST);
8419     TmpInst.addOperand(Inst.getOperand(4)); // Rt
8420     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8421     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8422     TmpInst.addOperand(MCOperand::createImm(4));
8423     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8424     TmpInst.addOperand(Inst.getOperand(3));
8425     Inst = TmpInst;
8426     return true;
8427   }
8428   case ARM::t2STMDB_UPD: {
8429     // If this is a store of a single register, then we should use
8430     // a pre-indexed STR instruction instead, per the ARM ARM.
8431     if (Inst.getNumOperands() != 5)
8432       return false;
8433     MCInst TmpInst;
8434     TmpInst.setOpcode(ARM::t2STR_PRE);
8435     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8436     TmpInst.addOperand(Inst.getOperand(4)); // Rt
8437     TmpInst.addOperand(Inst.getOperand(1)); // Rn
8438     TmpInst.addOperand(MCOperand::createImm(-4));
8439     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8440     TmpInst.addOperand(Inst.getOperand(3));
8441     Inst = TmpInst;
8442     return true;
8443   }
8444   case ARM::LDMIA_UPD:
8445     // If this is a load of a single register via a 'pop', then we should use
8446     // a post-indexed LDR instruction instead, per the ARM ARM.
8447     if (static_cast<ARMOperand &>(*Operands[0]).getToken() == "pop" &&
8448         Inst.getNumOperands() == 5) {
8449       MCInst TmpInst;
8450       TmpInst.setOpcode(ARM::LDR_POST_IMM);
8451       TmpInst.addOperand(Inst.getOperand(4)); // Rt
8452       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8453       TmpInst.addOperand(Inst.getOperand(1)); // Rn
8454       TmpInst.addOperand(MCOperand::createReg(0));  // am2offset
8455       TmpInst.addOperand(MCOperand::createImm(4));
8456       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8457       TmpInst.addOperand(Inst.getOperand(3));
8458       Inst = TmpInst;
8459       return true;
8460     }
8461     break;
8462   case ARM::STMDB_UPD:
8463     // If this is a store of a single register via a 'push', then we should use
8464     // a pre-indexed STR instruction instead, per the ARM ARM.
8465     if (static_cast<ARMOperand &>(*Operands[0]).getToken() == "push" &&
8466         Inst.getNumOperands() == 5) {
8467       MCInst TmpInst;
8468       TmpInst.setOpcode(ARM::STR_PRE_IMM);
8469       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
8470       TmpInst.addOperand(Inst.getOperand(4)); // Rt
8471       TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
8472       TmpInst.addOperand(MCOperand::createImm(-4));
8473       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
8474       TmpInst.addOperand(Inst.getOperand(3));
8475       Inst = TmpInst;
8476     }
8477     break;
8478   case ARM::t2ADDri12:
8479     // If the immediate fits for encoding T3 (t2ADDri) and the generic "add"
8480     // mnemonic was used (not "addw"), encoding T3 is preferred.
8481     if (static_cast<ARMOperand &>(*Operands[0]).getToken() != "add" ||
8482         ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
8483       break;
8484     Inst.setOpcode(ARM::t2ADDri);
8485     Inst.addOperand(MCOperand::createReg(0)); // cc_out
8486     break;
8487   case ARM::t2SUBri12:
8488     // If the immediate fits for encoding T3 (t2SUBri) and the generic "sub"
8489     // mnemonic was used (not "subw"), encoding T3 is preferred.
8490     if (static_cast<ARMOperand &>(*Operands[0]).getToken() != "sub" ||
8491         ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
8492       break;
8493     Inst.setOpcode(ARM::t2SUBri);
8494     Inst.addOperand(MCOperand::createReg(0)); // cc_out
8495     break;
8496   case ARM::tADDi8:
8497     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
8498     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
8499     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
8500     // to encoding T1 if <Rd> is omitted."
8501     if ((unsigned)Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
8502       Inst.setOpcode(ARM::tADDi3);
8503       return true;
8504     }
8505     break;
8506   case ARM::tSUBi8:
8507     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
8508     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
8509     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
8510     // to encoding T1 if <Rd> is omitted."
8511     if ((unsigned)Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
8512       Inst.setOpcode(ARM::tSUBi3);
8513       return true;
8514     }
8515     break;
8516   case ARM::t2ADDri:
8517   case ARM::t2SUBri: {
8518     // If the destination and first source operand are the same, and
8519     // the flags are compatible with the current IT status, use encoding T2
8520     // instead of T3. For compatibility with the system 'as'. Make sure the
8521     // wide encoding wasn't explicit.
8522     if (Inst.getOperand(0).getReg() != Inst.getOperand(1).getReg() ||
8523         !isARMLowRegister(Inst.getOperand(0).getReg()) ||
8524         (unsigned)Inst.getOperand(2).getImm() > 255 ||
8525         ((!inITBlock() && Inst.getOperand(5).getReg() != ARM::CPSR) ||
8526          (inITBlock() && Inst.getOperand(5).getReg() != 0)) ||
8527         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8528          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".w"))
8529       break;
8530     MCInst TmpInst;
8531     TmpInst.setOpcode(Inst.getOpcode() == ARM::t2ADDri ?
8532                       ARM::tADDi8 : ARM::tSUBi8);
8533     TmpInst.addOperand(Inst.getOperand(0));
8534     TmpInst.addOperand(Inst.getOperand(5));
8535     TmpInst.addOperand(Inst.getOperand(0));
8536     TmpInst.addOperand(Inst.getOperand(2));
8537     TmpInst.addOperand(Inst.getOperand(3));
8538     TmpInst.addOperand(Inst.getOperand(4));
8539     Inst = TmpInst;
8540     return true;
8541   }
8542   case ARM::t2ADDrr: {
8543     // If the destination and first source operand are the same, and
8544     // there's no setting of the flags, use encoding T2 instead of T3.
8545     // Note that this is only for ADD, not SUB. This mirrors the system
8546     // 'as' behaviour.  Also take advantage of ADD being commutative.
8547     // Make sure the wide encoding wasn't explicit.
8548     bool Swap = false;
8549     auto DestReg = Inst.getOperand(0).getReg();
8550     bool Transform = DestReg == Inst.getOperand(1).getReg();
8551     if (!Transform && DestReg == Inst.getOperand(2).getReg()) {
8552       Transform = true;
8553       Swap = true;
8554     }
8555     if (!Transform ||
8556         Inst.getOperand(5).getReg() != 0 ||
8557         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8558          static_cast<ARMOperand &>(*Operands[3]).getToken() == ".w"))
8559       break;
8560     MCInst TmpInst;
8561     TmpInst.setOpcode(ARM::tADDhirr);
8562     TmpInst.addOperand(Inst.getOperand(0));
8563     TmpInst.addOperand(Inst.getOperand(0));
8564     TmpInst.addOperand(Inst.getOperand(Swap ? 1 : 2));
8565     TmpInst.addOperand(Inst.getOperand(3));
8566     TmpInst.addOperand(Inst.getOperand(4));
8567     Inst = TmpInst;
8568     return true;
8569   }
8570   case ARM::tADDrSP: {
8571     // If the non-SP source operand and the destination operand are not the
8572     // same, we need to use the 32-bit encoding if it's available.
8573     if (Inst.getOperand(0).getReg() != Inst.getOperand(2).getReg()) {
8574       Inst.setOpcode(ARM::t2ADDrr);
8575       Inst.addOperand(MCOperand::createReg(0)); // cc_out
8576       return true;
8577     }
8578     break;
8579   }
8580   case ARM::tB:
8581     // A Thumb conditional branch outside of an IT block is a tBcc.
8582     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()) {
8583       Inst.setOpcode(ARM::tBcc);
8584       return true;
8585     }
8586     break;
8587   case ARM::t2B:
8588     // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
8589     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()){
8590       Inst.setOpcode(ARM::t2Bcc);
8591       return true;
8592     }
8593     break;
8594   case ARM::t2Bcc:
8595     // If the conditional is AL or we're in an IT block, we really want t2B.
8596     if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock()) {
8597       Inst.setOpcode(ARM::t2B);
8598       return true;
8599     }
8600     break;
8601   case ARM::tBcc:
8602     // If the conditional is AL, we really want tB.
8603     if (Inst.getOperand(1).getImm() == ARMCC::AL) {
8604       Inst.setOpcode(ARM::tB);
8605       return true;
8606     }
8607     break;
8608   case ARM::tLDMIA: {
8609     // If the register list contains any high registers, or if the writeback
8610     // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
8611     // instead if we're in Thumb2. Otherwise, this should have generated
8612     // an error in validateInstruction().
8613     unsigned Rn = Inst.getOperand(0).getReg();
8614     bool hasWritebackToken =
8615         (static_cast<ARMOperand &>(*Operands[3]).isToken() &&
8616          static_cast<ARMOperand &>(*Operands[3]).getToken() == "!");
8617     bool listContainsBase;
8618     if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
8619         (!listContainsBase && !hasWritebackToken) ||
8620         (listContainsBase && hasWritebackToken)) {
8621       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
8622       assert (isThumbTwo());
8623       Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
8624       // If we're switching to the updating version, we need to insert
8625       // the writeback tied operand.
8626       if (hasWritebackToken)
8627         Inst.insert(Inst.begin(),
8628                     MCOperand::createReg(Inst.getOperand(0).getReg()));
8629       return true;
8630     }
8631     break;
8632   }
8633   case ARM::tSTMIA_UPD: {
8634     // If the register list contains any high registers, we need to use
8635     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
8636     // should have generated an error in validateInstruction().
8637     unsigned Rn = Inst.getOperand(0).getReg();
8638     bool listContainsBase;
8639     if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
8640       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
8641       assert (isThumbTwo());
8642       Inst.setOpcode(ARM::t2STMIA_UPD);
8643       return true;
8644     }
8645     break;
8646   }
8647   case ARM::tPOP: {
8648     bool listContainsBase;
8649     // If the register list contains any high registers, we need to use
8650     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
8651     // should have generated an error in validateInstruction().
8652     if (!checkLowRegisterList(Inst, 2, 0, ARM::PC, listContainsBase))
8653       return false;
8654     assert (isThumbTwo());
8655     Inst.setOpcode(ARM::t2LDMIA_UPD);
8656     // Add the base register and writeback operands.
8657     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8658     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8659     return true;
8660   }
8661   case ARM::tPUSH: {
8662     bool listContainsBase;
8663     if (!checkLowRegisterList(Inst, 2, 0, ARM::LR, listContainsBase))
8664       return false;
8665     assert (isThumbTwo());
8666     Inst.setOpcode(ARM::t2STMDB_UPD);
8667     // Add the base register and writeback operands.
8668     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8669     Inst.insert(Inst.begin(), MCOperand::createReg(ARM::SP));
8670     return true;
8671   }
8672   case ARM::t2MOVi: {
8673     // If we can use the 16-bit encoding and the user didn't explicitly
8674     // request the 32-bit variant, transform it here.
8675     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8676         (unsigned)Inst.getOperand(1).getImm() <= 255 &&
8677         ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
8678           Inst.getOperand(4).getReg() == ARM::CPSR) ||
8679          (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
8680         (!static_cast<ARMOperand &>(*Operands[2]).isToken() ||
8681          static_cast<ARMOperand &>(*Operands[2]).getToken() != ".w")) {
8682       // The operands aren't in the same order for tMOVi8...
8683       MCInst TmpInst;
8684       TmpInst.setOpcode(ARM::tMOVi8);
8685       TmpInst.addOperand(Inst.getOperand(0));
8686       TmpInst.addOperand(Inst.getOperand(4));
8687       TmpInst.addOperand(Inst.getOperand(1));
8688       TmpInst.addOperand(Inst.getOperand(2));
8689       TmpInst.addOperand(Inst.getOperand(3));
8690       Inst = TmpInst;
8691       return true;
8692     }
8693     break;
8694   }
8695   case ARM::t2MOVr: {
8696     // If we can use the 16-bit encoding and the user didn't explicitly
8697     // request the 32-bit variant, transform it here.
8698     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8699         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8700         Inst.getOperand(2).getImm() == ARMCC::AL &&
8701         Inst.getOperand(4).getReg() == ARM::CPSR &&
8702         (!static_cast<ARMOperand &>(*Operands[2]).isToken() ||
8703          static_cast<ARMOperand &>(*Operands[2]).getToken() != ".w")) {
8704       // The operands aren't the same for tMOV[S]r... (no cc_out)
8705       MCInst TmpInst;
8706       TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
8707       TmpInst.addOperand(Inst.getOperand(0));
8708       TmpInst.addOperand(Inst.getOperand(1));
8709       TmpInst.addOperand(Inst.getOperand(2));
8710       TmpInst.addOperand(Inst.getOperand(3));
8711       Inst = TmpInst;
8712       return true;
8713     }
8714     break;
8715   }
8716   case ARM::t2SXTH:
8717   case ARM::t2SXTB:
8718   case ARM::t2UXTH:
8719   case ARM::t2UXTB: {
8720     // If we can use the 16-bit encoding and the user didn't explicitly
8721     // request the 32-bit variant, transform it here.
8722     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
8723         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8724         Inst.getOperand(2).getImm() == 0 &&
8725         (!static_cast<ARMOperand &>(*Operands[2]).isToken() ||
8726          static_cast<ARMOperand &>(*Operands[2]).getToken() != ".w")) {
8727       unsigned NewOpc;
8728       switch (Inst.getOpcode()) {
8729       default: llvm_unreachable("Illegal opcode!");
8730       case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
8731       case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
8732       case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
8733       case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
8734       }
8735       // The operands aren't the same for thumb1 (no rotate operand).
8736       MCInst TmpInst;
8737       TmpInst.setOpcode(NewOpc);
8738       TmpInst.addOperand(Inst.getOperand(0));
8739       TmpInst.addOperand(Inst.getOperand(1));
8740       TmpInst.addOperand(Inst.getOperand(3));
8741       TmpInst.addOperand(Inst.getOperand(4));
8742       Inst = TmpInst;
8743       return true;
8744     }
8745     break;
8746   }
8747   case ARM::MOVsi: {
8748     ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(2).getImm());
8749     // rrx shifts and asr/lsr of #32 is encoded as 0
8750     if (SOpc == ARM_AM::rrx || SOpc == ARM_AM::asr || SOpc == ARM_AM::lsr)
8751       return false;
8752     if (ARM_AM::getSORegOffset(Inst.getOperand(2).getImm()) == 0) {
8753       // Shifting by zero is accepted as a vanilla 'MOVr'
8754       MCInst TmpInst;
8755       TmpInst.setOpcode(ARM::MOVr);
8756       TmpInst.addOperand(Inst.getOperand(0));
8757       TmpInst.addOperand(Inst.getOperand(1));
8758       TmpInst.addOperand(Inst.getOperand(3));
8759       TmpInst.addOperand(Inst.getOperand(4));
8760       TmpInst.addOperand(Inst.getOperand(5));
8761       Inst = TmpInst;
8762       return true;
8763     }
8764     return false;
8765   }
8766   case ARM::ANDrsi:
8767   case ARM::ORRrsi:
8768   case ARM::EORrsi:
8769   case ARM::BICrsi:
8770   case ARM::SUBrsi:
8771   case ARM::ADDrsi: {
8772     unsigned newOpc;
8773     ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(3).getImm());
8774     if (SOpc == ARM_AM::rrx) return false;
8775     switch (Inst.getOpcode()) {
8776     default: llvm_unreachable("unexpected opcode!");
8777     case ARM::ANDrsi: newOpc = ARM::ANDrr; break;
8778     case ARM::ORRrsi: newOpc = ARM::ORRrr; break;
8779     case ARM::EORrsi: newOpc = ARM::EORrr; break;
8780     case ARM::BICrsi: newOpc = ARM::BICrr; break;
8781     case ARM::SUBrsi: newOpc = ARM::SUBrr; break;
8782     case ARM::ADDrsi: newOpc = ARM::ADDrr; break;
8783     }
8784     // If the shift is by zero, use the non-shifted instruction definition.
8785     // The exception is for right shifts, where 0 == 32
8786     if (ARM_AM::getSORegOffset(Inst.getOperand(3).getImm()) == 0 &&
8787         !(SOpc == ARM_AM::lsr || SOpc == ARM_AM::asr)) {
8788       MCInst TmpInst;
8789       TmpInst.setOpcode(newOpc);
8790       TmpInst.addOperand(Inst.getOperand(0));
8791       TmpInst.addOperand(Inst.getOperand(1));
8792       TmpInst.addOperand(Inst.getOperand(2));
8793       TmpInst.addOperand(Inst.getOperand(4));
8794       TmpInst.addOperand(Inst.getOperand(5));
8795       TmpInst.addOperand(Inst.getOperand(6));
8796       Inst = TmpInst;
8797       return true;
8798     }
8799     return false;
8800   }
8801   case ARM::ITasm:
8802   case ARM::t2IT: {
8803     MCOperand &MO = Inst.getOperand(1);
8804     unsigned Mask = MO.getImm();
8805     ARMCC::CondCodes Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
8806 
8807     // Set up the IT block state according to the IT instruction we just
8808     // matched.
8809     assert(!inITBlock() && "nested IT blocks?!");
8810     startExplicitITBlock(Cond, Mask);
8811     MO.setImm(getITMaskEncoding());
8812     break;
8813   }
8814   case ARM::t2LSLrr:
8815   case ARM::t2LSRrr:
8816   case ARM::t2ASRrr:
8817   case ARM::t2SBCrr:
8818   case ARM::t2RORrr:
8819   case ARM::t2BICrr:
8820   {
8821     // Assemblers should use the narrow encodings of these instructions when permissible.
8822     if ((isARMLowRegister(Inst.getOperand(1).getReg()) &&
8823          isARMLowRegister(Inst.getOperand(2).getReg())) &&
8824         Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
8825         ((!inITBlock() && Inst.getOperand(5).getReg() == ARM::CPSR) ||
8826          (inITBlock() && Inst.getOperand(5).getReg() != ARM::CPSR)) &&
8827         (!static_cast<ARMOperand &>(*Operands[3]).isToken() ||
8828          !static_cast<ARMOperand &>(*Operands[3]).getToken().equals_lower(
8829              ".w"))) {
8830       unsigned NewOpc;
8831       switch (Inst.getOpcode()) {
8832         default: llvm_unreachable("unexpected opcode");
8833         case ARM::t2LSLrr: NewOpc = ARM::tLSLrr; break;
8834         case ARM::t2LSRrr: NewOpc = ARM::tLSRrr; break;
8835         case ARM::t2ASRrr: NewOpc = ARM::tASRrr; break;
8836         case ARM::t2SBCrr: NewOpc = ARM::tSBC; break;
8837         case ARM::t2RORrr: NewOpc = ARM::tROR; break;
8838         case ARM::t2BICrr: NewOpc = ARM::tBIC; break;
8839       }
8840       MCInst TmpInst;
8841       TmpInst.setOpcode(NewOpc);
8842       TmpInst.addOperand(Inst.getOperand(0));
8843       TmpInst.addOperand(Inst.getOperand(5));
8844       TmpInst.addOperand(Inst.getOperand(1));
8845       TmpInst.addOperand(Inst.getOperand(2));
8846       TmpInst.addOperand(Inst.getOperand(3));
8847       TmpInst.addOperand(Inst.getOperand(4));
8848       Inst = TmpInst;
8849       return true;
8850     }
8851     return false;
8852   }
8853   case ARM::t2ANDrr:
8854   case ARM::t2EORrr:
8855   case ARM::t2ADCrr:
8856   case ARM::t2ORRrr:
8857   {
8858     // Assemblers should use the narrow encodings of these instructions when permissible.
8859     // These instructions are special in that they are commutable, so shorter encodings
8860     // are available more often.
8861     if ((isARMLowRegister(Inst.getOperand(1).getReg()) &&
8862          isARMLowRegister(Inst.getOperand(2).getReg())) &&
8863         (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() ||
8864          Inst.getOperand(0).getReg() == Inst.getOperand(2).getReg()) &&
8865         ((!inITBlock() && Inst.getOperand(5).getReg() == ARM::CPSR) ||
8866          (inITBlock() && Inst.getOperand(5).getReg() != ARM::CPSR)) &&
8867         (!static_cast<ARMOperand &>(*Operands[3]).isToken() ||
8868          !static_cast<ARMOperand &>(*Operands[3]).getToken().equals_lower(
8869              ".w"))) {
8870       unsigned NewOpc;
8871       switch (Inst.getOpcode()) {
8872         default: llvm_unreachable("unexpected opcode");
8873         case ARM::t2ADCrr: NewOpc = ARM::tADC; break;
8874         case ARM::t2ANDrr: NewOpc = ARM::tAND; break;
8875         case ARM::t2EORrr: NewOpc = ARM::tEOR; break;
8876         case ARM::t2ORRrr: NewOpc = ARM::tORR; break;
8877       }
8878       MCInst TmpInst;
8879       TmpInst.setOpcode(NewOpc);
8880       TmpInst.addOperand(Inst.getOperand(0));
8881       TmpInst.addOperand(Inst.getOperand(5));
8882       if (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()) {
8883         TmpInst.addOperand(Inst.getOperand(1));
8884         TmpInst.addOperand(Inst.getOperand(2));
8885       } else {
8886         TmpInst.addOperand(Inst.getOperand(2));
8887         TmpInst.addOperand(Inst.getOperand(1));
8888       }
8889       TmpInst.addOperand(Inst.getOperand(3));
8890       TmpInst.addOperand(Inst.getOperand(4));
8891       Inst = TmpInst;
8892       return true;
8893     }
8894     return false;
8895   }
8896   }
8897   return false;
8898 }
8899 
8900 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
8901   // 16-bit thumb arithmetic instructions either require or preclude the 'S'
8902   // suffix depending on whether they're in an IT block or not.
8903   unsigned Opc = Inst.getOpcode();
8904   const MCInstrDesc &MCID = MII.get(Opc);
8905   if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
8906     assert(MCID.hasOptionalDef() &&
8907            "optionally flag setting instruction missing optional def operand");
8908     assert(MCID.NumOperands == Inst.getNumOperands() &&
8909            "operand count mismatch!");
8910     // Find the optional-def operand (cc_out).
8911     unsigned OpNo;
8912     for (OpNo = 0;
8913          !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
8914          ++OpNo)
8915       ;
8916     // If we're parsing Thumb1, reject it completely.
8917     if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
8918       return Match_MnemonicFail;
8919     // If we're parsing Thumb2, which form is legal depends on whether we're
8920     // in an IT block.
8921     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
8922         !inITBlock())
8923       return Match_RequiresITBlock;
8924     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
8925         inITBlock())
8926       return Match_RequiresNotITBlock;
8927   } else if (isThumbOne()) {
8928     // Some high-register supporting Thumb1 encodings only allow both registers
8929     // to be from r0-r7 when in Thumb2.
8930     if (Opc == ARM::tADDhirr && !hasV6MOps() &&
8931         isARMLowRegister(Inst.getOperand(1).getReg()) &&
8932         isARMLowRegister(Inst.getOperand(2).getReg()))
8933       return Match_RequiresThumb2;
8934     // Others only require ARMv6 or later.
8935     else if (Opc == ARM::tMOVr && !hasV6Ops() &&
8936              isARMLowRegister(Inst.getOperand(0).getReg()) &&
8937              isARMLowRegister(Inst.getOperand(1).getReg()))
8938       return Match_RequiresV6;
8939   }
8940 
8941   for (unsigned I = 0; I < MCID.NumOperands; ++I)
8942     if (MCID.OpInfo[I].RegClass == ARM::rGPRRegClassID) {
8943       // rGPRRegClass excludes PC, and also excluded SP before ARMv8
8944       if ((Inst.getOperand(I).getReg() == ARM::SP) && !hasV8Ops())
8945         return Match_RequiresV8;
8946       else if (Inst.getOperand(I).getReg() == ARM::PC)
8947         return Match_InvalidOperand;
8948     }
8949 
8950   return Match_Success;
8951 }
8952 
8953 namespace llvm {
8954 template <> inline bool IsCPSRDead<MCInst>(MCInst *Instr) {
8955   return true; // In an assembly source, no need to second-guess
8956 }
8957 }
8958 
8959 // Returns true if Inst is unpredictable if it is in and IT block, but is not
8960 // the last instruction in the block.
8961 bool ARMAsmParser::isITBlockTerminator(MCInst &Inst) const {
8962   const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
8963 
8964   // All branch & call instructions terminate IT blocks.
8965   if (MCID.isTerminator() || MCID.isCall() || MCID.isReturn() ||
8966       MCID.isBranch() || MCID.isIndirectBranch())
8967     return true;
8968 
8969   // Any arithmetic instruction which writes to the PC also terminates the IT
8970   // block.
8971   for (unsigned OpIdx = 0; OpIdx < MCID.getNumDefs(); ++OpIdx) {
8972     MCOperand &Op = Inst.getOperand(OpIdx);
8973     if (Op.isReg() && Op.getReg() == ARM::PC)
8974       return true;
8975   }
8976 
8977   if (MCID.hasImplicitDefOfPhysReg(ARM::PC, MRI))
8978     return true;
8979 
8980   // Instructions with variable operand lists, which write to the variable
8981   // operands. We only care about Thumb instructions here, as ARM instructions
8982   // obviously can't be in an IT block.
8983   switch (Inst.getOpcode()) {
8984   case ARM::t2LDMIA:
8985   case ARM::t2LDMIA_UPD:
8986   case ARM::t2LDMDB:
8987   case ARM::t2LDMDB_UPD:
8988     if (listContainsReg(Inst, 3, ARM::PC))
8989       return true;
8990     break;
8991   case ARM::tPOP:
8992     if (listContainsReg(Inst, 2, ARM::PC))
8993       return true;
8994     break;
8995   }
8996 
8997   return false;
8998 }
8999 
9000 unsigned ARMAsmParser::MatchInstruction(OperandVector &Operands, MCInst &Inst,
9001                                           uint64_t &ErrorInfo,
9002                                           bool MatchingInlineAsm,
9003                                           bool &EmitInITBlock,
9004                                           MCStreamer &Out) {
9005   // If we can't use an implicit IT block here, just match as normal.
9006   if (inExplicitITBlock() || !isThumbTwo() || !useImplicitITThumb())
9007     return MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
9008 
9009   // Try to match the instruction in an extension of the current IT block (if
9010   // there is one).
9011   if (inImplicitITBlock()) {
9012     extendImplicitITBlock(ITState.Cond);
9013     if (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm) ==
9014             Match_Success) {
9015       // The match succeded, but we still have to check that the instruction is
9016       // valid in this implicit IT block.
9017       const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
9018       if (MCID.isPredicable()) {
9019         ARMCC::CondCodes InstCond =
9020             (ARMCC::CondCodes)Inst.getOperand(MCID.findFirstPredOperandIdx())
9021                 .getImm();
9022         ARMCC::CondCodes ITCond = currentITCond();
9023         if (InstCond == ITCond) {
9024           EmitInITBlock = true;
9025           return Match_Success;
9026         } else if (InstCond == ARMCC::getOppositeCondition(ITCond)) {
9027           invertCurrentITCondition();
9028           EmitInITBlock = true;
9029           return Match_Success;
9030         }
9031       }
9032     }
9033     rewindImplicitITPosition();
9034   }
9035 
9036   // Finish the current IT block, and try to match outside any IT block.
9037   flushPendingInstructions(Out);
9038   unsigned PlainMatchResult =
9039       MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
9040   if (PlainMatchResult == Match_Success) {
9041     const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
9042     if (MCID.isPredicable()) {
9043       ARMCC::CondCodes InstCond =
9044           (ARMCC::CondCodes)Inst.getOperand(MCID.findFirstPredOperandIdx())
9045               .getImm();
9046       // Some forms of the branch instruction have their own condition code
9047       // fields, so can be conditionally executed without an IT block.
9048       if (Inst.getOpcode() == ARM::tBcc || Inst.getOpcode() == ARM::t2Bcc) {
9049         EmitInITBlock = false;
9050         return Match_Success;
9051       }
9052       if (InstCond == ARMCC::AL) {
9053         EmitInITBlock = false;
9054         return Match_Success;
9055       }
9056     } else {
9057       EmitInITBlock = false;
9058       return Match_Success;
9059     }
9060   }
9061 
9062   // Try to match in a new IT block. The matcher doesn't check the actual
9063   // condition, so we create an IT block with a dummy condition, and fix it up
9064   // once we know the actual condition.
9065   startImplicitITBlock();
9066   if (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm) ==
9067       Match_Success) {
9068     const MCInstrDesc &MCID = MII.get(Inst.getOpcode());
9069     if (MCID.isPredicable()) {
9070       ITState.Cond =
9071           (ARMCC::CondCodes)Inst.getOperand(MCID.findFirstPredOperandIdx())
9072               .getImm();
9073       EmitInITBlock = true;
9074       return Match_Success;
9075     }
9076   }
9077   discardImplicitITBlock();
9078 
9079   // If none of these succeed, return the error we got when trying to match
9080   // outside any IT blocks.
9081   EmitInITBlock = false;
9082   return PlainMatchResult;
9083 }
9084 
9085 static const char *getSubtargetFeatureName(uint64_t Val);
9086 bool ARMAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
9087                                            OperandVector &Operands,
9088                                            MCStreamer &Out, uint64_t &ErrorInfo,
9089                                            bool MatchingInlineAsm) {
9090   MCInst Inst;
9091   unsigned MatchResult;
9092   bool PendConditionalInstruction = false;
9093 
9094   MatchResult = MatchInstruction(Operands, Inst, ErrorInfo, MatchingInlineAsm,
9095                                  PendConditionalInstruction, Out);
9096 
9097   switch (MatchResult) {
9098   case Match_Success:
9099     // Context sensitive operand constraints aren't handled by the matcher,
9100     // so check them here.
9101     if (validateInstruction(Inst, Operands)) {
9102       // Still progress the IT block, otherwise one wrong condition causes
9103       // nasty cascading errors.
9104       forwardITPosition();
9105       return true;
9106     }
9107 
9108     { // processInstruction() updates inITBlock state, we need to save it away
9109       bool wasInITBlock = inITBlock();
9110 
9111       // Some instructions need post-processing to, for example, tweak which
9112       // encoding is selected. Loop on it while changes happen so the
9113       // individual transformations can chain off each other. E.g.,
9114       // tPOP(r8)->t2LDMIA_UPD(sp,r8)->t2STR_POST(sp,r8)
9115       while (processInstruction(Inst, Operands, Out))
9116         ;
9117 
9118       // Only after the instruction is fully processed, we can validate it
9119       if (wasInITBlock && hasV8Ops() && isThumb() &&
9120           !isV8EligibleForIT(&Inst)) {
9121         Warning(IDLoc, "deprecated instruction in IT block");
9122       }
9123     }
9124 
9125     // Only move forward at the very end so that everything in validate
9126     // and process gets a consistent answer about whether we're in an IT
9127     // block.
9128     forwardITPosition();
9129 
9130     // ITasm is an ARM mode pseudo-instruction that just sets the ITblock and
9131     // doesn't actually encode.
9132     if (Inst.getOpcode() == ARM::ITasm)
9133       return false;
9134 
9135     Inst.setLoc(IDLoc);
9136     if (PendConditionalInstruction) {
9137       PendingConditionalInsts.push_back(Inst);
9138       if (isITBlockFull() || isITBlockTerminator(Inst))
9139         flushPendingInstructions(Out);
9140     } else {
9141       Out.EmitInstruction(Inst, getSTI());
9142     }
9143     return false;
9144   case Match_MissingFeature: {
9145     assert(ErrorInfo && "Unknown missing feature!");
9146     // Special case the error message for the very common case where only
9147     // a single subtarget feature is missing (Thumb vs. ARM, e.g.).
9148     std::string Msg = "instruction requires:";
9149     uint64_t Mask = 1;
9150     for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
9151       if (ErrorInfo & Mask) {
9152         Msg += " ";
9153         Msg += getSubtargetFeatureName(ErrorInfo & Mask);
9154       }
9155       Mask <<= 1;
9156     }
9157     return Error(IDLoc, Msg);
9158   }
9159   case Match_InvalidOperand: {
9160     SMLoc ErrorLoc = IDLoc;
9161     if (ErrorInfo != ~0ULL) {
9162       if (ErrorInfo >= Operands.size())
9163         return Error(IDLoc, "too few operands for instruction");
9164 
9165       ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
9166       if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
9167     }
9168 
9169     return Error(ErrorLoc, "invalid operand for instruction");
9170   }
9171   case Match_MnemonicFail:
9172     return Error(IDLoc, "invalid instruction",
9173                  ((ARMOperand &)*Operands[0]).getLocRange());
9174   case Match_RequiresNotITBlock:
9175     return Error(IDLoc, "flag setting instruction only valid outside IT block");
9176   case Match_RequiresITBlock:
9177     return Error(IDLoc, "instruction only valid inside IT block");
9178   case Match_RequiresV6:
9179     return Error(IDLoc, "instruction variant requires ARMv6 or later");
9180   case Match_RequiresThumb2:
9181     return Error(IDLoc, "instruction variant requires Thumb2");
9182   case Match_RequiresV8:
9183     return Error(IDLoc, "instruction variant requires ARMv8 or later");
9184   case Match_ImmRange0_15: {
9185     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
9186     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
9187     return Error(ErrorLoc, "immediate operand must be in the range [0,15]");
9188   }
9189   case Match_ImmRange0_239: {
9190     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getStartLoc();
9191     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
9192     return Error(ErrorLoc, "immediate operand must be in the range [0,239]");
9193   }
9194   case Match_AlignedMemoryRequiresNone:
9195   case Match_DupAlignedMemoryRequiresNone:
9196   case Match_AlignedMemoryRequires16:
9197   case Match_DupAlignedMemoryRequires16:
9198   case Match_AlignedMemoryRequires32:
9199   case Match_DupAlignedMemoryRequires32:
9200   case Match_AlignedMemoryRequires64:
9201   case Match_DupAlignedMemoryRequires64:
9202   case Match_AlignedMemoryRequires64or128:
9203   case Match_DupAlignedMemoryRequires64or128:
9204   case Match_AlignedMemoryRequires64or128or256:
9205   {
9206     SMLoc ErrorLoc = ((ARMOperand &)*Operands[ErrorInfo]).getAlignmentLoc();
9207     if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
9208     switch (MatchResult) {
9209       default:
9210         llvm_unreachable("Missing Match_Aligned type");
9211       case Match_AlignedMemoryRequiresNone:
9212       case Match_DupAlignedMemoryRequiresNone:
9213         return Error(ErrorLoc, "alignment must be omitted");
9214       case Match_AlignedMemoryRequires16:
9215       case Match_DupAlignedMemoryRequires16:
9216         return Error(ErrorLoc, "alignment must be 16 or omitted");
9217       case Match_AlignedMemoryRequires32:
9218       case Match_DupAlignedMemoryRequires32:
9219         return Error(ErrorLoc, "alignment must be 32 or omitted");
9220       case Match_AlignedMemoryRequires64:
9221       case Match_DupAlignedMemoryRequires64:
9222         return Error(ErrorLoc, "alignment must be 64 or omitted");
9223       case Match_AlignedMemoryRequires64or128:
9224       case Match_DupAlignedMemoryRequires64or128:
9225         return Error(ErrorLoc, "alignment must be 64, 128 or omitted");
9226       case Match_AlignedMemoryRequires64or128or256:
9227         return Error(ErrorLoc, "alignment must be 64, 128, 256 or omitted");
9228     }
9229   }
9230   }
9231 
9232   llvm_unreachable("Implement any new match types added!");
9233 }
9234 
9235 /// parseDirective parses the arm specific directives
9236 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
9237   const MCObjectFileInfo::Environment Format =
9238     getContext().getObjectFileInfo()->getObjectFileType();
9239   bool IsMachO = Format == MCObjectFileInfo::IsMachO;
9240   bool IsCOFF = Format == MCObjectFileInfo::IsCOFF;
9241 
9242   StringRef IDVal = DirectiveID.getIdentifier();
9243   if (IDVal == ".word")
9244     return parseLiteralValues(4, DirectiveID.getLoc());
9245   else if (IDVal == ".short" || IDVal == ".hword")
9246     return parseLiteralValues(2, DirectiveID.getLoc());
9247   else if (IDVal == ".thumb")
9248     return parseDirectiveThumb(DirectiveID.getLoc());
9249   else if (IDVal == ".arm")
9250     return parseDirectiveARM(DirectiveID.getLoc());
9251   else if (IDVal == ".thumb_func")
9252     return parseDirectiveThumbFunc(DirectiveID.getLoc());
9253   else if (IDVal == ".code")
9254     return parseDirectiveCode(DirectiveID.getLoc());
9255   else if (IDVal == ".syntax")
9256     return parseDirectiveSyntax(DirectiveID.getLoc());
9257   else if (IDVal == ".unreq")
9258     return parseDirectiveUnreq(DirectiveID.getLoc());
9259   else if (IDVal == ".fnend")
9260     return parseDirectiveFnEnd(DirectiveID.getLoc());
9261   else if (IDVal == ".cantunwind")
9262     return parseDirectiveCantUnwind(DirectiveID.getLoc());
9263   else if (IDVal == ".personality")
9264     return parseDirectivePersonality(DirectiveID.getLoc());
9265   else if (IDVal == ".handlerdata")
9266     return parseDirectiveHandlerData(DirectiveID.getLoc());
9267   else if (IDVal == ".setfp")
9268     return parseDirectiveSetFP(DirectiveID.getLoc());
9269   else if (IDVal == ".pad")
9270     return parseDirectivePad(DirectiveID.getLoc());
9271   else if (IDVal == ".save")
9272     return parseDirectiveRegSave(DirectiveID.getLoc(), false);
9273   else if (IDVal == ".vsave")
9274     return parseDirectiveRegSave(DirectiveID.getLoc(), true);
9275   else if (IDVal == ".ltorg" || IDVal == ".pool")
9276     return parseDirectiveLtorg(DirectiveID.getLoc());
9277   else if (IDVal == ".even")
9278     return parseDirectiveEven(DirectiveID.getLoc());
9279   else if (IDVal == ".personalityindex")
9280     return parseDirectivePersonalityIndex(DirectiveID.getLoc());
9281   else if (IDVal == ".unwind_raw")
9282     return parseDirectiveUnwindRaw(DirectiveID.getLoc());
9283   else if (IDVal == ".movsp")
9284     return parseDirectiveMovSP(DirectiveID.getLoc());
9285   else if (IDVal == ".arch_extension")
9286     return parseDirectiveArchExtension(DirectiveID.getLoc());
9287   else if (IDVal == ".align")
9288     return parseDirectiveAlign(DirectiveID.getLoc());
9289   else if (IDVal == ".thumb_set")
9290     return parseDirectiveThumbSet(DirectiveID.getLoc());
9291 
9292   if (!IsMachO && !IsCOFF) {
9293     if (IDVal == ".arch")
9294       return parseDirectiveArch(DirectiveID.getLoc());
9295     else if (IDVal == ".cpu")
9296       return parseDirectiveCPU(DirectiveID.getLoc());
9297     else if (IDVal == ".eabi_attribute")
9298       return parseDirectiveEabiAttr(DirectiveID.getLoc());
9299     else if (IDVal == ".fpu")
9300       return parseDirectiveFPU(DirectiveID.getLoc());
9301     else if (IDVal == ".fnstart")
9302       return parseDirectiveFnStart(DirectiveID.getLoc());
9303     else if (IDVal == ".inst")
9304       return parseDirectiveInst(DirectiveID.getLoc());
9305     else if (IDVal == ".inst.n")
9306       return parseDirectiveInst(DirectiveID.getLoc(), 'n');
9307     else if (IDVal == ".inst.w")
9308       return parseDirectiveInst(DirectiveID.getLoc(), 'w');
9309     else if (IDVal == ".object_arch")
9310       return parseDirectiveObjectArch(DirectiveID.getLoc());
9311     else if (IDVal == ".tlsdescseq")
9312       return parseDirectiveTLSDescSeq(DirectiveID.getLoc());
9313   }
9314 
9315   return true;
9316 }
9317 
9318 /// parseLiteralValues
9319 ///  ::= .hword expression [, expression]*
9320 ///  ::= .short expression [, expression]*
9321 ///  ::= .word expression [, expression]*
9322 bool ARMAsmParser::parseLiteralValues(unsigned Size, SMLoc L) {
9323   MCAsmParser &Parser = getParser();
9324   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9325     for (;;) {
9326       const MCExpr *Value;
9327       if (getParser().parseExpression(Value)) {
9328         Parser.eatToEndOfStatement();
9329         return false;
9330       }
9331 
9332       getParser().getStreamer().EmitValue(Value, Size, L);
9333 
9334       if (getLexer().is(AsmToken::EndOfStatement))
9335         break;
9336 
9337       // FIXME: Improve diagnostic.
9338       if (getLexer().isNot(AsmToken::Comma)) {
9339         Error(L, "unexpected token in directive");
9340         return false;
9341       }
9342       Parser.Lex();
9343     }
9344   }
9345 
9346   Parser.Lex();
9347   return false;
9348 }
9349 
9350 /// parseDirectiveThumb
9351 ///  ::= .thumb
9352 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
9353   MCAsmParser &Parser = getParser();
9354   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9355     Error(L, "unexpected token in directive");
9356     return false;
9357   }
9358   Parser.Lex();
9359 
9360   if (!hasThumb()) {
9361     Error(L, "target does not support Thumb mode");
9362     return false;
9363   }
9364 
9365   if (!isThumb())
9366     SwitchMode();
9367 
9368   getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
9369   return false;
9370 }
9371 
9372 /// parseDirectiveARM
9373 ///  ::= .arm
9374 bool ARMAsmParser::parseDirectiveARM(SMLoc L) {
9375   MCAsmParser &Parser = getParser();
9376   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9377     Error(L, "unexpected token in directive");
9378     return false;
9379   }
9380   Parser.Lex();
9381 
9382   if (!hasARM()) {
9383     Error(L, "target does not support ARM mode");
9384     return false;
9385   }
9386 
9387   if (isThumb())
9388     SwitchMode();
9389 
9390   getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
9391   return false;
9392 }
9393 
9394 void ARMAsmParser::onLabelParsed(MCSymbol *Symbol) {
9395   // We need to flush the current implicit IT block on a label, because it is
9396   // not legal to branch into an IT block.
9397   flushPendingInstructions(getStreamer());
9398   if (NextSymbolIsThumb) {
9399     getParser().getStreamer().EmitThumbFunc(Symbol);
9400     NextSymbolIsThumb = false;
9401   }
9402 }
9403 
9404 /// parseDirectiveThumbFunc
9405 ///  ::= .thumbfunc symbol_name
9406 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
9407   MCAsmParser &Parser = getParser();
9408   const auto Format = getContext().getObjectFileInfo()->getObjectFileType();
9409   bool IsMachO = Format == MCObjectFileInfo::IsMachO;
9410 
9411   // Darwin asm has (optionally) function name after .thumb_func direction
9412   // ELF doesn't
9413   if (IsMachO) {
9414     const AsmToken &Tok = Parser.getTok();
9415     if (Tok.isNot(AsmToken::EndOfStatement)) {
9416       if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String)) {
9417         Error(L, "unexpected token in .thumb_func directive");
9418         return false;
9419       }
9420 
9421       MCSymbol *Func =
9422           getParser().getContext().getOrCreateSymbol(Tok.getIdentifier());
9423       getParser().getStreamer().EmitThumbFunc(Func);
9424       Parser.Lex(); // Consume the identifier token.
9425       return false;
9426     }
9427   }
9428 
9429   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9430     Error(Parser.getTok().getLoc(), "unexpected token in directive");
9431     Parser.eatToEndOfStatement();
9432     return false;
9433   }
9434 
9435   NextSymbolIsThumb = true;
9436   return false;
9437 }
9438 
9439 /// parseDirectiveSyntax
9440 ///  ::= .syntax unified | divided
9441 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
9442   MCAsmParser &Parser = getParser();
9443   const AsmToken &Tok = Parser.getTok();
9444   if (Tok.isNot(AsmToken::Identifier)) {
9445     Error(L, "unexpected token in .syntax directive");
9446     return false;
9447   }
9448 
9449   StringRef Mode = Tok.getString();
9450   if (Mode == "unified" || Mode == "UNIFIED") {
9451     Parser.Lex();
9452   } else if (Mode == "divided" || Mode == "DIVIDED") {
9453     Error(L, "'.syntax divided' arm asssembly not supported");
9454     return false;
9455   } else {
9456     Error(L, "unrecognized syntax mode in .syntax directive");
9457     return false;
9458   }
9459 
9460   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9461     Error(Parser.getTok().getLoc(), "unexpected token in directive");
9462     return false;
9463   }
9464   Parser.Lex();
9465 
9466   // TODO tell the MC streamer the mode
9467   // getParser().getStreamer().Emit???();
9468   return false;
9469 }
9470 
9471 /// parseDirectiveCode
9472 ///  ::= .code 16 | 32
9473 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
9474   MCAsmParser &Parser = getParser();
9475   const AsmToken &Tok = Parser.getTok();
9476   if (Tok.isNot(AsmToken::Integer)) {
9477     Error(L, "unexpected token in .code directive");
9478     return false;
9479   }
9480   int64_t Val = Parser.getTok().getIntVal();
9481   if (Val != 16 && Val != 32) {
9482     Error(L, "invalid operand to .code directive");
9483     return false;
9484   }
9485   Parser.Lex();
9486 
9487   if (getLexer().isNot(AsmToken::EndOfStatement)) {
9488     Error(Parser.getTok().getLoc(), "unexpected token in directive");
9489     return false;
9490   }
9491   Parser.Lex();
9492 
9493   if (Val == 16) {
9494     if (!hasThumb()) {
9495       Error(L, "target does not support Thumb mode");
9496       return false;
9497     }
9498 
9499     if (!isThumb())
9500       SwitchMode();
9501     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
9502   } else {
9503     if (!hasARM()) {
9504       Error(L, "target does not support ARM mode");
9505       return false;
9506     }
9507 
9508     if (isThumb())
9509       SwitchMode();
9510     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
9511   }
9512 
9513   return false;
9514 }
9515 
9516 /// parseDirectiveReq
9517 ///  ::= name .req registername
9518 bool ARMAsmParser::parseDirectiveReq(StringRef Name, SMLoc L) {
9519   MCAsmParser &Parser = getParser();
9520   Parser.Lex(); // Eat the '.req' token.
9521   unsigned Reg;
9522   SMLoc SRegLoc, ERegLoc;
9523   if (ParseRegister(Reg, SRegLoc, ERegLoc)) {
9524     Parser.eatToEndOfStatement();
9525     Error(SRegLoc, "register name expected");
9526     return false;
9527   }
9528 
9529   // Shouldn't be anything else.
9530   if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
9531     Parser.eatToEndOfStatement();
9532     Error(Parser.getTok().getLoc(), "unexpected input in .req directive.");
9533     return false;
9534   }
9535 
9536   Parser.Lex(); // Consume the EndOfStatement
9537 
9538   if (RegisterReqs.insert(std::make_pair(Name, Reg)).first->second != Reg) {
9539     Error(SRegLoc, "redefinition of '" + Name + "' does not match original.");
9540     return false;
9541   }
9542 
9543   return false;
9544 }
9545 
9546 /// parseDirectiveUneq
9547 ///  ::= .unreq registername
9548 bool ARMAsmParser::parseDirectiveUnreq(SMLoc L) {
9549   MCAsmParser &Parser = getParser();
9550   if (Parser.getTok().isNot(AsmToken::Identifier)) {
9551     Parser.eatToEndOfStatement();
9552     Error(L, "unexpected input in .unreq directive.");
9553     return false;
9554   }
9555   RegisterReqs.erase(Parser.getTok().getIdentifier().lower());
9556   Parser.Lex(); // Eat the identifier.
9557   return false;
9558 }
9559 
9560 // After changing arch/CPU, try to put the ARM/Thumb mode back to what it was
9561 // before, if supported by the new target, or emit mapping symbols for the mode
9562 // switch.
9563 void ARMAsmParser::FixModeAfterArchChange(bool WasThumb, SMLoc Loc) {
9564   if (WasThumb != isThumb()) {
9565     if (WasThumb && hasThumb()) {
9566       // Stay in Thumb mode
9567       SwitchMode();
9568     } else if (!WasThumb && hasARM()) {
9569       // Stay in ARM mode
9570       SwitchMode();
9571     } else {
9572       // Mode switch forced, because the new arch doesn't support the old mode.
9573       getParser().getStreamer().EmitAssemblerFlag(isThumb() ? MCAF_Code16
9574                                                             : MCAF_Code32);
9575       // Warn about the implcit mode switch. GAS does not switch modes here,
9576       // but instead stays in the old mode, reporting an error on any following
9577       // instructions as the mode does not exist on the target.
9578       Warning(Loc, Twine("new target does not support ") +
9579                        (WasThumb ? "thumb" : "arm") + " mode, switching to " +
9580                        (!WasThumb ? "thumb" : "arm") + " mode");
9581     }
9582   }
9583 }
9584 
9585 /// parseDirectiveArch
9586 ///  ::= .arch token
9587 bool ARMAsmParser::parseDirectiveArch(SMLoc L) {
9588   StringRef Arch = getParser().parseStringToEndOfStatement().trim();
9589 
9590   unsigned ID = ARM::parseArch(Arch);
9591 
9592   if (ID == ARM::AK_INVALID) {
9593     Error(L, "Unknown arch name");
9594     return false;
9595   }
9596 
9597   bool WasThumb = isThumb();
9598   Triple T;
9599   MCSubtargetInfo &STI = copySTI();
9600   STI.setDefaultFeatures("", ("+" + ARM::getArchName(ID)).str());
9601   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9602   FixModeAfterArchChange(WasThumb, L);
9603 
9604   getTargetStreamer().emitArch(ID);
9605   return false;
9606 }
9607 
9608 /// parseDirectiveEabiAttr
9609 ///  ::= .eabi_attribute int, int [, "str"]
9610 ///  ::= .eabi_attribute Tag_name, int [, "str"]
9611 bool ARMAsmParser::parseDirectiveEabiAttr(SMLoc L) {
9612   MCAsmParser &Parser = getParser();
9613   int64_t Tag;
9614   SMLoc TagLoc;
9615   TagLoc = Parser.getTok().getLoc();
9616   if (Parser.getTok().is(AsmToken::Identifier)) {
9617     StringRef Name = Parser.getTok().getIdentifier();
9618     Tag = ARMBuildAttrs::AttrTypeFromString(Name);
9619     if (Tag == -1) {
9620       Error(TagLoc, "attribute name not recognised: " + Name);
9621       Parser.eatToEndOfStatement();
9622       return false;
9623     }
9624     Parser.Lex();
9625   } else {
9626     const MCExpr *AttrExpr;
9627 
9628     TagLoc = Parser.getTok().getLoc();
9629     if (Parser.parseExpression(AttrExpr)) {
9630       Parser.eatToEndOfStatement();
9631       return false;
9632     }
9633 
9634     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(AttrExpr);
9635     if (!CE) {
9636       Error(TagLoc, "expected numeric constant");
9637       Parser.eatToEndOfStatement();
9638       return false;
9639     }
9640 
9641     Tag = CE->getValue();
9642   }
9643 
9644   if (Parser.getTok().isNot(AsmToken::Comma)) {
9645     Error(Parser.getTok().getLoc(), "comma expected");
9646     Parser.eatToEndOfStatement();
9647     return false;
9648   }
9649   Parser.Lex(); // skip comma
9650 
9651   StringRef StringValue = "";
9652   bool IsStringValue = false;
9653 
9654   int64_t IntegerValue = 0;
9655   bool IsIntegerValue = false;
9656 
9657   if (Tag == ARMBuildAttrs::CPU_raw_name || Tag == ARMBuildAttrs::CPU_name)
9658     IsStringValue = true;
9659   else if (Tag == ARMBuildAttrs::compatibility) {
9660     IsStringValue = true;
9661     IsIntegerValue = true;
9662   } else if (Tag < 32 || Tag % 2 == 0)
9663     IsIntegerValue = true;
9664   else if (Tag % 2 == 1)
9665     IsStringValue = true;
9666   else
9667     llvm_unreachable("invalid tag type");
9668 
9669   if (IsIntegerValue) {
9670     const MCExpr *ValueExpr;
9671     SMLoc ValueExprLoc = Parser.getTok().getLoc();
9672     if (Parser.parseExpression(ValueExpr)) {
9673       Parser.eatToEndOfStatement();
9674       return false;
9675     }
9676 
9677     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ValueExpr);
9678     if (!CE) {
9679       Error(ValueExprLoc, "expected numeric constant");
9680       Parser.eatToEndOfStatement();
9681       return false;
9682     }
9683 
9684     IntegerValue = CE->getValue();
9685   }
9686 
9687   if (Tag == ARMBuildAttrs::compatibility) {
9688     if (Parser.getTok().isNot(AsmToken::Comma))
9689       IsStringValue = false;
9690     if (Parser.getTok().isNot(AsmToken::Comma)) {
9691       Error(Parser.getTok().getLoc(), "comma expected");
9692       Parser.eatToEndOfStatement();
9693       return false;
9694     } else {
9695        Parser.Lex();
9696     }
9697   }
9698 
9699   if (IsStringValue) {
9700     if (Parser.getTok().isNot(AsmToken::String)) {
9701       Error(Parser.getTok().getLoc(), "bad string constant");
9702       Parser.eatToEndOfStatement();
9703       return false;
9704     }
9705 
9706     StringValue = Parser.getTok().getStringContents();
9707     Parser.Lex();
9708   }
9709 
9710   if (IsIntegerValue && IsStringValue) {
9711     assert(Tag == ARMBuildAttrs::compatibility);
9712     getTargetStreamer().emitIntTextAttribute(Tag, IntegerValue, StringValue);
9713   } else if (IsIntegerValue)
9714     getTargetStreamer().emitAttribute(Tag, IntegerValue);
9715   else if (IsStringValue)
9716     getTargetStreamer().emitTextAttribute(Tag, StringValue);
9717   return false;
9718 }
9719 
9720 /// parseDirectiveCPU
9721 ///  ::= .cpu str
9722 bool ARMAsmParser::parseDirectiveCPU(SMLoc L) {
9723   StringRef CPU = getParser().parseStringToEndOfStatement().trim();
9724   getTargetStreamer().emitTextAttribute(ARMBuildAttrs::CPU_name, CPU);
9725 
9726   // FIXME: This is using table-gen data, but should be moved to
9727   // ARMTargetParser once that is table-gen'd.
9728   if (!getSTI().isCPUStringValid(CPU)) {
9729     Error(L, "Unknown CPU name");
9730     return false;
9731   }
9732 
9733   bool WasThumb = isThumb();
9734   MCSubtargetInfo &STI = copySTI();
9735   STI.setDefaultFeatures(CPU, "");
9736   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9737   FixModeAfterArchChange(WasThumb, L);
9738 
9739   return false;
9740 }
9741 /// parseDirectiveFPU
9742 ///  ::= .fpu str
9743 bool ARMAsmParser::parseDirectiveFPU(SMLoc L) {
9744   SMLoc FPUNameLoc = getTok().getLoc();
9745   StringRef FPU = getParser().parseStringToEndOfStatement().trim();
9746 
9747   unsigned ID = ARM::parseFPU(FPU);
9748   std::vector<const char *> Features;
9749   if (!ARM::getFPUFeatures(ID, Features)) {
9750     Error(FPUNameLoc, "Unknown FPU name");
9751     return false;
9752   }
9753 
9754   MCSubtargetInfo &STI = copySTI();
9755   for (auto Feature : Features)
9756     STI.ApplyFeatureFlag(Feature);
9757   setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
9758 
9759   getTargetStreamer().emitFPU(ID);
9760   return false;
9761 }
9762 
9763 /// parseDirectiveFnStart
9764 ///  ::= .fnstart
9765 bool ARMAsmParser::parseDirectiveFnStart(SMLoc L) {
9766   if (UC.hasFnStart()) {
9767     Error(L, ".fnstart starts before the end of previous one");
9768     UC.emitFnStartLocNotes();
9769     return false;
9770   }
9771 
9772   // Reset the unwind directives parser state
9773   UC.reset();
9774 
9775   getTargetStreamer().emitFnStart();
9776 
9777   UC.recordFnStart(L);
9778   return false;
9779 }
9780 
9781 /// parseDirectiveFnEnd
9782 ///  ::= .fnend
9783 bool ARMAsmParser::parseDirectiveFnEnd(SMLoc L) {
9784   // Check the ordering of unwind directives
9785   if (!UC.hasFnStart()) {
9786     Error(L, ".fnstart must precede .fnend directive");
9787     return false;
9788   }
9789 
9790   // Reset the unwind directives parser state
9791   getTargetStreamer().emitFnEnd();
9792 
9793   UC.reset();
9794   return false;
9795 }
9796 
9797 /// parseDirectiveCantUnwind
9798 ///  ::= .cantunwind
9799 bool ARMAsmParser::parseDirectiveCantUnwind(SMLoc L) {
9800   UC.recordCantUnwind(L);
9801 
9802   // Check the ordering of unwind directives
9803   if (!UC.hasFnStart()) {
9804     Error(L, ".fnstart must precede .cantunwind directive");
9805     return false;
9806   }
9807   if (UC.hasHandlerData()) {
9808     Error(L, ".cantunwind can't be used with .handlerdata directive");
9809     UC.emitHandlerDataLocNotes();
9810     return false;
9811   }
9812   if (UC.hasPersonality()) {
9813     Error(L, ".cantunwind can't be used with .personality directive");
9814     UC.emitPersonalityLocNotes();
9815     return false;
9816   }
9817 
9818   getTargetStreamer().emitCantUnwind();
9819   return false;
9820 }
9821 
9822 /// parseDirectivePersonality
9823 ///  ::= .personality name
9824 bool ARMAsmParser::parseDirectivePersonality(SMLoc L) {
9825   MCAsmParser &Parser = getParser();
9826   bool HasExistingPersonality = UC.hasPersonality();
9827 
9828   UC.recordPersonality(L);
9829 
9830   // Check the ordering of unwind directives
9831   if (!UC.hasFnStart()) {
9832     Error(L, ".fnstart must precede .personality directive");
9833     return false;
9834   }
9835   if (UC.cantUnwind()) {
9836     Error(L, ".personality can't be used with .cantunwind directive");
9837     UC.emitCantUnwindLocNotes();
9838     return false;
9839   }
9840   if (UC.hasHandlerData()) {
9841     Error(L, ".personality must precede .handlerdata directive");
9842     UC.emitHandlerDataLocNotes();
9843     return false;
9844   }
9845   if (HasExistingPersonality) {
9846     Parser.eatToEndOfStatement();
9847     Error(L, "multiple personality directives");
9848     UC.emitPersonalityLocNotes();
9849     return false;
9850   }
9851 
9852   // Parse the name of the personality routine
9853   if (Parser.getTok().isNot(AsmToken::Identifier)) {
9854     Parser.eatToEndOfStatement();
9855     Error(L, "unexpected input in .personality directive.");
9856     return false;
9857   }
9858   StringRef Name(Parser.getTok().getIdentifier());
9859   Parser.Lex();
9860 
9861   MCSymbol *PR = getParser().getContext().getOrCreateSymbol(Name);
9862   getTargetStreamer().emitPersonality(PR);
9863   return false;
9864 }
9865 
9866 /// parseDirectiveHandlerData
9867 ///  ::= .handlerdata
9868 bool ARMAsmParser::parseDirectiveHandlerData(SMLoc L) {
9869   UC.recordHandlerData(L);
9870 
9871   // Check the ordering of unwind directives
9872   if (!UC.hasFnStart()) {
9873     Error(L, ".fnstart must precede .personality directive");
9874     return false;
9875   }
9876   if (UC.cantUnwind()) {
9877     Error(L, ".handlerdata can't be used with .cantunwind directive");
9878     UC.emitCantUnwindLocNotes();
9879     return false;
9880   }
9881 
9882   getTargetStreamer().emitHandlerData();
9883   return false;
9884 }
9885 
9886 /// parseDirectiveSetFP
9887 ///  ::= .setfp fpreg, spreg [, offset]
9888 bool ARMAsmParser::parseDirectiveSetFP(SMLoc L) {
9889   MCAsmParser &Parser = getParser();
9890   // Check the ordering of unwind directives
9891   if (!UC.hasFnStart()) {
9892     Error(L, ".fnstart must precede .setfp directive");
9893     return false;
9894   }
9895   if (UC.hasHandlerData()) {
9896     Error(L, ".setfp must precede .handlerdata directive");
9897     return false;
9898   }
9899 
9900   // Parse fpreg
9901   SMLoc FPRegLoc = Parser.getTok().getLoc();
9902   int FPReg = tryParseRegister();
9903   if (FPReg == -1) {
9904     Error(FPRegLoc, "frame pointer register expected");
9905     return false;
9906   }
9907 
9908   // Consume comma
9909   if (Parser.getTok().isNot(AsmToken::Comma)) {
9910     Error(Parser.getTok().getLoc(), "comma expected");
9911     return false;
9912   }
9913   Parser.Lex(); // skip comma
9914 
9915   // Parse spreg
9916   SMLoc SPRegLoc = Parser.getTok().getLoc();
9917   int SPReg = tryParseRegister();
9918   if (SPReg == -1) {
9919     Error(SPRegLoc, "stack pointer register expected");
9920     return false;
9921   }
9922 
9923   if (SPReg != ARM::SP && SPReg != UC.getFPReg()) {
9924     Error(SPRegLoc, "register should be either $sp or the latest fp register");
9925     return false;
9926   }
9927 
9928   // Update the frame pointer register
9929   UC.saveFPReg(FPReg);
9930 
9931   // Parse offset
9932   int64_t Offset = 0;
9933   if (Parser.getTok().is(AsmToken::Comma)) {
9934     Parser.Lex(); // skip comma
9935 
9936     if (Parser.getTok().isNot(AsmToken::Hash) &&
9937         Parser.getTok().isNot(AsmToken::Dollar)) {
9938       Error(Parser.getTok().getLoc(), "'#' expected");
9939       return false;
9940     }
9941     Parser.Lex(); // skip hash token.
9942 
9943     const MCExpr *OffsetExpr;
9944     SMLoc ExLoc = Parser.getTok().getLoc();
9945     SMLoc EndLoc;
9946     if (getParser().parseExpression(OffsetExpr, EndLoc)) {
9947       Error(ExLoc, "malformed setfp offset");
9948       return false;
9949     }
9950     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9951     if (!CE) {
9952       Error(ExLoc, "setfp offset must be an immediate");
9953       return false;
9954     }
9955 
9956     Offset = CE->getValue();
9957   }
9958 
9959   getTargetStreamer().emitSetFP(static_cast<unsigned>(FPReg),
9960                                 static_cast<unsigned>(SPReg), Offset);
9961   return false;
9962 }
9963 
9964 /// parseDirective
9965 ///  ::= .pad offset
9966 bool ARMAsmParser::parseDirectivePad(SMLoc L) {
9967   MCAsmParser &Parser = getParser();
9968   // Check the ordering of unwind directives
9969   if (!UC.hasFnStart()) {
9970     Error(L, ".fnstart must precede .pad directive");
9971     return false;
9972   }
9973   if (UC.hasHandlerData()) {
9974     Error(L, ".pad must precede .handlerdata directive");
9975     return false;
9976   }
9977 
9978   // Parse the offset
9979   if (Parser.getTok().isNot(AsmToken::Hash) &&
9980       Parser.getTok().isNot(AsmToken::Dollar)) {
9981     Error(Parser.getTok().getLoc(), "'#' expected");
9982     return false;
9983   }
9984   Parser.Lex(); // skip hash token.
9985 
9986   const MCExpr *OffsetExpr;
9987   SMLoc ExLoc = Parser.getTok().getLoc();
9988   SMLoc EndLoc;
9989   if (getParser().parseExpression(OffsetExpr, EndLoc)) {
9990     Error(ExLoc, "malformed pad offset");
9991     return false;
9992   }
9993   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
9994   if (!CE) {
9995     Error(ExLoc, "pad offset must be an immediate");
9996     return false;
9997   }
9998 
9999   getTargetStreamer().emitPad(CE->getValue());
10000   return false;
10001 }
10002 
10003 /// parseDirectiveRegSave
10004 ///  ::= .save  { registers }
10005 ///  ::= .vsave { registers }
10006 bool ARMAsmParser::parseDirectiveRegSave(SMLoc L, bool IsVector) {
10007   // Check the ordering of unwind directives
10008   if (!UC.hasFnStart()) {
10009     Error(L, ".fnstart must precede .save or .vsave directives");
10010     return false;
10011   }
10012   if (UC.hasHandlerData()) {
10013     Error(L, ".save or .vsave must precede .handlerdata directive");
10014     return false;
10015   }
10016 
10017   // RAII object to make sure parsed operands are deleted.
10018   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Operands;
10019 
10020   // Parse the register list
10021   if (parseRegisterList(Operands))
10022     return false;
10023   ARMOperand &Op = (ARMOperand &)*Operands[0];
10024   if (!IsVector && !Op.isRegList()) {
10025     Error(L, ".save expects GPR registers");
10026     return false;
10027   }
10028   if (IsVector && !Op.isDPRRegList()) {
10029     Error(L, ".vsave expects DPR registers");
10030     return false;
10031   }
10032 
10033   getTargetStreamer().emitRegSave(Op.getRegList(), IsVector);
10034   return false;
10035 }
10036 
10037 /// parseDirectiveInst
10038 ///  ::= .inst opcode [, ...]
10039 ///  ::= .inst.n opcode [, ...]
10040 ///  ::= .inst.w opcode [, ...]
10041 bool ARMAsmParser::parseDirectiveInst(SMLoc Loc, char Suffix) {
10042   MCAsmParser &Parser = getParser();
10043   int Width;
10044 
10045   if (isThumb()) {
10046     switch (Suffix) {
10047     case 'n':
10048       Width = 2;
10049       break;
10050     case 'w':
10051       Width = 4;
10052       break;
10053     default:
10054       Parser.eatToEndOfStatement();
10055       Error(Loc, "cannot determine Thumb instruction size, "
10056                  "use inst.n/inst.w instead");
10057       return false;
10058     }
10059   } else {
10060     if (Suffix) {
10061       Parser.eatToEndOfStatement();
10062       Error(Loc, "width suffixes are invalid in ARM mode");
10063       return false;
10064     }
10065     Width = 4;
10066   }
10067 
10068   if (getLexer().is(AsmToken::EndOfStatement)) {
10069     Parser.eatToEndOfStatement();
10070     Error(Loc, "expected expression following directive");
10071     return false;
10072   }
10073 
10074   for (;;) {
10075     const MCExpr *Expr;
10076 
10077     if (getParser().parseExpression(Expr)) {
10078       Error(Loc, "expected expression");
10079       return false;
10080     }
10081 
10082     const MCConstantExpr *Value = dyn_cast_or_null<MCConstantExpr>(Expr);
10083     if (!Value) {
10084       Error(Loc, "expected constant expression");
10085       return false;
10086     }
10087 
10088     switch (Width) {
10089     case 2:
10090       if (Value->getValue() > 0xffff) {
10091         Error(Loc, "inst.n operand is too big, use inst.w instead");
10092         return false;
10093       }
10094       break;
10095     case 4:
10096       if (Value->getValue() > 0xffffffff) {
10097         Error(Loc,
10098               StringRef(Suffix ? "inst.w" : "inst") + " operand is too big");
10099         return false;
10100       }
10101       break;
10102     default:
10103       llvm_unreachable("only supported widths are 2 and 4");
10104     }
10105 
10106     getTargetStreamer().emitInst(Value->getValue(), Suffix);
10107 
10108     if (getLexer().is(AsmToken::EndOfStatement))
10109       break;
10110 
10111     if (getLexer().isNot(AsmToken::Comma)) {
10112       Error(Loc, "unexpected token in directive");
10113       return false;
10114     }
10115 
10116     Parser.Lex();
10117   }
10118 
10119   Parser.Lex();
10120   return false;
10121 }
10122 
10123 /// parseDirectiveLtorg
10124 ///  ::= .ltorg | .pool
10125 bool ARMAsmParser::parseDirectiveLtorg(SMLoc L) {
10126   getTargetStreamer().emitCurrentConstantPool();
10127   return false;
10128 }
10129 
10130 bool ARMAsmParser::parseDirectiveEven(SMLoc L) {
10131   const MCSection *Section = getStreamer().getCurrentSection().first;
10132 
10133   if (getLexer().isNot(AsmToken::EndOfStatement)) {
10134     TokError("unexpected token in directive");
10135     return false;
10136   }
10137 
10138   if (!Section) {
10139     getStreamer().InitSections(false);
10140     Section = getStreamer().getCurrentSection().first;
10141   }
10142 
10143   assert(Section && "must have section to emit alignment");
10144   if (Section->UseCodeAlign())
10145     getStreamer().EmitCodeAlignment(2);
10146   else
10147     getStreamer().EmitValueToAlignment(2);
10148 
10149   return false;
10150 }
10151 
10152 /// parseDirectivePersonalityIndex
10153 ///   ::= .personalityindex index
10154 bool ARMAsmParser::parseDirectivePersonalityIndex(SMLoc L) {
10155   MCAsmParser &Parser = getParser();
10156   bool HasExistingPersonality = UC.hasPersonality();
10157 
10158   UC.recordPersonalityIndex(L);
10159 
10160   if (!UC.hasFnStart()) {
10161     Parser.eatToEndOfStatement();
10162     Error(L, ".fnstart must precede .personalityindex directive");
10163     return false;
10164   }
10165   if (UC.cantUnwind()) {
10166     Parser.eatToEndOfStatement();
10167     Error(L, ".personalityindex cannot be used with .cantunwind");
10168     UC.emitCantUnwindLocNotes();
10169     return false;
10170   }
10171   if (UC.hasHandlerData()) {
10172     Parser.eatToEndOfStatement();
10173     Error(L, ".personalityindex must precede .handlerdata directive");
10174     UC.emitHandlerDataLocNotes();
10175     return false;
10176   }
10177   if (HasExistingPersonality) {
10178     Parser.eatToEndOfStatement();
10179     Error(L, "multiple personality directives");
10180     UC.emitPersonalityLocNotes();
10181     return false;
10182   }
10183 
10184   const MCExpr *IndexExpression;
10185   SMLoc IndexLoc = Parser.getTok().getLoc();
10186   if (Parser.parseExpression(IndexExpression)) {
10187     Parser.eatToEndOfStatement();
10188     return false;
10189   }
10190 
10191   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(IndexExpression);
10192   if (!CE) {
10193     Parser.eatToEndOfStatement();
10194     Error(IndexLoc, "index must be a constant number");
10195     return false;
10196   }
10197   if (CE->getValue() < 0 ||
10198       CE->getValue() >= ARM::EHABI::NUM_PERSONALITY_INDEX) {
10199     Parser.eatToEndOfStatement();
10200     Error(IndexLoc, "personality routine index should be in range [0-3]");
10201     return false;
10202   }
10203 
10204   getTargetStreamer().emitPersonalityIndex(CE->getValue());
10205   return false;
10206 }
10207 
10208 /// parseDirectiveUnwindRaw
10209 ///   ::= .unwind_raw offset, opcode [, opcode...]
10210 bool ARMAsmParser::parseDirectiveUnwindRaw(SMLoc L) {
10211   MCAsmParser &Parser = getParser();
10212   if (!UC.hasFnStart()) {
10213     Parser.eatToEndOfStatement();
10214     Error(L, ".fnstart must precede .unwind_raw directives");
10215     return false;
10216   }
10217 
10218   int64_t StackOffset;
10219 
10220   const MCExpr *OffsetExpr;
10221   SMLoc OffsetLoc = getLexer().getLoc();
10222   if (getLexer().is(AsmToken::EndOfStatement) ||
10223       getParser().parseExpression(OffsetExpr)) {
10224     Error(OffsetLoc, "expected expression");
10225     Parser.eatToEndOfStatement();
10226     return false;
10227   }
10228 
10229   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
10230   if (!CE) {
10231     Error(OffsetLoc, "offset must be a constant");
10232     Parser.eatToEndOfStatement();
10233     return false;
10234   }
10235 
10236   StackOffset = CE->getValue();
10237 
10238   if (getLexer().isNot(AsmToken::Comma)) {
10239     Error(getLexer().getLoc(), "expected comma");
10240     Parser.eatToEndOfStatement();
10241     return false;
10242   }
10243   Parser.Lex();
10244 
10245   SmallVector<uint8_t, 16> Opcodes;
10246   for (;;) {
10247     const MCExpr *OE;
10248 
10249     SMLoc OpcodeLoc = getLexer().getLoc();
10250     if (getLexer().is(AsmToken::EndOfStatement) || Parser.parseExpression(OE)) {
10251       Error(OpcodeLoc, "expected opcode expression");
10252       Parser.eatToEndOfStatement();
10253       return false;
10254     }
10255 
10256     const MCConstantExpr *OC = dyn_cast<MCConstantExpr>(OE);
10257     if (!OC) {
10258       Error(OpcodeLoc, "opcode value must be a constant");
10259       Parser.eatToEndOfStatement();
10260       return false;
10261     }
10262 
10263     const int64_t Opcode = OC->getValue();
10264     if (Opcode & ~0xff) {
10265       Error(OpcodeLoc, "invalid opcode");
10266       Parser.eatToEndOfStatement();
10267       return false;
10268     }
10269 
10270     Opcodes.push_back(uint8_t(Opcode));
10271 
10272     if (getLexer().is(AsmToken::EndOfStatement))
10273       break;
10274 
10275     if (getLexer().isNot(AsmToken::Comma)) {
10276       Error(getLexer().getLoc(), "unexpected token in directive");
10277       Parser.eatToEndOfStatement();
10278       return false;
10279     }
10280 
10281     Parser.Lex();
10282   }
10283 
10284   getTargetStreamer().emitUnwindRaw(StackOffset, Opcodes);
10285 
10286   Parser.Lex();
10287   return false;
10288 }
10289 
10290 /// parseDirectiveTLSDescSeq
10291 ///   ::= .tlsdescseq tls-variable
10292 bool ARMAsmParser::parseDirectiveTLSDescSeq(SMLoc L) {
10293   MCAsmParser &Parser = getParser();
10294 
10295   if (getLexer().isNot(AsmToken::Identifier)) {
10296     TokError("expected variable after '.tlsdescseq' directive");
10297     Parser.eatToEndOfStatement();
10298     return false;
10299   }
10300 
10301   const MCSymbolRefExpr *SRE =
10302     MCSymbolRefExpr::create(Parser.getTok().getIdentifier(),
10303                             MCSymbolRefExpr::VK_ARM_TLSDESCSEQ, getContext());
10304   Lex();
10305 
10306   if (getLexer().isNot(AsmToken::EndOfStatement)) {
10307     Error(Parser.getTok().getLoc(), "unexpected token");
10308     Parser.eatToEndOfStatement();
10309     return false;
10310   }
10311 
10312   getTargetStreamer().AnnotateTLSDescriptorSequence(SRE);
10313   return false;
10314 }
10315 
10316 /// parseDirectiveMovSP
10317 ///  ::= .movsp reg [, #offset]
10318 bool ARMAsmParser::parseDirectiveMovSP(SMLoc L) {
10319   MCAsmParser &Parser = getParser();
10320   if (!UC.hasFnStart()) {
10321     Parser.eatToEndOfStatement();
10322     Error(L, ".fnstart must precede .movsp directives");
10323     return false;
10324   }
10325   if (UC.getFPReg() != ARM::SP) {
10326     Parser.eatToEndOfStatement();
10327     Error(L, "unexpected .movsp directive");
10328     return false;
10329   }
10330 
10331   SMLoc SPRegLoc = Parser.getTok().getLoc();
10332   int SPReg = tryParseRegister();
10333   if (SPReg == -1) {
10334     Parser.eatToEndOfStatement();
10335     Error(SPRegLoc, "register expected");
10336     return false;
10337   }
10338 
10339   if (SPReg == ARM::SP || SPReg == ARM::PC) {
10340     Parser.eatToEndOfStatement();
10341     Error(SPRegLoc, "sp and pc are not permitted in .movsp directive");
10342     return false;
10343   }
10344 
10345   int64_t Offset = 0;
10346   if (Parser.getTok().is(AsmToken::Comma)) {
10347     Parser.Lex();
10348 
10349     if (Parser.getTok().isNot(AsmToken::Hash)) {
10350       Error(Parser.getTok().getLoc(), "expected #constant");
10351       Parser.eatToEndOfStatement();
10352       return false;
10353     }
10354     Parser.Lex();
10355 
10356     const MCExpr *OffsetExpr;
10357     SMLoc OffsetLoc = Parser.getTok().getLoc();
10358     if (Parser.parseExpression(OffsetExpr)) {
10359       Parser.eatToEndOfStatement();
10360       Error(OffsetLoc, "malformed offset expression");
10361       return false;
10362     }
10363 
10364     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(OffsetExpr);
10365     if (!CE) {
10366       Parser.eatToEndOfStatement();
10367       Error(OffsetLoc, "offset must be an immediate constant");
10368       return false;
10369     }
10370 
10371     Offset = CE->getValue();
10372   }
10373 
10374   getTargetStreamer().emitMovSP(SPReg, Offset);
10375   UC.saveFPReg(SPReg);
10376 
10377   return false;
10378 }
10379 
10380 /// parseDirectiveObjectArch
10381 ///   ::= .object_arch name
10382 bool ARMAsmParser::parseDirectiveObjectArch(SMLoc L) {
10383   MCAsmParser &Parser = getParser();
10384   if (getLexer().isNot(AsmToken::Identifier)) {
10385     Error(getLexer().getLoc(), "unexpected token");
10386     Parser.eatToEndOfStatement();
10387     return false;
10388   }
10389 
10390   StringRef Arch = Parser.getTok().getString();
10391   SMLoc ArchLoc = Parser.getTok().getLoc();
10392   Lex();
10393 
10394   unsigned ID = ARM::parseArch(Arch);
10395 
10396   if (ID == ARM::AK_INVALID) {
10397     Error(ArchLoc, "unknown architecture '" + Arch + "'");
10398     Parser.eatToEndOfStatement();
10399     return false;
10400   }
10401 
10402   getTargetStreamer().emitObjectArch(ID);
10403 
10404   if (getLexer().isNot(AsmToken::EndOfStatement)) {
10405     Error(getLexer().getLoc(), "unexpected token");
10406     Parser.eatToEndOfStatement();
10407   }
10408 
10409   return false;
10410 }
10411 
10412 /// parseDirectiveAlign
10413 ///   ::= .align
10414 bool ARMAsmParser::parseDirectiveAlign(SMLoc L) {
10415   // NOTE: if this is not the end of the statement, fall back to the target
10416   // agnostic handling for this directive which will correctly handle this.
10417   if (getLexer().isNot(AsmToken::EndOfStatement))
10418     return true;
10419 
10420   // '.align' is target specifically handled to mean 2**2 byte alignment.
10421   const MCSection *Section = getStreamer().getCurrentSection().first;
10422   assert(Section && "must have section to emit alignment");
10423   if (Section->UseCodeAlign())
10424     getStreamer().EmitCodeAlignment(4, 0);
10425   else
10426     getStreamer().EmitValueToAlignment(4, 0, 1, 0);
10427 
10428   return false;
10429 }
10430 
10431 /// parseDirectiveThumbSet
10432 ///  ::= .thumb_set name, value
10433 bool ARMAsmParser::parseDirectiveThumbSet(SMLoc L) {
10434   MCAsmParser &Parser = getParser();
10435 
10436   StringRef Name;
10437   if (Parser.parseIdentifier(Name)) {
10438     TokError("expected identifier after '.thumb_set'");
10439     Parser.eatToEndOfStatement();
10440     return false;
10441   }
10442 
10443   if (getLexer().isNot(AsmToken::Comma)) {
10444     TokError("expected comma after name '" + Name + "'");
10445     Parser.eatToEndOfStatement();
10446     return false;
10447   }
10448   Lex();
10449 
10450   MCSymbol *Sym;
10451   const MCExpr *Value;
10452   if (MCParserUtils::parseAssignmentExpression(Name, /* allow_redef */ true,
10453                                                Parser, Sym, Value))
10454     return true;
10455 
10456   getTargetStreamer().emitThumbSet(Sym, Value);
10457   return false;
10458 }
10459 
10460 /// Force static initialization.
10461 extern "C" void LLVMInitializeARMAsmParser() {
10462   RegisterMCAsmParser<ARMAsmParser> X(TheARMLETarget);
10463   RegisterMCAsmParser<ARMAsmParser> Y(TheARMBETarget);
10464   RegisterMCAsmParser<ARMAsmParser> A(TheThumbLETarget);
10465   RegisterMCAsmParser<ARMAsmParser> B(TheThumbBETarget);
10466 }
10467 
10468 #define GET_REGISTER_MATCHER
10469 #define GET_SUBTARGET_FEATURE_NAME
10470 #define GET_MATCHER_IMPLEMENTATION
10471 #include "ARMGenAsmMatcher.inc"
10472 
10473 // FIXME: This structure should be moved inside ARMTargetParser
10474 // when we start to table-generate them, and we can use the ARM
10475 // flags below, that were generated by table-gen.
10476 static const struct {
10477   const unsigned Kind;
10478   const uint64_t ArchCheck;
10479   const FeatureBitset Features;
10480 } Extensions[] = {
10481   { ARM::AEK_CRC, Feature_HasV8, {ARM::FeatureCRC} },
10482   { ARM::AEK_CRYPTO,  Feature_HasV8,
10483     {ARM::FeatureCrypto, ARM::FeatureNEON, ARM::FeatureFPARMv8} },
10484   { ARM::AEK_FP, Feature_HasV8, {ARM::FeatureFPARMv8} },
10485   { (ARM::AEK_HWDIV | ARM::AEK_HWDIVARM), Feature_HasV7 | Feature_IsNotMClass,
10486     {ARM::FeatureHWDiv, ARM::FeatureHWDivARM} },
10487   { ARM::AEK_MP, Feature_HasV7 | Feature_IsNotMClass, {ARM::FeatureMP} },
10488   { ARM::AEK_SIMD, Feature_HasV8, {ARM::FeatureNEON, ARM::FeatureFPARMv8} },
10489   { ARM::AEK_SEC, Feature_HasV6K, {ARM::FeatureTrustZone} },
10490   // FIXME: Only available in A-class, isel not predicated
10491   { ARM::AEK_VIRT, Feature_HasV7, {ARM::FeatureVirtualization} },
10492   { ARM::AEK_FP16, Feature_HasV8_2a, {ARM::FeatureFPARMv8, ARM::FeatureFullFP16} },
10493   { ARM::AEK_RAS, Feature_HasV8, {ARM::FeatureRAS} },
10494   // FIXME: Unsupported extensions.
10495   { ARM::AEK_OS, Feature_None, {} },
10496   { ARM::AEK_IWMMXT, Feature_None, {} },
10497   { ARM::AEK_IWMMXT2, Feature_None, {} },
10498   { ARM::AEK_MAVERICK, Feature_None, {} },
10499   { ARM::AEK_XSCALE, Feature_None, {} },
10500 };
10501 
10502 /// parseDirectiveArchExtension
10503 ///   ::= .arch_extension [no]feature
10504 bool ARMAsmParser::parseDirectiveArchExtension(SMLoc L) {
10505   MCAsmParser &Parser = getParser();
10506 
10507   if (getLexer().isNot(AsmToken::Identifier)) {
10508     Error(getLexer().getLoc(), "expected architecture extension name");
10509     Parser.eatToEndOfStatement();
10510     return false;
10511   }
10512 
10513   StringRef Name = Parser.getTok().getString();
10514   SMLoc ExtLoc = Parser.getTok().getLoc();
10515   Lex();
10516 
10517   bool EnableFeature = true;
10518   if (Name.startswith_lower("no")) {
10519     EnableFeature = false;
10520     Name = Name.substr(2);
10521   }
10522   unsigned FeatureKind = ARM::parseArchExt(Name);
10523   if (FeatureKind == ARM::AEK_INVALID) {
10524     Error(ExtLoc, "unknown architectural extension: " + Name);
10525     return false;
10526   }
10527 
10528   for (const auto &Extension : Extensions) {
10529     if (Extension.Kind != FeatureKind)
10530       continue;
10531 
10532     if (Extension.Features.none()) {
10533       Error(ExtLoc, "unsupported architectural extension: " + Name);
10534       return false;
10535     }
10536 
10537     if ((getAvailableFeatures() & Extension.ArchCheck) != Extension.ArchCheck) {
10538       Error(ExtLoc, "architectural extension '" + Name + "' is not "
10539             "allowed for the current base architecture");
10540       return false;
10541     }
10542 
10543     MCSubtargetInfo &STI = copySTI();
10544     FeatureBitset ToggleFeatures = EnableFeature
10545       ? (~STI.getFeatureBits() & Extension.Features)
10546       : ( STI.getFeatureBits() & Extension.Features);
10547 
10548     uint64_t Features =
10549         ComputeAvailableFeatures(STI.ToggleFeature(ToggleFeatures));
10550     setAvailableFeatures(Features);
10551     return false;
10552   }
10553 
10554   Error(ExtLoc, "unknown architectural extension: " + Name);
10555   Parser.eatToEndOfStatement();
10556   return false;
10557 }
10558 
10559 // Define this matcher function after the auto-generated include so we
10560 // have the match class enum definitions.
10561 unsigned ARMAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
10562                                                   unsigned Kind) {
10563   ARMOperand &Op = static_cast<ARMOperand &>(AsmOp);
10564   // If the kind is a token for a literal immediate, check if our asm
10565   // operand matches. This is for InstAliases which have a fixed-value
10566   // immediate in the syntax.
10567   switch (Kind) {
10568   default: break;
10569   case MCK__35_0:
10570     if (Op.isImm())
10571       if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm()))
10572         if (CE->getValue() == 0)
10573           return Match_Success;
10574     break;
10575   case MCK_ModImm:
10576     if (Op.isImm()) {
10577       const MCExpr *SOExpr = Op.getImm();
10578       int64_t Value;
10579       if (!SOExpr->evaluateAsAbsolute(Value))
10580         return Match_Success;
10581       assert((Value >= INT32_MIN && Value <= UINT32_MAX) &&
10582              "expression value must be representable in 32 bits");
10583     }
10584     break;
10585   case MCK_rGPR:
10586     if (hasV8Ops() && Op.isReg() && Op.getReg() == ARM::SP)
10587       return Match_Success;
10588     break;
10589   case MCK_GPRPair:
10590     if (Op.isReg() &&
10591         MRI->getRegClass(ARM::GPRRegClassID).contains(Op.getReg()))
10592       return Match_Success;
10593     break;
10594   }
10595   return Match_InvalidOperand;
10596 }
10597