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 void generateCompactUnwindEncodings();
83 MCSymbol *emitCFILabel() override;
84 void emitCFISections(bool EH, bool Debug, bool SFrame) override;
85
86public:
87 void visitUsedSymbol(const MCSymbol &Sym) override;
88
89 MCAssembler &getAssembler() { return *Assembler; }
90 MCAssembler *getAssemblerPtr() override;
91 /// \name MCStreamer Interface
92 /// @{
93
95 return reinterpret_cast<uint8_t *>(CurFrag + 1) + CurFrag->getFixedSize();
96 }
97 MCFragment *allocFragSpace(size_t Headroom);
98 // Add a new fragment to the current section without a variable-size tail.
99 void newFragment();
100
101 // Add a new special fragment to the current section and start a new empty
102 // fragment.
103 template <typename FT, typename... Args>
104 FT *newSpecialFragment(Args &&...args) {
105 auto *F = new (SpecialFragAllocator.Allocate(sizeof(FT), alignof(FT)))
106 FT(std::forward<Args>(args)...);
107 addSpecialFragment(F);
108 return F;
109 }
110
111 void ensureHeadroom(size_t Headroom);
112 void appendContents(ArrayRef<char> Contents);
113 void appendContents(size_t Num, uint8_t Elt);
114 // Add a fixup to the current fragment. Call ensureHeadroom beforehand to
115 // ensure the fixup and appended content apply to the same fragment.
116 void addFixup(const MCExpr *Value, MCFixupKind Kind);
117
118 void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
119 virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F,
121 void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
122 void emitConditionalAssignment(MCSymbol *Symbol,
123 const MCExpr *Value) override;
124 void emitValueImpl(const MCExpr *Value, unsigned Size,
125 SMLoc Loc = SMLoc()) override;
126 void emitULEB128Value(const MCExpr *Value) override;
127 void emitSLEB128Value(const MCExpr *Value) override;
128 void emitWeakReference(MCSymbol *Alias, const MCSymbol *Target) override;
129 void changeSection(MCSection *Section, uint32_t Subsection = 0) override;
130 void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
131
132 /// Emit an instruction to a special fragment, because this instruction
133 /// can change its size during relaxation.
134 void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
135
136 void emitBytes(StringRef Data) override;
137 void emitValueToAlignment(Align Alignment, int64_t Fill = 0,
138 uint8_t FillLen = 1,
139 unsigned MaxBytesToEmit = 0) override;
140 void emitCodeAlignment(Align ByteAlignment, const MCSubtargetInfo *STI,
141 unsigned MaxBytesToEmit = 0) override;
142 void emitPrefAlign(Align Alignment) override;
143 void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
144 SMLoc Loc) override;
145 void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column,
146 unsigned Flags, unsigned Isa,
147 unsigned Discriminator, StringRef FileName,
148 StringRef Comment = {}) override;
149 void emitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
150 const MCSymbol *Label,
151 unsigned PointerSize) override;
152 void emitDwarfLineEndEntry(MCSection *Section, MCSymbol *LastLabel,
153 MCSymbol *EndLabel = nullptr) override;
154 void emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
155 const MCSymbol *Label, SMLoc Loc);
156 void emitSFrameCalculateFuncOffset(const MCSymbol *FunCabsel,
157 const MCSymbol *FREBegin,
158 MCFragment *FDEFrag, SMLoc Loc);
159 void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line,
160 unsigned Column, bool PrologueEnd, bool IsStmt,
161 StringRef FileName, SMLoc Loc) override;
162 void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin,
163 const MCSymbol *End) override;
164 void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
165 unsigned SourceFileId,
166 unsigned SourceLineNum,
167 const MCSymbol *FnStartSym,
168 const MCSymbol *FnEndSym) override;
169 void emitCVDefRangeDirective(
170 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
171 StringRef FixedSizePortion) override;
172 void emitCVStringTableDirective() override;
173 void emitCVFileChecksumsDirective() override;
174 void emitCVFileChecksumOffsetDirective(unsigned FileNo) override;
175 void emitRelocDirective(const MCExpr &Offset, StringRef Name,
176 const MCExpr *Expr, SMLoc Loc = {}) override;
177 using MCStreamer::emitFill;
178 void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
179 SMLoc Loc = SMLoc()) override;
180 void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
181 SMLoc Loc = SMLoc()) override;
182 void emitNops(int64_t NumBytes, int64_t ControlledNopLength, SMLoc Loc,
183 const MCSubtargetInfo &STI) override;
184 void emitFileDirective(StringRef Filename) override;
185 void emitFileDirective(StringRef Filename, StringRef CompilerVersion,
186 StringRef TimeStamp, StringRef Description) override;
187
188 void emitAddrsig() override;
189 void emitAddrsigSym(const MCSymbol *Sym) override;
190
191 void finishImpl() override;
192
193 /// Emit the absolute difference between two symbols if possible.
194 ///
195 /// Emit the absolute difference between \c Hi and \c Lo, as long as we can
196 /// compute it. Currently, that requires that both symbols are in the same
197 /// data fragment and that the target has not specified that diff expressions
198 /// require relocations to be emitted. Otherwise, do nothing and return
199 /// \c false.
200 ///
201 /// \pre Offset of \c Hi is greater than the offset \c Lo.
202 void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
203 unsigned Size) override;
204
205 void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
206 const MCSymbol *Lo) override;
207
208 bool mayHaveInstructions(MCSection &Sec) const override;
209
210 /// Emits pending conditional assignments that depend on \p Symbol
211 /// being emitted.
212 void emitPendingAssignments(MCSymbol *Symbol);
213};
214
215} // end namespace llvm
216
217#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
#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.
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:516
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
@ Debug
Register 'use' is for debugging purpose.
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