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/MCParser/MCAsmParserExtension.h"
14 #include "llvm/MC/MCExpr.h"
21 class MCParsedAsmOperand;
23 template <typename T> class SmallVectorImpl;
26 AOK_Delete = 0, // Rewrite should be ignored.
27 AOK_Align, // Rewrite align as .align.
28 AOK_DotOperator, // Rewrite a dot operator expression as an immediate.
29 // E.g., [eax].foo.bar -> [eax].8
30 AOK_Emit, // Rewrite _emit as .byte.
31 AOK_Imm, // Rewrite as $$N.
32 AOK_ImmPrefix, // Add $$ before a parsed Imm.
33 AOK_Input, // Rewrite in terms of $N.
34 AOK_Output, // Rewrite in terms of $N.
35 AOK_SizeDirective, // Add a sizing directive (e.g., dword ptr).
36 AOK_Skip // Skip emission (e.g., offset/type operators).
39 const char AsmRewritePrecedence [] = {
48 4, // AOK_SizeDirective
58 AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len = 0, unsigned val = 0)
59 : Kind(kind), Loc(loc), Len(len), Val(val) {}
62 struct ParseInstructionInfo {
64 SmallVectorImpl<AsmRewrite> *AsmRewrites;
66 ParseInstructionInfo() : AsmRewrites(0) {}
67 ParseInstructionInfo(SmallVectorImpl<AsmRewrite> *rewrites)
68 : AsmRewrites(rewrites) {}
70 ~ParseInstructionInfo() {}
73 /// MCTargetAsmParser - Generic interface to target specific assembly parsers.
74 class MCTargetAsmParser : public MCAsmParserExtension {
81 FIRST_TARGET_MATCH_RESULT_TY
85 MCTargetAsmParser(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
86 void operator=(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
87 protected: // Can only create subclasses.
90 /// AvailableFeatures - The current set of available features.
91 unsigned AvailableFeatures;
93 /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
94 bool ParsingInlineAsm;
96 /// SemaCallback - The Sema callback implementation. Must be set when parsing
97 /// ms-style inline assembly.
98 MCAsmParserSemaCallback *SemaCallback;
101 virtual ~MCTargetAsmParser();
103 unsigned getAvailableFeatures() const { return AvailableFeatures; }
104 void setAvailableFeatures(unsigned Value) { AvailableFeatures = Value; }
106 bool isParsingInlineAsm () { return ParsingInlineAsm; }
107 void setParsingInlineAsm (bool Value) { ParsingInlineAsm = Value; }
109 void setSemaCallback(MCAsmParserSemaCallback *Callback) {
110 SemaCallback = Callback;
113 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
116 /// ParseInstruction - Parse one assembly instruction.
118 /// The parser is positioned following the instruction name. The target
119 /// specific instruction parser should parse the entire instruction and
120 /// construct the appropriate MCInst, or emit an error. On success, the entire
121 /// line should be parsed up to and including the end-of-statement token. On
122 /// failure, the parser is not required to read to the end of the line.
124 /// \param Name - The instruction name.
125 /// \param NameLoc - The source location of the name.
126 /// \param Operands [out] - The list of parsed operands, this returns
127 /// ownership of them to the caller.
128 /// \return True on failure.
129 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
131 SmallVectorImpl<MCParsedAsmOperand*> &Operands) = 0;
133 /// ParseDirective - Parse a target specific assembler directive
135 /// The parser is positioned following the directive name. The target
136 /// specific directive parser should parse the entire directive doing or
137 /// recording any target specific work, or return true and do nothing if the
138 /// directive is not target specific. If the directive is specific for
139 /// the target, the entire line is parsed up to and including the
140 /// end-of-statement token and false is returned.
142 /// \param DirectiveID - the identifier token of the directive.
143 virtual bool ParseDirective(AsmToken DirectiveID) = 0;
145 /// mnemonicIsValid - This returns true if this is a valid mnemonic and false
147 virtual bool mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) = 0;
149 /// MatchAndEmitInstruction - Recognize a series of operands of a parsed
150 /// instruction as an actual MCInst and emit it to the specified MCStreamer.
151 /// This returns false on success and returns true on failure to match.
153 /// On failure, the target parser is responsible for emitting a diagnostic
154 /// explaining the match failure.
156 MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
157 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
158 MCStreamer &Out, unsigned &ErrorInfo,
159 bool MatchingInlineAsm) = 0;
161 /// Allow a target to add special case operand matching for things that
162 /// tblgen doesn't/can't handle effectively. For example, literal
163 /// immediates on ARM. TableGen expects a token operand, but the parser
164 /// will recognize them as immediates.
165 virtual unsigned validateTargetOperandClass(MCParsedAsmOperand *Op,
167 return Match_InvalidOperand;
170 /// checkTargetMatchPredicate - Validate the instruction match against
171 /// any complex target predicates not expressible via match classes.
172 virtual unsigned checkTargetMatchPredicate(MCInst &Inst) {
173 return Match_Success;
176 virtual void convertToMapAndConstraints(unsigned Kind,
177 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) = 0;
179 virtual const MCExpr *applyModifierToExpr(const MCExpr *E,
180 MCSymbolRefExpr::VariantKind,
186 } // End llvm namespace