LLVM 19.0.0git
MCAsmParser.cpp
Go to the documentation of this file.
1//===-- MCAsmParser.cpp - Abstract Asm Parser Interface -------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/ADT/StringRef.h"
11#include "llvm/ADT/Twine.h"
12#include "llvm/Config/llvm-config.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/SMLoc.h"
20#include <cassert>
21
22using namespace llvm;
23
24namespace llvm {
26 "asm-macro-max-nesting-depth", cl::init(20), cl::Hidden,
27 cl::desc("The maximum nesting depth allowed for assembly macros."));
28}
29
30MCAsmParser::MCAsmParser() = default;
31
33
35 assert(!TargetParser && "Target parser is already initialized!");
36 TargetParser = &P;
37 TargetParser->Initialize(*this);
38}
39
41 return getLexer().getTok();
42}
43
45 Loc = getTok().getLoc();
46 return false;
47}
48
50 if (getTok().getKind() != AsmToken::EndOfStatement)
51 return Error(getTok().getLoc(), "expected newline");
52 Lex();
53 return false;
54}
55
56bool MCAsmParser::parseEOL(const Twine &Msg) {
57 if (getTok().getKind() != AsmToken::EndOfStatement)
58 return Error(getTok().getLoc(), Msg);
59 Lex();
60 return false;
61}
62
65 return parseEOL(Msg);
66 if (getTok().getKind() != T)
67 return Error(getTok().getLoc(), Msg);
68 Lex();
69 return false;
70}
71
72bool MCAsmParser::parseIntToken(int64_t &V, const Twine &Msg) {
73 if (getTok().getKind() != AsmToken::Integer)
74 return TokError(Msg);
75 V = getTok().getIntVal();
76 Lex();
77 return false;
78}
79
81 bool Present = (getTok().getKind() == T);
82 if (Present)
84 return Present;
85}
86
87bool MCAsmParser::check(bool P, const Twine &Msg) {
88 return check(P, getTok().getLoc(), Msg);
89}
90
91bool MCAsmParser::check(bool P, SMLoc Loc, const Twine &Msg) {
92 if (P)
93 return Error(Loc, Msg);
94 return false;
95}
96
97bool MCAsmParser::TokError(const Twine &Msg, SMRange Range) {
98 return Error(getLexer().getLoc(), Msg, Range);
99}
100
101bool MCAsmParser::Error(SMLoc L, const Twine &Msg, SMRange Range) {
102
103 MCPendingError PErr;
104 PErr.Loc = L;
105 Msg.toVector(PErr.Msg);
106 PErr.Range = Range;
107 PendingErrors.push_back(PErr);
108
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))
113 getLexer().Lex();
114 return true;
115}
116
118 // Make sure lexing errors have propagated to the parser.
119 if (getTok().is(AsmToken::Error))
120 Lex();
121 for (auto &PErr : PendingErrors)
122 Suffix.toVector(PErr.Msg);
123 return true;
124}
125
126bool MCAsmParser::parseMany(function_ref<bool()> parseOne, bool hasComma) {
128 return false;
129 while (true) {
130 if (parseOne())
131 return true;
133 return false;
134 if (hasComma && parseToken(AsmToken::Comma))
135 return true;
136 }
137 return false;
138}
139
141 SMLoc L;
142 return parseExpression(Res, L);
143}
144
146 int64_t &IntegerValue) {
147 // Parse a .gnu_attribute with numerical tag and value.
148 StringRef S(L.getPointer());
149 SMLoc TagLoc;
150 TagLoc = getTok().getLoc();
151 const AsmToken &Tok = getTok();
152 if (Tok.isNot(AsmToken::Integer))
153 return false;
154 Tag = Tok.getIntVal();
155 Lex(); // Eat the Tag
156 Lex(); // Eat the comma
157 if (Tok.isNot(AsmToken::Integer))
158 return false;
159 IntegerValue = Tok.getIntVal();
160 Lex(); // Eat the IntegerValue
161 return true;
162}
163
165 // Cannot completely remove virtual function even in release mode.
166#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
167 dbgs() << " " << *this;
168#endif
169}
#define check(cond)
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Target independent representation for an assembler token.
Definition: MCAsmMacro.h:21
SMLoc getLoc() const
Definition: MCAsmLexer.cpp:26
int64_t getIntVal() const
Definition: MCAsmMacro.h:115
bool isNot(TokenKind K) const
Definition: MCAsmMacro.h:83
TokenKind getKind() const
Definition: MCAsmMacro.h:81
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
const AsmToken & getTok() const
Get the current (last) lexed token.
Definition: MCAsmLexer.h:106
const AsmToken & Lex()
Consume the next token from the input stream and return it.
Definition: MCAsmLexer.h:79
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
bool parseMany(function_ref< bool()> parseOne, bool hasComma=true)
bool parseToken(AsmToken::TokenKind T, const Twine &Msg="unexpected token")
Definition: MCAsmParser.cpp:63
bool addErrorSuffix(const Twine &Suffix)
bool check(bool P, const Twine &Msg)
Definition: MCAsmParser.cpp:87
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
const AsmToken & getTok() const
Get the current AsmToken from the stream.
Definition: MCAsmParser.cpp:40
SmallVector< MCPendingError, 0 > PendingErrors
Definition: MCAsmParser.h:141
bool parseOptionalToken(AsmToken::TokenKind T)
Attempt to parse and consume token, returning true on success.
Definition: MCAsmParser.cpp:80
bool parseIntToken(int64_t &V, const Twine &ErrMsg)
Definition: MCAsmParser.cpp:72
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
virtual MCAsmLexer & getLexer()=0
virtual ~MCAsmParser()
bool TokError(const Twine &Msg, SMRange Range=std::nullopt)
Report an error at the current lexer location.
Definition: MCAsmParser.cpp:97
bool parseTokenLoc(SMLoc &Loc)
Definition: MCAsmParser.cpp:44
bool parseGNUAttribute(SMLoc L, int64_t &Tag, int64_t &IntegerValue)
Parse a .gnu_attribute.
void setTargetParser(MCTargetAsmParser &P)
Definition: MCAsmParser.cpp:34
bool Error(SMLoc L, const Twine &Msg, SMRange Range=std::nullopt)
Return an error at the location L, with the message Msg.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
virtual void dump() const
dump - Print to the debug stream.
MCTargetAsmParser - Generic interface to target specific assembly parsers.
Represents a location in source code.
Definition: SMLoc.h:23
Represents a range in source code.
Definition: SMLoc.h:48
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:32
An efficient, type-erasing, non-owning reference to a callable.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:450
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
cl::opt< unsigned > AsmMacroMaxNestingDepth
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163