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