MCTargetAsmParser.h revision 9ba9d4d76bfa8de2b05cbce02a5a3ff7d46cb331
1//===-- llvm/MC/MCTargetAsmParser.h - Target Assembly Parser ----*- 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_TARGETPARSER_H
11#define LLVM_MC_TARGETPARSER_H
12
13#include "llvm/MC/MCParser/MCAsmParserExtension.h"
14
15namespace llvm {
16class MCStreamer;
17class StringRef;
18class SMLoc;
19class AsmToken;
20class MCParsedAsmOperand;
21class MCInst;
22template <typename T> class SmallVectorImpl;
23
24/// MCTargetAsmParser - Generic interface to target specific assembly parsers.
25class MCTargetAsmParser : public MCAsmParserExtension {
26public:
27  enum MatchResultTy {
28    Match_InvalidOperand,
29    Match_MissingFeature,
30    Match_MnemonicFail,
31    Match_Success,
32    FIRST_TARGET_MATCH_RESULT_TY
33  };
34
35private:
36  MCTargetAsmParser(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
37  void operator=(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
38protected: // Can only create subclasses.
39  MCTargetAsmParser();
40
41  /// AvailableFeatures - The current set of available features.
42  unsigned AvailableFeatures;
43
44public:
45  virtual ~MCTargetAsmParser();
46
47  unsigned getAvailableFeatures() const { return AvailableFeatures; }
48  void setAvailableFeatures(unsigned Value) { AvailableFeatures = Value; }
49
50  virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
51                             SMLoc &EndLoc) = 0;
52
53  typedef std::pair< unsigned, std::string > MapAndConstraint;
54  typedef SmallVector<MapAndConstraint, 4> MatchInstMapAndConstraints;
55  typedef SmallVectorImpl<MapAndConstraint> MatchInstMapAndConstraintsImpl;
56
57  /// ParseInstruction - Parse one assembly instruction.
58  ///
59  /// The parser is positioned following the instruction name. The target
60  /// specific instruction parser should parse the entire instruction and
61  /// construct the appropriate MCInst, or emit an error. On success, the entire
62  /// line should be parsed up to and including the end-of-statement token. On
63  /// failure, the parser is not required to read to the end of the line.
64  //
65  /// \param Name - The instruction name.
66  /// \param NameLoc - The source location of the name.
67  /// \param Operands [out] - The list of parsed operands, this returns
68  ///        ownership of them to the caller.
69  /// \return True on failure.
70  virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
71                            SmallVectorImpl<MCParsedAsmOperand*> &Operands) = 0;
72
73  /// ParseDirective - Parse a target specific assembler directive
74  ///
75  /// The parser is positioned following the directive name.  The target
76  /// specific directive parser should parse the entire directive doing or
77  /// recording any target specific work, or return true and do nothing if the
78  /// directive is not target specific. If the directive is specific for
79  /// the target, the entire line is parsed up to and including the
80  /// end-of-statement token and false is returned.
81  ///
82  /// \param DirectiveID - the identifier token of the directive.
83  virtual bool ParseDirective(AsmToken DirectiveID) = 0;
84
85  /// mnemonicIsValid - This returns true if this is a valid mnemonic and false
86  /// otherwise.
87  virtual bool mnemonicIsValid(StringRef Mnemonic) = 0;
88
89  /// MatchInstruction - Recognize a series of operands of a parsed instruction
90  /// as an actual MCInst.  This returns false on success and returns true on
91  /// failure to match.
92  ///
93  /// On failure, the target parser is responsible for emitting a diagnostic
94  /// explaining the match failure.
95  virtual bool
96  MatchInstruction(SMLoc IDLoc,
97                   SmallVectorImpl<MCParsedAsmOperand*> &Operands,
98                   MCStreamer &Out, unsigned &Kind, unsigned &Opcode,
99                   MatchInstMapAndConstraintsImpl &MapAndConstraints,
100                   unsigned &OrigErrorInfo, bool matchingInlineAsm = false) {
101    OrigErrorInfo = ~0x0;
102    return true;
103  }
104
105  /// MatchAndEmitInstruction - Recognize a series of operands of a parsed
106  /// instruction as an actual MCInst and emit it to the specified MCStreamer.
107  /// This returns false on success and returns true on failure to match.
108  ///
109  /// On failure, the target parser is responsible for emitting a diagnostic
110  /// explaining the match failure.
111  virtual bool
112  MatchAndEmitInstruction(SMLoc IDLoc,
113                          SmallVectorImpl<MCParsedAsmOperand*> &Operands,
114                          MCStreamer &Out) = 0;
115
116  /// checkTargetMatchPredicate - Validate the instruction match against
117  /// any complex target predicates not expressible via match classes.
118  virtual unsigned checkTargetMatchPredicate(MCInst &Inst) {
119    return Match_Success;
120  }
121
122  virtual void convertToMapAndConstraints(unsigned Kind,
123                           const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
124                         MatchInstMapAndConstraintsImpl &MapAndConstraints) = 0;
125};
126
127} // End llvm namespace
128
129#endif
130