LLVM API Documentation

MCTargetAsmParser.h
Go to the documentation of this file.
00001 //===-- llvm/MC/MCTargetAsmParser.h - Target Assembly Parser ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_MC_MCTARGETASMPARSER_H
00011 #define LLVM_MC_MCTARGETASMPARSER_H
00012 
00013 #include "llvm/MC/MCExpr.h"
00014 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
00015 #include "llvm/MC/MCTargetOptions.h"
00016 
00017 #include <memory>
00018 
00019 namespace llvm {
00020 class AsmToken;
00021 class MCInst;
00022 class MCParsedAsmOperand;
00023 class MCStreamer;
00024 class SMLoc;
00025 class StringRef;
00026 template <typename T> class SmallVectorImpl;
00027 
00028 typedef SmallVectorImpl<std::unique_ptr<MCParsedAsmOperand>> OperandVector;
00029 
00030 enum AsmRewriteKind {
00031   AOK_Delete = 0,     // Rewrite should be ignored.
00032   AOK_Align,          // Rewrite align as .align.
00033   AOK_DotOperator,    // Rewrite a dot operator expression as an immediate.
00034                       // E.g., [eax].foo.bar -> [eax].8
00035   AOK_Emit,           // Rewrite _emit as .byte.
00036   AOK_Imm,            // Rewrite as $$N.
00037   AOK_ImmPrefix,      // Add $$ before a parsed Imm.
00038   AOK_Input,          // Rewrite in terms of $N.
00039   AOK_Output,         // Rewrite in terms of $N.
00040   AOK_SizeDirective,  // Add a sizing directive (e.g., dword ptr).
00041   AOK_Skip            // Skip emission (e.g., offset/type operators).
00042 };
00043 
00044 const char AsmRewritePrecedence [] = {
00045   0, // AOK_Delete
00046   1, // AOK_Align
00047   1, // AOK_DotOperator
00048   1, // AOK_Emit
00049   3, // AOK_Imm
00050   3, // AOK_ImmPrefix
00051   2, // AOK_Input
00052   2, // AOK_Output
00053   4, // AOK_SizeDirective
00054   1  // AOK_Skip
00055 };
00056 
00057 struct AsmRewrite {
00058   AsmRewriteKind Kind;
00059   SMLoc Loc;
00060   unsigned Len;
00061   unsigned Val;
00062 public:
00063   AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len = 0, unsigned val = 0)
00064     : Kind(kind), Loc(loc), Len(len), Val(val) {}
00065 };
00066 
00067 struct ParseInstructionInfo {
00068 
00069   SmallVectorImpl<AsmRewrite> *AsmRewrites;
00070 
00071   ParseInstructionInfo() : AsmRewrites(nullptr) {}
00072   ParseInstructionInfo(SmallVectorImpl<AsmRewrite> *rewrites)
00073     : AsmRewrites(rewrites) {}
00074 
00075   ~ParseInstructionInfo() {}
00076 };
00077 
00078 /// MCTargetAsmParser - Generic interface to target specific assembly parsers.
00079 class MCTargetAsmParser : public MCAsmParserExtension {
00080 public:
00081   enum MatchResultTy {
00082     Match_InvalidOperand,
00083     Match_MissingFeature,
00084     Match_MnemonicFail,
00085     Match_Success,
00086     FIRST_TARGET_MATCH_RESULT_TY
00087   };
00088 
00089 private:
00090   MCTargetAsmParser(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
00091   void operator=(const MCTargetAsmParser &) LLVM_DELETED_FUNCTION;
00092 protected: // Can only create subclasses.
00093   MCTargetAsmParser();
00094 
00095   /// AvailableFeatures - The current set of available features.
00096   uint64_t AvailableFeatures;
00097 
00098   /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
00099   bool ParsingInlineAsm;
00100 
00101   /// SemaCallback - The Sema callback implementation.  Must be set when parsing
00102   /// ms-style inline assembly.
00103   MCAsmParserSemaCallback *SemaCallback;
00104 
00105   /// Set of options which affects instrumentation of inline assembly.
00106   MCTargetOptions MCOptions;
00107 
00108 public:
00109   virtual ~MCTargetAsmParser();
00110 
00111   uint64_t getAvailableFeatures() const { return AvailableFeatures; }
00112   void setAvailableFeatures(uint64_t Value) { AvailableFeatures = Value; }
00113 
00114   bool isParsingInlineAsm () { return ParsingInlineAsm; }
00115   void setParsingInlineAsm (bool Value) { ParsingInlineAsm = Value; }
00116 
00117   MCTargetOptions getTargetOptions() const { return MCOptions; }
00118 
00119   void setSemaCallback(MCAsmParserSemaCallback *Callback) {
00120     SemaCallback = Callback;
00121   }
00122 
00123   virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
00124                              SMLoc &EndLoc) = 0;
00125 
00126   /// Sets frame register corresponding to the current MachineFunction.
00127   virtual void SetFrameRegister(unsigned RegNo) {}
00128 
00129   /// ParseInstruction - Parse one assembly instruction.
00130   ///
00131   /// The parser is positioned following the instruction name. The target
00132   /// specific instruction parser should parse the entire instruction and
00133   /// construct the appropriate MCInst, or emit an error. On success, the entire
00134   /// line should be parsed up to and including the end-of-statement token. On
00135   /// failure, the parser is not required to read to the end of the line.
00136   //
00137   /// \param Name - The instruction name.
00138   /// \param NameLoc - The source location of the name.
00139   /// \param Operands [out] - The list of parsed operands, this returns
00140   ///        ownership of them to the caller.
00141   /// \return True on failure.
00142   virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
00143                                 SMLoc NameLoc, OperandVector &Operands) = 0;
00144 
00145   /// ParseDirective - Parse a target specific assembler directive
00146   ///
00147   /// The parser is positioned following the directive name.  The target
00148   /// specific directive parser should parse the entire directive doing or
00149   /// recording any target specific work, or return true and do nothing if the
00150   /// directive is not target specific. If the directive is specific for
00151   /// the target, the entire line is parsed up to and including the
00152   /// end-of-statement token and false is returned.
00153   ///
00154   /// \param DirectiveID - the identifier token of the directive.
00155   virtual bool ParseDirective(AsmToken DirectiveID) = 0;
00156 
00157   /// mnemonicIsValid - This returns true if this is a valid mnemonic and false
00158   /// otherwise.
00159   virtual bool mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) = 0;
00160 
00161   /// MatchAndEmitInstruction - Recognize a series of operands of a parsed
00162   /// instruction as an actual MCInst and emit it to the specified MCStreamer.
00163   /// This returns false on success and returns true on failure to match.
00164   ///
00165   /// On failure, the target parser is responsible for emitting a diagnostic
00166   /// explaining the match failure.
00167   virtual bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
00168                                        OperandVector &Operands, MCStreamer &Out,
00169                                        uint64_t &ErrorInfo,
00170                                        bool MatchingInlineAsm) = 0;
00171 
00172   /// Allows targets to let registers opt out of clobber lists.
00173   virtual bool OmitRegisterFromClobberLists(unsigned RegNo) { return false; }
00174 
00175   /// Allow a target to add special case operand matching for things that
00176   /// tblgen doesn't/can't handle effectively. For example, literal
00177   /// immediates on ARM. TableGen expects a token operand, but the parser
00178   /// will recognize them as immediates.
00179   virtual unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
00180                                               unsigned Kind) {
00181     return Match_InvalidOperand;
00182   }
00183 
00184   /// checkTargetMatchPredicate - Validate the instruction match against
00185   /// any complex target predicates not expressible via match classes.
00186   virtual unsigned checkTargetMatchPredicate(MCInst &Inst) {
00187     return Match_Success;
00188   }
00189 
00190   virtual void convertToMapAndConstraints(unsigned Kind,
00191                                           const OperandVector &Operands) = 0;
00192 
00193   virtual const MCExpr *applyModifierToExpr(const MCExpr *E,
00194                                             MCSymbolRefExpr::VariantKind,
00195                                             MCContext &Ctx) {
00196     return nullptr;
00197   }
00198 
00199   virtual void onLabelParsed(MCSymbol *Symbol) { };
00200 };
00201 
00202 } // End llvm namespace
00203 
00204 #endif