1 //==- AArch64AsmParser.cpp - Parse AArch64 assembly to MCInst instructions -==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the (GNU-style) assembly parser for the AArch64
13 //===----------------------------------------------------------------------===//
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"
43 class AArch64AsmParser : public MCTargetAsmParser {
47 #define GET_ASSEMBLER_HEADER
48 #include "AArch64GenAsmMatcher.inc"
51 enum AArch64MatchResultTy {
52 Match_FirstAArch64 = FIRST_TARGET_MATCH_RESULT_TY,
53 #define GET_OPERAND_DIAGNOSTIC_TYPES
54 #include "AArch64GenAsmMatcher.inc"
57 AArch64AsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser,
58 const MCInstrInfo &MII)
59 : MCTargetAsmParser(), STI(_STI), Parser(_Parser) {
60 MCAsmParserExtension::Initialize(_Parser);
62 // Initialize the set of available features.
63 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
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,
70 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
72 bool ParseDirective(AsmToken DirectiveID);
73 bool ParseDirectiveTLSDescCall(SMLoc L);
74 bool ParseDirectiveWord(unsigned Size, SMLoc L);
76 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
77 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
78 MCStreamer&Out, unsigned &ErrorInfo,
79 bool MatchingInlineAsm);
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
85 ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
88 OperandMatchResultTy ParseImmediate(const MCExpr *&ExprVal);
90 OperandMatchResultTy ParseRelocPrefix(AArch64MCExpr::VariantKind &RefKind);
93 ParseNEONLane(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
97 ParseRegister(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
101 ParseImmWithLSLOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
104 ParseCondCodeOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
107 ParseCRxOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
110 ParseFPImmOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
112 template<typename SomeNamedImmMapper> OperandMatchResultTy
113 ParseNamedImmOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
114 return ParseNamedImmOperand(SomeNamedImmMapper(), Operands);
118 ParseNamedImmOperand(const NamedImmMapper &Mapper,
119 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
122 ParseLSXAddressOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
125 ParseShiftExtend(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
128 ParseSysRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
130 bool validateInstruction(MCInst &Inst,
131 const SmallVectorImpl<MCParsedAsmOperand*> &Operands);
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.
138 IdentifyRegister(unsigned &RegNum, SMLoc &RegEndLoc, StringRef &LayoutSpec,
139 SMLoc &LayoutLoc) const;
147 /// Instances of this class represent a parsed AArch64 machine instruction.
148 class AArch64Operand : public MCParsedAsmOperand {
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
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.
162 SMLoc StartLoc, EndLoc;
164 struct ImmWithLSLOp {
166 unsigned ShiftAmount;
171 A64CC::CondCodes Code;
186 struct ShiftExtendOp {
187 A64SE::ShiftExtSpecifiers ShiftType;
203 struct ImmWithLSLOp ImmWithLSL;
204 struct CondCodeOp CondCode;
205 struct FPImmOp FPImm;
208 struct ShiftExtendOp ShiftExtend;
209 struct SysRegOp SysReg;
213 AArch64Operand(KindTy K, SMLoc S, SMLoc E)
214 : MCParsedAsmOperand(), Kind(K), StartLoc(S), EndLoc(E) {}
217 AArch64Operand(const AArch64Operand &o) : MCParsedAsmOperand() {
220 SMLoc getStartLoc() const { return StartLoc; }
221 SMLoc getEndLoc() const { return EndLoc; }
222 void print(raw_ostream&) const;
225 StringRef getToken() const {
226 assert(Kind == k_Token && "Invalid access!");
227 return StringRef(Tok.Data, Tok.Length);
230 unsigned getReg() const {
231 assert((Kind == k_Register || Kind == k_WrappedRegister)
232 && "Invalid access!");
236 const MCExpr *getImm() const {
237 assert(Kind == k_Immediate && "Invalid access!");
241 A64CC::CondCodes getCondCode() const {
242 assert(Kind == k_CondCode && "Invalid access!");
243 return CondCode.Code;
246 static bool isNonConstantExpr(const MCExpr *E,
247 AArch64MCExpr::VariantKind &Variant) {
248 if (const AArch64MCExpr *A64E = dyn_cast<AArch64MCExpr>(E)) {
249 Variant = A64E->getKind();
251 } else if (!isa<MCConstantExpr>(E)) {
252 Variant = AArch64MCExpr::VK_AARCH64_None;
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; }
270 bool isAddSubImmLSL0() const {
271 if (!isImmWithLSL()) return false;
272 if (ImmWithLSL.ShiftAmount != 0) return false;
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;
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;
289 bool isAddSubImmLSL12() const {
290 if (!isImmWithLSL()) return false;
291 if (ImmWithLSL.ShiftAmount != 12) return false;
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;
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;
304 template<unsigned MemSize, unsigned RmSize> bool isAddrRegExtend() const {
305 if (!isShiftOrExtend()) return false;
307 A64SE::ShiftExtSpecifiers Ext = ShiftExtend.ShiftType;
308 if (RmSize == 32 && !(Ext == A64SE::UXTW || Ext == A64SE::SXTW))
311 if (RmSize == 64 && !(Ext == A64SE::LSL || Ext == A64SE::SXTX))
314 return ShiftExtend.Amount == Log2_32(MemSize) || ShiftExtend.Amount == 0;
317 bool isAdrpLabel() const {
318 if (!isImm()) return false;
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;
328 return isLabel<21, 4096>();
331 template<unsigned RegWidth> bool isBitfieldWidth() const {
332 if (!isImm()) return false;
334 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
335 if (!CE) return false;
337 return CE->getValue() >= 1 && CE->getValue() <= RegWidth;
340 template<int RegWidth>
341 bool isCVTFixedPos() const {
342 if (!isImm()) return false;
344 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
345 if (!CE) return false;
347 return CE->getValue() >= 1 && CE->getValue() <= RegWidth;
350 bool isFMOVImm() const {
351 if (!isFPImm()) return false;
353 APFloat RealVal(FPImm.Val);
355 return A64Imms::isFPImm(RealVal, ImmVal);
358 bool isFPZero() const {
359 if (!isFPImm()) return false;
361 APFloat RealVal(FPImm.Val);
362 return RealVal.isPosZero();
365 template<unsigned field_width, unsigned scale>
366 bool isLabel() const {
367 if (!isImm()) return false;
369 if (dyn_cast<MCSymbolRefExpr>(Imm.Val)) {
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;
378 // N.b. this disallows explicit relocation specifications via an
379 // AArch64MCExpr. Users needing that behaviour
383 bool isLane1() const {
384 if (!isImm()) return false;
386 // Because it's come through custom assembly parsing, it must always be a
387 // constant expression.
388 return cast<MCConstantExpr>(getImm())->getValue() == 1;
391 bool isLoadLitLabel() const {
392 if (!isImm()) return false;
394 AArch64MCExpr::VariantKind Variant;
395 if (isNonConstantExpr(getImm(), Variant)) {
396 return Variant == AArch64MCExpr::VK_AARCH64_None
397 || Variant == AArch64MCExpr::VK_AARCH64_GOTTPREL;
400 return isLabel<19, 4>();
403 template<unsigned RegWidth> bool isLogicalImm() const {
404 if (!isImm()) return false;
406 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val);
407 if (!CE) return false;
410 return A64Imms::isLogicalImm(RegWidth, CE->getValue(), Bits);
413 template<unsigned RegWidth> bool isLogicalImmMOV() const {
414 if (!isLogicalImm<RegWidth>()) return false;
416 const MCConstantExpr *CE = cast<MCConstantExpr>(Imm.Val);
418 // The move alias for ORR is only valid if the immediate cannot be
419 // represented with a move (immediate) instruction; they take priority.
421 return !A64Imms::isMOVZImm(RegWidth, CE->getValue(), UImm16, Shift)
422 && !A64Imms::isMOVNImm(RegWidth, CE->getValue(), UImm16, Shift);
425 template<int MemSize>
426 bool isOffsetUImm12() const {
427 if (!isImm()) return false;
429 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
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.
438 int64_t Val = CE->getValue();
440 // Must be a multiple of the access size in bytes.
441 if ((Val & (MemSize - 1)) != 0) return false;
443 // Must be 12-bit unsigned
444 return Val >= 0 && Val <= 0xfff * MemSize;
447 template<A64SE::ShiftExtSpecifiers SHKind, bool is64Bit>
448 bool isShift() const {
449 if (!isShiftOrExtend()) return false;
451 if (ShiftExtend.ShiftType != SHKind)
454 return is64Bit ? ShiftExtend.Amount <= 63 : ShiftExtend.Amount <= 31;
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,
467 const unsigned NumModifiers = llvm::array_lengthof(PermittedModifiers);
469 return isMoveWideImm(32, PermittedModifiers, NumModifiers);
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,
485 const unsigned NumModifiers = llvm::array_lengthof(PermittedModifiers);
487 return isMoveWideImm(64, PermittedModifiers, NumModifiers);
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,
503 const unsigned NumModifiers = llvm::array_lengthof(PermittedModifiers);
505 return isMoveWideImm(32, PermittedModifiers, NumModifiers);
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,
525 const unsigned NumModifiers = llvm::array_lengthof(PermittedModifiers);
527 return isMoveWideImm(64, PermittedModifiers, NumModifiers);
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,
540 const unsigned NumModifiers = llvm::array_lengthof(PermittedModifiers);
542 return isMoveWideImm(32, PermittedModifiers, NumModifiers);
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,
557 const unsigned NumModifiers = llvm::array_lengthof(PermittedModifiers);
559 return isMoveWideImm(64, PermittedModifiers, NumModifiers);
562 bool isMoveWideImm(unsigned RegWidth,
563 const AArch64MCExpr::VariantKind *PermittedModifiers,
564 unsigned NumModifiers) const {
565 if (!isImmWithLSL()) return false;
567 if (ImmWithLSL.ShiftAmount % 16 != 0) return false;
568 if (ImmWithLSL.ShiftAmount >= RegWidth) return false;
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;
575 for (unsigned i = 0; i < NumModifiers; ++i)
576 if (PermittedModifiers[i] == Modifier) return true;
581 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmWithLSL.Val);
582 return CE && CE->getValue() >= 0 && CE->getValue() <= 0xffff;
585 template<int RegWidth, bool (*isValidImm)(int, uint64_t, int&, int&)>
586 bool isMoveWideMovAlias() const {
587 if (!isImm()) return false;
589 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
590 if (!CE) return false;
593 uint64_t Value = CE->getValue();
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
598 if (RegWidth == 32) {
599 if ((Value >> 32) != 0 && (Value >> 32) != 0xffffffff)
602 Value &= 0xffffffffULL;
605 return isValidImm(RegWidth, Value, UImm16, Shift);
608 bool isMSRWithReg() const {
609 if (!isSysReg()) return false;
611 bool IsKnownRegister;
612 StringRef Name(SysReg.Data, SysReg.Length);
613 A64SysReg::MSRMapper().fromString(Name, IsKnownRegister);
615 return IsKnownRegister;
618 bool isMSRPState() const {
619 if (!isSysReg()) return false;
621 bool IsKnownRegister;
622 StringRef Name(SysReg.Data, SysReg.Length);
623 A64PState::PStateMapper().fromString(Name, IsKnownRegister);
625 return IsKnownRegister;
629 if (!isSysReg()) return false;
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);
636 return IsKnownRegister;
639 bool isPRFM() const {
640 if (!isImm()) return false;
642 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
647 return CE->getValue() >= 0 && CE->getValue() <= 31;
650 template<A64SE::ShiftExtSpecifiers SHKind> bool isRegExtend() const {
651 if (!isShiftOrExtend()) return false;
653 if (ShiftExtend.ShiftType != SHKind)
656 return ShiftExtend.Amount <= 4;
659 bool isRegExtendLSL() const {
660 if (!isShiftOrExtend()) return false;
662 if (ShiftExtend.ShiftType != A64SE::LSL)
665 return !ShiftExtend.ImplicitAmount && ShiftExtend.Amount <= 4;
668 // if 0 < value <= w, return true
669 bool isShrFixedWidth(int w) const {
672 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
675 int64_t Value = CE->getValue();
676 return Value > 0 && Value <= w;
679 bool isShrImm8() const { return isShrFixedWidth(8); }
681 bool isShrImm16() const { return isShrFixedWidth(16); }
683 bool isShrImm32() const { return isShrFixedWidth(32); }
685 bool isShrImm64() const { return isShrFixedWidth(64); }
687 bool isNeonMovImmShiftLSL() const {
688 if (!isShiftOrExtend())
691 if (ShiftExtend.ShiftType != A64SE::LSL)
694 // Valid shift amount is 0, 8, 16 and 24.
695 return ShiftExtend.Amount % 8 == 0 && ShiftExtend.Amount <= 24;
698 bool isNeonMovImmShiftLSLH() const {
699 if (!isShiftOrExtend())
702 if (ShiftExtend.ShiftType != A64SE::LSL)
705 // Valid shift amount is 0 and 8.
706 return ShiftExtend.Amount == 0 || ShiftExtend.Amount == 8;
709 bool isNeonMovImmShiftMSL() const {
710 if (!isShiftOrExtend())
713 if (ShiftExtend.ShiftType != A64SE::MSL)
716 // Valid shift amount is 8 and 16.
717 return ShiftExtend.Amount == 8 || ShiftExtend.Amount == 16;
720 template <int MemSize> bool isSImm7Scaled() const {
724 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
725 if (!CE) return false;
727 int64_t Val = CE->getValue();
728 if (Val % MemSize != 0) return false;
732 return Val >= -64 && Val < 64;
735 template<int BitWidth>
736 bool isSImm() const {
737 if (!isImm()) return false;
739 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
740 if (!CE) return false;
742 return CE->getValue() >= -(1LL << (BitWidth - 1))
743 && CE->getValue() < (1LL << (BitWidth - 1));
746 template<int bitWidth>
747 bool isUImm() const {
748 if (!isImm()) return false;
750 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
751 if (!CE) return false;
753 return CE->getValue() >= 0 && CE->getValue() < (1LL << bitWidth);
756 bool isUImm() const {
757 if (!isImm()) return false;
759 return isa<MCConstantExpr>(getImm());
762 bool isNeonUImm64Mask() const {
766 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
770 uint64_t Value = CE->getValue();
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)
779 static AArch64Operand *CreateImmWithLSL(const MCExpr *Val,
780 unsigned ShiftAmount,
783 AArch64Operand *Op = new AArch64Operand(k_ImmWithLSL, S, E);
784 Op->ImmWithLSL.Val = Val;
785 Op->ImmWithLSL.ShiftAmount = ShiftAmount;
786 Op->ImmWithLSL.ImplicitAmount = ImplicitAmount;
790 static AArch64Operand *CreateCondCode(A64CC::CondCodes Code,
792 AArch64Operand *Op = new AArch64Operand(k_CondCode, S, E);
793 Op->CondCode.Code = Code;
797 static AArch64Operand *CreateFPImm(double Val,
799 AArch64Operand *Op = new AArch64Operand(k_FPImmediate, S, E);
804 static AArch64Operand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
805 AArch64Operand *Op = new AArch64Operand(k_Immediate, S, E);
810 static AArch64Operand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
811 AArch64Operand *Op = new AArch64Operand(k_Register, S, E);
812 Op->Reg.RegNum = RegNum;
816 static AArch64Operand *CreateWrappedReg(unsigned RegNum, SMLoc S, SMLoc E) {
817 AArch64Operand *Op = new AArch64Operand(k_WrappedRegister, S, E);
818 Op->Reg.RegNum = RegNum;
822 static AArch64Operand *CreateShiftExtend(A64SE::ShiftExtSpecifiers ShiftTyp,
826 AArch64Operand *Op = new AArch64Operand(k_ShiftExtend, S, E);
827 Op->ShiftExtend.ShiftType = ShiftTyp;
828 Op->ShiftExtend.Amount = Amount;
829 Op->ShiftExtend.ImplicitAmount = ImplicitAmount;
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();
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();
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()));
853 Inst.addOperand(MCOperand::CreateExpr(Expr));
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));
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));
870 void addBFXWidthOperands(MCInst &Inst, unsigned N) const {
871 assert(N == 1 && "Invalid number of operands!");
873 uint64_t LSB = Inst.getOperand(Inst.getNumOperands()-1).getImm();
874 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
876 Inst.addOperand(MCOperand::CreateImm(LSB + CE->getValue() - 1));
879 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
880 assert(N == 1 && "Invalid number of operands!");
881 Inst.addOperand(MCOperand::CreateImm(getCondCode()));
884 void addCVTFixedPosOperands(MCInst &Inst, unsigned N) const {
885 assert(N == 1 && "Invalid number of operands!");
887 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
888 Inst.addOperand(MCOperand::CreateImm(64 - CE->getValue()));
891 void addFMOVImmOperands(MCInst &Inst, unsigned N) const {
892 assert(N == 1 && "Invalid number of operands!");
894 APFloat RealVal(FPImm.Val);
896 A64Imms::isFPImm(RealVal, ImmVal);
898 Inst.addOperand(MCOperand::CreateImm(ImmVal));
901 void addFPZeroOperands(MCInst &Inst, unsigned N) const {
902 assert(N == 1 && "Invalid number of operands");
903 Inst.addOperand(MCOperand::CreateImm(0));
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));
912 void addRegOperands(MCInst &Inst, unsigned N) const {
913 assert(N == 1 && "Invalid number of operands!");
914 Inst.addOperand(MCOperand::CreateReg(getReg()));
917 void addImmOperands(MCInst &Inst, unsigned N) const {
918 assert(N == 1 && "Invalid number of operands!");
919 addExpr(Inst, getImm());
922 template<int MemSize>
923 void addSImm7ScaledOperands(MCInst &Inst, unsigned N) const {
924 assert(N == 1 && "Invalid number of operands!");
926 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
927 uint64_t Val = CE->getValue() / MemSize;
928 Inst.addOperand(MCOperand::CreateImm(Val & 0x7f));
931 template<int BitWidth>
932 void addSImmOperands(MCInst &Inst, unsigned N) const {
933 assert(N == 1 && "Invalid number of operands!");
935 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
936 uint64_t Val = CE->getValue();
937 Inst.addOperand(MCOperand::CreateImm(Val & ((1ULL << BitWidth) - 1)));
940 void addImmWithLSLOperands(MCInst &Inst, unsigned N) const {
941 assert (N == 1 && "Invalid number of operands!");
943 addExpr(Inst, ImmWithLSL.Val);
946 template<unsigned field_width, unsigned scale>
947 void addLabelOperands(MCInst &Inst, unsigned N) const {
948 assert(N == 1 && "Invalid number of operands!");
950 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val);
953 addExpr(Inst, Imm.Val);
957 int64_t Val = CE->getValue();
958 assert(Val % scale == 0 && "Unaligned immediate in instruction");
961 Inst.addOperand(MCOperand::CreateImm(Val & ((1LL << field_width) - 1)));
964 template<int MemSize>
965 void addOffsetUImm12Operands(MCInst &Inst, unsigned N) const {
966 assert(N == 1 && "Invalid number of operands!");
968 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
969 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / MemSize));
971 Inst.addOperand(MCOperand::CreateExpr(getImm()));
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);
981 A64Imms::isLogicalImm(RegWidth, CE->getValue(), Bits);
983 Inst.addOperand(MCOperand::CreateImm(Bits));
986 void addMRSOperands(MCInst &Inst, unsigned N) const {
987 assert(N == 1 && "Invalid number of operands!");
990 StringRef Name(SysReg.Data, SysReg.Length);
991 uint32_t Bits = A64SysReg::MRSMapper().fromString(Name, Valid);
993 Inst.addOperand(MCOperand::CreateImm(Bits));
996 void addMSRWithRegOperands(MCInst &Inst, unsigned N) const {
997 assert(N == 1 && "Invalid number of operands!");
1000 StringRef Name(SysReg.Data, SysReg.Length);
1001 uint32_t Bits = A64SysReg::MSRMapper().fromString(Name, Valid);
1003 Inst.addOperand(MCOperand::CreateImm(Bits));
1006 void addMSRPStateOperands(MCInst &Inst, unsigned N) const {
1007 assert(N == 1 && "Invalid number of operands!");
1010 StringRef Name(SysReg.Data, SysReg.Length);
1011 uint32_t Bits = A64PState::PStateMapper().fromString(Name, Valid);
1013 Inst.addOperand(MCOperand::CreateImm(Bits));
1016 void addMoveWideImmOperands(MCInst &Inst, unsigned N) const {
1017 assert(N == 2 && "Invalid number of operands!");
1019 addExpr(Inst, ImmWithLSL.Val);
1021 AArch64MCExpr::VariantKind Variant;
1022 if (!isNonConstantExpr(ImmWithLSL.Val, Variant)) {
1023 Inst.addOperand(MCOperand::CreateImm(ImmWithLSL.ShiftAmount / 16));
1027 // We know it's relocated
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));
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));
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));
1056 case AArch64MCExpr::VK_AARCH64_ABS_G3:
1057 Inst.addOperand(MCOperand::CreateImm(3));
1059 default: llvm_unreachable("Inappropriate move wide relocation");
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!");
1068 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
1069 uint64_t Value = CE->getValue();
1071 if (RegWidth == 32) {
1072 Value &= 0xffffffffULL;
1075 bool Valid = isValidImm(RegWidth, Value, UImm16, Shift);
1077 assert(Valid && "Invalid immediates should have been weeded out by now");
1079 Inst.addOperand(MCOperand::CreateImm(UImm16));
1080 Inst.addOperand(MCOperand::CreateImm(Shift));
1083 void addPRFMOperands(MCInst &Inst, unsigned N) const {
1084 assert(N == 1 && "Invalid number of operands!");
1086 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
1087 assert(CE->getValue() >= 0 && CE->getValue() <= 31
1088 && "PRFM operand should be 5-bits");
1090 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1093 // For Add-sub (extended register) operands.
1094 void addRegExtendOperands(MCInst &Inst, unsigned N) const {
1095 assert(N == 1 && "Invalid number of operands!");
1097 Inst.addOperand(MCOperand::CreateImm(ShiftExtend.Amount));
1100 // For Vector Immediates shifted imm operands.
1101 void addNeonMovImmShiftLSLOperands(MCInst &Inst, unsigned N) const {
1102 assert(N == 1 && "Invalid number of operands!");
1104 if (ShiftExtend.Amount % 8 != 0 || ShiftExtend.Amount > 24)
1105 llvm_unreachable("Invalid shift amount for vector immediate inst.");
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));
1112 void addNeonMovImmShiftLSLHOperands(MCInst &Inst, unsigned N) const {
1113 assert(N == 1 && "Invalid number of operands!");
1115 if (ShiftExtend.Amount != 0 && ShiftExtend.Amount != 8)
1116 llvm_unreachable("Invalid shift amount for vector immediate inst.");
1118 // Encode LSLH shift amount 0, 8 as 0, 1.
1119 int64_t Imm = ShiftExtend.Amount / 8;
1120 Inst.addOperand(MCOperand::CreateImm(Imm));
1123 void addNeonMovImmShiftMSLOperands(MCInst &Inst, unsigned N) const {
1124 assert(N == 1 && "Invalid number of operands!");
1126 if (ShiftExtend.Amount != 8 && ShiftExtend.Amount != 16)
1127 llvm_unreachable("Invalid shift amount for vector immediate inst.");
1129 // Encode MSL shift amount 8, 16 as 0, 1.
1130 int64_t Imm = ShiftExtend.Amount / 8 - 1;
1131 Inst.addOperand(MCOperand::CreateImm(Imm));
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);
1140 void addAddrRegExtendOperands(MCInst &Inst, unsigned N,
1141 unsigned MemSize) const {
1142 assert(N == 1 && "Invalid number of operands!");
1144 // First bit of Option is set in instruction classes, the high two bits are
1146 unsigned OptionHi = 0;
1147 switch (ShiftExtend.ShiftType) {
1157 llvm_unreachable("Invalid extend type for register offset");
1161 if (MemSize == 1 && !ShiftExtend.ImplicitAmount)
1163 else if (MemSize != 1 && ShiftExtend.Amount != 0)
1166 Inst.addOperand(MCOperand::CreateImm((OptionHi << 1) | S));
1168 void addShiftOperands(MCInst &Inst, unsigned N) const {
1169 assert(N == 1 && "Invalid number of operands!");
1171 Inst.addOperand(MCOperand::CreateImm(ShiftExtend.Amount));
1174 void addNeonUImm64MaskOperands(MCInst &Inst, unsigned N) const {
1175 assert(N == 1 && "Invalid number of operands!");
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();
1182 for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
1183 Imm |= (Value & 1) << i;
1185 Inst.addOperand(MCOperand::CreateImm(Imm));
1189 } // end anonymous namespace.
1191 AArch64AsmParser::OperandMatchResultTy
1192 AArch64AsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1193 StringRef Mnemonic) {
1195 // See if the operand has a custom parser
1196 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1198 // It could either succeed, fail or just not care.
1199 if (ResTy != MatchOperand_NoMatch)
1202 switch (getLexer().getKind()) {
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);
1210 // We can only continue if no tokens were eaten.
1211 if (GotShift != MatchOperand_NoMatch)
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");
1220 if (GotReg == MatchOperand_Success) {
1221 if (Parser.getTok().is(AsmToken::LBrac))
1222 return ParseNEONLane(Operands, NumLanes);
1224 return MatchOperand_Success;
1227 // ... or it might be a symbolish thing
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();
1238 const MCExpr *ImmVal = 0;
1240 if (ParseImmediate(ImmVal) != MatchOperand_Success)
1241 return MatchOperand_ParseFail;
1243 EndLoc = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1244 Operands.push_back(AArch64Operand::CreateImm(ImmVal, StartLoc, EndLoc));
1245 return MatchOperand_Success;
1247 case AsmToken::Hash: { // Immediates
1248 SMLoc StartLoc = Parser.getTok().getLoc();
1250 const MCExpr *ImmVal = 0;
1253 if (ParseImmediate(ImmVal) != MatchOperand_Success)
1254 return MatchOperand_ParseFail;
1256 EndLoc = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1257 Operands.push_back(AArch64Operand::CreateImm(ImmVal, StartLoc, EndLoc));
1258 return MatchOperand_Success;
1260 case AsmToken::LBrac: {
1261 SMLoc Loc = Parser.getTok().getLoc();
1262 Operands.push_back(AArch64Operand::CreateToken("[", Loc));
1263 Parser.Lex(); // Eat '['
1265 // There's no comma after a '[', so we can parse the next operand
1267 return ParseOperand(Operands, Mnemonic);
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;
1276 AArch64AsmParser::OperandMatchResultTy
1277 AArch64AsmParser::ParseImmediate(const MCExpr *&ExprVal) {
1278 if (getLexer().is(AsmToken::Colon)) {
1279 AArch64MCExpr::VariantKind RefKind;
1281 OperandMatchResultTy ResTy = ParseRelocPrefix(RefKind);
1282 if (ResTy != MatchOperand_Success)
1285 const MCExpr *SubExprVal;
1286 if (getParser().parseExpression(SubExprVal))
1287 return MatchOperand_ParseFail;
1289 ExprVal = AArch64MCExpr::Create(RefKind, SubExprVal, getContext());
1290 return MatchOperand_Success;
1293 // No weird AArch64MCExpr prefix
1294 return getParser().parseExpression(ExprVal)
1295 ? MatchOperand_ParseFail : MatchOperand_Success;
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();
1305 assert(Parser.getTok().is(AsmToken::LBrac) && "inappropriate operand");
1306 Operands.push_back(AArch64Operand::CreateToken("[", Loc));
1307 Parser.Lex(); // Eat '['
1309 if (Parser.getTok().isNot(AsmToken::Integer)) {
1310 Error(Parser.getTok().getLoc(), "expected lane number");
1311 return MatchOperand_ParseFail;
1314 if (Parser.getTok().getIntVal() >= NumLanes) {
1315 Error(Parser.getTok().getLoc(), "lane number incompatible with layout");
1316 return MatchOperand_ParseFail;
1319 const MCExpr *Lane = MCConstantExpr::Create(Parser.getTok().getIntVal(),
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));
1327 if (Parser.getTok().isNot(AsmToken::RBrac)) {
1328 Error(Parser.getTok().getLoc(), "expected ']' after lane");
1329 return MatchOperand_ParseFail;
1332 Operands.push_back(AArch64Operand::CreateToken("]", Loc));
1333 Parser.Lex(); // Eat ']'
1335 return MatchOperand_Success;
1338 AArch64AsmParser::OperandMatchResultTy
1339 AArch64AsmParser::ParseRelocPrefix(AArch64MCExpr::VariantKind &RefKind) {
1340 assert(getLexer().is(AsmToken::Colon) && "expected a ':'");
1343 if (getLexer().isNot(AsmToken::Identifier)) {
1344 Error(Parser.getTok().getLoc(),
1345 "expected relocation specifier in operand after ':'");
1346 return MatchOperand_ParseFail;
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);
1388 if (RefKind == AArch64MCExpr::VK_AARCH64_None) {
1389 Error(Parser.getTok().getLoc(),
1390 "expected relocation specifier in operand after ':'");
1391 return MatchOperand_ParseFail;
1393 Parser.Lex(); // Eat identifier
1395 if (getLexer().isNot(AsmToken::Colon)) {
1396 Error(Parser.getTok().getLoc(),
1397 "expected ':' after relocation specifier");
1398 return MatchOperand_ParseFail;
1401 return MatchOperand_Success;
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;
1411 SMLoc S = Parser.getTok().getLoc();
1412 Parser.Lex(); // Eat '#'
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;
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") {
1431 if (Parser.getTok().is(AsmToken::Hash)) {
1434 if (Parser.getTok().isNot(AsmToken::Integer)) {
1435 Error(Parser.getTok().getLoc(), "only 'lsl #+N' valid after immediate");
1436 return MatchOperand_ParseFail;
1441 int64_t ShiftAmount = Parser.getTok().getIntVal();
1443 if (ShiftAmount < 0) {
1444 Error(Parser.getTok().getLoc(), "positive shift amount required");
1445 return MatchOperand_ParseFail;
1447 Parser.Lex(); // Eat the number
1449 SMLoc E = Parser.getTok().getLoc();
1450 Operands.push_back(AArch64Operand::CreateImmWithLSL(Imm, ShiftAmount,
1452 return MatchOperand_Success;
1456 AArch64AsmParser::OperandMatchResultTy
1457 AArch64AsmParser::ParseCondCodeOperand(
1458 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1459 if (Parser.getTok().isNot(AsmToken::Identifier))
1460 return MatchOperand_NoMatch;
1462 StringRef Tok = Parser.getTok().getIdentifier();
1463 A64CC::CondCodes CondCode = A64StringToCondCode(Tok);
1465 if (CondCode == A64CC::Invalid)
1466 return MatchOperand_NoMatch;
1468 SMLoc S = Parser.getTok().getLoc();
1469 Parser.Lex(); // Eat condition code
1470 SMLoc E = Parser.getTok().getLoc();
1472 Operands.push_back(AArch64Operand::CreateCondCode(CondCode, S, E));
1473 return MatchOperand_Success;
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;
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;
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;
1499 const MCExpr *CRImm = MCConstantExpr::Create(CRNum, getContext());
1502 SMLoc E = Parser.getTok().getLoc();
1504 Operands.push_back(AArch64Operand::CreateImm(CRImm, S, E));
1505 return MatchOperand_Success;
1508 AArch64AsmParser::OperandMatchResultTy
1509 AArch64AsmParser::ParseFPImmOperand(
1510 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
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;
1516 SMLoc S = Parser.getTok().getLoc();
1517 Parser.Lex(); // Eat '#'
1519 bool Negative = false;
1520 if (Parser.getTok().is(AsmToken::Minus)) {
1522 Parser.Lex(); // Eat '-'
1523 } else if (Parser.getTok().is(AsmToken::Plus)) {
1524 Parser.Lex(); // Eat '+'
1527 if (Parser.getTok().isNot(AsmToken::Real)) {
1528 Error(S, "Expected floating-point immediate");
1529 return MatchOperand_ParseFail;
1532 APFloat RealVal(APFloat::IEEEdouble, Parser.getTok().getString());
1533 if (Negative) RealVal.changeSign();
1534 double DblVal = RealVal.convertToDouble();
1536 Parser.Lex(); // Eat real number
1537 SMLoc E = Parser.getTok().getLoc();
1539 Operands.push_back(AArch64Operand::CreateFPImm(DblVal, S, E));
1540 return MatchOperand_Success;
1544 // Automatically generated
1545 static unsigned MatchRegisterName(StringRef Name);
1548 AArch64AsmParser::IdentifyRegister(unsigned &RegNum, SMLoc &RegEndLoc,
1550 SMLoc &LayoutLoc) const {
1551 const AsmToken &Tok = Parser.getTok();
1553 if (Tok.isNot(AsmToken::Identifier))
1556 std::string LowerReg = Tok.getString().lower();
1557 size_t DotPos = LowerReg.find('.');
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);
1568 if (RegNum == AArch64::NoRegister)
1571 SMLoc S = Tok.getLoc();
1572 RegEndLoc = SMLoc::getFromPointer(S.getPointer() + DotPos);
1574 if (DotPos == StringRef::npos) {
1575 Layout = StringRef();
1577 // Everything afterwards needs to be a literal token, expected to be
1578 // '.2d','.b' etc for vector registers.
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")
1592 if (Layout.size() == 0) {
1593 // Malformed register
1601 AArch64AsmParser::OperandMatchResultTy
1602 AArch64AsmParser::ParseRegister(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1603 uint32_t &NumLanes) {
1606 SMLoc RegEndLoc, LayoutLoc;
1607 SMLoc S = Parser.getTok().getLoc();
1609 if (!IdentifyRegister(RegNum, RegEndLoc, Layout, LayoutLoc))
1610 return MatchOperand_NoMatch;
1612 Operands.push_back(AArch64Operand::CreateReg(RegNum, S, RegEndLoc));
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;
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
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;
1632 Operands.push_back(AArch64Operand::CreateToken(Layout, LayoutLoc));
1636 return MatchOperand_Success;
1640 AArch64AsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
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();
1649 if (!IdentifyRegister(RegNo, RegEndLoc, LayoutSpec, LayoutLoc))
1653 EndLoc = Parser.getTok().getLoc();
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();
1668 if (Tok.is(AsmToken::Identifier)) {
1670 uint32_t Code = Mapper.fromString(Tok.getString().lower(), ValidName);
1673 Error(S, "operand specifier not recognised");
1674 return MatchOperand_ParseFail;
1677 Parser.Lex(); // We're done with the identifier. Eat it
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)) {
1686 const MCExpr *ImmVal;
1687 if (ParseImmediate(ImmVal) != MatchOperand_Success)
1688 return MatchOperand_ParseFail;
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;
1696 SMLoc E = Parser.getTok().getLoc();
1697 Operands.push_back(AArch64Operand::CreateImm(ImmVal, S, E));
1698 return MatchOperand_Success;
1701 Error(S, "unexpected operand for instruction");
1702 return MatchOperand_ParseFail;
1705 AArch64AsmParser::OperandMatchResultTy
1706 AArch64AsmParser::ParseSysRegOperand(
1707 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1708 const AsmToken &Tok = Parser.getTok();
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
1714 if (!Tok.is(AsmToken::Identifier)) {
1715 return MatchOperand_NoMatch;
1718 SMLoc S = Tok.getLoc();
1719 Operands.push_back(AArch64Operand::CreateSysReg(Tok.getString(), S));
1720 Parser.Lex(); // Eat identifier
1722 return MatchOperand_Success;
1725 AArch64AsmParser::OperandMatchResultTy
1726 AArch64AsmParser::ParseLSXAddressOperand(
1727 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1728 SMLoc S = Parser.getTok().getLoc();
1731 SMLoc RegEndLoc, LayoutLoc;
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
1738 return MatchOperand_NoMatch;
1740 Parser.Lex(); // Eat register
1742 if (Parser.getTok().is(AsmToken::RBrac)) {
1744 SMLoc E = Parser.getTok().getLoc();
1745 Operands.push_back(AArch64Operand::CreateWrappedReg(RegNum, S, E));
1746 return MatchOperand_Success;
1749 // Otherwise, only ", #0" is valid
1751 if (Parser.getTok().isNot(AsmToken::Comma)) {
1752 Error(Parser.getTok().getLoc(), "expected ',' or ']' after register");
1753 return MatchOperand_ParseFail;
1755 Parser.Lex(); // Eat ','
1757 if (Parser.getTok().isNot(AsmToken::Hash)) {
1758 Error(Parser.getTok().getLoc(), "expected '#0'");
1759 return MatchOperand_ParseFail;
1761 Parser.Lex(); // Eat '#'
1763 if (Parser.getTok().isNot(AsmToken::Integer)
1764 || Parser.getTok().getIntVal() != 0 ) {
1765 Error(Parser.getTok().getLoc(), "expected '#0'");
1766 return MatchOperand_ParseFail;
1768 Parser.Lex(); // Eat '0'
1770 SMLoc E = Parser.getTok().getLoc();
1771 Operands.push_back(AArch64Operand::CreateWrappedReg(RegNum, S, E));
1772 return MatchOperand_Success;
1775 AArch64AsmParser::OperandMatchResultTy
1776 AArch64AsmParser::ParseShiftExtend(
1777 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1778 StringRef IDVal = Parser.getTok().getIdentifier();
1779 std::string LowerID = IDVal.lower();
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);
1798 if (Spec == A64SE::Invalid)
1799 return MatchOperand_NoMatch;
1803 S = Parser.getTok().getLoc();
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
1810 // add x0, x0, x0, uxtb
1811 // is valid, and equivalent to
1812 // add x0, x0, x0, uxtb #0
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,
1819 return MatchOperand_Success;
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;
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;
1837 unsigned Amount = Parser.getTok().getIntVal();
1839 E = Parser.getTok().getLoc();
1841 Operands.push_back(AArch64Operand::CreateShiftExtend(Spec, Amount, false,
1844 return MatchOperand_Success;
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();
1862 if (ImmR != 0 && ImmS >= ImmR) {
1863 return Error(Operands[4]->getStartLoc(),
1864 "requested insert overflows register");
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:
1882 case AArch64::SBFXwwii: case AArch64::UBFXwwii: case AArch64::BFXILwwii:
1887 if (ImmS >= RegWidth || ImmS < ImmR) {
1888 return Error(Operands[4]->getStartLoc(),
1889 "requested extract overflows register");
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");
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");
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");
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");
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('.');
1942 StringRef Mnemonic = Name.substr(0, CondCodePos);
1943 Operands.push_back(AArch64Operand::CreateToken(Mnemonic, NameLoc));
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;
1951 Code = A64StringToCondCode(CondStr);
1953 if (Code == A64CC::Invalid) {
1954 Error(S, "invalid condition code");
1955 Parser.eatToEndOfStatement();
1959 SMLoc DotL = SMLoc::getFromPointer(NameLoc.getPointer() + CondCodePos);
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));
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();
1974 while (getLexer().is(AsmToken::Comma)) {
1975 Parser.Lex(); // Eat the comma.
1977 // Parse and remember the operand.
1978 if (ParseOperand(Operands, Mnemonic)) {
1979 Parser.eatToEndOfStatement();
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.
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));
1998 if (Parser.getTok().is(AsmToken::Exclaim)) {
1999 SMLoc Loc = Parser.getTok().getLoc();
2000 Operands.push_back(AArch64Operand::CreateToken("!", Loc));
2006 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2007 SMLoc Loc = getLexer().getLoc();
2008 Parser.eatToEndOfStatement();
2009 return Error(Loc, "expected comma before next operand");
2012 // Eat the EndOfStatement
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());
2032 /// parseDirectiveWord
2033 /// ::= .word [ expression (, expression)* ]
2034 bool AArch64AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2035 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2037 const MCExpr *Value;
2038 if (getParser().parseExpression(Value))
2041 getParser().getStreamer().EmitValue(Value, Size);
2043 if (getLexer().is(AsmToken::EndOfStatement))
2046 // FIXME: Improve diagnostic.
2047 if (getLexer().isNot(AsmToken::Comma))
2048 return Error(L, "unexpected token in directive");
2057 // parseDirectiveTLSDescCall:
2058 // ::= .tlsdesccall symbol
2059 bool AArch64AsmParser::ParseDirectiveTLSDescCall(SMLoc L) {
2061 if (getParser().parseIdentifier(Name))
2062 return Error(L, "expected symbol after directive");
2064 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2065 const MCSymbolRefExpr *Expr = MCSymbolRefExpr::Create(Sym, getContext());
2068 Inst.setOpcode(AArch64::TLSDESCCALL);
2069 Inst.addOperand(MCOperand::CreateExpr(Expr));
2071 getParser().getStreamer().EmitInstruction(Inst);
2076 bool AArch64AsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
2077 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2078 MCStreamer &Out, unsigned &ErrorInfo,
2079 bool MatchingInlineAsm) {
2081 unsigned MatchResult;
2082 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
2085 if (ErrorInfo != ~0U && ErrorInfo >= Operands.size())
2086 return Error(IDLoc, "too few operands for instruction");
2088 switch (MatchResult) {
2091 if (validateInstruction(Inst, Operands))
2094 Out.EmitInstruction(Inst);
2096 case Match_MissingFeature:
2097 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2099 case Match_InvalidOperand: {
2100 SMLoc ErrorLoc = IDLoc;
2101 if (ErrorInfo != ~0U) {
2102 ErrorLoc = ((AArch64Operand*)Operands[ErrorInfo])->getStartLoc();
2103 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2106 return Error(ErrorLoc, "invalid operand for instruction");
2108 case Match_MnemonicFail:
2109 return Error(IDLoc, "invalid instruction");
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");
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");
2141 return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2142 "expected floating-point constant #0.0 or invalid register type");
2144 return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2145 "expected label or encodable integer pc offset");
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]");
2213 return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2214 "expected readable system register");
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");
2240 return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2241 "expected integer in range [0, 65535]");
2243 return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2244 "expected integer in range [0, 7]");
2246 return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2247 "expected integer in range [0, 15]");
2249 return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2250 "expected integer in range [0, 31]");
2252 return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2253 "expected integer in range [0, 63]");
2255 return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2256 "expected integer in range [0, 127]");
2258 return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2259 "expected integer in range [<lsb>, 31]");
2261 return Error(((AArch64Operand*)Operands[ErrorInfo])->getStartLoc(),
2262 "expected integer in range [<lsb>, 63]");
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]");
2277 llvm_unreachable("Implement any new match types added!");
2281 void AArch64Operand::print(raw_ostream &OS) const {
2284 OS << "<CondCode: " << CondCode.Code << ">";
2287 OS << "<fpimm: " << FPImm.Val << ">";
2290 OS << "<immwithlsl: imm=" << ImmWithLSL.Val
2291 << ", shift=" << ImmWithLSL.ShiftAmount << ">";
2294 getImm()->print(OS);
2297 OS << "<register " << getReg() << '>';
2300 OS << '\'' << getToken() << '\'';
2303 OS << "<shift: type=" << ShiftExtend.ShiftType
2304 << ", amount=" << ShiftExtend.Amount << ">";
2307 StringRef Name(SysReg.Data, SysReg.Length);
2308 OS << "<sysreg: " << Name << '>';
2312 llvm_unreachable("No idea how to print this kind of operand");
2317 void AArch64Operand::dump() const {
2322 /// Force static initialization.
2323 extern "C" void LLVMInitializeAArch64AsmParser() {
2324 RegisterMCAsmParser<AArch64AsmParser> X(TheAArch64Target);
2327 #define GET_REGISTER_MATCHER
2328 #define GET_MATCHER_IMPLEMENTATION
2329 #include "AArch64GenAsmMatcher.inc"