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"
22 class MCAsmParserExtension;
27 class MCParsedAsmOperand;
29 class MCTargetAsmParser;
36 /// MCAsmParserSemaCallback - Generic Sema callback for assembly parser.
37 class MCAsmParserSemaCallback {
39 virtual ~MCAsmParserSemaCallback();
40 virtual void *LookupInlineAsmIdentifier(StringRef Name, void *Loc,
41 unsigned &Size, bool &IsVarDecl) = 0;
42 virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
43 unsigned &Offset) = 0;
47 /// \brief Helper types for tracking macro definitions.
48 typedef std::vector<AsmToken> MCAsmMacroArgument;
49 typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
50 typedef std::pair<StringRef, MCAsmMacroArgument> MCAsmMacroParameter;
51 typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
56 MCAsmMacroParameters Parameters;
59 MCAsmMacro(StringRef N, StringRef B, const MCAsmMacroParameters &P) :
60 Name(N), Body(B), Parameters(P) {}
62 MCAsmMacro(const MCAsmMacro& Other)
63 : Name(Other.Name), Body(Other.Body), Parameters(Other.Parameters) {}
66 /// MCAsmParser - Generic assembler parser interface, for use by target specific
70 typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc);
71 typedef std::pair<MCAsmParserExtension*, DirectiveHandler>
72 ExtensionDirectiveHandler;
75 MCAsmParser(const MCAsmParser &) LLVM_DELETED_FUNCTION;
76 void operator=(const MCAsmParser &) LLVM_DELETED_FUNCTION;
78 MCTargetAsmParser *TargetParser;
80 unsigned ShowParsedOperands : 1;
82 protected: // Can only create subclasses.
86 virtual ~MCAsmParser();
88 virtual void AddDirectiveHandler(StringRef Directive,
89 ExtensionDirectiveHandler Handler) = 0;
91 virtual SourceMgr &getSourceManager() = 0;
93 virtual MCAsmLexer &getLexer() = 0;
95 virtual MCContext &getContext() = 0;
97 /// getStreamer - Return the output streamer for the assembler.
98 virtual MCStreamer &getStreamer() = 0;
100 MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
101 void setTargetParser(MCTargetAsmParser &P);
103 virtual unsigned getAssemblerDialect() { return 0;}
104 virtual void setAssemblerDialect(unsigned i) { }
106 bool getShowParsedOperands() const { return ShowParsedOperands; }
107 void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
109 /// Run - Run the parser on the input source buffer.
110 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
112 virtual void setParsingInlineAsm(bool V) = 0;
113 virtual bool isParsingInlineAsm() = 0;
115 /// ParseMSInlineAsm - Parse ms-style inline assembly.
116 virtual bool ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
117 unsigned &NumOutputs, unsigned &NumInputs,
118 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
119 SmallVectorImpl<std::string> &Constraints,
120 SmallVectorImpl<std::string> &Clobbers,
121 const MCInstrInfo *MII,
122 const MCInstPrinter *IP,
123 MCAsmParserSemaCallback &SI) = 0;
125 /// Warning - Emit a warning at the location \p L, with the message \p Msg.
127 /// \return The return value is true, if warnings are fatal.
128 virtual bool Warning(SMLoc L, const Twine &Msg,
129 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
131 /// Error - Emit an error at the location \p L, with the message \p Msg.
133 /// \return The return value is always true, as an idiomatic convenience to
135 virtual bool Error(SMLoc L, const Twine &Msg,
136 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
138 /// Lex - Get the next AsmToken in the stream, possibly handling file
140 virtual const AsmToken &Lex() = 0;
142 /// getTok - Get the current AsmToken from the stream.
143 const AsmToken &getTok();
145 /// \brief Report an error at the current lexer location.
146 bool TokError(const Twine &Msg,
147 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
149 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
150 /// and set \p Res to 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 /// EatToEndOfStatement - Skip to the end of the current statement, for error
160 virtual void EatToEndOfStatement() = 0;
162 /// \brief Are macros enabled in the parser?
163 virtual bool MacrosEnabled() = 0;
165 /// \brief Control a flag in the parser that enables or disables macros.
166 virtual void SetMacrosEnabled(bool flag) = 0;
168 /// \brief Lookup a previously defined macro.
169 /// \param Name Macro name.
170 /// \returns Pointer to macro. NULL if no such macro was defined.
171 virtual const MCAsmMacro* LookupMacro(StringRef Name) = 0;
173 /// \brief Define a new macro with the given name and information.
174 virtual void DefineMacro(StringRef Name, const MCAsmMacro& Macro) = 0;
176 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
177 virtual void UndefineMacro(StringRef Name) = 0;
179 /// \brief Are we inside a macro instantiation?
180 virtual bool InsideMacroInstantiation() = 0;
182 /// \brief Handle entry to macro instantiation.
184 /// \param M The macro.
185 /// \param NameLoc Instantiation location.
186 virtual bool HandleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) = 0;
188 /// \brief Handle exit from macro instantiation.
189 virtual void HandleMacroExit() = 0;
191 /// ParseMacroArgument - Extract AsmTokens for a macro argument. If the
192 /// argument delimiter is initially unknown, set it to AsmToken::Eof. It will
193 /// be set to the correct delimiter by the method.
194 virtual bool ParseMacroArgument(MCAsmMacroArgument &MA,
195 AsmToken::TokenKind &ArgumentDelimiter) = 0;
197 /// ParseExpression - Parse an arbitrary expression.
199 /// @param Res - The value of the expression. The result is undefined
201 /// @result - False on success.
202 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
203 bool ParseExpression(const MCExpr *&Res);
205 /// ParseParenExpression - Parse an arbitrary expression, assuming that an
206 /// initial '(' has already been consumed.
208 /// @param Res - The value of the expression. The result is undefined
210 /// @result - False on success.
211 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
213 /// ParseAbsoluteExpression - Parse an expression which must evaluate to an
216 /// @param Res - The value of the absolute expression. The result is undefined
218 /// @result - False on success.
219 virtual bool ParseAbsoluteExpression(int64_t &Res) = 0;
221 /// CheckForValidSection - Ensure that we have a valid section set in the
222 /// streamer. Otherwise, report and error and switch to .text.
223 virtual void CheckForValidSection() = 0;
226 /// \brief Create an MCAsmParser instance.
227 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
228 MCStreamer &, const MCAsmInfo &);
230 } // End llvm namespace