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/STLFunctionalExtras.h"
13#include "llvm/ADT/SmallString.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/MC/MCAsmMacro.h"
18#include "llvm/Support/SMLoc.h"
19#include <cstdint>
20#include <string>
21#include <utility>
22
23namespace llvm {
24
25class MCAsmLexer;
26class MCAsmInfo;
27class MCAsmParserExtension;
28class MCContext;
29class MCExpr;
30class MCInstPrinter;
31class MCInstrInfo;
32class MCStreamer;
33class MCTargetAsmParser;
34class SourceMgr;
35
36struct InlineAsmIdentifierInfo {
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
52  struct VariableIdentifier {
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 {
61    EnumIdentifier Enum;
62    LabelIdentifier Label;
63    VariableIdentifier Var;
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  }
86  InlineAsmIdentifierInfo() = default;
87
88private:
89  // Discriminate using the current kind.
90  IdKind Kind = IK_Invalid;
91};
92
93// Generic type information for an assembly object.
94// All sizes measured in bytes.
95struct AsmTypeInfo {
96  StringRef Name;
97  unsigned Size = 0;
98  unsigned ElementSize = 0;
99  unsigned Length = 0;
100};
101
102struct AsmFieldInfo {
103  AsmTypeInfo Type;
104  unsigned Offset = 0;
105};
106
107/// Generic Sema callback for assembly parser.
108class MCAsmParserSemaCallback {
109public:
110  virtual ~MCAsmParserSemaCallback();
111
112  virtual void LookupInlineAsmIdentifier(StringRef &LineBuf,
113                                         InlineAsmIdentifierInfo &Info,
114                                         bool IsUnevaluatedContext) = 0;
115  virtual StringRef LookupInlineAsmLabel(StringRef Identifier, SourceMgr &SM,
116                                         SMLoc Location, bool Create) = 0;
117  virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
118                                    unsigned &Offset) = 0;
119};
120
121/// Generic assembler parser interface, for use by target specific
122/// assembly parsers.
123class MCAsmParser {
124public:
125  using DirectiveHandler = bool (*)(MCAsmParserExtension*, StringRef, SMLoc);
126  using ExtensionDirectiveHandler =
127      std::pair<MCAsmParserExtension*, DirectiveHandler>;
128
129  struct MCPendingError {
130    SMLoc Loc;
131    SmallString<64> Msg;
132    SMRange Range;
133  };
134
135private:
136  MCTargetAsmParser *TargetParser = nullptr;
137
138protected: // Can only create subclasses.
139  MCAsmParser();
140
141  SmallVector<MCPendingError, 0> PendingErrors;
142
143  /// Flag tracking whether any errors have been encountered.
144  bool HadError = false;
145
146  bool ShowParsedOperands = false;
147
148public:
149  MCAsmParser(const MCAsmParser &) = delete;
150  MCAsmParser &operator=(const MCAsmParser &) = delete;
151  virtual ~MCAsmParser();
152
153  virtual void addDirectiveHandler(StringRef Directive,
154                                   ExtensionDirectiveHandler Handler) = 0;
155
156  virtual void addAliasForDirective(StringRef Directive, StringRef Alias) = 0;
157
158  virtual SourceMgr &getSourceManager() = 0;
159
160  virtual MCAsmLexer &getLexer() = 0;
161  const MCAsmLexer &getLexer() const {
162    return const_cast<MCAsmParser*>(this)->getLexer();
163  }
164
165  virtual MCContext &getContext() = 0;
166
167  /// Return the output streamer for the assembler.
168  virtual MCStreamer &getStreamer() = 0;
169
170  MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
171  void setTargetParser(MCTargetAsmParser &P);
172
173  virtual unsigned getAssemblerDialect() { return 0;}
174  virtual void setAssemblerDialect(unsigned i) { }
175
176  bool getShowParsedOperands() const { return ShowParsedOperands; }
177  void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
178
179  /// Run the parser on the input source buffer.
180  virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
181
182  virtual void setParsingMSInlineAsm(bool V) = 0;
183  virtual bool isParsingMSInlineAsm() = 0;
184
185  virtual bool discardLTOSymbol(StringRef) const { return false; }
186
187  virtual bool isParsingMasm() const { return false; }
188
189  virtual bool defineMacro(StringRef Name, StringRef Value) { return true; }
190
191  virtual bool lookUpField(StringRef Name, AsmFieldInfo &Info) const {
192    return true;
193  }
194  virtual bool lookUpField(StringRef Base, StringRef Member,
195                           AsmFieldInfo &Info) const {
196    return true;
197  }
198
199  virtual bool lookUpType(StringRef Name, AsmTypeInfo &Info) const {
200    return true;
201  }
202
203  /// Parse MS-style inline assembly.
204  virtual bool parseMSInlineAsm(
205      std::string &AsmString, unsigned &NumOutputs, unsigned &NumInputs,
206      SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
207      SmallVectorImpl<std::string> &Constraints,
208      SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
209      const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) = 0;
210
211  /// Emit a note at the location \p L, with the message \p Msg.
212  virtual void Note(SMLoc L, const Twine &Msg,
213                    SMRange Range = std::nullopt) = 0;
214
215  /// Emit a warning at the location \p L, with the message \p Msg.
216  ///
217  /// \return The return value is true, if warnings are fatal.
218  virtual bool Warning(SMLoc L, const Twine &Msg,
219                       SMRange Range = std::nullopt) = 0;
220
221  /// Return an error at the location \p L, with the message \p Msg. This
222  /// may be modified before being emitted.
223  ///
224  /// \return The return value is always true, as an idiomatic convenience to
225  /// clients.
226  bool Error(SMLoc L, const Twine &Msg, SMRange Range = std::nullopt);
227
228  /// Emit an error at the location \p L, with the message \p Msg.
229  ///
230  /// \return The return value is always true, as an idiomatic convenience to
231  /// clients.
232  virtual bool printError(SMLoc L, const Twine &Msg,
233                          SMRange Range = std::nullopt) = 0;
234
235  bool hasPendingError() { return !PendingErrors.empty(); }
236
237  bool printPendingErrors() {
238    bool rv = !PendingErrors.empty();
239    for (auto &Err : PendingErrors) {
240      printError(Err.Loc, Twine(Err.Msg), Err.Range);
241    }
242    PendingErrors.clear();
243    return rv;
244  }
245
246  void clearPendingErrors() { PendingErrors.clear(); }
247
248  bool addErrorSuffix(const Twine &Suffix);
249
250  /// Get the next AsmToken in the stream, possibly handling file
251  /// inclusion first.
252  virtual const AsmToken &Lex() = 0;
253
254  /// Get the current AsmToken from the stream.
255  const AsmToken &getTok() const;
256
257  /// Report an error at the current lexer location.
258  bool TokError(const Twine &Msg, SMRange Range = std::nullopt);
259
260  bool parseTokenLoc(SMLoc &Loc);
261  bool parseToken(AsmToken::TokenKind T, const Twine &Msg = "unexpected token");
262  /// Attempt to parse and consume token, returning true on
263  /// success.
264  bool parseOptionalToken(AsmToken::TokenKind T);
265
266  bool parseComma() { return parseToken(AsmToken::Comma, "expected comma"); }
267  bool parseRParen() { return parseToken(AsmToken::RParen, "expected ')'"); }
268  bool parseEOL();
269  bool parseEOL(const Twine &ErrMsg);
270
271  bool parseMany(function_ref<bool()> parseOne, bool hasComma = true);
272
273  bool parseIntToken(int64_t &V, const Twine &ErrMsg);
274
275  bool check(bool P, const Twine &Msg);
276  bool check(bool P, SMLoc Loc, const Twine &Msg);
277
278  /// Parse an identifier or string (as a quoted identifier) and set \p
279  /// Res to the identifier contents.
280  virtual bool parseIdentifier(StringRef &Res) = 0;
281
282  /// Parse up to the end of statement and return the contents from the
283  /// current token until the end of the statement; the current token on exit
284  /// will be either the EndOfStatement or EOF.
285  virtual StringRef parseStringToEndOfStatement() = 0;
286
287  /// Parse the current token as a string which may include escaped
288  /// characters and return the string contents.
289  virtual bool parseEscapedString(std::string &Data) = 0;
290
291  /// Parse an angle-bracket delimited string at the current position if one is
292  /// present, returning the string contents.
293  virtual bool parseAngleBracketString(std::string &Data) = 0;
294
295  /// Skip to the end of the current statement, for error recovery.
296  virtual void eatToEndOfStatement() = 0;
297
298  /// Parse an arbitrary expression.
299  ///
300  /// \param Res - The value of the expression. The result is undefined
301  /// on error.
302  /// \return - False on success.
303  virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
304  bool parseExpression(const MCExpr *&Res);
305
306  /// Parse a primary expression.
307  ///
308  /// \param Res - The value of the expression. The result is undefined
309  /// on error.
310  /// \return - False on success.
311  virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
312                                AsmTypeInfo *TypeInfo) = 0;
313
314  /// Parse an arbitrary expression, assuming that an initial '(' has
315  /// already been consumed.
316  ///
317  /// \param Res - The value of the expression. The result is undefined
318  /// on error.
319  /// \return - False on success.
320  virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
321
322  /// Parse an expression which must evaluate to an absolute value.
323  ///
324  /// \param Res - The value of the absolute expression. The result is undefined
325  /// on error.
326  /// \return - False on success.
327  virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
328
329  /// Ensure that we have a valid section set in the streamer. Otherwise,
330  /// report an error and switch to .text.
331  /// \return - False on success.
332  virtual bool checkForValidSection() = 0;
333
334  /// Parse an arbitrary expression of a specified parenthesis depth,
335  /// assuming that the initial '(' characters have already been consumed.
336  ///
337  /// \param ParenDepth - Specifies how many trailing expressions outside the
338  /// current parentheses we have to parse.
339  /// \param Res - The value of the expression. The result is undefined
340  /// on error.
341  /// \return - False on success.
342  virtual bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
343                                     SMLoc &EndLoc) = 0;
344
345  /// Parse a .gnu_attribute.
346  bool parseGNUAttribute(SMLoc L, int64_t &Tag, int64_t &IntegerValue);
347};
348
349/// Create an MCAsmParser instance for parsing assembly similar to gas syntax
350MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &,
351                               const MCAsmInfo &, unsigned CB = 0);
352
353/// Create an MCAsmParser instance for parsing Microsoft MASM-style assembly
354MCAsmParser *createMCMasmParser(SourceMgr &, MCContext &, MCStreamer &,
355                                const MCAsmInfo &, struct tm, unsigned CB = 0);
356
357} // end namespace llvm
358
359#endif // LLVM_MC_MCPARSER_MCASMPARSER_H
360