Have getCurrentSectionData return a MCSection.
[oota-llvm.git] / include / llvm / MC / MCObjectStreamer.h
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/MCSection.h"
16 #include "llvm/MC/MCStreamer.h"
17
18 namespace llvm {
19 class MCAssembler;
20 class MCCodeEmitter;
21 class MCSectionData;
22 class MCSubtargetInfo;
23 class MCExpr;
24 class MCFragment;
25 class MCDataFragment;
26 class MCAsmBackend;
27 class raw_ostream;
28 class raw_pwrite_stream;
29
30 /// \brief 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   MCAssembler *Assembler;
39   MCSection *CurSectionData;
40   MCSectionData::iterator CurInsertionPoint;
41   bool EmitEHFrame;
42   bool EmitDebugFrame;
43   SmallVector<MCSymbolData *, 2> PendingLabels;
44
45   virtual void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo&) = 0;
46   void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
47   void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
48
49 protected:
50   MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB, raw_pwrite_stream &OS,
51                    MCCodeEmitter *Emitter);
52   ~MCObjectStreamer() override;
53
54 public:
55   /// state management
56   void reset() override;
57
58   /// Object streamers require the integrated assembler.
59   bool isIntegratedAssemblerRequired() const override { return true; }
60
61   MCSymbolData &getOrCreateSymbolData(const MCSymbol *Symbol) {
62     return getAssembler().getOrCreateSymbolData(*Symbol);
63   }
64   void EmitFrames(MCAsmBackend *MAB);
65   void EmitCFISections(bool EH, bool Debug) override;
66
67 protected:
68   MCSection *getCurrentSectionData() const { return CurSectionData; }
69
70   MCFragment *getCurrentFragment() const;
71
72   void insert(MCFragment *F) {
73     flushPendingLabels(F);
74     CurSectionData->getFragmentList().insert(CurInsertionPoint, F);
75     F->setParent(CurSectionData);
76   }
77
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();
81
82   bool changeSectionImpl(MCSection *Section, const MCExpr *Subsection);
83
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);
89
90 public:
91   void visitUsedSymbol(const MCSymbol &Sym) override;
92
93   MCAssembler &getAssembler() { return *Assembler; }
94
95   /// \name MCStreamer Interface
96   /// @{
97
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(MCSection *Section, const MCExpr *Subsection) override;
106   void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo& STI) override;
107
108   /// \brief Emit an instruction to a special fragment, because this instruction
109   /// can change its size during relaxation.
110   virtual void EmitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
111
112   void EmitBundleAlignMode(unsigned AlignPow2) override;
113   void EmitBundleLock(bool AlignToEnd) override;
114   void EmitBundleUnlock() override;
115   void EmitBytes(StringRef Data) override;
116   void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
117                             unsigned ValueSize = 1,
118                             unsigned MaxBytesToEmit = 0) override;
119   void EmitCodeAlignment(unsigned ByteAlignment,
120                          unsigned MaxBytesToEmit = 0) override;
121   bool EmitValueToOffset(const MCExpr *Offset, unsigned char Value) override;
122   void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
123                              unsigned Column, unsigned Flags,
124                              unsigned Isa, unsigned Discriminator,
125                              StringRef FileName) override;
126   void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
127                                 const MCSymbol *Label,
128                                 unsigned PointerSize);
129   void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
130                                  const MCSymbol *Label);
131   void EmitGPRel32Value(const MCExpr *Value) override;
132   void EmitGPRel64Value(const MCExpr *Value) override;
133   void EmitFill(uint64_t NumBytes, uint8_t FillValue) override;
134   void EmitZeros(uint64_t NumBytes) override;
135   void FinishImpl() override;
136
137   /// Emit the absolute difference between two symbols if possible.
138   ///
139   /// Emit the absolute difference between \c Hi and \c Lo, as long as we can
140   /// compute it.  Currently, that requires that both symbols are in the same
141   /// data fragment.  Otherwise, do nothing and return \c false.
142   ///
143   /// \pre Offset of \c Hi is greater than the offset \c Lo.
144   /// \return true on success.
145   bool emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
146                               unsigned Size) override;
147
148   bool mayHaveInstructions(MCSection &Sec) const override;
149 };
150
151 } // end namespace llvm
152
153 #endif