MCTargetAsmParser.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #ifndef LLVM_MC_MCTARGETASMPARSER_H
  10. #define LLVM_MC_MCTARGETASMPARSER_H
  11. #include "llvm/MC/MCExpr.h"
  12. #include "llvm/MC/MCParser/MCAsmParserExtension.h"
  13. #include "llvm/MC/MCTargetOptions.h"
  14. #include <memory>
  15. namespace llvm {
  16. class AsmToken;
  17. class MCInst;
  18. class MCParsedAsmOperand;
  19. class MCStreamer;
  20. class SMLoc;
  21. class StringRef;
  22. template <typename T> class SmallVectorImpl;
  23. typedef SmallVectorImpl<std::unique_ptr<MCParsedAsmOperand>> OperandVector;
  24. enum AsmRewriteKind {
  25. AOK_Delete = 0, // Rewrite should be ignored.
  26. AOK_Align, // Rewrite align as .align.
  27. AOK_DotOperator, // Rewrite a dot operator expression as an immediate.
  28. // E.g., [eax].foo.bar -> [eax].8
  29. AOK_Emit, // Rewrite _emit as .byte.
  30. AOK_Imm, // Rewrite as $$N.
  31. AOK_ImmPrefix, // Add $$ before a parsed Imm.
  32. AOK_Input, // Rewrite in terms of $N.
  33. AOK_Output, // Rewrite in terms of $N.
  34. AOK_SizeDirective, // Add a sizing directive (e.g., dword ptr).
  35. AOK_Label, // Rewrite local labels.
  36. AOK_Skip // Skip emission (e.g., offset/type operators).
  37. };
  38. const char AsmRewritePrecedence [] = {
  39. 0, // AOK_Delete
  40. 2, // AOK_Align
  41. 2, // AOK_DotOperator
  42. 2, // AOK_Emit
  43. 4, // AOK_Imm
  44. 4, // AOK_ImmPrefix
  45. 3, // AOK_Input
  46. 3, // AOK_Output
  47. 5, // AOK_SizeDirective
  48. 1, // AOK_Label
  49. 2 // AOK_Skip
  50. };
  51. struct AsmRewrite {
  52. AsmRewriteKind Kind;
  53. SMLoc Loc;
  54. unsigned Len;
  55. unsigned Val;
  56. StringRef Label;
  57. public:
  58. AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len = 0, unsigned val = 0)
  59. : Kind(kind), Loc(loc), Len(len), Val(val) {}
  60. AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len, StringRef label)
  61. : Kind(kind), Loc(loc), Len(len), Val(0), Label(label) {}
  62. };
  63. struct ParseInstructionInfo {
  64. SmallVectorImpl<AsmRewrite> *AsmRewrites;
  65. ParseInstructionInfo() : AsmRewrites(nullptr) {}
  66. ParseInstructionInfo(SmallVectorImpl<AsmRewrite> *rewrites)
  67. : AsmRewrites(rewrites) {}
  68. };
  69. /// MCTargetAsmParser - Generic interface to target specific assembly parsers.
  70. class MCTargetAsmParser : public MCAsmParserExtension {
  71. public:
  72. enum MatchResultTy {
  73. Match_InvalidOperand,
  74. Match_MissingFeature,
  75. Match_MnemonicFail,
  76. Match_Success,
  77. FIRST_TARGET_MATCH_RESULT_TY
  78. };
  79. private:
  80. MCTargetAsmParser(const MCTargetAsmParser &) = delete;
  81. void operator=(const MCTargetAsmParser &) = delete;
  82. protected: // Can only create subclasses.
  83. MCTargetAsmParser();
  84. /// AvailableFeatures - The current set of available features.
  85. uint64_t AvailableFeatures;
  86. /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
  87. bool ParsingInlineAsm;
  88. /// SemaCallback - The Sema callback implementation. Must be set when parsing
  89. /// ms-style inline assembly.
  90. MCAsmParserSemaCallback *SemaCallback;
  91. /// Set of options which affects instrumentation of inline assembly.
  92. MCTargetOptions MCOptions;
  93. public:
  94. ~MCTargetAsmParser() override;
  95. uint64_t getAvailableFeatures() const { return AvailableFeatures; }
  96. void setAvailableFeatures(uint64_t Value) { AvailableFeatures = Value; }
  97. bool isParsingInlineAsm () { return ParsingInlineAsm; }
  98. void setParsingInlineAsm (bool Value) { ParsingInlineAsm = Value; }
  99. MCTargetOptions getTargetOptions() const { return MCOptions; }
  100. void setSemaCallback(MCAsmParserSemaCallback *Callback) {
  101. SemaCallback = Callback;
  102. }
  103. virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
  104. SMLoc &EndLoc) = 0;
  105. /// Sets frame register corresponding to the current MachineFunction.
  106. virtual void SetFrameRegister(unsigned RegNo) {}
  107. /// ParseInstruction - Parse one assembly instruction.
  108. ///
  109. /// The parser is positioned following the instruction name. The target
  110. /// specific instruction parser should parse the entire instruction and
  111. /// construct the appropriate MCInst, or emit an error. On success, the entire
  112. /// line should be parsed up to and including the end-of-statement token. On
  113. /// failure, the parser is not required to read to the end of the line.
  114. //
  115. /// \param Name - The instruction name.
  116. /// \param NameLoc - The source location of the name.
  117. /// \param Operands [out] - The list of parsed operands, this returns
  118. /// ownership of them to the caller.
  119. /// \return True on failure.
  120. virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
  121. SMLoc NameLoc, OperandVector &Operands) = 0;
  122. /// ParseDirective - Parse a target specific assembler directive
  123. ///
  124. /// The parser is positioned following the directive name. The target
  125. /// specific directive parser should parse the entire directive doing or
  126. /// recording any target specific work, or return true and do nothing if the
  127. /// directive is not target specific. If the directive is specific for
  128. /// the target, the entire line is parsed up to and including the
  129. /// end-of-statement token and false is returned.
  130. ///
  131. /// \param DirectiveID - the identifier token of the directive.
  132. virtual bool ParseDirective(AsmToken DirectiveID) = 0;
  133. /// mnemonicIsValid - This returns true if this is a valid mnemonic and false
  134. /// otherwise.
  135. virtual bool mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) = 0;
  136. /// MatchAndEmitInstruction - Recognize a series of operands of a parsed
  137. /// instruction as an actual MCInst and emit it to the specified MCStreamer.
  138. /// This returns false on success and returns true on failure to match.
  139. ///
  140. /// On failure, the target parser is responsible for emitting a diagnostic
  141. /// explaining the match failure.
  142. virtual bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
  143. OperandVector &Operands, MCStreamer &Out,
  144. uint64_t &ErrorInfo,
  145. bool MatchingInlineAsm) = 0;
  146. /// Allows targets to let registers opt out of clobber lists.
  147. virtual bool OmitRegisterFromClobberLists(unsigned RegNo) { return false; }
  148. /// Allow a target to add special case operand matching for things that
  149. /// tblgen doesn't/can't handle effectively. For example, literal
  150. /// immediates on ARM. TableGen expects a token operand, but the parser
  151. /// will recognize them as immediates.
  152. virtual unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
  153. unsigned Kind) {
  154. return Match_InvalidOperand;
  155. }
  156. /// checkTargetMatchPredicate - Validate the instruction match against
  157. /// any complex target predicates not expressible via match classes.
  158. virtual unsigned checkTargetMatchPredicate(MCInst &Inst) {
  159. return Match_Success;
  160. }
  161. virtual void convertToMapAndConstraints(unsigned Kind,
  162. const OperandVector &Operands) = 0;
  163. virtual const MCExpr *applyModifierToExpr(const MCExpr *E,
  164. MCSymbolRefExpr::VariantKind,
  165. MCContext &Ctx) {
  166. return nullptr;
  167. }
  168. virtual void onLabelParsed(MCSymbol *Symbol) { };
  169. };
  170. } // End llvm namespace
  171. #endif