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