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);
73 MCAsmParser(const MCAsmParser &) LLVM_DELETED_FUNCTION;
74 void operator=(const MCAsmParser &) LLVM_DELETED_FUNCTION;
76 MCTargetAsmParser *TargetParser;
78 unsigned ShowParsedOperands : 1;
80 protected: // Can only create subclasses.
84 virtual ~MCAsmParser();
86 virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
88 DirectiveHandler Handler) = 0;
90 virtual SourceMgr &getSourceManager() = 0;
92 virtual MCAsmLexer &getLexer() = 0;
94 virtual MCContext &getContext() = 0;
96 /// getStreamer - 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 - 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 /// ParseMSInlineAsm - Parse ms-style inline assembly.
115 virtual bool ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
116 unsigned &NumOutputs, unsigned &NumInputs,
117 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
118 SmallVectorImpl<std::string> &Constraints,
119 SmallVectorImpl<std::string> &Clobbers,
120 const MCInstrInfo *MII,
121 const MCInstPrinter *IP,
122 MCAsmParserSemaCallback &SI) = 0;
124 /// Warning - Emit a warning at the location \p L, with the message \p Msg.
126 /// \return The return value is true, if warnings are fatal.
127 virtual bool Warning(SMLoc L, const Twine &Msg,
128 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
130 /// Error - Emit an error at the location \p L, with the message \p Msg.
132 /// \return The return value is always true, as an idiomatic convenience to
134 virtual bool Error(SMLoc L, const Twine &Msg,
135 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
137 /// Lex - Get the next AsmToken in the stream, possibly handling file
139 virtual const AsmToken &Lex() = 0;
141 /// getTok - Get the current AsmToken from the stream.
142 const AsmToken &getTok();
144 /// \brief Report an error at the current lexer location.
145 bool TokError(const Twine &Msg,
146 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
148 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
149 /// and set \p Res to the identifier contents.
150 virtual bool ParseIdentifier(StringRef &Res) = 0;
152 /// \brief Parse up to the end of statement and return the contents from the
153 /// current token until the end of the statement; the current token on exit
154 /// will be either the EndOfStatement or EOF.
155 virtual StringRef ParseStringToEndOfStatement() = 0;
157 /// EatToEndOfStatement - Skip to the end of the current statement, for error
159 virtual void EatToEndOfStatement() = 0;
161 /// \brief Are macros enabled in the parser?
162 virtual bool MacrosEnabled() = 0;
164 /// \brief Control a flag in the parser that enables or disables macros.
165 virtual void SetMacrosEnabled(bool flag) = 0;
167 /// \brief Lookup a previously defined macro.
168 /// \param Name Macro name.
169 /// \returns Pointer to macro. NULL if no such macro was defined.
170 virtual const MCAsmMacro* LookupMacro(StringRef Name) = 0;
172 /// \brief Define a new macro with the given name and information.
173 virtual void DefineMacro(StringRef Name, const MCAsmMacro& Macro) = 0;
175 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
176 virtual void UndefineMacro(StringRef Name) = 0;
178 /// \brief Are we inside a macro instantiation?
179 virtual bool InsideMacroInstantiation() = 0;
181 /// \brief Handle entry to macro instantiation.
183 /// \param M The macro.
184 /// \param NameLoc Instantiation location.
185 virtual bool HandleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) = 0;
187 /// \brief Handle exit from macro instantiation.
188 virtual void HandleMacroExit() = 0;
190 /// ParseMacroArgument - Extract AsmTokens for a macro argument. If the
191 /// argument delimiter is initially unknown, set it to AsmToken::Eof. It will
192 /// be set to the correct delimiter by the method.
193 virtual bool ParseMacroArgument(MCAsmMacroArgument &MA,
194 AsmToken::TokenKind &ArgumentDelimiter) = 0;
196 /// ParseExpression - Parse an arbitrary expression.
198 /// @param Res - The value of the expression. The result is undefined
200 /// @result - False on success.
201 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
202 bool ParseExpression(const MCExpr *&Res);
204 /// ParseParenExpression - Parse an arbitrary expression, assuming that an
205 /// initial '(' has already been consumed.
207 /// @param Res - The value of the expression. The result is undefined
209 /// @result - False on success.
210 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
212 /// ParseAbsoluteExpression - Parse an expression which must evaluate to an
215 /// @param Res - The value of the absolute expression. The result is undefined
217 /// @result - False on success.
218 virtual bool ParseAbsoluteExpression(int64_t &Res) = 0;
220 /// CheckForValidSection - Ensure that we have a valid section set in the
221 /// streamer. Otherwise, report and error and switch to .text.
222 virtual void CheckForValidSection() = 0;
225 /// \brief Create an MCAsmParser instance.
226 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
227 MCStreamer &, const MCAsmInfo &);
229 } // End llvm namespace