[AArch64] Cleanup AsmParser: no need to use dyn_cast + assert. cast does it for us.
[oota-llvm.git] / lib / Target / AArch64 / AsmParser / AArch64AsmParser.cpp
1 //==- AArch64AsmParser.cpp - Parse AArch64 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 "MCTargetDesc/AArch64AddressingModes.h"
11 #include "MCTargetDesc/AArch64MCExpr.h"
12 #include "Utils/AArch64BaseInfo.h"
13 #include "llvm/MC/MCParser/MCAsmLexer.h"
14 #include "llvm/MC/MCParser/MCAsmParser.h"
15 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCRegisterInfo.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/MC/MCSubtargetInfo.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/MC/MCTargetAsmParser.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "llvm/Support/TargetRegistry.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include "llvm/ADT/StringSwitch.h"
32 #include "llvm/ADT/Twine.h"
33 #include <cstdio>
34 using namespace llvm;
35
36 namespace {
37
38 class AArch64Operand;
39
40 class AArch64AsmParser : public MCTargetAsmParser {
41 private:
42   StringRef Mnemonic; ///< Instruction mnemonic.
43   MCSubtargetInfo &STI;
44   MCAsmParser &Parser;
45
46   // Map of register aliases registers via the .req directive.
47   StringMap<std::pair<bool, unsigned> > RegisterReqs;
48
49   AArch64TargetStreamer &getTargetStreamer() {
50     MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
51     return static_cast<AArch64TargetStreamer &>(TS);
52   }
53
54   MCAsmParser &getParser() const { return Parser; }
55   MCAsmLexer &getLexer() const { return Parser.getLexer(); }
56
57   SMLoc getLoc() const { return Parser.getTok().getLoc(); }
58
59   bool parseSysAlias(StringRef Name, SMLoc NameLoc, OperandVector &Operands);
60   AArch64CC::CondCode parseCondCodeString(StringRef Cond);
61   bool parseCondCode(OperandVector &Operands, bool invertCondCode);
62   unsigned matchRegisterNameAlias(StringRef Name, bool isVector);
63   int tryParseRegister();
64   int tryMatchVectorRegister(StringRef &Kind, bool expected);
65   bool parseRegister(OperandVector &Operands);
66   bool parseSymbolicImmVal(const MCExpr *&ImmVal);
67   bool parseVectorList(OperandVector &Operands);
68   bool parseOperand(OperandVector &Operands, bool isCondCode,
69                     bool invertCondCode);
70
71   void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
72   bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
73   bool showMatchError(SMLoc Loc, unsigned ErrCode);
74
75   bool parseDirectiveWord(unsigned Size, SMLoc L);
76   bool parseDirectiveTLSDescCall(SMLoc L);
77
78   bool parseDirectiveLOH(StringRef LOH, SMLoc L);
79   bool parseDirectiveLtorg(SMLoc L);
80
81   bool parseDirectiveReq(StringRef Name, SMLoc L);
82   bool parseDirectiveUnreq(SMLoc L);
83
84   bool validateInstruction(MCInst &Inst, SmallVectorImpl<SMLoc> &Loc);
85   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
86                                OperandVector &Operands, MCStreamer &Out,
87                                unsigned &ErrorInfo,
88                                bool MatchingInlineAsm) override;
89 /// @name Auto-generated Match Functions
90 /// {
91
92 #define GET_ASSEMBLER_HEADER
93 #include "AArch64GenAsmMatcher.inc"
94
95   /// }
96
97   OperandMatchResultTy tryParseOptionalShiftExtend(OperandVector &Operands);
98   OperandMatchResultTy tryParseBarrierOperand(OperandVector &Operands);
99   OperandMatchResultTy tryParseMRSSystemRegister(OperandVector &Operands);
100   OperandMatchResultTy tryParseSysReg(OperandVector &Operands);
101   OperandMatchResultTy tryParseSysCROperand(OperandVector &Operands);
102   OperandMatchResultTy tryParsePrefetch(OperandVector &Operands);
103   OperandMatchResultTy tryParseAdrpLabel(OperandVector &Operands);
104   OperandMatchResultTy tryParseAdrLabel(OperandVector &Operands);
105   OperandMatchResultTy tryParseFPImm(OperandVector &Operands);
106   OperandMatchResultTy tryParseAddSubImm(OperandVector &Operands);
107   OperandMatchResultTy tryParseGPR64sp0Operand(OperandVector &Operands);
108   bool tryParseVectorRegister(OperandVector &Operands);
109
110 public:
111   enum AArch64MatchResultTy {
112     Match_InvalidSuffix = FIRST_TARGET_MATCH_RESULT_TY,
113 #define GET_OPERAND_DIAGNOSTIC_TYPES
114 #include "AArch64GenAsmMatcher.inc"
115   };
116   AArch64AsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser,
117                  const MCInstrInfo &MII,
118                  const MCTargetOptions &Options)
119       : MCTargetAsmParser(), STI(_STI), Parser(_Parser) {
120     MCAsmParserExtension::Initialize(_Parser);
121     if (Parser.getStreamer().getTargetStreamer() == nullptr)
122       new AArch64TargetStreamer(Parser.getStreamer());
123
124     // Initialize the set of available features.
125     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
126   }
127
128   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
129                         SMLoc NameLoc, OperandVector &Operands) override;
130   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
131   bool ParseDirective(AsmToken DirectiveID) override;
132   unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
133                                       unsigned Kind) override;
134
135   static bool classifySymbolRef(const MCExpr *Expr,
136                                 AArch64MCExpr::VariantKind &ELFRefKind,
137                                 MCSymbolRefExpr::VariantKind &DarwinRefKind,
138                                 int64_t &Addend);
139 };
140 } // end anonymous namespace
141
142 namespace {
143
144 /// AArch64Operand - Instances of this class represent a parsed AArch64 machine
145 /// instruction.
146 class AArch64Operand : public MCParsedAsmOperand {
147 private:
148   enum KindTy {
149     k_Immediate,
150     k_ShiftedImm,
151     k_CondCode,
152     k_Register,
153     k_VectorList,
154     k_VectorIndex,
155     k_Token,
156     k_SysReg,
157     k_SysCR,
158     k_Prefetch,
159     k_ShiftExtend,
160     k_FPImm,
161     k_Barrier
162   } Kind;
163
164   SMLoc StartLoc, EndLoc;
165
166   struct TokOp {
167     const char *Data;
168     unsigned Length;
169     bool IsSuffix; // Is the operand actually a suffix on the mnemonic.
170   };
171
172   struct RegOp {
173     unsigned RegNum;
174     bool isVector;
175   };
176
177   struct VectorListOp {
178     unsigned RegNum;
179     unsigned Count;
180     unsigned NumElements;
181     unsigned ElementKind;
182   };
183
184   struct VectorIndexOp {
185     unsigned Val;
186   };
187
188   struct ImmOp {
189     const MCExpr *Val;
190   };
191
192   struct ShiftedImmOp {
193     const MCExpr *Val;
194     unsigned ShiftAmount;
195   };
196
197   struct CondCodeOp {
198     AArch64CC::CondCode Code;
199   };
200
201   struct FPImmOp {
202     unsigned Val; // Encoded 8-bit representation.
203   };
204
205   struct BarrierOp {
206     unsigned Val; // Not the enum since not all values have names.
207   };
208
209   struct SysRegOp {
210     const char *Data;
211     unsigned Length;
212     uint64_t FeatureBits; // We need to pass through information about which
213                           // core we are compiling for so that the SysReg
214                           // Mappers can appropriately conditionalize.
215   };
216
217   struct SysCRImmOp {
218     unsigned Val;
219   };
220
221   struct PrefetchOp {
222     unsigned Val;
223   };
224
225   struct ShiftExtendOp {
226     AArch64_AM::ShiftExtendType Type;
227     unsigned Amount;
228     bool HasExplicitAmount;
229   };
230
231   struct ExtendOp {
232     unsigned Val;
233   };
234
235   union {
236     struct TokOp Tok;
237     struct RegOp Reg;
238     struct VectorListOp VectorList;
239     struct VectorIndexOp VectorIndex;
240     struct ImmOp Imm;
241     struct ShiftedImmOp ShiftedImm;
242     struct CondCodeOp CondCode;
243     struct FPImmOp FPImm;
244     struct BarrierOp Barrier;
245     struct SysRegOp SysReg;
246     struct SysCRImmOp SysCRImm;
247     struct PrefetchOp Prefetch;
248     struct ShiftExtendOp ShiftExtend;
249   };
250
251   // Keep the MCContext around as the MCExprs may need manipulated during
252   // the add<>Operands() calls.
253   MCContext &Ctx;
254
255 public:
256   AArch64Operand(KindTy K, MCContext &_Ctx)
257       : MCParsedAsmOperand(), Kind(K), Ctx(_Ctx) {}
258
259   AArch64Operand(const AArch64Operand &o) : MCParsedAsmOperand(), Ctx(o.Ctx) {
260     Kind = o.Kind;
261     StartLoc = o.StartLoc;
262     EndLoc = o.EndLoc;
263     switch (Kind) {
264     case k_Token:
265       Tok = o.Tok;
266       break;
267     case k_Immediate:
268       Imm = o.Imm;
269       break;
270     case k_ShiftedImm:
271       ShiftedImm = o.ShiftedImm;
272       break;
273     case k_CondCode:
274       CondCode = o.CondCode;
275       break;
276     case k_FPImm:
277       FPImm = o.FPImm;
278       break;
279     case k_Barrier:
280       Barrier = o.Barrier;
281       break;
282     case k_Register:
283       Reg = o.Reg;
284       break;
285     case k_VectorList:
286       VectorList = o.VectorList;
287       break;
288     case k_VectorIndex:
289       VectorIndex = o.VectorIndex;
290       break;
291     case k_SysReg:
292       SysReg = o.SysReg;
293       break;
294     case k_SysCR:
295       SysCRImm = o.SysCRImm;
296       break;
297     case k_Prefetch:
298       Prefetch = o.Prefetch;
299       break;
300     case k_ShiftExtend:
301       ShiftExtend = o.ShiftExtend;
302       break;
303     }
304   }
305
306   /// getStartLoc - Get the location of the first token of this operand.
307   SMLoc getStartLoc() const override { return StartLoc; }
308   /// getEndLoc - Get the location of the last token of this operand.
309   SMLoc getEndLoc() const override { return EndLoc; }
310
311   StringRef getToken() const {
312     assert(Kind == k_Token && "Invalid access!");
313     return StringRef(Tok.Data, Tok.Length);
314   }
315
316   bool isTokenSuffix() const {
317     assert(Kind == k_Token && "Invalid access!");
318     return Tok.IsSuffix;
319   }
320
321   const MCExpr *getImm() const {
322     assert(Kind == k_Immediate && "Invalid access!");
323     return Imm.Val;
324   }
325
326   const MCExpr *getShiftedImmVal() const {
327     assert(Kind == k_ShiftedImm && "Invalid access!");
328     return ShiftedImm.Val;
329   }
330
331   unsigned getShiftedImmShift() const {
332     assert(Kind == k_ShiftedImm && "Invalid access!");
333     return ShiftedImm.ShiftAmount;
334   }
335
336   AArch64CC::CondCode getCondCode() const {
337     assert(Kind == k_CondCode && "Invalid access!");
338     return CondCode.Code;
339   }
340
341   unsigned getFPImm() const {
342     assert(Kind == k_FPImm && "Invalid access!");
343     return FPImm.Val;
344   }
345
346   unsigned getBarrier() const {
347     assert(Kind == k_Barrier && "Invalid access!");
348     return Barrier.Val;
349   }
350
351   unsigned getReg() const override {
352     assert(Kind == k_Register && "Invalid access!");
353     return Reg.RegNum;
354   }
355
356   unsigned getVectorListStart() const {
357     assert(Kind == k_VectorList && "Invalid access!");
358     return VectorList.RegNum;
359   }
360
361   unsigned getVectorListCount() const {
362     assert(Kind == k_VectorList && "Invalid access!");
363     return VectorList.Count;
364   }
365
366   unsigned getVectorIndex() const {
367     assert(Kind == k_VectorIndex && "Invalid access!");
368     return VectorIndex.Val;
369   }
370
371   StringRef getSysReg() const {
372     assert(Kind == k_SysReg && "Invalid access!");
373     return StringRef(SysReg.Data, SysReg.Length);
374   }
375
376   uint64_t getSysRegFeatureBits() const {
377     assert(Kind == k_SysReg && "Invalid access!");
378     return SysReg.FeatureBits;
379   }
380
381   unsigned getSysCR() const {
382     assert(Kind == k_SysCR && "Invalid access!");
383     return SysCRImm.Val;
384   }
385
386   unsigned getPrefetch() const {
387     assert(Kind == k_Prefetch && "Invalid access!");
388     return Prefetch.Val;
389   }
390
391   AArch64_AM::ShiftExtendType getShiftExtendType() const {
392     assert(Kind == k_ShiftExtend && "Invalid access!");
393     return ShiftExtend.Type;
394   }
395
396   unsigned getShiftExtendAmount() const {
397     assert(Kind == k_ShiftExtend && "Invalid access!");
398     return ShiftExtend.Amount;
399   }
400
401   bool hasShiftExtendAmount() const {
402     assert(Kind == k_ShiftExtend && "Invalid access!");
403     return ShiftExtend.HasExplicitAmount;
404   }
405
406   bool isImm() const override { return Kind == k_Immediate; }
407   bool isMem() const override { return false; }
408   bool isSImm9() const {
409     if (!isImm())
410       return false;
411     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
412     if (!MCE)
413       return false;
414     int64_t Val = MCE->getValue();
415     return (Val >= -256 && Val < 256);
416   }
417   bool isSImm7s4() const {
418     if (!isImm())
419       return false;
420     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
421     if (!MCE)
422       return false;
423     int64_t Val = MCE->getValue();
424     return (Val >= -256 && Val <= 252 && (Val & 3) == 0);
425   }
426   bool isSImm7s8() const {
427     if (!isImm())
428       return false;
429     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
430     if (!MCE)
431       return false;
432     int64_t Val = MCE->getValue();
433     return (Val >= -512 && Val <= 504 && (Val & 7) == 0);
434   }
435   bool isSImm7s16() const {
436     if (!isImm())
437       return false;
438     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
439     if (!MCE)
440       return false;
441     int64_t Val = MCE->getValue();
442     return (Val >= -1024 && Val <= 1008 && (Val & 15) == 0);
443   }
444
445   bool isSymbolicUImm12Offset(const MCExpr *Expr, unsigned Scale) const {
446     AArch64MCExpr::VariantKind ELFRefKind;
447     MCSymbolRefExpr::VariantKind DarwinRefKind;
448     int64_t Addend;
449     if (!AArch64AsmParser::classifySymbolRef(Expr, ELFRefKind, DarwinRefKind,
450                                            Addend)) {
451       // If we don't understand the expression, assume the best and
452       // let the fixup and relocation code deal with it.
453       return true;
454     }
455
456     if (DarwinRefKind == MCSymbolRefExpr::VK_PAGEOFF ||
457         ELFRefKind == AArch64MCExpr::VK_LO12 ||
458         ELFRefKind == AArch64MCExpr::VK_GOT_LO12 ||
459         ELFRefKind == AArch64MCExpr::VK_DTPREL_LO12 ||
460         ELFRefKind == AArch64MCExpr::VK_DTPREL_LO12_NC ||
461         ELFRefKind == AArch64MCExpr::VK_TPREL_LO12 ||
462         ELFRefKind == AArch64MCExpr::VK_TPREL_LO12_NC ||
463         ELFRefKind == AArch64MCExpr::VK_GOTTPREL_LO12_NC ||
464         ELFRefKind == AArch64MCExpr::VK_TLSDESC_LO12) {
465       // Note that we don't range-check the addend. It's adjusted modulo page
466       // size when converted, so there is no "out of range" condition when using
467       // @pageoff.
468       return Addend >= 0 && (Addend % Scale) == 0;
469     } else if (DarwinRefKind == MCSymbolRefExpr::VK_GOTPAGEOFF ||
470                DarwinRefKind == MCSymbolRefExpr::VK_TLVPPAGEOFF) {
471       // @gotpageoff/@tlvppageoff can only be used directly, not with an addend.
472       return Addend == 0;
473     }
474
475     return false;
476   }
477
478   template <int Scale> bool isUImm12Offset() const {
479     if (!isImm())
480       return false;
481
482     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
483     if (!MCE)
484       return isSymbolicUImm12Offset(getImm(), Scale);
485
486     int64_t Val = MCE->getValue();
487     return (Val % Scale) == 0 && Val >= 0 && (Val / Scale) < 0x1000;
488   }
489
490   bool isImm0_7() const {
491     if (!isImm())
492       return false;
493     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
494     if (!MCE)
495       return false;
496     int64_t Val = MCE->getValue();
497     return (Val >= 0 && Val < 8);
498   }
499   bool isImm1_8() const {
500     if (!isImm())
501       return false;
502     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
503     if (!MCE)
504       return false;
505     int64_t Val = MCE->getValue();
506     return (Val > 0 && Val < 9);
507   }
508   bool isImm0_15() const {
509     if (!isImm())
510       return false;
511     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
512     if (!MCE)
513       return false;
514     int64_t Val = MCE->getValue();
515     return (Val >= 0 && Val < 16);
516   }
517   bool isImm1_16() const {
518     if (!isImm())
519       return false;
520     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
521     if (!MCE)
522       return false;
523     int64_t Val = MCE->getValue();
524     return (Val > 0 && Val < 17);
525   }
526   bool isImm0_31() const {
527     if (!isImm())
528       return false;
529     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
530     if (!MCE)
531       return false;
532     int64_t Val = MCE->getValue();
533     return (Val >= 0 && Val < 32);
534   }
535   bool isImm1_31() const {
536     if (!isImm())
537       return false;
538     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
539     if (!MCE)
540       return false;
541     int64_t Val = MCE->getValue();
542     return (Val >= 1 && Val < 32);
543   }
544   bool isImm1_32() const {
545     if (!isImm())
546       return false;
547     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
548     if (!MCE)
549       return false;
550     int64_t Val = MCE->getValue();
551     return (Val >= 1 && Val < 33);
552   }
553   bool isImm0_63() const {
554     if (!isImm())
555       return false;
556     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
557     if (!MCE)
558       return false;
559     int64_t Val = MCE->getValue();
560     return (Val >= 0 && Val < 64);
561   }
562   bool isImm1_63() const {
563     if (!isImm())
564       return false;
565     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
566     if (!MCE)
567       return false;
568     int64_t Val = MCE->getValue();
569     return (Val >= 1 && Val < 64);
570   }
571   bool isImm1_64() const {
572     if (!isImm())
573       return false;
574     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
575     if (!MCE)
576       return false;
577     int64_t Val = MCE->getValue();
578     return (Val >= 1 && Val < 65);
579   }
580   bool isImm0_127() const {
581     if (!isImm())
582       return false;
583     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
584     if (!MCE)
585       return false;
586     int64_t Val = MCE->getValue();
587     return (Val >= 0 && Val < 128);
588   }
589   bool isImm0_255() const {
590     if (!isImm())
591       return false;
592     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
593     if (!MCE)
594       return false;
595     int64_t Val = MCE->getValue();
596     return (Val >= 0 && Val < 256);
597   }
598   bool isImm0_65535() const {
599     if (!isImm())
600       return false;
601     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
602     if (!MCE)
603       return false;
604     int64_t Val = MCE->getValue();
605     return (Val >= 0 && Val < 65536);
606   }
607   bool isImm32_63() const {
608     if (!isImm())
609       return false;
610     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
611     if (!MCE)
612       return false;
613     int64_t Val = MCE->getValue();
614     return (Val >= 32 && Val < 64);
615   }
616   bool isLogicalImm32() const {
617     if (!isImm())
618       return false;
619     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
620     if (!MCE)
621       return false;
622     int64_t Val = MCE->getValue();
623     if (Val >> 32 != 0 && Val >> 32 != ~0LL)
624       return false;
625     Val &= 0xFFFFFFFF;
626     return AArch64_AM::isLogicalImmediate(Val, 32);
627   }
628   bool isLogicalImm64() const {
629     if (!isImm())
630       return false;
631     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
632     if (!MCE)
633       return false;
634     return AArch64_AM::isLogicalImmediate(MCE->getValue(), 64);
635   }
636   bool isLogicalImm32Not() const {
637     if (!isImm())
638       return false;
639     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
640     if (!MCE)
641       return false;
642     int64_t Val = ~MCE->getValue() & 0xFFFFFFFF;
643     return AArch64_AM::isLogicalImmediate(Val, 32);
644   }
645   bool isLogicalImm64Not() const {
646     if (!isImm())
647       return false;
648     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
649     if (!MCE)
650       return false;
651     return AArch64_AM::isLogicalImmediate(~MCE->getValue(), 64);
652   }
653   bool isShiftedImm() const { return Kind == k_ShiftedImm; }
654   bool isAddSubImm() const {
655     if (!isShiftedImm() && !isImm())
656       return false;
657
658     const MCExpr *Expr;
659
660     // An ADD/SUB shifter is either 'lsl #0' or 'lsl #12'.
661     if (isShiftedImm()) {
662       unsigned Shift = ShiftedImm.ShiftAmount;
663       Expr = ShiftedImm.Val;
664       if (Shift != 0 && Shift != 12)
665         return false;
666     } else {
667       Expr = getImm();
668     }
669
670     AArch64MCExpr::VariantKind ELFRefKind;
671     MCSymbolRefExpr::VariantKind DarwinRefKind;
672     int64_t Addend;
673     if (AArch64AsmParser::classifySymbolRef(Expr, ELFRefKind,
674                                           DarwinRefKind, Addend)) {
675       return DarwinRefKind == MCSymbolRefExpr::VK_PAGEOFF
676           || DarwinRefKind == MCSymbolRefExpr::VK_TLVPPAGEOFF
677           || (DarwinRefKind == MCSymbolRefExpr::VK_GOTPAGEOFF && Addend == 0)
678           || ELFRefKind == AArch64MCExpr::VK_LO12
679           || ELFRefKind == AArch64MCExpr::VK_DTPREL_HI12
680           || ELFRefKind == AArch64MCExpr::VK_DTPREL_LO12
681           || ELFRefKind == AArch64MCExpr::VK_DTPREL_LO12_NC
682           || ELFRefKind == AArch64MCExpr::VK_TPREL_HI12
683           || ELFRefKind == AArch64MCExpr::VK_TPREL_LO12
684           || ELFRefKind == AArch64MCExpr::VK_TPREL_LO12_NC
685           || ELFRefKind == AArch64MCExpr::VK_TLSDESC_LO12;
686     }
687
688     // Otherwise it should be a real immediate in range:
689     const MCConstantExpr *CE = cast<MCConstantExpr>(Expr);
690     return CE->getValue() >= 0 && CE->getValue() <= 0xfff;
691   }
692   bool isCondCode() const { return Kind == k_CondCode; }
693   bool isSIMDImmType10() const {
694     if (!isImm())
695       return false;
696     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
697     if (!MCE)
698       return false;
699     return AArch64_AM::isAdvSIMDModImmType10(MCE->getValue());
700   }
701   bool isBranchTarget26() const {
702     if (!isImm())
703       return false;
704     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
705     if (!MCE)
706       return true;
707     int64_t Val = MCE->getValue();
708     if (Val & 0x3)
709       return false;
710     return (Val >= -(0x2000000 << 2) && Val <= (0x1ffffff << 2));
711   }
712   bool isPCRelLabel19() const {
713     if (!isImm())
714       return false;
715     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
716     if (!MCE)
717       return true;
718     int64_t Val = MCE->getValue();
719     if (Val & 0x3)
720       return false;
721     return (Val >= -(0x40000 << 2) && Val <= (0x3ffff << 2));
722   }
723   bool isBranchTarget14() const {
724     if (!isImm())
725       return false;
726     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
727     if (!MCE)
728       return true;
729     int64_t Val = MCE->getValue();
730     if (Val & 0x3)
731       return false;
732     return (Val >= -(0x2000 << 2) && Val <= (0x1fff << 2));
733   }
734
735   bool
736   isMovWSymbol(ArrayRef<AArch64MCExpr::VariantKind> AllowedModifiers) const {
737     if (!isImm())
738       return false;
739
740     AArch64MCExpr::VariantKind ELFRefKind;
741     MCSymbolRefExpr::VariantKind DarwinRefKind;
742     int64_t Addend;
743     if (!AArch64AsmParser::classifySymbolRef(getImm(), ELFRefKind,
744                                              DarwinRefKind, Addend)) {
745       return false;
746     }
747     if (DarwinRefKind != MCSymbolRefExpr::VK_None)
748       return false;
749
750     for (unsigned i = 0; i != AllowedModifiers.size(); ++i) {
751       if (ELFRefKind == AllowedModifiers[i])
752         return Addend == 0;
753     }
754
755     return false;
756   }
757
758   bool isMovZSymbolG3() const {
759     static AArch64MCExpr::VariantKind Variants[] = { AArch64MCExpr::VK_ABS_G3 };
760     return isMovWSymbol(Variants);
761   }
762
763   bool isMovZSymbolG2() const {
764     static AArch64MCExpr::VariantKind Variants[] = {
765         AArch64MCExpr::VK_ABS_G2, AArch64MCExpr::VK_ABS_G2_S,
766         AArch64MCExpr::VK_TPREL_G2, AArch64MCExpr::VK_DTPREL_G2};
767     return isMovWSymbol(Variants);
768   }
769
770   bool isMovZSymbolG1() const {
771     static AArch64MCExpr::VariantKind Variants[] = {
772         AArch64MCExpr::VK_ABS_G1,      AArch64MCExpr::VK_ABS_G1_S,
773         AArch64MCExpr::VK_GOTTPREL_G1, AArch64MCExpr::VK_TPREL_G1,
774         AArch64MCExpr::VK_DTPREL_G1,
775     };
776     return isMovWSymbol(Variants);
777   }
778
779   bool isMovZSymbolG0() const {
780     static AArch64MCExpr::VariantKind Variants[] = {
781         AArch64MCExpr::VK_ABS_G0, AArch64MCExpr::VK_ABS_G0_S,
782         AArch64MCExpr::VK_TPREL_G0, AArch64MCExpr::VK_DTPREL_G0};
783     return isMovWSymbol(Variants);
784   }
785
786   bool isMovKSymbolG3() const {
787     static AArch64MCExpr::VariantKind Variants[] = { AArch64MCExpr::VK_ABS_G3 };
788     return isMovWSymbol(Variants);
789   }
790
791   bool isMovKSymbolG2() const {
792     static AArch64MCExpr::VariantKind Variants[] = {
793         AArch64MCExpr::VK_ABS_G2_NC};
794     return isMovWSymbol(Variants);
795   }
796
797   bool isMovKSymbolG1() const {
798     static AArch64MCExpr::VariantKind Variants[] = {
799       AArch64MCExpr::VK_ABS_G1_NC, AArch64MCExpr::VK_TPREL_G1_NC,
800       AArch64MCExpr::VK_DTPREL_G1_NC
801     };
802     return isMovWSymbol(Variants);
803   }
804
805   bool isMovKSymbolG0() const {
806     static AArch64MCExpr::VariantKind Variants[] = {
807       AArch64MCExpr::VK_ABS_G0_NC,   AArch64MCExpr::VK_GOTTPREL_G0_NC,
808       AArch64MCExpr::VK_TPREL_G0_NC, AArch64MCExpr::VK_DTPREL_G0_NC
809     };
810     return isMovWSymbol(Variants);
811   }
812
813   template<int RegWidth, int Shift>
814   bool isMOVZMovAlias() const {
815     if (!isImm()) return false;
816
817     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
818     if (!CE) return false;
819     uint64_t Value = CE->getValue();
820
821     if (RegWidth == 32)
822       Value &= 0xffffffffULL;
823
824     // "lsl #0" takes precedence: in practice this only affects "#0, lsl #0".
825     if (Value == 0 && Shift != 0)
826       return false;
827
828     return (Value & ~(0xffffULL << Shift)) == 0;
829   }
830
831   template<int RegWidth, int Shift>
832   bool isMOVNMovAlias() const {
833     if (!isImm()) return false;
834
835     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
836     if (!CE) return false;
837     uint64_t Value = CE->getValue();
838
839     // MOVZ takes precedence over MOVN.
840     for (int MOVZShift = 0; MOVZShift <= 48; MOVZShift += 16)
841       if ((Value & ~(0xffffULL << MOVZShift)) == 0)
842         return false;
843
844     Value = ~Value;
845     if (RegWidth == 32)
846       Value &= 0xffffffffULL;
847
848     return (Value & ~(0xffffULL << Shift)) == 0;
849   }
850
851   bool isFPImm() const { return Kind == k_FPImm; }
852   bool isBarrier() const { return Kind == k_Barrier; }
853   bool isSysReg() const { return Kind == k_SysReg; }
854   bool isMRSSystemRegister() const {
855     if (!isSysReg()) return false;
856
857     bool IsKnownRegister;
858     auto Mapper = AArch64SysReg::MRSMapper(getSysRegFeatureBits());
859     Mapper.fromString(getSysReg(), IsKnownRegister);
860
861     return IsKnownRegister;
862   }
863   bool isMSRSystemRegister() const {
864     if (!isSysReg()) return false;
865
866     bool IsKnownRegister;
867     auto Mapper = AArch64SysReg::MSRMapper(getSysRegFeatureBits());
868     Mapper.fromString(getSysReg(), IsKnownRegister);
869
870     return IsKnownRegister;
871   }
872   bool isSystemPStateField() const {
873     if (!isSysReg()) return false;
874
875     bool IsKnownRegister;
876     AArch64PState::PStateMapper().fromString(getSysReg(), IsKnownRegister);
877
878     return IsKnownRegister;
879   }
880   bool isReg() const override { return Kind == k_Register && !Reg.isVector; }
881   bool isVectorReg() const { return Kind == k_Register && Reg.isVector; }
882   bool isVectorRegLo() const {
883     return Kind == k_Register && Reg.isVector &&
884            AArch64MCRegisterClasses[AArch64::FPR128_loRegClassID].contains(
885                Reg.RegNum);
886   }
887   bool isGPR32as64() const {
888     return Kind == k_Register && !Reg.isVector &&
889       AArch64MCRegisterClasses[AArch64::GPR64RegClassID].contains(Reg.RegNum);
890   }
891
892   bool isGPR64sp0() const {
893     return Kind == k_Register && !Reg.isVector &&
894       AArch64MCRegisterClasses[AArch64::GPR64spRegClassID].contains(Reg.RegNum);
895   }
896
897   /// Is this a vector list with the type implicit (presumably attached to the
898   /// instruction itself)?
899   template <unsigned NumRegs> bool isImplicitlyTypedVectorList() const {
900     return Kind == k_VectorList && VectorList.Count == NumRegs &&
901            !VectorList.ElementKind;
902   }
903
904   template <unsigned NumRegs, unsigned NumElements, char ElementKind>
905   bool isTypedVectorList() const {
906     if (Kind != k_VectorList)
907       return false;
908     if (VectorList.Count != NumRegs)
909       return false;
910     if (VectorList.ElementKind != ElementKind)
911       return false;
912     return VectorList.NumElements == NumElements;
913   }
914
915   bool isVectorIndex1() const {
916     return Kind == k_VectorIndex && VectorIndex.Val == 1;
917   }
918   bool isVectorIndexB() const {
919     return Kind == k_VectorIndex && VectorIndex.Val < 16;
920   }
921   bool isVectorIndexH() const {
922     return Kind == k_VectorIndex && VectorIndex.Val < 8;
923   }
924   bool isVectorIndexS() const {
925     return Kind == k_VectorIndex && VectorIndex.Val < 4;
926   }
927   bool isVectorIndexD() const {
928     return Kind == k_VectorIndex && VectorIndex.Val < 2;
929   }
930   bool isToken() const override { return Kind == k_Token; }
931   bool isTokenEqual(StringRef Str) const {
932     return Kind == k_Token && getToken() == Str;
933   }
934   bool isSysCR() const { return Kind == k_SysCR; }
935   bool isPrefetch() const { return Kind == k_Prefetch; }
936   bool isShiftExtend() const { return Kind == k_ShiftExtend; }
937   bool isShifter() const {
938     if (!isShiftExtend())
939       return false;
940
941     AArch64_AM::ShiftExtendType ST = getShiftExtendType();
942     return (ST == AArch64_AM::LSL || ST == AArch64_AM::LSR ||
943             ST == AArch64_AM::ASR || ST == AArch64_AM::ROR ||
944             ST == AArch64_AM::MSL);
945   }
946   bool isExtend() const {
947     if (!isShiftExtend())
948       return false;
949
950     AArch64_AM::ShiftExtendType ET = getShiftExtendType();
951     return (ET == AArch64_AM::UXTB || ET == AArch64_AM::SXTB ||
952             ET == AArch64_AM::UXTH || ET == AArch64_AM::SXTH ||
953             ET == AArch64_AM::UXTW || ET == AArch64_AM::SXTW ||
954             ET == AArch64_AM::UXTX || ET == AArch64_AM::SXTX ||
955             ET == AArch64_AM::LSL) &&
956            getShiftExtendAmount() <= 4;
957   }
958
959   bool isExtend64() const {
960     if (!isExtend())
961       return false;
962     // UXTX and SXTX require a 64-bit source register (the ExtendLSL64 class).
963     AArch64_AM::ShiftExtendType ET = getShiftExtendType();
964     return ET != AArch64_AM::UXTX && ET != AArch64_AM::SXTX;
965   }
966   bool isExtendLSL64() const {
967     if (!isExtend())
968       return false;
969     AArch64_AM::ShiftExtendType ET = getShiftExtendType();
970     return (ET == AArch64_AM::UXTX || ET == AArch64_AM::SXTX ||
971             ET == AArch64_AM::LSL) &&
972            getShiftExtendAmount() <= 4;
973   }
974
975   template<int Width> bool isMemXExtend() const {
976     if (!isExtend())
977       return false;
978     AArch64_AM::ShiftExtendType ET = getShiftExtendType();
979     return (ET == AArch64_AM::LSL || ET == AArch64_AM::SXTX) &&
980            (getShiftExtendAmount() == Log2_32(Width / 8) ||
981             getShiftExtendAmount() == 0);
982   }
983
984   template<int Width> bool isMemWExtend() const {
985     if (!isExtend())
986       return false;
987     AArch64_AM::ShiftExtendType ET = getShiftExtendType();
988     return (ET == AArch64_AM::UXTW || ET == AArch64_AM::SXTW) &&
989            (getShiftExtendAmount() == Log2_32(Width / 8) ||
990             getShiftExtendAmount() == 0);
991   }
992
993   template <unsigned width>
994   bool isArithmeticShifter() const {
995     if (!isShifter())
996       return false;
997
998     // An arithmetic shifter is LSL, LSR, or ASR.
999     AArch64_AM::ShiftExtendType ST = getShiftExtendType();
1000     return (ST == AArch64_AM::LSL || ST == AArch64_AM::LSR ||
1001             ST == AArch64_AM::ASR) && getShiftExtendAmount() < width;
1002   }
1003
1004   template <unsigned width>
1005   bool isLogicalShifter() const {
1006     if (!isShifter())
1007       return false;
1008
1009     // A logical shifter is LSL, LSR, ASR or ROR.
1010     AArch64_AM::ShiftExtendType ST = getShiftExtendType();
1011     return (ST == AArch64_AM::LSL || ST == AArch64_AM::LSR ||
1012             ST == AArch64_AM::ASR || ST == AArch64_AM::ROR) &&
1013            getShiftExtendAmount() < width;
1014   }
1015
1016   bool isMovImm32Shifter() const {
1017     if (!isShifter())
1018       return false;
1019
1020     // A MOVi shifter is LSL of 0, 16, 32, or 48.
1021     AArch64_AM::ShiftExtendType ST = getShiftExtendType();
1022     if (ST != AArch64_AM::LSL)
1023       return false;
1024     uint64_t Val = getShiftExtendAmount();
1025     return (Val == 0 || Val == 16);
1026   }
1027
1028   bool isMovImm64Shifter() const {
1029     if (!isShifter())
1030       return false;
1031
1032     // A MOVi shifter is LSL of 0 or 16.
1033     AArch64_AM::ShiftExtendType ST = getShiftExtendType();
1034     if (ST != AArch64_AM::LSL)
1035       return false;
1036     uint64_t Val = getShiftExtendAmount();
1037     return (Val == 0 || Val == 16 || Val == 32 || Val == 48);
1038   }
1039
1040   bool isLogicalVecShifter() const {
1041     if (!isShifter())
1042       return false;
1043
1044     // A logical vector shifter is a left shift by 0, 8, 16, or 24.
1045     unsigned Shift = getShiftExtendAmount();
1046     return getShiftExtendType() == AArch64_AM::LSL &&
1047            (Shift == 0 || Shift == 8 || Shift == 16 || Shift == 24);
1048   }
1049
1050   bool isLogicalVecHalfWordShifter() const {
1051     if (!isLogicalVecShifter())
1052       return false;
1053
1054     // A logical vector shifter is a left shift by 0 or 8.
1055     unsigned Shift = getShiftExtendAmount();
1056     return getShiftExtendType() == AArch64_AM::LSL &&
1057            (Shift == 0 || Shift == 8);
1058   }
1059
1060   bool isMoveVecShifter() const {
1061     if (!isShiftExtend())
1062       return false;
1063
1064     // A logical vector shifter is a left shift by 8 or 16.
1065     unsigned Shift = getShiftExtendAmount();
1066     return getShiftExtendType() == AArch64_AM::MSL &&
1067            (Shift == 8 || Shift == 16);
1068   }
1069
1070   // Fallback unscaled operands are for aliases of LDR/STR that fall back
1071   // to LDUR/STUR when the offset is not legal for the former but is for
1072   // the latter. As such, in addition to checking for being a legal unscaled
1073   // address, also check that it is not a legal scaled address. This avoids
1074   // ambiguity in the matcher.
1075   template<int Width>
1076   bool isSImm9OffsetFB() const {
1077     return isSImm9() && !isUImm12Offset<Width / 8>();
1078   }
1079
1080   bool isAdrpLabel() const {
1081     // Validation was handled during parsing, so we just sanity check that
1082     // something didn't go haywire.
1083     if (!isImm())
1084         return false;
1085
1086     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
1087       int64_t Val = CE->getValue();
1088       int64_t Min = - (4096 * (1LL << (21 - 1)));
1089       int64_t Max = 4096 * ((1LL << (21 - 1)) - 1);
1090       return (Val % 4096) == 0 && Val >= Min && Val <= Max;
1091     }
1092
1093     return true;
1094   }
1095
1096   bool isAdrLabel() const {
1097     // Validation was handled during parsing, so we just sanity check that
1098     // something didn't go haywire.
1099     if (!isImm())
1100         return false;
1101
1102     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
1103       int64_t Val = CE->getValue();
1104       int64_t Min = - (1LL << (21 - 1));
1105       int64_t Max = ((1LL << (21 - 1)) - 1);
1106       return Val >= Min && Val <= Max;
1107     }
1108
1109     return true;
1110   }
1111
1112   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
1113     // Add as immediates when possible.  Null MCExpr = 0.
1114     if (!Expr)
1115       Inst.addOperand(MCOperand::CreateImm(0));
1116     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
1117       Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1118     else
1119       Inst.addOperand(MCOperand::CreateExpr(Expr));
1120   }
1121
1122   void addRegOperands(MCInst &Inst, unsigned N) const {
1123     assert(N == 1 && "Invalid number of operands!");
1124     Inst.addOperand(MCOperand::CreateReg(getReg()));
1125   }
1126
1127   void addGPR32as64Operands(MCInst &Inst, unsigned N) const {
1128     assert(N == 1 && "Invalid number of operands!");
1129     assert(
1130         AArch64MCRegisterClasses[AArch64::GPR64RegClassID].contains(getReg()));
1131
1132     const MCRegisterInfo *RI = Ctx.getRegisterInfo();
1133     uint32_t Reg = RI->getRegClass(AArch64::GPR32RegClassID).getRegister(
1134         RI->getEncodingValue(getReg()));
1135
1136     Inst.addOperand(MCOperand::CreateReg(Reg));
1137   }
1138
1139   void addVectorReg64Operands(MCInst &Inst, unsigned N) const {
1140     assert(N == 1 && "Invalid number of operands!");
1141     assert(
1142         AArch64MCRegisterClasses[AArch64::FPR128RegClassID].contains(getReg()));
1143     Inst.addOperand(MCOperand::CreateReg(AArch64::D0 + getReg() - AArch64::Q0));
1144   }
1145
1146   void addVectorReg128Operands(MCInst &Inst, unsigned N) const {
1147     assert(N == 1 && "Invalid number of operands!");
1148     assert(
1149         AArch64MCRegisterClasses[AArch64::FPR128RegClassID].contains(getReg()));
1150     Inst.addOperand(MCOperand::CreateReg(getReg()));
1151   }
1152
1153   void addVectorRegLoOperands(MCInst &Inst, unsigned N) const {
1154     assert(N == 1 && "Invalid number of operands!");
1155     Inst.addOperand(MCOperand::CreateReg(getReg()));
1156   }
1157
1158   template <unsigned NumRegs>
1159   void addVectorList64Operands(MCInst &Inst, unsigned N) const {
1160     assert(N == 1 && "Invalid number of operands!");
1161     static unsigned FirstRegs[] = { AArch64::D0,       AArch64::D0_D1,
1162                                     AArch64::D0_D1_D2, AArch64::D0_D1_D2_D3 };
1163     unsigned FirstReg = FirstRegs[NumRegs - 1];
1164
1165     Inst.addOperand(
1166         MCOperand::CreateReg(FirstReg + getVectorListStart() - AArch64::Q0));
1167   }
1168
1169   template <unsigned NumRegs>
1170   void addVectorList128Operands(MCInst &Inst, unsigned N) const {
1171     assert(N == 1 && "Invalid number of operands!");
1172     static unsigned FirstRegs[] = { AArch64::Q0,       AArch64::Q0_Q1,
1173                                     AArch64::Q0_Q1_Q2, AArch64::Q0_Q1_Q2_Q3 };
1174     unsigned FirstReg = FirstRegs[NumRegs - 1];
1175
1176     Inst.addOperand(
1177         MCOperand::CreateReg(FirstReg + getVectorListStart() - AArch64::Q0));
1178   }
1179
1180   void addVectorIndex1Operands(MCInst &Inst, unsigned N) const {
1181     assert(N == 1 && "Invalid number of operands!");
1182     Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1183   }
1184
1185   void addVectorIndexBOperands(MCInst &Inst, unsigned N) const {
1186     assert(N == 1 && "Invalid number of operands!");
1187     Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1188   }
1189
1190   void addVectorIndexHOperands(MCInst &Inst, unsigned N) const {
1191     assert(N == 1 && "Invalid number of operands!");
1192     Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1193   }
1194
1195   void addVectorIndexSOperands(MCInst &Inst, unsigned N) const {
1196     assert(N == 1 && "Invalid number of operands!");
1197     Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1198   }
1199
1200   void addVectorIndexDOperands(MCInst &Inst, unsigned N) const {
1201     assert(N == 1 && "Invalid number of operands!");
1202     Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1203   }
1204
1205   void addImmOperands(MCInst &Inst, unsigned N) const {
1206     assert(N == 1 && "Invalid number of operands!");
1207     // If this is a pageoff symrefexpr with an addend, adjust the addend
1208     // to be only the page-offset portion. Otherwise, just add the expr
1209     // as-is.
1210     addExpr(Inst, getImm());
1211   }
1212
1213   void addAddSubImmOperands(MCInst &Inst, unsigned N) const {
1214     assert(N == 2 && "Invalid number of operands!");
1215     if (isShiftedImm()) {
1216       addExpr(Inst, getShiftedImmVal());
1217       Inst.addOperand(MCOperand::CreateImm(getShiftedImmShift()));
1218     } else {
1219       addExpr(Inst, getImm());
1220       Inst.addOperand(MCOperand::CreateImm(0));
1221     }
1222   }
1223
1224   void addCondCodeOperands(MCInst &Inst, unsigned N) const {
1225     assert(N == 1 && "Invalid number of operands!");
1226     Inst.addOperand(MCOperand::CreateImm(getCondCode()));
1227   }
1228
1229   void addAdrpLabelOperands(MCInst &Inst, unsigned N) const {
1230     assert(N == 1 && "Invalid number of operands!");
1231     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
1232     if (!MCE)
1233       addExpr(Inst, getImm());
1234     else
1235       Inst.addOperand(MCOperand::CreateImm(MCE->getValue() >> 12));
1236   }
1237
1238   void addAdrLabelOperands(MCInst &Inst, unsigned N) const {
1239     addImmOperands(Inst, N);
1240   }
1241
1242   template<int Scale>
1243   void addUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1244     assert(N == 1 && "Invalid number of operands!");
1245     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
1246
1247     if (!MCE) {
1248       Inst.addOperand(MCOperand::CreateExpr(getImm()));
1249       return;
1250     }
1251     Inst.addOperand(MCOperand::CreateImm(MCE->getValue() / Scale));
1252   }
1253
1254   void addSImm9Operands(MCInst &Inst, unsigned N) const {
1255     assert(N == 1 && "Invalid number of operands!");
1256     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1257     Inst.addOperand(MCOperand::CreateImm(MCE->getValue()));
1258   }
1259
1260   void addSImm7s4Operands(MCInst &Inst, unsigned N) const {
1261     assert(N == 1 && "Invalid number of operands!");
1262     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1263     Inst.addOperand(MCOperand::CreateImm(MCE->getValue() / 4));
1264   }
1265
1266   void addSImm7s8Operands(MCInst &Inst, unsigned N) const {
1267     assert(N == 1 && "Invalid number of operands!");
1268     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1269     Inst.addOperand(MCOperand::CreateImm(MCE->getValue() / 8));
1270   }
1271
1272   void addSImm7s16Operands(MCInst &Inst, unsigned N) const {
1273     assert(N == 1 && "Invalid number of operands!");
1274     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1275     Inst.addOperand(MCOperand::CreateImm(MCE->getValue() / 16));
1276   }
1277
1278   void addImm0_7Operands(MCInst &Inst, unsigned N) const {
1279     assert(N == 1 && "Invalid number of operands!");
1280     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1281     Inst.addOperand(MCOperand::CreateImm(MCE->getValue()));
1282   }
1283
1284   void addImm1_8Operands(MCInst &Inst, unsigned N) const {
1285     assert(N == 1 && "Invalid number of operands!");
1286     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1287     Inst.addOperand(MCOperand::CreateImm(MCE->getValue()));
1288   }
1289
1290   void addImm0_15Operands(MCInst &Inst, unsigned N) const {
1291     assert(N == 1 && "Invalid number of operands!");
1292     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1293     Inst.addOperand(MCOperand::CreateImm(MCE->getValue()));
1294   }
1295
1296   void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1297     assert(N == 1 && "Invalid number of operands!");
1298     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1299     assert(MCE && "Invalid constant immediate operand!");
1300     Inst.addOperand(MCOperand::CreateImm(MCE->getValue()));
1301   }
1302
1303   void addImm0_31Operands(MCInst &Inst, unsigned N) const {
1304     assert(N == 1 && "Invalid number of operands!");
1305     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1306     Inst.addOperand(MCOperand::CreateImm(MCE->getValue()));
1307   }
1308
1309   void addImm1_31Operands(MCInst &Inst, unsigned N) const {
1310     assert(N == 1 && "Invalid number of operands!");
1311     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1312     Inst.addOperand(MCOperand::CreateImm(MCE->getValue()));
1313   }
1314
1315   void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1316     assert(N == 1 && "Invalid number of operands!");
1317     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1318     Inst.addOperand(MCOperand::CreateImm(MCE->getValue()));
1319   }
1320
1321   void addImm0_63Operands(MCInst &Inst, unsigned N) const {
1322     assert(N == 1 && "Invalid number of operands!");
1323     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1324     Inst.addOperand(MCOperand::CreateImm(MCE->getValue()));
1325   }
1326
1327   void addImm1_63Operands(MCInst &Inst, unsigned N) const {
1328     assert(N == 1 && "Invalid number of operands!");
1329     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1330     Inst.addOperand(MCOperand::CreateImm(MCE->getValue()));
1331   }
1332
1333   void addImm1_64Operands(MCInst &Inst, unsigned N) const {
1334     assert(N == 1 && "Invalid number of operands!");
1335     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1336     Inst.addOperand(MCOperand::CreateImm(MCE->getValue()));
1337   }
1338
1339   void addImm0_127Operands(MCInst &Inst, unsigned N) const {
1340     assert(N == 1 && "Invalid number of operands!");
1341     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1342     Inst.addOperand(MCOperand::CreateImm(MCE->getValue()));
1343   }
1344
1345   void addImm0_255Operands(MCInst &Inst, unsigned N) const {
1346     assert(N == 1 && "Invalid number of operands!");
1347     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1348     Inst.addOperand(MCOperand::CreateImm(MCE->getValue()));
1349   }
1350
1351   void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
1352     assert(N == 1 && "Invalid number of operands!");
1353     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1354     Inst.addOperand(MCOperand::CreateImm(MCE->getValue()));
1355   }
1356
1357   void addImm32_63Operands(MCInst &Inst, unsigned N) const {
1358     assert(N == 1 && "Invalid number of operands!");
1359     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1360     Inst.addOperand(MCOperand::CreateImm(MCE->getValue()));
1361   }
1362
1363   void addLogicalImm32Operands(MCInst &Inst, unsigned N) const {
1364     assert(N == 1 && "Invalid number of operands!");
1365     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1366     uint64_t encoding =
1367         AArch64_AM::encodeLogicalImmediate(MCE->getValue() & 0xFFFFFFFF, 32);
1368     Inst.addOperand(MCOperand::CreateImm(encoding));
1369   }
1370
1371   void addLogicalImm64Operands(MCInst &Inst, unsigned N) const {
1372     assert(N == 1 && "Invalid number of operands!");
1373     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1374     uint64_t encoding = AArch64_AM::encodeLogicalImmediate(MCE->getValue(), 64);
1375     Inst.addOperand(MCOperand::CreateImm(encoding));
1376   }
1377
1378   void addLogicalImm32NotOperands(MCInst &Inst, unsigned N) const {
1379     assert(N == 1 && "Invalid number of operands!");
1380     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1381     int64_t Val = ~MCE->getValue() & 0xFFFFFFFF;
1382     uint64_t encoding = AArch64_AM::encodeLogicalImmediate(Val, 32);
1383     Inst.addOperand(MCOperand::CreateImm(encoding));
1384   }
1385
1386   void addLogicalImm64NotOperands(MCInst &Inst, unsigned N) const {
1387     assert(N == 1 && "Invalid number of operands!");
1388     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1389     uint64_t encoding =
1390         AArch64_AM::encodeLogicalImmediate(~MCE->getValue(), 64);
1391     Inst.addOperand(MCOperand::CreateImm(encoding));
1392   }
1393
1394   void addSIMDImmType10Operands(MCInst &Inst, unsigned N) const {
1395     assert(N == 1 && "Invalid number of operands!");
1396     const MCConstantExpr *MCE = cast<MCConstantExpr>(getImm());
1397     uint64_t encoding = AArch64_AM::encodeAdvSIMDModImmType10(MCE->getValue());
1398     Inst.addOperand(MCOperand::CreateImm(encoding));
1399   }
1400
1401   void addBranchTarget26Operands(MCInst &Inst, unsigned N) const {
1402     // Branch operands don't encode the low bits, so shift them off
1403     // here. If it's a label, however, just put it on directly as there's
1404     // not enough information now to do anything.
1405     assert(N == 1 && "Invalid number of operands!");
1406     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
1407     if (!MCE) {
1408       addExpr(Inst, getImm());
1409       return;
1410     }
1411     assert(MCE && "Invalid constant immediate operand!");
1412     Inst.addOperand(MCOperand::CreateImm(MCE->getValue() >> 2));
1413   }
1414
1415   void addPCRelLabel19Operands(MCInst &Inst, unsigned N) const {
1416     // Branch operands don't encode the low bits, so shift them off
1417     // here. If it's a label, however, just put it on directly as there's
1418     // not enough information now to do anything.
1419     assert(N == 1 && "Invalid number of operands!");
1420     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
1421     if (!MCE) {
1422       addExpr(Inst, getImm());
1423       return;
1424     }
1425     assert(MCE && "Invalid constant immediate operand!");
1426     Inst.addOperand(MCOperand::CreateImm(MCE->getValue() >> 2));
1427   }
1428
1429   void addBranchTarget14Operands(MCInst &Inst, unsigned N) const {
1430     // Branch operands don't encode the low bits, so shift them off
1431     // here. If it's a label, however, just put it on directly as there's
1432     // not enough information now to do anything.
1433     assert(N == 1 && "Invalid number of operands!");
1434     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(getImm());
1435     if (!MCE) {
1436       addExpr(Inst, getImm());
1437       return;
1438     }
1439     assert(MCE && "Invalid constant immediate operand!");
1440     Inst.addOperand(MCOperand::CreateImm(MCE->getValue() >> 2));
1441   }
1442
1443   void addFPImmOperands(MCInst &Inst, unsigned N) const {
1444     assert(N == 1 && "Invalid number of operands!");
1445     Inst.addOperand(MCOperand::CreateImm(getFPImm()));
1446   }
1447
1448   void addBarrierOperands(MCInst &Inst, unsigned N) const {
1449     assert(N == 1 && "Invalid number of operands!");
1450     Inst.addOperand(MCOperand::CreateImm(getBarrier()));
1451   }
1452
1453   void addMRSSystemRegisterOperands(MCInst &Inst, unsigned N) const {
1454     assert(N == 1 && "Invalid number of operands!");
1455
1456     bool Valid;
1457     auto Mapper = AArch64SysReg::MRSMapper(getSysRegFeatureBits());
1458     uint32_t Bits = Mapper.fromString(getSysReg(), Valid);
1459
1460     Inst.addOperand(MCOperand::CreateImm(Bits));
1461   }
1462
1463   void addMSRSystemRegisterOperands(MCInst &Inst, unsigned N) const {
1464     assert(N == 1 && "Invalid number of operands!");
1465
1466     bool Valid;
1467     auto Mapper = AArch64SysReg::MSRMapper(getSysRegFeatureBits());
1468     uint32_t Bits = Mapper.fromString(getSysReg(), Valid);
1469
1470     Inst.addOperand(MCOperand::CreateImm(Bits));
1471   }
1472
1473   void addSystemPStateFieldOperands(MCInst &Inst, unsigned N) const {
1474     assert(N == 1 && "Invalid number of operands!");
1475
1476     bool Valid;
1477     uint32_t Bits =
1478         AArch64PState::PStateMapper().fromString(getSysReg(), Valid);
1479
1480     Inst.addOperand(MCOperand::CreateImm(Bits));
1481   }
1482
1483   void addSysCROperands(MCInst &Inst, unsigned N) const {
1484     assert(N == 1 && "Invalid number of operands!");
1485     Inst.addOperand(MCOperand::CreateImm(getSysCR()));
1486   }
1487
1488   void addPrefetchOperands(MCInst &Inst, unsigned N) const {
1489     assert(N == 1 && "Invalid number of operands!");
1490     Inst.addOperand(MCOperand::CreateImm(getPrefetch()));
1491   }
1492
1493   void addShifterOperands(MCInst &Inst, unsigned N) const {
1494     assert(N == 1 && "Invalid number of operands!");
1495     unsigned Imm =
1496         AArch64_AM::getShifterImm(getShiftExtendType(), getShiftExtendAmount());
1497     Inst.addOperand(MCOperand::CreateImm(Imm));
1498   }
1499
1500   void addExtendOperands(MCInst &Inst, unsigned N) const {
1501     assert(N == 1 && "Invalid number of operands!");
1502     AArch64_AM::ShiftExtendType ET = getShiftExtendType();
1503     if (ET == AArch64_AM::LSL) ET = AArch64_AM::UXTW;
1504     unsigned Imm = AArch64_AM::getArithExtendImm(ET, getShiftExtendAmount());
1505     Inst.addOperand(MCOperand::CreateImm(Imm));
1506   }
1507
1508   void addExtend64Operands(MCInst &Inst, unsigned N) const {
1509     assert(N == 1 && "Invalid number of operands!");
1510     AArch64_AM::ShiftExtendType ET = getShiftExtendType();
1511     if (ET == AArch64_AM::LSL) ET = AArch64_AM::UXTX;
1512     unsigned Imm = AArch64_AM::getArithExtendImm(ET, getShiftExtendAmount());
1513     Inst.addOperand(MCOperand::CreateImm(Imm));
1514   }
1515
1516   void addMemExtendOperands(MCInst &Inst, unsigned N) const {
1517     assert(N == 2 && "Invalid number of operands!");
1518     AArch64_AM::ShiftExtendType ET = getShiftExtendType();
1519     bool IsSigned = ET == AArch64_AM::SXTW || ET == AArch64_AM::SXTX;
1520     Inst.addOperand(MCOperand::CreateImm(IsSigned));
1521     Inst.addOperand(MCOperand::CreateImm(getShiftExtendAmount() != 0));
1522   }
1523
1524   // For 8-bit load/store instructions with a register offset, both the
1525   // "DoShift" and "NoShift" variants have a shift of 0. Because of this,
1526   // they're disambiguated by whether the shift was explicit or implicit rather
1527   // than its size.
1528   void addMemExtend8Operands(MCInst &Inst, unsigned N) const {
1529     assert(N == 2 && "Invalid number of operands!");
1530     AArch64_AM::ShiftExtendType ET = getShiftExtendType();
1531     bool IsSigned = ET == AArch64_AM::SXTW || ET == AArch64_AM::SXTX;
1532     Inst.addOperand(MCOperand::CreateImm(IsSigned));
1533     Inst.addOperand(MCOperand::CreateImm(hasShiftExtendAmount()));
1534   }
1535
1536   template<int Shift>
1537   void addMOVZMovAliasOperands(MCInst &Inst, unsigned N) const {
1538     assert(N == 1 && "Invalid number of operands!");
1539
1540     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
1541     uint64_t Value = CE->getValue();
1542     Inst.addOperand(MCOperand::CreateImm((Value >> Shift) & 0xffff));
1543   }
1544
1545   template<int Shift>
1546   void addMOVNMovAliasOperands(MCInst &Inst, unsigned N) const {
1547     assert(N == 1 && "Invalid number of operands!");
1548
1549     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
1550     uint64_t Value = CE->getValue();
1551     Inst.addOperand(MCOperand::CreateImm((~Value >> Shift) & 0xffff));
1552   }
1553
1554   void print(raw_ostream &OS) const override;
1555
1556   static std::unique_ptr<AArch64Operand>
1557   CreateToken(StringRef Str, bool IsSuffix, SMLoc S, MCContext &Ctx) {
1558     auto Op = make_unique<AArch64Operand>(k_Token, Ctx);
1559     Op->Tok.Data = Str.data();
1560     Op->Tok.Length = Str.size();
1561     Op->Tok.IsSuffix = IsSuffix;
1562     Op->StartLoc = S;
1563     Op->EndLoc = S;
1564     return Op;
1565   }
1566
1567   static std::unique_ptr<AArch64Operand>
1568   CreateReg(unsigned RegNum, bool isVector, SMLoc S, SMLoc E, MCContext &Ctx) {
1569     auto Op = make_unique<AArch64Operand>(k_Register, Ctx);
1570     Op->Reg.RegNum = RegNum;
1571     Op->Reg.isVector = isVector;
1572     Op->StartLoc = S;
1573     Op->EndLoc = E;
1574     return Op;
1575   }
1576
1577   static std::unique_ptr<AArch64Operand>
1578   CreateVectorList(unsigned RegNum, unsigned Count, unsigned NumElements,
1579                    char ElementKind, SMLoc S, SMLoc E, MCContext &Ctx) {
1580     auto Op = make_unique<AArch64Operand>(k_VectorList, Ctx);
1581     Op->VectorList.RegNum = RegNum;
1582     Op->VectorList.Count = Count;
1583     Op->VectorList.NumElements = NumElements;
1584     Op->VectorList.ElementKind = ElementKind;
1585     Op->StartLoc = S;
1586     Op->EndLoc = E;
1587     return Op;
1588   }
1589
1590   static std::unique_ptr<AArch64Operand>
1591   CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E, MCContext &Ctx) {
1592     auto Op = make_unique<AArch64Operand>(k_VectorIndex, Ctx);
1593     Op->VectorIndex.Val = Idx;
1594     Op->StartLoc = S;
1595     Op->EndLoc = E;
1596     return Op;
1597   }
1598
1599   static std::unique_ptr<AArch64Operand> CreateImm(const MCExpr *Val, SMLoc S,
1600                                                    SMLoc E, MCContext &Ctx) {
1601     auto Op = make_unique<AArch64Operand>(k_Immediate, Ctx);
1602     Op->Imm.Val = Val;
1603     Op->StartLoc = S;
1604     Op->EndLoc = E;
1605     return Op;
1606   }
1607
1608   static std::unique_ptr<AArch64Operand> CreateShiftedImm(const MCExpr *Val,
1609                                                           unsigned ShiftAmount,
1610                                                           SMLoc S, SMLoc E,
1611                                                           MCContext &Ctx) {
1612     auto Op = make_unique<AArch64Operand>(k_ShiftedImm, Ctx);
1613     Op->ShiftedImm .Val = Val;
1614     Op->ShiftedImm.ShiftAmount = ShiftAmount;
1615     Op->StartLoc = S;
1616     Op->EndLoc = E;
1617     return Op;
1618   }
1619
1620   static std::unique_ptr<AArch64Operand>
1621   CreateCondCode(AArch64CC::CondCode Code, SMLoc S, SMLoc E, MCContext &Ctx) {
1622     auto Op = make_unique<AArch64Operand>(k_CondCode, Ctx);
1623     Op->CondCode.Code = Code;
1624     Op->StartLoc = S;
1625     Op->EndLoc = E;
1626     return Op;
1627   }
1628
1629   static std::unique_ptr<AArch64Operand> CreateFPImm(unsigned Val, SMLoc S,
1630                                                      MCContext &Ctx) {
1631     auto Op = make_unique<AArch64Operand>(k_FPImm, Ctx);
1632     Op->FPImm.Val = Val;
1633     Op->StartLoc = S;
1634     Op->EndLoc = S;
1635     return Op;
1636   }
1637
1638   static std::unique_ptr<AArch64Operand> CreateBarrier(unsigned Val, SMLoc S,
1639                                                        MCContext &Ctx) {
1640     auto Op = make_unique<AArch64Operand>(k_Barrier, Ctx);
1641     Op->Barrier.Val = Val;
1642     Op->StartLoc = S;
1643     Op->EndLoc = S;
1644     return Op;
1645   }
1646
1647   static std::unique_ptr<AArch64Operand>
1648   CreateSysReg(StringRef Str, SMLoc S, uint64_t FeatureBits, MCContext &Ctx) {
1649     auto Op = make_unique<AArch64Operand>(k_SysReg, Ctx);
1650     Op->SysReg.Data = Str.data();
1651     Op->SysReg.Length = Str.size();
1652     Op->SysReg.FeatureBits = FeatureBits;
1653     Op->StartLoc = S;
1654     Op->EndLoc = S;
1655     return Op;
1656   }
1657
1658   static std::unique_ptr<AArch64Operand> CreateSysCR(unsigned Val, SMLoc S,
1659                                                      SMLoc E, MCContext &Ctx) {
1660     auto Op = make_unique<AArch64Operand>(k_SysCR, Ctx);
1661     Op->SysCRImm.Val = Val;
1662     Op->StartLoc = S;
1663     Op->EndLoc = E;
1664     return Op;
1665   }
1666
1667   static std::unique_ptr<AArch64Operand> CreatePrefetch(unsigned Val, SMLoc S,
1668                                                         MCContext &Ctx) {
1669     auto Op = make_unique<AArch64Operand>(k_Prefetch, Ctx);
1670     Op->Prefetch.Val = Val;
1671     Op->StartLoc = S;
1672     Op->EndLoc = S;
1673     return Op;
1674   }
1675
1676   static std::unique_ptr<AArch64Operand>
1677   CreateShiftExtend(AArch64_AM::ShiftExtendType ShOp, unsigned Val,
1678                     bool HasExplicitAmount, SMLoc S, SMLoc E, MCContext &Ctx) {
1679     auto Op = make_unique<AArch64Operand>(k_ShiftExtend, Ctx);
1680     Op->ShiftExtend.Type = ShOp;
1681     Op->ShiftExtend.Amount = Val;
1682     Op->ShiftExtend.HasExplicitAmount = HasExplicitAmount;
1683     Op->StartLoc = S;
1684     Op->EndLoc = E;
1685     return Op;
1686   }
1687 };
1688
1689 } // end anonymous namespace.
1690
1691 void AArch64Operand::print(raw_ostream &OS) const {
1692   switch (Kind) {
1693   case k_FPImm:
1694     OS << "<fpimm " << getFPImm() << "("
1695        << AArch64_AM::getFPImmFloat(getFPImm()) << ") >";
1696     break;
1697   case k_Barrier: {
1698     bool Valid;
1699     StringRef Name = AArch64DB::DBarrierMapper().toString(getBarrier(), Valid);
1700     if (Valid)
1701       OS << "<barrier " << Name << ">";
1702     else
1703       OS << "<barrier invalid #" << getBarrier() << ">";
1704     break;
1705   }
1706   case k_Immediate:
1707     getImm()->print(OS);
1708     break;
1709   case k_ShiftedImm: {
1710     unsigned Shift = getShiftedImmShift();
1711     OS << "<shiftedimm ";
1712     getShiftedImmVal()->print(OS);
1713     OS << ", lsl #" << AArch64_AM::getShiftValue(Shift) << ">";
1714     break;
1715   }
1716   case k_CondCode:
1717     OS << "<condcode " << getCondCode() << ">";
1718     break;
1719   case k_Register:
1720     OS << "<register " << getReg() << ">";
1721     break;
1722   case k_VectorList: {
1723     OS << "<vectorlist ";
1724     unsigned Reg = getVectorListStart();
1725     for (unsigned i = 0, e = getVectorListCount(); i != e; ++i)
1726       OS << Reg + i << " ";
1727     OS << ">";
1728     break;
1729   }
1730   case k_VectorIndex:
1731     OS << "<vectorindex " << getVectorIndex() << ">";
1732     break;
1733   case k_SysReg:
1734     OS << "<sysreg: " << getSysReg() << '>';
1735     break;
1736   case k_Token:
1737     OS << "'" << getToken() << "'";
1738     break;
1739   case k_SysCR:
1740     OS << "c" << getSysCR();
1741     break;
1742   case k_Prefetch: {
1743     bool Valid;
1744     StringRef Name = AArch64PRFM::PRFMMapper().toString(getPrefetch(), Valid);
1745     if (Valid)
1746       OS << "<prfop " << Name << ">";
1747     else
1748       OS << "<prfop invalid #" << getPrefetch() << ">";
1749     break;
1750   }
1751   case k_ShiftExtend: {
1752     OS << "<" << AArch64_AM::getShiftExtendName(getShiftExtendType()) << " #"
1753        << getShiftExtendAmount();
1754     if (!hasShiftExtendAmount())
1755       OS << "<imp>";
1756     OS << '>';
1757     break;
1758   }
1759   }
1760 }
1761
1762 /// @name Auto-generated Match Functions
1763 /// {
1764
1765 static unsigned MatchRegisterName(StringRef Name);
1766
1767 /// }
1768
1769 static unsigned matchVectorRegName(StringRef Name) {
1770   return StringSwitch<unsigned>(Name)
1771       .Case("v0", AArch64::Q0)
1772       .Case("v1", AArch64::Q1)
1773       .Case("v2", AArch64::Q2)
1774       .Case("v3", AArch64::Q3)
1775       .Case("v4", AArch64::Q4)
1776       .Case("v5", AArch64::Q5)
1777       .Case("v6", AArch64::Q6)
1778       .Case("v7", AArch64::Q7)
1779       .Case("v8", AArch64::Q8)
1780       .Case("v9", AArch64::Q9)
1781       .Case("v10", AArch64::Q10)
1782       .Case("v11", AArch64::Q11)
1783       .Case("v12", AArch64::Q12)
1784       .Case("v13", AArch64::Q13)
1785       .Case("v14", AArch64::Q14)
1786       .Case("v15", AArch64::Q15)
1787       .Case("v16", AArch64::Q16)
1788       .Case("v17", AArch64::Q17)
1789       .Case("v18", AArch64::Q18)
1790       .Case("v19", AArch64::Q19)
1791       .Case("v20", AArch64::Q20)
1792       .Case("v21", AArch64::Q21)
1793       .Case("v22", AArch64::Q22)
1794       .Case("v23", AArch64::Q23)
1795       .Case("v24", AArch64::Q24)
1796       .Case("v25", AArch64::Q25)
1797       .Case("v26", AArch64::Q26)
1798       .Case("v27", AArch64::Q27)
1799       .Case("v28", AArch64::Q28)
1800       .Case("v29", AArch64::Q29)
1801       .Case("v30", AArch64::Q30)
1802       .Case("v31", AArch64::Q31)
1803       .Default(0);
1804 }
1805
1806 static bool isValidVectorKind(StringRef Name) {
1807   return StringSwitch<bool>(Name.lower())
1808       .Case(".8b", true)
1809       .Case(".16b", true)
1810       .Case(".4h", true)
1811       .Case(".8h", true)
1812       .Case(".2s", true)
1813       .Case(".4s", true)
1814       .Case(".1d", true)
1815       .Case(".2d", true)
1816       .Case(".1q", true)
1817       // Accept the width neutral ones, too, for verbose syntax. If those
1818       // aren't used in the right places, the token operand won't match so
1819       // all will work out.
1820       .Case(".b", true)
1821       .Case(".h", true)
1822       .Case(".s", true)
1823       .Case(".d", true)
1824       .Default(false);
1825 }
1826
1827 static void parseValidVectorKind(StringRef Name, unsigned &NumElements,
1828                                  char &ElementKind) {
1829   assert(isValidVectorKind(Name));
1830
1831   ElementKind = Name.lower()[Name.size() - 1];
1832   NumElements = 0;
1833
1834   if (Name.size() == 2)
1835     return;
1836
1837   // Parse the lane count
1838   Name = Name.drop_front();
1839   while (isdigit(Name.front())) {
1840     NumElements = 10 * NumElements + (Name.front() - '0');
1841     Name = Name.drop_front();
1842   }
1843 }
1844
1845 bool AArch64AsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
1846                                      SMLoc &EndLoc) {
1847   StartLoc = getLoc();
1848   RegNo = tryParseRegister();
1849   EndLoc = SMLoc::getFromPointer(getLoc().getPointer() - 1);
1850   return (RegNo == (unsigned)-1);
1851 }
1852
1853 // Matches a register name or register alias previously defined by '.req'
1854 unsigned AArch64AsmParser::matchRegisterNameAlias(StringRef Name,
1855                                                   bool isVector) {
1856   unsigned RegNum = isVector ? matchVectorRegName(Name)
1857                              : MatchRegisterName(Name);
1858
1859   if (RegNum == 0) {
1860     // Check for aliases registered via .req. Canonicalize to lower case.
1861     // That's more consistent since register names are case insensitive, and
1862     // it's how the original entry was passed in from MC/MCParser/AsmParser.
1863     auto Entry = RegisterReqs.find(Name.lower());
1864     if (Entry == RegisterReqs.end())
1865       return 0;
1866     // set RegNum if the match is the right kind of register
1867     if (isVector == Entry->getValue().first)
1868       RegNum = Entry->getValue().second;
1869   }
1870   return RegNum;
1871 }
1872
1873 /// tryParseRegister - Try to parse a register name. The token must be an
1874 /// Identifier when called, and if it is a register name the token is eaten and
1875 /// the register is added to the operand list.
1876 int AArch64AsmParser::tryParseRegister() {
1877   const AsmToken &Tok = Parser.getTok();
1878   assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1879
1880   std::string lowerCase = Tok.getString().lower();
1881   unsigned RegNum = matchRegisterNameAlias(lowerCase, false);
1882   // Also handle a few aliases of registers.
1883   if (RegNum == 0)
1884     RegNum = StringSwitch<unsigned>(lowerCase)
1885                  .Case("fp",  AArch64::FP)
1886                  .Case("lr",  AArch64::LR)
1887                  .Case("x31", AArch64::XZR)
1888                  .Case("w31", AArch64::WZR)
1889                  .Default(0);
1890
1891   if (RegNum == 0)
1892     return -1;
1893
1894   Parser.Lex(); // Eat identifier token.
1895   return RegNum;
1896 }
1897
1898 /// tryMatchVectorRegister - Try to parse a vector register name with optional
1899 /// kind specifier. If it is a register specifier, eat the token and return it.
1900 int AArch64AsmParser::tryMatchVectorRegister(StringRef &Kind, bool expected) {
1901   if (Parser.getTok().isNot(AsmToken::Identifier)) {
1902     TokError("vector register expected");
1903     return -1;
1904   }
1905
1906   StringRef Name = Parser.getTok().getString();
1907   // If there is a kind specifier, it's separated from the register name by
1908   // a '.'.
1909   size_t Start = 0, Next = Name.find('.');
1910   StringRef Head = Name.slice(Start, Next);
1911   unsigned RegNum = matchRegisterNameAlias(Head, true);
1912
1913   if (RegNum) {
1914     if (Next != StringRef::npos) {
1915       Kind = Name.slice(Next, StringRef::npos);
1916       if (!isValidVectorKind(Kind)) {
1917         TokError("invalid vector kind qualifier");
1918         return -1;
1919       }
1920     }
1921     Parser.Lex(); // Eat the register token.
1922     return RegNum;
1923   }
1924
1925   if (expected)
1926     TokError("vector register expected");
1927   return -1;
1928 }
1929
1930 /// tryParseSysCROperand - Try to parse a system instruction CR operand name.
1931 AArch64AsmParser::OperandMatchResultTy
1932 AArch64AsmParser::tryParseSysCROperand(OperandVector &Operands) {
1933   SMLoc S = getLoc();
1934
1935   if (Parser.getTok().isNot(AsmToken::Identifier)) {
1936     Error(S, "Expected cN operand where 0 <= N <= 15");
1937     return MatchOperand_ParseFail;
1938   }
1939
1940   StringRef Tok = Parser.getTok().getIdentifier();
1941   if (Tok[0] != 'c' && Tok[0] != 'C') {
1942     Error(S, "Expected cN operand where 0 <= N <= 15");
1943     return MatchOperand_ParseFail;
1944   }
1945
1946   uint32_t CRNum;
1947   bool BadNum = Tok.drop_front().getAsInteger(10, CRNum);
1948   if (BadNum || CRNum > 15) {
1949     Error(S, "Expected cN operand where 0 <= N <= 15");
1950     return MatchOperand_ParseFail;
1951   }
1952
1953   Parser.Lex(); // Eat identifier token.
1954   Operands.push_back(
1955       AArch64Operand::CreateSysCR(CRNum, S, getLoc(), getContext()));
1956   return MatchOperand_Success;
1957 }
1958
1959 /// tryParsePrefetch - Try to parse a prefetch operand.
1960 AArch64AsmParser::OperandMatchResultTy
1961 AArch64AsmParser::tryParsePrefetch(OperandVector &Operands) {
1962   SMLoc S = getLoc();
1963   const AsmToken &Tok = Parser.getTok();
1964   // Either an identifier for named values or a 5-bit immediate.
1965   bool Hash = Tok.is(AsmToken::Hash);
1966   if (Hash || Tok.is(AsmToken::Integer)) {
1967     if (Hash)
1968       Parser.Lex(); // Eat hash token.
1969     const MCExpr *ImmVal;
1970     if (getParser().parseExpression(ImmVal))
1971       return MatchOperand_ParseFail;
1972
1973     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
1974     if (!MCE) {
1975       TokError("immediate value expected for prefetch operand");
1976       return MatchOperand_ParseFail;
1977     }
1978     unsigned prfop = MCE->getValue();
1979     if (prfop > 31) {
1980       TokError("prefetch operand out of range, [0,31] expected");
1981       return MatchOperand_ParseFail;
1982     }
1983
1984     Operands.push_back(AArch64Operand::CreatePrefetch(prfop, S, getContext()));
1985     return MatchOperand_Success;
1986   }
1987
1988   if (Tok.isNot(AsmToken::Identifier)) {
1989     TokError("pre-fetch hint expected");
1990     return MatchOperand_ParseFail;
1991   }
1992
1993   bool Valid;
1994   unsigned prfop = AArch64PRFM::PRFMMapper().fromString(Tok.getString(), Valid);
1995   if (!Valid) {
1996     TokError("pre-fetch hint expected");
1997     return MatchOperand_ParseFail;
1998   }
1999
2000   Parser.Lex(); // Eat identifier token.
2001   Operands.push_back(AArch64Operand::CreatePrefetch(prfop, S, getContext()));
2002   return MatchOperand_Success;
2003 }
2004
2005 /// tryParseAdrpLabel - Parse and validate a source label for the ADRP
2006 /// instruction.
2007 AArch64AsmParser::OperandMatchResultTy
2008 AArch64AsmParser::tryParseAdrpLabel(OperandVector &Operands) {
2009   SMLoc S = getLoc();
2010   const MCExpr *Expr;
2011
2012   if (Parser.getTok().is(AsmToken::Hash)) {
2013     Parser.Lex(); // Eat hash token.
2014   }
2015
2016   if (parseSymbolicImmVal(Expr))
2017     return MatchOperand_ParseFail;
2018
2019   AArch64MCExpr::VariantKind ELFRefKind;
2020   MCSymbolRefExpr::VariantKind DarwinRefKind;
2021   int64_t Addend;
2022   if (classifySymbolRef(Expr, ELFRefKind, DarwinRefKind, Addend)) {
2023     if (DarwinRefKind == MCSymbolRefExpr::VK_None &&
2024         ELFRefKind == AArch64MCExpr::VK_INVALID) {
2025       // No modifier was specified at all; this is the syntax for an ELF basic
2026       // ADRP relocation (unfortunately).
2027       Expr =
2028           AArch64MCExpr::Create(Expr, AArch64MCExpr::VK_ABS_PAGE, getContext());
2029     } else if ((DarwinRefKind == MCSymbolRefExpr::VK_GOTPAGE ||
2030                 DarwinRefKind == MCSymbolRefExpr::VK_TLVPPAGE) &&
2031                Addend != 0) {
2032       Error(S, "gotpage label reference not allowed an addend");
2033       return MatchOperand_ParseFail;
2034     } else if (DarwinRefKind != MCSymbolRefExpr::VK_PAGE &&
2035                DarwinRefKind != MCSymbolRefExpr::VK_GOTPAGE &&
2036                DarwinRefKind != MCSymbolRefExpr::VK_TLVPPAGE &&
2037                ELFRefKind != AArch64MCExpr::VK_GOT_PAGE &&
2038                ELFRefKind != AArch64MCExpr::VK_GOTTPREL_PAGE &&
2039                ELFRefKind != AArch64MCExpr::VK_TLSDESC_PAGE) {
2040       // The operand must be an @page or @gotpage qualified symbolref.
2041       Error(S, "page or gotpage label reference expected");
2042       return MatchOperand_ParseFail;
2043     }
2044   }
2045
2046   // We have either a label reference possibly with addend or an immediate. The
2047   // addend is a raw value here. The linker will adjust it to only reference the
2048   // page.
2049   SMLoc E = SMLoc::getFromPointer(getLoc().getPointer() - 1);
2050   Operands.push_back(AArch64Operand::CreateImm(Expr, S, E, getContext()));
2051
2052   return MatchOperand_Success;
2053 }
2054
2055 /// tryParseAdrLabel - Parse and validate a source label for the ADR
2056 /// instruction.
2057 AArch64AsmParser::OperandMatchResultTy
2058 AArch64AsmParser::tryParseAdrLabel(OperandVector &Operands) {
2059   SMLoc S = getLoc();
2060   const MCExpr *Expr;
2061
2062   if (Parser.getTok().is(AsmToken::Hash)) {
2063     Parser.Lex(); // Eat hash token.
2064   }
2065
2066   if (getParser().parseExpression(Expr))
2067     return MatchOperand_ParseFail;
2068
2069   SMLoc E = SMLoc::getFromPointer(getLoc().getPointer() - 1);
2070   Operands.push_back(AArch64Operand::CreateImm(Expr, S, E, getContext()));
2071
2072   return MatchOperand_Success;
2073 }
2074
2075 /// tryParseFPImm - A floating point immediate expression operand.
2076 AArch64AsmParser::OperandMatchResultTy
2077 AArch64AsmParser::tryParseFPImm(OperandVector &Operands) {
2078   SMLoc S = getLoc();
2079
2080   bool Hash = false;
2081   if (Parser.getTok().is(AsmToken::Hash)) {
2082     Parser.Lex(); // Eat '#'
2083     Hash = true;
2084   }
2085
2086   // Handle negation, as that still comes through as a separate token.
2087   bool isNegative = false;
2088   if (Parser.getTok().is(AsmToken::Minus)) {
2089     isNegative = true;
2090     Parser.Lex();
2091   }
2092   const AsmToken &Tok = Parser.getTok();
2093   if (Tok.is(AsmToken::Real)) {
2094     APFloat RealVal(APFloat::IEEEdouble, Tok.getString());
2095     uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
2096     // If we had a '-' in front, toggle the sign bit.
2097     IntVal ^= (uint64_t)isNegative << 63;
2098     int Val = AArch64_AM::getFP64Imm(APInt(64, IntVal));
2099     Parser.Lex(); // Eat the token.
2100     // Check for out of range values. As an exception, we let Zero through,
2101     // as we handle that special case in post-processing before matching in
2102     // order to use the zero register for it.
2103     if (Val == -1 && !RealVal.isZero()) {
2104       TokError("expected compatible register or floating-point constant");
2105       return MatchOperand_ParseFail;
2106     }
2107     Operands.push_back(AArch64Operand::CreateFPImm(Val, S, getContext()));
2108     return MatchOperand_Success;
2109   }
2110   if (Tok.is(AsmToken::Integer)) {
2111     int64_t Val;
2112     if (!isNegative && Tok.getString().startswith("0x")) {
2113       Val = Tok.getIntVal();
2114       if (Val > 255 || Val < 0) {
2115         TokError("encoded floating point value out of range");
2116         return MatchOperand_ParseFail;
2117       }
2118     } else {
2119       APFloat RealVal(APFloat::IEEEdouble, Tok.getString());
2120       uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
2121       // If we had a '-' in front, toggle the sign bit.
2122       IntVal ^= (uint64_t)isNegative << 63;
2123       Val = AArch64_AM::getFP64Imm(APInt(64, IntVal));
2124     }
2125     Parser.Lex(); // Eat the token.
2126     Operands.push_back(AArch64Operand::CreateFPImm(Val, S, getContext()));
2127     return MatchOperand_Success;
2128   }
2129
2130   if (!Hash)
2131     return MatchOperand_NoMatch;
2132
2133   TokError("invalid floating point immediate");
2134   return MatchOperand_ParseFail;
2135 }
2136
2137 /// tryParseAddSubImm - Parse ADD/SUB shifted immediate operand
2138 AArch64AsmParser::OperandMatchResultTy
2139 AArch64AsmParser::tryParseAddSubImm(OperandVector &Operands) {
2140   SMLoc S = getLoc();
2141
2142   if (Parser.getTok().is(AsmToken::Hash))
2143     Parser.Lex(); // Eat '#'
2144   else if (Parser.getTok().isNot(AsmToken::Integer))
2145     // Operand should start from # or should be integer, emit error otherwise.
2146     return MatchOperand_NoMatch;
2147
2148   const MCExpr *Imm;
2149   if (parseSymbolicImmVal(Imm))
2150     return MatchOperand_ParseFail;
2151   else if (Parser.getTok().isNot(AsmToken::Comma)) {
2152     uint64_t ShiftAmount = 0;
2153     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Imm);
2154     if (MCE) {
2155       int64_t Val = MCE->getValue();
2156       if (Val > 0xfff && (Val & 0xfff) == 0) {
2157         Imm = MCConstantExpr::Create(Val >> 12, getContext());
2158         ShiftAmount = 12;
2159       }
2160     }
2161     SMLoc E = Parser.getTok().getLoc();
2162     Operands.push_back(AArch64Operand::CreateShiftedImm(Imm, ShiftAmount, S, E,
2163                                                         getContext()));
2164     return MatchOperand_Success;
2165   }
2166
2167   // Eat ','
2168   Parser.Lex();
2169
2170   // The optional operand must be "lsl #N" where N is non-negative.
2171   if (!Parser.getTok().is(AsmToken::Identifier) ||
2172       !Parser.getTok().getIdentifier().equals_lower("lsl")) {
2173     Error(Parser.getTok().getLoc(), "only 'lsl #+N' valid after immediate");
2174     return MatchOperand_ParseFail;
2175   }
2176
2177   // Eat 'lsl'
2178   Parser.Lex();
2179
2180   if (Parser.getTok().is(AsmToken::Hash)) {
2181     Parser.Lex();
2182   }
2183
2184   if (Parser.getTok().isNot(AsmToken::Integer)) {
2185     Error(Parser.getTok().getLoc(), "only 'lsl #+N' valid after immediate");
2186     return MatchOperand_ParseFail;
2187   }
2188
2189   int64_t ShiftAmount = Parser.getTok().getIntVal();
2190
2191   if (ShiftAmount < 0) {
2192     Error(Parser.getTok().getLoc(), "positive shift amount required");
2193     return MatchOperand_ParseFail;
2194   }
2195   Parser.Lex(); // Eat the number
2196
2197   SMLoc E = Parser.getTok().getLoc();
2198   Operands.push_back(AArch64Operand::CreateShiftedImm(Imm, ShiftAmount,
2199                                                       S, E, getContext()));
2200   return MatchOperand_Success;
2201 }
2202
2203 /// parseCondCodeString - Parse a Condition Code string.
2204 AArch64CC::CondCode AArch64AsmParser::parseCondCodeString(StringRef Cond) {
2205   AArch64CC::CondCode CC = StringSwitch<AArch64CC::CondCode>(Cond.lower())
2206                     .Case("eq", AArch64CC::EQ)
2207                     .Case("ne", AArch64CC::NE)
2208                     .Case("cs", AArch64CC::HS)
2209                     .Case("hs", AArch64CC::HS)
2210                     .Case("cc", AArch64CC::LO)
2211                     .Case("lo", AArch64CC::LO)
2212                     .Case("mi", AArch64CC::MI)
2213                     .Case("pl", AArch64CC::PL)
2214                     .Case("vs", AArch64CC::VS)
2215                     .Case("vc", AArch64CC::VC)
2216                     .Case("hi", AArch64CC::HI)
2217                     .Case("ls", AArch64CC::LS)
2218                     .Case("ge", AArch64CC::GE)
2219                     .Case("lt", AArch64CC::LT)
2220                     .Case("gt", AArch64CC::GT)
2221                     .Case("le", AArch64CC::LE)
2222                     .Case("al", AArch64CC::AL)
2223                     .Case("nv", AArch64CC::NV)
2224                     .Default(AArch64CC::Invalid);
2225   return CC;
2226 }
2227
2228 /// parseCondCode - Parse a Condition Code operand.
2229 bool AArch64AsmParser::parseCondCode(OperandVector &Operands,
2230                                      bool invertCondCode) {
2231   SMLoc S = getLoc();
2232   const AsmToken &Tok = Parser.getTok();
2233   assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2234
2235   StringRef Cond = Tok.getString();
2236   AArch64CC::CondCode CC = parseCondCodeString(Cond);
2237   if (CC == AArch64CC::Invalid)
2238     return TokError("invalid condition code");
2239   Parser.Lex(); // Eat identifier token.
2240
2241   if (invertCondCode) {
2242     if (CC == AArch64CC::AL || CC == AArch64CC::NV)
2243       return TokError("condition codes AL and NV are invalid for this instruction");
2244     CC = AArch64CC::getInvertedCondCode(AArch64CC::CondCode(CC));
2245   }
2246
2247   Operands.push_back(
2248       AArch64Operand::CreateCondCode(CC, S, getLoc(), getContext()));
2249   return false;
2250 }
2251
2252 /// tryParseOptionalShift - Some operands take an optional shift argument. Parse
2253 /// them if present.
2254 AArch64AsmParser::OperandMatchResultTy
2255 AArch64AsmParser::tryParseOptionalShiftExtend(OperandVector &Operands) {
2256   const AsmToken &Tok = Parser.getTok();
2257   std::string LowerID = Tok.getString().lower();
2258   AArch64_AM::ShiftExtendType ShOp =
2259       StringSwitch<AArch64_AM::ShiftExtendType>(LowerID)
2260           .Case("lsl", AArch64_AM::LSL)
2261           .Case("lsr", AArch64_AM::LSR)
2262           .Case("asr", AArch64_AM::ASR)
2263           .Case("ror", AArch64_AM::ROR)
2264           .Case("msl", AArch64_AM::MSL)
2265           .Case("uxtb", AArch64_AM::UXTB)
2266           .Case("uxth", AArch64_AM::UXTH)
2267           .Case("uxtw", AArch64_AM::UXTW)
2268           .Case("uxtx", AArch64_AM::UXTX)
2269           .Case("sxtb", AArch64_AM::SXTB)
2270           .Case("sxth", AArch64_AM::SXTH)
2271           .Case("sxtw", AArch64_AM::SXTW)
2272           .Case("sxtx", AArch64_AM::SXTX)
2273           .Default(AArch64_AM::InvalidShiftExtend);
2274
2275   if (ShOp == AArch64_AM::InvalidShiftExtend)
2276     return MatchOperand_NoMatch;
2277
2278   SMLoc S = Tok.getLoc();
2279   Parser.Lex();
2280
2281   bool Hash = getLexer().is(AsmToken::Hash);
2282   if (!Hash && getLexer().isNot(AsmToken::Integer)) {
2283     if (ShOp == AArch64_AM::LSL || ShOp == AArch64_AM::LSR ||
2284         ShOp == AArch64_AM::ASR || ShOp == AArch64_AM::ROR ||
2285         ShOp == AArch64_AM::MSL) {
2286       // We expect a number here.
2287       TokError("expected #imm after shift specifier");
2288       return MatchOperand_ParseFail;
2289     }
2290
2291     // "extend" type operatoins don't need an immediate, #0 is implicit.
2292     SMLoc E = SMLoc::getFromPointer(getLoc().getPointer() - 1);
2293     Operands.push_back(
2294         AArch64Operand::CreateShiftExtend(ShOp, 0, false, S, E, getContext()));
2295     return MatchOperand_Success;
2296   }
2297
2298   if (Hash)
2299     Parser.Lex(); // Eat the '#'.
2300
2301   // Make sure we do actually have a number
2302   if (!Parser.getTok().is(AsmToken::Integer)) {
2303     Error(Parser.getTok().getLoc(),
2304           "expected integer shift amount");
2305     return MatchOperand_ParseFail;
2306   }
2307
2308   const MCExpr *ImmVal;
2309   if (getParser().parseExpression(ImmVal))
2310     return MatchOperand_ParseFail;
2311
2312   const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
2313   if (!MCE) {
2314     TokError("expected #imm after shift specifier");
2315     return MatchOperand_ParseFail;
2316   }
2317
2318   SMLoc E = SMLoc::getFromPointer(getLoc().getPointer() - 1);
2319   Operands.push_back(AArch64Operand::CreateShiftExtend(
2320       ShOp, MCE->getValue(), true, S, E, getContext()));
2321   return MatchOperand_Success;
2322 }
2323
2324 /// parseSysAlias - The IC, DC, AT, and TLBI instructions are simple aliases for
2325 /// the SYS instruction. Parse them specially so that we create a SYS MCInst.
2326 bool AArch64AsmParser::parseSysAlias(StringRef Name, SMLoc NameLoc,
2327                                    OperandVector &Operands) {
2328   if (Name.find('.') != StringRef::npos)
2329     return TokError("invalid operand");
2330
2331   Mnemonic = Name;
2332   Operands.push_back(
2333       AArch64Operand::CreateToken("sys", false, NameLoc, getContext()));
2334
2335   const AsmToken &Tok = Parser.getTok();
2336   StringRef Op = Tok.getString();
2337   SMLoc S = Tok.getLoc();
2338
2339   const MCExpr *Expr = nullptr;
2340
2341 #define SYS_ALIAS(op1, Cn, Cm, op2)                                            \
2342   do {                                                                         \
2343     Expr = MCConstantExpr::Create(op1, getContext());                          \
2344     Operands.push_back(                                                        \
2345         AArch64Operand::CreateImm(Expr, S, getLoc(), getContext()));           \
2346     Operands.push_back(                                                        \
2347         AArch64Operand::CreateSysCR(Cn, S, getLoc(), getContext()));           \
2348     Operands.push_back(                                                        \
2349         AArch64Operand::CreateSysCR(Cm, S, getLoc(), getContext()));           \
2350     Expr = MCConstantExpr::Create(op2, getContext());                          \
2351     Operands.push_back(                                                        \
2352         AArch64Operand::CreateImm(Expr, S, getLoc(), getContext()));           \
2353   } while (0)
2354
2355   if (Mnemonic == "ic") {
2356     if (!Op.compare_lower("ialluis")) {
2357       // SYS #0, C7, C1, #0
2358       SYS_ALIAS(0, 7, 1, 0);
2359     } else if (!Op.compare_lower("iallu")) {
2360       // SYS #0, C7, C5, #0
2361       SYS_ALIAS(0, 7, 5, 0);
2362     } else if (!Op.compare_lower("ivau")) {
2363       // SYS #3, C7, C5, #1
2364       SYS_ALIAS(3, 7, 5, 1);
2365     } else {
2366       return TokError("invalid operand for IC instruction");
2367     }
2368   } else if (Mnemonic == "dc") {
2369     if (!Op.compare_lower("zva")) {
2370       // SYS #3, C7, C4, #1
2371       SYS_ALIAS(3, 7, 4, 1);
2372     } else if (!Op.compare_lower("ivac")) {
2373       // SYS #3, C7, C6, #1
2374       SYS_ALIAS(0, 7, 6, 1);
2375     } else if (!Op.compare_lower("isw")) {
2376       // SYS #0, C7, C6, #2
2377       SYS_ALIAS(0, 7, 6, 2);
2378     } else if (!Op.compare_lower("cvac")) {
2379       // SYS #3, C7, C10, #1
2380       SYS_ALIAS(3, 7, 10, 1);
2381     } else if (!Op.compare_lower("csw")) {
2382       // SYS #0, C7, C10, #2
2383       SYS_ALIAS(0, 7, 10, 2);
2384     } else if (!Op.compare_lower("cvau")) {
2385       // SYS #3, C7, C11, #1
2386       SYS_ALIAS(3, 7, 11, 1);
2387     } else if (!Op.compare_lower("civac")) {
2388       // SYS #3, C7, C14, #1
2389       SYS_ALIAS(3, 7, 14, 1);
2390     } else if (!Op.compare_lower("cisw")) {
2391       // SYS #0, C7, C14, #2
2392       SYS_ALIAS(0, 7, 14, 2);
2393     } else {
2394       return TokError("invalid operand for DC instruction");
2395     }
2396   } else if (Mnemonic == "at") {
2397     if (!Op.compare_lower("s1e1r")) {
2398       // SYS #0, C7, C8, #0
2399       SYS_ALIAS(0, 7, 8, 0);
2400     } else if (!Op.compare_lower("s1e2r")) {
2401       // SYS #4, C7, C8, #0
2402       SYS_ALIAS(4, 7, 8, 0);
2403     } else if (!Op.compare_lower("s1e3r")) {
2404       // SYS #6, C7, C8, #0
2405       SYS_ALIAS(6, 7, 8, 0);
2406     } else if (!Op.compare_lower("s1e1w")) {
2407       // SYS #0, C7, C8, #1
2408       SYS_ALIAS(0, 7, 8, 1);
2409     } else if (!Op.compare_lower("s1e2w")) {
2410       // SYS #4, C7, C8, #1
2411       SYS_ALIAS(4, 7, 8, 1);
2412     } else if (!Op.compare_lower("s1e3w")) {
2413       // SYS #6, C7, C8, #1
2414       SYS_ALIAS(6, 7, 8, 1);
2415     } else if (!Op.compare_lower("s1e0r")) {
2416       // SYS #0, C7, C8, #3
2417       SYS_ALIAS(0, 7, 8, 2);
2418     } else if (!Op.compare_lower("s1e0w")) {
2419       // SYS #0, C7, C8, #3
2420       SYS_ALIAS(0, 7, 8, 3);
2421     } else if (!Op.compare_lower("s12e1r")) {
2422       // SYS #4, C7, C8, #4
2423       SYS_ALIAS(4, 7, 8, 4);
2424     } else if (!Op.compare_lower("s12e1w")) {
2425       // SYS #4, C7, C8, #5
2426       SYS_ALIAS(4, 7, 8, 5);
2427     } else if (!Op.compare_lower("s12e0r")) {
2428       // SYS #4, C7, C8, #6
2429       SYS_ALIAS(4, 7, 8, 6);
2430     } else if (!Op.compare_lower("s12e0w")) {
2431       // SYS #4, C7, C8, #7
2432       SYS_ALIAS(4, 7, 8, 7);
2433     } else {
2434       return TokError("invalid operand for AT instruction");
2435     }
2436   } else if (Mnemonic == "tlbi") {
2437     if (!Op.compare_lower("vmalle1is")) {
2438       // SYS #0, C8, C3, #0
2439       SYS_ALIAS(0, 8, 3, 0);
2440     } else if (!Op.compare_lower("alle2is")) {
2441       // SYS #4, C8, C3, #0
2442       SYS_ALIAS(4, 8, 3, 0);
2443     } else if (!Op.compare_lower("alle3is")) {
2444       // SYS #6, C8, C3, #0
2445       SYS_ALIAS(6, 8, 3, 0);
2446     } else if (!Op.compare_lower("vae1is")) {
2447       // SYS #0, C8, C3, #1
2448       SYS_ALIAS(0, 8, 3, 1);
2449     } else if (!Op.compare_lower("vae2is")) {
2450       // SYS #4, C8, C3, #1
2451       SYS_ALIAS(4, 8, 3, 1);
2452     } else if (!Op.compare_lower("vae3is")) {
2453       // SYS #6, C8, C3, #1
2454       SYS_ALIAS(6, 8, 3, 1);
2455     } else if (!Op.compare_lower("aside1is")) {
2456       // SYS #0, C8, C3, #2
2457       SYS_ALIAS(0, 8, 3, 2);
2458     } else if (!Op.compare_lower("vaae1is")) {
2459       // SYS #0, C8, C3, #3
2460       SYS_ALIAS(0, 8, 3, 3);
2461     } else if (!Op.compare_lower("alle1is")) {
2462       // SYS #4, C8, C3, #4
2463       SYS_ALIAS(4, 8, 3, 4);
2464     } else if (!Op.compare_lower("vale1is")) {
2465       // SYS #0, C8, C3, #5
2466       SYS_ALIAS(0, 8, 3, 5);
2467     } else if (!Op.compare_lower("vaale1is")) {
2468       // SYS #0, C8, C3, #7
2469       SYS_ALIAS(0, 8, 3, 7);
2470     } else if (!Op.compare_lower("vmalle1")) {
2471       // SYS #0, C8, C7, #0
2472       SYS_ALIAS(0, 8, 7, 0);
2473     } else if (!Op.compare_lower("alle2")) {
2474       // SYS #4, C8, C7, #0
2475       SYS_ALIAS(4, 8, 7, 0);
2476     } else if (!Op.compare_lower("vale2is")) {
2477       // SYS #4, C8, C3, #5
2478       SYS_ALIAS(4, 8, 3, 5);
2479     } else if (!Op.compare_lower("vale3is")) {
2480       // SYS #6, C8, C3, #5
2481       SYS_ALIAS(6, 8, 3, 5);
2482     } else if (!Op.compare_lower("alle3")) {
2483       // SYS #6, C8, C7, #0
2484       SYS_ALIAS(6, 8, 7, 0);
2485     } else if (!Op.compare_lower("vae1")) {
2486       // SYS #0, C8, C7, #1
2487       SYS_ALIAS(0, 8, 7, 1);
2488     } else if (!Op.compare_lower("vae2")) {
2489       // SYS #4, C8, C7, #1
2490       SYS_ALIAS(4, 8, 7, 1);
2491     } else if (!Op.compare_lower("vae3")) {
2492       // SYS #6, C8, C7, #1
2493       SYS_ALIAS(6, 8, 7, 1);
2494     } else if (!Op.compare_lower("aside1")) {
2495       // SYS #0, C8, C7, #2
2496       SYS_ALIAS(0, 8, 7, 2);
2497     } else if (!Op.compare_lower("vaae1")) {
2498       // SYS #0, C8, C7, #3
2499       SYS_ALIAS(0, 8, 7, 3);
2500     } else if (!Op.compare_lower("alle1")) {
2501       // SYS #4, C8, C7, #4
2502       SYS_ALIAS(4, 8, 7, 4);
2503     } else if (!Op.compare_lower("vale1")) {
2504       // SYS #0, C8, C7, #5
2505       SYS_ALIAS(0, 8, 7, 5);
2506     } else if (!Op.compare_lower("vale2")) {
2507       // SYS #4, C8, C7, #5
2508       SYS_ALIAS(4, 8, 7, 5);
2509     } else if (!Op.compare_lower("vale3")) {
2510       // SYS #6, C8, C7, #5
2511       SYS_ALIAS(6, 8, 7, 5);
2512     } else if (!Op.compare_lower("vaale1")) {
2513       // SYS #0, C8, C7, #7
2514       SYS_ALIAS(0, 8, 7, 7);
2515     } else if (!Op.compare_lower("ipas2e1")) {
2516       // SYS #4, C8, C4, #1
2517       SYS_ALIAS(4, 8, 4, 1);
2518     } else if (!Op.compare_lower("ipas2le1")) {
2519       // SYS #4, C8, C4, #5
2520       SYS_ALIAS(4, 8, 4, 5);
2521     } else if (!Op.compare_lower("ipas2e1is")) {
2522       // SYS #4, C8, C4, #1
2523       SYS_ALIAS(4, 8, 0, 1);
2524     } else if (!Op.compare_lower("ipas2le1is")) {
2525       // SYS #4, C8, C4, #5
2526       SYS_ALIAS(4, 8, 0, 5);
2527     } else if (!Op.compare_lower("vmalls12e1")) {
2528       // SYS #4, C8, C7, #6
2529       SYS_ALIAS(4, 8, 7, 6);
2530     } else if (!Op.compare_lower("vmalls12e1is")) {
2531       // SYS #4, C8, C3, #6
2532       SYS_ALIAS(4, 8, 3, 6);
2533     } else {
2534       return TokError("invalid operand for TLBI instruction");
2535     }
2536   }
2537
2538 #undef SYS_ALIAS
2539
2540   Parser.Lex(); // Eat operand.
2541
2542   bool ExpectRegister = (Op.lower().find("all") == StringRef::npos);
2543   bool HasRegister = false;
2544
2545   // Check for the optional register operand.
2546   if (getLexer().is(AsmToken::Comma)) {
2547     Parser.Lex(); // Eat comma.
2548
2549     if (Tok.isNot(AsmToken::Identifier) || parseRegister(Operands))
2550       return TokError("expected register operand");
2551
2552     HasRegister = true;
2553   }
2554
2555   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2556     Parser.eatToEndOfStatement();
2557     return TokError("unexpected token in argument list");
2558   }
2559
2560   if (ExpectRegister && !HasRegister) {
2561     return TokError("specified " + Mnemonic + " op requires a register");
2562   }
2563   else if (!ExpectRegister && HasRegister) {
2564     return TokError("specified " + Mnemonic + " op does not use a register");
2565   }
2566
2567   Parser.Lex(); // Consume the EndOfStatement
2568   return false;
2569 }
2570
2571 AArch64AsmParser::OperandMatchResultTy
2572 AArch64AsmParser::tryParseBarrierOperand(OperandVector &Operands) {
2573   const AsmToken &Tok = Parser.getTok();
2574
2575   // Can be either a #imm style literal or an option name
2576   bool Hash = Tok.is(AsmToken::Hash);
2577   if (Hash || Tok.is(AsmToken::Integer)) {
2578     // Immediate operand.
2579     if (Hash)
2580       Parser.Lex(); // Eat the '#'
2581     const MCExpr *ImmVal;
2582     SMLoc ExprLoc = getLoc();
2583     if (getParser().parseExpression(ImmVal))
2584       return MatchOperand_ParseFail;
2585     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
2586     if (!MCE) {
2587       Error(ExprLoc, "immediate value expected for barrier operand");
2588       return MatchOperand_ParseFail;
2589     }
2590     if (MCE->getValue() < 0 || MCE->getValue() > 15) {
2591       Error(ExprLoc, "barrier operand out of range");
2592       return MatchOperand_ParseFail;
2593     }
2594     Operands.push_back(
2595         AArch64Operand::CreateBarrier(MCE->getValue(), ExprLoc, getContext()));
2596     return MatchOperand_Success;
2597   }
2598
2599   if (Tok.isNot(AsmToken::Identifier)) {
2600     TokError("invalid operand for instruction");
2601     return MatchOperand_ParseFail;
2602   }
2603
2604   bool Valid;
2605   unsigned Opt = AArch64DB::DBarrierMapper().fromString(Tok.getString(), Valid);
2606   if (!Valid) {
2607     TokError("invalid barrier option name");
2608     return MatchOperand_ParseFail;
2609   }
2610
2611   // The only valid named option for ISB is 'sy'
2612   if (Mnemonic == "isb" && Opt != AArch64DB::SY) {
2613     TokError("'sy' or #imm operand expected");
2614     return MatchOperand_ParseFail;
2615   }
2616
2617   Operands.push_back(
2618       AArch64Operand::CreateBarrier(Opt, getLoc(), getContext()));
2619   Parser.Lex(); // Consume the option
2620
2621   return MatchOperand_Success;
2622 }
2623
2624 AArch64AsmParser::OperandMatchResultTy
2625 AArch64AsmParser::tryParseSysReg(OperandVector &Operands) {
2626   const AsmToken &Tok = Parser.getTok();
2627
2628   if (Tok.isNot(AsmToken::Identifier))
2629     return MatchOperand_NoMatch;
2630
2631   Operands.push_back(AArch64Operand::CreateSysReg(Tok.getString(), getLoc(),
2632                      STI.getFeatureBits(), getContext()));
2633   Parser.Lex(); // Eat identifier
2634
2635   return MatchOperand_Success;
2636 }
2637
2638 /// tryParseVectorRegister - Parse a vector register operand.
2639 bool AArch64AsmParser::tryParseVectorRegister(OperandVector &Operands) {
2640   if (Parser.getTok().isNot(AsmToken::Identifier))
2641     return true;
2642
2643   SMLoc S = getLoc();
2644   // Check for a vector register specifier first.
2645   StringRef Kind;
2646   int64_t Reg = tryMatchVectorRegister(Kind, false);
2647   if (Reg == -1)
2648     return true;
2649   Operands.push_back(
2650       AArch64Operand::CreateReg(Reg, true, S, getLoc(), getContext()));
2651   // If there was an explicit qualifier, that goes on as a literal text
2652   // operand.
2653   if (!Kind.empty())
2654     Operands.push_back(
2655         AArch64Operand::CreateToken(Kind, false, S, getContext()));
2656
2657   // If there is an index specifier following the register, parse that too.
2658   if (Parser.getTok().is(AsmToken::LBrac)) {
2659     SMLoc SIdx = getLoc();
2660     Parser.Lex(); // Eat left bracket token.
2661
2662     const MCExpr *ImmVal;
2663     if (getParser().parseExpression(ImmVal))
2664       return false;
2665     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
2666     if (!MCE) {
2667       TokError("immediate value expected for vector index");
2668       return false;
2669     }
2670
2671     SMLoc E = getLoc();
2672     if (Parser.getTok().isNot(AsmToken::RBrac)) {
2673       Error(E, "']' expected");
2674       return false;
2675     }
2676
2677     Parser.Lex(); // Eat right bracket token.
2678
2679     Operands.push_back(AArch64Operand::CreateVectorIndex(MCE->getValue(), SIdx,
2680                                                          E, getContext()));
2681   }
2682
2683   return false;
2684 }
2685
2686 /// parseRegister - Parse a non-vector register operand.
2687 bool AArch64AsmParser::parseRegister(OperandVector &Operands) {
2688   SMLoc S = getLoc();
2689   // Try for a vector register.
2690   if (!tryParseVectorRegister(Operands))
2691     return false;
2692
2693   // Try for a scalar register.
2694   int64_t Reg = tryParseRegister();
2695   if (Reg == -1)
2696     return true;
2697   Operands.push_back(
2698       AArch64Operand::CreateReg(Reg, false, S, getLoc(), getContext()));
2699
2700   // A small number of instructions (FMOVXDhighr, for example) have "[1]"
2701   // as a string token in the instruction itself.
2702   if (getLexer().getKind() == AsmToken::LBrac) {
2703     SMLoc LBracS = getLoc();
2704     Parser.Lex();
2705     const AsmToken &Tok = Parser.getTok();
2706     if (Tok.is(AsmToken::Integer)) {
2707       SMLoc IntS = getLoc();
2708       int64_t Val = Tok.getIntVal();
2709       if (Val == 1) {
2710         Parser.Lex();
2711         if (getLexer().getKind() == AsmToken::RBrac) {
2712           SMLoc RBracS = getLoc();
2713           Parser.Lex();
2714           Operands.push_back(
2715               AArch64Operand::CreateToken("[", false, LBracS, getContext()));
2716           Operands.push_back(
2717               AArch64Operand::CreateToken("1", false, IntS, getContext()));
2718           Operands.push_back(
2719               AArch64Operand::CreateToken("]", false, RBracS, getContext()));
2720           return false;
2721         }
2722       }
2723     }
2724   }
2725
2726   return false;
2727 }
2728
2729 bool AArch64AsmParser::parseSymbolicImmVal(const MCExpr *&ImmVal) {
2730   bool HasELFModifier = false;
2731   AArch64MCExpr::VariantKind RefKind;
2732
2733   if (Parser.getTok().is(AsmToken::Colon)) {
2734     Parser.Lex(); // Eat ':"
2735     HasELFModifier = true;
2736
2737     if (Parser.getTok().isNot(AsmToken::Identifier)) {
2738       Error(Parser.getTok().getLoc(),
2739             "expect relocation specifier in operand after ':'");
2740       return true;
2741     }
2742
2743     std::string LowerCase = Parser.getTok().getIdentifier().lower();
2744     RefKind = StringSwitch<AArch64MCExpr::VariantKind>(LowerCase)
2745                   .Case("lo12", AArch64MCExpr::VK_LO12)
2746                   .Case("abs_g3", AArch64MCExpr::VK_ABS_G3)
2747                   .Case("abs_g2", AArch64MCExpr::VK_ABS_G2)
2748                   .Case("abs_g2_s", AArch64MCExpr::VK_ABS_G2_S)
2749                   .Case("abs_g2_nc", AArch64MCExpr::VK_ABS_G2_NC)
2750                   .Case("abs_g1", AArch64MCExpr::VK_ABS_G1)
2751                   .Case("abs_g1_s", AArch64MCExpr::VK_ABS_G1_S)
2752                   .Case("abs_g1_nc", AArch64MCExpr::VK_ABS_G1_NC)
2753                   .Case("abs_g0", AArch64MCExpr::VK_ABS_G0)
2754                   .Case("abs_g0_s", AArch64MCExpr::VK_ABS_G0_S)
2755                   .Case("abs_g0_nc", AArch64MCExpr::VK_ABS_G0_NC)
2756                   .Case("dtprel_g2", AArch64MCExpr::VK_DTPREL_G2)
2757                   .Case("dtprel_g1", AArch64MCExpr::VK_DTPREL_G1)
2758                   .Case("dtprel_g1_nc", AArch64MCExpr::VK_DTPREL_G1_NC)
2759                   .Case("dtprel_g0", AArch64MCExpr::VK_DTPREL_G0)
2760                   .Case("dtprel_g0_nc", AArch64MCExpr::VK_DTPREL_G0_NC)
2761                   .Case("dtprel_hi12", AArch64MCExpr::VK_DTPREL_HI12)
2762                   .Case("dtprel_lo12", AArch64MCExpr::VK_DTPREL_LO12)
2763                   .Case("dtprel_lo12_nc", AArch64MCExpr::VK_DTPREL_LO12_NC)
2764                   .Case("tprel_g2", AArch64MCExpr::VK_TPREL_G2)
2765                   .Case("tprel_g1", AArch64MCExpr::VK_TPREL_G1)
2766                   .Case("tprel_g1_nc", AArch64MCExpr::VK_TPREL_G1_NC)
2767                   .Case("tprel_g0", AArch64MCExpr::VK_TPREL_G0)
2768                   .Case("tprel_g0_nc", AArch64MCExpr::VK_TPREL_G0_NC)
2769                   .Case("tprel_hi12", AArch64MCExpr::VK_TPREL_HI12)
2770                   .Case("tprel_lo12", AArch64MCExpr::VK_TPREL_LO12)
2771                   .Case("tprel_lo12_nc", AArch64MCExpr::VK_TPREL_LO12_NC)
2772                   .Case("tlsdesc_lo12", AArch64MCExpr::VK_TLSDESC_LO12)
2773                   .Case("got", AArch64MCExpr::VK_GOT_PAGE)
2774                   .Case("got_lo12", AArch64MCExpr::VK_GOT_LO12)
2775                   .Case("gottprel", AArch64MCExpr::VK_GOTTPREL_PAGE)
2776                   .Case("gottprel_lo12", AArch64MCExpr::VK_GOTTPREL_LO12_NC)
2777                   .Case("gottprel_g1", AArch64MCExpr::VK_GOTTPREL_G1)
2778                   .Case("gottprel_g0_nc", AArch64MCExpr::VK_GOTTPREL_G0_NC)
2779                   .Case("tlsdesc", AArch64MCExpr::VK_TLSDESC_PAGE)
2780                   .Default(AArch64MCExpr::VK_INVALID);
2781
2782     if (RefKind == AArch64MCExpr::VK_INVALID) {
2783       Error(Parser.getTok().getLoc(),
2784             "expect relocation specifier in operand after ':'");
2785       return true;
2786     }
2787
2788     Parser.Lex(); // Eat identifier
2789
2790     if (Parser.getTok().isNot(AsmToken::Colon)) {
2791       Error(Parser.getTok().getLoc(), "expect ':' after relocation specifier");
2792       return true;
2793     }
2794     Parser.Lex(); // Eat ':'
2795   }
2796
2797   if (getParser().parseExpression(ImmVal))
2798     return true;
2799
2800   if (HasELFModifier)
2801     ImmVal = AArch64MCExpr::Create(ImmVal, RefKind, getContext());
2802
2803   return false;
2804 }
2805
2806 /// parseVectorList - Parse a vector list operand for AdvSIMD instructions.
2807 bool AArch64AsmParser::parseVectorList(OperandVector &Operands) {
2808   assert(Parser.getTok().is(AsmToken::LCurly) && "Token is not a Left Bracket");
2809   SMLoc S = getLoc();
2810   Parser.Lex(); // Eat left bracket token.
2811   StringRef Kind;
2812   int64_t FirstReg = tryMatchVectorRegister(Kind, true);
2813   if (FirstReg == -1)
2814     return true;
2815   int64_t PrevReg = FirstReg;
2816   unsigned Count = 1;
2817
2818   if (Parser.getTok().is(AsmToken::Minus)) {
2819     Parser.Lex(); // Eat the minus.
2820
2821     SMLoc Loc = getLoc();
2822     StringRef NextKind;
2823     int64_t Reg = tryMatchVectorRegister(NextKind, true);
2824     if (Reg == -1)
2825       return true;
2826     // Any Kind suffices must match on all regs in the list.
2827     if (Kind != NextKind)
2828       return Error(Loc, "mismatched register size suffix");
2829
2830     unsigned Space = (PrevReg < Reg) ? (Reg - PrevReg) : (Reg + 32 - PrevReg);
2831
2832     if (Space == 0 || Space > 3) {
2833       return Error(Loc, "invalid number of vectors");
2834     }
2835
2836     Count += Space;
2837   }
2838   else {
2839     while (Parser.getTok().is(AsmToken::Comma)) {
2840       Parser.Lex(); // Eat the comma token.
2841
2842       SMLoc Loc = getLoc();
2843       StringRef NextKind;
2844       int64_t Reg = tryMatchVectorRegister(NextKind, true);
2845       if (Reg == -1)
2846         return true;
2847       // Any Kind suffices must match on all regs in the list.
2848       if (Kind != NextKind)
2849         return Error(Loc, "mismatched register size suffix");
2850
2851       // Registers must be incremental (with wraparound at 31)
2852       if (getContext().getRegisterInfo()->getEncodingValue(Reg) !=
2853           (getContext().getRegisterInfo()->getEncodingValue(PrevReg) + 1) % 32)
2854        return Error(Loc, "registers must be sequential");
2855
2856       PrevReg = Reg;
2857       ++Count;
2858     }
2859   }
2860
2861   if (Parser.getTok().isNot(AsmToken::RCurly))
2862     return Error(getLoc(), "'}' expected");
2863   Parser.Lex(); // Eat the '}' token.
2864
2865   if (Count > 4)
2866     return Error(S, "invalid number of vectors");
2867
2868   unsigned NumElements = 0;
2869   char ElementKind = 0;
2870   if (!Kind.empty())
2871     parseValidVectorKind(Kind, NumElements, ElementKind);
2872
2873   Operands.push_back(AArch64Operand::CreateVectorList(
2874       FirstReg, Count, NumElements, ElementKind, S, getLoc(), getContext()));
2875
2876   // If there is an index specifier following the list, parse that too.
2877   if (Parser.getTok().is(AsmToken::LBrac)) {
2878     SMLoc SIdx = getLoc();
2879     Parser.Lex(); // Eat left bracket token.
2880
2881     const MCExpr *ImmVal;
2882     if (getParser().parseExpression(ImmVal))
2883       return false;
2884     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
2885     if (!MCE) {
2886       TokError("immediate value expected for vector index");
2887       return false;
2888     }
2889
2890     SMLoc E = getLoc();
2891     if (Parser.getTok().isNot(AsmToken::RBrac)) {
2892       Error(E, "']' expected");
2893       return false;
2894     }
2895
2896     Parser.Lex(); // Eat right bracket token.
2897
2898     Operands.push_back(AArch64Operand::CreateVectorIndex(MCE->getValue(), SIdx,
2899                                                          E, getContext()));
2900   }
2901   return false;
2902 }
2903
2904 AArch64AsmParser::OperandMatchResultTy
2905 AArch64AsmParser::tryParseGPR64sp0Operand(OperandVector &Operands) {
2906   const AsmToken &Tok = Parser.getTok();
2907   if (!Tok.is(AsmToken::Identifier))
2908     return MatchOperand_NoMatch;
2909
2910   unsigned RegNum = matchRegisterNameAlias(Tok.getString().lower(), false);
2911
2912   MCContext &Ctx = getContext();
2913   const MCRegisterInfo *RI = Ctx.getRegisterInfo();
2914   if (!RI->getRegClass(AArch64::GPR64spRegClassID).contains(RegNum))
2915     return MatchOperand_NoMatch;
2916
2917   SMLoc S = getLoc();
2918   Parser.Lex(); // Eat register
2919
2920   if (Parser.getTok().isNot(AsmToken::Comma)) {
2921     Operands.push_back(
2922         AArch64Operand::CreateReg(RegNum, false, S, getLoc(), Ctx));
2923     return MatchOperand_Success;
2924   }
2925   Parser.Lex(); // Eat comma.
2926
2927   if (Parser.getTok().is(AsmToken::Hash))
2928     Parser.Lex(); // Eat hash
2929
2930   if (Parser.getTok().isNot(AsmToken::Integer)) {
2931     Error(getLoc(), "index must be absent or #0");
2932     return MatchOperand_ParseFail;
2933   }
2934
2935   const MCExpr *ImmVal;
2936   if (Parser.parseExpression(ImmVal) || !isa<MCConstantExpr>(ImmVal) ||
2937       cast<MCConstantExpr>(ImmVal)->getValue() != 0) {
2938     Error(getLoc(), "index must be absent or #0");
2939     return MatchOperand_ParseFail;
2940   }
2941
2942   Operands.push_back(
2943       AArch64Operand::CreateReg(RegNum, false, S, getLoc(), Ctx));
2944   return MatchOperand_Success;
2945 }
2946
2947 /// parseOperand - Parse a arm instruction operand.  For now this parses the
2948 /// operand regardless of the mnemonic.
2949 bool AArch64AsmParser::parseOperand(OperandVector &Operands, bool isCondCode,
2950                                   bool invertCondCode) {
2951   // Check if the current operand has a custom associated parser, if so, try to
2952   // custom parse the operand, or fallback to the general approach.
2953   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
2954   if (ResTy == MatchOperand_Success)
2955     return false;
2956   // If there wasn't a custom match, try the generic matcher below. Otherwise,
2957   // there was a match, but an error occurred, in which case, just return that
2958   // the operand parsing failed.
2959   if (ResTy == MatchOperand_ParseFail)
2960     return true;
2961
2962   // Nothing custom, so do general case parsing.
2963   SMLoc S, E;
2964   switch (getLexer().getKind()) {
2965   default: {
2966     SMLoc S = getLoc();
2967     const MCExpr *Expr;
2968     if (parseSymbolicImmVal(Expr))
2969       return Error(S, "invalid operand");
2970
2971     SMLoc E = SMLoc::getFromPointer(getLoc().getPointer() - 1);
2972     Operands.push_back(AArch64Operand::CreateImm(Expr, S, E, getContext()));
2973     return false;
2974   }
2975   case AsmToken::LBrac: {
2976     SMLoc Loc = Parser.getTok().getLoc();
2977     Operands.push_back(AArch64Operand::CreateToken("[", false, Loc,
2978                                                    getContext()));
2979     Parser.Lex(); // Eat '['
2980
2981     // There's no comma after a '[', so we can parse the next operand
2982     // immediately.
2983     return parseOperand(Operands, false, false);
2984   }
2985   case AsmToken::LCurly:
2986     return parseVectorList(Operands);
2987   case AsmToken::Identifier: {
2988     // If we're expecting a Condition Code operand, then just parse that.
2989     if (isCondCode)
2990       return parseCondCode(Operands, invertCondCode);
2991
2992     // If it's a register name, parse it.
2993     if (!parseRegister(Operands))
2994       return false;
2995
2996     // This could be an optional "shift" or "extend" operand.
2997     OperandMatchResultTy GotShift = tryParseOptionalShiftExtend(Operands);
2998     // We can only continue if no tokens were eaten.
2999     if (GotShift != MatchOperand_NoMatch)
3000       return GotShift;
3001
3002     // This was not a register so parse other operands that start with an
3003     // identifier (like labels) as expressions and create them as immediates.
3004     const MCExpr *IdVal;
3005     S = getLoc();
3006     if (getParser().parseExpression(IdVal))
3007       return true;
3008
3009     E = SMLoc::getFromPointer(getLoc().getPointer() - 1);
3010     Operands.push_back(AArch64Operand::CreateImm(IdVal, S, E, getContext()));
3011     return false;
3012   }
3013   case AsmToken::Integer:
3014   case AsmToken::Real:
3015   case AsmToken::Hash: {
3016     // #42 -> immediate.
3017     S = getLoc();
3018     if (getLexer().is(AsmToken::Hash))
3019       Parser.Lex();
3020
3021     // Parse a negative sign
3022     bool isNegative = false;
3023     if (Parser.getTok().is(AsmToken::Minus)) {
3024       isNegative = true;
3025       // We need to consume this token only when we have a Real, otherwise
3026       // we let parseSymbolicImmVal take care of it
3027       if (Parser.getLexer().peekTok().is(AsmToken::Real))
3028         Parser.Lex();
3029     }
3030
3031     // The only Real that should come through here is a literal #0.0 for
3032     // the fcmp[e] r, #0.0 instructions. They expect raw token operands,
3033     // so convert the value.
3034     const AsmToken &Tok = Parser.getTok();
3035     if (Tok.is(AsmToken::Real)) {
3036       APFloat RealVal(APFloat::IEEEdouble, Tok.getString());
3037       uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
3038       if (Mnemonic != "fcmp" && Mnemonic != "fcmpe" && Mnemonic != "fcmeq" &&
3039           Mnemonic != "fcmge" && Mnemonic != "fcmgt" && Mnemonic != "fcmle" &&
3040           Mnemonic != "fcmlt")
3041         return TokError("unexpected floating point literal");
3042       else if (IntVal != 0 || isNegative)
3043         return TokError("expected floating-point constant #0.0");
3044       Parser.Lex(); // Eat the token.
3045
3046       Operands.push_back(
3047           AArch64Operand::CreateToken("#0", false, S, getContext()));
3048       Operands.push_back(
3049           AArch64Operand::CreateToken(".0", false, S, getContext()));
3050       return false;
3051     }
3052
3053     const MCExpr *ImmVal;
3054     if (parseSymbolicImmVal(ImmVal))
3055       return true;
3056
3057     E = SMLoc::getFromPointer(getLoc().getPointer() - 1);
3058     Operands.push_back(AArch64Operand::CreateImm(ImmVal, S, E, getContext()));
3059     return false;
3060   }
3061   case AsmToken::Equal: {
3062     SMLoc Loc = Parser.getTok().getLoc();
3063     if (Mnemonic != "ldr") // only parse for ldr pseudo (e.g. ldr r0, =val)
3064       return Error(Loc, "unexpected token in operand");
3065     Parser.Lex(); // Eat '='
3066     const MCExpr *SubExprVal;
3067     if (getParser().parseExpression(SubExprVal))
3068       return true;
3069
3070     MCContext& Ctx = getContext();
3071     E = SMLoc::getFromPointer(Loc.getPointer() - 1);
3072     // If the op is an imm and can be fit into a mov, then replace ldr with mov.
3073     if (isa<MCConstantExpr>(SubExprVal) && Operands.size() >= 2 &&
3074         static_cast<AArch64Operand &>(*Operands[1]).isReg()) {
3075       bool IsXReg =  AArch64MCRegisterClasses[AArch64::GPR64allRegClassID].contains(
3076             Operands[1]->getReg());
3077       uint64_t Imm = (cast<MCConstantExpr>(SubExprVal))->getValue();
3078       uint32_t ShiftAmt = 0, MaxShiftAmt = IsXReg ? 48 : 16;
3079       while(Imm > 0xFFFF && countTrailingZeros(Imm) >= 16) {
3080         ShiftAmt += 16;
3081         Imm >>= 16;
3082       }
3083       if (ShiftAmt <= MaxShiftAmt && Imm <= 0xFFFF) {
3084           Operands[0] = AArch64Operand::CreateToken("movz", false, Loc, Ctx);
3085           Operands.push_back(AArch64Operand::CreateImm(
3086                      MCConstantExpr::Create(Imm, Ctx), S, E, Ctx));
3087         if (ShiftAmt)
3088           Operands.push_back(AArch64Operand::CreateShiftExtend(AArch64_AM::LSL,
3089                      ShiftAmt, true, S, E, Ctx));
3090         return false;
3091       }
3092     }
3093     // If it is a label or an imm that cannot fit in a movz, put it into CP.
3094     const MCExpr *CPLoc = getTargetStreamer().addConstantPoolEntry(SubExprVal);
3095     Operands.push_back(AArch64Operand::CreateImm(CPLoc, S, E, Ctx));
3096     return false;
3097   }
3098   }
3099 }
3100
3101 /// ParseInstruction - Parse an AArch64 instruction mnemonic followed by its
3102 /// operands.
3103 bool AArch64AsmParser::ParseInstruction(ParseInstructionInfo &Info,
3104                                         StringRef Name, SMLoc NameLoc,
3105                                         OperandVector &Operands) {
3106   Name = StringSwitch<StringRef>(Name.lower())
3107              .Case("beq", "b.eq")
3108              .Case("bne", "b.ne")
3109              .Case("bhs", "b.hs")
3110              .Case("bcs", "b.cs")
3111              .Case("blo", "b.lo")
3112              .Case("bcc", "b.cc")
3113              .Case("bmi", "b.mi")
3114              .Case("bpl", "b.pl")
3115              .Case("bvs", "b.vs")
3116              .Case("bvc", "b.vc")
3117              .Case("bhi", "b.hi")
3118              .Case("bls", "b.ls")
3119              .Case("bge", "b.ge")
3120              .Case("blt", "b.lt")
3121              .Case("bgt", "b.gt")
3122              .Case("ble", "b.le")
3123              .Case("bal", "b.al")
3124              .Case("bnv", "b.nv")
3125              .Default(Name);
3126
3127   // First check for the AArch64-specific .req directive.
3128   if (Parser.getTok().is(AsmToken::Identifier) &&
3129       Parser.getTok().getIdentifier() == ".req") {
3130     parseDirectiveReq(Name, NameLoc);
3131     // We always return 'error' for this, as we're done with this
3132     // statement and don't need to match the 'instruction."
3133     return true;
3134   }
3135
3136   // Create the leading tokens for the mnemonic, split by '.' characters.
3137   size_t Start = 0, Next = Name.find('.');
3138   StringRef Head = Name.slice(Start, Next);
3139
3140   // IC, DC, AT, and TLBI instructions are aliases for the SYS instruction.
3141   if (Head == "ic" || Head == "dc" || Head == "at" || Head == "tlbi") {
3142     bool IsError = parseSysAlias(Head, NameLoc, Operands);
3143     if (IsError && getLexer().isNot(AsmToken::EndOfStatement))
3144       Parser.eatToEndOfStatement();
3145     return IsError;
3146   }
3147
3148   Operands.push_back(
3149       AArch64Operand::CreateToken(Head, false, NameLoc, getContext()));
3150   Mnemonic = Head;
3151
3152   // Handle condition codes for a branch mnemonic
3153   if (Head == "b" && Next != StringRef::npos) {
3154     Start = Next;
3155     Next = Name.find('.', Start + 1);
3156     Head = Name.slice(Start + 1, Next);
3157
3158     SMLoc SuffixLoc = SMLoc::getFromPointer(NameLoc.getPointer() +
3159                                             (Head.data() - Name.data()));
3160     AArch64CC::CondCode CC = parseCondCodeString(Head);
3161     if (CC == AArch64CC::Invalid)
3162       return Error(SuffixLoc, "invalid condition code");
3163     Operands.push_back(
3164         AArch64Operand::CreateToken(".", true, SuffixLoc, getContext()));
3165     Operands.push_back(
3166         AArch64Operand::CreateCondCode(CC, NameLoc, NameLoc, getContext()));
3167   }
3168
3169   // Add the remaining tokens in the mnemonic.
3170   while (Next != StringRef::npos) {
3171     Start = Next;
3172     Next = Name.find('.', Start + 1);
3173     Head = Name.slice(Start, Next);
3174     SMLoc SuffixLoc = SMLoc::getFromPointer(NameLoc.getPointer() +
3175                                             (Head.data() - Name.data()) + 1);
3176     Operands.push_back(
3177         AArch64Operand::CreateToken(Head, true, SuffixLoc, getContext()));
3178   }
3179
3180   // Conditional compare instructions have a Condition Code operand, which needs
3181   // to be parsed and an immediate operand created.
3182   bool condCodeFourthOperand =
3183       (Head == "ccmp" || Head == "ccmn" || Head == "fccmp" ||
3184        Head == "fccmpe" || Head == "fcsel" || Head == "csel" ||
3185        Head == "csinc" || Head == "csinv" || Head == "csneg");
3186
3187   // These instructions are aliases to some of the conditional select
3188   // instructions. However, the condition code is inverted in the aliased
3189   // instruction.
3190   //
3191   // FIXME: Is this the correct way to handle these? Or should the parser
3192   //        generate the aliased instructions directly?
3193   bool condCodeSecondOperand = (Head == "cset" || Head == "csetm");
3194   bool condCodeThirdOperand =
3195       (Head == "cinc" || Head == "cinv" || Head == "cneg");
3196
3197   // Read the remaining operands.
3198   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3199     // Read the first operand.
3200     if (parseOperand(Operands, false, false)) {
3201       Parser.eatToEndOfStatement();
3202       return true;
3203     }
3204
3205     unsigned N = 2;
3206     while (getLexer().is(AsmToken::Comma)) {
3207       Parser.Lex(); // Eat the comma.
3208
3209       // Parse and remember the operand.
3210       if (parseOperand(Operands, (N == 4 && condCodeFourthOperand) ||
3211                                      (N == 3 && condCodeThirdOperand) ||
3212                                      (N == 2 && condCodeSecondOperand),
3213                        condCodeSecondOperand || condCodeThirdOperand)) {
3214         Parser.eatToEndOfStatement();
3215         return true;
3216       }
3217
3218       // After successfully parsing some operands there are two special cases to
3219       // consider (i.e. notional operands not separated by commas). Both are due
3220       // to memory specifiers:
3221       //  + An RBrac will end an address for load/store/prefetch
3222       //  + An '!' will indicate a pre-indexed operation.
3223       //
3224       // It's someone else's responsibility to make sure these tokens are sane
3225       // in the given context!
3226       if (Parser.getTok().is(AsmToken::RBrac)) {
3227         SMLoc Loc = Parser.getTok().getLoc();
3228         Operands.push_back(AArch64Operand::CreateToken("]", false, Loc,
3229                                                        getContext()));
3230         Parser.Lex();
3231       }
3232
3233       if (Parser.getTok().is(AsmToken::Exclaim)) {
3234         SMLoc Loc = Parser.getTok().getLoc();
3235         Operands.push_back(AArch64Operand::CreateToken("!", false, Loc,
3236                                                        getContext()));
3237         Parser.Lex();
3238       }
3239
3240       ++N;
3241     }
3242   }
3243
3244   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3245     SMLoc Loc = Parser.getTok().getLoc();
3246     Parser.eatToEndOfStatement();
3247     return Error(Loc, "unexpected token in argument list");
3248   }
3249
3250   Parser.Lex(); // Consume the EndOfStatement
3251   return false;
3252 }
3253
3254 // FIXME: This entire function is a giant hack to provide us with decent
3255 // operand range validation/diagnostics until TableGen/MC can be extended
3256 // to support autogeneration of this kind of validation.
3257 bool AArch64AsmParser::validateInstruction(MCInst &Inst,
3258                                          SmallVectorImpl<SMLoc> &Loc) {
3259   const MCRegisterInfo *RI = getContext().getRegisterInfo();
3260   // Check for indexed addressing modes w/ the base register being the
3261   // same as a destination/source register or pair load where
3262   // the Rt == Rt2. All of those are undefined behaviour.
3263   switch (Inst.getOpcode()) {
3264   case AArch64::LDPSWpre:
3265   case AArch64::LDPWpost:
3266   case AArch64::LDPWpre:
3267   case AArch64::LDPXpost:
3268   case AArch64::LDPXpre: {
3269     unsigned Rt = Inst.getOperand(1).getReg();
3270     unsigned Rt2 = Inst.getOperand(2).getReg();
3271     unsigned Rn = Inst.getOperand(3).getReg();
3272     if (RI->isSubRegisterEq(Rn, Rt))
3273       return Error(Loc[0], "unpredictable LDP instruction, writeback base "
3274                            "is also a destination");
3275     if (RI->isSubRegisterEq(Rn, Rt2))
3276       return Error(Loc[1], "unpredictable LDP instruction, writeback base "
3277                            "is also a destination");
3278     // FALLTHROUGH
3279   }
3280   case AArch64::LDPDi:
3281   case AArch64::LDPQi:
3282   case AArch64::LDPSi:
3283   case AArch64::LDPSWi:
3284   case AArch64::LDPWi:
3285   case AArch64::LDPXi: {
3286     unsigned Rt = Inst.getOperand(0).getReg();
3287     unsigned Rt2 = Inst.getOperand(1).getReg();
3288     if (Rt == Rt2)
3289       return Error(Loc[1], "unpredictable LDP instruction, Rt2==Rt");
3290     break;
3291   }
3292   case AArch64::LDPDpost:
3293   case AArch64::LDPDpre:
3294   case AArch64::LDPQpost:
3295   case AArch64::LDPQpre:
3296   case AArch64::LDPSpost:
3297   case AArch64::LDPSpre:
3298   case AArch64::LDPSWpost: {
3299     unsigned Rt = Inst.getOperand(1).getReg();
3300     unsigned Rt2 = Inst.getOperand(2).getReg();
3301     if (Rt == Rt2)
3302       return Error(Loc[1], "unpredictable LDP instruction, Rt2==Rt");
3303     break;
3304   }
3305   case AArch64::STPDpost:
3306   case AArch64::STPDpre:
3307   case AArch64::STPQpost:
3308   case AArch64::STPQpre:
3309   case AArch64::STPSpost:
3310   case AArch64::STPSpre:
3311   case AArch64::STPWpost:
3312   case AArch64::STPWpre:
3313   case AArch64::STPXpost:
3314   case AArch64::STPXpre: {
3315     unsigned Rt = Inst.getOperand(1).getReg();
3316     unsigned Rt2 = Inst.getOperand(2).getReg();
3317     unsigned Rn = Inst.getOperand(3).getReg();
3318     if (RI->isSubRegisterEq(Rn, Rt))
3319       return Error(Loc[0], "unpredictable STP instruction, writeback base "
3320                            "is also a source");
3321     if (RI->isSubRegisterEq(Rn, Rt2))
3322       return Error(Loc[1], "unpredictable STP instruction, writeback base "
3323                            "is also a source");
3324     break;
3325   }
3326   case AArch64::LDRBBpre:
3327   case AArch64::LDRBpre:
3328   case AArch64::LDRHHpre:
3329   case AArch64::LDRHpre:
3330   case AArch64::LDRSBWpre:
3331   case AArch64::LDRSBXpre:
3332   case AArch64::LDRSHWpre:
3333   case AArch64::LDRSHXpre:
3334   case AArch64::LDRSWpre:
3335   case AArch64::LDRWpre:
3336   case AArch64::LDRXpre:
3337   case AArch64::LDRBBpost:
3338   case AArch64::LDRBpost:
3339   case AArch64::LDRHHpost:
3340   case AArch64::LDRHpost:
3341   case AArch64::LDRSBWpost:
3342   case AArch64::LDRSBXpost:
3343   case AArch64::LDRSHWpost:
3344   case AArch64::LDRSHXpost:
3345   case AArch64::LDRSWpost:
3346   case AArch64::LDRWpost:
3347   case AArch64::LDRXpost: {
3348     unsigned Rt = Inst.getOperand(1).getReg();
3349     unsigned Rn = Inst.getOperand(2).getReg();
3350     if (RI->isSubRegisterEq(Rn, Rt))
3351       return Error(Loc[0], "unpredictable LDR instruction, writeback base "
3352                            "is also a source");
3353     break;
3354   }
3355   case AArch64::STRBBpost:
3356   case AArch64::STRBpost:
3357   case AArch64::STRHHpost:
3358   case AArch64::STRHpost:
3359   case AArch64::STRWpost:
3360   case AArch64::STRXpost:
3361   case AArch64::STRBBpre:
3362   case AArch64::STRBpre:
3363   case AArch64::STRHHpre:
3364   case AArch64::STRHpre:
3365   case AArch64::STRWpre:
3366   case AArch64::STRXpre: {
3367     unsigned Rt = Inst.getOperand(1).getReg();
3368     unsigned Rn = Inst.getOperand(2).getReg();
3369     if (RI->isSubRegisterEq(Rn, Rt))
3370       return Error(Loc[0], "unpredictable STR instruction, writeback base "
3371                            "is also a source");
3372     break;
3373   }
3374   }
3375
3376   // Now check immediate ranges. Separate from the above as there is overlap
3377   // in the instructions being checked and this keeps the nested conditionals
3378   // to a minimum.
3379   switch (Inst.getOpcode()) {
3380   case AArch64::ADDSWri:
3381   case AArch64::ADDSXri:
3382   case AArch64::ADDWri:
3383   case AArch64::ADDXri:
3384   case AArch64::SUBSWri:
3385   case AArch64::SUBSXri:
3386   case AArch64::SUBWri:
3387   case AArch64::SUBXri: {
3388     // Annoyingly we can't do this in the isAddSubImm predicate, so there is
3389     // some slight duplication here.
3390     if (Inst.getOperand(2).isExpr()) {
3391       const MCExpr *Expr = Inst.getOperand(2).getExpr();
3392       AArch64MCExpr::VariantKind ELFRefKind;
3393       MCSymbolRefExpr::VariantKind DarwinRefKind;
3394       int64_t Addend;
3395       if (!classifySymbolRef(Expr, ELFRefKind, DarwinRefKind, Addend)) {
3396         return Error(Loc[2], "invalid immediate expression");
3397       }
3398
3399       // Only allow these with ADDXri.
3400       if ((DarwinRefKind == MCSymbolRefExpr::VK_PAGEOFF ||
3401           DarwinRefKind == MCSymbolRefExpr::VK_TLVPPAGEOFF) &&
3402           Inst.getOpcode() == AArch64::ADDXri)
3403         return false;
3404
3405       // Only allow these with ADDXri/ADDWri
3406       if ((ELFRefKind == AArch64MCExpr::VK_LO12 ||
3407           ELFRefKind == AArch64MCExpr::VK_DTPREL_HI12 ||
3408           ELFRefKind == AArch64MCExpr::VK_DTPREL_LO12 ||
3409           ELFRefKind == AArch64MCExpr::VK_DTPREL_LO12_NC ||
3410           ELFRefKind == AArch64MCExpr::VK_TPREL_HI12 ||
3411           ELFRefKind == AArch64MCExpr::VK_TPREL_LO12 ||
3412           ELFRefKind == AArch64MCExpr::VK_TPREL_LO12_NC ||
3413           ELFRefKind == AArch64MCExpr::VK_TLSDESC_LO12) &&
3414           (Inst.getOpcode() == AArch64::ADDXri ||
3415           Inst.getOpcode() == AArch64::ADDWri))
3416         return false;
3417
3418       // Don't allow expressions in the immediate field otherwise
3419       return Error(Loc[2], "invalid immediate expression");
3420     }
3421     return false;
3422   }
3423   default:
3424     return false;
3425   }
3426 }
3427
3428 bool AArch64AsmParser::showMatchError(SMLoc Loc, unsigned ErrCode) {
3429   switch (ErrCode) {
3430   case Match_MissingFeature:
3431     return Error(Loc,
3432                  "instruction requires a CPU feature not currently enabled");
3433   case Match_InvalidOperand:
3434     return Error(Loc, "invalid operand for instruction");
3435   case Match_InvalidSuffix:
3436     return Error(Loc, "invalid type suffix for instruction");
3437   case Match_InvalidCondCode:
3438     return Error(Loc, "expected AArch64 condition code");
3439   case Match_AddSubRegExtendSmall:
3440     return Error(Loc,
3441       "expected '[su]xt[bhw]' or 'lsl' with optional integer in range [0, 4]");
3442   case Match_AddSubRegExtendLarge:
3443     return Error(Loc,
3444       "expected 'sxtx' 'uxtx' or 'lsl' with optional integer in range [0, 4]");
3445   case Match_AddSubSecondSource:
3446     return Error(Loc,
3447       "expected compatible register, symbol or integer in range [0, 4095]");
3448   case Match_LogicalSecondSource:
3449     return Error(Loc, "expected compatible register or logical immediate");
3450   case Match_InvalidMovImm32Shift:
3451     return Error(Loc, "expected 'lsl' with optional integer 0 or 16");
3452   case Match_InvalidMovImm64Shift:
3453     return Error(Loc, "expected 'lsl' with optional integer 0, 16, 32 or 48");
3454   case Match_AddSubRegShift32:
3455     return Error(Loc,
3456        "expected 'lsl', 'lsr' or 'asr' with optional integer in range [0, 31]");
3457   case Match_AddSubRegShift64:
3458     return Error(Loc,
3459        "expected 'lsl', 'lsr' or 'asr' with optional integer in range [0, 63]");
3460   case Match_InvalidFPImm:
3461     return Error(Loc,
3462                  "expected compatible register or floating-point constant");
3463   case Match_InvalidMemoryIndexedSImm9:
3464     return Error(Loc, "index must be an integer in range [-256, 255].");
3465   case Match_InvalidMemoryIndexed4SImm7:
3466     return Error(Loc, "index must be a multiple of 4 in range [-256, 252].");
3467   case Match_InvalidMemoryIndexed8SImm7:
3468     return Error(Loc, "index must be a multiple of 8 in range [-512, 504].");
3469   case Match_InvalidMemoryIndexed16SImm7:
3470     return Error(Loc, "index must be a multiple of 16 in range [-1024, 1008].");
3471   case Match_InvalidMemoryWExtend8:
3472     return Error(Loc,
3473                  "expected 'uxtw' or 'sxtw' with optional shift of #0");
3474   case Match_InvalidMemoryWExtend16:
3475     return Error(Loc,
3476                  "expected 'uxtw' or 'sxtw' with optional shift of #0 or #1");
3477   case Match_InvalidMemoryWExtend32:
3478     return Error(Loc,
3479                  "expected 'uxtw' or 'sxtw' with optional shift of #0 or #2");
3480   case Match_InvalidMemoryWExtend64:
3481     return Error(Loc,
3482                  "expected 'uxtw' or 'sxtw' with optional shift of #0 or #3");
3483   case Match_InvalidMemoryWExtend128:
3484     return Error(Loc,
3485                  "expected 'uxtw' or 'sxtw' with optional shift of #0 or #4");
3486   case Match_InvalidMemoryXExtend8:
3487     return Error(Loc,
3488                  "expected 'lsl' or 'sxtx' with optional shift of #0");
3489   case Match_InvalidMemoryXExtend16:
3490     return Error(Loc,
3491                  "expected 'lsl' or 'sxtx' with optional shift of #0 or #1");
3492   case Match_InvalidMemoryXExtend32:
3493     return Error(Loc,
3494                  "expected 'lsl' or 'sxtx' with optional shift of #0 or #2");
3495   case Match_InvalidMemoryXExtend64:
3496     return Error(Loc,
3497                  "expected 'lsl' or 'sxtx' with optional shift of #0 or #3");
3498   case Match_InvalidMemoryXExtend128:
3499     return Error(Loc,
3500                  "expected 'lsl' or 'sxtx' with optional shift of #0 or #4");
3501   case Match_InvalidMemoryIndexed1:
3502     return Error(Loc, "index must be an integer in range [0, 4095].");
3503   case Match_InvalidMemoryIndexed2:
3504     return Error(Loc, "index must be a multiple of 2 in range [0, 8190].");
3505   case Match_InvalidMemoryIndexed4:
3506     return Error(Loc, "index must be a multiple of 4 in range [0, 16380].");
3507   case Match_InvalidMemoryIndexed8:
3508     return Error(Loc, "index must be a multiple of 8 in range [0, 32760].");
3509   case Match_InvalidMemoryIndexed16:
3510     return Error(Loc, "index must be a multiple of 16 in range [0, 65520].");
3511   case Match_InvalidImm0_7:
3512     return Error(Loc, "immediate must be an integer in range [0, 7].");
3513   case Match_InvalidImm0_15:
3514     return Error(Loc, "immediate must be an integer in range [0, 15].");
3515   case Match_InvalidImm0_31:
3516     return Error(Loc, "immediate must be an integer in range [0, 31].");
3517   case Match_InvalidImm0_63:
3518     return Error(Loc, "immediate must be an integer in range [0, 63].");
3519   case Match_InvalidImm0_127:
3520     return Error(Loc, "immediate must be an integer in range [0, 127].");
3521   case Match_InvalidImm0_65535:
3522     return Error(Loc, "immediate must be an integer in range [0, 65535].");
3523   case Match_InvalidImm1_8:
3524     return Error(Loc, "immediate must be an integer in range [1, 8].");
3525   case Match_InvalidImm1_16:
3526     return Error(Loc, "immediate must be an integer in range [1, 16].");
3527   case Match_InvalidImm1_32:
3528     return Error(Loc, "immediate must be an integer in range [1, 32].");
3529   case Match_InvalidImm1_64:
3530     return Error(Loc, "immediate must be an integer in range [1, 64].");
3531   case Match_InvalidIndex1:
3532     return Error(Loc, "expected lane specifier '[1]'");
3533   case Match_InvalidIndexB:
3534     return Error(Loc, "vector lane must be an integer in range [0, 15].");
3535   case Match_InvalidIndexH:
3536     return Error(Loc, "vector lane must be an integer in range [0, 7].");
3537   case Match_InvalidIndexS:
3538     return Error(Loc, "vector lane must be an integer in range [0, 3].");
3539   case Match_InvalidIndexD:
3540     return Error(Loc, "vector lane must be an integer in range [0, 1].");
3541   case Match_InvalidLabel:
3542     return Error(Loc, "expected label or encodable integer pc offset");
3543   case Match_MRS:
3544     return Error(Loc, "expected readable system register");
3545   case Match_MSR:
3546     return Error(Loc, "expected writable system register or pstate");
3547   case Match_MnemonicFail:
3548     return Error(Loc, "unrecognized instruction mnemonic");
3549   default:
3550     llvm_unreachable("unexpected error code!");
3551   }
3552 }
3553
3554 static const char *getSubtargetFeatureName(unsigned Val);
3555
3556 bool AArch64AsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
3557                                                OperandVector &Operands,
3558                                                MCStreamer &Out,
3559                                                unsigned &ErrorInfo,
3560                                                bool MatchingInlineAsm) {
3561   assert(!Operands.empty() && "Unexpect empty operand list!");
3562   AArch64Operand &Op = static_cast<AArch64Operand &>(*Operands[0]);
3563   assert(Op.isToken() && "Leading operand should always be a mnemonic!");
3564
3565   StringRef Tok = Op.getToken();
3566   unsigned NumOperands = Operands.size();
3567
3568   if (NumOperands == 4 && Tok == "lsl") {
3569     AArch64Operand &Op2 = static_cast<AArch64Operand &>(*Operands[2]);
3570     AArch64Operand &Op3 = static_cast<AArch64Operand &>(*Operands[3]);
3571     if (Op2.isReg() && Op3.isImm()) {
3572       const MCConstantExpr *Op3CE = dyn_cast<MCConstantExpr>(Op3.getImm());
3573       if (Op3CE) {
3574         uint64_t Op3Val = Op3CE->getValue();
3575         uint64_t NewOp3Val = 0;
3576         uint64_t NewOp4Val = 0;
3577         if (AArch64MCRegisterClasses[AArch64::GPR32allRegClassID].contains(
3578                 Op2.getReg())) {
3579           NewOp3Val = (32 - Op3Val) & 0x1f;
3580           NewOp4Val = 31 - Op3Val;
3581         } else {
3582           NewOp3Val = (64 - Op3Val) & 0x3f;
3583           NewOp4Val = 63 - Op3Val;
3584         }
3585
3586         const MCExpr *NewOp3 = MCConstantExpr::Create(NewOp3Val, getContext());
3587         const MCExpr *NewOp4 = MCConstantExpr::Create(NewOp4Val, getContext());
3588
3589         Operands[0] = AArch64Operand::CreateToken(
3590             "ubfm", false, Op.getStartLoc(), getContext());
3591         Operands.push_back(AArch64Operand::CreateImm(
3592             NewOp4, Op3.getStartLoc(), Op3.getEndLoc(), getContext()));
3593         Operands[3] = AArch64Operand::CreateImm(NewOp3, Op3.getStartLoc(),
3594                                                 Op3.getEndLoc(), getContext());
3595       }
3596     }
3597   } else if (NumOperands == 5) {
3598     // FIXME: Horrible hack to handle the BFI -> BFM, SBFIZ->SBFM, and
3599     // UBFIZ -> UBFM aliases.
3600     if (Tok == "bfi" || Tok == "sbfiz" || Tok == "ubfiz") {
3601       AArch64Operand &Op1 = static_cast<AArch64Operand &>(*Operands[1]);
3602       AArch64Operand &Op3 = static_cast<AArch64Operand &>(*Operands[3]);
3603       AArch64Operand &Op4 = static_cast<AArch64Operand &>(*Operands[4]);
3604
3605       if (Op1.isReg() && Op3.isImm() && Op4.isImm()) {
3606         const MCConstantExpr *Op3CE = dyn_cast<MCConstantExpr>(Op3.getImm());
3607         const MCConstantExpr *Op4CE = dyn_cast<MCConstantExpr>(Op4.getImm());
3608
3609         if (Op3CE && Op4CE) {
3610           uint64_t Op3Val = Op3CE->getValue();
3611           uint64_t Op4Val = Op4CE->getValue();
3612
3613           uint64_t RegWidth = 0;
3614           if (AArch64MCRegisterClasses[AArch64::GPR64allRegClassID].contains(
3615                   Op1.getReg()))
3616             RegWidth = 64;
3617           else
3618             RegWidth = 32;
3619
3620           if (Op3Val >= RegWidth)
3621             return Error(Op3.getStartLoc(),
3622                          "expected integer in range [0, 31]");
3623           if (Op4Val < 1 || Op4Val > RegWidth)
3624             return Error(Op4.getStartLoc(),
3625                          "expected integer in range [1, 32]");
3626
3627           uint64_t NewOp3Val = 0;
3628           if (AArch64MCRegisterClasses[AArch64::GPR32allRegClassID].contains(
3629                   Op1.getReg()))
3630             NewOp3Val = (32 - Op3Val) & 0x1f;
3631           else
3632             NewOp3Val = (64 - Op3Val) & 0x3f;
3633
3634           uint64_t NewOp4Val = Op4Val - 1;
3635
3636           if (NewOp3Val != 0 && NewOp4Val >= NewOp3Val)
3637             return Error(Op4.getStartLoc(),
3638                          "requested insert overflows register");
3639
3640           const MCExpr *NewOp3 =
3641               MCConstantExpr::Create(NewOp3Val, getContext());
3642           const MCExpr *NewOp4 =
3643               MCConstantExpr::Create(NewOp4Val, getContext());
3644           Operands[3] = AArch64Operand::CreateImm(
3645               NewOp3, Op3.getStartLoc(), Op3.getEndLoc(), getContext());
3646           Operands[4] = AArch64Operand::CreateImm(
3647               NewOp4, Op4.getStartLoc(), Op4.getEndLoc(), getContext());
3648           if (Tok == "bfi")
3649             Operands[0] = AArch64Operand::CreateToken(
3650                 "bfm", false, Op.getStartLoc(), getContext());
3651           else if (Tok == "sbfiz")
3652             Operands[0] = AArch64Operand::CreateToken(
3653                 "sbfm", false, Op.getStartLoc(), getContext());
3654           else if (Tok == "ubfiz")
3655             Operands[0] = AArch64Operand::CreateToken(
3656                 "ubfm", false, Op.getStartLoc(), getContext());
3657           else
3658             llvm_unreachable("No valid mnemonic for alias?");
3659         }
3660       }
3661
3662       // FIXME: Horrible hack to handle the BFXIL->BFM, SBFX->SBFM, and
3663       // UBFX -> UBFM aliases.
3664     } else if (NumOperands == 5 &&
3665                (Tok == "bfxil" || Tok == "sbfx" || Tok == "ubfx")) {
3666       AArch64Operand &Op1 = static_cast<AArch64Operand &>(*Operands[1]);
3667       AArch64Operand &Op3 = static_cast<AArch64Operand &>(*Operands[3]);
3668       AArch64Operand &Op4 = static_cast<AArch64Operand &>(*Operands[4]);
3669
3670       if (Op1.isReg() && Op3.isImm() && Op4.isImm()) {
3671         const MCConstantExpr *Op3CE = dyn_cast<MCConstantExpr>(Op3.getImm());
3672         const MCConstantExpr *Op4CE = dyn_cast<MCConstantExpr>(Op4.getImm());
3673
3674         if (Op3CE && Op4CE) {
3675           uint64_t Op3Val = Op3CE->getValue();
3676           uint64_t Op4Val = Op4CE->getValue();
3677
3678           uint64_t RegWidth = 0;
3679           if (AArch64MCRegisterClasses[AArch64::GPR64allRegClassID].contains(
3680                   Op1.getReg()))
3681             RegWidth = 64;
3682           else
3683             RegWidth = 32;
3684
3685           if (Op3Val >= RegWidth)
3686             return Error(Op3.getStartLoc(),
3687                          "expected integer in range [0, 31]");
3688           if (Op4Val < 1 || Op4Val > RegWidth)
3689             return Error(Op4.getStartLoc(),
3690                          "expected integer in range [1, 32]");
3691
3692           uint64_t NewOp4Val = Op3Val + Op4Val - 1;
3693
3694           if (NewOp4Val >= RegWidth || NewOp4Val < Op3Val)
3695             return Error(Op4.getStartLoc(),
3696                          "requested extract overflows register");
3697
3698           const MCExpr *NewOp4 =
3699               MCConstantExpr::Create(NewOp4Val, getContext());
3700           Operands[4] = AArch64Operand::CreateImm(
3701               NewOp4, Op4.getStartLoc(), Op4.getEndLoc(), getContext());
3702           if (Tok == "bfxil")
3703             Operands[0] = AArch64Operand::CreateToken(
3704                 "bfm", false, Op.getStartLoc(), getContext());
3705           else if (Tok == "sbfx")
3706             Operands[0] = AArch64Operand::CreateToken(
3707                 "sbfm", false, Op.getStartLoc(), getContext());
3708           else if (Tok == "ubfx")
3709             Operands[0] = AArch64Operand::CreateToken(
3710                 "ubfm", false, Op.getStartLoc(), getContext());
3711           else
3712             llvm_unreachable("No valid mnemonic for alias?");
3713         }
3714       }
3715     }
3716   }
3717   // FIXME: Horrible hack for sxtw and uxtw with Wn src and Xd dst operands.
3718   //        InstAlias can't quite handle this since the reg classes aren't
3719   //        subclasses.
3720   if (NumOperands == 3 && (Tok == "sxtw" || Tok == "uxtw")) {
3721     // The source register can be Wn here, but the matcher expects a
3722     // GPR64. Twiddle it here if necessary.
3723     AArch64Operand &Op = static_cast<AArch64Operand &>(*Operands[2]);
3724     if (Op.isReg()) {
3725       unsigned Reg = getXRegFromWReg(Op.getReg());
3726       Operands[2] = AArch64Operand::CreateReg(Reg, false, Op.getStartLoc(),
3727                                               Op.getEndLoc(), getContext());
3728     }
3729   }
3730   // FIXME: Likewise for sxt[bh] with a Xd dst operand
3731   else if (NumOperands == 3 && (Tok == "sxtb" || Tok == "sxth")) {
3732     AArch64Operand &Op = static_cast<AArch64Operand &>(*Operands[1]);
3733     if (Op.isReg() &&
3734         AArch64MCRegisterClasses[AArch64::GPR64allRegClassID].contains(
3735             Op.getReg())) {
3736       // The source register can be Wn here, but the matcher expects a
3737       // GPR64. Twiddle it here if necessary.
3738       AArch64Operand &Op = static_cast<AArch64Operand &>(*Operands[2]);
3739       if (Op.isReg()) {
3740         unsigned Reg = getXRegFromWReg(Op.getReg());
3741         Operands[2] = AArch64Operand::CreateReg(Reg, false, Op.getStartLoc(),
3742                                                 Op.getEndLoc(), getContext());
3743       }
3744     }
3745   }
3746   // FIXME: Likewise for uxt[bh] with a Xd dst operand
3747   else if (NumOperands == 3 && (Tok == "uxtb" || Tok == "uxth")) {
3748     AArch64Operand &Op = static_cast<AArch64Operand &>(*Operands[1]);
3749     if (Op.isReg() &&
3750         AArch64MCRegisterClasses[AArch64::GPR64allRegClassID].contains(
3751             Op.getReg())) {
3752       // The source register can be Wn here, but the matcher expects a
3753       // GPR32. Twiddle it here if necessary.
3754       AArch64Operand &Op = static_cast<AArch64Operand &>(*Operands[1]);
3755       if (Op.isReg()) {
3756         unsigned Reg = getWRegFromXReg(Op.getReg());
3757         Operands[1] = AArch64Operand::CreateReg(Reg, false, Op.getStartLoc(),
3758                                                 Op.getEndLoc(), getContext());
3759       }
3760     }
3761   }
3762
3763   // Yet another horrible hack to handle FMOV Rd, #0.0 using [WX]ZR.
3764   if (NumOperands == 3 && Tok == "fmov") {
3765     AArch64Operand &RegOp = static_cast<AArch64Operand &>(*Operands[1]);
3766     AArch64Operand &ImmOp = static_cast<AArch64Operand &>(*Operands[2]);
3767     if (RegOp.isReg() && ImmOp.isFPImm() && ImmOp.getFPImm() == (unsigned)-1) {
3768       unsigned zreg =
3769           AArch64MCRegisterClasses[AArch64::FPR32RegClassID].contains(
3770               RegOp.getReg())
3771               ? AArch64::WZR
3772               : AArch64::XZR;
3773       Operands[2] = AArch64Operand::CreateReg(zreg, false, Op.getStartLoc(),
3774                                               Op.getEndLoc(), getContext());
3775     }
3776   }
3777
3778   MCInst Inst;
3779   // First try to match against the secondary set of tables containing the
3780   // short-form NEON instructions (e.g. "fadd.2s v0, v1, v2").
3781   unsigned MatchResult =
3782       MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm, 1);
3783
3784   // If that fails, try against the alternate table containing long-form NEON:
3785   // "fadd v0.2s, v1.2s, v2.2s"
3786   if (MatchResult != Match_Success)
3787     MatchResult =
3788         MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm, 0);
3789
3790   switch (MatchResult) {
3791   case Match_Success: {
3792     // Perform range checking and other semantic validations
3793     SmallVector<SMLoc, 8> OperandLocs;
3794     NumOperands = Operands.size();
3795     for (unsigned i = 1; i < NumOperands; ++i)
3796       OperandLocs.push_back(Operands[i]->getStartLoc());
3797     if (validateInstruction(Inst, OperandLocs))
3798       return true;
3799
3800     Inst.setLoc(IDLoc);
3801     Out.EmitInstruction(Inst, STI);
3802     return false;
3803   }
3804   case Match_MissingFeature: {
3805     assert(ErrorInfo && "Unknown missing feature!");
3806     // Special case the error message for the very common case where only
3807     // a single subtarget feature is missing (neon, e.g.).
3808     std::string Msg = "instruction requires:";
3809     unsigned Mask = 1;
3810     for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
3811       if (ErrorInfo & Mask) {
3812         Msg += " ";
3813         Msg += getSubtargetFeatureName(ErrorInfo & Mask);
3814       }
3815       Mask <<= 1;
3816     }
3817     return Error(IDLoc, Msg);
3818   }
3819   case Match_MnemonicFail:
3820     return showMatchError(IDLoc, MatchResult);
3821   case Match_InvalidOperand: {
3822     SMLoc ErrorLoc = IDLoc;
3823     if (ErrorInfo != ~0U) {
3824       if (ErrorInfo >= Operands.size())
3825         return Error(IDLoc, "too few operands for instruction");
3826
3827       ErrorLoc = ((AArch64Operand &)*Operands[ErrorInfo]).getStartLoc();
3828       if (ErrorLoc == SMLoc())
3829         ErrorLoc = IDLoc;
3830     }
3831     // If the match failed on a suffix token operand, tweak the diagnostic
3832     // accordingly.
3833     if (((AArch64Operand &)*Operands[ErrorInfo]).isToken() &&
3834         ((AArch64Operand &)*Operands[ErrorInfo]).isTokenSuffix())
3835       MatchResult = Match_InvalidSuffix;
3836
3837     return showMatchError(ErrorLoc, MatchResult);
3838   }
3839   case Match_InvalidMemoryIndexed1:
3840   case Match_InvalidMemoryIndexed2:
3841   case Match_InvalidMemoryIndexed4:
3842   case Match_InvalidMemoryIndexed8:
3843   case Match_InvalidMemoryIndexed16:
3844   case Match_InvalidCondCode:
3845   case Match_AddSubRegExtendSmall:
3846   case Match_AddSubRegExtendLarge:
3847   case Match_AddSubSecondSource:
3848   case Match_LogicalSecondSource:
3849   case Match_AddSubRegShift32:
3850   case Match_AddSubRegShift64:
3851   case Match_InvalidMovImm32Shift:
3852   case Match_InvalidMovImm64Shift:
3853   case Match_InvalidFPImm:
3854   case Match_InvalidMemoryWExtend8:
3855   case Match_InvalidMemoryWExtend16:
3856   case Match_InvalidMemoryWExtend32:
3857   case Match_InvalidMemoryWExtend64:
3858   case Match_InvalidMemoryWExtend128:
3859   case Match_InvalidMemoryXExtend8:
3860   case Match_InvalidMemoryXExtend16:
3861   case Match_InvalidMemoryXExtend32:
3862   case Match_InvalidMemoryXExtend64:
3863   case Match_InvalidMemoryXExtend128:
3864   case Match_InvalidMemoryIndexed4SImm7:
3865   case Match_InvalidMemoryIndexed8SImm7:
3866   case Match_InvalidMemoryIndexed16SImm7:
3867   case Match_InvalidMemoryIndexedSImm9:
3868   case Match_InvalidImm0_7:
3869   case Match_InvalidImm0_15:
3870   case Match_InvalidImm0_31:
3871   case Match_InvalidImm0_63:
3872   case Match_InvalidImm0_127:
3873   case Match_InvalidImm0_65535:
3874   case Match_InvalidImm1_8:
3875   case Match_InvalidImm1_16:
3876   case Match_InvalidImm1_32:
3877   case Match_InvalidImm1_64:
3878   case Match_InvalidIndex1:
3879   case Match_InvalidIndexB:
3880   case Match_InvalidIndexH:
3881   case Match_InvalidIndexS:
3882   case Match_InvalidIndexD:
3883   case Match_InvalidLabel:
3884   case Match_MSR:
3885   case Match_MRS: {
3886     if (ErrorInfo >= Operands.size())
3887       return Error(IDLoc, "too few operands for instruction");
3888     // Any time we get here, there's nothing fancy to do. Just get the
3889     // operand SMLoc and display the diagnostic.
3890     SMLoc ErrorLoc = ((AArch64Operand &)*Operands[ErrorInfo]).getStartLoc();
3891     if (ErrorLoc == SMLoc())
3892       ErrorLoc = IDLoc;
3893     return showMatchError(ErrorLoc, MatchResult);
3894   }
3895   }
3896
3897   llvm_unreachable("Implement any new match types added!");
3898   return true;
3899 }
3900
3901 /// ParseDirective parses the arm specific directives
3902 bool AArch64AsmParser::ParseDirective(AsmToken DirectiveID) {
3903   StringRef IDVal = DirectiveID.getIdentifier();
3904   SMLoc Loc = DirectiveID.getLoc();
3905   if (IDVal == ".hword")
3906     return parseDirectiveWord(2, Loc);
3907   if (IDVal == ".word")
3908     return parseDirectiveWord(4, Loc);
3909   if (IDVal == ".xword")
3910     return parseDirectiveWord(8, Loc);
3911   if (IDVal == ".tlsdesccall")
3912     return parseDirectiveTLSDescCall(Loc);
3913   if (IDVal == ".ltorg" || IDVal == ".pool")
3914     return parseDirectiveLtorg(Loc);
3915   if (IDVal == ".unreq")
3916     return parseDirectiveUnreq(DirectiveID.getLoc());
3917
3918   return parseDirectiveLOH(IDVal, Loc);
3919 }
3920
3921 /// parseDirectiveWord
3922 ///  ::= .word [ expression (, expression)* ]
3923 bool AArch64AsmParser::parseDirectiveWord(unsigned Size, SMLoc L) {
3924   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3925     for (;;) {
3926       const MCExpr *Value;
3927       if (getParser().parseExpression(Value))
3928         return true;
3929
3930       getParser().getStreamer().EmitValue(Value, Size);
3931
3932       if (getLexer().is(AsmToken::EndOfStatement))
3933         break;
3934
3935       // FIXME: Improve diagnostic.
3936       if (getLexer().isNot(AsmToken::Comma))
3937         return Error(L, "unexpected token in directive");
3938       Parser.Lex();
3939     }
3940   }
3941
3942   Parser.Lex();
3943   return false;
3944 }
3945
3946 // parseDirectiveTLSDescCall:
3947 //   ::= .tlsdesccall symbol
3948 bool AArch64AsmParser::parseDirectiveTLSDescCall(SMLoc L) {
3949   StringRef Name;
3950   if (getParser().parseIdentifier(Name))
3951     return Error(L, "expected symbol after directive");
3952
3953   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3954   const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, getContext());
3955   Expr = AArch64MCExpr::Create(Expr, AArch64MCExpr::VK_TLSDESC, getContext());
3956
3957   MCInst Inst;
3958   Inst.setOpcode(AArch64::TLSDESCCALL);
3959   Inst.addOperand(MCOperand::CreateExpr(Expr));
3960
3961   getParser().getStreamer().EmitInstruction(Inst, STI);
3962   return false;
3963 }
3964
3965 /// ::= .loh <lohName | lohId> label1, ..., labelN
3966 /// The number of arguments depends on the loh identifier.
3967 bool AArch64AsmParser::parseDirectiveLOH(StringRef IDVal, SMLoc Loc) {
3968   if (IDVal != MCLOHDirectiveName())
3969     return true;
3970   MCLOHType Kind;
3971   if (getParser().getTok().isNot(AsmToken::Identifier)) {
3972     if (getParser().getTok().isNot(AsmToken::Integer))
3973       return TokError("expected an identifier or a number in directive");
3974     // We successfully get a numeric value for the identifier.
3975     // Check if it is valid.
3976     int64_t Id = getParser().getTok().getIntVal();
3977     Kind = (MCLOHType)Id;
3978     // Check that Id does not overflow MCLOHType.
3979     if (!isValidMCLOHType(Kind) || Id != Kind)
3980       return TokError("invalid numeric identifier in directive");
3981   } else {
3982     StringRef Name = getTok().getIdentifier();
3983     // We successfully parse an identifier.
3984     // Check if it is a recognized one.
3985     int Id = MCLOHNameToId(Name);
3986
3987     if (Id == -1)
3988       return TokError("invalid identifier in directive");
3989     Kind = (MCLOHType)Id;
3990   }
3991   // Consume the identifier.
3992   Lex();
3993   // Get the number of arguments of this LOH.
3994   int NbArgs = MCLOHIdToNbArgs(Kind);
3995
3996   assert(NbArgs != -1 && "Invalid number of arguments");
3997
3998   SmallVector<MCSymbol *, 3> Args;
3999   for (int Idx = 0; Idx < NbArgs; ++Idx) {
4000     StringRef Name;
4001     if (getParser().parseIdentifier(Name))
4002       return TokError("expected identifier in directive");
4003     Args.push_back(getContext().GetOrCreateSymbol(Name));
4004
4005     if (Idx + 1 == NbArgs)
4006       break;
4007     if (getLexer().isNot(AsmToken::Comma))
4008       return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
4009     Lex();
4010   }
4011   if (getLexer().isNot(AsmToken::EndOfStatement))
4012     return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
4013
4014   getStreamer().EmitLOHDirective((MCLOHType)Kind, Args);
4015   return false;
4016 }
4017
4018 /// parseDirectiveLtorg
4019 ///  ::= .ltorg | .pool
4020 bool AArch64AsmParser::parseDirectiveLtorg(SMLoc L) {
4021   getTargetStreamer().emitCurrentConstantPool();
4022   return false;
4023 }
4024
4025 /// parseDirectiveReq
4026 ///  ::= name .req registername
4027 bool AArch64AsmParser::parseDirectiveReq(StringRef Name, SMLoc L) {
4028   Parser.Lex(); // Eat the '.req' token.
4029   SMLoc SRegLoc = getLoc();
4030   unsigned RegNum = tryParseRegister();
4031   bool IsVector = false;
4032
4033   if (RegNum == static_cast<unsigned>(-1)) {
4034     StringRef Kind;
4035     RegNum = tryMatchVectorRegister(Kind, false);
4036     if (!Kind.empty()) {
4037       Error(SRegLoc, "vector register without type specifier expected");
4038       return false;
4039     }
4040     IsVector = true;
4041   }
4042
4043   if (RegNum == static_cast<unsigned>(-1)) {
4044     Parser.eatToEndOfStatement();
4045     Error(SRegLoc, "register name or alias expected");
4046     return false;
4047   }
4048
4049   // Shouldn't be anything else.
4050   if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
4051     Error(Parser.getTok().getLoc(), "unexpected input in .req directive");
4052     Parser.eatToEndOfStatement();
4053     return false;
4054   }
4055
4056   Parser.Lex(); // Consume the EndOfStatement
4057
4058   auto pair = std::make_pair(IsVector, RegNum);
4059   if (RegisterReqs.GetOrCreateValue(Name, pair).getValue() != pair)
4060     Warning(L, "ignoring redefinition of register alias '" + Name + "'");
4061
4062   return true;
4063 }
4064
4065 /// parseDirectiveUneq
4066 ///  ::= .unreq registername
4067 bool AArch64AsmParser::parseDirectiveUnreq(SMLoc L) {
4068   if (Parser.getTok().isNot(AsmToken::Identifier)) {
4069     Error(Parser.getTok().getLoc(), "unexpected input in .unreq directive.");
4070     Parser.eatToEndOfStatement();
4071     return false;
4072   }
4073   RegisterReqs.erase(Parser.getTok().getIdentifier().lower());
4074   Parser.Lex(); // Eat the identifier.
4075   return false;
4076 }
4077
4078 bool
4079 AArch64AsmParser::classifySymbolRef(const MCExpr *Expr,
4080                                     AArch64MCExpr::VariantKind &ELFRefKind,
4081                                     MCSymbolRefExpr::VariantKind &DarwinRefKind,
4082                                     int64_t &Addend) {
4083   ELFRefKind = AArch64MCExpr::VK_INVALID;
4084   DarwinRefKind = MCSymbolRefExpr::VK_None;
4085   Addend = 0;
4086
4087   if (const AArch64MCExpr *AE = dyn_cast<AArch64MCExpr>(Expr)) {
4088     ELFRefKind = AE->getKind();
4089     Expr = AE->getSubExpr();
4090   }
4091
4092   const MCSymbolRefExpr *SE = dyn_cast<MCSymbolRefExpr>(Expr);
4093   if (SE) {
4094     // It's a simple symbol reference with no addend.
4095     DarwinRefKind = SE->getKind();
4096     return true;
4097   }
4098
4099   const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr);
4100   if (!BE)
4101     return false;
4102
4103   SE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
4104   if (!SE)
4105     return false;
4106   DarwinRefKind = SE->getKind();
4107
4108   if (BE->getOpcode() != MCBinaryExpr::Add &&
4109       BE->getOpcode() != MCBinaryExpr::Sub)
4110     return false;
4111
4112   // See if the addend is is a constant, otherwise there's more going
4113   // on here than we can deal with.
4114   auto AddendExpr = dyn_cast<MCConstantExpr>(BE->getRHS());
4115   if (!AddendExpr)
4116     return false;
4117
4118   Addend = AddendExpr->getValue();
4119   if (BE->getOpcode() == MCBinaryExpr::Sub)
4120     Addend = -Addend;
4121
4122   // It's some symbol reference + a constant addend, but really
4123   // shouldn't use both Darwin and ELF syntax.
4124   return ELFRefKind == AArch64MCExpr::VK_INVALID ||
4125          DarwinRefKind == MCSymbolRefExpr::VK_None;
4126 }
4127
4128 /// Force static initialization.
4129 extern "C" void LLVMInitializeAArch64AsmParser() {
4130   RegisterMCAsmParser<AArch64AsmParser> X(TheAArch64leTarget);
4131   RegisterMCAsmParser<AArch64AsmParser> Y(TheAArch64beTarget);
4132
4133   RegisterMCAsmParser<AArch64AsmParser> Z(TheARM64leTarget);
4134   RegisterMCAsmParser<AArch64AsmParser> W(TheARM64beTarget);
4135 }
4136
4137 #define GET_REGISTER_MATCHER
4138 #define GET_SUBTARGET_FEATURE_NAME
4139 #define GET_MATCHER_IMPLEMENTATION
4140 #include "AArch64GenAsmMatcher.inc"
4141
4142 // Define this matcher function after the auto-generated include so we
4143 // have the match class enum definitions.
4144 unsigned AArch64AsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
4145                                                       unsigned Kind) {
4146   AArch64Operand &Op = static_cast<AArch64Operand &>(AsmOp);
4147   // If the kind is a token for a literal immediate, check if our asm
4148   // operand matches. This is for InstAliases which have a fixed-value
4149   // immediate in the syntax.
4150   int64_t ExpectedVal;
4151   switch (Kind) {
4152   default:
4153     return Match_InvalidOperand;
4154   case MCK__35_0:
4155     ExpectedVal = 0;
4156     break;
4157   case MCK__35_1:
4158     ExpectedVal = 1;
4159     break;
4160   case MCK__35_12:
4161     ExpectedVal = 12;
4162     break;
4163   case MCK__35_16:
4164     ExpectedVal = 16;
4165     break;
4166   case MCK__35_2:
4167     ExpectedVal = 2;
4168     break;
4169   case MCK__35_24:
4170     ExpectedVal = 24;
4171     break;
4172   case MCK__35_3:
4173     ExpectedVal = 3;
4174     break;
4175   case MCK__35_32:
4176     ExpectedVal = 32;
4177     break;
4178   case MCK__35_4:
4179     ExpectedVal = 4;
4180     break;
4181   case MCK__35_48:
4182     ExpectedVal = 48;
4183     break;
4184   case MCK__35_6:
4185     ExpectedVal = 6;
4186     break;
4187   case MCK__35_64:
4188     ExpectedVal = 64;
4189     break;
4190   case MCK__35_8:
4191     ExpectedVal = 8;
4192     break;
4193   }
4194   if (!Op.isImm())
4195     return Match_InvalidOperand;
4196   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op.getImm());
4197   if (!CE)
4198     return Match_InvalidOperand;
4199   if (CE->getValue() == ExpectedVal)
4200     return Match_Success;
4201   return Match_InvalidOperand;
4202 }