MCAsmBackend.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/MC/MCAsmBackend.h - MC Asm Backend ------------------*- 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_MCASMBACKEND_H
  14. #define LLVM_MC_MCASMBACKEND_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/MC/MCDirectives.h"
  17. #include "llvm/MC/MCFixup.h"
  18. #include "llvm/Support/Endian.h"
  19. #include <cstdint>
  20. namespace llvm {
  21. class MCAlignFragment;
  22. class MCDwarfCallFrameFragment;
  23. class MCDwarfLineAddrFragment;
  24. class MCFragment;
  25. class MCRelaxableFragment;
  26. class MCSymbol;
  27. class MCAsmLayout;
  28. class MCAssembler;
  29. class MCCFIInstruction;
  30. struct MCFixupKindInfo;
  31. class MCInst;
  32. class MCObjectStreamer;
  33. class MCObjectTargetWriter;
  34. class MCObjectWriter;
  35. class MCSubtargetInfo;
  36. class MCValue;
  37. class raw_pwrite_stream;
  38. class StringRef;
  39. class raw_ostream;
  40. /// Generic interface to target specific assembler backends.
  41. class MCAsmBackend {
  42. protected: // Can only create subclasses.
  43. MCAsmBackend(support::endianness Endian);
  44. public:
  45. MCAsmBackend(const MCAsmBackend &) = delete;
  46. MCAsmBackend &operator=(const MCAsmBackend &) = delete;
  47. virtual ~MCAsmBackend();
  48. const support::endianness Endian;
  49. /// Return true if this target might automatically pad instructions and thus
  50. /// need to emit padding enable/disable directives around sensative code.
  51. virtual bool allowAutoPadding() const { return false; }
  52. /// Return true if this target allows an unrelaxable instruction to be
  53. /// emitted into RelaxableFragment and then we can increase its size in a
  54. /// tricky way for optimization.
  55. virtual bool allowEnhancedRelaxation() const { return false; }
  56. /// Give the target a chance to manipulate state related to instruction
  57. /// alignment (e.g. padding for optimization), instruction relaxablility, etc.
  58. /// before and after actually emitting the instruction.
  59. virtual void emitInstructionBegin(MCObjectStreamer &OS, const MCInst &Inst,
  60. const MCSubtargetInfo &STI) {}
  61. virtual void emitInstructionEnd(MCObjectStreamer &OS, const MCInst &Inst) {}
  62. /// lifetime management
  63. virtual void reset() {}
  64. /// Create a new MCObjectWriter instance for use by the assembler backend to
  65. /// emit the final object file.
  66. std::unique_ptr<MCObjectWriter>
  67. createObjectWriter(raw_pwrite_stream &OS) const;
  68. /// Create an MCObjectWriter that writes two object files: a .o file which is
  69. /// linked into the final program and a .dwo file which is used by debuggers.
  70. /// This function is only supported with ELF targets.
  71. std::unique_ptr<MCObjectWriter>
  72. createDwoObjectWriter(raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS) const;
  73. virtual std::unique_ptr<MCObjectTargetWriter>
  74. createObjectTargetWriter() const = 0;
  75. /// \name Target Fixup Interfaces
  76. /// @{
  77. /// Get the number of target specific fixup kinds.
  78. virtual unsigned getNumFixupKinds() const = 0;
  79. /// Map a relocation name used in .reloc to a fixup kind.
  80. virtual std::optional<MCFixupKind> getFixupKind(StringRef Name) const;
  81. /// Get information on a fixup kind.
  82. virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;
  83. /// Hook to check if a relocation is needed for some target specific reason.
  84. virtual bool shouldForceRelocation(const MCAssembler &Asm,
  85. const MCFixup &Fixup,
  86. const MCValue &Target) {
  87. return false;
  88. }
  89. /// Hook to check if extra nop bytes must be inserted for alignment directive.
  90. /// For some targets this may be necessary in order to support linker
  91. /// relaxation. The number of bytes to insert are returned in Size.
  92. virtual bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF,
  93. unsigned &Size) {
  94. return false;
  95. }
  96. /// Hook which indicates if the target requires a fixup to be generated when
  97. /// handling an align directive in an executable section
  98. virtual bool shouldInsertFixupForCodeAlign(MCAssembler &Asm,
  99. const MCAsmLayout &Layout,
  100. MCAlignFragment &AF) {
  101. return false;
  102. }
  103. virtual bool evaluateTargetFixup(const MCAssembler &Asm,
  104. const MCAsmLayout &Layout,
  105. const MCFixup &Fixup, const MCFragment *DF,
  106. const MCValue &Target, uint64_t &Value,
  107. bool &WasForced) {
  108. llvm_unreachable("Need to implement hook if target has custom fixups");
  109. }
  110. /// Apply the \p Value for given \p Fixup into the provided data fragment, at
  111. /// the offset specified by the fixup and following the fixup kind as
  112. /// appropriate. Errors (such as an out of range fixup value) should be
  113. /// reported via \p Ctx.
  114. /// The \p STI is present only for fragments of type MCRelaxableFragment and
  115. /// MCDataFragment with hasInstructions() == true.
  116. virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
  117. const MCValue &Target, MutableArrayRef<char> Data,
  118. uint64_t Value, bool IsResolved,
  119. const MCSubtargetInfo *STI) const = 0;
  120. /// @}
  121. /// \name Target Relaxation Interfaces
  122. /// @{
  123. /// Check whether the given instruction may need relaxation.
  124. ///
  125. /// \param Inst - The instruction to test.
  126. /// \param STI - The MCSubtargetInfo in effect when the instruction was
  127. /// encoded.
  128. virtual bool mayNeedRelaxation(const MCInst &Inst,
  129. const MCSubtargetInfo &STI) const {
  130. return false;
  131. }
  132. /// Target specific predicate for whether a given fixup requires the
  133. /// associated instruction to be relaxed.
  134. virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
  135. uint64_t Value,
  136. const MCRelaxableFragment *DF,
  137. const MCAsmLayout &Layout,
  138. const bool WasForced) const;
  139. /// Simple predicate for targets where !Resolved implies requiring relaxation
  140. virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
  141. const MCRelaxableFragment *DF,
  142. const MCAsmLayout &Layout) const = 0;
  143. /// Relax the instruction in the given fragment to the next wider instruction.
  144. ///
  145. /// \param [out] Inst The instruction to relax, which is also the relaxed
  146. /// instruction.
  147. /// \param STI the subtarget information for the associated instruction.
  148. virtual void relaxInstruction(MCInst &Inst,
  149. const MCSubtargetInfo &STI) const {};
  150. virtual bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF,
  151. MCAsmLayout &Layout, bool &WasRelaxed) const {
  152. return false;
  153. }
  154. virtual bool relaxDwarfCFA(MCDwarfCallFrameFragment &DF, MCAsmLayout &Layout,
  155. bool &WasRelaxed) const {
  156. return false;
  157. }
  158. /// @}
  159. /// Returns the minimum size of a nop in bytes on this target. The assembler
  160. /// will use this to emit excess padding in situations where the padding
  161. /// required for simple alignment would be less than the minimum nop size.
  162. ///
  163. virtual unsigned getMinimumNopSize() const { return 1; }
  164. /// Returns the maximum size of a nop in bytes on this target.
  165. ///
  166. virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const {
  167. return 0;
  168. }
  169. /// Write an (optimal) nop sequence of Count bytes to the given output. If the
  170. /// target cannot generate such a sequence, it should return an error.
  171. ///
  172. /// \return - True on success.
  173. virtual bool writeNopData(raw_ostream &OS, uint64_t Count,
  174. const MCSubtargetInfo *STI) const = 0;
  175. /// Give backend an opportunity to finish layout after relaxation
  176. virtual void finishLayout(MCAssembler const &Asm,
  177. MCAsmLayout &Layout) const {}
  178. /// Handle any target-specific assembler flags. By default, do nothing.
  179. virtual void handleAssemblerFlag(MCAssemblerFlag Flag) {}
  180. /// Generate the compact unwind encoding for the CFI instructions.
  181. virtual uint32_t
  182. generateCompactUnwindEncoding(ArrayRef<MCCFIInstruction>) const {
  183. return 0;
  184. }
  185. /// Check whether a given symbol has been flagged with MICROMIPS flag.
  186. virtual bool isMicroMips(const MCSymbol *Sym) const {
  187. return false;
  188. }
  189. };
  190. } // end namespace llvm
  191. #endif // LLVM_MC_MCASMBACKEND_H
  192. #ifdef __GNUC__
  193. #pragma GCC diagnostic pop
  194. #endif