Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:11

0001 //===- llvm/MC/MCAsmParser.h - Abstract Asm Parser Interface ----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_MC_MCPARSER_MCASMPARSER_H
0010 #define LLVM_MC_MCPARSER_MCASMPARSER_H
0011 
0012 #include "llvm/ADT/STLFunctionalExtras.h"
0013 #include "llvm/ADT/SmallString.h"
0014 #include "llvm/ADT/SmallVector.h"
0015 #include "llvm/ADT/StringRef.h"
0016 #include "llvm/ADT/Twine.h"
0017 #include "llvm/MC/MCAsmMacro.h"
0018 #include "llvm/Support/SMLoc.h"
0019 #include <cstdint>
0020 #include <string>
0021 #include <utility>
0022 
0023 namespace llvm {
0024 
0025 class MCAsmLexer;
0026 class MCAsmInfo;
0027 class MCAsmParserExtension;
0028 class MCContext;
0029 class MCExpr;
0030 class MCInstPrinter;
0031 class MCInstrInfo;
0032 class MCStreamer;
0033 class MCTargetAsmParser;
0034 class SourceMgr;
0035 
0036 struct InlineAsmIdentifierInfo {
0037   enum IdKind {
0038     IK_Invalid,  // Initial state. Unexpected after a successful parsing.
0039     IK_Label,    // Function/Label reference.
0040     IK_EnumVal,  // Value of enumeration type.
0041     IK_Var       // Variable.
0042   };
0043   // Represents an Enum value
0044   struct EnumIdentifier {
0045     int64_t EnumVal;
0046   };
0047   // Represents a label/function reference
0048   struct LabelIdentifier {
0049     void *Decl;
0050   };
0051   // Represents a variable
0052   struct VariableIdentifier {
0053     void *Decl;
0054     bool IsGlobalLV;
0055     unsigned Length;
0056     unsigned Size;
0057     unsigned Type;
0058   };
0059   // An InlineAsm identifier can only be one of those
0060   union {
0061     EnumIdentifier Enum;
0062     LabelIdentifier Label;
0063     VariableIdentifier Var;
0064   };
0065   bool isKind(IdKind kind) const { return Kind == kind; }
0066   // Initializers
0067   void setEnum(int64_t enumVal) {
0068     assert(isKind(IK_Invalid) && "should be initialized only once");
0069     Kind = IK_EnumVal;
0070     Enum.EnumVal = enumVal;
0071   }
0072   void setLabel(void *decl) {
0073     assert(isKind(IK_Invalid) && "should be initialized only once");
0074     Kind = IK_Label;
0075     Label.Decl = decl;
0076   }
0077   void setVar(void *decl, bool isGlobalLV, unsigned size, unsigned type) {
0078     assert(isKind(IK_Invalid) && "should be initialized only once");
0079     Kind = IK_Var;
0080     Var.Decl = decl;
0081     Var.IsGlobalLV = isGlobalLV;
0082     Var.Size = size;
0083     Var.Type = type;
0084     Var.Length = size / type;
0085   }
0086   InlineAsmIdentifierInfo() = default;
0087 
0088 private:
0089   // Discriminate using the current kind.
0090   IdKind Kind = IK_Invalid;
0091 };
0092 
0093 // Generic type information for an assembly object.
0094 // All sizes measured in bytes.
0095 struct AsmTypeInfo {
0096   StringRef Name;
0097   unsigned Size = 0;
0098   unsigned ElementSize = 0;
0099   unsigned Length = 0;
0100 };
0101 
0102 struct AsmFieldInfo {
0103   AsmTypeInfo Type;
0104   unsigned Offset = 0;
0105 };
0106 
0107 /// Generic Sema callback for assembly parser.
0108 class MCAsmParserSemaCallback {
0109 public:
0110   virtual ~MCAsmParserSemaCallback();
0111 
0112   virtual void LookupInlineAsmIdentifier(StringRef &LineBuf,
0113                                          InlineAsmIdentifierInfo &Info,
0114                                          bool IsUnevaluatedContext) = 0;
0115   virtual StringRef LookupInlineAsmLabel(StringRef Identifier, SourceMgr &SM,
0116                                          SMLoc Location, bool Create) = 0;
0117   virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
0118                                     unsigned &Offset) = 0;
0119 };
0120 
0121 /// Generic assembler parser interface, for use by target specific
0122 /// assembly parsers.
0123 class MCAsmParser {
0124 public:
0125   using DirectiveHandler = bool (*)(MCAsmParserExtension*, StringRef, SMLoc);
0126   using ExtensionDirectiveHandler =
0127       std::pair<MCAsmParserExtension*, DirectiveHandler>;
0128 
0129   struct MCPendingError {
0130     SMLoc Loc;
0131     SmallString<64> Msg;
0132     SMRange Range;
0133   };
0134 
0135 private:
0136   MCTargetAsmParser *TargetParser = nullptr;
0137 
0138 protected: // Can only create subclasses.
0139   MCAsmParser();
0140 
0141   SmallVector<MCPendingError, 0> PendingErrors;
0142 
0143   /// Flag tracking whether any errors have been encountered.
0144   bool HadError = false;
0145 
0146   bool ShowParsedOperands = false;
0147 
0148 public:
0149   MCAsmParser(const MCAsmParser &) = delete;
0150   MCAsmParser &operator=(const MCAsmParser &) = delete;
0151   virtual ~MCAsmParser();
0152 
0153   virtual void addDirectiveHandler(StringRef Directive,
0154                                    ExtensionDirectiveHandler Handler) = 0;
0155 
0156   virtual void addAliasForDirective(StringRef Directive, StringRef Alias) = 0;
0157 
0158   virtual SourceMgr &getSourceManager() = 0;
0159 
0160   virtual MCAsmLexer &getLexer() = 0;
0161   const MCAsmLexer &getLexer() const {
0162     return const_cast<MCAsmParser*>(this)->getLexer();
0163   }
0164 
0165   virtual MCContext &getContext() = 0;
0166 
0167   /// Return the output streamer for the assembler.
0168   virtual MCStreamer &getStreamer() = 0;
0169 
0170   MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
0171   void setTargetParser(MCTargetAsmParser &P);
0172 
0173   virtual unsigned getAssemblerDialect() { return 0;}
0174   virtual void setAssemblerDialect(unsigned i) { }
0175 
0176   bool getShowParsedOperands() const { return ShowParsedOperands; }
0177   void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
0178 
0179   /// Run the parser on the input source buffer.
0180   virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
0181 
0182   virtual void setParsingMSInlineAsm(bool V) = 0;
0183   virtual bool isParsingMSInlineAsm() = 0;
0184 
0185   virtual bool discardLTOSymbol(StringRef) const { return false; }
0186 
0187   virtual bool isParsingMasm() const { return false; }
0188 
0189   virtual bool defineMacro(StringRef Name, StringRef Value) { return true; }
0190 
0191   virtual bool lookUpField(StringRef Name, AsmFieldInfo &Info) const {
0192     return true;
0193   }
0194   virtual bool lookUpField(StringRef Base, StringRef Member,
0195                            AsmFieldInfo &Info) const {
0196     return true;
0197   }
0198 
0199   virtual bool lookUpType(StringRef Name, AsmTypeInfo &Info) const {
0200     return true;
0201   }
0202 
0203   /// Parse MS-style inline assembly.
0204   virtual bool parseMSInlineAsm(
0205       std::string &AsmString, unsigned &NumOutputs, unsigned &NumInputs,
0206       SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
0207       SmallVectorImpl<std::string> &Constraints,
0208       SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
0209       MCInstPrinter *IP, MCAsmParserSemaCallback &SI) = 0;
0210 
0211   /// Emit a note at the location \p L, with the message \p Msg.
0212   virtual void Note(SMLoc L, const Twine &Msg,
0213                     SMRange Range = std::nullopt) = 0;
0214 
0215   /// Emit a warning at the location \p L, with the message \p Msg.
0216   ///
0217   /// \return The return value is true, if warnings are fatal.
0218   virtual bool Warning(SMLoc L, const Twine &Msg,
0219                        SMRange Range = std::nullopt) = 0;
0220 
0221   /// Return an error at the location \p L, with the message \p Msg. This
0222   /// may be modified before being emitted.
0223   ///
0224   /// \return The return value is always true, as an idiomatic convenience to
0225   /// clients.
0226   bool Error(SMLoc L, const Twine &Msg, SMRange Range = std::nullopt);
0227 
0228   /// Emit an error at the location \p L, with the message \p Msg.
0229   ///
0230   /// \return The return value is always true, as an idiomatic convenience to
0231   /// clients.
0232   virtual bool printError(SMLoc L, const Twine &Msg,
0233                           SMRange Range = std::nullopt) = 0;
0234 
0235   bool hasPendingError() { return !PendingErrors.empty(); }
0236 
0237   bool printPendingErrors() {
0238     bool rv = !PendingErrors.empty();
0239     for (auto &Err : PendingErrors) {
0240       printError(Err.Loc, Twine(Err.Msg), Err.Range);
0241     }
0242     PendingErrors.clear();
0243     return rv;
0244   }
0245 
0246   void clearPendingErrors() { PendingErrors.clear(); }
0247 
0248   bool addErrorSuffix(const Twine &Suffix);
0249 
0250   /// Get the next AsmToken in the stream, possibly handling file
0251   /// inclusion first.
0252   virtual const AsmToken &Lex() = 0;
0253 
0254   /// Get the current AsmToken from the stream.
0255   const AsmToken &getTok() const;
0256 
0257   /// Report an error at the current lexer location.
0258   bool TokError(const Twine &Msg, SMRange Range = std::nullopt);
0259 
0260   bool parseTokenLoc(SMLoc &Loc);
0261   bool parseToken(AsmToken::TokenKind T, const Twine &Msg = "unexpected token");
0262   /// Attempt to parse and consume token, returning true on
0263   /// success.
0264   bool parseOptionalToken(AsmToken::TokenKind T);
0265 
0266   bool parseComma() { return parseToken(AsmToken::Comma, "expected comma"); }
0267   bool parseRParen() { return parseToken(AsmToken::RParen, "expected ')'"); }
0268   bool parseEOL();
0269   bool parseEOL(const Twine &ErrMsg);
0270 
0271   bool parseMany(function_ref<bool()> parseOne, bool hasComma = true);
0272 
0273   bool parseIntToken(int64_t &V, const Twine &ErrMsg);
0274 
0275   bool check(bool P, const Twine &Msg);
0276   bool check(bool P, SMLoc Loc, const Twine &Msg);
0277 
0278   /// Parse an identifier or string (as a quoted identifier) and set \p
0279   /// Res to the identifier contents.
0280   virtual bool parseIdentifier(StringRef &Res) = 0;
0281 
0282   /// Parse up to the end of statement and return the contents from the
0283   /// current token until the end of the statement; the current token on exit
0284   /// will be either the EndOfStatement or EOF.
0285   virtual StringRef parseStringToEndOfStatement() = 0;
0286 
0287   /// Parse the current token as a string which may include escaped
0288   /// characters and return the string contents.
0289   virtual bool parseEscapedString(std::string &Data) = 0;
0290 
0291   /// Parse an angle-bracket delimited string at the current position if one is
0292   /// present, returning the string contents.
0293   virtual bool parseAngleBracketString(std::string &Data) = 0;
0294 
0295   /// Skip to the end of the current statement, for error recovery.
0296   virtual void eatToEndOfStatement() = 0;
0297 
0298   /// Parse an arbitrary expression.
0299   ///
0300   /// \param Res - The value of the expression. The result is undefined
0301   /// on error.
0302   /// \return - False on success.
0303   virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
0304   bool parseExpression(const MCExpr *&Res);
0305 
0306   /// Parse a primary expression.
0307   ///
0308   /// \param Res - The value of the expression. The result is undefined
0309   /// on error.
0310   /// \return - False on success.
0311   virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
0312                                 AsmTypeInfo *TypeInfo) = 0;
0313 
0314   /// Parse an arbitrary expression, assuming that an initial '(' has
0315   /// already been consumed.
0316   ///
0317   /// \param Res - The value of the expression. The result is undefined
0318   /// on error.
0319   /// \return - False on success.
0320   virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
0321 
0322   /// Parse an expression which must evaluate to an absolute value.
0323   ///
0324   /// \param Res - The value of the absolute expression. The result is undefined
0325   /// on error.
0326   /// \return - False on success.
0327   virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
0328 
0329   /// Ensure that we have a valid section set in the streamer. Otherwise,
0330   /// report an error and switch to .text.
0331   /// \return - False on success.
0332   virtual bool checkForValidSection() = 0;
0333 
0334   /// Parse an arbitrary expression of a specified parenthesis depth,
0335   /// assuming that the initial '(' characters have already been consumed.
0336   ///
0337   /// \param ParenDepth - Specifies how many trailing expressions outside the
0338   /// current parentheses we have to parse.
0339   /// \param Res - The value of the expression. The result is undefined
0340   /// on error.
0341   /// \return - False on success.
0342   virtual bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
0343                                      SMLoc &EndLoc) = 0;
0344 
0345   /// Parse a .gnu_attribute.
0346   bool parseGNUAttribute(SMLoc L, int64_t &Tag, int64_t &IntegerValue);
0347 };
0348 
0349 /// Create an MCAsmParser instance for parsing assembly similar to gas syntax
0350 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &,
0351                                const MCAsmInfo &, unsigned CB = 0);
0352 
0353 /// Create an MCAsmParser instance for parsing Microsoft MASM-style assembly
0354 MCAsmParser *createMCMasmParser(SourceMgr &, MCContext &, MCStreamer &,
0355                                 const MCAsmInfo &, struct tm, unsigned CB = 0);
0356 
0357 } // end namespace llvm
0358 
0359 #endif // LLVM_MC_MCPARSER_MCASMPARSER_H