LLVM 24.0.0git
MCObjectStreamer.h
Go to the documentation of this file.
1//===- MCObjectStreamer.h - MCStreamer Object File Interface ----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_MC_MCOBJECTSTREAMER_H
10#define LLVM_MC_MCOBJECTSTREAMER_H
11
12#include "llvm/ADT/SetVector.h"
14#include "llvm/MC/MCFixup.h"
15#include "llvm/MC/MCSection.h"
16#include "llvm/MC/MCStreamer.h"
18
19namespace llvm {
20class MCContext;
21class MCInst;
22class MCObjectWriter;
23class MCSymbol;
24struct MCDwarfFrameInfo;
25class MCAssembler;
26class MCCodeEmitter;
27class MCSubtargetInfo;
28class MCExpr;
29class MCAsmBackend;
30class raw_ostream;
32
33/// Streaming object file generation interface.
34///
35/// This class provides an implementation of the MCStreamer interface which is
36/// suitable for use with the assembler backend. Specific object file formats
37/// are expected to subclass this interface to implement directives specific
38/// to that file format or custom semantics expected by the object writer
39/// implementation.
41 std::unique_ptr<MCAssembler> Assembler;
42 bool EmitEHFrame;
43 bool EmitDebugFrame;
44 bool EmitSFrame;
45
46 struct PendingAssignment {
47 MCSymbol *Symbol;
48 const MCExpr *Value;
49 };
50
51 /// A list of conditional assignments we may need to emit if the target
52 /// symbol is later emitted.
54 pendingAssignments;
55
57 // Available bytes in the current block for trailing data or new fragments.
58 size_t FragSpace = 0;
59 // Used to allocate special fragments that do not use MCFragment's fixed-size
60 // part.
61 BumpPtrAllocator SpecialFragAllocator;
62
63 void addSpecialFragment(MCFragment *F);
64 void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &);
65 void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
66 void emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
67
68protected:
69 // Within .bundle_lock/.bundle_unlock. A section cannot be switched while a
70 // group is open, so no per-section state is needed.
71 bool BundleLocked = false;
72
73 MCObjectStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
74 std::unique_ptr<MCObjectWriter> OW,
75 std::unique_ptr<MCCodeEmitter> Emitter);
77
78public:
79 /// state management
80 void reset() override;
81
82 /// Object streamers require the integrated assembler.
83 bool isIntegratedAssemblerRequired() const override { return true; }
84
85 void emitFrames();
86 void generateCompactUnwindEncodings();
87 MCSymbol *emitCFILabel() override;
88 void emitCFISections(bool EH, bool Debug, bool SFrame) override;
89
90public:
91 void visitUsedSymbol(const MCSymbol &Sym) override;
92
93 MCAssembler &getAssembler() { return *Assembler; }
94 MCAssembler *getAssemblerPtr() override;
95
96 bool isBundleLocked() const { return BundleLocked; }
97
98 /// \name MCStreamer Interface
99 /// @{
100
102 return reinterpret_cast<uint8_t *>(CurFrag + 1) + CurFrag->getFixedSize();
103 }
104 MCFragment *allocFragSpace(size_t Headroom);
105 // Add a new fragment to the current section without a variable-size tail.
106 void newFragment();
107
108 // Add a new special fragment to the current section and start a new empty
109 // fragment.
110 template <typename FT, typename... Args>
111 FT *newSpecialFragment(Args &&...args) {
112 auto *F = new (SpecialFragAllocator.Allocate(sizeof(FT), alignof(FT)))
113 FT(std::forward<Args>(args)...);
114 addSpecialFragment(F);
115 return F;
116 }
117
118 void ensureHeadroom(size_t Headroom);
119 void appendContents(ArrayRef<char> Contents);
120 void appendContents(size_t Num, uint8_t Elt);
121 // Add a fixup to the current fragment. Call ensureHeadroom beforehand to
122 // ensure the fixup and appended content apply to the same fragment.
123 void addFixup(const MCExpr *Value, MCFixupKind Kind);
124
125 void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
126 virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F,
128 void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
129 void emitConditionalAssignment(MCSymbol *Symbol,
130 const MCExpr *Value) override;
131 void emitValueImpl(const MCExpr *Value, unsigned Size,
132 SMLoc Loc = SMLoc()) override;
133 void emitULEB128Value(const MCExpr *Value) override;
134 void emitSLEB128Value(const MCExpr *Value) override;
135 void emitWeakReference(MCSymbol *Alias, const MCSymbol *Target) override;
136 void changeSection(MCSection *Section, uint32_t Subsection = 0) override;
137 void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
138
139 /// Emit an instruction to a special fragment, because this instruction
140 /// can change its size during relaxation.
141 void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
142
143 void emitBytes(StringRef Data) override;
144 void emitValueToAlignment(Align Alignment, int64_t Fill = 0,
145 uint8_t FillLen = 1,
146 unsigned MaxBytesToEmit = 0) override;
147 void emitCodeAlignment(Align ByteAlignment, const MCSubtargetInfo &STI,
148 unsigned MaxBytesToEmit = 0) override;
149 void emitPrefAlign(Align Alignment, const MCSymbol &End, bool EmitNops,
150 uint8_t Fill, const MCSubtargetInfo &STI) override;
151 void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
152 SMLoc Loc) override;
153 void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column,
154 unsigned Flags, unsigned Isa,
155 unsigned Discriminator, StringRef FileName,
156 StringRef Comment = {}) override;
157 void emitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
158 const MCSymbol *Label,
159 unsigned PointerSize) override;
160 void emitDwarfLineEndEntry(MCSection *Section, MCSymbol *LastLabel,
161 MCSymbol *EndLabel = nullptr) override;
162 void emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
163 const MCSymbol *Label, SMLoc Loc);
164 void emitSFrameCalculateFuncOffset(const MCSymbol *FunCabsel,
165 const MCSymbol *FREBegin,
166 MCFragment *FDEFrag, SMLoc Loc);
167 void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line,
168 unsigned Column, bool PrologueEnd, bool IsStmt,
169 StringRef FileName, SMLoc Loc) override;
170 void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin,
171 const MCSymbol *End) override;
172 void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
173 unsigned SourceFileId,
174 unsigned SourceLineNum,
175 const MCSymbol *FnStartSym,
176 const MCSymbol *FnEndSym) override;
177 void emitCVDefRangeDirective(
178 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
179 StringRef FixedSizePortion) override;
180 void emitCVStringTableDirective() override;
181 void emitCVFileChecksumsDirective() override;
182 void emitCVFileChecksumOffsetDirective(unsigned FileNo) override;
183 void emitRelocDirective(const MCExpr &Offset, StringRef Name,
184 const MCExpr *Expr, SMLoc Loc = {}) override;
185 using MCStreamer::emitFill;
186 void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
187 SMLoc Loc = SMLoc()) override;
188 void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
189 SMLoc Loc = SMLoc()) override;
190 void emitNops(int64_t NumBytes, int64_t ControlledNopLength, SMLoc Loc,
191 const MCSubtargetInfo &STI) override;
192 void emitFileDirective(StringRef Filename) override;
193 void emitFileDirective(StringRef Filename, StringRef CompilerVersion,
194 StringRef TimeStamp, StringRef Description) override;
195
196 void emitAddrsig() override;
197 void emitAddrsigSym(const MCSymbol *Sym) override;
198
199 void finishImpl() override;
200
201 /// Emit the absolute difference between two symbols if possible.
202 ///
203 /// Emit the absolute difference between \c Hi and \c Lo, as long as we can
204 /// compute it. Currently, that requires that both symbols are in the same
205 /// data fragment and that the target has not specified that diff expressions
206 /// require relocations to be emitted. Otherwise, do nothing and return
207 /// \c false.
208 ///
209 /// \pre Offset of \c Hi is greater than the offset \c Lo.
210 void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
211 unsigned Size) override;
212
213 void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
214 const MCSymbol *Lo) override;
215
216 bool mayHaveInstructions(MCSection &Sec) const override;
217
218 /// Emits pending conditional assignments that depend on \p Symbol
219 /// being emitted.
220 void emitPendingAssignments(MCSymbol *Symbol);
221};
222
223} // end namespace llvm
224
225#endif
static void addFixup(SmallVectorImpl< MCFixup > &Fixups, uint32_t Offset, const MCExpr *Value, uint16_t Kind, bool PCRel=false)
#define LLVM_ABI
Definition Compiler.h:215
dxil DXContainer Global Emitter
#define F(x, y, z)
Definition MD5.cpp:54
nvptx lower args
static constexpr StringLiteral Filename
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Generic interface to target specific assembler backends.
MCCodeEmitter - Generic instruction encoding interface.
Context object for machine code objects.
Definition MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
void reset() override
state management
FT * newSpecialFragment(Args &&...args)
MCAssembler & getAssembler()
bool isIntegratedAssemblerRequired() const override
Object streamers require the integrated assembler.
MCObjectStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
~MCObjectStreamer() override
uint8_t * getCurFragEnd() const
Defines the object file and target independent interfaces used by the assembler backend to write nati...
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:580
MCStreamer(MCContext &Ctx)
MCFragment * CurFrag
Definition MCStreamer.h:271
Generic base class for all target subtargets.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Represents a location in source code.
Definition SMLoc.h:22
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
An abstract base class for streams implementations that also support a pwrite operation.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
@ Debug
Register 'use' is for debugging purpose.
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:390
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39