blob: 8e9b4ac56320fc791da9774512df88dfebbb3337 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- 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
18namespace llvm {
19class MCAssembler;
20class MCCodeEmitter;
21class MCSubtargetInfo;
22class MCExpr;
23class MCFragment;
24class MCDataFragment;
25class MCAsmBackend;
26class raw_ostream;
27class 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.
36class MCObjectStreamer : public MCStreamer {
37 std::unique_ptr<MCObjectWriter> ObjectWriter;
38 std::unique_ptr<MCAsmBackend> TAB;
39 std::unique_ptr<MCCodeEmitter> Emitter;
40 std::unique_ptr<MCAssembler> Assembler;
41 MCSection::iterator CurInsertionPoint;
42 bool EmitEHFrame;
43 bool EmitDebugFrame;
44 SmallVector<MCSymbol *, 2> PendingLabels;
45
46 virtual void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo&) = 0;
47 void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
48 void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
49 MCSymbol *EmitCFILabel() override;
50 void EmitInstructionImpl(const MCInst &Inst, const MCSubtargetInfo &STI);
51
52protected:
53 MCObjectStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
54 raw_pwrite_stream &OS,
55 std::unique_ptr<MCCodeEmitter> Emitter);
56 ~MCObjectStreamer();
57
58public:
59 /// state management
60 void reset() override;
61
62 /// Object streamers require the integrated assembler.
63 bool isIntegratedAssemblerRequired() const override { return true; }
64
65 void EmitFrames(MCAsmBackend *MAB);
66 void EmitCFISections(bool EH, bool Debug) override;
67
68 MCFragment *getCurrentFragment() const;
69
70 void insert(MCFragment *F) {
71 flushPendingLabels(F);
72 MCSection *CurSection = getCurrentSectionOnly();
73 CurSection->getFragmentList().insert(CurInsertionPoint, F);
74 F->setParent(CurSection);
75 }
76
77 /// Get a data fragment to write into, creating a new one if the current
78 /// fragment is not a data fragment.
79 MCDataFragment *getOrCreateDataFragment();
80 MCPaddingFragment *getOrCreatePaddingFragment();
81
82protected:
83 bool changeSectionImpl(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
91public:
92 void visitUsedSymbol(const MCSymbol &Sym) override;
93
94 MCAssembler &getAssembler() { return *Assembler; }
95
96 /// \name MCStreamer Interface
97 /// @{
98
99 void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
100 virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc, MCFragment *F);
101 void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
102 void EmitValueImpl(const MCExpr *Value, unsigned Size,
103 SMLoc Loc = SMLoc()) override;
104 void EmitULEB128Value(const MCExpr *Value) override;
105 void EmitSLEB128Value(const MCExpr *Value) override;
106 void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
107 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override;
108 void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
109 bool = false) override;
110
111 /// \brief Emit an instruction to a special fragment, because this instruction
112 /// can change its size during relaxation.
113 virtual void EmitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
114
115 void EmitBundleAlignMode(unsigned AlignPow2) override;
116 void EmitBundleLock(bool AlignToEnd) override;
117 void EmitBundleUnlock() override;
118 void EmitBytes(StringRef Data) override;
119 void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
120 unsigned ValueSize = 1,
121 unsigned MaxBytesToEmit = 0) override;
122 void EmitCodeAlignment(unsigned ByteAlignment,
123 unsigned MaxBytesToEmit = 0) override;
124 void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
125 SMLoc Loc) override;
126 void
127 EmitCodePaddingBasicBlockStart(const MCCodePaddingContext &Context) override;
128 void
129 EmitCodePaddingBasicBlockEnd(const MCCodePaddingContext &Context) override;
130 void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
131 unsigned Column, unsigned Flags,
132 unsigned Isa, unsigned Discriminator,
133 StringRef FileName) override;
134 void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
135 const MCSymbol *Label,
136 unsigned PointerSize);
137 void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
138 const MCSymbol *Label);
139 void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line,
140 unsigned Column, bool PrologueEnd, bool IsStmt,
141 StringRef FileName, SMLoc Loc) override;
142 void EmitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin,
143 const MCSymbol *End) override;
144 void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
145 unsigned SourceFileId,
146 unsigned SourceLineNum,
147 const MCSymbol *FnStartSym,
148 const MCSymbol *FnEndSym) override;
149 void EmitCVDefRangeDirective(
150 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
151 StringRef FixedSizePortion) override;
152 void EmitCVStringTableDirective() override;
153 void EmitCVFileChecksumsDirective() override;
154 void EmitCVFileChecksumOffsetDirective(unsigned FileNo) override;
155 void EmitDTPRel32Value(const MCExpr *Value) override;
156 void EmitDTPRel64Value(const MCExpr *Value) override;
157 void EmitTPRel32Value(const MCExpr *Value) override;
158 void EmitTPRel64Value(const MCExpr *Value) override;
159 void EmitGPRel32Value(const MCExpr *Value) override;
160 void EmitGPRel64Value(const MCExpr *Value) override;
161 bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
162 const MCExpr *Expr, SMLoc Loc) override;
163 using MCStreamer::emitFill;
164 void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
165 SMLoc Loc = SMLoc()) override;
166 void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
167 SMLoc Loc = SMLoc()) override;
168 void EmitFileDirective(StringRef Filename) override;
169
170 void FinishImpl() override;
171
172 /// Emit the absolute difference between two symbols if possible.
173 ///
174 /// Emit the absolute difference between \c Hi and \c Lo, as long as we can
175 /// compute it. Currently, that requires that both symbols are in the same
176 /// data fragment. Otherwise, do nothing and return \c false.
177 ///
178 /// \pre Offset of \c Hi is greater than the offset \c Lo.
179 void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
180 unsigned Size) override;
181
182 void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
183 const MCSymbol *Lo) override;
184
185 bool mayHaveInstructions(MCSection &Sec) const override;
186};
187
188} // end namespace llvm
189
190#endif