MCAsmBackend.h 8.2 KB

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