MCObjectStreamer.h 9.9 KB

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