Add an instruction deprecation feature to TableGen.
[oota-llvm.git] / lib / Target / AArch64 / AsmParser / AArch64AsmParser.cpp
1 //==- AArch64AsmParser.cpp - Parse AArch64 assembly to MCInst instructions -==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the (GNU-style) assembly parser for the AArch64
11 // architecture.
12 //
13 //===----------------------------------------------------------------------===//
14
15
16 #include "MCTargetDesc/AArch64MCTargetDesc.h"
17 #include "MCTargetDesc/AArch64MCExpr.h"
18 #include "Utils/AArch64BaseInfo.h"
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/ADT/APInt.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/MC/MCSubtargetInfo.h"
26 #include "llvm/MC/MCTargetAsmParser.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCRegisterInfo.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCParser/MCAsmLexer.h"
31 #include "llvm/MC/MCParser/MCAsmParser.h"
32 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Support/TargetRegistry.h"
36
37 using namespace llvm;
38
39 namespace {
40
41 class AArch64Operand;
42
43 class AArch64AsmParser : public MCTargetAsmParser {
44   MCSubtargetInfo &STI;
45   MCAsmParser &Parser;
46
47 #define GET_ASSEMBLER_HEADER
48 #include "AArch64GenAsmMatcher.inc"
49
50 public:
51   enum AArch64MatchResultTy {
52     Match_FirstAArch64 = FIRST_TARGET_MATCH_RESULT_TY,
53 #define GET_OPERAND_DIAGNOSTIC_TYPES
54 #include "AArch64GenAsmMatcher.inc"
55   };
56
57   AArch64AsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser,
58                    const MCInstrInfo &MII)
59       : MCTargetAsmParser(), STI(_STI), Parser(_Parser) {
60     MCAsmParserExtension::Initialize(_Parser);
61
62     // Initialize the set of available features.
63     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
64   }
65
66   // These are the public interface of the MCTargetAsmParser
67   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
68   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
69                         SMLoc NameLoc,
70                         SmallVectorImpl<MCParsedAsmOperand*> &Operands);
71
72   bool ParseDirective(AsmToken DirectiveID);
73   bool ParseDirectiveTLSDescCall(SMLoc L);
74   bool ParseDirectiveWord(unsigned Size, SMLoc L);
75
76   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
77                                SmallVectorImpl<MCParsedAsmOperand*> &Operands,
78                                MCStreamer&Out, unsigned &ErrorInfo,
79                                bool MatchingInlineAsm);
80
81   // The rest of the sub-parsers have more freedom over interface: they return
82   // an OperandMatchResultTy because it's less ambiguous than true/false or
83   // -1/0/1 even if it is more verbose
84   OperandMatchResultTy
85   ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
86                StringRef Mnemonic);
87
88   OperandMatchResultTy ParseImmediate(const MCExpr *&ExprVal);
89
90   OperandMatchResultTy ParseRelocPrefix(AArch64MCExpr::VariantKind &RefKind);
91
92   OperandMatchResultTy
93   ParseNEONLane(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
94                 uint32_t NumLanes);
95
96   OperandMatchResultTy
97   ParseRegister(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
98                 uint32_t &NumLanes);
99
100   OperandMatchResultTy
101   ParseImmWithLSLOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
102
103   OperandMatchResultTy
104   ParseCondCodeOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
105
106   OperandMatchResultTy
107   ParseCRxOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
108
109   OperandMatchResultTy
110   ParseFPImmOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
111
112   template<typename SomeNamedImmMapper> OperandMatchResultTy
113   ParseNamedImmOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
114     return ParseNamedImmOperand(SomeNamedImmMapper(), Operands);
115   }
116
117   OperandMatchResultTy
118   ParseNamedImmOperand(const NamedImmMapper &Mapper,
119                        SmallVectorImpl<MCParsedAsmOperand*> &Operands);
120
121   OperandMatchResultTy
122   ParseLSXAddressOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
123
124   OperandMatchResultTy
125   ParseShiftExtend(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
126
127   OperandMatchResultTy
128   ParseSysRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
129
130   bool validateInstruction(MCInst &Inst,
131                           const SmallVectorImpl<MCParsedAsmOperand*> &Operands);
132
133   /// Scan the next token (which had better be an identifier) and determine
134   /// whether it represents a general-purpose or vector register. It returns
135   /// true if an identifier was found and populates its reference arguments. It
136   /// does not consume the token.
137   bool
138   IdentifyRegister(unsigned &RegNum, SMLoc &RegEndLoc, StringRef &LayoutSpec,
139                    SMLoc &LayoutLoc) const;
140
141 };
142
143 }
144
145 namespace {
146
147 /// Instances of this class represent a parsed AArch64 machine instruction.
148 class AArch64Operand : public MCParsedAsmOperand {
149 private:
150   enum KindTy {
151     k_ImmWithLSL,     // #uimm {, LSL #amt }
152     k_CondCode,       // eq/ne/...
153     k_FPImmediate,    // Limited-precision floating-point imm
154     k_Immediate,      // Including expressions referencing symbols
155     k_Register,
156     k_ShiftExtend,
157     k_SysReg,         // The register operand of MRS and MSR instructions
158     k_Token,          // The mnemonic; other raw tokens the auto-generated
159     k_WrappedRegister // Load/store exclusive permit a wrapped register.
160   } Kind;
161
162   SMLoc StartLoc, EndLoc;
163
164   struct ImmWithLSLOp {
165     const MCExpr *Val;
166     unsigned ShiftAmount;
167     bool ImplicitAmount;
168   };
169
170   struct CondCodeOp {
171     A64CC::CondCodes Code;
172   };
173
174   struct FPImmOp {
175     double Val;
176   };
177
178   struct ImmOp {
179     const MCExpr *Val;
180   };
181
182   struct RegOp {
183     unsigned RegNum;
184   };
185
186   struct ShiftExtendOp {
187     A64SE::ShiftExtSpecifiers ShiftType;
188     unsigned Amount;
189     bool ImplicitAmount;
190   };
191
192   struct SysRegOp {
193     const char *Data;
194     unsigned Length;
195   };
196
197   struct TokOp {
198     const char *Data;
199     unsigned Length;
200   };
201
202   union {
203     struct ImmWithLSLOp ImmWithLSL;
204     struct CondCodeOp CondCode;
205     struct FPImmOp FPImm;
206     struct ImmOp Imm;
207     struct RegOp Reg;
208     struct ShiftExtendOp ShiftExtend;
209     struct SysRegOp SysReg;
210     struct TokOp Tok;
211   };
212
213   AArch64Operand(KindTy K, SMLoc S, SMLoc E)
214     : MCParsedAsmOperand(), Kind(K), StartLoc(S), EndLoc(E) {}
215
216 public:
217   AArch64Operand(const AArch64Operand &o) : MCParsedAsmOperand() {
218   }
219
220   SMLoc getStartLoc() const { return StartLoc; }
221   SMLoc getEndLoc() const { return EndLoc; }
222   void print(raw_ostream&) const;
223   void dump() const;
224
225   StringRef getToken() const {
226     assert(Kind == k_Token && "Invalid access!");
227     return StringRef(Tok.Data, Tok.Length);
228   }
229
230   unsigned getReg() const {
231     assert((Kind == k_Register || Kind == k_WrappedRegister)
232            && "Invalid access!");
233     return Reg.RegNum;
234   }
235
236   const MCExpr *getImm() const {
237     assert(Kind == k_Immediate && "Invalid access!");
238     return Imm.Val;
239   }
240
241   A64CC::CondCodes getCondCode() const {
242     assert(Kind == k_CondCode && "Invalid access!");
243     return CondCode.Code;
244   }
245
246   static bool isNonConstantExpr(const MCExpr *E,
247                                 AArch64MCExpr::VariantKind &Variant) {
248     if (const AArch64MCExpr *A64E = dyn_cast<AArch64MCExpr>(E)) {
249       Variant = A64E->getKind();
250       return true;
251     } else if (!isa<MCConstantExpr>(E)) {
252       Variant = AArch64MCExpr::VK_AARCH64_None;
253       return true;
254     }
255
256     return false;
257   }
258
259   bool isCondCode() const { return Kind == k_CondCode; }
260   bool isToken() const { return Kind == k_Token; }
261   bool isReg() const { return Kind == k_Register; }
262   bool isImm() const { return Kind == k_Immediate; }
263   bool isMem() const { return false; }
264   bool isFPImm() const { return Kind == k_FPImmediate; }
265   bool isShiftOrExtend() const { return Kind == k_ShiftExtend; }
266   bool isSysReg() const { return Kind == k_SysReg; }
267   bool isImmWithLSL() const { return Kind == k_ImmWithLSL; }
268   bool isWrappedReg() const { return Kind == k_WrappedRegister; }
269
270   bool isAddSubImmLSL0() const {
271     if (!isImmWithLSL()) return false;
272     if (ImmWithLSL.ShiftAmount != 0) return false;
273
274     AArch64MCExpr::VariantKind Variant;
275     if (isNonConstantExpr(ImmWithLSL.Val, Variant)) {
276       return Variant == AArch64MCExpr::VK_AARCH64_LO12
277           || Variant == AArch64MCExpr::VK_AARCH64_DTPREL_LO12
278           || Variant == AArch64MCExpr::VK_AARCH64_DTPREL_LO12_NC
279           || Variant == AArch64MCExpr::VK_AARCH64_TPREL_LO12
280           || Variant == AArch64MCExpr::VK_AARCH64_TPREL_LO12_NC
281           || Variant == AArch64MCExpr::VK_AARCH64_TLSDESC_LO12;
282     }
283
284     // Otherwise it should be a real immediate in range:
285     const MCConstantExpr *CE = cast<MCConstantExpr>(ImmWithLSL.Val);
286     return CE->getValue() >= 0 && CE->getValue() <= 0xfff;
287   }
288
289   bool isAddSubImmLSL12() const {
290     if (!isImmWithLSL()) return false;
291     if (ImmWithLSL.ShiftAmount != 12) return false;
292
293     AArch64MCExpr::VariantKind Variant;
294     if (isNonConstantExpr(ImmWithLSL.Val, Variant)) {
295       return Variant == AArch64MCExpr::VK_AARCH64_DTPREL_HI12
296           || Variant == AArch64MCExpr::VK_AARCH64_TPREL_HI12;
297     }
298
299     // Otherwise it should be a real immediate in range:
300     const MCConstantExpr *CE = cast<MCConstantExpr>(ImmWithLSL.Val);
301     return CE->getValue() >= 0 && CE->getValue() <= 0xfff;
302   }
303
304   template<unsigned MemSize, unsigned RmSize> bool isAddrRegExtend() const {
305     if (!isShiftOrExtend()) return false;
306
307     A64SE::ShiftExtSpecifiers Ext = ShiftExtend.ShiftType;
308     if (RmSize == 32 && !(Ext == A64SE::UXTW || Ext == A64SE::SXTW))
309       return false;
310
311     if (RmSize == 64 && !(Ext == A64SE::LSL || Ext == A64SE::SXTX))
312       return false;
313
314     return ShiftExtend.Amount == Log2_32(MemSize) || ShiftExtend.Amount == 0;
315   }
316
317   bool isAdrpLabel() const {
318     if (!isImm()) return false;
319
320     AArch64MCExpr::VariantKind Variant;
321     if (isNonConstantExpr(getImm(), Variant)) {
322       return Variant == AArch64MCExpr::VK_AARCH64_None
323         || Variant == AArch64MCExpr::VK_AARCH64_GOT
324         || Variant == AArch64MCExpr::VK_AARCH64_GOTTPREL
325         || Variant == AArch64MCExpr::VK_AARCH64_TLSDESC;
326     }
327
328     return isLabel<21, 4096>();
329   }
330
331   template<unsigned RegWidth>  bool isBitfieldWidth() const {
332     if (!isImm()) return false;
333
334     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
335     if (!CE) return false;
336
337     return CE->getValue() >= 1 && CE->getValue() <= RegWidth;
338   }
339
340   template<int RegWidth>
341   bool isCVTFixedPos() const {
342     if (!isImm()) return false;
343
344     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
345     if (!CE) return false;
346
347     return CE->getValue() >= 1 && CE->getValue() <= RegWidth;
348   }
349
350   bool isFMOVImm() const {
351     if (!isFPImm()) return false;
352
353     APFloat RealVal(FPImm.Val);
354     uint32_t ImmVal;
355     return A64Imms::isFPImm(RealVal, ImmVal);
356   }
357
358   bool isFPZero() const {
359     if (!isFPImm()) return false;
360
361     APFloat RealVal(FPImm.Val);
362     return RealVal.isPosZero();
363   }
364
365   template<unsigned field_width, unsigned scale>
366   bool isLabel() const {
367     if (!isImm()) return false;
368
369     if (dyn_cast<MCSymbolRefExpr>(Imm.Val)) {
370       return true;
371     } else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
372       int64_t Val = CE->getValue();
373       int64_t Min = - (scale * (1LL << (field_width - 1)));
374       int64_t Max = scale * ((1LL << (field_width - 1)) - 1);
375       return (Val % scale) == 0 && Val >= Min && Val <= Max;
376     }
377
378     // N.b. this disallows explicit relocation specifications via an
379     // AArch64MCExpr. Users needing that behaviour
380     return false;
381   }
382
383   bool isLane1() const {
384     if (!isImm()) return false;
385
386     // Because it's come through custom assembly parsing, it must always be a
387     // constant expression.
388     return cast<MCConstantExpr>(getImm())->getValue() == 1;
389   }
390
391   bool isLoadLitLabel() const {
392     if (!isImm()) return false;
393
394     AArch64MCExpr::VariantKind Variant;
395     if (isNonConstantExpr(getImm(), Variant)) {
396       return Variant == AArch64MCExpr::VK_AARCH64_None
397           || Variant == AArch64MCExpr::VK_AARCH64_GOTTPREL;
398     }
399
400     return isLabel<19, 4>();
401   }
402
403   template<unsigned RegWidth> bool isLogicalImm() const {
404     if (!isImm()) return false;
405
406     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val);
407     if (!CE) return false;
408
409     uint32_t Bits;
410     return A64Imms::isLogicalImm(RegWidth, CE->getValue(), Bits);
411   }
412
413   template<unsigned RegWidth> bool isLogicalImmMOV() const {
414     if (!isLogicalImm<RegWidth>()) return false;
415
416     const MCConstantExpr *CE = cast<MCConstantExpr>(Imm.Val);
417
418     // The move alias for ORR is only valid if the immediate cannot be
419     // represented with a move (immediate) instruction; they take priority.
420     int UImm16, Shift;
421     return !A64Imms::isMOVZImm(RegWidth, CE->getValue(), UImm16, Shift)
422       && !A64Imms::isMOVNImm(RegWidth, CE->getValue(), UImm16, Shift);
423   }
424
425   template<int MemSize>
426   bool isOffsetUImm12() const {
427     if (!isImm()) return false;
428
429     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
430
431     // Assume they know what they're doing for now if they've given us a
432     // non-constant expression. In principle we could check for ridiculous
433     // things that can't possibly work or relocations that would almost
434     // certainly break resulting code.
435     if (!CE)
436       return true;
437
438     int64_t Val = CE->getValue();
439
440     // Must be a multiple of the access size in bytes.
441     if ((Val & (MemSize - 1)) != 0) return false;
442
443     // Must be 12-bit unsigned
444     return Val >= 0 && Val <= 0xfff * MemSize;
445   }
446
447   template<A64SE::ShiftExtSpecifiers SHKind, bool is64Bit>
448   bool isShift() const {
449     if (!isShiftOrExtend()) return false;
450
451     if (ShiftExtend.ShiftType != SHKind)
452       return false;
453
454     return is64Bit ? ShiftExtend.Amount <= 63 : ShiftExtend.Amount <= 31;
455   }
456
457   bool isMOVN32Imm() const {
458     static const AArch64MCExpr::VariantKind PermittedModifiers[] = {
459       AArch64MCExpr::VK_AARCH64_SABS_G0,
460       AArch64MCExpr::VK_AARCH64_SABS_G1,
461       AArch64MCExpr::VK_AARCH64_DTPREL_G1,
462       AArch64MCExpr::VK_AARCH64_DTPREL_G0,
463       AArch64MCExpr::VK_AARCH64_GOTTPREL_G1,
464       AArch64MCExpr::VK_AARCH64_TPREL_G1,
465       AArch64MCExpr::VK_AARCH64_TPREL_G0,
466     };
467     const unsigned NumModifiers = llvm::array_lengthof(PermittedModifiers);
468
469     return isMoveWideImm(32, PermittedModifiers, NumModifiers);
470   }
471
472   bool isMOVN64Imm() const {
473     static const AArch64MCExpr::VariantKind PermittedModifiers[] = {
474       AArch64MCExpr::VK_AARCH64_SABS_G0,
475       AArch64MCExpr::VK_AARCH64_SABS_G1,
476       AArch64MCExpr::VK_AARCH64_SABS_G2,
477       AArch64MCExpr::VK_AARCH64_DTPREL_G2,
478       AArch64MCExpr::VK_AARCH64_DTPREL_G1,
479       AArch64MCExpr::VK_AARCH64_DTPREL_G0,
480       AArch64MCExpr::VK_AARCH64_GOTTPREL_G1,
481       AArch64MCExpr::VK_AARCH64_TPREL_G2,
482       AArch64MCExpr::VK_AARCH64_TPREL_G1,
483       AArch64MCExpr::VK_AARCH64_TPREL_G0,
484     };
485     const unsigned NumModifiers = llvm::array_lengthof(PermittedModifiers);
486
487     return isMoveWideImm(64, PermittedModifiers, NumModifiers);
488   }
489
490
491   bool isMOVZ32Imm() const {
492     static const AArch64MCExpr::VariantKind PermittedModifiers[] = {
493       AArch64MCExpr::VK_AARCH64_ABS_G0,
494       AArch64MCExpr::VK_AARCH64_ABS_G1,
495       AArch64MCExpr::VK_AARCH64_SABS_G0,
496       AArch64MCExpr::VK_AARCH64_SABS_G1,
497       AArch64MCExpr::VK_AARCH64_DTPREL_G1,
498       AArch64MCExpr::VK_AARCH64_DTPREL_G0,
499       AArch64MCExpr::VK_AARCH64_GOTTPREL_G1,
500       AArch64MCExpr::VK_AARCH64_TPREL_G1,
501       AArch64MCExpr::VK_AARCH64_TPREL_G0,
502     };
503     const unsigned NumModifiers = llvm::array_lengthof(PermittedModifiers);
504
505     return isMoveWideImm(32, PermittedModifiers, NumModifiers);
506   }
507
508   bool isMOVZ64Imm() const {
509     static const AArch64MCExpr::VariantKind PermittedModifiers[] = {
510       AArch64MCExpr::VK_AARCH64_ABS_G0,
511       AArch64MCExpr::VK_AARCH64_ABS_G1,
512       AArch64MCExpr::VK_AARCH64_ABS_G2,
513       AArch64MCExpr::VK_AARCH64_ABS_G3,
514       AArch64MCExpr::VK_AARCH64_SABS_G0,
515       AArch64MCExpr::VK_AARCH64_SABS_G1,
516       AArch64MCExpr::VK_AARCH64_SABS_G2,
517       AArch64MCExpr::VK_AARCH64_DTPREL_G2,
518       AArch64MCExpr::VK_AARCH64_DTPREL_G1,
519       AArch64MCExpr::VK_AARCH64_DTPREL_G0,
520       AArch64MCExpr::VK_AARCH64_GOTTPREL_G1,
521       AArch64MCExpr::VK_AARCH64_TPREL_G2,
522       AArch64MCExpr::VK_AARCH64_TPREL_G1,
523       AArch64MCExpr::VK_AARCH64_TPREL_G0,
524     };
525     const unsigned NumModifiers = llvm::array_lengthof(PermittedModifiers);
526
527     return isMoveWideImm(64, PermittedModifiers, NumModifiers);
528   }
529
530   bool isMOVK32Imm() const {
531     static const AArch64MCExpr::VariantKind PermittedModifiers[] = {
532       AArch64MCExpr::VK_AARCH64_ABS_G0_NC,
533       AArch64MCExpr::VK_AARCH64_ABS_G1_NC,
534       AArch64MCExpr::VK_AARCH64_DTPREL_G1_NC,
535       AArch64MCExpr::VK_AARCH64_DTPREL_G0_NC,
536       AArch64MCExpr::VK_AARCH64_GOTTPREL_G0_NC,
537       AArch64MCExpr::VK_AARCH64_TPREL_G1_NC,
538       AArch64MCExpr::VK_AARCH64_TPREL_G0_NC,
539     };
540     const unsigned NumModifiers = llvm::array_lengthof(PermittedModifiers);
541
542     return isMoveWideImm(32, PermittedModifiers, NumModifiers);
543   }
544
545   bool isMOVK64Imm() const {
546     static const AArch64MCExpr::VariantKind PermittedModifiers[] = {
547       AArch64MCExpr::VK_AARCH64_ABS_G0_NC,
548       AArch64MCExpr::VK_AARCH64_ABS_G1_NC,
549       AArch64MCExpr::VK_AARCH64_ABS_G2_NC,
550       AArch64MCExpr::VK_AARCH64_ABS_G3,
551       AArch64MCExpr::VK_AARCH64_DTPREL_G1_NC,
552       AArch64MCExpr::VK_AARCH64_DTPREL_G0_NC,
553       AArch64MCExpr::VK_AARCH64_GOTTPREL_G0_NC,
554       AArch64MCExpr::VK_AARCH64_TPREL_G1_NC,
555       AArch64MCExpr::VK_AARCH64_TPREL_G0_NC,
556     };
557     const unsigned NumModifiers = llvm::array_lengthof(PermittedModifiers);
558
559     return isMoveWideImm(64, PermittedModifiers, NumModifiers);
560   }
561
562   bool isMoveWideImm(unsigned RegWidth,
563                      const AArch64MCExpr::VariantKind *PermittedModifiers,
564                      unsigned NumModifiers) const {
565     if (!isImmWithLSL()) return false;
566
567     if (ImmWithLSL.ShiftAmount % 16 != 0) return false;
568     if (ImmWithLSL.ShiftAmount >= RegWidth) return false;
569
570     AArch64MCExpr::VariantKind Modifier;
571     if (isNonConstantExpr(ImmWithLSL.Val, Modifier)) {
572       // E.g. "#:abs_g0:sym, lsl #16" makes no sense.
573       if (!ImmWithLSL.ImplicitAmount) return false;
574
575       for (unsigned i = 0; i < NumModifiers; ++i)
576         if (PermittedModifiers[i] == Modifier) return true;
577
578       return false;
579     }
580
581     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmWithLSL.Val);
582     return CE && CE->getValue() >= 0  && CE->getValue() <= 0xffff;
583   }
584
585   template<int RegWidth, bool (*isValidImm)(int, uint64_t, int&, int&)>
586   bool isMoveWideMovAlias() const {
587     if (!isImm()) return false;
588
589     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
590     if (!CE) return false;
591
592     int UImm16, Shift;
593     uint64_t Value = CE->getValue();
594
595     // If this is a 32-bit instruction then all bits above 32 should be the
596     // same: either of these is fine because signed/unsigned values should be
597     // permitted.
598     if (RegWidth == 32) {
599       if ((Value >> 32) != 0 && (Value >> 32) != 0xffffffff)
600         return false;
601
602       Value &= 0xffffffffULL;
603     }
604
605     return isValidImm(RegWidth, Value, UImm16, Shift);
606   }
607
608   bool isMSRWithReg() const {
609     if (!isSysReg()) return false;
610
611     bool IsKnownRegister;
612     StringRef Name(SysReg.Data, SysReg.Length);
613     A64SysReg::MSRMapper().fromString(Name, IsKnownRegister);
614
615     return IsKnownRegister;
616   }
617
618   bool isMSRPState() const {
619     if (!isSysReg()) return false;
620
621     bool IsKnownRegister;
622     StringRef Name(SysReg.Data, SysReg.Length);
623     A64PState::PStateMapper().fromString(Name, IsKnownRegister);
624
625     return IsKnownRegister;
626   }
627
628   bool isMRS() const {
629     if (!isSysReg()) return false;
630
631     // First check against specific MSR-only (write-only) registers
632     bool IsKnownRegister;
633     StringRef Name(SysReg.Data, SysReg.Length);
634     A64SysReg::MRSMapper().fromString(Name, IsKnownRegister);
635
636     return IsKnownRegister;
637   }
638
639   bool isPRFM() const {
640     if (!isImm()) return false;
641
642     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
643
644     if (!CE)
645       return false;
646
647     return CE->getValue() >= 0 && CE->getValue() <= 31;
648   }
649
650   template<A64SE::ShiftExtSpecifiers SHKind> bool isRegExtend() const {
651     if (!isShiftOrExtend()) return false;
652
653     if (ShiftExtend.ShiftType != SHKind)
654       return false;
655
656     return ShiftExtend.Amount <= 4;
657   }
658
659   bool isRegExtendLSL() const {
660     if (!isShiftOrExtend()) return false;
661
662     if (ShiftExtend.ShiftType != A64SE::LSL)
663       return false;
664
665     return !ShiftExtend.ImplicitAmount && ShiftExtend.Amount <= 4;
666   }
667
668   // if 0 < value <= w, return true
669   bool isShrFixedWidth(int w) const {
670     if (!isImm())
671       return false;
672     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
673     if (!CE)
674       return false;
675     int64_t Value = CE->getValue();
676     return Value > 0 && Value <= w;
677   }
678
679   bool isShrImm8() const { return isShrFixedWidth(8); }
680
681   bool isShrImm16() const { return isShrFixedWidth(16); }
682
683   bool isShrImm32() const { return isShrFixedWidth(32); }
684
685   bool isShrImm64() const { return isShrFixedWidth(64); }
686
687   bool isNeonMovImmShiftLSL() const {
688     if (!isShiftOrExtend())
689       return false;
690
691     if (ShiftExtend.ShiftType != A64SE::LSL)
692       return false;
693
694     // Valid shift amount is 0, 8, 16 and 24.
695     return ShiftExtend.Amount % 8 == 0 && ShiftExtend.Amount <= 24;
696   }
697
698   bool isNeonMovImmShiftLSLH() const {
699     if (!isShiftOrExtend())
700       return false;
701
702     if (ShiftExtend.ShiftType != A64SE::LSL)
703       return false;
704
705     // Valid shift amount is 0 and 8.
706     return ShiftExtend.Amount == 0 || ShiftExtend.Amount == 8;
707   }
708
709   bool isNeonMovImmShiftMSL() const {
710     if (!isShiftOrExtend())
711       return false;
712
713     if (ShiftExtend.ShiftType != A64SE::MSL)
714       return false;
715
716     // Valid shift amount is 8 and 16.
717     return ShiftExtend.Amount == 8 || ShiftExtend.Amount == 16;
718   }
719
720   template <int MemSize> bool isSImm7Scaled() const {
721     if (!isImm())
722       return false;
723
724     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
725     if (!CE) return false;
726
727     int64_t Val = CE->getValue();
728     if (Val % MemSize != 0) return false;
729
730     Val /= MemSize;
731
732     return Val >= -64 && Val < 64;
733   }
734
735   template<int BitWidth>
736   bool isSImm() const {
737     if (!isImm()) return false;
738
739     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
740     if (!CE) return false;
741
742     return CE->getValue() >= -(1LL << (BitWidth - 1))
743       && CE->getValue() < (1LL << (BitWidth - 1));
744   }
745
746   template<int bitWidth>
747   bool isUImm() const {
748     if (!isImm()) return false;
749
750     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
751     if (!CE) return false;
752
753     return CE->getValue() >= 0 && CE->getValue() < (1LL << bitWidth);
754   }
755
756   bool isUImm() const {
757     if (!isImm()) return false;
758
759     return isa<MCConstantExpr>(getImm());
760   }
761
762   bool isNeonUImm64Mask() const {
763     if (!isImm())
764       return false;
765
766     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
767     if (!CE)
768       return false;
769
770     uint64_t Value = CE->getValue();
771
772     // i64 value with each byte being either 0x00 or 0xff.
773     for (unsigned i = 0; i < 8; ++i, Value >>= 8)
774       if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff)
775         return false;
776     return true;
777   }
778
779   static AArch64Operand *CreateImmWithLSL(const MCExpr *Val,
780                                           unsigned ShiftAmount,
781                                           bool ImplicitAmount,
782                                                                                   SMLoc S,SMLoc E) {
783     AArch64Operand *Op = new AArch64Operand(k_ImmWithLSL, S, E);
784     Op->ImmWithLSL.Val = Val;
785     Op->ImmWithLSL.ShiftAmount = ShiftAmount;
786     Op->ImmWithLSL.ImplicitAmount = ImplicitAmount;
787     return Op;
788   }
789
790   static AArch64Operand *CreateCondCode(A64CC::CondCodes Code,
791                                         SMLoc S, SMLoc E) {
792     AArch64Operand *Op = new AArch64Operand(k_CondCode, S, E);
793     Op->CondCode.Code = Code;
794     return Op;
795   }
796
797   static AArch64Operand *CreateFPImm(double Val,
798                                      SMLoc S, SMLoc E) {
799     AArch64Operand *Op = new AArch64Operand(k_FPImmediate, S, E);
800     Op->FPImm.Val = Val;
801     return Op;
802   }
803
804   static AArch64Operand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
805     AArch64Operand *Op = new AArch64Operand(k_Immediate, S, E);
806     Op->Imm.Val = Val;
807     return Op;
808   }
809
810   static AArch64Operand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
811     AArch64Operand *Op = new AArch64Operand(k_Register, S, E);
812     Op->Reg.RegNum = RegNum;
813     return Op;
814   }
815
816   static AArch64Operand *CreateWrappedReg(unsigned RegNum, SMLoc S, SMLoc E) {
817     AArch64Operand *Op = new AArch64Operand(k_WrappedRegister, S, E);
818     Op->Reg.RegNum = RegNum;
819     return Op;
820   }
821
822   static AArch64Operand *CreateShiftExtend(A64SE::ShiftExtSpecifiers ShiftTyp,
823                                            unsigned Amount,
824                                            bool ImplicitAmount,
825                                            SMLoc S, SMLoc E) {
826     AArch64Operand *Op = new AArch64Operand(k_ShiftExtend, S, E);
827     Op->ShiftExtend.ShiftType = ShiftTyp;
828     Op->ShiftExtend.Amount = Amount;
829     Op->ShiftExtend.ImplicitAmount = ImplicitAmount;
830     return Op;
831   }
832
833   static AArch64Operand *CreateSysReg(StringRef Str, SMLoc S) {
834     AArch64Operand *Op = new AArch64Operand(k_SysReg, S, S);
835     Op->Tok.Data = Str.data();
836     Op->Tok.Length = Str.size();
837     return Op;
838   }
839
840   static AArch64Operand *CreateToken(StringRef Str, SMLoc S) {
841     AArch64Operand *Op = new AArch64Operand(k_Token, S, S);
842     Op->Tok.Data = Str.data();
843     Op->Tok.Length = Str.size();
844     return Op;
845   }
846
847
848   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
849     // Add as immediates when possible.
850     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
851       Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
852     else
853       Inst.addOperand(MCOperand::CreateExpr(Expr));
854   }
855
856   template<unsigned RegWidth>
857   void addBFILSBOperands(MCInst &Inst, unsigned N) const {
858     assert(N == 1 && "Invalid number of operands!");
859     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
860     unsigned EncodedVal = (RegWidth - CE->getValue()) % RegWidth;
861     Inst.addOperand(MCOperand::CreateImm(EncodedVal));
862   }
863
864   void addBFIWidthOperands(MCInst &Inst, unsigned N) const {
865     assert(N == 1 && "Invalid number of operands!");
866     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
867     Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
868   }
869
870   void addBFXWidthOperands(MCInst &Inst, unsigned N) const {
871     assert(N == 1 && "Invalid number of operands!");
872
873     uint64_t LSB = Inst.getOperand(Inst.getNumOperands()-1).getImm();
874     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
875
876     Inst.addOperand(MCOperand::CreateImm(LSB + CE->getValue() - 1));
877   }
878
879   void addCondCodeOperands(MCInst &Inst, unsigned N) const {
880     assert(N == 1 && "Invalid number of operands!");
881     Inst.addOperand(MCOperand::CreateImm(getCondCode()));
882   }
883
884   void addCVTFixedPosOperands(MCInst &Inst, unsigned N) const {
885     assert(N == 1 && "Invalid number of operands!");
886
887     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
888     Inst.addOperand(MCOperand::CreateImm(64 - CE->getValue()));
889   }
890
891   void addFMOVImmOperands(MCInst &Inst, unsigned N) const {
892     assert(N == 1 && "Invalid number of operands!");
893
894     APFloat RealVal(FPImm.Val);
895     uint32_t ImmVal;
896     A64Imms::isFPImm(RealVal, ImmVal);
897
898     Inst.addOperand(MCOperand::CreateImm(ImmVal));
899   }
900
901   void addFPZeroOperands(MCInst &Inst, unsigned N) const {
902     assert(N == 1 && "Invalid number of operands");
903     Inst.addOperand(MCOperand::CreateImm(0));
904   }
905
906   void addInvCondCodeOperands(MCInst &Inst, unsigned N) const {
907     assert(N == 1 && "Invalid number of operands!");
908     unsigned Encoded = A64InvertCondCode(getCondCode());
909     Inst.addOperand(MCOperand::CreateImm(Encoded));
910   }
911
912   void addRegOperands(MCInst &Inst, unsigned N) const {
913     assert(N == 1 && "Invalid number of operands!");
914     Inst.addOperand(MCOperand::CreateReg(getReg()));
915   }
916
917   void addImmOperands(MCInst &Inst, unsigned N) const {
918     assert(N == 1 && "Invalid number of operands!");
919     addExpr(Inst, getImm());
920   }
921
922   template<int MemSize>
923   void addSImm7ScaledOperands(MCInst &Inst, unsigned N) const {
924     assert(N == 1 && "Invalid number of operands!");
925
926     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
927     uint64_t Val = CE->getValue() / MemSize;
928     Inst.addOperand(MCOperand::CreateImm(Val  & 0x7f));
929   }
930
931   template<int BitWidth>
932   void addSImmOperands(MCInst &Inst, unsigned N) const {
933     assert(N == 1 && "Invalid number of operands!");
934
935     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
936     uint64_t Val = CE->getValue();
937     Inst.addOperand(MCOperand::CreateImm(Val  & ((1ULL << BitWidth) - 1)));
938   }
939
940   void addImmWithLSLOperands(MCInst &Inst, unsigned N) const {
941     assert (N == 1 && "Invalid number of operands!");
942
943     addExpr(Inst, ImmWithLSL.Val);
944   }
945
946   template<unsigned field_width, unsigned scale>
947   void addLabelOperands(MCInst &Inst, unsigned N) const {
948     assert(N == 1 && "Invalid number of operands!");
949
950     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val);
951
952     if (!CE) {
953       addExpr(Inst, Imm.Val);
954       return;
955     }
956
957     int64_t Val = CE->getValue();
958     assert(Val % scale == 0 && "Unaligned immediate in instruction");
959     Val /= scale;
960
961     Inst.addOperand(MCOperand::CreateImm(Val & ((1LL << field_width) - 1)));
962   }
963
964   template<int MemSize>
965   void addOffsetUImm12Operands(MCInst &Inst, unsigned N) const {
966     assert(N == 1 && "Invalid number of operands!");
967
968     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
969       Inst.addOperand(MCOperand::CreateImm(CE->getValue() / MemSize));
970     } else {
971       Inst.addOperand(MCOperand::CreateExpr(getImm()));
972     }
973   }
974
975   template<unsigned RegWidth>
976   void addLogicalImmOperands(MCInst &Inst, unsigned N) const {
977     assert(N == 1 && "Invalid number of operands");
978     const MCConstantExpr *CE = cast<MCConstantExpr>(Imm.Val);
979
980     uint32_t Bits;
981     A64Imms::isLogicalImm(RegWidth, CE->getValue(), Bits);
982
983     Inst.addOperand(MCOperand::CreateImm(Bits));
984   }
985
986   void addMRSOperands(MCInst &Inst, unsigned N) const {
987     assert(N == 1 && "Invalid number of operands!");
988
989     bool Valid;
990     StringRef Name(SysReg.Data, SysReg.Length);
991     uint32_t Bits = A64SysReg::MRSMapper().fromString(Name, Valid);
992
993     Inst.addOperand(MCOperand::CreateImm(Bits));
994   }
995
996   void addMSRWithRegOperands(MCInst &Inst, unsigned N) const {
997     assert(N == 1 && "Invalid number of operands!");
998
999     bool Valid;
1000     StringRef Name(SysReg.Data, SysReg.Length);
1001     uint32_t Bits = A64SysReg::MSRMapper().fromString(Name, Valid);
1002
1003     Inst.addOperand(MCOperand::CreateImm(Bits));
1004   }
1005
1006   void addMSRPStateOperands(MCInst &Inst, unsigned N) const {
1007     assert(N == 1 && "Invalid number of operands!");
1008
1009     bool Valid;
1010     StringRef Name(SysReg.Data, SysReg.Length);
1011     uint32_t Bits = A64PState::PStateMapper().fromString(Name, Valid);
1012
1013     Inst.addOperand(MCOperand::CreateImm(Bits));
1014   }
1015
1016   void addMoveWideImmOperands(MCInst &Inst, unsigned N) const {
1017     assert(N == 2 && "Invalid number of operands!");
1018
1019     addExpr(Inst, ImmWithLSL.Val);
1020
1021     AArch64MCExpr::VariantKind Variant;
1022     if (!isNonConstantExpr(ImmWithLSL.Val, Variant)) {
1023       Inst.addOperand(MCOperand::CreateImm(ImmWithLSL.ShiftAmount / 16));
1024       return;
1025     }
1026
1027     // We know it's relocated
1028     switch (Variant) {
1029     case AArch64MCExpr::VK_AARCH64_ABS_G0:
1030     case AArch64MCExpr::VK_AARCH64_ABS_G0_NC:
1031     case AArch64MCExpr::VK_AARCH64_SABS_G0:
1032     case AArch64MCExpr::VK_AARCH64_DTPREL_G0:
1033     case AArch64MCExpr::VK_AARCH64_DTPREL_G0_NC:
1034     case AArch64MCExpr::VK_AARCH64_GOTTPREL_G0_NC:
1035     case AArch64MCExpr::VK_AARCH64_TPREL_G0:
1036     case AArch64MCExpr::VK_AARCH64_TPREL_G0_NC:
1037       Inst.addOperand(MCOperand::CreateImm(0));
1038       break;
1039     case AArch64MCExpr::VK_AARCH64_ABS_G1:
1040     case AArch64MCExpr::VK_AARCH64_ABS_G1_NC:
1041     case AArch64MCExpr::VK_AARCH64_SABS_G1:
1042     case AArch64MCExpr::VK_AARCH64_DTPREL_G1:
1043     case AArch64MCExpr::VK_AARCH64_DTPREL_G1_NC:
1044     case AArch64MCExpr::VK_AARCH64_GOTTPREL_G1:
1045     case AArch64MCExpr::VK_AARCH64_TPREL_G1:
1046     case AArch64MCExpr::VK_AARCH64_TPREL_G1_NC:
1047       Inst.addOperand(MCOperand::CreateImm(1));
1048       break;
1049     case AArch64MCExpr::VK_AARCH64_ABS_G2:
1050     case AArch64MCExpr::VK_AARCH64_ABS_G2_NC:
1051     case AArch64MCExpr::VK_AARCH64_SABS_G2:
1052     case AArch64MCExpr::VK_AARCH64_DTPREL_G2:
1053     case AArch64MCExpr::VK_AARCH64_TPREL_G2:
1054       Inst.addOperand(MCOperand::CreateImm(2));
1055       break;
1056     case AArch64MCExpr::VK_AARCH64_ABS_G3:
1057       Inst.addOperand(MCOperand::CreateImm(3));
1058       break;
1059     default: llvm_unreachable("Inappropriate move wide relocation");
1060     }
1061   }
1062
1063   template<int RegWidth, bool isValidImm(int, uint64_t, int&, int&)>
1064   void addMoveWideMovAliasOperands(MCInst &Inst, unsigned N) const {
1065     assert(N == 2 && "Invalid number of operands!");
1066     int UImm16, Shift;
1067
1068     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
1069     uint64_t Value = CE->getValue();
1070
1071     if (RegWidth == 32) {
1072       Value &= 0xffffffffULL;
1073     }
1074
1075     bool Valid = isValidImm(RegWidth, Value, UImm16, Shift);
1076     (void)Valid;
1077     assert(Valid && "Invalid immediates should have been weeded out by now");
1078
1079     Inst.addOperand(MCOperand::CreateImm(UImm16));
1080     Inst.addOperand(MCOperand::CreateImm(Shift));
1081   }
1082
1083   void addPRFMOperands(MCInst &Inst, unsigned N) const {
1084     assert(N == 1 && "Invalid number of operands!");
1085
1086     const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
1087     assert(CE->getValue() >= 0 && CE->getValue() <= 31
1088            && "PRFM operand should be 5-bits");
1089
1090     Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1091   }
1092
1093   // For Add-sub (extended register) operands.
1094   void addRegExtendOperands(MCInst &Inst, unsigned N) const {
1095     assert(N == 1 && "Invalid number of operands!");
1096
1097     Inst.addOperand(MCOperand::CreateImm(ShiftExtend.Amount));
1098   }
1099
1100   // For Vector Immediates shifted imm operands.
1101   void addNeonMovImmShiftLSLOperands(MCInst &Inst, unsigned N) const {
1102     assert(N == 1 && "Invalid number of operands!");
1103
1104     if (ShiftExtend.Amount % 8 != 0 || ShiftExtend.Amount > 24)
1105       llvm_unreachable("Invalid shift amount for vector immediate inst.");
1106
1107     // Encode LSL shift amount 0, 8, 16, 24 as 0, 1, 2, 3.
1108     int64_t Imm = ShiftExtend.Amount / 8;
1109     Inst.addOperand(MCOperand::CreateImm(Imm));
1110   }
1111
1112   void addNeonMovImmShiftLSLHOperands(MCInst &Inst, unsigned N) const {
1113     assert(N == 1 && "Invalid number of operands!");
1114
1115     if (ShiftExtend.Amount != 0 && ShiftExtend.Amount != 8)
1116       llvm_unreachable("Invalid shift amount for vector immediate inst.");
1117
1118     // Encode LSLH shift amount 0, 8  as 0, 1.
1119     int64_t Imm = ShiftExtend.Amount / 8;
1120     Inst.addOperand(MCOperand::CreateImm(Imm));
1121   }
1122
1123   void addNeonMovImmShiftMSLOperands(MCInst &Inst, unsigned N) const {
1124     assert(N == 1 && "Invalid number of operands!");
1125
1126     if (ShiftExtend.Amount != 8 && ShiftExtend.Amount != 16)
1127       llvm_unreachable("Invalid shift amount for vector immediate inst.");
1128
1129     // Encode MSL shift amount 8, 16  as 0, 1.
1130     int64_t Imm = ShiftExtend.Amount / 8 - 1;
1131     Inst.addOperand(MCOperand::CreateImm(Imm));
1132   }
1133
1134   // For the extend in load-store (register offset) instructions.
1135   template<unsigned MemSize>
1136   void addAddrRegExtendOperands(MCInst &Inst, unsigned N) const {
1137     addAddrRegExtendOperands(Inst, N, MemSize);
1138   }
1139
1140   void addAddrRegExtendOperands(MCInst &Inst, unsigned N,
1141                                 unsigned MemSize) const {
1142     assert(N == 1 && "Invalid number of operands!");
1143
1144     // First bit of Option is set in instruction classes, the high two bits are
1145     // as follows:
1146     unsigned OptionHi = 0;
1147     switch (ShiftExtend.ShiftType) {
1148     case A64SE::UXTW:
1149     case A64SE::LSL:
1150       OptionHi = 1;
1151       break;
1152     case A64SE::SXTW:
1153     case A64SE::SXTX:
1154       OptionHi = 3;
1155       break;
1156     default:
1157       llvm_unreachable("Invalid extend type for register offset");
1158     }
1159
1160     unsigned S = 0;
1161     if (MemSize == 1 && !ShiftExtend.ImplicitAmount)
1162       S = 1;
1163     else if (MemSize != 1 && ShiftExtend.Amount != 0)
1164       S = 1;
1165
1166     Inst.addOperand(MCOperand::CreateImm((OptionHi << 1) | S));
1167   }
1168   void addShiftOperands(MCInst &Inst, unsigned N) const {
1169     assert(N == 1 && "Invalid number of operands!");
1170
1171     Inst.addOperand(MCOperand::CreateImm(ShiftExtend.Amount));
1172   }
1173
1174   void addNeonUImm64MaskOperands(MCInst &Inst, unsigned N) const {
1175     assert(N == 1 && "Invalid number of operands!");
1176
1177     // A bit from each byte in the constant forms the encoded immediate
1178     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1179     uint64_t Value = CE->getValue();
1180
1181     unsigned Imm = 0;
1182     for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
1183       Imm |= (Value & 1) << i;
1184     }
1185     Inst.addOperand(MCOperand::CreateImm(Imm));
1186   }
1187 };
1188
1189 } // end anonymous namespace.
1190
1191 AArch64AsmParser::OperandMatchResultTy
1192 AArch64AsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1193                                StringRef Mnemonic) {
1194
1195   // See if the operand has a custom parser
1196   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1197
1198   // It could either succeed, fail or just not care.
1199   if (ResTy != MatchOperand_NoMatch)
1200     return ResTy;
1201
1202   switch (getLexer().getKind()) {
1203   default:
1204     Error(Parser.getTok().getLoc(), "unexpected token in operand");
1205     return MatchOperand_ParseFail;
1206   case AsmToken::Identifier: {
1207     // It might be in the LSL/UXTB family ...
1208     OperandMatchResultTy GotShift = ParseShiftExtend(Operands);
1209
1210     // We can only continue if no tokens were eaten.
1211     if (GotShift != MatchOperand_NoMatch)
1212       return GotShift;
1213
1214     // ... or it might be a register ...
1215     uint32_t NumLanes = 0;
1216     OperandMatchResultTy GotReg = ParseRegister(Operands, NumLanes);
1217     assert(GotReg != MatchOperand_ParseFail
1218            && "register parsing shouldn't partially succeed");
1219
1220     if (GotReg == MatchOperand_Success) {
1221       if (Parser.getTok().is(AsmToken::LBrac))
1222         return ParseNEONLane(Operands, NumLanes);
1223       else
1224         return MatchOperand_Success;
1225     }
1226
1227     // ... or it might be a symbolish thing
1228   }
1229     // Fall through
1230   case AsmToken::LParen:  // E.g. (strcmp-4)
1231   case AsmToken::Integer: // 1f, 2b labels
1232   case AsmToken::String:  // quoted labels
1233   case AsmToken::Dot:     // . is Current location
1234   case AsmToken::Dollar:  // $ is PC
1235   case AsmToken::Colon: {
1236     SMLoc StartLoc  = Parser.getTok().getLoc();
1237     SMLoc EndLoc;
1238     const MCExpr *ImmVal = 0;
1239
1240     if (ParseImmediate(ImmVal) != MatchOperand_Success)
1241       return MatchOperand_ParseFail;
1242
1243     EndLoc = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1244     Operands.push_back(AArch64Operand::CreateImm(ImmVal, StartLoc, EndLoc));
1245     return MatchOperand_Success;
1246   }
1247   case AsmToken::Hash: {   // Immediates
1248     SMLoc StartLoc = Parser.getTok().getLoc();
1249     SMLoc EndLoc;
1250     const MCExpr *ImmVal = 0;
1251     Parser.Lex();
1252
1253     if (ParseImmediate(ImmVal) != MatchOperand_Success)
1254       return MatchOperand_ParseFail;
1255
1256     EndLoc = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1257     Operands.push_back(AArch64Operand::CreateImm(ImmVal, StartLoc, EndLoc));
1258     return MatchOperand_Success;
1259   }
1260   case AsmToken::LBrac: {
1261     SMLoc Loc = Parser.getTok().getLoc();
1262     Operands.push_back(AArch64Operand::CreateToken("[", Loc));
1263     Parser.Lex(); // Eat '['
1264
1265     // There's no comma after a '[', so we can parse the next operand
1266     // immediately.
1267     return ParseOperand(Operands, Mnemonic);
1268   }
1269   // The following will likely be useful later, but not in very early cases
1270   case AsmToken::LCurly:  // Weird SIMD lists
1271     llvm_unreachable("Don't know how to deal with '{' in operand");
1272     return MatchOperand_ParseFail;
1273   }
1274 }
1275
1276 AArch64AsmParser::OperandMatchResultTy
1277 AArch64AsmParser::ParseImmediate(const MCExpr *&ExprVal) {
1278   if (getLexer().is(AsmToken::Colon)) {
1279     AArch64MCExpr::VariantKind RefKind;
1280
1281     OperandMatchResultTy ResTy = ParseRelocPrefix(RefKind);
1282     if (ResTy != MatchOperand_Success)
1283       return ResTy;
1284
1285     const MCExpr *SubExprVal;
1286     if (getParser().parseExpression(SubExprVal))
1287       return MatchOperand_ParseFail;
1288
1289     ExprVal = AArch64MCExpr::Create(RefKind, SubExprVal, getContext());
1290     return MatchOperand_Success;
1291   }
1292
1293   // No weird AArch64MCExpr prefix
1294   return getParser().parseExpression(ExprVal)
1295     ? MatchOperand_ParseFail : MatchOperand_Success;
1296 }
1297
1298 // A lane attached to a NEON register. "[N]", which should yield three tokens:
1299 // '[', N, ']'. A hash is not allowed to precede the immediate here.
1300 AArch64AsmParser::OperandMatchResultTy
1301 AArch64AsmParser::ParseNEONLane(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1302                                 uint32_t NumLanes) {
1303   SMLoc Loc = Parser.getTok().getLoc();
1304
1305   assert(Parser.getTok().is(AsmToken::LBrac) && "inappropriate operand");
1306   Operands.push_back(AArch64Operand::CreateToken("[", Loc));
1307   Parser.Lex(); // Eat '['
1308
1309   if (Parser.getTok().isNot(AsmToken::Integer)) {
1310     Error(Parser.getTok().getLoc(), "expected lane number");
1311     return MatchOperand_ParseFail;
1312   }
1313
1314   if (Parser.getTok().getIntVal() >= NumLanes) {
1315     Error(Parser.getTok().getLoc(), "lane number incompatible with layout");
1316     return MatchOperand_ParseFail;
1317   }
1318
1319   const MCExpr *Lane = MCConstantExpr::Create(Parser.getTok().getIntVal(),
1320                                               getContext());
1321   SMLoc S = Parser.getTok().getLoc();
1322   Parser.Lex(); // Eat actual lane
1323   SMLoc E = Parser.getTok().getLoc();
1324   Operands.push_back(AArch64Operand::CreateImm(Lane, S, E));
1325
1326
1327   if (Parser.getTok().isNot(AsmToken::RBrac)) {
1328     Error(Parser.getTok().getLoc(), "expected ']' after lane");
1329     return MatchOperand_ParseFail;
1330   }
1331
1332   Operands.push_back(AArch64Operand::CreateToken("]", Loc));
1333   Parser.Lex(); // Eat ']'
1334
1335   return MatchOperand_Success;
1336 }
1337
1338 AArch64AsmParser::OperandMatchResultTy
1339 AArch64AsmParser::ParseRelocPrefix(AArch64MCExpr::VariantKind &RefKind) {
1340   assert(getLexer().is(AsmToken::Colon) && "expected a ':'");
1341   Parser.Lex();
1342
1343   if (getLexer().isNot(AsmToken::Identifier)) {
1344     Error(Parser.getTok().getLoc(),
1345           "expected relocation specifier in operand after ':'");
1346     return MatchOperand_ParseFail;
1347   }
1348
1349   std::string LowerCase = Parser.getTok().getIdentifier().lower();
1350   RefKind = StringSwitch<AArch64MCExpr::VariantKind>(LowerCase)
1351     .Case("got",              AArch64MCExpr::VK_AARCH64_GOT)
1352     .Case("got_lo12",         AArch64MCExpr::VK_AARCH64_GOT_LO12)
1353     .Case("lo12",             AArch64MCExpr::VK_AARCH64_LO12)
1354     .Case("abs_g0",           AArch64MCExpr::VK_AARCH64_ABS_G0)
1355     .Case("abs_g0_nc",        AArch64MCExpr::VK_AARCH64_ABS_G0_NC)
1356     .Case("abs_g1",           AArch64MCExpr::VK_AARCH64_ABS_G1)
1357     .Case("abs_g1_nc",        AArch64MCExpr::VK_AARCH64_ABS_G1_NC)
1358     .Case("abs_g2",           AArch64MCExpr::VK_AARCH64_ABS_G2)
1359     .Case("abs_g2_nc",        AArch64MCExpr::VK_AARCH64_ABS_G2_NC)
1360     .Case("abs_g3",           AArch64MCExpr::VK_AARCH64_ABS_G3)
1361     .Case("abs_g0_s",         AArch64MCExpr::VK_AARCH64_SABS_G0)
1362     .Case("abs_g1_s",         AArch64MCExpr::VK_AARCH64_SABS_G1)
1363     .Case("abs_g2_s",         AArch64MCExpr::VK_AARCH64_SABS_G2)
1364     .Case("dtprel_g2",        AArch64MCExpr::VK_AARCH64_DTPREL_G2)
1365     .Case("dtprel_g1",        AArch64MCExpr::VK_AARCH64_DTPREL_G1)
1366     .Case("dtprel_g1_nc",     AArch64MCExpr::VK_AARCH64_DTPREL_G1_NC)
1367     .Case("dtprel_g0",        AArch64MCExpr::VK_AARCH64_DTPREL_G0)
1368     .Case("dtprel_g0_nc",     AArch64MCExpr::VK_AARCH64_DTPREL_G0_NC)
1369     .Case("dtprel_hi12",      AArch64MCExpr::VK_AARCH64_DTPREL_HI12)
1370     .Case("dtprel_lo12",      AArch64MCExpr::VK_AARCH64_DTPREL_LO12)
1371     .Case("dtprel_lo12_nc",   AArch64MCExpr::VK_AARCH64_DTPREL_LO12_NC)
1372     .Case("gottprel_g1",      AArch64MCExpr::VK_AARCH64_GOTTPREL_G1)
1373     .Case("gottprel_g0_nc",   AArch64MCExpr::VK_AARCH64_GOTTPREL_G0_NC)
1374     .Case("gottprel",         AArch64MCExpr::VK_AARCH64_GOTTPREL)
1375     .Case("gottprel_lo12",    AArch64MCExpr::VK_AARCH64_GOTTPREL_LO12)
1376     .Case("tprel_g2",         AArch64MCExpr::VK_AARCH64_TPREL_G2)
1377     .Case("tprel_g1",         AArch64MCExpr::VK_AARCH64_TPREL_G1)
1378     .Case("tprel_g1_nc",      AArch64MCExpr::VK_AARCH64_TPREL_G1_NC)
1379     .Case("tprel_g0",         AArch64MCExpr::VK_AARCH64_TPREL_G0)
1380     .Case("tprel_g0_nc",      AArch64MCExpr::VK_AARCH64_TPREL_G0_NC)
1381     .Case("tprel_hi12",       AArch64MCExpr::VK_AARCH64_TPREL_HI12)
1382     .Case("tprel_lo12",       AArch64MCExpr::VK_AARCH64_TPREL_LO12)
1383     .Case("tprel_lo12_nc",    AArch64MCExpr::VK_AARCH64_TPREL_LO12_NC)
1384     .Case("tlsdesc",          AArch64MCExpr::VK_AARCH64_TLSDESC)
1385     .Case("tlsdesc_lo12",     AArch64MCExpr::VK_AARCH64_TLSDESC_LO12)
1386     .Default(AArch64MCExpr::VK_AARCH64_None);
1387
1388   if (RefKind == AArch64MCExpr::VK_AARCH64_None) {
1389     Error(Parser.getTok().getLoc(),
1390           "expected relocation specifier in operand after ':'");
1391     return MatchOperand_ParseFail;
1392   }
1393   Parser.Lex(); // Eat identifier
1394
1395   if (getLexer().isNot(AsmToken::Colon)) {
1396     Error(Parser.getTok().getLoc(),
1397           "expected ':' after relocation specifier");
1398     return MatchOperand_ParseFail;
1399   }
1400   Parser.Lex();
1401   return MatchOperand_Success;
1402 }
1403
1404 AArch64AsmParser::OperandMatchResultTy
1405 AArch64AsmParser::ParseImmWithLSLOperand(
1406                                SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1407   // FIXME?: I want to live in a world where immediates must start with
1408   // #. Please don't dash my hopes (well, do if you have a good reason).
1409   if (Parser.getTok().isNot(AsmToken::Hash)) return MatchOperand_NoMatch;
1410
1411   SMLoc S = Parser.getTok().getLoc();
1412   Parser.Lex(); // Eat '#'
1413
1414   const MCExpr *Imm;
1415   if (ParseImmediate(Imm) != MatchOperand_Success)
1416     return MatchOperand_ParseFail;
1417   else if (Parser.getTok().isNot(AsmToken::Comma)) {
1418     SMLoc E = Parser.getTok().getLoc();
1419     Operands.push_back(AArch64Operand::CreateImmWithLSL(Imm, 0, true, S, E));
1420     return MatchOperand_Success;
1421   }
1422
1423   // Eat ','
1424   Parser.Lex();
1425
1426   // The optional operand must be "lsl #N" where N is non-negative.
1427   if (Parser.getTok().is(AsmToken::Identifier)
1428       && Parser.getTok().getIdentifier().lower() == "lsl") {
1429     Parser.Lex();
1430
1431     if (Parser.getTok().is(AsmToken::Hash)) {
1432       Parser.Lex();
1433
1434       if (Parser.getTok().isNot(AsmToken::Integer)) {
1435         Error(Parser.getTok().getLoc(), "only 'lsl #+N' valid after immediate");
1436         return MatchOperand_ParseFail;
1437       }
1438     }
1439   }
1440
1441   int64_t ShiftAmount = Parser.getTok().getIntVal();
1442
1443   if (ShiftAmount < 0) {
1444     Error(Parser.getTok().getLoc(), "positive shift amount required");
1445     return MatchOperand_ParseFail;
1446   }
1447   Parser.Lex(); // Eat the number
1448
1449   SMLoc E = Parser.getTok().getLoc();
1450   Operands.push_back(AArch64Operand::CreateImmWithLSL(Imm, ShiftAmount,
1451                                                       false, S, E));
1452   return MatchOperand_Success;
1453 }
1454
1455
1456 AArch64AsmParser::OperandMatchResultTy
1457 AArch64AsmParser::ParseCondCodeOperand(
1458                                SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1459   if (Parser.getTok().isNot(AsmToken::Identifier))
1460     return MatchOperand_NoMatch;
1461
1462   StringRef Tok = Parser.getTok().getIdentifier();
1463   A64CC::CondCodes CondCode = A64StringToCondCode(Tok);
1464
1465   if (CondCode == A64CC::Invalid)
1466     return MatchOperand_NoMatch;
1467
1468   SMLoc S = Parser.getTok().getLoc();
1469   Parser.Lex(); // Eat condition code
1470   SMLoc E = Parser.getTok().getLoc();
1471
1472   Operands.push_back(AArch64Operand::CreateCondCode(CondCode, S, E));
1473   return MatchOperand_Success;
1474 }
1475
1476 AArch64AsmParser::OperandMatchResultTy
1477 AArch64AsmParser::ParseCRxOperand(
1478                                SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1479   SMLoc S = Parser.getTok().getLoc();
1480   if (Parser.getTok().isNot(AsmToken::Identifier)) {
1481     Error(S, "Expected cN operand where 0 <= N <= 15");
1482     return MatchOperand_ParseFail;
1483   }
1484
1485   std::string LowerTok = Parser.getTok().getIdentifier().lower();
1486   StringRef Tok(LowerTok);
1487   if (Tok[0] != 'c') {
1488     Error(S, "Expected cN operand where 0 <= N <= 15");
1489     return MatchOperand_ParseFail;
1490   }
1491
1492   uint32_t CRNum;
1493   bool BadNum = Tok.drop_front().getAsInteger(10, CRNum);
1494   if (BadNum || CRNum > 15) {
1495     Error(S, "Expected cN operand where 0 <= N <= 15");
1496     return MatchOperand_ParseFail;
1497   }
1498
1499   const MCExpr *CRImm = MCConstantExpr::Create(CRNum, getContext());
1500
1501   Parser.Lex();
1502   SMLoc E = Parser.getTok().getLoc();
1503
1504   Operands.push_back(AArch64Operand::CreateImm(CRImm, S, E));
1505   return MatchOperand_Success;
1506 }
1507
1508 AArch64AsmParser::OperandMatchResultTy
1509 AArch64AsmParser::ParseFPImmOperand(
1510                                SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1511
1512   // FIXME?: I want to live in a world where immediates must start with
1513   // #. Please don't dash my hopes (well, do if you have a good reason).
1514   if (Parser.getTok().isNot(AsmToken::Hash)) return MatchOperand_NoMatch;
1515
1516   SMLoc S = Parser.getTok().getLoc();
1517   Parser.Lex(); // Eat '#'
1518
1519   bool Negative = false;
1520   if (Parser.getTok().is(AsmToken::Minus)) {
1521     Negative = true;
1522     Parser.Lex(); // Eat '-'
1523   } else if (Parser.getTok().is(AsmToken::Plus)) {
1524     Parser.Lex(); // Eat '+'
1525   }
1526
1527   if (Parser.getTok().isNot(AsmToken::Real)) {
1528     Error(S, "Expected floating-point immediate");
1529     return MatchOperand_ParseFail;
1530   }
1531
1532   APFloat RealVal(APFloat::IEEEdouble, Parser.getTok().getString());
1533   if (Negative) RealVal.changeSign();
1534   double DblVal = RealVal.convertToDouble();
1535
1536   Parser.Lex(); // Eat real number
1537   SMLoc E = Parser.getTok().getLoc();
1538
1539   Operands.push_back(AArch64Operand::CreateFPImm(DblVal, S, E));
1540   return MatchOperand_Success;
1541 }
1542
1543
1544 // Automatically generated
1545 static unsigned MatchRegisterName(StringRef Name);
1546
1547 bool
1548 AArch64AsmParser::IdentifyRegister(unsigned &RegNum, SMLoc &RegEndLoc,
1549                                    StringRef &Layout,
1550                                    SMLoc &LayoutLoc) const {
1551   const AsmToken &Tok = Parser.getTok();
1552
1553   if (Tok.isNot(AsmToken::Identifier))
1554     return false;
1555
1556   std::string LowerReg = Tok.getString().lower();
1557   size_t DotPos = LowerReg.find('.');
1558
1559   RegNum = MatchRegisterName(LowerReg.substr(0, DotPos));
1560   if (RegNum == AArch64::NoRegister) {
1561     RegNum = StringSwitch<unsigned>(LowerReg.substr(0, DotPos))
1562       .Case("ip0", AArch64::X16)
1563       .Case("ip1", AArch64::X17)
1564       .Case("fp", AArch64::X29)
1565       .Case("lr", AArch64::X30)
1566       .Default(AArch64::NoRegister);
1567   }
1568   if (RegNum == AArch64::NoRegister)
1569     return false;
1570
1571   SMLoc S = Tok.getLoc();
1572   RegEndLoc = SMLoc::getFromPointer(S.getPointer() + DotPos);
1573
1574   if (DotPos == StringRef::npos) {
1575     Layout = StringRef();
1576   } else {
1577     // Everything afterwards needs to be a literal token, expected to be
1578     // '.2d','.b' etc for vector registers.
1579
1580     // This StringSwitch validates the input and (perhaps more importantly)
1581     // gives us a permanent string to use in the token (a pointer into LowerReg
1582     // would go out of scope when we return).
1583     LayoutLoc = SMLoc::getFromPointer(S.getPointer() + DotPos + 1);
1584     std::string LayoutText = LowerReg.substr(DotPos, StringRef::npos);
1585     Layout = StringSwitch<const char *>(LayoutText)
1586       .Case(".d", ".d").Case(".1d", ".1d").Case(".2d", ".2d")
1587       .Case(".s", ".s").Case(".2s", ".2s").Case(".4s", ".4s")
1588       .Case(".h", ".h").Case(".4h", ".4h").Case(".8h", ".8h")
1589       .Case(".b", ".b").Case(".8b", ".8b").Case(".16b", ".16b")
1590       .Default("");
1591
1592     if (Layout.size() == 0) {
1593       // Malformed register
1594       return false;
1595     }
1596   }
1597
1598   return true;
1599 }
1600
1601 AArch64AsmParser::OperandMatchResultTy
1602 AArch64AsmParser::ParseRegister(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1603                                 uint32_t &NumLanes) {
1604   unsigned RegNum;
1605   StringRef Layout;
1606   SMLoc RegEndLoc, LayoutLoc;
1607   SMLoc S = Parser.getTok().getLoc();
1608
1609   if (!IdentifyRegister(RegNum, RegEndLoc, Layout, LayoutLoc))
1610     return MatchOperand_NoMatch;
1611
1612   Operands.push_back(AArch64Operand::CreateReg(RegNum, S, RegEndLoc));
1613
1614   if (Layout.size() != 0) {
1615     unsigned long long TmpLanes = 0;
1616     llvm::getAsUnsignedInteger(Layout.substr(1), 10, TmpLanes);
1617     if (TmpLanes != 0) {
1618       NumLanes = TmpLanes;
1619     } else {
1620       // If the number of lanes isn't specified explicitly, a valid instruction
1621       // will have an element specifier and be capable of acting on the entire
1622       // vector register.
1623       switch (Layout.back()) {
1624       default: llvm_unreachable("Invalid layout specifier");
1625       case 'b': NumLanes = 16; break;
1626       case 'h': NumLanes = 8; break;
1627       case 's': NumLanes = 4; break;
1628       case 'd': NumLanes = 2; break;
1629       }
1630     }
1631
1632     Operands.push_back(AArch64Operand::CreateToken(Layout, LayoutLoc));
1633   }
1634
1635   Parser.Lex();
1636   return MatchOperand_Success;
1637 }
1638
1639 bool
1640 AArch64AsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
1641                                 SMLoc &EndLoc) {
1642   // This callback is used for things like DWARF frame directives in
1643   // assembly. They don't care about things like NEON layouts or lanes, they
1644   // just want to be able to produce the DWARF register number.
1645   StringRef LayoutSpec;
1646   SMLoc RegEndLoc, LayoutLoc;
1647   StartLoc = Parser.getTok().getLoc();
1648
1649   if (!IdentifyRegister(RegNo, RegEndLoc, LayoutSpec, LayoutLoc))
1650     return true;
1651
1652   Parser.Lex();
1653   EndLoc = Parser.getTok().getLoc();
1654
1655   return false;
1656 }
1657
1658 AArch64AsmParser::OperandMatchResultTy
1659 AArch64AsmParser::ParseNamedImmOperand(const NamedImmMapper &Mapper,
1660                                SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1661   // Since these operands occur in very limited circumstances, without
1662   // alternatives, we actually signal an error if there is no match. If relaxing
1663   // this, beware of unintended consequences: an immediate will be accepted
1664   // during matching, no matter how it gets into the AArch64Operand.
1665   const AsmToken &Tok = Parser.getTok();
1666   SMLoc S = Tok.getLoc();
1667
1668   if (Tok.is(AsmToken::Identifier)) {
1669     bool ValidName;
1670     uint32_t Code = Mapper.fromString(Tok.getString().lower(), ValidName);
1671
1672     if (!ValidName) {
1673       Error(S, "operand specifier not recognised");
1674       return MatchOperand_ParseFail;
1675     }
1676
1677     Parser.Lex(); // We're done with the identifier. Eat it
1678
1679     SMLoc E = Parser.getTok().getLoc();
1680     const MCExpr *Imm = MCConstantExpr::Create(Code, getContext());
1681     Operands.push_back(AArch64Operand::CreateImm(Imm, S, E));
1682     return MatchOperand_Success;
1683   } else if (Tok.is(AsmToken::Hash)) {
1684     Parser.Lex();
1685
1686     const MCExpr *ImmVal;
1687     if (ParseImmediate(ImmVal) != MatchOperand_Success)
1688       return MatchOperand_ParseFail;
1689
1690     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
1691     if (!CE || CE->getValue() < 0 || !Mapper.validImm(CE->getValue())) {
1692       Error(S, "Invalid immediate for instruction");
1693       return MatchOperand_ParseFail;
1694     }
1695
1696     SMLoc E = Parser.getTok().getLoc();
1697     Operands.push_back(AArch64Operand::CreateImm(ImmVal, S, E));
1698     return MatchOperand_Success;
1699   }
1700
1701   Error(S, "unexpected operand for instruction");
1702   return MatchOperand_ParseFail;
1703 }
1704
1705 AArch64AsmParser::OperandMatchResultTy
1706 AArch64AsmParser::ParseSysRegOperand(
1707                                SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1708   const AsmToken &Tok = Parser.getTok();
1709
1710   // Any MSR/MRS operand will be an identifier, and we want to store it as some
1711   // kind of string: SPSel is valid for two different forms of MSR with two
1712   // different encodings. There's no collision at the moment, but the potential
1713   // is there.
1714   if (!Tok.is(AsmToken::Identifier)) {
1715     return MatchOperand_NoMatch;
1716   }
1717
1718   SMLoc S = Tok.getLoc();
1719   Operands.push_back(AArch64Operand::CreateSysReg(Tok.getString(), S));
1720   Parser.Lex(); // Eat identifier
1721
1722   return MatchOperand_Success;
1723 }
1724
1725 AArch64AsmParser::OperandMatchResultTy
1726 AArch64AsmParser::ParseLSXAddressOperand(
1727                                SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1728   SMLoc S = Parser.getTok().getLoc();
1729
1730   unsigned RegNum;
1731   SMLoc RegEndLoc, LayoutLoc;
1732   StringRef Layout;
1733   if(!IdentifyRegister(RegNum, RegEndLoc, Layout, LayoutLoc)
1734      || !AArch64MCRegisterClasses[AArch64::GPR64xspRegClassID].contains(RegNum)
1735      || Layout.size() != 0) {
1736     // Check Layout.size because we don't want to let "x3.4s" or similar
1737     // through.
1738     return MatchOperand_NoMatch;
1739   }
1740   Parser.Lex(); // Eat register
1741
1742   if (Parser.getTok().is(AsmToken::RBrac)) {
1743     // We're done
1744     SMLoc E = Parser.getTok().getLoc();
1745     Operands.push_back(AArch64Operand::CreateWrappedReg(RegNum, S, E));
1746     return MatchOperand_Success;
1747   }
1748
1749   // Otherwise, only ", #0" is valid
1750
1751   if (Parser.getTok().isNot(AsmToken::Comma)) {
1752     Error(Parser.getTok().getLoc(), "expected ',' or ']' after register");
1753     return MatchOperand_ParseFail;
1754   }
1755   Parser.Lex(); // Eat ','
1756
1757   if (Parser.getTok().isNot(AsmToken::Hash)) {
1758     Error(Parser.getTok().getLoc(), "expected '#0'");
1759     return MatchOperand_ParseFail;
1760   }
1761   Parser.Lex(); // Eat '#'
1762
1763   if (Parser.getTok().isNot(AsmToken::Integer)
1764       || Parser.getTok().getIntVal() != 0 ) {
1765     Error(Parser.getTok().getLoc(), "expected '#0'");
1766     return MatchOperand_ParseFail;
1767   }
1768   Parser.Lex(); // Eat '0'
1769
1770   SMLoc E = Parser.getTok().getLoc();
1771   Operands.push_back(AArch64Operand::CreateWrappedReg(RegNum, S, E));
1772   return MatchOperand_Success;
1773 }
1774
1775 AArch64AsmParser::OperandMatchResultTy
1776 AArch64AsmParser::ParseShiftExtend(
1777                                SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1778   StringRef IDVal = Parser.getTok().getIdentifier();
1779   std::string LowerID = IDVal.lower();
1780
1781   A64SE::ShiftExtSpecifiers Spec =
1782       StringSwitch<A64SE::ShiftExtSpecifiers>(LowerID)
1783         .Case("lsl", A64SE::LSL)
1784         .Case("msl", A64SE::MSL)
1785         .Case("lsr", A64SE::LSR)
1786         .Case("asr", A64SE::ASR)
1787         .Case("ror", A64SE::ROR)
1788         .Case("uxtb", A64SE::UXTB)
1789         .Case("uxth", A64SE::UXTH)
1790         .Case("uxtw", A64SE::UXTW)
1791         .Case("uxtx", A64SE::UXTX)
1792         .Case("sxtb", A64SE::SXTB)
1793         .Case("sxth", A64SE::SXTH)
1794         .Case("sxtw", A64SE::SXTW)
1795         .Case("sxtx", A64SE::SXTX)
1796         .Default(A64SE::Invalid);
1797
1798   if (Spec == A64SE::Invalid)
1799     return MatchOperand_NoMatch;
1800
1801   // Eat the shift
1802   SMLoc S, E;
1803   S = Parser.getTok().getLoc();
1804   Parser.Lex();
1805
1806   if (Spec != A64SE::LSL && Spec != A64SE::LSR && Spec != A64SE::ASR &&
1807       Spec != A64SE::ROR && Spec != A64SE::MSL) {
1808     // The shift amount can be omitted for the extending versions, but not real
1809     // shifts:
1810     //     add x0, x0, x0, uxtb
1811     // is valid, and equivalent to
1812     //     add x0, x0, x0, uxtb #0
1813
1814     if (Parser.getTok().is(AsmToken::Comma) ||
1815         Parser.getTok().is(AsmToken::EndOfStatement) ||
1816         Parser.getTok().is(AsmToken::RBrac)) {
1817       Operands.push_back(AArch64Operand::CreateShiftExtend(Spec, 0, true,
1818                                                            S, E));
1819       return MatchOperand_Success;
1820     }
1821   }
1822
1823   // Eat # at beginning of immediate
1824   if (!Parser.getTok().is(AsmToken::Hash)) {
1825     Error(Parser.getTok().getLoc(),
1826           "expected #imm after shift specifier");
1827     return MatchOperand_ParseFail;
1828   }
1829   Parser.Lex();
1830
1831   // Make sure we do actually have a number
1832   if (!Parser.getTok().is(AsmToken::Integer)) {
1833     Error(Parser.getTok().getLoc(),
1834           "expected integer shift amount");
1835     return MatchOperand_ParseFail;
1836   }
1837   unsigned Amount = Parser.getTok().getIntVal();
1838   Parser.Lex();
1839   E = Parser.getTok().getLoc();
1840
1841   Operands.push_back(AArch64Operand::CreateShiftExtend(Spec, Amount, false,
1842                                                        S, E));
1843
1844   return MatchOperand_Success;
1845 }
1846
1847 // FIXME: We would really like to be able to tablegen'erate this.
1848 bool AArch64AsmParser::
1849 validateInstruction(MCInst &Inst,
1850                     const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1851   switch (Inst.getOpcode()) {
1852   case AArch64::BFIwwii:
1853   case AArch64::BFIxxii:
1854   case AArch64::SBFIZwwii:
1855   case AArch64::SBFIZxxii:
1856   case AArch64::UBFIZwwii:
1857   case AArch64::UBFIZxxii:  {
1858     unsigned ImmOps = Inst.getNumOperands() - 2;
1859     int64_t ImmR = Inst.getOperand(ImmOps).getImm();
1860     int64_t ImmS = Inst.getOperand(ImmOps+1).getImm();
1861
1862     if (ImmR != 0 && ImmS >= ImmR) {
1863       return Error(Operands[4]->getStartLoc(),
1864                    "requested insert overflows register");
1865     }
1866     return false;
1867   }
1868   case AArch64::BFXILwwii:
1869   case AArch64::BFXILxxii:
1870   case AArch64::SBFXwwii:
1871   case AArch64::SBFXxxii:
1872   case AArch64::UBFXwwii:
1873   case AArch64::UBFXxxii: {
1874     unsigned ImmOps = Inst.getNumOperands() - 2;
1875     int64_t ImmR = Inst.getOperand(ImmOps).getImm();
1876     int64_t ImmS = Inst.getOperand(ImmOps+1).getImm();
1877     int64_t RegWidth = 0;
1878     switch (Inst.getOpcode()) {
1879     case AArch64::SBFXxxii: case AArch64::UBFXxxii: case AArch64::BFXILxxii:
1880       RegWidth = 64;
1881       break;
1882     case AArch64::SBFXwwii: case AArch64::UBFXwwii: case AArch64::BFXILwwii:
1883       RegWidth = 32;
1884       break;
1885     }
1886
1887     if (ImmS >= RegWidth || ImmS < ImmR) {
1888       return Error(Operands[4]->getStartLoc(),
1889                    "requested extract overflows register");
1890     }
1891     return false;
1892   }
1893   case AArch64::ICix: {
1894     int64_t ImmVal = Inst.getOperand(0).getImm();
1895     A64IC::ICValues ICOp = static_cast<A64IC::ICValues>(ImmVal);
1896     if (!A64IC::NeedsRegister(ICOp)) {
1897       return Error(Operands[1]->getStartLoc(),
1898                    "specified IC op does not use a register");
1899     }
1900     return false;
1901   }
1902   case AArch64::ICi: {
1903     int64_t ImmVal = Inst.getOperand(0).getImm();
1904     A64IC::ICValues ICOp = static_cast<A64IC::ICValues>(ImmVal);
1905     if (A64IC::NeedsRegister(ICOp)) {
1906       return Error(Operands[1]->getStartLoc(),
1907                    "specified IC op requires a register");
1908     }
1909     return false;
1910   }
1911   case AArch64::TLBIix: {
1912     int64_t ImmVal = Inst.getOperand(0).getImm();
1913     A64TLBI::TLBIValues TLBIOp = static_cast<A64TLBI::TLBIValues>(ImmVal);
1914     if (!A64TLBI::NeedsRegister(TLBIOp)) {
1915       return Error(Operands[1]->getStartLoc(),
1916                    "specified TLBI op does not use a register");
1917     }
1918     return false;
1919   }
1920   case AArch64::TLBIi: {
1921     int64_t ImmVal = Inst.getOperand(0).getImm();
1922     A64TLBI::TLBIValues TLBIOp = static_cast<A64TLBI::TLBIValues>(ImmVal);
1923     if (A64TLBI::NeedsRegister(TLBIOp)) {
1924       return Error(Operands[1]->getStartLoc(),
1925                    "specified TLBI op requires a register");
1926     }
1927     return false;
1928   }
1929   }
1930
1931   return false;
1932 }
1933
1934
1935 // Parses the instruction *together with* all operands, appending each parsed
1936 // operand to the "Operands" list
1937 bool AArch64AsmParser::ParseInstruction(ParseInstructionInfo &Info,
1938                                         StringRef Name, SMLoc NameLoc,
1939                                SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1940   size_t CondCodePos = Name.find('.');
1941
1942   StringRef Mnemonic = Name.substr(0, CondCodePos);
1943   Operands.push_back(AArch64Operand::CreateToken(Mnemonic, NameLoc));
1944
1945   if (CondCodePos != StringRef::npos) {
1946     // We have a condition code
1947     SMLoc S = SMLoc::getFromPointer(NameLoc.getPointer() + CondCodePos + 1);
1948     StringRef CondStr = Name.substr(CondCodePos + 1, StringRef::npos);
1949     A64CC::CondCodes Code;
1950
1951     Code = A64StringToCondCode(CondStr);
1952
1953     if (Code == A64CC::Invalid) {
1954       Error(S, "invalid condition code");
1955       Parser.eatToEndOfStatement();
1956       return true;
1957     }
1958
1959     SMLoc DotL = SMLoc::getFromPointer(NameLoc.getPointer() + CondCodePos);
1960
1961     Operands.push_back(AArch64Operand::CreateToken(".",  DotL));
1962     SMLoc E = SMLoc::getFromPointer(NameLoc.getPointer() + CondCodePos + 3);
1963     Operands.push_back(AArch64Operand::CreateCondCode(Code, S, E));
1964   }
1965
1966   // Now we parse the operands of this instruction
1967   if (getLexer().isNot(AsmToken::EndOfStatement)) {
1968     // Read the first operand.
1969     if (ParseOperand(Operands, Mnemonic)) {
1970       Parser.eatToEndOfStatement();
1971       return true;
1972     }
1973
1974     while (getLexer().is(AsmToken::Comma)) {
1975       Parser.Lex();  // Eat the comma.
1976
1977       // Parse and remember the operand.
1978       if (ParseOperand(Operands, Mnemonic)) {
1979         Parser.eatToEndOfStatement();
1980         return true;
1981       }
1982
1983
1984       // After successfully parsing some operands there are two special cases to
1985       // consider (i.e. notional operands not separated by commas). Both are due
1986       // to memory specifiers:
1987       //  + An RBrac will end an address for load/store/prefetch
1988       //  + An '!' will indicate a pre-indexed operation.
1989       //
1990       // It's someone else's responsibility to make sure these tokens are sane
1991       // in the given context!
1992       if (Parser.getTok().is(AsmToken::RBrac)) {
1993         SMLoc Loc = Parser.getTok().getLoc();
1994         Operands.push_back(AArch64Operand::CreateToken("]", Loc));
1995         Parser.Lex();
1996       }
1997
1998       if (Parser.getTok().is(AsmToken::Exclaim)) {
1999         SMLoc Loc = Parser.getTok().getLoc();
2000         Operands.push_back(AArch64Operand::CreateToken("!", Loc));
2001         Parser.Lex();
2002       }
2003     }
2004   }
2005
2006   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2007     SMLoc Loc = getLexer().getLoc();
2008     Parser.eatToEndOfStatement();
2009     return Error(Loc, "expected comma before next operand");
2010   }
2011
2012   // Eat the EndOfStatement
2013   Parser.Lex();
2014
2015   return false;
2016 }
2017
2018 bool AArch64AsmParser::ParseDirective(AsmToken DirectiveID) {
2019   StringRef IDVal = DirectiveID.getIdentifier();
2020   if (IDVal == ".hword")
2021     return ParseDirectiveWord(2, DirectiveID.getLoc());
2022   else if (IDVal == ".word")
2023     return ParseDirectiveWord(4, DirectiveID.getLoc());
2024   else if (IDVal == ".xword")
2025     return ParseDirectiveWord(8, DirectiveID.getLoc());
2026   else if (IDVal == ".tlsdesccall")
2027     return ParseDirectiveTLSDescCall(DirectiveID.getLoc());
2028
2029   return true;
2030 }
2031
2032 /// parseDirectiveWord
2033 ///  ::= .word [ expression (, expression)* ]
2034 bool AArch64AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2035   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2036     for (;;) {
2037       const MCExpr *Value;
2038       if (getParser().parseExpression(Value))
2039         return true;
2040
2041       getParser().getStreamer().EmitValue(Value, Size);
2042
2043       if (getLexer().is(AsmToken::EndOfStatement))
2044         break;
2045
2046       // FIXME: Improve diagnostic.
2047       if (getLexer().isNot(AsmToken::Comma))
2048         return Error(L, "unexpected token in directive");
2049       Parser.Lex();
2050     }
2051   }
2052
2053   Parser.Lex();
2054   return false;
2055 }
2056
2057 // parseDirectiveTLSDescCall:
2058 //   ::= .tlsdesccall symbol
2059 bool AArch64AsmParser::ParseDirectiveTLSDescCall(SMLoc L) {
2060   StringRef Name;
2061   if (getParser().parseIdentifier(Name))
2062     return Error(L, "expected symbol after directive");
2063
2064   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2065   const MCSymbolRefExpr *Expr = MCSymbolRefExpr::Create(Sym, getContext());
2066
2067   MCInst Inst;
2068   Inst.setOpcode(AArch64::TLSDESCCALL);
2069   Inst.addOperand(MCOperand::CreateExpr(Expr));
2070
2071   getParser().getStreamer().EmitInstruction(Inst);
2072   return false;
2073 }
2074
2075
2076 bool AArch64AsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
2077                                  SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2078                                  MCStreamer &Out, unsigned &ErrorInfo,
2079                                  bool MatchingInlineAsm) {
2080   MCInst Inst;
2081   unsigned MatchResult;
2082   MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
2083                                      MatchingInlineAsm);
2084
2085   if (ErrorInfo != ~0U && ErrorInfo >= Operands.size())
2086     return Error(IDLoc, "too few operands for instruction");
2087
2088   switch (MatchResult) {
2089   default: break;
2090   case Match_Success:
2091     if (validateInstruction(Inst, Operands))
2092       return true;
2093
2094     Out.EmitInstruction(Inst);
2095     return false;
2096   case Match_MissingFeature:
2097     Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2098     return true;
2099   case Match_InvalidOperand: {
2100     SMLoc ErrorLoc = IDLoc;
2101     if (ErrorInfo != ~0U) {
2102       ErrorLoc = ((AArch64Operand*)Operands[ErrorInfo])->getStartLoc();
2103       if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2104     }
2105
2106     return Error(ErrorLoc, "invalid operand for instruction");
2107   }
2108   case Match_MnemonicFail:
2109     return Error(IDLoc, "invalid instruction");
2110
2111   case Match_AddSubRegExtendSmall:
2112     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2113       "expected '[su]xt[bhw]' or 'lsl' with optional integer in range [0, 4]");
2114   case Match_AddSubRegExtendLarge:
2115     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2116       "expected 'sxtx' 'uxtx' or 'lsl' with optional integer in range [0, 4]");
2117   case Match_AddSubRegShift32:
2118     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2119        "expected 'lsl', 'lsr' or 'asr' with optional integer in range [0, 31]");
2120   case Match_AddSubRegShift64:
2121     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2122        "expected 'lsl', 'lsr' or 'asr' with optional integer in range [0, 63]");
2123   case Match_AddSubSecondSource:
2124       return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2125           "expected compatible register, symbol or integer in range [0, 4095]");
2126   case Match_CVTFixedPos32:
2127     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2128                  "expected integer in range [1, 32]");
2129   case Match_CVTFixedPos64:
2130     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2131                  "expected integer in range [1, 64]");
2132   case Match_CondCode:
2133     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2134                  "expected AArch64 condition code");
2135   case Match_FPImm:
2136     // Any situation which allows a nontrivial floating-point constant also
2137     // allows a register.
2138     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2139                  "expected compatible register or floating-point constant");
2140   case Match_FPZero:
2141     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2142                  "expected floating-point constant #0.0 or invalid register type");
2143   case Match_Label:
2144     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2145                  "expected label or encodable integer pc offset");
2146   case Match_Lane1:
2147     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2148                  "expected lane specifier '[1]'");
2149   case Match_LoadStoreExtend32_1:
2150     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2151                  "expected 'uxtw' or 'sxtw' with optional shift of #0");
2152   case Match_LoadStoreExtend32_2:
2153     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2154                  "expected 'uxtw' or 'sxtw' with optional shift of #0 or #1");
2155   case Match_LoadStoreExtend32_4:
2156     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2157                  "expected 'uxtw' or 'sxtw' with optional shift of #0 or #2");
2158   case Match_LoadStoreExtend32_8:
2159     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2160                  "expected 'uxtw' or 'sxtw' with optional shift of #0 or #3");
2161   case Match_LoadStoreExtend32_16:
2162     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2163                  "expected 'lsl' or 'sxtw' with optional shift of #0 or #4");
2164   case Match_LoadStoreExtend64_1:
2165     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2166                  "expected 'lsl' or 'sxtx' with optional shift of #0");
2167   case Match_LoadStoreExtend64_2:
2168     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2169                  "expected 'lsl' or 'sxtx' with optional shift of #0 or #1");
2170   case Match_LoadStoreExtend64_4:
2171     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2172                  "expected 'lsl' or 'sxtx' with optional shift of #0 or #2");
2173   case Match_LoadStoreExtend64_8:
2174     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2175                  "expected 'lsl' or 'sxtx' with optional shift of #0 or #3");
2176   case Match_LoadStoreExtend64_16:
2177     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2178                  "expected 'lsl' or 'sxtx' with optional shift of #0 or #4");
2179   case Match_LoadStoreSImm7_4:
2180     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2181                  "expected integer multiple of 4 in range [-256, 252]");
2182   case Match_LoadStoreSImm7_8:
2183     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2184                  "expected integer multiple of 8 in range [-512, 508]");
2185   case Match_LoadStoreSImm7_16:
2186     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2187                  "expected integer multiple of 16 in range [-1024, 1016]");
2188   case Match_LoadStoreSImm9:
2189     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2190                  "expected integer in range [-256, 255]");
2191   case Match_LoadStoreUImm12_1:
2192     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2193                  "expected symbolic reference or integer in range [0, 4095]");
2194   case Match_LoadStoreUImm12_2:
2195     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2196                  "expected symbolic reference or integer in range [0, 8190]");
2197   case Match_LoadStoreUImm12_4:
2198     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2199                  "expected symbolic reference or integer in range [0, 16380]");
2200   case Match_LoadStoreUImm12_8:
2201     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2202                  "expected symbolic reference or integer in range [0, 32760]");
2203   case Match_LoadStoreUImm12_16:
2204     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2205                  "expected symbolic reference or integer in range [0, 65520]");
2206   case Match_LogicalSecondSource:
2207     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2208                  "expected compatible register or logical immediate");
2209   case Match_MOVWUImm16:
2210     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2211                  "expected relocated symbol or integer in range [0, 65535]");
2212   case Match_MRS:
2213     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2214                  "expected readable system register");
2215   case Match_MSR:
2216     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2217                  "expected writable system register or pstate");
2218   case Match_NamedImm_at:
2219     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2220                 "expected symbolic 'at' operand: s1e[0-3][rw] or s12e[01][rw]");
2221   case Match_NamedImm_dbarrier:
2222     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2223              "expected integer in range [0, 15] or symbolic barrier operand");
2224   case Match_NamedImm_dc:
2225     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2226                  "expected symbolic 'dc' operand");
2227   case Match_NamedImm_ic:
2228     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2229                  "expected 'ic' operand: 'ialluis', 'iallu' or 'ivau'");
2230   case Match_NamedImm_isb:
2231     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2232                  "expected integer in range [0, 15] or 'sy'");
2233   case Match_NamedImm_prefetch:
2234     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2235                  "expected prefetch hint: p(ld|st|i)l[123](strm|keep)");
2236   case Match_NamedImm_tlbi:
2237     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2238                  "expected translation buffer invalidation operand");
2239   case Match_UImm16:
2240     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2241                  "expected integer in range [0, 65535]");
2242   case Match_UImm3:
2243     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2244                  "expected integer in range [0, 7]");
2245   case Match_UImm4:
2246     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2247                  "expected integer in range [0, 15]");
2248   case Match_UImm5:
2249     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2250                  "expected integer in range [0, 31]");
2251   case Match_UImm6:
2252     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2253                  "expected integer in range [0, 63]");
2254   case Match_UImm7:
2255     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2256                  "expected integer in range [0, 127]");
2257   case Match_Width32:
2258     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2259                  "expected integer in range [<lsb>, 31]");
2260   case Match_Width64:
2261     return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2262                  "expected integer in range [<lsb>, 63]");
2263   case Match_ShrImm8:
2264     return Error(((AArch64Operand *)Operands[ErrorInfo])->getStartLoc(),
2265                  "expected integer in range [1, 8]");
2266   case Match_ShrImm16:
2267     return Error(((AArch64Operand *)Operands[ErrorInfo])->getStartLoc(),
2268                  "expected integer in range [1, 16]");
2269   case Match_ShrImm32:
2270     return Error(((AArch64Operand *)Operands[ErrorInfo])->getStartLoc(),
2271                  "expected integer in range [1, 32]");
2272   case Match_ShrImm64:
2273     return Error(((AArch64Operand *)Operands[ErrorInfo])->getStartLoc(),
2274                  "expected integer in range [1, 64]");
2275   }
2276
2277   llvm_unreachable("Implement any new match types added!");
2278   return true;
2279 }
2280
2281 void AArch64Operand::print(raw_ostream &OS) const {
2282   switch (Kind) {
2283   case k_CondCode:
2284     OS << "<CondCode: " << CondCode.Code << ">";
2285     break;
2286   case k_FPImmediate:
2287     OS << "<fpimm: " << FPImm.Val << ">";
2288     break;
2289   case k_ImmWithLSL:
2290     OS << "<immwithlsl: imm=" << ImmWithLSL.Val
2291        << ", shift=" << ImmWithLSL.ShiftAmount << ">";
2292     break;
2293   case k_Immediate:
2294     getImm()->print(OS);
2295     break;
2296   case k_Register:
2297     OS << "<register " << getReg() << '>';
2298     break;
2299   case k_Token:
2300     OS << '\'' << getToken() << '\'';
2301     break;
2302   case k_ShiftExtend:
2303     OS << "<shift: type=" << ShiftExtend.ShiftType
2304        << ", amount=" << ShiftExtend.Amount << ">";
2305     break;
2306   case k_SysReg: {
2307     StringRef Name(SysReg.Data, SysReg.Length);
2308     OS << "<sysreg: " << Name << '>';
2309     break;
2310   }
2311   default:
2312     llvm_unreachable("No idea how to print this kind of operand");
2313     break;
2314   }
2315 }
2316
2317 void AArch64Operand::dump() const {
2318   print(errs());
2319 }
2320
2321
2322 /// Force static initialization.
2323 extern "C" void LLVMInitializeAArch64AsmParser() {
2324   RegisterMCAsmParser<AArch64AsmParser> X(TheAArch64Target);
2325 }
2326
2327 #define GET_REGISTER_MATCHER
2328 #define GET_MATCHER_IMPLEMENTATION
2329 #include "AArch64GenAsmMatcher.inc"