MCTargetAsmParser.h revision fa05def52c6bb8266d856a9af2de6fa066d93d44
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
24enum AsmRewriteKind {
25  AOK_DotOperator,    // Rewrite a dot operator expression as an immediate.
26                      // E.g., [eax].foo.bar -> [eax].8
27  AOK_Emit,           // Rewrite _emit as .byte.
28  AOK_Imm,            // Rewrite as $$N.
29  AOK_ImmPrefix,      // Add $$ before a parsed Imm.
30  AOK_Input,          // Rewrite in terms of $N.
31  AOK_Output,         // Rewrite in terms of $N.
32  AOK_SizeDirective,  // Add a sizing directive (e.g., dword ptr).
33  AOK_Skip            // Skip emission (e.g., offset/type operators).
34};
35
36struct AsmRewrite {
37  AsmRewriteKind Kind;
38  SMLoc Loc;
39  unsigned Len;
40  unsigned Val;
41public:
42  AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len = 0, unsigned val = 0)
43    : Kind(kind), Loc(loc), Len(len), Val(val) {}
44};
45
46struct ParseInstructionInfo {
47
48  SmallVectorImpl<AsmRewrite> *AsmRewrites;
49
50  ParseInstructionInfo() : AsmRewrites(0) {}
51  ParseInstructionInfo(SmallVectorImpl<AsmRewrite> *rewrites)
52    : AsmRewrites(rewrites) {}
53
54  ~ParseInstructionInfo() {}
55};
56
57/// MCTargetAsmParser - Generic interface to target specific assembly parsers.
58class MCTargetAsmParser : public MCAsmParserExtension {
59public:
60  enum MatchResultTy {
61    Match_InvalidOperand,
62    Match_MissingFeature,
63    Match_MnemonicFail,
64    Match_Success,
65    FIRST_TARGET_MATCH_RESULT_TY
66  };
67
68private:
69  MCTargetAsmParser(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
70  void operator=(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
71protected: // Can only create subclasses.
72  MCTargetAsmParser();
73
74  /// AvailableFeatures - The current set of available features.
75  unsigned AvailableFeatures;
76
77  /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
78  bool ParsingInlineAsm;
79
80  /// SemaCallback - The Sema callback implementation.  Must be set when parsing
81  /// ms-style inline assembly.
82  MCAsmParserSemaCallback *SemaCallback;
83
84public:
85  virtual ~MCTargetAsmParser();
86
87  unsigned getAvailableFeatures() const { return AvailableFeatures; }
88  void setAvailableFeatures(unsigned Value) { AvailableFeatures = Value; }
89
90  bool isParsingInlineAsm () { return ParsingInlineAsm; }
91  void setParsingInlineAsm (bool Value) { ParsingInlineAsm = Value; }
92
93  void setSemaCallback(MCAsmParserSemaCallback *Callback) {
94    SemaCallback = Callback;
95  }
96
97  virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
98                             SMLoc &EndLoc) = 0;
99
100  /// ParseInstruction - Parse one assembly instruction.
101  ///
102  /// The parser is positioned following the instruction name. The target
103  /// specific instruction parser should parse the entire instruction and
104  /// construct the appropriate MCInst, or emit an error. On success, the entire
105  /// line should be parsed up to and including the end-of-statement token. On
106  /// failure, the parser is not required to read to the end of the line.
107  //
108  /// \param Name - The instruction name.
109  /// \param NameLoc - The source location of the name.
110  /// \param Operands [out] - The list of parsed operands, this returns
111  ///        ownership of them to the caller.
112  /// \return True on failure.
113  virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
114                                SMLoc NameLoc,
115                            SmallVectorImpl<MCParsedAsmOperand*> &Operands) = 0;
116
117  /// ParseDirective - Parse a target specific assembler directive
118  ///
119  /// The parser is positioned following the directive name.  The target
120  /// specific directive parser should parse the entire directive doing or
121  /// recording any target specific work, or return true and do nothing if the
122  /// directive is not target specific. If the directive is specific for
123  /// the target, the entire line is parsed up to and including the
124  /// end-of-statement token and false is returned.
125  ///
126  /// \param DirectiveID - the identifier token of the directive.
127  virtual bool ParseDirective(AsmToken DirectiveID) = 0;
128
129  /// mnemonicIsValid - This returns true if this is a valid mnemonic and false
130  /// otherwise.
131  virtual bool mnemonicIsValid(StringRef Mnemonic) = 0;
132
133  /// MatchAndEmitInstruction - Recognize a series of operands of a parsed
134  /// instruction as an actual MCInst and emit it to the specified MCStreamer.
135  /// This returns false on success and returns true on failure to match.
136  ///
137  /// On failure, the target parser is responsible for emitting a diagnostic
138  /// explaining the match failure.
139  virtual bool
140  MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
141                          SmallVectorImpl<MCParsedAsmOperand*> &Operands,
142                          MCStreamer &Out, unsigned &ErrorInfo,
143                          bool MatchingInlineAsm) = 0;
144
145  /// Allow a target to add special case operand matching for things that
146  /// tblgen doesn't/can't handle effectively. For example, literal
147  /// immediates on ARM. TableGen expects a token operand, but the parser
148  /// will recognize them as immediates.
149  virtual unsigned validateTargetOperandClass(MCParsedAsmOperand *Op,
150                                              unsigned Kind) {
151    return Match_InvalidOperand;
152  }
153
154  /// checkTargetMatchPredicate - Validate the instruction match against
155  /// any complex target predicates not expressible via match classes.
156  virtual unsigned checkTargetMatchPredicate(MCInst &Inst) {
157    return Match_Success;
158  }
159
160  virtual void convertToMapAndConstraints(unsigned Kind,
161                      const SmallVectorImpl<MCParsedAsmOperand*> &Operands) = 0;
162};
163
164} // End llvm namespace
165
166#endif
167