MCAsmBackend.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //===-- llvm/MC/MCAsmBackend.h - MC Asm Backend -----------------*- 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_MCASMBACKEND_H
  10. #define LLVM_MC_MCASMBACKEND_H
  11. #include "llvm/ADT/ArrayRef.h"
  12. #include "llvm/MC/MCDirectives.h"
  13. #include "llvm/MC/MCDwarf.h"
  14. #include "llvm/MC/MCFixup.h"
  15. #include "llvm/Support/DataTypes.h"
  16. #include "llvm/Support/ErrorHandling.h"
  17. namespace llvm {
  18. class MCAsmLayout;
  19. class MCAssembler;
  20. class MCELFObjectTargetWriter;
  21. struct MCFixupKindInfo;
  22. class MCFragment;
  23. class MCInst;
  24. class MCRelaxableFragment;
  25. class MCObjectWriter;
  26. class MCSection;
  27. class MCValue;
  28. class raw_ostream;
  29. /// Generic interface to target specific assembler backends.
  30. class MCAsmBackend {
  31. MCAsmBackend(const MCAsmBackend &) = delete;
  32. void operator=(const MCAsmBackend &) = delete;
  33. protected: // Can only create subclasses.
  34. MCAsmBackend();
  35. unsigned HasDataInCodeSupport : 1;
  36. public:
  37. virtual ~MCAsmBackend();
  38. /// lifetime management
  39. virtual void reset() {}
  40. /// Create a new MCObjectWriter instance for use by the assembler backend to
  41. /// emit the final object file.
  42. virtual MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const = 0;
  43. /// Create a new ELFObjectTargetWriter to enable non-standard
  44. /// ELFObjectWriters.
  45. virtual MCELFObjectTargetWriter *createELFObjectTargetWriter() const {
  46. llvm_unreachable("createELFObjectTargetWriter is not supported by asm "
  47. "backend");
  48. }
  49. /// Check whether this target implements data-in-code markers. If not, data
  50. /// region directives will be ignored.
  51. bool hasDataInCodeSupport() const { return HasDataInCodeSupport; }
  52. /// \name Target Fixup Interfaces
  53. /// @{
  54. /// Get the number of target specific fixup kinds.
  55. virtual unsigned getNumFixupKinds() const = 0;
  56. /// Get information on a fixup kind.
  57. virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;
  58. /// Target hook to adjust the literal value of a fixup if necessary.
  59. /// IsResolved signals whether the caller believes a relocation is needed; the
  60. /// target can modify the value. The default does nothing.
  61. virtual void processFixupValue(const MCAssembler &Asm,
  62. const MCAsmLayout &Layout,
  63. const MCFixup &Fixup, const MCFragment *DF,
  64. const MCValue &Target, uint64_t &Value,
  65. bool &IsResolved) {}
  66. /// Apply the \p Value for given \p Fixup into the provided data fragment, at
  67. /// the offset specified by the fixup and following the fixup kind as
  68. /// appropriate.
  69. virtual void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
  70. uint64_t Value, bool IsPCRel) const = 0;
  71. /// @}
  72. /// \name Target Relaxation Interfaces
  73. /// @{
  74. /// Check whether the given instruction may need relaxation.
  75. ///
  76. /// \param Inst - The instruction to test.
  77. virtual bool mayNeedRelaxation(const MCInst &Inst) const = 0;
  78. /// Target specific predicate for whether a given fixup requires the
  79. /// associated instruction to be relaxed.
  80. virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
  81. uint64_t Value,
  82. const MCRelaxableFragment *DF,
  83. const MCAsmLayout &Layout) const;
  84. /// Simple predicate for targets where !Resolved implies requiring relaxation
  85. virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
  86. const MCRelaxableFragment *DF,
  87. const MCAsmLayout &Layout) const = 0;
  88. /// Relax the instruction in the given fragment to the next wider instruction.
  89. ///
  90. /// \param Inst The instruction to relax, which may be the same as the
  91. /// output.
  92. /// \param [out] Res On return, the relaxed instruction.
  93. virtual void relaxInstruction(const MCInst &Inst, MCInst &Res) const = 0;
  94. /// @}
  95. /// Returns the minimum size of a nop in bytes on this target. The assembler
  96. /// will use this to emit excess padding in situations where the padding
  97. /// required for simple alignment would be less than the minimum nop size.
  98. ///
  99. virtual unsigned getMinimumNopSize() const { return 1; }
  100. /// Write an (optimal) nop sequence of Count bytes to the given output. If the
  101. /// target cannot generate such a sequence, it should return an error.
  102. ///
  103. /// \return - True on success.
  104. virtual bool writeNopData(uint64_t Count, MCObjectWriter *OW) const = 0;
  105. /// Handle any target-specific assembler flags. By default, do nothing.
  106. virtual void handleAssemblerFlag(MCAssemblerFlag Flag) {}
  107. /// \brief Generate the compact unwind encoding for the CFI instructions.
  108. virtual uint32_t
  109. generateCompactUnwindEncoding(ArrayRef<MCCFIInstruction>) const {
  110. return 0;
  111. }
  112. };
  113. } // End llvm namespace
  114. #endif