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