1214640Sdim//===-- llvm/MC/MCTargetAsmParser.h - Target Assembly Parser ----*- C++ -*-===//
2214640Sdim//
3214640Sdim//                     The LLVM Compiler Infrastructure
4214640Sdim//
5214640Sdim// This file is distributed under the University of Illinois Open Source
6214640Sdim// License. See LICENSE.TXT for details.
7214640Sdim//
8214640Sdim//===----------------------------------------------------------------------===//
9214640Sdim
10214640Sdim#ifndef LLVM_MC_TARGETPARSER_H
11214640Sdim#define LLVM_MC_TARGETPARSER_H
12214640Sdim
13214640Sdim#include "llvm/MC/MCParser/MCAsmParserExtension.h"
14214640Sdim#include "llvm/MC/MCExpr.h"
15214640Sdim
16214640Sdimnamespace llvm {
17214640Sdimclass MCStreamer;
18214640Sdimclass StringRef;
19214640Sdimclass SMLoc;
20214640Sdimclass AsmToken;
21214640Sdimclass MCParsedAsmOperand;
22214640Sdimclass MCInst;
23214640Sdimtemplate <typename T> class SmallVectorImpl;
24214640Sdim
25214640Sdimenum AsmRewriteKind {
26214640Sdim  AOK_Delete = 0,     // Rewrite should be ignored.
27214640Sdim  AOK_Align,          // Rewrite align as .align.
28214640Sdim  AOK_DotOperator,    // Rewrite a dot operator expression as an immediate.
29214640Sdim                      // E.g., [eax].foo.bar -> [eax].8
30214640Sdim  AOK_Emit,           // Rewrite _emit as .byte.
31214640Sdim  AOK_Imm,            // Rewrite as $$N.
32214640Sdim  AOK_ImmPrefix,      // Add $$ before a parsed Imm.
33214640Sdim  AOK_Input,          // Rewrite in terms of $N.
34214640Sdim  AOK_Output,         // Rewrite in terms of $N.
35214640Sdim  AOK_SizeDirective,  // Add a sizing directive (e.g., dword ptr).
36214640Sdim  AOK_Skip            // Skip emission (e.g., offset/type operators).
37214640Sdim};
38214640Sdim
39214640Sdimconst char AsmRewritePrecedence [] = {
40214640Sdim  0, // AOK_Delete
41214640Sdim  1, // AOK_Align
42214640Sdim  1, // AOK_DotOperator
43214640Sdim  1, // AOK_Emit
44214640Sdim  3, // AOK_Imm
45214640Sdim  3, // AOK_ImmPrefix
46214640Sdim  2, // AOK_Input
47214640Sdim  2, // AOK_Output
48214640Sdim  4, // AOK_SizeDirective
49214640Sdim  1  // AOK_Skip
50214640Sdim};
51214640Sdim
52214640Sdimstruct AsmRewrite {
53214640Sdim  AsmRewriteKind Kind;
54214640Sdim  SMLoc Loc;
55214640Sdim  unsigned Len;
56214640Sdim  unsigned Val;
57214640Sdimpublic:
58214640Sdim  AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len = 0, unsigned val = 0)
59214640Sdim    : Kind(kind), Loc(loc), Len(len), Val(val) {}
60214640Sdim};
61214640Sdim
62214640Sdimstruct ParseInstructionInfo {
63214640Sdim
64214640Sdim  SmallVectorImpl<AsmRewrite> *AsmRewrites;
65214640Sdim
66214640Sdim  ParseInstructionInfo() : AsmRewrites(0) {}
67214640Sdim  ParseInstructionInfo(SmallVectorImpl<AsmRewrite> *rewrites)
68214640Sdim    : AsmRewrites(rewrites) {}
69214640Sdim
70214640Sdim  ~ParseInstructionInfo() {}
71214640Sdim};
72214640Sdim
73214640Sdim/// MCTargetAsmParser - Generic interface to target specific assembly parsers.
74214640Sdimclass MCTargetAsmParser : public MCAsmParserExtension {
75214640Sdimpublic:
76214640Sdim  enum MatchResultTy {
77214640Sdim    Match_InvalidOperand,
78214640Sdim    Match_MissingFeature,
79214640Sdim    Match_MnemonicFail,
80214640Sdim    Match_Success,
81214640Sdim    FIRST_TARGET_MATCH_RESULT_TY
82214640Sdim  };
83214640Sdim
84214640Sdimprivate:
85214640Sdim  MCTargetAsmParser(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
86214640Sdim  void operator=(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
87214640Sdimprotected: // Can only create subclasses.
88214640Sdim  MCTargetAsmParser();
89214640Sdim
90214640Sdim  /// AvailableFeatures - The current set of available features.
91214640Sdim  unsigned AvailableFeatures;
92214640Sdim
93214640Sdim  /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
94214640Sdim  bool ParsingInlineAsm;
95214640Sdim
96214640Sdim  /// SemaCallback - The Sema callback implementation.  Must be set when parsing
97214640Sdim  /// ms-style inline assembly.
98214640Sdim  MCAsmParserSemaCallback *SemaCallback;
99214640Sdim
100214640Sdimpublic:
101214640Sdim  virtual ~MCTargetAsmParser();
102214640Sdim
103214640Sdim  unsigned getAvailableFeatures() const { return AvailableFeatures; }
104214640Sdim  void setAvailableFeatures(unsigned Value) { AvailableFeatures = Value; }
105214640Sdim
106214640Sdim  bool isParsingInlineAsm () { return ParsingInlineAsm; }
107214640Sdim  void setParsingInlineAsm (bool Value) { ParsingInlineAsm = Value; }
108214640Sdim
109214640Sdim  void setSemaCallback(MCAsmParserSemaCallback *Callback) {
110214640Sdim    SemaCallback = Callback;
111214640Sdim  }
112214640Sdim
113214640Sdim  virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
114214640Sdim                             SMLoc &EndLoc) = 0;
115214640Sdim
116214640Sdim  /// ParseInstruction - Parse one assembly instruction.
117214640Sdim  ///
118214640Sdim  /// The parser is positioned following the instruction name. The target
119214640Sdim  /// specific instruction parser should parse the entire instruction and
120214640Sdim  /// construct the appropriate MCInst, or emit an error. On success, the entire
121214640Sdim  /// line should be parsed up to and including the end-of-statement token. On
122214640Sdim  /// failure, the parser is not required to read to the end of the line.
123214640Sdim  //
124214640Sdim  /// \param Name - The instruction name.
125214640Sdim  /// \param NameLoc - The source location of the name.
126214640Sdim  /// \param Operands [out] - The list of parsed operands, this returns
127214640Sdim  ///        ownership of them to the caller.
128214640Sdim  /// \return True on failure.
129214640Sdim  virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
130214640Sdim                                SMLoc NameLoc,
131214640Sdim                            SmallVectorImpl<MCParsedAsmOperand*> &Operands) = 0;
132214640Sdim
133214640Sdim  /// ParseDirective - Parse a target specific assembler directive
134235211Sgjb  ///
135214640Sdim  /// The parser is positioned following the directive name.  The target
136214640Sdim  /// specific directive parser should parse the entire directive doing or
137214640Sdim  /// recording any target specific work, or return true and do nothing if the
138214640Sdim  /// directive is not target specific. If the directive is specific for
139214640Sdim  /// the target, the entire line is parsed up to and including the
140214640Sdim  /// end-of-statement token and false is returned.
141214640Sdim  ///
142214640Sdim  /// \param DirectiveID - the identifier token of the directive.
143214640Sdim  virtual bool ParseDirective(AsmToken DirectiveID) = 0;
144214640Sdim
145214640Sdim  /// mnemonicIsValid - This returns true if this is a valid mnemonic and false
146214640Sdim  /// otherwise.
147214640Sdim  virtual bool mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) = 0;
148214640Sdim
149214640Sdim  /// MatchAndEmitInstruction - Recognize a series of operands of a parsed
150214640Sdim  /// instruction as an actual MCInst and emit it to the specified MCStreamer.
151214640Sdim  /// This returns false on success and returns true on failure to match.
152214640Sdim  ///
153214640Sdim  /// On failure, the target parser is responsible for emitting a diagnostic
154214640Sdim  /// explaining the match failure.
155214640Sdim  virtual bool
156214640Sdim  MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
157214640Sdim                          SmallVectorImpl<MCParsedAsmOperand*> &Operands,
158214640Sdim                          MCStreamer &Out, unsigned &ErrorInfo,
159214640Sdim                          bool MatchingInlineAsm) = 0;
160214640Sdim
161214640Sdim  /// Allow a target to add special case operand matching for things that
162214640Sdim  /// tblgen doesn't/can't handle effectively. For example, literal
163214640Sdim  /// immediates on ARM. TableGen expects a token operand, but the parser
164214640Sdim  /// will recognize them as immediates.
165214640Sdim  virtual unsigned validateTargetOperandClass(MCParsedAsmOperand *Op,
166214640Sdim                                              unsigned Kind) {
167214640Sdim    return Match_InvalidOperand;
168214640Sdim  }
169214640Sdim
170214640Sdim  /// checkTargetMatchPredicate - Validate the instruction match against
171214640Sdim  /// any complex target predicates not expressible via match classes.
172214640Sdim  virtual unsigned checkTargetMatchPredicate(MCInst &Inst) {
173214640Sdim    return Match_Success;
174214640Sdim  }
175214640Sdim
176214640Sdim  virtual void convertToMapAndConstraints(unsigned Kind,
177214640Sdim                      const SmallVectorImpl<MCParsedAsmOperand*> &Operands) = 0;
178214640Sdim
179214640Sdim  virtual const MCExpr *applyModifierToExpr(const MCExpr *E,
180214640Sdim                                            MCSymbolRefExpr::VariantKind,
181214640Sdim                                            MCContext &Ctx) {
182214640Sdim    return 0;
183214640Sdim  }
184214640Sdim
185214640Sdim  virtual void onLabelParsed(MCSymbol *Symbol) { };
186214640Sdim};
187214640Sdim
188214640Sdim} // End llvm namespace
189214640Sdim
190214640Sdim#endif
191214640Sdim