1 //===- MCObjectStreamer.h - MCStreamer Object File Interface ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCOBJECTSTREAMER_H
11 #define LLVM_MC_MCOBJECTSTREAMER_H
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCSection.h"
16 #include "llvm/MC/MCStreamer.h"
21 class MCSubtargetInfo;
27 class raw_pwrite_stream;
29 /// \brief Streaming object file generation interface.
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
36 class MCObjectStreamer : public MCStreamer {
37 MCAssembler *Assembler;
38 MCSection::iterator CurInsertionPoint;
41 SmallVector<MCSymbolData *, 2> PendingLabels;
43 virtual void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo&) = 0;
44 void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
45 void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
48 MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB, raw_pwrite_stream &OS,
49 MCCodeEmitter *Emitter);
50 ~MCObjectStreamer() override;
54 void reset() override;
56 /// Object streamers require the integrated assembler.
57 bool isIntegratedAssemblerRequired() const override { return true; }
59 MCSymbolData &getOrCreateSymbolData(const MCSymbol *Symbol) {
60 return getAssembler().getOrCreateSymbolData(*Symbol);
62 void EmitFrames(MCAsmBackend *MAB);
63 void EmitCFISections(bool EH, bool Debug) override;
66 MCFragment *getCurrentFragment() const;
68 void insert(MCFragment *F) {
69 flushPendingLabels(F);
70 MCSection *CurSection = getCurrentSectionOnly();
71 CurSection->getFragmentList().insert(CurInsertionPoint, F);
72 F->setParent(CurSection);
75 /// Get a data fragment to write into, creating a new one if the current
76 /// fragment is not a data fragment.
77 MCDataFragment *getOrCreateDataFragment();
79 bool changeSectionImpl(MCSection *Section, const MCExpr *Subsection);
81 /// If any labels have been emitted but not assigned fragments, ensure that
82 /// they get assigned, either to F if possible or to a new data fragment.
83 /// Optionally, it is also possible to provide an offset \p FOffset, which
84 /// will be used as a symbol offset within the fragment.
85 void flushPendingLabels(MCFragment *F, uint64_t FOffset = 0);
88 void visitUsedSymbol(const MCSymbol &Sym) override;
90 MCAssembler &getAssembler() { return *Assembler; }
92 /// \name MCStreamer Interface
95 void EmitLabel(MCSymbol *Symbol) override;
96 void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
97 void EmitValueImpl(const MCExpr *Value, unsigned Size,
98 const SMLoc &Loc = SMLoc()) override;
99 void EmitULEB128Value(const MCExpr *Value) override;
100 void EmitSLEB128Value(const MCExpr *Value) override;
101 void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
102 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override;
103 void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo& STI) override;
105 /// \brief Emit an instruction to a special fragment, because this instruction
106 /// can change its size during relaxation.
107 virtual void EmitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
109 void EmitBundleAlignMode(unsigned AlignPow2) override;
110 void EmitBundleLock(bool AlignToEnd) override;
111 void EmitBundleUnlock() override;
112 void EmitBytes(StringRef Data) override;
113 void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
114 unsigned ValueSize = 1,
115 unsigned MaxBytesToEmit = 0) override;
116 void EmitCodeAlignment(unsigned ByteAlignment,
117 unsigned MaxBytesToEmit = 0) override;
118 bool EmitValueToOffset(const MCExpr *Offset, unsigned char Value) override;
119 void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
120 unsigned Column, unsigned Flags,
121 unsigned Isa, unsigned Discriminator,
122 StringRef FileName) override;
123 void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
124 const MCSymbol *Label,
125 unsigned PointerSize);
126 void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
127 const MCSymbol *Label);
128 void EmitGPRel32Value(const MCExpr *Value) override;
129 void EmitGPRel64Value(const MCExpr *Value) override;
130 void EmitFill(uint64_t NumBytes, uint8_t FillValue) override;
131 void EmitZeros(uint64_t NumBytes) override;
132 void FinishImpl() override;
134 /// Emit the absolute difference between two symbols if possible.
136 /// Emit the absolute difference between \c Hi and \c Lo, as long as we can
137 /// compute it. Currently, that requires that both symbols are in the same
138 /// data fragment. Otherwise, do nothing and return \c false.
140 /// \pre Offset of \c Hi is greater than the offset \c Lo.
141 /// \return true on success.
142 bool emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
143 unsigned Size) override;
145 bool mayHaveInstructions(MCSection &Sec) const override;
148 } // end namespace llvm