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_MCTARGETASMPARSER_H
11 #define LLVM_MC_MCTARGETASMPARSER_H
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
15 #include "llvm/MC/MCTargetOptions.h"
22 class MCParsedAsmOperand;
26 template <typename T> class SmallVectorImpl;
28 typedef SmallVectorImpl<std::unique_ptr<MCParsedAsmOperand>> OperandVector;
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_Skip // Skip emission (e.g., offset/type operators).
44 const char AsmRewritePrecedence [] = {
53 4, // AOK_SizeDirective
63 AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len = 0, unsigned val = 0)
64 : Kind(kind), Loc(loc), Len(len), Val(val) {}
67 struct ParseInstructionInfo {
69 SmallVectorImpl<AsmRewrite> *AsmRewrites;
71 ParseInstructionInfo() : AsmRewrites(nullptr) {}
72 ParseInstructionInfo(SmallVectorImpl<AsmRewrite> *rewrites)
73 : AsmRewrites(rewrites) {}
75 ~ParseInstructionInfo() {}
78 /// MCTargetAsmParser - Generic interface to target specific assembly parsers.
79 class MCTargetAsmParser : public MCAsmParserExtension {
86 FIRST_TARGET_MATCH_RESULT_TY
90 MCTargetAsmParser(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
91 void operator=(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
92 protected: // Can only create subclasses.
95 /// AvailableFeatures - The current set of available features.
96 uint64_t AvailableFeatures;
98 /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
99 bool ParsingInlineAsm;
101 /// SemaCallback - The Sema callback implementation. Must be set when parsing
102 /// ms-style inline assembly.
103 MCAsmParserSemaCallback *SemaCallback;
105 /// Set of options which affects instrumentation of inline assembly.
106 MCTargetOptions MCOptions;
109 virtual ~MCTargetAsmParser();
111 uint64_t getAvailableFeatures() const { return AvailableFeatures; }
112 void setAvailableFeatures(uint64_t Value) { AvailableFeatures = Value; }
114 bool isParsingInlineAsm () { return ParsingInlineAsm; }
115 void setParsingInlineAsm (bool Value) { ParsingInlineAsm = Value; }
117 MCTargetOptions getTargetOptions() const { return MCOptions; }
119 void setSemaCallback(MCAsmParserSemaCallback *Callback) {
120 SemaCallback = Callback;
123 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
126 /// ParseInstruction - Parse one assembly instruction.
128 /// The parser is positioned following the instruction name. The target
129 /// specific instruction parser should parse the entire instruction and
130 /// construct the appropriate MCInst, or emit an error. On success, the entire
131 /// line should be parsed up to and including the end-of-statement token. On
132 /// failure, the parser is not required to read to the end of the line.
134 /// \param Name - The instruction name.
135 /// \param NameLoc - The source location of the name.
136 /// \param Operands [out] - The list of parsed operands, this returns
137 /// ownership of them to the caller.
138 /// \return True on failure.
139 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
140 SMLoc NameLoc, OperandVector &Operands) = 0;
142 /// ParseDirective - Parse a target specific assembler directive
144 /// The parser is positioned following the directive name. The target
145 /// specific directive parser should parse the entire directive doing or
146 /// recording any target specific work, or return true and do nothing if the
147 /// directive is not target specific. If the directive is specific for
148 /// the target, the entire line is parsed up to and including the
149 /// end-of-statement token and false is returned.
151 /// \param DirectiveID - the identifier token of the directive.
152 virtual bool ParseDirective(AsmToken DirectiveID) = 0;
154 /// mnemonicIsValid - This returns true if this is a valid mnemonic and false
156 virtual bool mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) = 0;
158 /// MatchAndEmitInstruction - Recognize a series of operands of a parsed
159 /// instruction as an actual MCInst and emit it to the specified MCStreamer.
160 /// This returns false on success and returns true on failure to match.
162 /// On failure, the target parser is responsible for emitting a diagnostic
163 /// explaining the match failure.
164 virtual bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
165 OperandVector &Operands, MCStreamer &Out,
167 bool MatchingInlineAsm) = 0;
169 /// Allows targets to let registers opt out of clobber lists.
170 virtual bool OmitRegisterFromClobberLists(unsigned RegNo) { return false; }
172 /// Allow a target to add special case operand matching for things that
173 /// tblgen doesn't/can't handle effectively. For example, literal
174 /// immediates on ARM. TableGen expects a token operand, but the parser
175 /// will recognize them as immediates.
176 virtual unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
178 return Match_InvalidOperand;
181 /// checkTargetMatchPredicate - Validate the instruction match against
182 /// any complex target predicates not expressible via match classes.
183 virtual unsigned checkTargetMatchPredicate(MCInst &Inst) {
184 return Match_Success;
187 virtual void convertToMapAndConstraints(unsigned Kind,
188 const OperandVector &Operands) = 0;
190 virtual const MCExpr *applyModifierToExpr(const MCExpr *E,
191 MCSymbolRefExpr::VariantKind,
196 virtual void onLabelParsed(MCSymbol *Symbol) { };
199 } // End llvm namespace