1 //===-- llvm/MC/MCAsmParser.h - Abstract Asm Parser Interface ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCPARSER_MCASMPARSER_H
11 #define LLVM_MC_MCPARSER_MCASMPARSER_H
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"
21 class MCAsmParserExtension;
27 class MCTargetAsmParser;
33 class InlineAsmIdentifierInfo {
37 unsigned Length, Size, Type;
48 /// Generic Sema callback for assembly parser.
49 class MCAsmParserSemaCallback {
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;
58 virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
59 unsigned &Offset) = 0;
62 /// Generic assembler parser interface, for use by target specific assembly
66 typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc);
67 typedef std::pair<MCAsmParserExtension*, DirectiveHandler>
68 ExtensionDirectiveHandler;
71 MCAsmParser(const MCAsmParser &) = delete;
72 void operator=(const MCAsmParser &) = delete;
74 MCTargetAsmParser *TargetParser;
76 unsigned ShowParsedOperands : 1;
78 protected: // Can only create subclasses.
82 virtual ~MCAsmParser();
84 virtual void addDirectiveHandler(StringRef Directive,
85 ExtensionDirectiveHandler Handler) = 0;
87 virtual SourceMgr &getSourceManager() = 0;
89 virtual MCAsmLexer &getLexer() = 0;
90 const MCAsmLexer &getLexer() const {
91 return const_cast<MCAsmParser*>(this)->getLexer();
94 virtual MCContext &getContext() = 0;
96 /// Return the output streamer for the assembler.
97 virtual MCStreamer &getStreamer() = 0;
99 MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
100 void setTargetParser(MCTargetAsmParser &P);
102 virtual unsigned getAssemblerDialect() { return 0;}
103 virtual void setAssemblerDialect(unsigned i) { }
105 bool getShowParsedOperands() const { return ShowParsedOperands; }
106 void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
108 /// Run the parser on the input source buffer.
109 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
111 virtual void setParsingInlineAsm(bool V) = 0;
112 virtual bool isParsingInlineAsm() = 0;
114 /// Parse ms-style inline assembly.
115 virtual bool parseMSInlineAsm(
116 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
117 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
118 SmallVectorImpl<std::string> &Constraints,
119 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
120 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) = 0;
122 /// Emit a note at the location \p L, with the message \p Msg.
123 virtual void Note(SMLoc L, const Twine &Msg,
124 ArrayRef<SMRange> Ranges = None) = 0;
126 /// Emit a warning at the location \p L, with the message \p Msg.
128 /// \return The return value is true, if warnings are fatal.
129 virtual bool Warning(SMLoc L, const Twine &Msg,
130 ArrayRef<SMRange> Ranges = None) = 0;
132 /// Emit an error at the location \p L, with the message \p Msg.
134 /// \return The return value is always true, as an idiomatic convenience to
136 virtual bool Error(SMLoc L, const Twine &Msg,
137 ArrayRef<SMRange> Ranges = None) = 0;
139 /// Get the next AsmToken in the stream, possibly handling file inclusion
141 virtual const AsmToken &Lex() = 0;
143 /// Get the current AsmToken from the stream.
144 const AsmToken &getTok() const;
146 /// \brief Report an error at the current lexer location.
147 bool TokError(const Twine &Msg, ArrayRef<SMRange> Ranges = None);
149 /// Parse an identifier or string (as a quoted identifier) and set \p Res to
150 /// the identifier contents.
151 virtual bool parseIdentifier(StringRef &Res) = 0;
153 /// \brief Parse up to the end of statement and return the contents from the
154 /// current token until the end of the statement; the current token on exit
155 /// will be either the EndOfStatement or EOF.
156 virtual StringRef parseStringToEndOfStatement() = 0;
158 /// Parse the current token as a string which may include escaped characters
159 /// and return the string contents.
160 virtual bool parseEscapedString(std::string &Data) = 0;
162 /// Skip to the end of the current statement, for error recovery.
163 virtual void eatToEndOfStatement() = 0;
165 /// Parse an arbitrary expression.
167 /// @param Res - The value of the expression. The result is undefined
169 /// @result - False on success.
170 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
171 bool parseExpression(const MCExpr *&Res);
173 /// Parse a primary expression.
175 /// @param Res - The value of the expression. The result is undefined
177 /// @result - False on success.
178 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) = 0;
180 /// Parse an arbitrary expression, assuming that an initial '(' has already
183 /// @param Res - The value of the expression. The result is undefined
185 /// @result - False on success.
186 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
188 /// Parse an expression which must evaluate to an absolute value.
190 /// @param Res - The value of the absolute expression. The result is undefined
192 /// @result - False on success.
193 virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
195 /// Ensure that we have a valid section set in the streamer. Otherwise, report
196 /// an error and switch to .text.
197 virtual void checkForValidSection() = 0;
200 /// \brief Create an MCAsmParser instance.
201 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
202 MCStreamer &, const MCAsmInfo &);
204 } // End llvm namespace