LLVM  6.0.0svn
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  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
45  struct EnumIdentifier {
46  int64_t EnumVal;
47  };
48  // Represents a label/function reference
49  struct LabelIdentifier {
50  void *Decl;
51  };
52  // Represents a variable
54  void *Decl;
55  bool IsGlobalLV;
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 
89 private:
90  // Discriminate using the current kind.
91  IdKind Kind;
92 };
93 
94 /// \brief Generic Sema callback for assembly parser.
96 public:
97  virtual ~MCAsmParserSemaCallback();
98 
99  virtual void LookupInlineAsmIdentifier(StringRef &LineBuf,
101  bool IsUnevaluatedContext) = 0;
102  virtual StringRef LookupInlineAsmLabel(StringRef Identifier, SourceMgr &SM,
103  SMLoc Location, bool Create) = 0;
104  virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
105  unsigned &Offset) = 0;
106 };
107 
108 /// \brief Generic assembler parser interface, for use by target specific
109 /// assembly parsers.
110 class MCAsmParser {
111 public:
114  std::pair<MCAsmParserExtension*, DirectiveHandler>;
115 
116  struct MCPendingError {
120  };
121 
122 private:
123  MCTargetAsmParser *TargetParser = nullptr;
124 
125  unsigned ShowParsedOperands : 1;
126 
127 protected: // Can only create subclasses.
128  MCAsmParser();
129 
130  /// Flag tracking whether any errors have been encountered.
131  bool HadError = false;
132  /// Enable print [latency:throughput] in output file.
133  bool EnablePrintSchedInfo = false;
134 
136 
137 public:
138  MCAsmParser(const MCAsmParser &) = delete;
139  MCAsmParser &operator=(const MCAsmParser &) = delete;
140  virtual ~MCAsmParser();
141 
142  virtual void addDirectiveHandler(StringRef Directive,
143  ExtensionDirectiveHandler Handler) = 0;
144 
145  virtual void addAliasForDirective(StringRef Directive, StringRef Alias) = 0;
146 
147  virtual SourceMgr &getSourceManager() = 0;
148 
149  virtual MCAsmLexer &getLexer() = 0;
150  const MCAsmLexer &getLexer() const {
151  return const_cast<MCAsmParser*>(this)->getLexer();
152  }
153 
154  virtual MCContext &getContext() = 0;
155 
156  /// \brief Return the output streamer for the assembler.
157  virtual MCStreamer &getStreamer() = 0;
158 
159  MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
160  void setTargetParser(MCTargetAsmParser &P);
161 
162  virtual unsigned getAssemblerDialect() { return 0;}
163  virtual void setAssemblerDialect(unsigned i) { }
164 
165  bool getShowParsedOperands() const { return ShowParsedOperands; }
166  void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
167 
168  void setEnablePrintSchedInfo(bool Value) { EnablePrintSchedInfo = Value; }
169  bool shouldPrintSchedInfo() { return EnablePrintSchedInfo; }
170 
171  /// \brief Run the parser on the input source buffer.
172  virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
173 
174  virtual void setParsingInlineAsm(bool V) = 0;
175  virtual bool isParsingInlineAsm() = 0;
176 
177  /// \brief Parse MS-style inline assembly.
178  virtual bool parseMSInlineAsm(
179  void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
180  unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
181  SmallVectorImpl<std::string> &Constraints,
182  SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
183  const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) = 0;
184 
185  /// \brief Emit a note at the location \p L, with the message \p Msg.
186  virtual void Note(SMLoc L, const Twine &Msg, SMRange Range = None) = 0;
187 
188  /// \brief Emit a warning at the location \p L, with the message \p Msg.
189  ///
190  /// \return The return value is true, if warnings are fatal.
191  virtual bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) = 0;
192 
193  /// \brief Return an error at the location \p L, with the message \p Msg. This
194  /// may be modified before being emitted.
195  ///
196  /// \return The return value is always true, as an idiomatic convenience to
197  /// clients.
198  bool Error(SMLoc L, const Twine &Msg, SMRange Range = None);
199 
200  /// \brief Emit an error at the location \p L, with the message \p Msg.
201  ///
202  /// \return The return value is always true, as an idiomatic convenience to
203  /// clients.
204  virtual bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) = 0;
205 
206  bool hasPendingError() { return !PendingErrors.empty(); }
207 
209  bool rv = !PendingErrors.empty();
210  for (auto Err : PendingErrors) {
211  printError(Err.Loc, Twine(Err.Msg), Err.Range);
212  }
213  PendingErrors.clear();
214  return rv;
215  }
216 
217  bool addErrorSuffix(const Twine &Suffix);
218 
219  /// \brief Get the next AsmToken in the stream, possibly handling file
220  /// inclusion first.
221  virtual const AsmToken &Lex() = 0;
222 
223  /// \brief Get the current AsmToken from the stream.
224  const AsmToken &getTok() const;
225 
226  /// \brief Report an error at the current lexer location.
227  bool TokError(const Twine &Msg, SMRange Range = None);
228 
229  bool parseTokenLoc(SMLoc &Loc);
230  bool parseToken(AsmToken::TokenKind T, const Twine &Msg = "unexpected token");
231  /// \brief Attempt to parse and consume token, returning true on
232  /// success.
233  bool parseOptionalToken(AsmToken::TokenKind T);
234 
235  bool parseEOL(const Twine &ErrMsg);
236 
237  bool parseMany(function_ref<bool()> parseOne, bool hasComma = true);
238 
239  bool parseIntToken(int64_t &V, const Twine &ErrMsg);
240 
241  bool check(bool P, const Twine &Msg);
242  bool check(bool P, SMLoc Loc, const Twine &Msg);
243 
244  /// \brief Parse an identifier or string (as a quoted identifier) and set \p
245  /// Res to the identifier contents.
246  virtual bool parseIdentifier(StringRef &Res) = 0;
247 
248  /// \brief Parse up to the end of statement and return the contents from the
249  /// current token until the end of the statement; the current token on exit
250  /// will be either the EndOfStatement or EOF.
251  virtual StringRef parseStringToEndOfStatement() = 0;
252 
253  /// \brief Parse the current token as a string which may include escaped
254  /// characters and return the string contents.
255  virtual bool parseEscapedString(std::string &Data) = 0;
256 
257  /// \brief Skip to the end of the current statement, for error recovery.
258  virtual void eatToEndOfStatement() = 0;
259 
260  /// \brief Parse an arbitrary expression.
261  ///
262  /// \param Res - The value of the expression. The result is undefined
263  /// on error.
264  /// \return - False on success.
265  virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
266  bool parseExpression(const MCExpr *&Res);
267 
268  /// \brief Parse a primary expression.
269  ///
270  /// \param Res - The value of the expression. The result is undefined
271  /// on error.
272  /// \return - False on success.
273  virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) = 0;
274 
275  /// \brief Parse an arbitrary expression, assuming that an initial '(' has
276  /// already been consumed.
277  ///
278  /// \param Res - The value of the expression. The result is undefined
279  /// on error.
280  /// \return - False on success.
281  virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
282 
283  /// \brief Parse an expression which must evaluate to an absolute value.
284  ///
285  /// \param Res - The value of the absolute expression. The result is undefined
286  /// on error.
287  /// \return - False on success.
288  virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
289 
290  /// \brief Ensure that we have a valid section set in the streamer. Otherwise,
291  /// report an error and switch to .text.
292  /// \return - False on success.
293  virtual bool checkForValidSection() = 0;
294 
295  /// \brief Parse an arbitrary expression of a specified parenthesis depth,
296  /// assuming that the initial '(' characters have already been consumed.
297  ///
298  /// \param ParenDepth - Specifies how many trailing expressions outside the
299  /// current parentheses we have to parse.
300  /// \param Res - The value of the expression. The result is undefined
301  /// on error.
302  /// \return - False on success.
303  virtual bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
304  SMLoc &EndLoc) = 0;
305 };
306 
307 /// \brief Create an MCAsmParser instance.
309  const MCAsmInfo &, unsigned CB = 0);
310 
311 } // end namespace llvm
312 
313 #endif // LLVM_MC_MCPARSER_MCASMPARSER_H
Represents a range in source code.
Definition: SMLoc.h:49
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Generic assembler parser interface, for use by target specific assembly parsers.
Definition: MCAsmParser.h:110
virtual void setAssemblerDialect(unsigned i)
Definition: MCAsmParser.h:163
bool isKind(IdKind kind) const
Definition: MCAsmParser.h:66
MCTargetAsmParser - Generic interface to target specific assembly parsers.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:89
void setEnum(int64_t enumVal)
Definition: MCAsmParser.h:68
MCAsmParser * createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &, unsigned CB=0)
Create an MCAsmParser instance.
Definition: AsmParser.cpp:5830
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
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
bool printPendingErrors()
Definition: MCAsmParser.h:208
Target independent representation for an assembler token.
Definition: MCAsmLexer.h:27
void setEnablePrintSchedInfo(bool Value)
Definition: MCAsmParser.h:168
Context object for machine code objects.
Definition: MCContext.h:59
void setShowParsedOperands(bool Value)
Definition: MCAsmParser.h:166
bool(*)(MCAsmParserExtension *, StringRef, SMLoc) DirectiveHandler
Definition: MCAsmParser.h:112
const MCAsmLexer & getLexer() const
Definition: MCAsmParser.h:150
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
#define P(N)
Streaming machine code generation interface.
Definition: MCStreamer.h:169
bool hasPendingError()
Definition: MCAsmParser.h:206
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
Generic Sema callback for assembly parser.
Definition: MCAsmParser.h:95
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:41
void setVar(void *decl, bool isGlobalLV, unsigned size, unsigned type)
Definition: MCAsmParser.h:78
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
SmallVector< MCPendingError, 1 > PendingErrors
Definition: MCAsmParser.h:135
VariableIdentifier Var
Definition: MCAsmParser.h:64
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:41
bool shouldPrintSchedInfo()
Definition: MCAsmParser.h:169
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:61
bool getShowParsedOperands() const
Definition: MCAsmParser.h:165
virtual unsigned getAssemblerDialect()
Definition: MCAsmParser.h:162
void setLabel(void *decl)
Definition: MCAsmParser.h:73
MCTargetAsmParser & getTargetParser() const
Definition: MCAsmParser.h:159
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Generic interface for extending the MCAsmParser, which is implemented by target and object file assem...
LLVM Value Representation.
Definition: Value.h:73
std::pair< MCAsmParserExtension *, DirectiveHandler > ExtensionDirectiveHandler
Definition: MCAsmParser.h:114
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Represents a location in source code.
Definition: SMLoc.h:24