LLVM  3.7.0
MCAsmParser.h
Go to the documentation of this file.
1 //===-- llvm/MC/MCAsmParser.h - Abstract Asm Parser Interface ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_MC_MCPARSER_MCASMPARSER_H
11 #define LLVM_MC_MCPARSER_MCASMPARSER_H
12 
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/DataTypes.h"
17 
18 namespace llvm {
19 class MCAsmInfo;
20 class MCAsmLexer;
21 class MCAsmParserExtension;
22 class MCContext;
23 class MCExpr;
24 class MCInstPrinter;
25 class MCInstrInfo;
26 class MCStreamer;
27 class MCTargetAsmParser;
28 class SMLoc;
29 class SMRange;
30 class SourceMgr;
31 class Twine;
32 
34 public:
35  void *OpDecl;
36  bool IsVarDecl;
37  unsigned Length, Size, Type;
38 
39  void clear() {
40  OpDecl = nullptr;
41  IsVarDecl = false;
42  Length = 1;
43  Size = 0;
44  Type = 0;
45  }
46 };
47 
48 /// \brief Generic Sema callback for assembly parser.
50 public:
51  virtual ~MCAsmParserSemaCallback();
52  virtual void *LookupInlineAsmIdentifier(StringRef &LineBuf,
54  bool IsUnevaluatedContext) = 0;
55  virtual StringRef LookupInlineAsmLabel(StringRef Identifier, SourceMgr &SM,
56  SMLoc Location, bool Create) = 0;
57 
59  unsigned &Offset) = 0;
60 };
61 
62 /// \brief Generic assembler parser interface, for use by target specific
63 /// assembly parsers.
64 class MCAsmParser {
65 public:
67  typedef std::pair<MCAsmParserExtension*, DirectiveHandler>
69 
70 private:
71  MCAsmParser(const MCAsmParser &) = delete;
72  void operator=(const MCAsmParser &) = delete;
73 
74  MCTargetAsmParser *TargetParser;
75 
76  unsigned ShowParsedOperands : 1;
77 
78 protected: // Can only create subclasses.
79  MCAsmParser();
80 
81 public:
82  virtual ~MCAsmParser();
83 
84  virtual void addDirectiveHandler(StringRef Directive,
85  ExtensionDirectiveHandler Handler) = 0;
86 
87  virtual void addAliasForDirective(StringRef Directive, StringRef Alias) = 0;
88 
89  virtual SourceMgr &getSourceManager() = 0;
90 
91  virtual MCAsmLexer &getLexer() = 0;
92  const MCAsmLexer &getLexer() const {
93  return const_cast<MCAsmParser*>(this)->getLexer();
94  }
95 
96  virtual MCContext &getContext() = 0;
97 
98  /// \brief Return the output streamer for the assembler.
99  virtual MCStreamer &getStreamer() = 0;
100 
101  MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
103 
104  virtual unsigned getAssemblerDialect() { return 0;}
105  virtual void setAssemblerDialect(unsigned i) { }
106 
107  bool getShowParsedOperands() const { return ShowParsedOperands; }
108  void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
109 
110  /// \brief Run the parser on the input source buffer.
111  virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
112 
113  virtual void setParsingInlineAsm(bool V) = 0;
114  virtual bool isParsingInlineAsm() = 0;
115 
116  /// \brief Parse MS-style inline assembly.
117  virtual bool parseMSInlineAsm(
118  void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
119  unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
120  SmallVectorImpl<std::string> &Constraints,
121  SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
123 
124  /// \brief Emit a note at the location \p L, with the message \p Msg.
125  virtual void Note(SMLoc L, const Twine &Msg,
127 
128  /// \brief Emit a warning at the location \p L, with the message \p Msg.
129  ///
130  /// \return The return value is true, if warnings are fatal.
131  virtual bool Warning(SMLoc L, const Twine &Msg,
133 
134  /// \brief Emit an error at the location \p L, with the message \p Msg.
135  ///
136  /// \return The return value is always true, as an idiomatic convenience to
137  /// clients.
138  virtual bool Error(SMLoc L, const Twine &Msg,
140 
141  /// \brief Get the next AsmToken in the stream, possibly handling file
142  /// inclusion first.
143  virtual const AsmToken &Lex() = 0;
144 
145  /// \brief Get the current AsmToken from the stream.
146  const AsmToken &getTok() const;
147 
148  /// \brief Report an error at the current lexer location.
149  bool TokError(const Twine &Msg, ArrayRef<SMRange> Ranges = None);
150 
151  /// \brief Parse an identifier or string (as a quoted identifier) and set \p
152  /// Res to the identifier contents.
153  virtual bool parseIdentifier(StringRef &Res) = 0;
154 
155  /// \brief Parse up to the end of statement and return the contents from the
156  /// current token until the end of the statement; the current token on exit
157  /// will be either the EndOfStatement or EOF.
159 
160  /// \brief Parse the current token as a string which may include escaped
161  /// characters and return the string contents.
162  virtual bool parseEscapedString(std::string &Data) = 0;
163 
164  /// \brief Skip to the end of the current statement, for error recovery.
165  virtual void eatToEndOfStatement() = 0;
166 
167  /// \brief Parse an arbitrary expression.
168  ///
169  /// \param Res - The value of the expression. The result is undefined
170  /// on error.
171  /// \return - False on success.
172  virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
173  bool parseExpression(const MCExpr *&Res);
174 
175  /// \brief Parse a primary expression.
176  ///
177  /// \param Res - The value of the expression. The result is undefined
178  /// on error.
179  /// \return - False on success.
180  virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) = 0;
181 
182  /// \brief Parse an arbitrary expression, assuming that an initial '(' has
183  /// already been consumed.
184  ///
185  /// \param Res - The value of the expression. The result is undefined
186  /// on error.
187  /// \return - False on success.
188  virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
189 
190  /// \brief Parse an expression which must evaluate to an absolute value.
191  ///
192  /// \param Res - The value of the absolute expression. The result is undefined
193  /// on error.
194  /// \return - False on success.
195  virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
196 
197  /// \brief Ensure that we have a valid section set in the streamer. Otherwise,
198  /// report an error and switch to .text.
199  virtual void checkForValidSection() = 0;
200 
201  /// \brief Parse an arbitrary expression of a specified parenthesis depth,
202  /// assuming that the initial '(' characters have already been consumed.
203  ///
204  /// \param ParenDepth - Specifies how many trailing expressions outside the
205  /// current parentheses we have to parse.
206  /// \param Res - The value of the expression. The result is undefined
207  /// on error.
208  /// \return - False on success.
209  virtual bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
210  SMLoc &EndLoc) = 0;
211 };
212 
213 /// \brief Create an MCAsmParser instance.
214 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &,
215  const MCAsmInfo &);
216 
217 } // End llvm namespace
218 
219 #endif
virtual bool Warning(SMLoc L, const Twine &Msg, ArrayRef< SMRange > Ranges=None)=0
Emit a warning at the location L, with the message Msg.
Generic assembler parser interface, for use by target specific assembly parsers.
Definition: MCAsmParser.h:64
virtual void setAssemblerDialect(unsigned i)
Definition: MCAsmParser.h:105
MCTargetAsmParser - Generic interface to target specific assembly parsers.
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
virtual SourceMgr & getSourceManager()=0
virtual void * LookupInlineAsmIdentifier(StringRef &LineBuf, InlineAsmIdentifierInfo &Info, bool IsUnevaluatedContext)=0
virtual void Note(SMLoc L, const Twine &Msg, ArrayRef< SMRange > Ranges=None)=0
Emit a note at the location L, with the message Msg.
bool TokError(const Twine &Msg, ArrayRef< SMRange > Ranges=None)
Report an error at the current lexer location.
Definition: MCAsmParser.cpp:36
MCTargetAsmParser & getTargetParser() const
Definition: MCAsmParser.h:101
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
Generic assembler lexer interface, for use by target specific assembly lexers.
Definition: MCAsmLexer.h:119
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
Target independent representation for an assembler token.
Definition: MCAsmLexer.h:22
virtual void addDirectiveHandler(StringRef Directive, ExtensionDirectiveHandler Handler)=0
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
Context object for machine code objects.
Definition: MCContext.h:48
void setShowParsedOperands(bool Value)
Definition: MCAsmParser.h:108
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
virtual bool parseEscapedString(std::string &Data)=0
Parse the current token as a string which may include escaped characters and return the string conten...
const AsmToken & getTok() const
Get the current AsmToken from the stream.
Definition: MCAsmParser.cpp:32
virtual void eatToEndOfStatement()=0
Skip to the end of the current statement, for error recovery.
virtual void addAliasForDirective(StringRef Directive, StringRef Alias)=0
#define P(N)
virtual MCContext & getContext()=0
Streaming machine code generation interface.
Definition: MCStreamer.h:157
virtual ~MCAsmParser()
Definition: MCAsmParser.cpp:23
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
virtual bool isParsingInlineAsm()=0
virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse a primary expression.
const MCAsmLexer & getLexer() const
Definition: MCAsmParser.h:92
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:24
virtual bool Run(bool NoInitialTextSection, bool NoFinalize=false)=0
Run the parser on the input source buffer.
virtual MCAsmLexer & getLexer()=0
MCAsmParser * createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &)
Create an MCAsmParser instance.
Definition: AsmParser.cpp:4840
Generic Sema callback for assembly parser.
Definition: MCAsmParser.h:49
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:35
std::pair< MCAsmParserExtension *, DirectiveHandler > ExtensionDirectiveHandler
Definition: MCAsmParser.h:68
virtual StringRef parseStringToEndOfStatement()=0
Parse up to the end of statement and return the contents from the current token until the end of the ...
virtual bool Error(SMLoc L, const Twine &Msg, ArrayRef< SMRange > Ranges=None)=0
Emit an error at the location L, with the message Msg.
virtual bool LookupInlineAsmField(StringRef Base, StringRef Member, unsigned &Offset)=0
virtual void setParsingInlineAsm(bool V)=0
virtual MCStreamer & getStreamer()=0
Return the output streamer for the assembler.
virtual bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression of a specified parenthesis depth, assuming that the initial '(' charact...
virtual void checkForValidSection()=0
Ensure that we have a valid section set in the streamer.
SI Fix SGPR Live Ranges
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:38
virtual bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString, unsigned &NumOutputs, unsigned &NumInputs, SmallVectorImpl< std::pair< void *, bool >> &OpDecls, SmallVectorImpl< std::string > &Constraints, SmallVectorImpl< std::string > &Clobbers, const MCInstrInfo *MII, const MCInstPrinter *IP, MCAsmParserSemaCallback &SI)=0
Parse MS-style inline assembly.
virtual StringRef LookupInlineAsmLabel(StringRef Identifier, SourceMgr &SM, SMLoc Location, bool Create)=0
bool(* DirectiveHandler)(MCAsmParserExtension *, StringRef, SMLoc)
Definition: MCAsmParser.h:66
virtual bool parseAbsoluteExpression(int64_t &Res)=0
Parse an expression which must evaluate to an absolute value.
virtual unsigned getAssemblerDialect()
Definition: MCAsmParser.h:104
Generic interface for extending the MCAsmParser, which is implemented by target and object file assem...
LLVM Value Representation.
Definition: Value.h:69
void setTargetParser(MCTargetAsmParser &P)
Definition: MCAsmParser.cpp:26
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
virtual bool parseIdentifier(StringRef &Res)=0
Parse an identifier or string (as a quoted identifier) and set Res to the identifier contents...
bool getShowParsedOperands() const
Definition: MCAsmParser.h:107
Represents a location in source code.
Definition: SMLoc.h:23
virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression, assuming that an initial '(' has already been consumed.