LLVM 19.0.0git
MCAsmParser.h
Go to the documentation of this file.
1//===- llvm/MC/MCAsmParser.h - Abstract Asm Parser Interface ----*- C++ -*-===//
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
9#ifndef LLVM_MC_MCPARSER_MCASMPARSER_H
10#define LLVM_MC_MCPARSER_MCASMPARSER_H
11
15#include "llvm/ADT/StringRef.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/MC/MCAsmMacro.h"
18#include "llvm/Support/SMLoc.h"
19#include <cstdint>
20#include <string>
21#include <utility>
22
23namespace llvm {
24
25class MCAsmLexer;
26class MCAsmInfo;
27class MCAsmParserExtension;
28class MCContext;
29class MCExpr;
30class MCInstPrinter;
31class MCInstrInfo;
32class MCStreamer;
33class MCTargetAsmParser;
34class SourceMgr;
35
37 enum IdKind {
38 IK_Invalid, // Initial state. Unexpected after a successful parsing.
39 IK_Label, // Function/Label reference.
40 IK_EnumVal, // Value of enumeration type.
41 IK_Var // Variable.
42 };
43 // Represents an Enum value
45 int64_t EnumVal;
46 };
47 // Represents a label/function reference
49 void *Decl;
50 };
51 // Represents a variable
53 void *Decl;
55 unsigned Length;
56 unsigned Size;
57 unsigned Type;
58 };
59 // An InlineAsm identifier can only be one of those
60 union {
64 };
65 bool isKind(IdKind kind) const { return Kind == kind; }
66 // Initializers
67 void setEnum(int64_t enumVal) {
68 assert(isKind(IK_Invalid) && "should be initialized only once");
69 Kind = IK_EnumVal;
70 Enum.EnumVal = enumVal;
71 }
72 void setLabel(void *decl) {
73 assert(isKind(IK_Invalid) && "should be initialized only once");
74 Kind = IK_Label;
75 Label.Decl = decl;
76 }
77 void setVar(void *decl, bool isGlobalLV, unsigned size, unsigned type) {
78 assert(isKind(IK_Invalid) && "should be initialized only once");
79 Kind = IK_Var;
80 Var.Decl = decl;
81 Var.IsGlobalLV = isGlobalLV;
82 Var.Size = size;
83 Var.Type = type;
84 Var.Length = size / type;
85 }
87
88private:
89 // Discriminate using the current kind.
90 IdKind Kind = IK_Invalid;
91};
92
93// Generic type information for an assembly object.
94// All sizes measured in bytes.
97 unsigned Size = 0;
98 unsigned ElementSize = 0;
99 unsigned Length = 0;
100};
101
104 unsigned Offset = 0;
105};
106
107/// Generic Sema callback for assembly parser.
109public:
111
114 bool IsUnevaluatedContext) = 0;
116 SMLoc Location, bool Create) = 0;
118 unsigned &Offset) = 0;
119};
120
121/// Generic assembler parser interface, for use by target specific
122/// assembly parsers.
124public:
127 std::pair<MCAsmParserExtension*, DirectiveHandler>;
128
133 };
134
135private:
136 MCTargetAsmParser *TargetParser = nullptr;
137
138protected: // Can only create subclasses.
140
142
143 /// Flag tracking whether any errors have been encountered.
144 bool HadError = false;
145
146 bool ShowParsedOperands = false;
147
148public:
149 MCAsmParser(const MCAsmParser &) = delete;
151 virtual ~MCAsmParser();
152
154 ExtensionDirectiveHandler Handler) = 0;
155
157
159
160 virtual MCAsmLexer &getLexer() = 0;
161 const MCAsmLexer &getLexer() const {
162 return const_cast<MCAsmParser*>(this)->getLexer();
163 }
164
165 virtual MCContext &getContext() = 0;
166
167 /// Return the output streamer for the assembler.
168 virtual MCStreamer &getStreamer() = 0;
169
170 MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
172
173 virtual unsigned getAssemblerDialect() { return 0;}
174 virtual void setAssemblerDialect(unsigned i) { }
175
178
179 /// Run the parser on the input source buffer.
180 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
181
182 virtual void setParsingMSInlineAsm(bool V) = 0;
183 virtual bool isParsingMSInlineAsm() = 0;
184
185 virtual bool discardLTOSymbol(StringRef) const { return false; }
186
187 virtual bool isParsingMasm() const { return false; }
188
189 virtual bool defineMacro(StringRef Name, StringRef Value) { return true; }
190
192 return true;
193 }
194 virtual bool lookUpField(StringRef Base, StringRef Member,
195 AsmFieldInfo &Info) const {
196 return true;
197 }
198
199 virtual bool lookUpType(StringRef Name, AsmTypeInfo &Info) const {
200 return true;
201 }
202
203 /// Parse MS-style inline assembly.
204 virtual bool parseMSInlineAsm(
205 std::string &AsmString, unsigned &NumOutputs, unsigned &NumInputs,
206 SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
207 SmallVectorImpl<std::string> &Constraints,
208 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
209 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) = 0;
210
211 /// Emit a note at the location \p L, with the message \p Msg.
212 virtual void Note(SMLoc L, const Twine &Msg,
213 SMRange Range = std::nullopt) = 0;
214
215 /// Emit a warning at the location \p L, with the message \p Msg.
216 ///
217 /// \return The return value is true, if warnings are fatal.
218 virtual bool Warning(SMLoc L, const Twine &Msg,
219 SMRange Range = std::nullopt) = 0;
220
221 /// Return an error at the location \p L, with the message \p Msg. This
222 /// may be modified before being emitted.
223 ///
224 /// \return The return value is always true, as an idiomatic convenience to
225 /// clients.
226 bool Error(SMLoc L, const Twine &Msg, SMRange Range = std::nullopt);
227
228 /// Emit an error at the location \p L, with the message \p Msg.
229 ///
230 /// \return The return value is always true, as an idiomatic convenience to
231 /// clients.
232 virtual bool printError(SMLoc L, const Twine &Msg,
233 SMRange Range = std::nullopt) = 0;
234
235 bool hasPendingError() { return !PendingErrors.empty(); }
236
238 bool rv = !PendingErrors.empty();
239 for (auto &Err : PendingErrors) {
240 printError(Err.Loc, Twine(Err.Msg), Err.Range);
241 }
242 PendingErrors.clear();
243 return rv;
244 }
245
247
248 bool addErrorSuffix(const Twine &Suffix);
249
250 /// Get the next AsmToken in the stream, possibly handling file
251 /// inclusion first.
252 virtual const AsmToken &Lex() = 0;
253
254 /// Get the current AsmToken from the stream.
255 const AsmToken &getTok() const;
256
257 /// Report an error at the current lexer location.
258 bool TokError(const Twine &Msg, SMRange Range = std::nullopt);
259
260 bool parseTokenLoc(SMLoc &Loc);
261 bool parseToken(AsmToken::TokenKind T, const Twine &Msg = "unexpected token");
262 /// Attempt to parse and consume token, returning true on
263 /// success.
265
266 bool parseComma() { return parseToken(AsmToken::Comma, "expected comma"); }
267 bool parseRParen() { return parseToken(AsmToken::RParen, "expected ')'"); }
268 bool parseEOL();
269 bool parseEOL(const Twine &ErrMsg);
270
271 bool parseMany(function_ref<bool()> parseOne, bool hasComma = true);
272
273 bool parseIntToken(int64_t &V, const Twine &ErrMsg);
274
275 bool check(bool P, const Twine &Msg);
276 bool check(bool P, SMLoc Loc, const Twine &Msg);
277
278 /// Parse an identifier or string (as a quoted identifier) and set \p
279 /// Res to the identifier contents.
280 virtual bool parseIdentifier(StringRef &Res) = 0;
281
282 /// Parse up to the end of statement and return the contents from the
283 /// current token until the end of the statement; the current token on exit
284 /// will be either the EndOfStatement or EOF.
286
287 /// Parse the current token as a string which may include escaped
288 /// characters and return the string contents.
289 virtual bool parseEscapedString(std::string &Data) = 0;
290
291 /// Parse an angle-bracket delimited string at the current position if one is
292 /// present, returning the string contents.
293 virtual bool parseAngleBracketString(std::string &Data) = 0;
294
295 /// Skip to the end of the current statement, for error recovery.
296 virtual void eatToEndOfStatement() = 0;
297
298 /// Parse an arbitrary expression.
299 ///
300 /// \param Res - The value of the expression. The result is undefined
301 /// on error.
302 /// \return - False on success.
303 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
304 bool parseExpression(const MCExpr *&Res);
305
306 /// Parse a primary expression.
307 ///
308 /// \param Res - The value of the expression. The result is undefined
309 /// on error.
310 /// \return - False on success.
311 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
312 AsmTypeInfo *TypeInfo) = 0;
313
314 /// Parse an arbitrary expression, assuming that an initial '(' has
315 /// already been consumed.
316 ///
317 /// \param Res - The value of the expression. The result is undefined
318 /// on error.
319 /// \return - False on success.
320 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
321
322 /// Parse an expression which must evaluate to an absolute value.
323 ///
324 /// \param Res - The value of the absolute expression. The result is undefined
325 /// on error.
326 /// \return - False on success.
327 virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
328
329 /// Ensure that we have a valid section set in the streamer. Otherwise,
330 /// report an error and switch to .text.
331 /// \return - False on success.
332 virtual bool checkForValidSection() = 0;
333
334 /// Parse an arbitrary expression of a specified parenthesis depth,
335 /// assuming that the initial '(' characters have already been consumed.
336 ///
337 /// \param ParenDepth - Specifies how many trailing expressions outside the
338 /// current parentheses we have to parse.
339 /// \param Res - The value of the expression. The result is undefined
340 /// on error.
341 /// \return - False on success.
342 virtual bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
343 SMLoc &EndLoc) = 0;
344
345 /// Parse a .gnu_attribute.
346 bool parseGNUAttribute(SMLoc L, int64_t &Tag, int64_t &IntegerValue);
347};
348
349/// Create an MCAsmParser instance for parsing assembly similar to gas syntax
351 const MCAsmInfo &, unsigned CB = 0);
352
353/// Create an MCAsmParser instance for parsing Microsoft MASM-style assembly
355 const MCAsmInfo &, struct tm, unsigned CB = 0);
356
357} // end namespace llvm
358
359#endif // LLVM_MC_MCPARSER_MCASMPARSER_H
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
std::string Name
#define check(cond)
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
This file defines the SmallVector class.
Target independent representation for an assembler token.
Definition: MCAsmMacro.h:21
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
Generic assembler lexer interface, for use by target specific assembly lexers.
Definition: MCAsmLexer.h:37
Generic interface for extending the MCAsmParser, which is implemented by target and object file assem...
Generic Sema callback for assembly parser.
Definition: MCAsmParser.h:108
virtual bool LookupInlineAsmField(StringRef Base, StringRef Member, unsigned &Offset)=0
virtual StringRef LookupInlineAsmLabel(StringRef Identifier, SourceMgr &SM, SMLoc Location, bool Create)=0
virtual void LookupInlineAsmIdentifier(StringRef &LineBuf, InlineAsmIdentifierInfo &Info, bool IsUnevaluatedContext)=0
Generic assembler parser interface, for use by target specific assembly parsers.
Definition: MCAsmParser.h:123
virtual void eatToEndOfStatement()=0
Skip to the end of the current statement, for error recovery.
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)
virtual bool printError(SMLoc L, const Twine &Msg, SMRange Range=std::nullopt)=0
Emit an error at the location L, with the message Msg.
bool printPendingErrors()
Definition: MCAsmParser.h:237
virtual bool parseEscapedString(std::string &Data)=0
Parse the current token as a string which may include escaped characters and return the string conten...
virtual bool defineMacro(StringRef Name, StringRef Value)
Definition: MCAsmParser.h:189
virtual MCStreamer & getStreamer()=0
Return the output streamer for the assembler.
void clearPendingErrors()
Definition: MCAsmParser.h:246
bool hasPendingError()
Definition: MCAsmParser.h:235
virtual void Note(SMLoc L, const Twine &Msg, SMRange Range=std::nullopt)=0
Emit a note at the location L, with the message Msg.
virtual StringRef parseStringToEndOfStatement()=0
Parse up to the end of statement and return the contents from the current token until the end of the ...
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
virtual SourceMgr & getSourceManager()=0
virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc, AsmTypeInfo *TypeInfo)=0
Parse a primary expression.
const AsmToken & getTok() const
Get the current AsmToken from the stream.
Definition: MCAsmParser.cpp:40
virtual bool checkForValidSection()=0
Ensure that we have a valid section set in the streamer.
virtual bool isParsingMasm() const
Definition: MCAsmParser.h:187
virtual bool parseIdentifier(StringRef &Res)=0
Parse an identifier or string (as a quoted identifier) and set Res to the identifier contents.
MCAsmParser(const MCAsmParser &)=delete
bool getShowParsedOperands() const
Definition: MCAsmParser.h:176
virtual bool discardLTOSymbol(StringRef) const
Definition: MCAsmParser.h:185
MCAsmParser & operator=(const MCAsmParser &)=delete
SmallVector< MCPendingError, 0 > PendingErrors
Definition: MCAsmParser.h:141
virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression, assuming that an initial '(' has already been consumed.
bool parseOptionalToken(AsmToken::TokenKind T)
Attempt to parse and consume token, returning true on success.
Definition: MCAsmParser.cpp:80
virtual bool isParsingMSInlineAsm()=0
std::pair< MCAsmParserExtension *, DirectiveHandler > ExtensionDirectiveHandler
Definition: MCAsmParser.h:127
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 bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression of a specified parenthesis depth, assuming that the initial '(' charact...
virtual unsigned getAssemblerDialect()
Definition: MCAsmParser.h:173
void setShowParsedOperands(bool Value)
Definition: MCAsmParser.h:177
virtual MCAsmLexer & getLexer()=0
bool(*)(MCAsmParserExtension *, StringRef, SMLoc) DirectiveHandler
Definition: MCAsmParser.h:125
virtual bool Warning(SMLoc L, const Twine &Msg, SMRange Range=std::nullopt)=0
Emit a warning at the location L, with the message Msg.
virtual bool Run(bool NoInitialTextSection, bool NoFinalize=false)=0
Run the parser on the input source buffer.
virtual void addAliasForDirective(StringRef Directive, StringRef Alias)=0
virtual bool parseAngleBracketString(std::string &Data)=0
Parse an angle-bracket delimited string at the current position if one is present,...
virtual ~MCAsmParser()
virtual bool lookUpType(StringRef Name, AsmTypeInfo &Info) const
Definition: MCAsmParser.h:199
bool TokError(const Twine &Msg, SMRange Range=std::nullopt)
Report an error at the current lexer location.
Definition: MCAsmParser.cpp:97
const MCAsmLexer & getLexer() const
Definition: MCAsmParser.h:161
virtual bool lookUpField(StringRef Base, StringRef Member, AsmFieldInfo &Info) const
Definition: MCAsmParser.h:194
virtual bool parseAbsoluteExpression(int64_t &Res)=0
Parse an expression which must evaluate to an absolute value.
virtual bool lookUpField(StringRef Name, AsmFieldInfo &Info) const
Definition: MCAsmParser.h:191
bool parseTokenLoc(SMLoc &Loc)
Definition: MCAsmParser.cpp:44
bool parseGNUAttribute(SMLoc L, int64_t &Tag, int64_t &IntegerValue)
Parse a .gnu_attribute.
virtual bool parseMSInlineAsm(std::string &AsmString, unsigned &NumOutputs, unsigned &NumInputs, SmallVectorImpl< std::pair< void *, bool > > &OpDecls, SmallVectorImpl< std::string > &Constraints, SmallVectorImpl< std::string > &Clobbers, const MCInstrInfo *MII, const MCInstPrinter *IP, MCAsmParserSemaCallback &SI)=0
Parse MS-style inline assembly.
virtual void setAssemblerDialect(unsigned i)
Definition: MCAsmParser.h:174
void setTargetParser(MCTargetAsmParser &P)
Definition: MCAsmParser.cpp:34
virtual MCContext & getContext()=0
virtual void setParsingMSInlineAsm(bool V)=0
bool HadError
Flag tracking whether any errors have been encountered.
Definition: MCAsmParser.h:144
MCTargetAsmParser & getTargetParser() const
Definition: MCAsmParser.h:170
virtual void addDirectiveHandler(StringRef Directive, ExtensionDirectiveHandler Handler)=0
Context object for machine code objects.
Definition: MCContext.h:76
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:45
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:26
Streaming machine code generation interface.
Definition: MCStreamer.h:212
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
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
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
LLVM Value Representation.
Definition: Value.h:74
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1689
MCAsmParser * createMCMasmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &, struct tm, unsigned CB=0)
Create an MCAsmParser instance for parsing Microsoft MASM-style assembly.
MCAsmParser * createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &, unsigned CB=0)
Create an MCAsmParser instance for parsing assembly similar to gas syntax.
Definition: AsmParser.cpp:6458
AsmTypeInfo Type
Definition: MCAsmParser.h:103
unsigned ElementSize
Definition: MCAsmParser.h:98
StringRef Name
Definition: MCAsmParser.h:96
void setVar(void *decl, bool isGlobalLV, unsigned size, unsigned type)
Definition: MCAsmParser.h:77
VariableIdentifier Var
Definition: MCAsmParser.h:63
void setLabel(void *decl)
Definition: MCAsmParser.h:72
bool isKind(IdKind kind) const
Definition: MCAsmParser.h:65
void setEnum(int64_t enumVal)
Definition: MCAsmParser.h:67