LLVM 23.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 MCObjectStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
70 std::unique_ptr<MCObjectWriter> OW,
71 std::unique_ptr<MCCodeEmitter> Emitter);
73
74public:
75 /// state management
76 void reset() override;
77
78 /// Object streamers require the integrated assembler.
79 bool isIntegratedAssemblerRequired() const override { return true; }
80
81 void emitFrames();
82 MCSymbol *emitCFILabel() override;
83 void emitCFISections(bool EH, bool Debug, bool SFrame) override;
84
85public:
86 void visitUsedSymbol(const MCSymbol &Sym) override;
87
88 MCAssembler &getAssembler() { return *Assembler; }
89 MCAssembler *getAssemblerPtr() override;
90 /// \name MCStreamer Interface
91 /// @{
92
94 return reinterpret_cast<uint8_t *>(CurFrag + 1) + CurFrag->getFixedSize();
95 }
96 MCFragment *allocFragSpace(size_t Headroom);
97 // Add a new fragment to the current section without a variable-size tail.
98 void newFragment();
99
100 // Add a new special fragment to the current section and start a new empty
101 // fragment.
102 template <typename FT, typename... Args>
103 FT *newSpecialFragment(Args &&...args) {
104 auto *F = new (SpecialFragAllocator.Allocate(sizeof(FT), alignof(FT)))
105 FT(std::forward<Args>(args)...);
106 addSpecialFragment(F);
107 return F;
108 }
109
110 void ensureHeadroom(size_t Headroom);
111 void appendContents(ArrayRef<char> Contents);
112 void appendContents(size_t Num, uint8_t Elt);
113 // Add a fixup to the current fragment. Call ensureHeadroom beforehand to
114 // ensure the fixup and appended content apply to the same fragment.
115 void addFixup(const MCExpr *Value, MCFixupKind Kind);
116
117 void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
118 virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F,
120 void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
121 void emitConditionalAssignment(MCSymbol *Symbol,
122 const MCExpr *Value) override;
123 void emitValueImpl(const MCExpr *Value, unsigned Size,
124 SMLoc Loc = SMLoc()) override;
125 void emitULEB128Value(const MCExpr *Value) override;
126 void emitSLEB128Value(const MCExpr *Value) override;
127 void emitWeakReference(MCSymbol *Alias, const MCSymbol *Target) override;
128 void changeSection(MCSection *Section, uint32_t Subsection = 0) override;
129 void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
130
131 /// Emit an instruction to a special fragment, because this instruction
132 /// can change its size during relaxation.
133 void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
134
135 void emitBytes(StringRef Data) override;
136 void emitValueToAlignment(Align Alignment, int64_t Fill = 0,
137 uint8_t FillLen = 1,
138 unsigned MaxBytesToEmit = 0) override;
139 void emitCodeAlignment(Align ByteAlignment, const MCSubtargetInfo *STI,
140 unsigned MaxBytesToEmit = 0) override;
141 void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
142 SMLoc Loc) override;
143 void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column,
144 unsigned Flags, unsigned Isa,
145 unsigned Discriminator, StringRef FileName,
146 StringRef Comment = {}) override;
147 void emitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
148 const MCSymbol *Label,
149 unsigned PointerSize) override;
150 void emitDwarfLineEndEntry(MCSection *Section, MCSymbol *LastLabel,
151 MCSymbol *EndLabel = nullptr) override;
152 void emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
153 const MCSymbol *Label, SMLoc Loc);
154 void emitSFrameCalculateFuncOffset(const MCSymbol *FunCabsel,
155 const MCSymbol *FREBegin,
156 MCFragment *FDEFrag, SMLoc Loc);
157 void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line,
158 unsigned Column, bool PrologueEnd, bool IsStmt,
159 StringRef FileName, SMLoc Loc) override;
160 void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin,
161 const MCSymbol *End) override;
162 void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
163 unsigned SourceFileId,
164 unsigned SourceLineNum,
165 const MCSymbol *FnStartSym,
166 const MCSymbol *FnEndSym) override;
167 void emitCVDefRangeDirective(
168 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
169 StringRef FixedSizePortion) override;
170 void emitCVStringTableDirective() override;
171 void emitCVFileChecksumsDirective() override;
172 void emitCVFileChecksumOffsetDirective(unsigned FileNo) override;
173 void emitRelocDirective(const MCExpr &Offset, StringRef Name,
174 const MCExpr *Expr, SMLoc Loc = {}) override;
175 using MCStreamer::emitFill;
176 void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
177 SMLoc Loc = SMLoc()) override;
178 void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
179 SMLoc Loc = SMLoc()) override;
180 void emitNops(int64_t NumBytes, int64_t ControlledNopLength, SMLoc Loc,
181 const MCSubtargetInfo &STI) override;
182 void emitFileDirective(StringRef Filename) override;
183 void emitFileDirective(StringRef Filename, StringRef CompilerVersion,
184 StringRef TimeStamp, StringRef Description) override;
185
186 void emitAddrsig() override;
187 void emitAddrsigSym(const MCSymbol *Sym) override;
188
189 void finishImpl() override;
190
191 /// Emit the absolute difference between two symbols if possible.
192 ///
193 /// Emit the absolute difference between \c Hi and \c Lo, as long as we can
194 /// compute it. Currently, that requires that both symbols are in the same
195 /// data fragment and that the target has not specified that diff expressions
196 /// require relocations to be emitted. Otherwise, do nothing and return
197 /// \c false.
198 ///
199 /// \pre Offset of \c Hi is greater than the offset \c Lo.
200 void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
201 unsigned Size) override;
202
203 void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
204 const MCSymbol *Lo) override;
205
206 bool mayHaveInstructions(MCSection &Sec) const override;
207
208 /// Emits pending conditional assignments that depend on \p Symbol
209 /// being emitted.
210 void emitPendingAssignments(MCSymbol *Symbol);
211};
212
213} // end namespace llvm
214
215#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:213
dxil DXContainer Global Emitter
static ManagedStatic< cl::opt< bool, true >, CreateDebug > Debug
Definition Debug.cpp:147
#define F(x, y, z)
Definition MD5.cpp:54
nvptx lower args
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
ArrayRef - 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:517
MCStreamer(MCContext &Ctx)
MCFragment * CurFrag
Definition MCStreamer.h:267
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.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39