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 /// MCAsmParserSemaCallback - Generic Sema callback for assembly parser.
34 class MCAsmParserSemaCallback {
39 unsigned Length, Size, Type;
48 } InlineAsmIdentifierInfo;
50 virtual ~MCAsmParserSemaCallback();
51 virtual void *LookupInlineAsmIdentifier(StringRef &LineBuf,
52 InlineAsmIdentifierInfo &Info,
53 bool IsUnevaluatedContext) = 0;
55 virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
56 unsigned &Offset) = 0;
59 typedef MCAsmParserSemaCallback::InlineAsmIdentifierInfo
60 InlineAsmIdentifierInfo;
62 /// MCAsmParser - Generic assembler parser interface, for use by target specific
66 typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc);
67 typedef std::pair<MCAsmParserExtension*, DirectiveHandler>
68 ExtensionDirectiveHandler;
71 MCAsmParser(const MCAsmParser &) LLVM_DELETED_FUNCTION;
72 void operator=(const MCAsmParser &) LLVM_DELETED_FUNCTION;
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;
91 virtual MCContext &getContext() = 0;
93 /// getStreamer - Return the output streamer for the assembler.
94 virtual MCStreamer &getStreamer() = 0;
96 MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
97 void setTargetParser(MCTargetAsmParser &P);
99 virtual unsigned getAssemblerDialect() { return 0;}
100 virtual void setAssemblerDialect(unsigned i) { }
102 bool getShowParsedOperands() const { return ShowParsedOperands; }
103 void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
105 /// Run - Run the parser on the input source buffer.
106 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
108 virtual void setParsingInlineAsm(bool V) = 0;
109 virtual bool isParsingInlineAsm() = 0;
111 /// parseMSInlineAsm - Parse ms-style inline assembly.
112 virtual bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
113 unsigned &NumOutputs, unsigned &NumInputs,
114 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
115 SmallVectorImpl<std::string> &Constraints,
116 SmallVectorImpl<std::string> &Clobbers,
117 const MCInstrInfo *MII,
118 const MCInstPrinter *IP,
119 MCAsmParserSemaCallback &SI) = 0;
121 /// Warning - Emit a warning at the location \p L, with the message \p Msg.
123 /// \return The return value is true, if warnings are fatal.
124 virtual bool Warning(SMLoc L, const Twine &Msg,
125 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
127 /// Error - Emit an error at the location \p L, with the message \p Msg.
129 /// \return The return value is always true, as an idiomatic convenience to
131 virtual bool Error(SMLoc L, const Twine &Msg,
132 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
134 /// Lex - Get the next AsmToken in the stream, possibly handling file
136 virtual const AsmToken &Lex() = 0;
138 /// getTok - Get the current AsmToken from the stream.
139 const AsmToken &getTok();
141 /// \brief Report an error at the current lexer location.
142 bool TokError(const Twine &Msg,
143 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
145 /// parseIdentifier - Parse an identifier or string (as a quoted identifier)
146 /// and set \p Res to the identifier contents.
147 virtual bool parseIdentifier(StringRef &Res) = 0;
149 /// \brief Parse up to the end of statement and return the contents from the
150 /// current token until the end of the statement; the current token on exit
151 /// will be either the EndOfStatement or EOF.
152 virtual StringRef parseStringToEndOfStatement() = 0;
154 /// parseEscapedString - Parse the current token as a string which may include
155 /// escaped characters and return the string contents.
156 virtual bool parseEscapedString(std::string &Data) = 0;
158 /// eatToEndOfStatement - Skip to the end of the current statement, for error
160 virtual void eatToEndOfStatement() = 0;
162 /// parseExpression - Parse an arbitrary expression.
164 /// @param Res - The value of the expression. The result is undefined
166 /// @result - False on success.
167 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
168 bool parseExpression(const MCExpr *&Res);
170 /// parsePrimaryExpr - Parse a primary expression.
172 /// @param Res - The value of the expression. The result is undefined
174 /// @result - False on success.
175 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) = 0;
177 /// parseParenExpression - Parse an arbitrary expression, assuming that an
178 /// initial '(' has already been consumed.
180 /// @param Res - The value of the expression. The result is undefined
182 /// @result - False on success.
183 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
185 /// parseAbsoluteExpression - Parse an expression which must evaluate to an
188 /// @param Res - The value of the absolute expression. The result is undefined
190 /// @result - False on success.
191 virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
193 /// checkForValidSection - Ensure that we have a valid section set in the
194 /// streamer. Otherwise, report an error and switch to .text.
195 virtual void checkForValidSection() = 0;
198 /// \brief Create an MCAsmParser instance.
199 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
200 MCStreamer &, const MCAsmInfo &);
202 } // End llvm namespace