MCAsmBackend.h 8.7 KB

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