LLVM 23.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/MC/MCContext.h"
21#include "llvm/Support/SMLoc.h"
22#include <cstdint>
23#include <string>
24#include <utility>
25
26namespace llvm {
27
28class MCAsmInfo;
30class MCExpr;
31class MCInstPrinter;
32class MCInstrInfo;
33class MCStreamer;
35class SourceMgr;
36
38 enum IdKind {
39 IK_Invalid, // Initial state. Unexpected after a successful parsing.
40 IK_Label, // Function/Label reference.
41 IK_EnumVal, // Value of enumeration type.
42 IK_Var // Variable.
43 };
44 // Represents an Enum value
46 int64_t EnumVal;
47 };
48 // Represents a label/function reference
50 void *Decl;
51 };
52 // Represents a variable
54 void *Decl;
56 unsigned Length;
57 unsigned Size;
58 unsigned Type;
59 };
60 // An InlineAsm identifier can only be one of those
61 union {
65 };
66 bool isKind(IdKind kind) const { return Kind == kind; }
67 // Initializers
68 void setEnum(int64_t enumVal) {
69 assert(isKind(IK_Invalid) && "should be initialized only once");
70 Kind = IK_EnumVal;
71 Enum.EnumVal = enumVal;
72 }
73 void setLabel(void *decl) {
74 assert(isKind(IK_Invalid) && "should be initialized only once");
75 Kind = IK_Label;
76 Label.Decl = decl;
77 }
78 void setVar(void *decl, bool isGlobalLV, unsigned size, unsigned type) {
79 assert(isKind(IK_Invalid) && "should be initialized only once");
80 Kind = IK_Var;
81 Var.Decl = decl;
82 Var.IsGlobalLV = isGlobalLV;
83 Var.Size = size;
84 Var.Type = type;
85 Var.Length = size / type;
86 }
88
89private:
90 // Discriminate using the current kind.
91 IdKind Kind = IK_Invalid;
92};
93
94// Generic type information for an assembly object.
95// All sizes measured in bytes.
98 unsigned Size = 0;
99 unsigned ElementSize = 0;
100 unsigned Length = 0;
101};
102
105 unsigned Offset = 0;
106};
107
108/// Generic Sema callback for assembly parser.
110public:
112
115 bool IsUnevaluatedContext) = 0;
117 SMLoc Location, bool Create) = 0;
119 unsigned &Offset) = 0;
120};
121
122/// Generic assembler parser interface, for use by target specific
123/// assembly parsers.
125public:
128 std::pair<MCAsmParserExtension*, DirectiveHandler>;
129
135
136private:
137 MCTargetAsmParser *TargetParser = nullptr;
138
139protected: // Can only create subclasses.
141
148
149 /// Flag tracking whether any errors have been encountered.
150 bool HadError = false;
151
152 bool ShowParsedOperands = false;
153
154 /// Flag tracking whether we're only interested in symbols, which allows us to
155 /// avoid some work (e.g. resolving .incbin directives).
156 // TODO: Adopt this in more places.
157 bool SymbolScanningMode = false;
158
159public:
160 MCAsmParser(const MCAsmParser &) = delete;
162 virtual ~MCAsmParser();
163
165 ExtensionDirectiveHandler Handler) = 0;
166
168
169 MCContext &getContext() { return Ctx; }
172 AsmLexer &getLexer() { return Lexer; }
173 const AsmLexer &getLexer() const { return Lexer; }
174
175 MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
176 void setTargetParser(MCTargetAsmParser &P);
177
178 virtual unsigned getAssemblerDialect() { return 0;}
179 virtual void setAssemblerDialect(unsigned i) { }
180
183
185
186 /// Run the parser on the input source buffer.
187 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
188
189 virtual void setParsingMSInlineAsm(bool V) = 0;
190 virtual bool isParsingMSInlineAsm() = 0;
191
192 virtual bool discardLTOSymbol(StringRef) const { return false; }
193
194 virtual bool isParsingMasm() const { return false; }
195
196 virtual bool defineMacro(StringRef Name, StringRef Value) { return true; }
197
198 virtual bool lookUpField(StringRef Name, AsmFieldInfo &Info) const {
199 return true;
200 }
201 virtual bool lookUpField(StringRef Base, StringRef Member,
202 AsmFieldInfo &Info) const {
203 return true;
204 }
205
206 virtual bool lookUpType(StringRef Name, AsmTypeInfo &Info) const {
207 return true;
208 }
209
210 /// Parse MS-style inline assembly.
211 virtual bool parseMSInlineAsm(
212 std::string &AsmString, unsigned &NumOutputs, unsigned &NumInputs,
213 SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
214 SmallVectorImpl<std::string> &Constraints,
215 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
217
218 /// Emit a note at the location \p L, with the message \p Msg.
219 virtual void Note(SMLoc L, const Twine &Msg, SMRange Range = {}) = 0;
220
221 /// Emit a warning at the location \p L, with the message \p Msg.
222 ///
223 /// \return The return value is true, if warnings are fatal.
224 virtual bool Warning(SMLoc L, const Twine &Msg, SMRange Range = {}) = 0;
225
226 /// Return an error at the location \p L, with the message \p Msg. This
227 /// may be modified before being emitted.
228 ///
229 /// \return The return value is always true, as an idiomatic convenience to
230 /// clients.
231 bool Error(SMLoc L, const Twine &Msg, SMRange Range = {});
232
233 /// Emit an error at the location \p L, with the message \p Msg.
234 ///
235 /// \return The return value is always true, as an idiomatic convenience to
236 /// clients.
237 virtual bool printError(SMLoc L, const Twine &Msg, SMRange Range = {}) = 0;
238
239 bool hasPendingError() { return !PendingErrors.empty(); }
240
242 bool rv = !PendingErrors.empty();
243 for (auto &Err : PendingErrors) {
244 printError(Err.Loc, Twine(Err.Msg), Err.Range);
245 }
246 PendingErrors.clear();
247 return rv;
248 }
249
251
252 bool addErrorSuffix(const Twine &Suffix);
253
254 /// Get the next AsmToken in the stream, possibly handling file
255 /// inclusion first.
256 virtual const AsmToken &Lex() = 0;
257
258 /// Get the current AsmToken from the stream.
259 const AsmToken &getTok() const;
260
261 /// Report an error at the current lexer location.
262 bool TokError(const Twine &Msg, SMRange Range = {});
263
264 bool parseTokenLoc(SMLoc &Loc);
265 bool parseToken(AsmToken::TokenKind T, const Twine &Msg = "unexpected token");
266 /// Attempt to parse and consume token, returning true on
267 /// success.
268 bool parseOptionalToken(AsmToken::TokenKind T);
269
270 bool parseComma() { return parseToken(AsmToken::Comma, "expected comma"); }
271 bool parseRParen() { return parseToken(AsmToken::RParen, "expected ')'"); }
272 bool parseEOL();
273 bool parseEOL(const Twine &ErrMsg);
274
275 bool parseMany(function_ref<bool()> parseOne, bool hasComma = true);
276
277 bool parseIntToken(int64_t &V, const Twine &ErrMsg = "expected integer");
278
279 bool check(bool P, const Twine &Msg);
280 bool check(bool P, SMLoc Loc, const Twine &Msg);
281
282 /// Parse an identifier or string (as a quoted identifier) and set \p
283 /// Res to the identifier contents.
284 virtual bool parseIdentifier(StringRef &Res) = 0;
285
286 /// Parse identifier and get or create symbol for it.
287 bool parseSymbol(MCSymbol *&Res);
288
289 /// Parse up to the end of statement and return the contents from the
290 /// current token until the end of the statement; the current token on exit
291 /// will be either the EndOfStatement or EOF.
293
294 /// Parse the current token as a string which may include escaped
295 /// characters and return the string contents.
296 virtual bool parseEscapedString(std::string &Data) = 0;
297
298 /// Parse an angle-bracket delimited string at the current position if one is
299 /// present, returning the string contents.
300 virtual bool parseAngleBracketString(std::string &Data) = 0;
301
302 /// Skip to the end of the current statement, for error recovery.
303 virtual void eatToEndOfStatement() = 0;
304
305 /// Parse an arbitrary expression.
306 ///
307 /// \param Res - The value of the expression. The result is undefined
308 /// on error.
309 /// \return - False on success.
310 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
311 bool parseExpression(const MCExpr *&Res);
312
313 /// Parse a primary expression.
314 ///
315 /// \param Res - The value of the expression. The result is undefined
316 /// on error.
317 /// \return - False on success.
318 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
319 AsmTypeInfo *TypeInfo = nullptr) = 0;
320
321 /// Parse an arbitrary expression, assuming that an initial '(' has
322 /// already been consumed.
323 ///
324 /// \param Res - The value of the expression. The result is undefined
325 /// on error.
326 /// \return - False on success.
327 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
328
329 /// Parse an expression which must evaluate to an absolute value.
330 ///
331 /// \param Res - The value of the absolute expression. The result is undefined
332 /// on error.
333 /// \return - False on success.
334 virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
335
336 /// Ensure that we have a valid section set in the streamer. Otherwise,
337 /// report an error and switch to .text.
338 /// \return - False on success.
339 virtual bool checkForValidSection() = 0;
340
341 /// Parse a .gnu_attribute.
342 bool parseGNUAttribute(SMLoc L, int64_t &Tag, int64_t &IntegerValue);
343
344 bool parseAtSpecifier(const MCExpr *&Res, SMLoc &EndLoc);
345 const MCExpr *applySpecifier(const MCExpr *E, uint32_t Variant);
346};
347
348/// Create an MCAsmParser instance for parsing assembly similar to gas syntax
350 const MCAsmInfo &, unsigned CB = 0);
351
352/// Create an MCAsmParser instance for parsing Microsoft MASM-style assembly
354 const MCAsmInfo &, struct tm,
355 unsigned CB = 0);
356
357} // end namespace llvm
358
359#endif // LLVM_MC_MCPARSER_MCASMPARSER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
#define T
static constexpr unsigned SM(unsigned Version)
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
This file defines the SmallString class.
This file defines the SmallVector class.
Target independent representation for an assembler token.
Definition MCAsmMacro.h:22
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
Generic interface for extending the MCAsmParser, which is implemented by target and object file assem...
Generic Sema callback for assembly parser.
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.
virtual void eatToEndOfStatement()=0
Skip to the end of the current statement, for error recovery.
bool parseToken(AsmToken::TokenKind T, const Twine &Msg="unexpected token")
bool printPendingErrors()
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)
void clearPendingErrors()
MCContext & getContext()
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.
AsmLexer & getLexer()
bool parseAtSpecifier(const MCExpr *&Res, SMLoc &EndLoc)
const AsmToken & getTok() const
Get the current AsmToken from the stream.
const MCExpr * applySpecifier(const MCExpr *E, uint32_t Variant)
virtual bool checkForValidSection()=0
Ensure that we have a valid section set in the streamer.
virtual bool isParsingMasm() const
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
bool parseSymbol(MCSymbol *&Res)
Parse identifier and get or create symbol for it.
virtual bool discardLTOSymbol(StringRef) const
MCAsmParser & operator=(const MCAsmParser &)=delete
SmallVector< MCPendingError, 0 > PendingErrors
virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression, assuming that an initial '(' has already been consumed.
MCAsmParser(MCContext &, MCStreamer &, SourceMgr &, const MCAsmInfo &)
virtual bool isParsingMSInlineAsm()=0
MCStreamer & Out
virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc, AsmTypeInfo *TypeInfo=nullptr)=0
Parse a primary expression.
virtual void Note(SMLoc L, const Twine &Msg, SMRange Range={})=0
Emit a note at the location L, with the message Msg.
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
SourceMgr & SrcMgr
virtual unsigned getAssemblerDialect()
std::pair< MCAsmParserExtension *, DirectiveHandler > ExtensionDirectiveHandler
virtual bool Warning(SMLoc L, const Twine &Msg, SMRange Range={})=0
Emit a warning at the location L, with the message Msg.
void setShowParsedOperands(bool Value)
bool(*)(MCAsmParserExtension *, StringRef, SMLoc) DirectiveHandler
bool TokError(const Twine &Msg, SMRange Range={})
Report an error at the current lexer location.
virtual bool Run(bool NoInitialTextSection, bool NoFinalize=false)=0
Run the parser on the input source buffer.
MCContext & Ctx
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,...
const MCAsmInfo & MAI
virtual ~MCAsmParser()
virtual bool lookUpType(StringRef Name, AsmTypeInfo &Info) const
void setSymbolScanningMode(bool Value)
virtual bool printError(SMLoc L, const Twine &Msg, SMRange Range={})=0
Emit an error at the location L, with the message Msg.
SourceMgr & getSourceManager()
virtual bool lookUpField(StringRef Base, StringRef Member, AsmFieldInfo &Info) const
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
const AsmLexer & getLexer() const
bool SymbolScanningMode
Flag tracking whether we're only interested in symbols, which allows us to avoid some work (e....
bool parseGNUAttribute(SMLoc L, int64_t &Tag, int64_t &IntegerValue)
Parse a .gnu_attribute.
MCStreamer & getStreamer()
virtual void setAssemblerDialect(unsigned i)
virtual void setParsingMSInlineAsm(bool V)=0
bool HadError
Flag tracking whether any errors have been encountered.
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, MCInstPrinter *IP, MCAsmParserSemaCallback &SI)=0
Parse MS-style inline assembly.
MCTargetAsmParser & getTargetParser() const
virtual void addDirectiveHandler(StringRef Directive, ExtensionDirectiveHandler Handler)=0
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
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
Streaming machine code generation interface.
Definition MCStreamer.h:221
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
MCTargetAsmParser - Generic interface to target specific assembly parsers.
Represents a location in source code.
Definition SMLoc.h:22
Represents a range in source code.
Definition SMLoc.h:47
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...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition SourceMgr.h:37
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 Value Representation.
Definition Value.h:75
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
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:1669
LLVM_ABI MCAsmParser * createMCMasmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &, struct tm, unsigned CB=0)
Create an MCAsmParser instance for parsing Microsoft MASM-style assembly.
LLVM_ABI MCAsmParser * createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &, unsigned CB=0)
Create an MCAsmParser instance for parsing assembly similar to gas syntax.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
unsigned ElementSize
Definition MCAsmParser.h:99
void setVar(void *decl, bool isGlobalLV, unsigned size, unsigned type)
Definition MCAsmParser.h:78
bool isKind(IdKind kind) const
Definition MCAsmParser.h:66
void setEnum(int64_t enumVal)
Definition MCAsmParser.h:68