LLVM  5.0.0
MCAsmParser.h
Go to the documentation of this file.
1 //===- llvm/MC/MCAsmParser.h - Abstract Asm Parser Interface ----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_MC_MCPARSER_MCASMPARSER_H
11 #define LLVM_MC_MCPARSER_MCASMPARSER_H
12 
13 #include "llvm/ADT/None.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Twine.h"
20 #include "llvm/Support/SMLoc.h"
21 #include <cstdint>
22 #include <string>
23 #include <utility>
24 
25 namespace llvm {
26 
27 class MCAsmInfo;
28 class MCAsmParserExtension;
29 class MCContext;
30 class MCExpr;
31 class MCInstPrinter;
32 class MCInstrInfo;
33 class MCStreamer;
34 class MCTargetAsmParser;
35 class SourceMgr;
36 
38 public:
39  void *OpDecl;
40  bool IsVarDecl;
41  unsigned Length, Size, Type;
42 
43  void clear() {
44  OpDecl = nullptr;
45  IsVarDecl = false;
46  Length = 1;
47  Size = 0;
48  Type = 0;
49  }
50 };
51 
52 /// \brief Generic Sema callback for assembly parser.
54 public:
55  virtual ~MCAsmParserSemaCallback();
56 
57  virtual void *LookupInlineAsmIdentifier(StringRef &LineBuf,
59  bool IsUnevaluatedContext) = 0;
61  SMLoc Location, bool Create) = 0;
62  virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
63  unsigned &Offset) = 0;
64 };
65 
66 /// \brief Generic assembler parser interface, for use by target specific
67 /// assembly parsers.
68 class MCAsmParser {
69 public:
72  std::pair<MCAsmParserExtension*, DirectiveHandler>;
73 
74  struct MCPendingError {
78  };
79 
80 private:
81  MCTargetAsmParser *TargetParser = nullptr;
82 
83  unsigned ShowParsedOperands : 1;
84 
85 protected: // Can only create subclasses.
86  MCAsmParser();
87 
88  bool HadError = false;
89 
91  /// Flag tracking whether any errors have been encountered.
92 
93 public:
94  MCAsmParser(const MCAsmParser &) = delete;
95  MCAsmParser &operator=(const MCAsmParser &) = delete;
96  virtual ~MCAsmParser();
97 
98  virtual void addDirectiveHandler(StringRef Directive,
99  ExtensionDirectiveHandler Handler) = 0;
100 
101  virtual void addAliasForDirective(StringRef Directive, StringRef Alias) = 0;
102 
103  virtual SourceMgr &getSourceManager() = 0;
104 
105  virtual MCAsmLexer &getLexer() = 0;
106  const MCAsmLexer &getLexer() const {
107  return const_cast<MCAsmParser*>(this)->getLexer();
108  }
109 
110  virtual MCContext &getContext() = 0;
111 
112  /// \brief Return the output streamer for the assembler.
113  virtual MCStreamer &getStreamer() = 0;
114 
115  MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
117 
118  virtual unsigned getAssemblerDialect() { return 0;}
119  virtual void setAssemblerDialect(unsigned i) { }
120 
121  bool getShowParsedOperands() const { return ShowParsedOperands; }
122  void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
123 
124  /// \brief Run the parser on the input source buffer.
125  virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
126 
127  virtual void setParsingInlineAsm(bool V) = 0;
128  virtual bool isParsingInlineAsm() = 0;
129 
130  /// \brief Parse MS-style inline assembly.
131  virtual bool parseMSInlineAsm(
132  void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
133  unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
134  SmallVectorImpl<std::string> &Constraints,
135  SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
136  const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) = 0;
137 
138  /// \brief Emit a note at the location \p L, with the message \p Msg.
139  virtual void Note(SMLoc L, const Twine &Msg, SMRange Range = None) = 0;
140 
141  /// \brief Emit a warning at the location \p L, with the message \p Msg.
142  ///
143  /// \return The return value is true, if warnings are fatal.
144  virtual bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) = 0;
145 
146  /// \brief Return an error at the location \p L, with the message \p Msg. This
147  /// may be modified before being emitted.
148  ///
149  /// \return The return value is always true, as an idiomatic convenience to
150  /// clients.
151  bool Error(SMLoc L, const Twine &Msg, SMRange Range = None);
152 
153  /// \brief Emit an error at the location \p L, with the message \p Msg.
154  ///
155  /// \return The return value is always true, as an idiomatic convenience to
156  /// clients.
157  virtual bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) = 0;
158 
159  bool hasPendingError() { return !PendingErrors.empty(); }
160 
162  bool rv = !PendingErrors.empty();
163  for (auto Err : PendingErrors) {
164  printError(Err.Loc, Twine(Err.Msg), Err.Range);
165  }
166  PendingErrors.clear();
167  return rv;
168  }
169 
170  bool addErrorSuffix(const Twine &Suffix);
171 
172  /// \brief Get the next AsmToken in the stream, possibly handling file
173  /// inclusion first.
174  virtual const AsmToken &Lex() = 0;
175 
176  /// \brief Get the current AsmToken from the stream.
177  const AsmToken &getTok() const;
178 
179  /// \brief Report an error at the current lexer location.
180  bool TokError(const Twine &Msg, SMRange Range = None);
181 
182  bool parseTokenLoc(SMLoc &Loc);
183  bool parseToken(AsmToken::TokenKind T, const Twine &Msg = "unexpected token");
184  /// \brief Attempt to parse and consume token, returning true on
185  /// success.
187 
188  bool parseEOL(const Twine &ErrMsg);
189 
190  bool parseMany(function_ref<bool()> parseOne, bool hasComma = true);
191 
192  bool parseIntToken(int64_t &V, const Twine &ErrMsg);
193 
194  bool check(bool P, const Twine &Msg);
195  bool check(bool P, SMLoc Loc, const Twine &Msg);
196 
197  /// \brief Parse an identifier or string (as a quoted identifier) and set \p
198  /// Res to the identifier contents.
199  virtual bool parseIdentifier(StringRef &Res) = 0;
200 
201  /// \brief Parse up to the end of statement and return the contents from the
202  /// current token until the end of the statement; the current token on exit
203  /// will be either the EndOfStatement or EOF.
205 
206  /// \brief Parse the current token as a string which may include escaped
207  /// characters and return the string contents.
208  virtual bool parseEscapedString(std::string &Data) = 0;
209 
210  /// \brief Skip to the end of the current statement, for error recovery.
211  virtual void eatToEndOfStatement() = 0;
212 
213  /// \brief Parse an arbitrary expression.
214  ///
215  /// \param Res - The value of the expression. The result is undefined
216  /// on error.
217  /// \return - False on success.
218  virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
219  bool parseExpression(const MCExpr *&Res);
220 
221  /// \brief Parse a primary expression.
222  ///
223  /// \param Res - The value of the expression. The result is undefined
224  /// on error.
225  /// \return - False on success.
226  virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) = 0;
227 
228  /// \brief Parse an arbitrary expression, assuming that an initial '(' has
229  /// already been consumed.
230  ///
231  /// \param Res - The value of the expression. The result is undefined
232  /// on error.
233  /// \return - False on success.
234  virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
235 
236  /// \brief Parse an expression which must evaluate to an absolute value.
237  ///
238  /// \param Res - The value of the absolute expression. The result is undefined
239  /// on error.
240  /// \return - False on success.
241  virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
242 
243  /// \brief Ensure that we have a valid section set in the streamer. Otherwise,
244  /// report an error and switch to .text.
245  /// \return - False on success.
246  virtual bool checkForValidSection() = 0;
247 
248  /// \brief Parse an arbitrary expression of a specified parenthesis depth,
249  /// assuming that the initial '(' characters have already been consumed.
250  ///
251  /// \param ParenDepth - Specifies how many trailing expressions outside the
252  /// current parentheses we have to parse.
253  /// \param Res - The value of the expression. The result is undefined
254  /// on error.
255  /// \return - False on success.
256  virtual bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
257  SMLoc &EndLoc) = 0;
258 };
259 
260 /// \brief Create an MCAsmParser instance.
261 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &,
262  const MCAsmInfo &, unsigned CB = 0);
263 
264 } // end namespace llvm
265 
266 #endif // LLVM_MC_MCPARSER_MCASMPARSER_H
MachineLoop * L
Represents a range in source code.
Definition: SMLoc.h:49
bool addErrorSuffix(const Twine &Suffix)
virtual bool Warning(SMLoc L, const Twine &Msg, SMRange Range=None)=0
Emit a warning at the location L, with the message Msg.
Generic assembler parser interface, for use by target specific assembly parsers.
Definition: MCAsmParser.h:68
virtual void setAssemblerDialect(unsigned i)
Definition: MCAsmParser.h:119
MCTargetAsmParser - Generic interface to target specific assembly parsers.
virtual bool checkForValidSection()=0
Ensure that we have a valid section set in the streamer.
bool check(bool P, const Twine &Msg)
Definition: MCAsmParser.cpp:81
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:89
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
virtual SourceMgr & getSourceManager()=0
bool parseIntToken(int64_t &V, const Twine &ErrMsg)
Definition: MCAsmParser.cpp:63
virtual void * LookupInlineAsmIdentifier(StringRef &LineBuf, InlineAsmIdentifierInfo &Info, bool IsUnevaluatedContext)=0
MCAsmParser * createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &, unsigned CB=0)
Create an MCAsmParser instance.
Definition: AsmParser.cpp:5626
MCTargetAsmParser & getTargetParser() const
Definition: MCAsmParser.h:115
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Generic assembler lexer interface, for use by target specific assembly lexers.
Definition: MCAsmLexer.h:149
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
virtual bool printError(SMLoc L, const Twine &Msg, SMRange Range=None)=0
Emit 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:36
bool printPendingErrors()
Definition: MCAsmParser.h:161
Target independent representation for an assembler token.
Definition: MCAsmLexer.h:27
virtual void addDirectiveHandler(StringRef Directive, ExtensionDirectiveHandler Handler)=0
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
Context object for machine code objects.
Definition: MCContext.h:59
void setShowParsedOperands(bool Value)
Definition: MCAsmParser.h:122
virtual bool parseEscapedString(std::string &Data)=0
Parse the current token as a string which may include escaped characters and return the string conten...
const AsmToken & getTok() const
Get the current AsmToken from the stream.
Definition: MCAsmParser.cpp:33
bool(*)(MCAsmParserExtension *, StringRef, SMLoc) DirectiveHandler
Definition: MCAsmParser.h:70
virtual void eatToEndOfStatement()=0
Skip to the end of the current statement, for error recovery.
virtual void addAliasForDirective(StringRef Directive, StringRef Alias)=0
bool parseToken(AsmToken::TokenKind T, const Twine &Msg="unexpected token")
Definition: MCAsmParser.cpp:54
#define P(N)
virtual MCContext & getContext()=0
std::string ErrMsg
Streaming machine code generation interface.
Definition: MCStreamer.h:167
bool hasPendingError()
Definition: MCAsmParser.h:159
MCAsmParser & operator=(const MCAsmParser &)=delete
virtual ~MCAsmParser()
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
virtual bool isParsingInlineAsm()=0
virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse a primary expression.
const MCAsmLexer & getLexer() const
Definition: MCAsmParser.h:106
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
virtual bool Run(bool NoInitialTextSection, bool NoFinalize=false)=0
Run the parser on the input source buffer.
virtual MCAsmLexer & getLexer()=0
bool parseTokenLoc(SMLoc &Loc)
Definition: MCAsmParser.cpp:37
Generic Sema callback for assembly parser.
Definition: MCAsmParser.h:53
bool Error(SMLoc L, const Twine &Msg, SMRange Range=None)
Return an error at the location L, with the message Msg.
Definition: MCAsmParser.cpp:95
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:41
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 LookupInlineAsmField(StringRef Base, StringRef Member, unsigned &Offset)=0
virtual void setParsingInlineAsm(bool V)=0
virtual MCStreamer & getStreamer()=0
Return the output streamer for the assembler.
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...
bool parseMany(function_ref< bool()> parseOne, bool hasComma=true)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
SmallVector< MCPendingError, 1 > PendingErrors
Definition: MCAsmParser.h:90
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:41
virtual bool parseMSInlineAsm(void *AsmLoc, 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 StringRef LookupInlineAsmLabel(StringRef Identifier, SourceMgr &SM, SMLoc Location, bool Create)=0
virtual bool parseAbsoluteExpression(int64_t &Res)=0
Parse an expression which must evaluate to an absolute value.
virtual unsigned getAssemblerDialect()
Definition: MCAsmParser.h:118
virtual void Note(SMLoc L, const Twine &Msg, SMRange Range=None)=0
Emit a note at the location L, with the message Msg.
bool TokError(const Twine &Msg, SMRange Range=None)
Report an error at the current lexer location.
Definition: MCAsmParser.cpp:91
Generic interface for extending the MCAsmParser, which is implemented by target and object file assem...
LLVM Value Representation.
Definition: Value.h:73
bool parseEOL(const Twine &ErrMsg)
Definition: MCAsmParser.cpp:42
std::pair< MCAsmParserExtension *, DirectiveHandler > ExtensionDirectiveHandler
Definition: MCAsmParser.h:72
void setTargetParser(MCTargetAsmParser &P)
Definition: MCAsmParser.cpp:27
bool parseOptionalToken(AsmToken::TokenKind T)
Attempt to parse and consume token, returning true on success.
Definition: MCAsmParser.cpp:71
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
virtual bool parseIdentifier(StringRef &Res)=0
Parse an identifier or string (as a quoted identifier) and set Res to the identifier contents...
bool getShowParsedOperands() const
Definition: MCAsmParser.h:121
Represents a location in source code.
Definition: SMLoc.h:24
virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression, assuming that an initial '(' has already been consumed.