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