MCObjectStreamer.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MCObjectStreamer.h - MCStreamer Object File Interface ----*- 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_MCOBJECTSTREAMER_H
  14. #define LLVM_MC_MCOBJECTSTREAMER_H
  15. #include "llvm/ADT/SetVector.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/MC/MCFixup.h"
  18. #include "llvm/MC/MCFragment.h"
  19. #include "llvm/MC/MCSection.h"
  20. #include "llvm/MC/MCStreamer.h"
  21. namespace llvm {
  22. class MCContext;
  23. class MCInst;
  24. class MCObjectWriter;
  25. class MCSymbol;
  26. struct MCDwarfFrameInfo;
  27. class MCAssembler;
  28. class MCCodeEmitter;
  29. class MCSubtargetInfo;
  30. class MCExpr;
  31. class MCFragment;
  32. class MCDataFragment;
  33. class MCAsmBackend;
  34. class raw_ostream;
  35. class raw_pwrite_stream;
  36. /// Streaming object file generation interface.
  37. ///
  38. /// This class provides an implementation of the MCStreamer interface which is
  39. /// suitable for use with the assembler backend. Specific object file formats
  40. /// are expected to subclass this interface to implement directives specific
  41. /// to that file format or custom semantics expected by the object writer
  42. /// implementation.
  43. class MCObjectStreamer : public MCStreamer {
  44. std::unique_ptr<MCAssembler> Assembler;
  45. MCSection::iterator CurInsertionPoint;
  46. bool EmitEHFrame;
  47. bool EmitDebugFrame;
  48. SmallVector<MCSymbol *, 2> PendingLabels;
  49. SmallSetVector<MCSection *, 4> PendingLabelSections;
  50. unsigned CurSubsectionIdx;
  51. struct PendingMCFixup {
  52. const MCSymbol *Sym;
  53. MCFixup Fixup;
  54. MCDataFragment *DF;
  55. PendingMCFixup(const MCSymbol *McSym, MCDataFragment *F, MCFixup McFixup)
  56. : Sym(McSym), Fixup(McFixup), DF(F) {}
  57. };
  58. SmallVector<PendingMCFixup, 2> PendingFixups;
  59. struct PendingAssignment {
  60. MCSymbol *Symbol;
  61. const MCExpr *Value;
  62. };
  63. /// A list of conditional assignments we may need to emit if the target
  64. /// symbol is later emitted.
  65. DenseMap<const MCSymbol *, SmallVector<PendingAssignment, 1>>
  66. pendingAssignments;
  67. virtual void emitInstToData(const MCInst &Inst, const MCSubtargetInfo&) = 0;
  68. void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
  69. void emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
  70. MCSymbol *emitCFILabel() override;
  71. void emitInstructionImpl(const MCInst &Inst, const MCSubtargetInfo &STI);
  72. void resolvePendingFixups();
  73. protected:
  74. MCObjectStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
  75. std::unique_ptr<MCObjectWriter> OW,
  76. std::unique_ptr<MCCodeEmitter> Emitter);
  77. ~MCObjectStreamer();
  78. public:
  79. /// state management
  80. void reset() override;
  81. /// Object streamers require the integrated assembler.
  82. bool isIntegratedAssemblerRequired() const override { return true; }
  83. void emitFrames(MCAsmBackend *MAB);
  84. void emitCFISections(bool EH, bool Debug) override;
  85. MCFragment *getCurrentFragment() const;
  86. void insert(MCFragment *F) {
  87. flushPendingLabels(F);
  88. MCSection *CurSection = getCurrentSectionOnly();
  89. CurSection->getFragmentList().insert(CurInsertionPoint, F);
  90. F->setParent(CurSection);
  91. }
  92. /// Get a data fragment to write into, creating a new one if the current
  93. /// fragment is not a data fragment.
  94. /// Optionally a \p STI can be passed in so that a new fragment is created
  95. /// if the Subtarget differs from the current fragment.
  96. MCDataFragment *getOrCreateDataFragment(const MCSubtargetInfo* STI = nullptr);
  97. protected:
  98. bool changeSectionImpl(MCSection *Section, const MCExpr *Subsection);
  99. /// Assign a label to the current Section and Subsection even though a
  100. /// fragment is not yet present. Use flushPendingLabels(F) to associate
  101. /// a fragment with this label.
  102. void addPendingLabel(MCSymbol* label);
  103. /// If any labels have been emitted but not assigned fragments in the current
  104. /// Section and Subsection, ensure that they get assigned, either to fragment
  105. /// F if possible or to a new data fragment. Optionally, one can provide an
  106. /// offset \p FOffset as a symbol offset within the fragment.
  107. void flushPendingLabels(MCFragment *F, uint64_t FOffset = 0);
  108. public:
  109. void visitUsedSymbol(const MCSymbol &Sym) override;
  110. /// Create a data fragment for any pending labels across all Sections
  111. /// and Subsections.
  112. void flushPendingLabels();
  113. MCAssembler &getAssembler() { return *Assembler; }
  114. MCAssembler *getAssemblerPtr() override;
  115. /// \name MCStreamer Interface
  116. /// @{
  117. void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
  118. virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment *F,
  119. uint64_t Offset);
  120. void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
  121. void emitConditionalAssignment(MCSymbol *Symbol,
  122. const MCExpr *Value) override;
  123. void emitValueImpl(const MCExpr *Value, unsigned Size,
  124. SMLoc Loc = SMLoc()) override;
  125. void emitULEB128Value(const MCExpr *Value) override;
  126. void emitSLEB128Value(const MCExpr *Value) override;
  127. void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
  128. void changeSection(MCSection *Section, const MCExpr *Subsection) override;
  129. void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
  130. /// Emit an instruction to a special fragment, because this instruction
  131. /// can change its size during relaxation.
  132. virtual void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
  133. void emitBundleAlignMode(Align Alignment) override;
  134. void emitBundleLock(bool AlignToEnd) override;
  135. void emitBundleUnlock() override;
  136. void emitBytes(StringRef Data) override;
  137. void emitValueToAlignment(Align Alignment, int64_t Value = 0,
  138. unsigned ValueSize = 1,
  139. unsigned MaxBytesToEmit = 0) override;
  140. void emitCodeAlignment(Align ByteAlignment, const MCSubtargetInfo *STI,
  141. unsigned MaxBytesToEmit = 0) override;
  142. void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
  143. SMLoc Loc) override;
  144. void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column,
  145. unsigned Flags, unsigned Isa,
  146. unsigned Discriminator,
  147. StringRef FileName) override;
  148. void emitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
  149. const MCSymbol *Label,
  150. unsigned PointerSize) override;
  151. void emitDwarfLineEndEntry(MCSection *Section, MCSymbol *LastLabel) override;
  152. void emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
  153. const MCSymbol *Label);
  154. void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line,
  155. unsigned Column, bool PrologueEnd, bool IsStmt,
  156. StringRef FileName, SMLoc Loc) override;
  157. void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin,
  158. const MCSymbol *End) override;
  159. void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
  160. unsigned SourceFileId,
  161. unsigned SourceLineNum,
  162. const MCSymbol *FnStartSym,
  163. const MCSymbol *FnEndSym) override;
  164. void emitCVDefRangeDirective(
  165. ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
  166. StringRef FixedSizePortion) override;
  167. void emitCVStringTableDirective() override;
  168. void emitCVFileChecksumsDirective() override;
  169. void emitCVFileChecksumOffsetDirective(unsigned FileNo) override;
  170. void emitDTPRel32Value(const MCExpr *Value) override;
  171. void emitDTPRel64Value(const MCExpr *Value) override;
  172. void emitTPRel32Value(const MCExpr *Value) override;
  173. void emitTPRel64Value(const MCExpr *Value) override;
  174. void emitGPRel32Value(const MCExpr *Value) override;
  175. void emitGPRel64Value(const MCExpr *Value) override;
  176. std::optional<std::pair<bool, std::string>>
  177. emitRelocDirective(const MCExpr &Offset, StringRef Name, const MCExpr *Expr,
  178. SMLoc Loc, const MCSubtargetInfo &STI) override;
  179. using MCStreamer::emitFill;
  180. void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
  181. SMLoc Loc = SMLoc()) override;
  182. void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
  183. SMLoc Loc = SMLoc()) override;
  184. void emitNops(int64_t NumBytes, int64_t ControlledNopLength, SMLoc Loc,
  185. const MCSubtargetInfo &STI) override;
  186. void emitFileDirective(StringRef Filename) override;
  187. void emitFileDirective(StringRef Filename, StringRef CompilerVerion,
  188. StringRef TimeStamp, StringRef Description) override;
  189. void emitAddrsig() override;
  190. void emitAddrsigSym(const MCSymbol *Sym) override;
  191. void finishImpl() override;
  192. /// Emit the absolute difference between two symbols if possible.
  193. ///
  194. /// Emit the absolute difference between \c Hi and \c Lo, as long as we can
  195. /// compute it. Currently, that requires that both symbols are in the same
  196. /// data fragment and that the target has not specified that diff expressions
  197. /// require relocations to be emitted. Otherwise, do nothing and return
  198. /// \c false.
  199. ///
  200. /// \pre Offset of \c Hi is greater than the offset \c Lo.
  201. void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
  202. unsigned Size) override;
  203. void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
  204. const MCSymbol *Lo) override;
  205. bool mayHaveInstructions(MCSection &Sec) const override;
  206. /// Emits pending conditional assignments that depend on \p Symbol
  207. /// being emitted.
  208. void emitPendingAssignments(MCSymbol *Symbol);
  209. };
  210. } // end namespace llvm
  211. #endif
  212. #ifdef __GNUC__
  213. #pragma GCC diagnostic pop
  214. #endif