MCInstPrinter.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MCInstPrinter.h - MCInst to target assembly syntax -------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_MC_MCINSTPRINTER_H
  14. #define LLVM_MC_MCINSTPRINTER_H
  15. #include "llvm/Support/Format.h"
  16. #include <cstdint>
  17. namespace llvm {
  18. class MCAsmInfo;
  19. class MCInst;
  20. class MCInstrAnalysis;
  21. class MCInstrInfo;
  22. class MCOperand;
  23. class MCRegister;
  24. class MCRegisterInfo;
  25. class MCSubtargetInfo;
  26. class StringRef;
  27. class raw_ostream;
  28. /// Convert `Bytes' to a hex string and output to `OS'
  29. void dumpBytes(ArrayRef<uint8_t> Bytes, raw_ostream &OS);
  30. namespace HexStyle {
  31. enum Style {
  32. C, ///< 0xff
  33. Asm ///< 0ffh
  34. };
  35. } // end namespace HexStyle
  36. struct AliasMatchingData;
  37. /// This is an instance of a target assembly language printer that
  38. /// converts an MCInst to valid target assembly syntax.
  39. class MCInstPrinter {
  40. protected:
  41. /// A stream that comments can be emitted to if desired. Each comment
  42. /// must end with a newline. This will be null if verbose assembly emission
  43. /// is disabled.
  44. raw_ostream *CommentStream = nullptr;
  45. const MCAsmInfo &MAI;
  46. const MCInstrInfo &MII;
  47. const MCRegisterInfo &MRI;
  48. const MCInstrAnalysis *MIA = nullptr;
  49. /// True if we are printing marked up assembly.
  50. bool UseMarkup = false;
  51. /// True if we prefer aliases (e.g. nop) to raw mnemonics.
  52. bool PrintAliases = true;
  53. /// True if we are printing immediates as hex.
  54. bool PrintImmHex = false;
  55. /// Which style to use for printing hexadecimal values.
  56. HexStyle::Style PrintHexStyle = HexStyle::C;
  57. /// If true, a branch immediate (e.g. bl 4) will be printed as a hexadecimal
  58. /// address (e.g. bl 0x20004). This is useful for a stream disassembler
  59. /// (llvm-objdump -d).
  60. bool PrintBranchImmAsAddress = false;
  61. /// If true, symbolize branch target and memory reference operands.
  62. bool SymbolizeOperands = false;
  63. /// Utility function for printing annotations.
  64. void printAnnotation(raw_ostream &OS, StringRef Annot);
  65. /// Helper for matching MCInsts to alias patterns when printing instructions.
  66. const char *matchAliasPatterns(const MCInst *MI, const MCSubtargetInfo *STI,
  67. const AliasMatchingData &M);
  68. public:
  69. MCInstPrinter(const MCAsmInfo &mai, const MCInstrInfo &mii,
  70. const MCRegisterInfo &mri) : MAI(mai), MII(mii), MRI(mri) {}
  71. virtual ~MCInstPrinter();
  72. /// Customize the printer according to a command line option.
  73. /// @return true if the option is recognized and applied.
  74. virtual bool applyTargetSpecificCLOption(StringRef Opt) { return false; }
  75. /// Specify a stream to emit comments to.
  76. void setCommentStream(raw_ostream &OS) { CommentStream = &OS; }
  77. /// Returns a pair containing the mnemonic for \p MI and the number of bits
  78. /// left for further processing by printInstruction (generated by tablegen).
  79. virtual std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) = 0;
  80. /// Print the specified MCInst to the specified raw_ostream.
  81. ///
  82. /// \p Address the address of current instruction on most targets, used to
  83. /// print a PC relative immediate as the target address. On targets where a PC
  84. /// relative immediate is relative to the next instruction and the length of a
  85. /// MCInst is difficult to measure (e.g. x86), this is the address of the next
  86. /// instruction. If Address is 0, the immediate will be printed.
  87. virtual void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
  88. const MCSubtargetInfo &STI, raw_ostream &OS) = 0;
  89. /// Return the name of the specified opcode enum (e.g. "MOV32ri") or
  90. /// empty if we can't resolve it.
  91. StringRef getOpcodeName(unsigned Opcode) const;
  92. /// Print the assembler register name.
  93. virtual void printRegName(raw_ostream &OS, MCRegister Reg) const;
  94. bool getUseMarkup() const { return UseMarkup; }
  95. void setUseMarkup(bool Value) { UseMarkup = Value; }
  96. /// Utility functions to make adding mark ups simpler.
  97. StringRef markup(StringRef s) const;
  98. bool getPrintImmHex() const { return PrintImmHex; }
  99. void setPrintImmHex(bool Value) { PrintImmHex = Value; }
  100. void setPrintHexStyle(HexStyle::Style Value) { PrintHexStyle = Value; }
  101. void setPrintBranchImmAsAddress(bool Value) {
  102. PrintBranchImmAsAddress = Value;
  103. }
  104. void setSymbolizeOperands(bool Value) { SymbolizeOperands = Value; }
  105. void setMCInstrAnalysis(const MCInstrAnalysis *Value) { MIA = Value; }
  106. /// Utility function to print immediates in decimal or hex.
  107. format_object<int64_t> formatImm(int64_t Value) const {
  108. return PrintImmHex ? formatHex(Value) : formatDec(Value);
  109. }
  110. /// Utility functions to print decimal/hexadecimal values.
  111. format_object<int64_t> formatDec(int64_t Value) const;
  112. format_object<int64_t> formatHex(int64_t Value) const;
  113. format_object<uint64_t> formatHex(uint64_t Value) const;
  114. };
  115. /// Map from opcode to pattern list by binary search.
  116. struct PatternsForOpcode {
  117. uint32_t Opcode;
  118. uint16_t PatternStart;
  119. uint16_t NumPatterns;
  120. };
  121. /// Data for each alias pattern. Includes feature bits, string, number of
  122. /// operands, and a variadic list of conditions to check.
  123. struct AliasPattern {
  124. uint32_t AsmStrOffset;
  125. uint32_t AliasCondStart;
  126. uint8_t NumOperands;
  127. uint8_t NumConds;
  128. };
  129. struct AliasPatternCond {
  130. enum CondKind : uint8_t {
  131. K_Feature, // Match only if a feature is enabled.
  132. K_NegFeature, // Match only if a feature is disabled.
  133. K_OrFeature, // Match only if one of a set of features is enabled.
  134. K_OrNegFeature, // Match only if one of a set of features is disabled.
  135. K_EndOrFeatures, // Note end of list of K_Or(Neg)?Features.
  136. K_Ignore, // Match any operand.
  137. K_Reg, // Match a specific register.
  138. K_TiedReg, // Match another already matched register.
  139. K_Imm, // Match a specific immediate.
  140. K_RegClass, // Match registers in a class.
  141. K_Custom, // Call custom matcher by index.
  142. };
  143. CondKind Kind;
  144. uint32_t Value;
  145. };
  146. /// Tablegenerated data structures needed to match alias patterns.
  147. struct AliasMatchingData {
  148. ArrayRef<PatternsForOpcode> OpToPatterns;
  149. ArrayRef<AliasPattern> Patterns;
  150. ArrayRef<AliasPatternCond> PatternConds;
  151. StringRef AsmStrings;
  152. bool (*ValidateMCOperand)(const MCOperand &MCOp, const MCSubtargetInfo &STI,
  153. unsigned PredicateIndex);
  154. };
  155. } // end namespace llvm
  156. #endif // LLVM_MC_MCINSTPRINTER_H
  157. #ifdef __GNUC__
  158. #pragma GCC diagnostic pop
  159. #endif