LLVM 22.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"
13#include "llvm/MC/MCAsmInfo.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/SMLoc.h"
21#include <cassert>
22
23using namespace llvm;
24
25namespace llvm {
27 "asm-macro-max-nesting-depth", cl::init(20), cl::Hidden,
28 cl::desc("The maximum nesting depth allowed for assembly macros."));
29}
30
32 const MCAsmInfo &MAI)
33 : Ctx(Ctx), Out(Out), SrcMgr(SM), MAI(MAI), Lexer(MAI) {}
34
36
38 assert(!TargetParser && "Target parser is already initialized!");
39 TargetParser = &P;
40 TargetParser->Initialize(*this);
41}
42
44 return getLexer().getTok();
45}
46
48 Loc = getTok().getLoc();
49 return false;
50}
51
53 if (getTok().getKind() != AsmToken::EndOfStatement)
54 return Error(getTok().getLoc(), "expected newline");
55 Lex();
56 return false;
57}
58
59bool MCAsmParser::parseEOL(const Twine &Msg) {
60 if (getTok().getKind() != AsmToken::EndOfStatement)
61 return Error(getTok().getLoc(), Msg);
62 Lex();
63 return false;
64}
65
68 return parseEOL(Msg);
69 if (getTok().getKind() != T)
70 return Error(getTok().getLoc(), Msg);
71 Lex();
72 return false;
73}
74
75bool MCAsmParser::parseIntToken(int64_t &V, const Twine &Msg) {
76 if (getTok().getKind() != AsmToken::Integer)
77 return TokError(Msg);
78 V = getTok().getIntVal();
79 Lex();
80 return false;
81}
82
84 bool Present = (getTok().getKind() == T);
85 if (Present)
87 return Present;
88}
89
90bool MCAsmParser::check(bool P, const Twine &Msg) {
91 return check(P, getTok().getLoc(), Msg);
92}
93
94bool MCAsmParser::check(bool P, SMLoc Loc, const Twine &Msg) {
95 if (P)
96 return Error(Loc, Msg);
97 return false;
98}
99
101 return Error(getLexer().getLoc(), Msg, Range);
102}
103
105 MCPendingError PErr;
106 PErr.Loc = L;
107 Msg.toVector(PErr.Msg);
108 PErr.Range = Range;
109 PendingErrors.push_back(PErr);
110
111 // If we threw this parsing error after a lexing error, this should
112 // supercede the lexing error and so we remove it from the Lexer
113 // before it can propagate
114 if (getTok().is(AsmToken::Error))
115 getLexer().Lex();
116 return true;
117}
118
120 // Make sure lexing errors have propagated to the parser.
121 if (getTok().is(AsmToken::Error))
122 Lex();
123 for (auto &PErr : PendingErrors)
124 Suffix.toVector(PErr.Msg);
125 return true;
126}
127
128bool MCAsmParser::parseMany(function_ref<bool()> parseOne, bool hasComma) {
130 return false;
131 while (true) {
132 if (parseOne())
133 return true;
135 return false;
136 if (hasComma && parseToken(AsmToken::Comma))
137 return true;
138 }
139 return false;
140}
141
143 SMLoc L;
144 return parseExpression(Res, L);
145}
146
148 int64_t &IntegerValue) {
149 // Parse a .gnu_attribute with numerical tag and value.
150 StringRef S(L.getPointer());
151 SMLoc TagLoc;
152 TagLoc = getTok().getLoc();
153 const AsmToken &Tok = getTok();
154 if (Tok.isNot(AsmToken::Integer))
155 return false;
156 Tag = Tok.getIntVal();
157 Lex(); // Eat the Tag
158 Lex(); // Eat the comma
159 if (Tok.isNot(AsmToken::Integer))
160 return false;
161 IntegerValue = Tok.getIntVal();
162 Lex(); // Eat the IntegerValue
163 return true;
164}
165
167 // Cannot completely remove virtual function even in release mode.
168#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
169 dbgs() << " ";
170 print(dbgs(), MCAsmInfo());
171#endif
172}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
const AsmToken & getTok() const
Get the current (last) lexed token.
Definition: AsmLexer.h:118
const AsmToken & Lex()
Consume the next token from the input stream and return it.
Definition: AsmLexer.h:92
Target independent representation for an assembler token.
Definition: MCAsmMacro.h:22
LLVM_ABI SMLoc getLoc() const
Definition: AsmLexer.cpp:32
int64_t getIntVal() const
Definition: MCAsmMacro.h:108
bool isNot(TokenKind K) const
Definition: MCAsmMacro.h:76
TokenKind getKind() const
Definition: MCAsmMacro.h:74
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:64
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:66
bool addErrorSuffix(const Twine &Suffix)
bool check(bool P, const Twine &Msg)
Definition: MCAsmParser.cpp:90
bool parseIntToken(int64_t &V, const Twine &ErrMsg="expected integer")
Definition: MCAsmParser.cpp:75
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
AsmLexer & getLexer()
Definition: MCAsmParser.h:167
const AsmToken & getTok() const
Get the current AsmToken from the stream.
Definition: MCAsmParser.cpp:43
SmallVector< MCPendingError, 0 > PendingErrors
Definition: MCAsmParser.h:147
MCAsmParser(MCContext &, MCStreamer &, SourceMgr &, const MCAsmInfo &)
Definition: MCAsmParser.cpp:31
bool parseOptionalToken(AsmToken::TokenKind T)
Attempt to parse and consume token, returning true on success.
Definition: MCAsmParser.cpp:83
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
virtual ~MCAsmParser()
bool TokError(const Twine &Msg, SMRange Range=std::nullopt)
Report an error at the current lexer location.
bool parseTokenLoc(SMLoc &Loc)
Definition: MCAsmParser.cpp:47
bool parseGNUAttribute(SMLoc L, int64_t &Tag, int64_t &IntegerValue)
Parse a .gnu_attribute.
void setTargetParser(MCTargetAsmParser &P)
Definition: MCAsmParser.cpp:37
bool Error(SMLoc L, const Twine &Msg, SMRange Range=std::nullopt)
Return an error at the location L, with the message Msg.
Context object for machine code objects.
Definition: MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
virtual void dump() const
dump - Print to the debug stream.
virtual void print(raw_ostream &, const MCAsmInfo &) const =0
print - Print a debug representation of the operand to the given stream.
Streaming machine code generation interface.
Definition: MCStreamer.h:220
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
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:32
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
LLVM_ABI 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:444
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
SourceMgr SrcMgr
Definition: Error.cpp:24
cl::opt< unsigned > AsmMacroMaxNestingDepth
Definition: MasmParser.cpp:966
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207