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;
30
31/// Streaming object file generation interface.
32///
33/// This class provides an implementation of the MCStreamer interface which is
34/// suitable for use with the assembler backend. Specific object file formats
35/// are expected to subclass this interface to implement directives specific
36/// to that file format or custom semantics expected by the object writer
37/// implementation.
39 std::unique_ptr<MCAssembler> Assembler;
40 bool EmitEHFrame;
41 bool EmitDebugFrame;
42 bool EmitSFrame;
43
44 struct PendingAssignment {
45 MCSymbol *Symbol;
46 const MCExpr *Value;
47 };
48
49 /// A list of conditional assignments we may need to emit if the target
50 /// symbol is later emitted.
52 pendingAssignments;
53
55 // Available bytes in the current block for trailing data or new fragments.
56 size_t FragSpace = 0;
57 // Used to allocate special fragments that do not use MCFragment's fixed-size
58 // part.
59 BumpPtrAllocator SpecialFragAllocator;
60
61 void addSpecialFragment(MCFragment *F);
62 void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &);
63 void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
64 void emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
65
66protected:
67 // Within .bundle_lock/.bundle_unlock. A section cannot be switched while a
68 // group is open, so no per-section state is needed.
69 bool BundleLocked = false;
70
71 MCObjectStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
72 std::unique_ptr<MCObjectWriter> OW,
73 std::unique_ptr<MCCodeEmitter> Emitter);
75
76public:
77 /// state management
78 void reset() override;
79
80 /// Object streamers require the integrated assembler.
81 bool isIntegratedAssemblerRequired() const override { return true; }
82
83 void emitFrames();
84 void generateCompactUnwindEncodings();
85 MCSymbol *emitCFILabel() override;
86 void emitCFISections(bool EH, bool Debug, bool SFrame) override;
87
88public:
89 void visitUsedSymbol(const MCSymbol &Sym) override;
90
91 MCAssembler &getAssembler() { return *Assembler; }
92 MCAssembler *getAssemblerPtr() override;
93
94 bool isBundleLocked() const { return BundleLocked; }
95
96 /// \name MCStreamer Interface
97 /// @{
98
100 return reinterpret_cast<uint8_t *>(CurFrag + 1) + CurFrag->getFixedSize();
101 }
102 MCFragment *allocFragSpace(size_t Headroom);
103 // Add a new fragment to the current section without a variable-size tail.
104 void newFragment();
105
106 // Add a new special fragment to the current section and start a new empty
107 // fragment.
108 template <typename FT, typename... Args>
109 FT *newSpecialFragment(Args &&...args) {
110 auto *F = new (SpecialFragAllocator.Allocate(sizeof(FT), alignof(FT)))
111 FT(std::forward<Args>(args)...);
112 addSpecialFragment(F);
113 return F;
114 }
115
116 void ensureHeadroom(size_t Headroom);
117 void appendContents(ArrayRef<char> Contents);
118 void appendContents(size_t Num, uint8_t Elt);
119 // Add a fixup to the current fragment. Call ensureHeadroom beforehand to
120 // ensure the fixup and appended content apply to the same fragment.
121 void addFixup(const MCExpr *Value, MCFixupKind Kind);
122
123 void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
124 virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F,
126 void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
127 void emitConditionalAssignment(MCSymbol *Symbol,
128 const MCExpr *Value) override;
129 void emitValueImpl(const MCExpr *Value, unsigned Size,
130 SMLoc Loc = SMLoc()) override;
131 void emitULEB128Value(const MCExpr *Value) override;
132 void emitSLEB128Value(const MCExpr *Value) override;
133 void emitWeakReference(MCSymbol *Alias, const MCSymbol *Target) override;
134 void changeSection(MCSection *Section, uint32_t Subsection = 0) override;
135 void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
136
137 /// Emit an instruction to a special fragment, because this instruction
138 /// can change its size during relaxation.
139 void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
140
141 void emitBytes(StringRef Data) override;
142 void emitValueToAlignment(Align Alignment, int64_t Fill = 0,
143 uint8_t FillLen = 1,
144 unsigned MaxBytesToEmit = 0) override;
145 void emitCodeAlignment(Align ByteAlignment, const MCSubtargetInfo &STI,
146 unsigned MaxBytesToEmit = 0) override;
147 void emitPrefAlign(Align Alignment, const MCSymbol &End, bool EmitNops,
148 uint8_t Fill, const MCSubtargetInfo &STI) override;
149 void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
150 SMLoc Loc) override;
151 void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column,
152 unsigned Flags, unsigned Isa,
153 unsigned Discriminator, StringRef FileName,
154 StringRef Comment = {}) override;
155 void emitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
156 const MCSymbol *Label,
157 unsigned PointerSize) override;
158 void emitDwarfLineEndEntry(MCSection *Section, MCSymbol *LastLabel,
159 MCSymbol *EndLabel = nullptr) override;
160 void emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
161 const MCSymbol *Label, SMLoc Loc);
162 void emitSFrameCalculateFuncOffset(const MCSymbol *FunCabsel,
163 const MCSymbol *FREBegin,
164 MCFragment *FDEFrag, SMLoc Loc);
165 void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line,
166 unsigned Column, bool PrologueEnd, bool IsStmt,
167 StringRef FileName, SMLoc Loc) override;
168 void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin,
169 const MCSymbol *End) override;
170 void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
171 unsigned SourceFileId,
172 unsigned SourceLineNum,
173 const MCSymbol *FnStartSym,
174 const MCSymbol *FnEndSym) override;
175 void emitCVDefRangeDirective(
176 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
177 StringRef FixedSizePortion) override;
178 void emitCVStringTableDirective() override;
179 void emitCVFileChecksumsDirective() override;
180 void emitCVFileChecksumOffsetDirective(unsigned FileNo) override;
181 void emitRelocDirective(const MCExpr &Offset, StringRef Name,
182 const MCExpr *Expr, SMLoc Loc = {}) override;
183 using MCStreamer::emitFill;
184 void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
185 SMLoc Loc = SMLoc()) override;
186 void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
187 SMLoc Loc = SMLoc()) override;
188 void emitNops(int64_t NumBytes, int64_t ControlledNopLength, SMLoc Loc,
189 const MCSubtargetInfo &STI) override;
190 void emitFileDirective(StringRef Filename) override;
191 void emitFileDirective(StringRef Filename, StringRef CompilerVersion,
192 StringRef TimeStamp, StringRef Description) override;
193
194 void emitAddrsig() override;
195 void emitAddrsigSym(const MCSymbol *Sym) override;
196
197 void finishImpl() override;
198
199 /// Emit the absolute difference between two symbols if possible.
200 ///
201 /// Emit the absolute difference between \c Hi and \c Lo, as long as we can
202 /// compute it. Currently, that requires that both symbols are in the same
203 /// data fragment and that the target has not specified that diff expressions
204 /// require relocations to be emitted. Otherwise, do nothing and return
205 /// \c false.
206 ///
207 /// \pre Offset of \c Hi is greater than the offset \c Lo.
208 void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
209 unsigned Size) override;
210
211 void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
212 const MCSymbol *Lo) override;
213
214 bool mayHaveInstructions(MCSection &Sec) const override;
215
216 /// Emits pending conditional assignments that depend on \p Symbol
217 /// being emitted.
218 void emitPendingAssignments(MCSymbol *Symbol);
219};
220
221} // end namespace llvm
222
223#endif
static void addFixup(SmallVectorImpl< MCFixup > &Fixups, uint32_t Offset, const MCExpr *Value, uint16_t Kind, bool PCRel=false)
uint64_t uint64_t
#define LLVM_ABI
Definition Compiler.h:211
dxil container DXContainer Global Emitter
#define F(x, y, z)
Definition MD5.cpp:54
nvptx lower args
static constexpr StringLiteral Filename
MachineInstr *& Lo
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:84
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:31
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:187
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:577
MCStreamer(MCContext &Ctx)
MCFragment * CurFrag
Definition MCStreamer.h:270
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 is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
@ 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:391
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39