1 //===-- MCAsmParser.cpp - Abstract Asm Parser Interface -------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/MC/MCParser/MCAsmParser.h"
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/MC/MCParser/MCAsmLexer.h"
14 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
15 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/SMLoc.h"
19 #include "llvm/Support/raw_ostream.h"
25 cl::opt
<unsigned> AsmMacroMaxNestingDepth(
26 "asm-macro-max-nesting-depth", cl::init(20), cl::Hidden
,
27 cl::desc("The maximum nesting depth allowed for assembly macros."));
30 MCAsmParser::MCAsmParser() = default;
32 MCAsmParser::~MCAsmParser() = default;
34 void MCAsmParser::setTargetParser(MCTargetAsmParser
&P
) {
35 assert(!TargetParser
&& "Target parser is already initialized!");
37 TargetParser
->Initialize(*this);
40 const AsmToken
&MCAsmParser::getTok() const {
41 return getLexer().getTok();
44 bool MCAsmParser::parseTokenLoc(SMLoc
&Loc
) {
45 Loc
= getTok().getLoc();
49 bool MCAsmParser::parseEOL() {
50 if (getTok().getKind() != AsmToken::EndOfStatement
)
51 return Error(getTok().getLoc(), "expected newline");
56 bool MCAsmParser::parseEOL(const Twine
&Msg
) {
57 if (getTok().getKind() != AsmToken::EndOfStatement
)
58 return Error(getTok().getLoc(), Msg
);
63 bool MCAsmParser::parseToken(AsmToken::TokenKind T
, const Twine
&Msg
) {
64 if (T
== AsmToken::EndOfStatement
)
66 if (getTok().getKind() != T
)
67 return Error(getTok().getLoc(), Msg
);
72 bool MCAsmParser::parseIntToken(int64_t &V
, const Twine
&Msg
) {
73 if (getTok().getKind() != AsmToken::Integer
)
75 V
= getTok().getIntVal();
80 bool MCAsmParser::parseOptionalToken(AsmToken::TokenKind T
) {
81 bool Present
= (getTok().getKind() == T
);
87 bool MCAsmParser::check(bool P
, const Twine
&Msg
) {
88 return check(P
, getTok().getLoc(), Msg
);
91 bool MCAsmParser::check(bool P
, SMLoc Loc
, const Twine
&Msg
) {
93 return Error(Loc
, Msg
);
97 bool MCAsmParser::TokError(const Twine
&Msg
, SMRange Range
) {
98 return Error(getLexer().getLoc(), Msg
, Range
);
101 bool MCAsmParser::Error(SMLoc L
, const Twine
&Msg
, SMRange Range
) {
105 Msg
.toVector(PErr
.Msg
);
107 PendingErrors
.push_back(PErr
);
109 // If we threw this parsing error after a lexing error, this should
110 // supercede the lexing error and so we remove it from the Lexer
111 // before it can propagate
112 if (getTok().is(AsmToken::Error
))
117 bool MCAsmParser::addErrorSuffix(const Twine
&Suffix
) {
118 // Make sure lexing errors have propagated to the parser.
119 if (getTok().is(AsmToken::Error
))
121 for (auto &PErr
: PendingErrors
)
122 Suffix
.toVector(PErr
.Msg
);
126 bool MCAsmParser::parseMany(function_ref
<bool()> parseOne
, bool hasComma
) {
127 if (parseOptionalToken(AsmToken::EndOfStatement
))
132 if (parseOptionalToken(AsmToken::EndOfStatement
))
134 if (hasComma
&& parseToken(AsmToken::Comma
))
140 bool MCAsmParser::parseExpression(const MCExpr
*&Res
) {
142 return parseExpression(Res
, L
);
145 bool MCAsmParser::parseGNUAttribute(SMLoc L
, int64_t &Tag
,
146 int64_t &IntegerValue
) {
147 // Parse a .gnu_attribute with numerical tag and value.
148 StringRef
S(L
.getPointer());
150 TagLoc
= getTok().getLoc();
151 const AsmToken
&Tok
= getTok();
152 if (Tok
.isNot(AsmToken::Integer
))
154 Tag
= Tok
.getIntVal();
155 Lex(); // Eat the Tag
156 Lex(); // Eat the comma
157 if (Tok
.isNot(AsmToken::Integer
))
159 IntegerValue
= Tok
.getIntVal();
160 Lex(); // Eat the IntegerValue
164 void MCParsedAsmOperand::dump() const {
165 // Cannot completely remove virtual function even in release mode.
166 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
167 dbgs() << " " << *this;