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