1 //===-- llvm/MC/MCTargetAsmParser.h - Target Assembly Parser ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_TARGETPARSER_H
11 #define LLVM_MC_TARGETPARSER_H
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
15 #include "llvm/MC/MCTargetOptions.h"
20 class MCParsedAsmOperand;
24 template <typename T> class SmallVectorImpl;
27 AOK_Delete = 0, // Rewrite should be ignored.
28 AOK_Align, // Rewrite align as .align.
29 AOK_DotOperator, // Rewrite a dot operator expression as an immediate.
30 // E.g., [eax].foo.bar -> [eax].8
31 AOK_Emit, // Rewrite _emit as .byte.
32 AOK_Imm, // Rewrite as $$N.
33 AOK_ImmPrefix, // Add $$ before a parsed Imm.
34 AOK_Input, // Rewrite in terms of $N.
35 AOK_Output, // Rewrite in terms of $N.
36 AOK_SizeDirective, // Add a sizing directive (e.g., dword ptr).
37 AOK_Skip // Skip emission (e.g., offset/type operators).
40 const char AsmRewritePrecedence [] = {
49 4, // AOK_SizeDirective
59 AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len = 0, unsigned val = 0)
60 : Kind(kind), Loc(loc), Len(len), Val(val) {}
63 struct ParseInstructionInfo {
65 SmallVectorImpl<AsmRewrite> *AsmRewrites;
67 ParseInstructionInfo() : AsmRewrites(nullptr) {}
68 ParseInstructionInfo(SmallVectorImpl<AsmRewrite> *rewrites)
69 : AsmRewrites(rewrites) {}
71 ~ParseInstructionInfo() {}
74 /// MCTargetAsmParser - Generic interface to target specific assembly parsers.
75 class MCTargetAsmParser : public MCAsmParserExtension {
82 FIRST_TARGET_MATCH_RESULT_TY
86 MCTargetAsmParser(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
87 void operator=(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
88 protected: // Can only create subclasses.
91 /// AvailableFeatures - The current set of available features.
92 unsigned AvailableFeatures;
94 /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
95 bool ParsingInlineAsm;
97 /// SemaCallback - The Sema callback implementation. Must be set when parsing
98 /// ms-style inline assembly.
99 MCAsmParserSemaCallback *SemaCallback;
101 /// Set of options which affects instrumentation of inline assembly.
102 MCTargetOptions MCOptions;
105 virtual ~MCTargetAsmParser();
107 unsigned getAvailableFeatures() const { return AvailableFeatures; }
108 void setAvailableFeatures(unsigned Value) { AvailableFeatures = Value; }
110 bool isParsingInlineAsm () { return ParsingInlineAsm; }
111 void setParsingInlineAsm (bool Value) { ParsingInlineAsm = Value; }
113 void setSemaCallback(MCAsmParserSemaCallback *Callback) {
114 SemaCallback = Callback;
117 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
120 /// ParseInstruction - Parse one assembly instruction.
122 /// The parser is positioned following the instruction name. The target
123 /// specific instruction parser should parse the entire instruction and
124 /// construct the appropriate MCInst, or emit an error. On success, the entire
125 /// line should be parsed up to and including the end-of-statement token. On
126 /// failure, the parser is not required to read to the end of the line.
128 /// \param Name - The instruction name.
129 /// \param NameLoc - The source location of the name.
130 /// \param Operands [out] - The list of parsed operands, this returns
131 /// ownership of them to the caller.
132 /// \return True on failure.
133 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
135 SmallVectorImpl<MCParsedAsmOperand*> &Operands) = 0;
137 /// ParseDirective - Parse a target specific assembler directive
139 /// The parser is positioned following the directive name. The target
140 /// specific directive parser should parse the entire directive doing or
141 /// recording any target specific work, or return true and do nothing if the
142 /// directive is not target specific. If the directive is specific for
143 /// the target, the entire line is parsed up to and including the
144 /// end-of-statement token and false is returned.
146 /// \param DirectiveID - the identifier token of the directive.
147 virtual bool ParseDirective(AsmToken DirectiveID) = 0;
149 /// mnemonicIsValid - This returns true if this is a valid mnemonic and false
151 virtual bool mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) = 0;
153 /// MatchAndEmitInstruction - Recognize a series of operands of a parsed
154 /// instruction as an actual MCInst and emit it to the specified MCStreamer.
155 /// This returns false on success and returns true on failure to match.
157 /// On failure, the target parser is responsible for emitting a diagnostic
158 /// explaining the match failure.
160 MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
161 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
162 MCStreamer &Out, unsigned &ErrorInfo,
163 bool MatchingInlineAsm) = 0;
165 /// Allow a target to add special case operand matching for things that
166 /// tblgen doesn't/can't handle effectively. For example, literal
167 /// immediates on ARM. TableGen expects a token operand, but the parser
168 /// will recognize them as immediates.
169 virtual unsigned validateTargetOperandClass(MCParsedAsmOperand *Op,
171 return Match_InvalidOperand;
174 /// checkTargetMatchPredicate - Validate the instruction match against
175 /// any complex target predicates not expressible via match classes.
176 virtual unsigned checkTargetMatchPredicate(MCInst &Inst) {
177 return Match_Success;
180 virtual void convertToMapAndConstraints(unsigned Kind,
181 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) = 0;
183 virtual const MCExpr *applyModifierToExpr(const MCExpr *E,
184 MCSymbolRefExpr::VariantKind,
189 virtual void onLabelParsed(MCSymbol *Symbol) { };
192 } // End llvm namespace