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" 18 #include "llvm/MC/MCParser/MCAsmLexer.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 36 struct 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() : Kind(IK_Invalid) {} 87 88 private: 89 // Discriminate using the current kind. 90 IdKind Kind; 91 }; 92 93 /// Generic Sema callback for assembly parser. 94 class MCAsmParserSemaCallback { 95 public: 96 virtual ~MCAsmParserSemaCallback(); 97 98 virtual void LookupInlineAsmIdentifier(StringRef &LineBuf, 99 InlineAsmIdentifierInfo &Info, 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: 111 using DirectiveHandler = bool (*)(MCAsmParserExtension*, StringRef, SMLoc); 112 using ExtensionDirectiveHandler = 113 std::pair<MCAsmParserExtension*, DirectiveHandler>; 114 115 struct MCPendingError { 116 SMLoc Loc; 117 SmallString<64> Msg; 118 SMRange Range; 119 }; 120 121 private: 122 MCTargetAsmParser *TargetParser = nullptr; 123 124 protected: // Can only create subclasses. 125 MCAsmParser(); 126 127 SmallVector<MCPendingError, 0> PendingErrors; 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 setParsingMSInlineAsm(bool V) = 0; 169 virtual bool isParsingMSInlineAsm() = 0; 170 171 virtual bool isParsingMasm() const { return false; } 172 173 virtual bool lookUpField(StringRef Name, StringRef &Type, 174 unsigned &Offset) const { 175 return true; 176 } 177 virtual bool lookUpField(StringRef Base, StringRef Member, StringRef &Type, 178 unsigned &Offset) const { 179 return true; 180 } 181 182 /// Parse MS-style inline assembly. 183 virtual bool parseMSInlineAsm( 184 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs, 185 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls, 186 SmallVectorImpl<std::string> &Constraints, 187 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII, 188 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) = 0; 189 190 /// Emit a note at the location \p L, with the message \p Msg. 191 virtual void Note(SMLoc L, const Twine &Msg, SMRange Range = None) = 0; 192 193 /// Emit a warning at the location \p L, with the message \p Msg. 194 /// 195 /// \return The return value is true, if warnings are fatal. 196 virtual bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) = 0; 197 198 /// Return an error at the location \p L, with the message \p Msg. This 199 /// may be modified before being emitted. 200 /// 201 /// \return The return value is always true, as an idiomatic convenience to 202 /// clients. 203 bool Error(SMLoc L, const Twine &Msg, SMRange Range = None); 204 205 /// Emit an error at the location \p L, with the message \p Msg. 206 /// 207 /// \return The return value is always true, as an idiomatic convenience to 208 /// clients. 209 virtual bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) = 0; 210 211 bool hasPendingError() { return !PendingErrors.empty(); } 212 213 bool printPendingErrors() { 214 bool rv = !PendingErrors.empty(); 215 for (auto Err : PendingErrors) { 216 printError(Err.Loc, Twine(Err.Msg), Err.Range); 217 } 218 PendingErrors.clear(); 219 return rv; 220 } 221 222 void clearPendingErrors() { PendingErrors.clear(); } 223 224 bool addErrorSuffix(const Twine &Suffix); 225 226 /// Get the next AsmToken in the stream, possibly handling file 227 /// inclusion first. 228 virtual const AsmToken &Lex() = 0; 229 230 /// Get the current AsmToken from the stream. 231 const AsmToken &getTok() const; 232 233 /// Report an error at the current lexer location. 234 bool TokError(const Twine &Msg, SMRange Range = None); 235 236 bool parseTokenLoc(SMLoc &Loc); 237 bool parseToken(AsmToken::TokenKind T, const Twine &Msg = "unexpected token"); 238 /// Attempt to parse and consume token, returning true on 239 /// success. 240 bool parseOptionalToken(AsmToken::TokenKind T); 241 242 bool parseEOL(const Twine &ErrMsg); 243 244 bool parseMany(function_ref<bool()> parseOne, bool hasComma = true); 245 246 bool parseIntToken(int64_t &V, const Twine &ErrMsg); 247 248 bool check(bool P, const Twine &Msg); 249 bool check(bool P, SMLoc Loc, const Twine &Msg); 250 251 /// Parse an identifier or string (as a quoted identifier) and set \p 252 /// Res to the identifier contents. 253 virtual bool parseIdentifier(StringRef &Res) = 0; 254 255 /// Parse up to the end of statement and return the contents from the 256 /// current token until the end of the statement; the current token on exit 257 /// will be either the EndOfStatement or EOF. 258 virtual StringRef parseStringToEndOfStatement() = 0; 259 260 /// Parse the current token as a string which may include escaped 261 /// characters and return the string contents. 262 virtual bool parseEscapedString(std::string &Data) = 0; 263 264 /// Parse an angle-bracket delimited string at the current position if one is 265 /// present, returning the string contents. 266 virtual bool parseAngleBracketString(std::string &Data) = 0; 267 268 /// Skip to the end of the current statement, for error recovery. 269 virtual void eatToEndOfStatement() = 0; 270 271 /// Parse an arbitrary expression. 272 /// 273 /// \param Res - The value of the expression. The result is undefined 274 /// on error. 275 /// \return - False on success. 276 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0; 277 bool parseExpression(const MCExpr *&Res); 278 279 /// Parse a primary expression. 280 /// 281 /// \param Res - The value of the expression. The result is undefined 282 /// on error. 283 /// \return - False on success. 284 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) = 0; 285 286 /// Parse an arbitrary expression, assuming that an initial '(' has 287 /// already been consumed. 288 /// 289 /// \param Res - The value of the expression. The result is undefined 290 /// on error. 291 /// \return - False on success. 292 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0; 293 294 /// Parse an expression which must evaluate to an absolute value. 295 /// 296 /// \param Res - The value of the absolute expression. The result is undefined 297 /// on error. 298 /// \return - False on success. 299 virtual bool parseAbsoluteExpression(int64_t &Res) = 0; 300 301 /// Ensure that we have a valid section set in the streamer. Otherwise, 302 /// report an error and switch to .text. 303 /// \return - False on success. 304 virtual bool checkForValidSection() = 0; 305 306 /// Parse an arbitrary expression of a specified parenthesis depth, 307 /// assuming that the initial '(' characters have already been consumed. 308 /// 309 /// \param ParenDepth - Specifies how many trailing expressions outside the 310 /// current parentheses we have to parse. 311 /// \param Res - The value of the expression. The result is undefined 312 /// on error. 313 /// \return - False on success. 314 virtual bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, 315 SMLoc &EndLoc) = 0; 316 }; 317 318 /// Create an MCAsmParser instance for parsing assembly similar to gas syntax 319 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, 320 const MCAsmInfo &, unsigned CB = 0); 321 322 /// Create an MCAsmParser instance for parsing Microsoft MASM-style assembly 323 MCAsmParser *createMCMasmParser(SourceMgr &, MCContext &, MCStreamer &, 324 const MCAsmInfo &, unsigned CB = 0); 325 326 } // end namespace llvm 327 328 #endif // LLVM_MC_MCPARSER_MCASMPARSER_H 329