There are a few places where subtarget features are still
[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 #include "llvm/MC/SubtargetFeature.h"
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
81 /// MCTargetAsmParser - Generic interface to target specific assembly parsers.
82 class MCTargetAsmParser : public MCAsmParserExtension {
83 public:
84   enum MatchResultTy {
85     Match_InvalidOperand,
86     Match_MissingFeature,
87     Match_MnemonicFail,
88     Match_Success,
89     FIRST_TARGET_MATCH_RESULT_TY
90   };
91
92 private:
93   MCTargetAsmParser(const MCTargetAsmParser &) = delete;
94   void operator=(const MCTargetAsmParser &) = delete;
95 protected: // Can only create subclasses.
96   MCTargetAsmParser();
97
98   /// AvailableFeatures - The current set of available features.
99   FeatureBitset AvailableFeatures;
100
101   /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
102   bool ParsingInlineAsm;
103
104   /// SemaCallback - The Sema callback implementation.  Must be set when parsing
105   /// ms-style inline assembly.
106   MCAsmParserSemaCallback *SemaCallback;
107
108   /// Set of options which affects instrumentation of inline assembly.
109   MCTargetOptions MCOptions;
110
111 public:
112   ~MCTargetAsmParser() override;
113
114   FeatureBitset getAvailableFeatures() const { return AvailableFeatures; }
115   void setAvailableFeatures(FeatureBitset Value) { AvailableFeatures = Value; }
116
117   bool isParsingInlineAsm () { return ParsingInlineAsm; }
118   void setParsingInlineAsm (bool Value) { ParsingInlineAsm = Value; }
119
120   MCTargetOptions getTargetOptions() const { return MCOptions; }
121
122   void setSemaCallback(MCAsmParserSemaCallback *Callback) {
123     SemaCallback = Callback;
124   }
125
126   virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
127                              SMLoc &EndLoc) = 0;
128
129   /// Sets frame register corresponding to the current MachineFunction.
130   virtual void SetFrameRegister(unsigned RegNo) {}
131
132   /// ParseInstruction - Parse one assembly instruction.
133   ///
134   /// The parser is positioned following the instruction name. The target
135   /// specific instruction parser should parse the entire instruction and
136   /// construct the appropriate MCInst, or emit an error. On success, the entire
137   /// line should be parsed up to and including the end-of-statement token. On
138   /// failure, the parser is not required to read to the end of the line.
139   //
140   /// \param Name - The instruction name.
141   /// \param NameLoc - The source location of the name.
142   /// \param Operands [out] - The list of parsed operands, this returns
143   ///        ownership of them to the caller.
144   /// \return True on failure.
145   virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
146                                 SMLoc NameLoc, OperandVector &Operands) = 0;
147
148   /// ParseDirective - Parse a target specific assembler directive
149   ///
150   /// The parser is positioned following the directive name.  The target
151   /// specific directive parser should parse the entire directive doing or
152   /// recording any target specific work, or return true and do nothing if the
153   /// directive is not target specific. If the directive is specific for
154   /// the target, the entire line is parsed up to and including the
155   /// end-of-statement token and false is returned.
156   ///
157   /// \param DirectiveID - the identifier token of the directive.
158   virtual bool ParseDirective(AsmToken DirectiveID) = 0;
159
160   /// mnemonicIsValid - This returns true if this is a valid mnemonic and false
161   /// otherwise.
162   virtual bool mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) = 0;
163
164   /// MatchAndEmitInstruction - Recognize a series of operands of a parsed
165   /// instruction as an actual MCInst and emit it to the specified MCStreamer.
166   /// This returns false on success and returns true on failure to match.
167   ///
168   /// On failure, the target parser is responsible for emitting a diagnostic
169   /// explaining the match failure.
170   virtual bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
171                                        OperandVector &Operands, MCStreamer &Out,
172                                        uint64_t &ErrorInfo,
173                                        FeatureBitset &ErrorMissingFeature,
174                                        bool MatchingInlineAsm) = 0;
175
176   /// Allows targets to let registers opt out of clobber lists.
177   virtual bool OmitRegisterFromClobberLists(unsigned RegNo) { return false; }
178
179   /// Allow a target to add special case operand matching for things that
180   /// tblgen doesn't/can't handle effectively. For example, literal
181   /// immediates on ARM. TableGen expects a token operand, but the parser
182   /// will recognize them as immediates.
183   virtual unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
184                                               unsigned Kind) {
185     return Match_InvalidOperand;
186   }
187
188   /// checkTargetMatchPredicate - Validate the instruction match against
189   /// any complex target predicates not expressible via match classes.
190   virtual unsigned checkTargetMatchPredicate(MCInst &Inst) {
191     return Match_Success;
192   }
193
194   virtual void convertToMapAndConstraints(unsigned Kind,
195                                           const OperandVector &Operands) = 0;
196
197   virtual const MCExpr *applyModifierToExpr(const MCExpr *E,
198                                             MCSymbolRefExpr::VariantKind,
199                                             MCContext &Ctx) {
200     return nullptr;
201   }
202
203   virtual void onLabelParsed(MCSymbol *Symbol) { };
204 };
205
206 } // End llvm namespace
207
208 #endif