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