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 /// MCAsmParserSemaCallback - 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;
56 virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
57 unsigned &Offset) = 0;
60 /// MCAsmParser - Generic assembler parser interface, for use by target specific
64 typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc);
65 typedef std::pair<MCAsmParserExtension*, DirectiveHandler>
66 ExtensionDirectiveHandler;
69 MCAsmParser(const MCAsmParser &) LLVM_DELETED_FUNCTION;
70 void operator=(const MCAsmParser &) LLVM_DELETED_FUNCTION;
72 MCTargetAsmParser *TargetParser;
74 unsigned ShowParsedOperands : 1;
76 protected: // Can only create subclasses.
80 virtual ~MCAsmParser();
82 virtual void addDirectiveHandler(StringRef Directive,
83 ExtensionDirectiveHandler Handler) = 0;
85 virtual SourceMgr &getSourceManager() = 0;
87 virtual MCAsmLexer &getLexer() = 0;
89 virtual MCContext &getContext() = 0;
91 /// getStreamer - Return the output streamer for the assembler.
92 virtual MCStreamer &getStreamer() = 0;
94 MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
95 void setTargetParser(MCTargetAsmParser &P);
97 virtual unsigned getAssemblerDialect() { return 0;}
98 virtual void setAssemblerDialect(unsigned i) { }
100 bool getShowParsedOperands() const { return ShowParsedOperands; }
101 void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
103 /// Run - Run the parser on the input source buffer.
104 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
106 virtual void setParsingInlineAsm(bool V) = 0;
107 virtual bool isParsingInlineAsm() = 0;
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;
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;
123 /// Warning - Emit a warning at the location \p L, with the message \p Msg.
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;
129 /// Error - Emit an error at the location \p L, with the message \p Msg.
131 /// \return The return value is always true, as an idiomatic convenience to
133 virtual bool Error(SMLoc L, const Twine &Msg,
134 ArrayRef<SMRange> Ranges = None) = 0;
136 /// Lex - Get the next AsmToken in the stream, possibly handling file
138 virtual const AsmToken &Lex() = 0;
140 /// getTok - Get the current AsmToken from the stream.
141 const AsmToken &getTok();
143 /// \brief Report an error at the current lexer location.
144 bool TokError(const Twine &Msg, ArrayRef<SMRange> Ranges = None);
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;
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;
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;
159 /// eatToEndOfStatement - Skip to the end of the current statement, for error
161 virtual void eatToEndOfStatement() = 0;
163 /// parseExpression - Parse an arbitrary expression.
165 /// @param Res - The value of the expression. The result is undefined
167 /// @result - False on success.
168 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
169 bool parseExpression(const MCExpr *&Res);
171 /// parsePrimaryExpr - Parse a primary expression.
173 /// @param Res - The value of the expression. The result is undefined
175 /// @result - False on success.
176 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) = 0;
178 /// parseParenExpression - Parse an arbitrary expression, assuming that an
179 /// initial '(' has already been consumed.
181 /// @param Res - The value of the expression. The result is undefined
183 /// @result - False on success.
184 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
186 /// parseAbsoluteExpression - Parse an expression which must evaluate to an
189 /// @param Res - The value of the absolute expression. The result is undefined
191 /// @result - False on success.
192 virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
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;
199 /// \brief Create an MCAsmParser instance.
200 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
201 MCStreamer &, const MCAsmInfo &);
203 } // End llvm namespace