MCObjectStreamer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //===- MCObjectStreamer.h - MCStreamer Object File Interface ----*- 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_MCOBJECTSTREAMER_H
  10. #define LLVM_MC_MCOBJECTSTREAMER_H
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "llvm/MC/MCAssembler.h"
  13. #include "llvm/MC/MCSection.h"
  14. #include "llvm/MC/MCStreamer.h"
  15. namespace llvm {
  16. class MCAssembler;
  17. class MCCodeEmitter;
  18. class MCSubtargetInfo;
  19. class MCExpr;
  20. class MCFragment;
  21. class MCDataFragment;
  22. class MCAsmBackend;
  23. class raw_ostream;
  24. class raw_pwrite_stream;
  25. /// \brief Streaming object file generation interface.
  26. ///
  27. /// This class provides an implementation of the MCStreamer interface which is
  28. /// suitable for use with the assembler backend. Specific object file formats
  29. /// are expected to subclass this interface to implement directives specific
  30. /// to that file format or custom semantics expected by the object writer
  31. /// implementation.
  32. class MCObjectStreamer : public MCStreamer {
  33. MCAssembler *Assembler;
  34. MCSection::iterator CurInsertionPoint;
  35. bool EmitEHFrame;
  36. bool EmitDebugFrame;
  37. SmallVector<MCSymbol *, 2> PendingLabels;
  38. virtual void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo&) = 0;
  39. void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
  40. void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
  41. protected:
  42. MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB, raw_pwrite_stream &OS,
  43. MCCodeEmitter *Emitter);
  44. ~MCObjectStreamer() override;
  45. public:
  46. /// state management
  47. void reset() override;
  48. /// Object streamers require the integrated assembler.
  49. bool isIntegratedAssemblerRequired() const override { return true; }
  50. void EmitFrames(MCAsmBackend *MAB);
  51. void EmitCFISections(bool EH, bool Debug) override;
  52. protected:
  53. MCFragment *getCurrentFragment() const;
  54. void insert(MCFragment *F) {
  55. flushPendingLabels(F);
  56. MCSection *CurSection = getCurrentSectionOnly();
  57. CurSection->getFragmentList().insert(CurInsertionPoint, F);
  58. F->setParent(CurSection);
  59. }
  60. /// Get a data fragment to write into, creating a new one if the current
  61. /// fragment is not a data fragment.
  62. MCDataFragment *getOrCreateDataFragment();
  63. bool changeSectionImpl(MCSection *Section, const MCExpr *Subsection);
  64. /// If any labels have been emitted but not assigned fragments, ensure that
  65. /// they get assigned, either to F if possible or to a new data fragment.
  66. /// Optionally, it is also possible to provide an offset \p FOffset, which
  67. /// will be used as a symbol offset within the fragment.
  68. void flushPendingLabels(MCFragment *F, uint64_t FOffset = 0);
  69. public:
  70. void visitUsedSymbol(const MCSymbol &Sym) override;
  71. MCAssembler &getAssembler() { return *Assembler; }
  72. /// \name MCStreamer Interface
  73. /// @{
  74. void EmitLabel(MCSymbol *Symbol) override;
  75. void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
  76. void EmitValueImpl(const MCExpr *Value, unsigned Size,
  77. const SMLoc &Loc = SMLoc()) override;
  78. void EmitULEB128Value(const MCExpr *Value) override;
  79. void EmitSLEB128Value(const MCExpr *Value) override;
  80. void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
  81. void ChangeSection(MCSection *Section, const MCExpr *Subsection) override;
  82. void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo& STI) override;
  83. /// \brief Emit an instruction to a special fragment, because this instruction
  84. /// can change its size during relaxation.
  85. virtual void EmitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
  86. void EmitBundleAlignMode(unsigned AlignPow2) override;
  87. void EmitBundleLock(bool AlignToEnd) override;
  88. void EmitBundleUnlock() override;
  89. void EmitBytes(StringRef Data) override;
  90. void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
  91. unsigned ValueSize = 1,
  92. unsigned MaxBytesToEmit = 0) override;
  93. void EmitCodeAlignment(unsigned ByteAlignment,
  94. unsigned MaxBytesToEmit = 0) override;
  95. bool EmitValueToOffset(const MCExpr *Offset, unsigned char Value) override;
  96. void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
  97. unsigned Column, unsigned Flags,
  98. unsigned Isa, unsigned Discriminator,
  99. StringRef FileName) override;
  100. void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
  101. const MCSymbol *Label,
  102. unsigned PointerSize);
  103. void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
  104. const MCSymbol *Label);
  105. void EmitGPRel32Value(const MCExpr *Value) override;
  106. void EmitGPRel64Value(const MCExpr *Value) override;
  107. void EmitFill(uint64_t NumBytes, uint8_t FillValue) override;
  108. void EmitZeros(uint64_t NumBytes) override;
  109. void FinishImpl() override;
  110. /// Emit the absolute difference between two symbols if possible.
  111. ///
  112. /// Emit the absolute difference between \c Hi and \c Lo, as long as we can
  113. /// compute it. Currently, that requires that both symbols are in the same
  114. /// data fragment. Otherwise, do nothing and return \c false.
  115. ///
  116. /// \pre Offset of \c Hi is greater than the offset \c Lo.
  117. void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
  118. unsigned Size) override;
  119. bool mayHaveInstructions(MCSection &Sec) const override;
  120. };
  121. } // end namespace llvm
  122. #endif