MCAsmParser.h revision 9bac6b29b832419f8b76bb2c27af74bb57a8d99a
1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//===-- llvm/MC/MCAsmParser.h - Abstract Asm Parser Interface ---*- C++ -*-===//
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// License. See LICENSE.TXT for details.
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===//
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#ifndef LLVM_MC_MCPARSER_MCASMPARSER_H
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define LLVM_MC_MCPARSER_MCASMPARSER_H
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "llvm/ADT/ArrayRef.h"
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "llvm/MC/MCParser/AsmLexer.h"
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "llvm/Support/DataTypes.h"
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <vector>
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace llvm {
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class MCAsmInfo;
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class MCAsmLexer;
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class MCAsmParserExtension;
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class MCContext;
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class MCExpr;
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class MCInstPrinter;
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class MCInstrInfo;
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class MCParsedAsmOperand;
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class MCStreamer;
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class MCTargetAsmParser;
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class SMLoc;
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class SMRange;
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class SourceMgr;
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class StringRef;
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class Twine;
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// MCAsmParserSemaCallback - Generic Sema callback for assembly parser.
36class MCAsmParserSemaCallback {
37public:
38  virtual ~MCAsmParserSemaCallback();
39  virtual void *LookupInlineAsmIdentifier(StringRef Name, void *Loc,
40                                          unsigned &Size, bool &IsVarDecl) = 0;
41  virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
42                                    unsigned &Offset) = 0;
43};
44
45
46typedef std::vector<AsmToken> MCAsmMacroArgument;
47
48
49/// MCAsmParser - Generic assembler parser interface, for use by target specific
50/// assembly parsers.
51class MCAsmParser {
52public:
53  typedef bool (*DirectiveHandler)(MCAsmParserExtension*, StringRef, SMLoc);
54
55private:
56  MCAsmParser(const MCAsmParser &) LLVM_DELETED_FUNCTION;
57  void operator=(const MCAsmParser &) LLVM_DELETED_FUNCTION;
58
59  MCTargetAsmParser *TargetParser;
60
61  unsigned ShowParsedOperands : 1;
62
63protected: // Can only create subclasses.
64  MCAsmParser();
65
66public:
67  virtual ~MCAsmParser();
68
69  virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
70                                   StringRef Directive,
71                                   DirectiveHandler Handler) = 0;
72
73  virtual SourceMgr &getSourceManager() = 0;
74
75  virtual MCAsmLexer &getLexer() = 0;
76
77  virtual MCContext &getContext() = 0;
78
79  /// getStreamer - Return the output streamer for the assembler.
80  virtual MCStreamer &getStreamer() = 0;
81
82  MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
83  void setTargetParser(MCTargetAsmParser &P);
84
85  virtual unsigned getAssemblerDialect() { return 0;}
86  virtual void setAssemblerDialect(unsigned i) { }
87
88  bool getShowParsedOperands() const { return ShowParsedOperands; }
89  void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
90
91  /// Run - Run the parser on the input source buffer.
92  virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
93
94  virtual void setParsingInlineAsm(bool V) = 0;
95  virtual bool isParsingInlineAsm() = 0;
96
97  /// ParseMSInlineAsm - Parse ms-style inline assembly.
98  virtual bool ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
99                                unsigned &NumOutputs, unsigned &NumInputs,
100                                SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
101                                SmallVectorImpl<std::string> &Constraints,
102                                SmallVectorImpl<std::string> &Clobbers,
103                                const MCInstrInfo *MII,
104                                const MCInstPrinter *IP,
105                                MCAsmParserSemaCallback &SI) = 0;
106
107  /// Warning - Emit a warning at the location \p L, with the message \p Msg.
108  ///
109  /// \return The return value is true, if warnings are fatal.
110  virtual bool Warning(SMLoc L, const Twine &Msg,
111                       ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
112
113  /// Error - Emit an error at the location \p L, with the message \p Msg.
114  ///
115  /// \return The return value is always true, as an idiomatic convenience to
116  /// clients.
117  virtual bool Error(SMLoc L, const Twine &Msg,
118                     ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) = 0;
119
120  /// Lex - Get the next AsmToken in the stream, possibly handling file
121  /// inclusion first.
122  virtual const AsmToken &Lex() = 0;
123
124  /// getTok - Get the current AsmToken from the stream.
125  const AsmToken &getTok();
126
127  /// \brief Report an error at the current lexer location.
128  bool TokError(const Twine &Msg,
129                ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
130
131  /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
132  /// and set \p Res to the identifier contents.
133  virtual bool ParseIdentifier(StringRef &Res) = 0;
134
135  /// \brief Parse up to the end of statement and return the contents from the
136  /// current token until the end of the statement; the current token on exit
137  /// will be either the EndOfStatement or EOF.
138  virtual StringRef ParseStringToEndOfStatement() = 0;
139
140  /// EatToEndOfStatement - Skip to the end of the current statement, for error
141  /// recovery.
142  virtual void EatToEndOfStatement() = 0;
143
144  /// Control a flag in the parser that enables or disables macros.
145  virtual bool MacrosEnabled() = 0;
146  virtual void SetMacrosEnabled(bool flag) = 0;
147
148  /// ParseMacroArgument - Extract AsmTokens for a macro argument. If the
149  /// argument delimiter is initially unknown, set it to AsmToken::Eof. It will
150  /// be set to the correct delimiter by the method.
151  virtual bool ParseMacroArgument(MCAsmMacroArgument &MA,
152                                  AsmToken::TokenKind &ArgumentDelimiter) = 0;
153
154  /// ParseExpression - Parse an arbitrary expression.
155  ///
156  /// @param Res - The value of the expression. The result is undefined
157  /// on error.
158  /// @result - False on success.
159  virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
160  bool ParseExpression(const MCExpr *&Res);
161
162  /// ParseParenExpression - Parse an arbitrary expression, assuming that an
163  /// initial '(' has already been consumed.
164  ///
165  /// @param Res - The value of the expression. The result is undefined
166  /// on error.
167  /// @result - False on success.
168  virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
169
170  /// ParseAbsoluteExpression - Parse an expression which must evaluate to an
171  /// absolute value.
172  ///
173  /// @param Res - The value of the absolute expression. The result is undefined
174  /// on error.
175  /// @result - False on success.
176  virtual bool ParseAbsoluteExpression(int64_t &Res) = 0;
177};
178
179/// \brief Create an MCAsmParser instance.
180MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &,
181                               MCStreamer &, const MCAsmInfo &);
182
183} // End llvm namespace
184
185#endif
186