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 /// MCAsmParserSemaCallback - 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 56 virtual bool LookupInlineAsmField(StringRef Base, StringRef Member, 57 unsigned &Offset) = 0; 58 }; 59 60 /// MCAsmParser - Generic assembler parser interface, for use by target specific 61 /// assembly parsers. 62 class MCAsmParser { 63 public: 64 typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc); 65 typedef std::pair<MCAsmParserExtension*, DirectiveHandler> 66 ExtensionDirectiveHandler; 67 68 private: 69 MCAsmParser(const MCAsmParser &) LLVM_DELETED_FUNCTION; 70 void operator=(const MCAsmParser &) LLVM_DELETED_FUNCTION; 71 72 MCTargetAsmParser *TargetParser; 73 74 unsigned ShowParsedOperands : 1; 75 76 protected: // Can only create subclasses. 77 MCAsmParser(); 78 79 public: 80 virtual ~MCAsmParser(); 81 82 virtual void addDirectiveHandler(StringRef Directive, 83 ExtensionDirectiveHandler Handler) = 0; 84 85 virtual SourceMgr &getSourceManager() = 0; 86 87 virtual MCAsmLexer &getLexer() = 0; 88 89 virtual MCContext &getContext() = 0; 90 91 /// getStreamer - Return the output streamer for the assembler. 92 virtual MCStreamer &getStreamer() = 0; 93 94 MCTargetAsmParser &getTargetParser() const { return *TargetParser; } 95 void setTargetParser(MCTargetAsmParser &P); 96 97 virtual unsigned getAssemblerDialect() { return 0;} 98 virtual void setAssemblerDialect(unsigned i) { } 99 100 bool getShowParsedOperands() const { return ShowParsedOperands; } 101 void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; } 102 103 /// Run - Run the parser on the input source buffer. 104 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0; 105 106 virtual void setParsingInlineAsm(bool V) = 0; 107 virtual bool isParsingInlineAsm() = 0; 108 109 /// parseMSInlineAsm - Parse ms-style inline assembly. 110 virtual bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString, 111 unsigned &NumOutputs, unsigned &NumInputs, 112 SmallVectorImpl<std::pair<void *, bool> > &OpDecls, 113 SmallVectorImpl<std::string> &Constraints, 114 SmallVectorImpl<std::string> &Clobbers, 115 const MCInstrInfo *MII, 116 const MCInstPrinter *IP, 117 MCAsmParserSemaCallback &SI) = 0; 118 119 /// Note - Emit a note at the location \p L, with the message \p Msg. 120 virtual void Note(SMLoc L, const Twine &Msg, 121 ArrayRef<SMRange> Ranges = None) = 0; 122 123 /// Warning - Emit a warning at the location \p L, with the message \p Msg. 124 /// 125 /// \return The return value is true, if warnings are fatal. 126 virtual bool Warning(SMLoc L, const Twine &Msg, 127 ArrayRef<SMRange> Ranges = None) = 0; 128 129 /// Error - Emit an error at the location \p L, with the message \p Msg. 130 /// 131 /// \return The return value is always true, as an idiomatic convenience to 132 /// clients. 133 virtual bool Error(SMLoc L, const Twine &Msg, 134 ArrayRef<SMRange> Ranges = None) = 0; 135 136 /// Lex - Get the next AsmToken in the stream, possibly handling file 137 /// inclusion first. 138 virtual const AsmToken &Lex() = 0; 139 140 /// getTok - Get the current AsmToken from the stream. 141 const AsmToken &getTok(); 142 143 /// \brief Report an error at the current lexer location. 144 bool TokError(const Twine &Msg, ArrayRef<SMRange> Ranges = None); 145 146 /// parseIdentifier - Parse an identifier or string (as a quoted identifier) 147 /// and set \p Res to the identifier contents. 148 virtual bool parseIdentifier(StringRef &Res) = 0; 149 150 /// \brief Parse up to the end of statement and return the contents from the 151 /// current token until the end of the statement; the current token on exit 152 /// will be either the EndOfStatement or EOF. 153 virtual StringRef parseStringToEndOfStatement() = 0; 154 155 /// parseEscapedString - Parse the current token as a string which may include 156 /// escaped characters and return the string contents. 157 virtual bool parseEscapedString(std::string &Data) = 0; 158 159 /// eatToEndOfStatement - Skip to the end of the current statement, for error 160 /// recovery. 161 virtual void eatToEndOfStatement() = 0; 162 163 /// parseExpression - Parse an arbitrary expression. 164 /// 165 /// @param Res - The value of the expression. The result is undefined 166 /// on error. 167 /// @result - False on success. 168 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0; 169 bool parseExpression(const MCExpr *&Res); 170 171 /// parsePrimaryExpr - Parse a primary expression. 172 /// 173 /// @param Res - The value of the expression. The result is undefined 174 /// on error. 175 /// @result - False on success. 176 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) = 0; 177 178 /// parseParenExpression - Parse an arbitrary expression, assuming that an 179 /// initial '(' has already been consumed. 180 /// 181 /// @param Res - The value of the expression. The result is undefined 182 /// on error. 183 /// @result - False on success. 184 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0; 185 186 /// parseAbsoluteExpression - Parse an expression which must evaluate to an 187 /// absolute value. 188 /// 189 /// @param Res - The value of the absolute expression. The result is undefined 190 /// on error. 191 /// @result - False on success. 192 virtual bool parseAbsoluteExpression(int64_t &Res) = 0; 193 194 /// checkForValidSection - Ensure that we have a valid section set in the 195 /// streamer. Otherwise, report an error and switch to .text. 196 virtual void checkForValidSection() = 0; 197 }; 198 199 /// \brief Create an MCAsmParser instance. 200 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &, 201 MCStreamer &, const MCAsmInfo &); 202 203 } // End llvm namespace 204 205 #endif 206