cf9230758f2f278f07340faca85e135af211f080
[oota-llvm.git] / include / llvm / MC / MCTargetAsmParser.h
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_MCTARGETASMPARSER_H
11 #define LLVM_MC_MCTARGETASMPARSER_H
12
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
15 #include "llvm/MC/MCTargetOptions.h"
16
17 #include <memory>
18
19 namespace llvm {
20 class AsmToken;
21 class MCInst;
22 class MCParsedAsmOperand;
23 class MCStreamer;
24 class SMLoc;
25 class StringRef;
26 template <typename T> class SmallVectorImpl;
27
28 typedef SmallVectorImpl<std::unique_ptr<MCParsedAsmOperand>> OperandVector;
29
30 enum AsmRewriteKind {
31   AOK_Delete = 0,     // Rewrite should be ignored.
32   AOK_Align,          // Rewrite align as .align.
33   AOK_DotOperator,    // Rewrite a dot operator expression as an immediate.
34                       // E.g., [eax].foo.bar -> [eax].8
35   AOK_Emit,           // Rewrite _emit as .byte.
36   AOK_Imm,            // Rewrite as $$N.
37   AOK_ImmPrefix,      // Add $$ before a parsed Imm.
38   AOK_Input,          // Rewrite in terms of $N.
39   AOK_Output,         // Rewrite in terms of $N.
40   AOK_SizeDirective,  // Add a sizing directive (e.g., dword ptr).
41   AOK_Label,          // Rewrite local labels.
42   AOK_Skip            // Skip emission (e.g., offset/type operators).
43 };
44
45 const char AsmRewritePrecedence [] = {
46   0, // AOK_Delete
47   2, // AOK_Align
48   2, // AOK_DotOperator
49   2, // AOK_Emit
50   4, // AOK_Imm
51   4, // AOK_ImmPrefix
52   3, // AOK_Input
53   3, // AOK_Output
54   5, // AOK_SizeDirective
55   1, // AOK_Label
56   2  // AOK_Skip
57 };
58
59 struct AsmRewrite {
60   AsmRewriteKind Kind;
61   SMLoc Loc;
62   unsigned Len;
63   unsigned Val;
64   StringRef Label;
65 public:
66   AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len = 0, unsigned val = 0)
67     : Kind(kind), Loc(loc), Len(len), Val(val) {}
68   AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len, StringRef label)
69     : Kind(kind), Loc(loc), Len(len), Val(0), Label(label) {}
70 };
71
72 struct ParseInstructionInfo {
73
74   SmallVectorImpl<AsmRewrite> *AsmRewrites;
75
76   ParseInstructionInfo() : AsmRewrites(nullptr) {}
77   ParseInstructionInfo(SmallVectorImpl<AsmRewrite> *rewrites)
78     : AsmRewrites(rewrites) {}
79
80   ~ParseInstructionInfo() {}
81 };
82
83 /// MCTargetAsmParser - Generic interface to target specific assembly parsers.
84 class MCTargetAsmParser : public MCAsmParserExtension {
85 public:
86   enum MatchResultTy {
87     Match_InvalidOperand,
88     Match_MissingFeature,
89     Match_MnemonicFail,
90     Match_Success,
91     FIRST_TARGET_MATCH_RESULT_TY
92   };
93
94 private:
95   MCTargetAsmParser(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
96   void operator=(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
97 protected: // Can only create subclasses.
98   MCTargetAsmParser();
99
100   /// AvailableFeatures - The current set of available features.
101   uint64_t AvailableFeatures;
102
103   /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
104   bool ParsingInlineAsm;
105
106   /// SemaCallback - The Sema callback implementation.  Must be set when parsing
107   /// ms-style inline assembly.
108   MCAsmParserSemaCallback *SemaCallback;
109
110   /// Set of options which affects instrumentation of inline assembly.
111   MCTargetOptions MCOptions;
112
113 public:
114   virtual ~MCTargetAsmParser();
115
116   uint64_t getAvailableFeatures() const { return AvailableFeatures; }
117   void setAvailableFeatures(uint64_t Value) { AvailableFeatures = Value; }
118
119   bool isParsingInlineAsm () { return ParsingInlineAsm; }
120   void setParsingInlineAsm (bool Value) { ParsingInlineAsm = Value; }
121
122   MCTargetOptions getTargetOptions() const { return MCOptions; }
123
124   void setSemaCallback(MCAsmParserSemaCallback *Callback) {
125     SemaCallback = Callback;
126   }
127
128   virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
129                              SMLoc &EndLoc) = 0;
130
131   /// Sets frame register corresponding to the current MachineFunction.
132   virtual void SetFrameRegister(unsigned RegNo) {}
133
134   /// ParseInstruction - Parse one assembly instruction.
135   ///
136   /// The parser is positioned following the instruction name. The target
137   /// specific instruction parser should parse the entire instruction and
138   /// construct the appropriate MCInst, or emit an error. On success, the entire
139   /// line should be parsed up to and including the end-of-statement token. On
140   /// failure, the parser is not required to read to the end of the line.
141   //
142   /// \param Name - The instruction name.
143   /// \param NameLoc - The source location of the name.
144   /// \param Operands [out] - The list of parsed operands, this returns
145   ///        ownership of them to the caller.
146   /// \return True on failure.
147   virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
148                                 SMLoc NameLoc, OperandVector &Operands) = 0;
149
150   /// ParseDirective - Parse a target specific assembler directive
151   ///
152   /// The parser is positioned following the directive name.  The target
153   /// specific directive parser should parse the entire directive doing or
154   /// recording any target specific work, or return true and do nothing if the
155   /// directive is not target specific. If the directive is specific for
156   /// the target, the entire line is parsed up to and including the
157   /// end-of-statement token and false is returned.
158   ///
159   /// \param DirectiveID - the identifier token of the directive.
160   virtual bool ParseDirective(AsmToken DirectiveID) = 0;
161
162   /// mnemonicIsValid - This returns true if this is a valid mnemonic and false
163   /// otherwise.
164   virtual bool mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) = 0;
165
166   /// MatchAndEmitInstruction - Recognize a series of operands of a parsed
167   /// instruction as an actual MCInst and emit it to the specified MCStreamer.
168   /// This returns false on success and returns true on failure to match.
169   ///
170   /// On failure, the target parser is responsible for emitting a diagnostic
171   /// explaining the match failure.
172   virtual bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
173                                        OperandVector &Operands, MCStreamer &Out,
174                                        uint64_t &ErrorInfo,
175                                        bool MatchingInlineAsm) = 0;
176
177   /// Allows targets to let registers opt out of clobber lists.
178   virtual bool OmitRegisterFromClobberLists(unsigned RegNo) { return false; }
179
180   /// Allow a target to add special case operand matching for things that
181   /// tblgen doesn't/can't handle effectively. For example, literal
182   /// immediates on ARM. TableGen expects a token operand, but the parser
183   /// will recognize them as immediates.
184   virtual unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
185                                               unsigned Kind) {
186     return Match_InvalidOperand;
187   }
188
189   /// checkTargetMatchPredicate - Validate the instruction match against
190   /// any complex target predicates not expressible via match classes.
191   virtual unsigned checkTargetMatchPredicate(MCInst &Inst) {
192     return Match_Success;
193   }
194
195   virtual void convertToMapAndConstraints(unsigned Kind,
196                                           const OperandVector &Operands) = 0;
197
198   virtual const MCExpr *applyModifierToExpr(const MCExpr *E,
199                                             MCSymbolRefExpr::VariantKind,
200                                             MCContext &Ctx) {
201     return nullptr;
202   }
203
204   virtual void onLabelParsed(MCSymbol *Symbol) { };
205 };
206
207 } // End llvm namespace
208
209 #endif