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/ArrayRef.h" 14 #include "llvm/ADT/StringRef.h" 15 #include "llvm/MC/MCParser/AsmLexer.h" 16 #include "llvm/Support/DataTypes.h" 17 18 namespace llvm { 19 class MCAsmInfo; 20 class MCAsmLexer; 21 class MCAsmParserExtension; 22 class MCContext; 23 class MCExpr; 24 class MCInstPrinter; 25 class MCInstrInfo; 26 class MCStreamer; 27 class MCTargetAsmParser; 28 class SMLoc; 29 class SMRange; 30 class SourceMgr; 31 class Twine; 32 33 class InlineAsmIdentifierInfo { 34 public: 35 void *OpDecl; 36 bool IsVarDecl; 37 unsigned Length, Size, Type; 38 39 void clear() { 40 OpDecl = nullptr; 41 IsVarDecl = false; 42 Length = 1; 43 Size = 0; 44 Type = 0; 45 } 46 }; 47 48 /// \brief Generic Sema callback for assembly parser. 49 class MCAsmParserSemaCallback { 50 public: 51 virtual ~MCAsmParserSemaCallback(); 52 virtual void *LookupInlineAsmIdentifier(StringRef &LineBuf, 53 InlineAsmIdentifierInfo &Info, 54 bool IsUnevaluatedContext) = 0; 55 virtual StringRef LookupInlineAsmLabel(StringRef Identifier, SourceMgr &SM, 56 SMLoc Location, bool Create) = 0; 57 58 virtual bool LookupInlineAsmField(StringRef Base, StringRef Member, 59 unsigned &Offset) = 0; 60 }; 61 62 /// \brief Generic assembler parser interface, for use by target specific 63 /// assembly parsers. 64 class MCAsmParser { 65 public: 66 typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc); 67 typedef std::pair<MCAsmParserExtension*, DirectiveHandler> 68 ExtensionDirectiveHandler; 69 70 private: 71 MCAsmParser(const MCAsmParser &) = delete; 72 void operator=(const MCAsmParser &) = delete; 73 74 MCTargetAsmParser *TargetParser; 75 76 unsigned ShowParsedOperands : 1; 77 78 protected: // Can only create subclasses. 79 MCAsmParser(); 80 81 public: 82 virtual ~MCAsmParser(); 83 84 virtual void addDirectiveHandler(StringRef Directive, 85 ExtensionDirectiveHandler Handler) = 0; 86 87 virtual void addAliasForDirective(StringRef Directive, StringRef Alias) = 0; 88 89 virtual SourceMgr &getSourceManager() = 0; 90 91 virtual MCAsmLexer &getLexer() = 0; 92 const MCAsmLexer &getLexer() const { 93 return const_cast<MCAsmParser*>(this)->getLexer(); 94 } 95 96 virtual MCContext &getContext() = 0; 97 98 /// \brief Return the output streamer for the assembler. 99 virtual MCStreamer &getStreamer() = 0; 100 101 MCTargetAsmParser &getTargetParser() const { return *TargetParser; } 102 void setTargetParser(MCTargetAsmParser &P); 103 104 virtual unsigned getAssemblerDialect() { return 0;} 105 virtual void setAssemblerDialect(unsigned i) { } 106 107 bool getShowParsedOperands() const { return ShowParsedOperands; } 108 void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; } 109 110 /// \brief Run the parser on the input source buffer. 111 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0; 112 113 virtual void setParsingInlineAsm(bool V) = 0; 114 virtual bool isParsingInlineAsm() = 0; 115 116 /// \brief Parse MS-style inline assembly. 117 virtual bool parseMSInlineAsm( 118 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs, 119 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls, 120 SmallVectorImpl<std::string> &Constraints, 121 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII, 122 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) = 0; 123 124 /// \brief Emit a note at the location \p L, with the message \p Msg. 125 virtual void Note(SMLoc L, const Twine &Msg, 126 ArrayRef<SMRange> Ranges = None) = 0; 127 128 /// \brief Emit a warning at the location \p L, with the message \p Msg. 129 /// 130 /// \return The return value is true, if warnings are fatal. 131 virtual bool Warning(SMLoc L, const Twine &Msg, 132 ArrayRef<SMRange> Ranges = None) = 0; 133 134 /// \brief Emit an error at the location \p L, with the message \p Msg. 135 /// 136 /// \return The return value is always true, as an idiomatic convenience to 137 /// clients. 138 virtual bool Error(SMLoc L, const Twine &Msg, 139 ArrayRef<SMRange> Ranges = None) = 0; 140 141 /// \brief Get the next AsmToken in the stream, possibly handling file 142 /// inclusion first. 143 virtual const AsmToken &Lex() = 0; 144 145 /// \brief Get the current AsmToken from the stream. 146 const AsmToken &getTok() const; 147 148 /// \brief Report an error at the current lexer location. 149 bool TokError(const Twine &Msg, ArrayRef<SMRange> Ranges = None); 150 151 /// \brief Parse an identifier or string (as a quoted identifier) and set \p 152 /// Res to the identifier contents. 153 virtual bool parseIdentifier(StringRef &Res) = 0; 154 155 /// \brief Parse up to the end of statement and return the contents from the 156 /// current token until the end of the statement; the current token on exit 157 /// will be either the EndOfStatement or EOF. 158 virtual StringRef parseStringToEndOfStatement() = 0; 159 160 /// \brief Parse the current token as a string which may include escaped 161 /// characters and return the string contents. 162 virtual bool parseEscapedString(std::string &Data) = 0; 163 164 /// \brief Skip to the end of the current statement, for error recovery. 165 virtual void eatToEndOfStatement() = 0; 166 167 /// \brief Parse an arbitrary expression. 168 /// 169 /// \param Res - The value of the expression. The result is undefined 170 /// on error. 171 /// \return - False on success. 172 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0; 173 bool parseExpression(const MCExpr *&Res); 174 175 /// \brief Parse a primary expression. 176 /// 177 /// \param Res - The value of the expression. The result is undefined 178 /// on error. 179 /// \return - False on success. 180 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) = 0; 181 182 /// \brief Parse an arbitrary expression, assuming that an initial '(' has 183 /// already been consumed. 184 /// 185 /// \param Res - The value of the expression. The result is undefined 186 /// on error. 187 /// \return - False on success. 188 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0; 189 190 /// \brief Parse an expression which must evaluate to an absolute value. 191 /// 192 /// \param Res - The value of the absolute expression. The result is undefined 193 /// on error. 194 /// \return - False on success. 195 virtual bool parseAbsoluteExpression(int64_t &Res) = 0; 196 197 /// \brief Ensure that we have a valid section set in the streamer. Otherwise, 198 /// report an error and switch to .text. 199 virtual void checkForValidSection() = 0; 200 201 /// \brief Parse an arbitrary expression of a specified parenthesis depth, 202 /// assuming that the initial '(' characters have already been consumed. 203 /// 204 /// \param ParenDepth - Specifies how many trailing expressions outside the 205 /// current parentheses we have to parse. 206 /// \param Res - The value of the expression. The result is undefined 207 /// on error. 208 /// \return - False on success. 209 virtual bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, 210 SMLoc &EndLoc) = 0; 211 }; 212 213 /// \brief Create an MCAsmParser instance. 214 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, 215 const MCAsmInfo &); 216 217 } // End llvm namespace 218 219 #endif 220