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/MCStreamer.h"
21 class MCSubtargetInfo;
28 /// \brief Streaming object file generation interface.
30 /// This class provides an implementation of the MCStreamer interface which is
31 /// suitable for use with the assembler backend. Specific object file formats
32 /// are expected to subclass this interface to implement directives specific
33 /// to that file format or custom semantics expected by the object writer
35 class MCObjectStreamer : public MCStreamer {
36 MCAssembler *Assembler;
37 MCSectionData *CurSectionData;
38 MCSectionData::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_ostream &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 MCSectionData *getCurrentSectionData() const {
67 return CurSectionData;
70 MCFragment *getCurrentFragment() const;
72 void insert(MCFragment *F) {
73 flushPendingLabels(F);
74 CurSectionData->getFragmentList().insert(CurInsertionPoint, F);
75 F->setParent(CurSectionData);
78 /// Get a data fragment to write into, creating a new one if the current
79 /// fragment is not a data fragment.
80 MCDataFragment *getOrCreateDataFragment();
82 bool changeSectionImpl(const MCSection *Section, const MCExpr *Subsection);
84 /// If any labels have been emitted but not assigned fragments, ensure that
85 /// they get assigned, either to F if possible or to a new data fragment.
86 /// Optionally, it is also possible to provide an offset \p FOffset, which
87 /// will be used as a symbol offset within the fragment.
88 void flushPendingLabels(MCFragment *F, uint64_t FOffset = 0);
91 void visitUsedSymbol(const MCSymbol &Sym) override;
93 MCAssembler &getAssembler() { return *Assembler; }
95 /// @name MCStreamer Interface
98 void EmitLabel(MCSymbol *Symbol) override;
99 void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
100 void EmitValueImpl(const MCExpr *Value, unsigned Size,
101 const SMLoc &Loc = SMLoc()) override;
102 void EmitULEB128Value(const MCExpr *Value) override;
103 void EmitSLEB128Value(const MCExpr *Value) override;
104 void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
105 void ChangeSection(const MCSection *Section,
106 const MCExpr *Subsection) override;
107 void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo& STI) override;
109 /// \brief Emit an instruction to a special fragment, because this instruction
110 /// can change its size during relaxation.
111 virtual void EmitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
113 void EmitBundleAlignMode(unsigned AlignPow2) override;
114 void EmitBundleLock(bool AlignToEnd) override;
115 void EmitBundleUnlock() override;
116 void EmitBytes(StringRef Data) override;
117 void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
118 unsigned ValueSize = 1,
119 unsigned MaxBytesToEmit = 0) override;
120 void EmitCodeAlignment(unsigned ByteAlignment,
121 unsigned MaxBytesToEmit = 0) override;
122 bool EmitValueToOffset(const MCExpr *Offset, unsigned char Value) override;
123 void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
124 unsigned Column, unsigned Flags,
125 unsigned Isa, unsigned Discriminator,
126 StringRef FileName) override;
127 void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
128 const MCSymbol *Label,
129 unsigned PointerSize);
130 void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
131 const MCSymbol *Label);
132 void EmitGPRel32Value(const MCExpr *Value) override;
133 void EmitGPRel64Value(const MCExpr *Value) override;
134 void EmitFill(uint64_t NumBytes, uint8_t FillValue) override;
135 void EmitZeros(uint64_t NumBytes) override;
136 void FinishImpl() override;
138 bool mayHaveInstructions() const override {
139 return getCurrentSectionData()->hasInstructions();
143 } // end namespace llvm