LLVM 20.0.0git
MCAssembler.h
Go to the documentation of this file.
1//===- MCAssembler.h - Object File Generation -------------------*- 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_MCASSEMBLER_H
10#define LLVM_MC_MCASSEMBLER_H
11
12#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/ADT/iterator.h"
18#include "llvm/MC/MCDwarf.h"
19#include "llvm/MC/MCSymbol.h"
20#include "llvm/Support/SMLoc.h"
21#include <algorithm>
22#include <cassert>
23#include <cstddef>
24#include <cstdint>
25#include <memory>
26#include <string>
27#include <tuple>
28#include <utility>
29#include <vector>
30
31namespace llvm {
32
33class MCBoundaryAlignFragment;
34class MCCVDefRangeFragment;
35class MCCVInlineLineTableFragment;
36class MCDwarfCallFrameFragment;
37class MCDwarfLineAddrFragment;
38class MCEncodedFragment;
39class MCFixup;
40class MCLEBFragment;
41class MCPseudoProbeAddrFragment;
42class MCRelaxableFragment;
43class MCSymbolRefExpr;
44class raw_ostream;
45class MCAsmBackend;
46class MCContext;
47class MCCodeEmitter;
48class MCFragment;
49class MCObjectWriter;
50class MCSection;
51class MCValue;
52
54public:
55 friend class MCObjectWriter;
58
59private:
60 MCContext &Context;
61
62 std::unique_ptr<MCAsmBackend> Backend;
63 std::unique_ptr<MCCodeEmitter> Emitter;
64 std::unique_ptr<MCObjectWriter> Writer;
65
66 bool HasLayout = false;
67 bool RelaxAll = false;
68
69 SectionListType Sections;
70
72
74
75 /// The set of function symbols for which a .thumb_func directive has
76 /// been seen.
77 //
78 // FIXME: We really would like this in target specific code rather than
79 // here. Maybe when the relocation stuff moves to target specific,
80 // this can go with it? The streamer would need some target specific
81 // refactoring too.
82 mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
83
84 /// The bundle alignment size currently set in the assembler.
85 ///
86 /// By default it's 0, which means bundling is disabled.
87 unsigned BundleAlignSize = 0;
88
89 /// Evaluate a fixup to a relocatable expression and the value which should be
90 /// placed into the fixup.
91 ///
92 /// \param Fixup The fixup to evaluate.
93 /// \param DF The fragment the fixup is inside.
94 /// \param Target [out] On return, the relocatable expression the fixup
95 /// evaluates to.
96 /// \param Value [out] On return, the value of the fixup as currently laid
97 /// out.
98 /// \param WasForced [out] On return, the value in the fixup is set to the
99 /// correct value if WasForced is true, even if evaluateFixup returns false.
100 /// \return Whether the fixup value was fully resolved. This is true if the
101 /// \p Value result is fixed, otherwise the value may change due to
102 /// relocation.
103 bool evaluateFixup(const MCFixup &Fixup, const MCFragment *DF,
104 MCValue &Target, const MCSubtargetInfo *STI,
105 uint64_t &Value, bool &WasForced) const;
106
107 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
108 /// (increased in size, in order to hold its value correctly).
109 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF) const;
110
111 /// Check whether the given fragment needs relaxation.
112 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF) const;
113
114 void layoutSection(MCSection &Sec);
115 /// Perform one layout iteration and return true if any offsets
116 /// were adjusted.
117 bool layoutOnce();
118
119 /// Perform relaxation on a single fragment - returns true if the fragment
120 /// changes as a result of relaxation.
121 bool relaxFragment(MCFragment &F);
122 bool relaxInstruction(MCRelaxableFragment &IF);
123 bool relaxLEB(MCLEBFragment &IF);
124 bool relaxBoundaryAlign(MCBoundaryAlignFragment &BF);
125 bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF);
126 bool relaxDwarfCallFrameFragment(MCDwarfCallFrameFragment &DF);
127 bool relaxCVInlineLineTable(MCCVInlineLineTableFragment &DF);
128 bool relaxCVDefRange(MCCVDefRangeFragment &DF);
129 bool relaxPseudoProbeAddr(MCPseudoProbeAddrFragment &DF);
130
131 std::tuple<MCValue, uint64_t, bool>
132 handleFixup(MCFragment &F, const MCFixup &Fixup, const MCSubtargetInfo *STI);
133
134public:
135 /// Construct a new assembler instance.
136 //
137 // FIXME: How are we going to parameterize this? Two obvious options are stay
138 // concrete and require clients to pass in a target like object. The other
139 // option is to make this abstract, and have targets provide concrete
140 // implementations as we do with AsmParser.
141 MCAssembler(MCContext &Context, std::unique_ptr<MCAsmBackend> Backend,
142 std::unique_ptr<MCCodeEmitter> Emitter,
143 std::unique_ptr<MCObjectWriter> Writer);
144 MCAssembler(const MCAssembler &) = delete;
146
147 /// Compute the effective fragment size.
149
150 void layoutBundle(MCFragment *Prev, MCFragment *F) const;
151
152 // Get the offset of the given fragment inside its containing section.
153 uint64_t getFragmentOffset(const MCFragment &F) const { return F.Offset; }
154
155 uint64_t getSectionAddressSize(const MCSection &Sec) const;
156 uint64_t getSectionFileSize(const MCSection &Sec) const;
157
158 // Get the offset of the given symbol, as computed in the current
159 // layout.
160 // \return True on success.
161 bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const;
162
163 // Variant that reports a fatal error if the offset is not computable.
164 uint64_t getSymbolOffset(const MCSymbol &S) const;
165
166 // If this symbol is equivalent to A + Constant, return A.
167 const MCSymbol *getBaseSymbol(const MCSymbol &Symbol) const;
168
169 /// Emit the section contents to \p OS.
170 void writeSectionData(raw_ostream &OS, const MCSection *Section) const;
171
172 /// Check whether a given symbol has been flagged with .thumb_func.
173 bool isThumbFunc(const MCSymbol *Func) const;
174
175 /// Flag a function symbol as the target of a .thumb_func directive.
176 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
177
178 /// Reuse an assembler instance
179 ///
180 void reset();
181
182 MCContext &getContext() const { return Context; }
183
184 MCAsmBackend *getBackendPtr() const { return Backend.get(); }
185
186 MCCodeEmitter *getEmitterPtr() const { return Emitter.get(); }
187
188 MCAsmBackend &getBackend() const { return *Backend; }
189
190 MCCodeEmitter &getEmitter() const { return *Emitter; }
191
192 MCObjectWriter &getWriter() const { return *Writer; }
193
195
196 /// Finish - Do final processing and write the object to the output stream.
197 /// \p Writer is used for custom object writer (as the MCJIT does),
198 /// if not specified it is automatically created from backend.
199 void Finish();
200
201 // Layout all section and prepare them for emission.
202 void layout();
203
204 bool hasLayout() const { return HasLayout; }
205 bool getRelaxAll() const { return RelaxAll; }
206 void setRelaxAll(bool Value) { RelaxAll = Value; }
207
208 bool isBundlingEnabled() const { return BundleAlignSize != 0; }
209
210 unsigned getBundleAlignSize() const { return BundleAlignSize; }
211
212 void setBundleAlignSize(unsigned Size) {
213 assert((Size == 0 || !(Size & (Size - 1))) &&
214 "Expect a power-of-two bundle align size");
215 BundleAlignSize = Size;
216 }
217
218 const_iterator begin() const { return Sections.begin(); }
219 const_iterator end() const { return Sections.end(); }
220
223 symbols() const {
224 return make_pointee_range(Symbols);
225 }
226
227 bool registerSection(MCSection &Section);
228 bool registerSymbol(const MCSymbol &Symbol);
229
230 /// Write the necessary bundle padding to \p OS.
231 /// Expects a fragment \p F containing instructions and its size \p FSize.
233 uint64_t FSize) const;
234
235 void dump() const;
236};
237
238} // end namespace llvm
239
240#endif // LLVM_MC_MCASSEMBLER_H
dxil DXContainer Global Emitter
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
uint64_t Size
#define F(x, y, z)
Definition: MD5.cpp:55
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:42
const_iterator begin() const
Definition: MCAssembler.h:218
MCContext & getContext() const
Definition: MCAssembler.h:182
bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
uint64_t getSectionAddressSize(const MCSection &Sec) const
MCAssembler & operator=(const MCAssembler &)=delete
void Finish()
Finish - Do final processing and write the object to the output stream.
unsigned getBundleAlignSize() const
Definition: MCAssembler.h:210
bool isBundlingEnabled() const
Definition: MCAssembler.h:208
void setBundleAlignSize(unsigned Size)
Definition: MCAssembler.h:212
void setIsThumbFunc(const MCSymbol *Func)
Flag a function symbol as the target of a .thumb_func directive.
Definition: MCAssembler.h:176
void writeSectionData(raw_ostream &OS, const MCSection *Section) const
Emit the section contents to OS.
void dump() const
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:192
MCCodeEmitter * getEmitterPtr() const
Definition: MCAssembler.h:186
void layoutBundle(MCFragment *Prev, MCFragment *F) const
bool getRelaxAll() const
Definition: MCAssembler.h:205
MCAssembler(const MCAssembler &)=delete
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:190
bool isThumbFunc(const MCSymbol *Func) const
Check whether a given symbol has been flagged with .thumb_func.
const_iterator end() const
Definition: MCAssembler.h:219
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:188
bool registerSection(MCSection &Section)
uint64_t computeFragmentSize(const MCFragment &F) const
Compute the effective fragment size.
const MCSymbol * getBaseSymbol(const MCSymbol &Symbol) const
MCAsmBackend * getBackendPtr() const
Definition: MCAssembler.h:184
iterator_range< pointee_iterator< typename SmallVector< const MCSymbol *, 0 >::const_iterator > > symbols() const
Definition: MCAssembler.h:223
uint64_t getSectionFileSize(const MCSection &Sec) const
void reset()
Reuse an assembler instance.
Definition: MCAssembler.cpp:89
bool registerSymbol(const MCSymbol &Symbol)
bool hasLayout() const
Definition: MCAssembler.h:204
uint64_t getFragmentOffset(const MCFragment &F) const
Definition: MCAssembler.h:153
MCDwarfLineTableParams getDWARFLinetableParams() const
Definition: MCAssembler.h:194
void writeFragmentPadding(raw_ostream &OS, const MCEncodedFragment &F, uint64_t FSize) const
Write the necessary bundle padding to OS.
void setRelaxAll(bool Value)
Definition: MCAssembler.h:206
Represents required padding such that a particular other set of fragments does not cross a particular...
Definition: MCFragment.h:563
Fragment representing the .cv_def_range directive.
Definition: MCFragment.h:533
Fragment representing the binary annotations produced by the .cv_inline_linetable directive.
Definition: MCFragment.h:501
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:21
Context object for machine code objects.
Definition: MCContext.h:83
Interface implemented by fragments that contain encoded instructions and/or data.
Definition: MCFragment.h:118
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
Defines the object file and target independent interfaces used by the assembler backend to write nati...
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:261
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
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:41
This represents an "assembler immediate".
Definition: MCValue.h:36
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:344
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:479
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition: Value.h:74
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< pointee_iterator< WrappedIteratorT > > make_pointee_range(RangeT &&Range)
Definition: iterator.h:336
An iterator type that allows iterating over the pointees via some other iterator.
Definition: iterator.h:324