LLVM 22.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"
17
18namespace llvm {
19class MCContext;
20class MCInst;
21class MCObjectWriter;
22class MCSymbol;
23struct MCDwarfFrameInfo;
24class MCAssembler;
25class MCCodeEmitter;
26class MCSubtargetInfo;
27class MCExpr;
28class MCAsmBackend;
29class raw_ostream;
30class raw_pwrite_stream;
31
32/// Streaming object file generation interface.
33///
34/// This class provides an implementation of the MCStreamer interface which is
35/// suitable for use with the assembler backend. Specific object file formats
36/// are expected to subclass this interface to implement directives specific
37/// to that file format or custom semantics expected by the object writer
38/// implementation.
40 std::unique_ptr<MCAssembler> Assembler;
41 bool EmitEHFrame;
42 bool EmitDebugFrame;
43 bool EmitSFrame;
44
45 struct PendingAssignment {
46 MCSymbol *Symbol;
47 const MCExpr *Value;
48 };
49
50 /// A list of conditional assignments we may need to emit if the target
51 /// symbol is later emitted.
53 pendingAssignments;
54
56 // Available bytes in the current block for trailing data or new fragments.
57 size_t FragSpace = 0;
58 // Used to allocate special fragments that do not use MCFragment's fixed-size
59 // part.
60 BumpPtrAllocator SpecialFragAllocator;
61
62 void addSpecialFragment(MCFragment *F);
63 void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &);
64 void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
65 void emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
66
67protected:
68 MCObjectStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
69 std::unique_ptr<MCObjectWriter> OW,
70 std::unique_ptr<MCCodeEmitter> Emitter);
72
73public:
74 /// state management
75 void reset() override;
76
77 /// Object streamers require the integrated assembler.
78 bool isIntegratedAssemblerRequired() const override { return true; }
79
80 void emitFrames(MCAsmBackend *MAB);
81 MCSymbol *emitCFILabel() override;
82 void emitCFISections(bool EH, bool Debug, bool SFrame) override;
83
84public:
85 void visitUsedSymbol(const MCSymbol &Sym) override;
86
87 MCAssembler &getAssembler() { return *Assembler; }
88 MCAssembler *getAssemblerPtr() override;
89 /// \name MCStreamer Interface
90 /// @{
91
93 return reinterpret_cast<uint8_t *>(CurFrag + 1) + CurFrag->getFixedSize();
94 }
95 MCFragment *allocFragSpace(size_t Headroom);
96 // Add a new fragment to the current section without a variable-size tail.
97 void newFragment();
98
99 // Add a new special fragment to the current section and start a new empty
100 // fragment.
101 template <typename FT, typename... Args>
102 FT *newSpecialFragment(Args &&...args) {
103 auto *F = new (SpecialFragAllocator.Allocate(sizeof(FT), alignof(FT)))
104 FT(std::forward<Args>(args)...);
105 addSpecialFragment(F);
106 return F;
107 }
108
109 void ensureHeadroom(size_t Headroom);
110 void appendContents(ArrayRef<char> Contents);
111 void appendContents(size_t Num, uint8_t Elt);
112 // Add a fixup to the current fragment. Call ensureHeadroom beforehand to
113 // ensure the fixup and appended content apply to the same fragment.
114 void addFixup(const MCExpr *Value, MCFixupKind Kind);
115
116 void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
117 virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F,
119 void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
121 const MCExpr *Value) override;
122 void emitValueImpl(const MCExpr *Value, unsigned Size,
123 SMLoc Loc = SMLoc()) override;
124 void emitULEB128Value(const MCExpr *Value) override;
125 void emitSLEB128Value(const MCExpr *Value) override;
126 void emitWeakReference(MCSymbol *Alias, const MCSymbol *Target) override;
127 void changeSection(MCSection *Section, uint32_t Subsection = 0) override;
128 void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
129
130 /// Emit an instruction to a special fragment, because this instruction
131 /// can change its size during relaxation.
132 void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
133
134 void emitBytes(StringRef Data) override;
135 void emitValueToAlignment(Align Alignment, int64_t Fill = 0,
136 uint8_t FillLen = 1,
137 unsigned MaxBytesToEmit = 0) override;
138 void emitCodeAlignment(Align ByteAlignment, const MCSubtargetInfo *STI,
139 unsigned MaxBytesToEmit = 0) override;
140 void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
141 SMLoc Loc) override;
142 void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column,
143 unsigned Flags, unsigned Isa,
144 unsigned Discriminator, StringRef FileName,
145 StringRef Comment = {}) override;
146 void emitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
147 const MCSymbol *Label,
148 unsigned PointerSize) override;
149 void emitDwarfLineEndEntry(MCSection *Section, MCSymbol *LastLabel,
150 MCSymbol *EndLabel = nullptr) override;
151 void emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
152 const MCSymbol *Label, SMLoc Loc);
153 void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line,
154 unsigned Column, bool PrologueEnd, bool IsStmt,
155 StringRef FileName, SMLoc Loc) override;
156 void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin,
157 const MCSymbol *End) override;
158 void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
159 unsigned SourceFileId,
160 unsigned SourceLineNum,
161 const MCSymbol *FnStartSym,
162 const MCSymbol *FnEndSym) override;
164 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
165 StringRef FixedSizePortion) override;
166 void emitCVStringTableDirective() override;
167 void emitCVFileChecksumsDirective() override;
168 void emitCVFileChecksumOffsetDirective(unsigned FileNo) override;
169 void emitRelocDirective(const MCExpr &Offset, StringRef Name,
170 const MCExpr *Expr, SMLoc Loc = {}) override;
172 void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
173 SMLoc Loc = SMLoc()) override;
174 void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
175 SMLoc Loc = SMLoc()) override;
176 void emitNops(int64_t NumBytes, int64_t ControlledNopLength, SMLoc Loc,
177 const MCSubtargetInfo &STI) override;
178 void emitFileDirective(StringRef Filename) override;
179 void emitFileDirective(StringRef Filename, StringRef CompilerVersion,
180 StringRef TimeStamp, StringRef Description) override;
181
182 void emitAddrsig() override;
183 void emitAddrsigSym(const MCSymbol *Sym) override;
184
185 void finishImpl() override;
186
187 /// Emit the absolute difference between two symbols if possible.
188 ///
189 /// Emit the absolute difference between \c Hi and \c Lo, as long as we can
190 /// compute it. Currently, that requires that both symbols are in the same
191 /// data fragment and that the target has not specified that diff expressions
192 /// require relocations to be emitted. Otherwise, do nothing and return
193 /// \c false.
194 ///
195 /// \pre Offset of \c Hi is greater than the offset \c Lo.
196 void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
197 unsigned Size) override;
198
199 void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
200 const MCSymbol *Lo) override;
201
202 bool mayHaveInstructions(MCSection &Sec) const override;
203
204 /// Emits pending conditional assignments that depend on \p Symbol
205 /// being emitted.
206 void emitPendingAssignments(MCSymbol *Symbol);
207};
208
209} // end namespace llvm
210
211#endif
dxil DXContainer Global Emitter
static ManagedStatic< cl::opt< bool, true >, CreateDebug > Debug
Definition: Debug.cpp:147
std::string Name
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define F(x, y, z)
Definition: MD5.cpp:55
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:41
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
LLVM_ATTRIBUTE_RETURNS_NONNULL void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:149
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:55
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
size_t getFixedSize() const
Definition: MCSection.h:200
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:188
Streaming object file generation interface.
void reset() override
state management
void emitFill(const MCExpr &NumBytes, uint64_t FillValue, SMLoc Loc=SMLoc()) override
Emit Size bytes worth of the value specified by FillValue.
FT * newSpecialFragment(Args &&...args)
void emitValueToAlignment(Align Alignment, int64_t Fill=0, uint8_t FillLen=1, unsigned MaxBytesToEmit=0) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override
Emit an assignment of Value to Symbol.
void emitCFISections(bool EH, bool Debug, bool SFrame) override
void emitULEB128Value(const MCExpr *Value) override
void emitSLEB128Value(const MCExpr *Value) override
void emitNops(int64_t NumBytes, int64_t ControlledNopLength, SMLoc Loc, const MCSubtargetInfo &STI) override
void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, const MCSymbol *FnStartSym, const MCSymbol *FnEndSym) override
This implements the CodeView '.cv_inline_linetable' assembler directive.
void emitCVStringTableDirective() override
This implements the CodeView '.cv_stringtable' assembler directive.
MCAssembler & getAssembler()
void emitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel, const MCSymbol *Label, unsigned PointerSize) override
If targets does not support representing debug line section by .loc/.file directives in assembly outp...
void emitWeakReference(MCSymbol *Alias, const MCSymbol *Target) override
Emit an weak reference from Alias to Symbol.
void appendContents(ArrayRef< char > Contents)
void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line, unsigned Column, bool PrologueEnd, bool IsStmt, StringRef FileName, SMLoc Loc) override
This implements the CodeView '.cv_loc' assembler directive.
void emitRelocDirective(const MCExpr &Offset, StringRef Name, const MCExpr *Expr, SMLoc Loc={}) override
Record a relocation described by the .reloc directive.
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
void emitPendingAssignments(MCSymbol *Symbol)
Emits pending conditional assignments that depend on Symbol being emitted.
void addFixup(const MCExpr *Value, MCFixupKind Kind)
void emitFileDirective(StringRef Filename) override
Switch to a new logical file.
void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column, unsigned Flags, unsigned Isa, unsigned Discriminator, StringRef FileName, StringRef Comment={}) override
This implements the DWARF2 '.loc fileno lineno ...' assembler directive.
MCSymbol * emitCFILabel() override
When emitting an object file, create and emit a real label.
void emitCVDefRangeDirective(ArrayRef< std::pair< const MCSymbol *, const MCSymbol * > > Ranges, StringRef FixedSizePortion) override
This implements the CodeView '.cv_def_range' assembler directive.
MCAssembler * getAssemblerPtr() override
void emitAddrsigSym(const MCSymbol *Sym) override
bool isIntegratedAssemblerRequired() const override
Object streamers require the integrated assembler.
void emitDwarfLineEndEntry(MCSection *Section, MCSymbol *LastLabel, MCSymbol *EndLabel=nullptr) override
Emit the debug line end entry.
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
void visitUsedSymbol(const MCSymbol &Sym) override
void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &)
Emit an instruction to a special fragment, because this instruction can change its size during relaxa...
virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F, uint64_t Offset)
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
void finishImpl() override
Streamer specific finalization.
void changeSection(MCSection *Section, uint32_t Subsection=0) override
This is called by popSection and switchSection, if the current section changes.
void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi, const MCSymbol *Lo) override
Emit the absolute difference between two symbols encoded with ULEB128.
MCFragment * allocFragSpace(size_t Headroom)
void emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel, const MCSymbol *Label, SMLoc Loc)
bool mayHaveInstructions(MCSection &Sec) const override
void emitCodeAlignment(Align ByteAlignment, const MCSubtargetInfo *STI, unsigned MaxBytesToEmit=0) override
Emit nops until the byte alignment ByteAlignment is reached.
void emitFrames(MCAsmBackend *MAB)
void ensureHeadroom(size_t Headroom)
void emitCVFileChecksumOffsetDirective(unsigned FileNo) override
This implements the CodeView '.cv_filechecksumoffset' assembler directive.
void emitConditionalAssignment(MCSymbol *Symbol, const MCExpr *Value) override
Emit an assignment of Value to Symbol, but only if Value is also emitted.
void emitCVFileChecksumsDirective() override
This implements the CodeView '.cv_filechecksums' assembler directive.
void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin, const MCSymbol *End) override
This implements the CodeView '.cv_linetable' assembler directive.
void emitValueToOffset(const MCExpr *Offset, unsigned char Value, SMLoc Loc) override
Emit some number of copies of Value until the byte offset Offset is reached.
uint8_t * getCurFragEnd() const
void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size) override
Emit the absolute difference between two symbols if possible.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:496
Streaming machine code generation interface.
Definition: MCStreamer.h:220
MCFragment * CurFrag
Definition: MCStreamer.h:267
void emitFill(uint64_t NumBytes, uint8_t FillValue)
Emit NumBytes bytes worth of the value specified by FillValue.
Definition: MCStreamer.cpp:195
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:23
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
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 is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39