MCTargetAsmParser.h revision dce4a407a24b04eebc6a376f8e62b41aaa7b071f
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_TARGETPARSER_H 11#define LLVM_MC_TARGETPARSER_H 12 13#include "llvm/MC/MCExpr.h" 14#include "llvm/MC/MCParser/MCAsmParserExtension.h" 15#include "llvm/MC/MCTargetOptions.h" 16 17namespace llvm { 18class AsmToken; 19class MCInst; 20class MCParsedAsmOperand; 21class MCStreamer; 22class SMLoc; 23class StringRef; 24template <typename T> class SmallVectorImpl; 25 26enum AsmRewriteKind { 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). 38}; 39 40const char AsmRewritePrecedence [] = { 41 0, // AOK_Delete 42 1, // AOK_Align 43 1, // AOK_DotOperator 44 1, // AOK_Emit 45 3, // AOK_Imm 46 3, // AOK_ImmPrefix 47 2, // AOK_Input 48 2, // AOK_Output 49 4, // AOK_SizeDirective 50 1 // AOK_Skip 51}; 52 53struct AsmRewrite { 54 AsmRewriteKind Kind; 55 SMLoc Loc; 56 unsigned Len; 57 unsigned Val; 58public: 59 AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len = 0, unsigned val = 0) 60 : Kind(kind), Loc(loc), Len(len), Val(val) {} 61}; 62 63struct ParseInstructionInfo { 64 65 SmallVectorImpl<AsmRewrite> *AsmRewrites; 66 67 ParseInstructionInfo() : AsmRewrites(nullptr) {} 68 ParseInstructionInfo(SmallVectorImpl<AsmRewrite> *rewrites) 69 : AsmRewrites(rewrites) {} 70 71 ~ParseInstructionInfo() {} 72}; 73 74/// MCTargetAsmParser - Generic interface to target specific assembly parsers. 75class MCTargetAsmParser : public MCAsmParserExtension { 76public: 77 enum MatchResultTy { 78 Match_InvalidOperand, 79 Match_MissingFeature, 80 Match_MnemonicFail, 81 Match_Success, 82 FIRST_TARGET_MATCH_RESULT_TY 83 }; 84 85private: 86 MCTargetAsmParser(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION; 87 void operator=(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION; 88protected: // Can only create subclasses. 89 MCTargetAsmParser(); 90 91 /// AvailableFeatures - The current set of available features. 92 unsigned AvailableFeatures; 93 94 /// ParsingInlineAsm - Are we parsing ms-style inline assembly? 95 bool ParsingInlineAsm; 96 97 /// SemaCallback - The Sema callback implementation. Must be set when parsing 98 /// ms-style inline assembly. 99 MCAsmParserSemaCallback *SemaCallback; 100 101 /// Set of options which affects instrumentation of inline assembly. 102 MCTargetOptions MCOptions; 103 104public: 105 virtual ~MCTargetAsmParser(); 106 107 unsigned getAvailableFeatures() const { return AvailableFeatures; } 108 void setAvailableFeatures(unsigned Value) { AvailableFeatures = Value; } 109 110 bool isParsingInlineAsm () { return ParsingInlineAsm; } 111 void setParsingInlineAsm (bool Value) { ParsingInlineAsm = Value; } 112 113 void setSemaCallback(MCAsmParserSemaCallback *Callback) { 114 SemaCallback = Callback; 115 } 116 117 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, 118 SMLoc &EndLoc) = 0; 119 120 /// ParseInstruction - Parse one assembly instruction. 121 /// 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. 127 // 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, 134 SMLoc NameLoc, 135 SmallVectorImpl<MCParsedAsmOperand*> &Operands) = 0; 136 137 /// ParseDirective - Parse a target specific assembler directive 138 /// 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. 145 /// 146 /// \param DirectiveID - the identifier token of the directive. 147 virtual bool ParseDirective(AsmToken DirectiveID) = 0; 148 149 /// mnemonicIsValid - This returns true if this is a valid mnemonic and false 150 /// otherwise. 151 virtual bool mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) = 0; 152 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. 156 /// 157 /// On failure, the target parser is responsible for emitting a diagnostic 158 /// explaining the match failure. 159 virtual bool 160 MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, 161 SmallVectorImpl<MCParsedAsmOperand*> &Operands, 162 MCStreamer &Out, unsigned &ErrorInfo, 163 bool MatchingInlineAsm) = 0; 164 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, 170 unsigned Kind) { 171 return Match_InvalidOperand; 172 } 173 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; 178 } 179 180 virtual void convertToMapAndConstraints(unsigned Kind, 181 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) = 0; 182 183 virtual const MCExpr *applyModifierToExpr(const MCExpr *E, 184 MCSymbolRefExpr::VariantKind, 185 MCContext &Ctx) { 186 return nullptr; 187 } 188 189 virtual void onLabelParsed(MCSymbol *Symbol) { }; 190}; 191 192} // End llvm namespace 193 194#endif 195