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