LLVM 24.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"
22#include "llvm/Support/SMLoc.h"
23#include <cassert>
24#include <cstddef>
25#include <cstdint>
26#include <memory>
27#include <string>
28#include <utility>
29
30namespace llvm {
31
35class MCFragment;
36class MCFixup;
37class MCSymbolRefExpr;
38class raw_ostream;
39class MCAsmBackend;
40class MCContext;
41class MCCodeEmitter;
42class MCFragment;
43class MCObjectWriter;
44class MCSection;
45class MCValue;
46
48public:
49 friend class MCObjectWriter;
52
53private:
54 MCContext &Context;
55
56 std::unique_ptr<MCAsmBackend> Backend;
57 std::unique_ptr<MCCodeEmitter> Emitter;
58 std::unique_ptr<MCObjectWriter> Writer;
59
60 bool HasLayout = false;
61 bool HasFinalLayout = false;
62 bool RelaxAll = false;
63
64 // Cumulative upstream size change during `relaxOnce`. Used to compensate
65 // forward-reference displacements in `evaluateFixup`.
66 int64_t Stretch = 0;
67
68 /// Non-empty when aligned instruction bundling is enabled.
69 MaybeAlign BundleAlign;
70
71 SectionListType Sections;
72
74
75 struct RelocDirective {
76 const MCExpr &Offset;
77 const MCExpr *Expr;
78 uint32_t Kind;
79 };
80 SmallVector<RelocDirective, 0> relocDirectives;
81
82 mutable SmallVector<std::pair<SMLoc, std::string>, 0> PendingErrors;
83
85
86 /// The set of function symbols for which a .thumb_func directive has
87 /// been seen.
88 //
89 // FIXME: We really would like this in target specific code rather than
90 // here. Maybe when the relocation stuff moves to target specific,
91 // this can go with it? The streamer would need some target specific
92 // refactoring too.
93 mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
94
95 /// Evaluate a fixup to a relocatable expression and the value which should be
96 /// placed into the fixup.
97 ///
98 /// \param F The fragment the fixup is inside.
99 /// \param Fixup The fixup to evaluate.
100 /// \param Target [out] On return, the relocatable expression the fixup
101 /// evaluates to.
102 /// \param Value [out] On return, the value of the fixup as currently laid
103 /// out.
104 /// \param RecordReloc Record relocation if needed.
105 /// relocation.
106 bool evaluateFixup(const MCFragment &F, MCFixup &Fixup, MCValue &Target,
107 uint64_t &Value, bool RecordReloc, uint8_t *Data) const;
108
109 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
110 /// (increased in size, in order to hold its value correctly).
111 bool fixupNeedsRelaxation(const MCFragment &, const MCFixup &) const;
112
113 void layoutSection(MCSection &Sec);
114 /// Perform one layout iteration and return the index of the first stable
115 /// section for subsequent optimization.
116 unsigned relaxOnce(unsigned FirstStable);
117
118 /// Perform relaxation on a single fragment.
119 void relaxFragment(MCFragment &F);
120 void relaxAlign(MCFragment &F);
121 void relaxPrefAlign(MCFragment &F);
122 void relaxInstruction(MCFragment &F);
123 void relaxLEB(MCFragment &F);
124 void relaxBoundaryAlign(MCBoundaryAlignFragment &BF);
125 void relaxDwarfLineAddr(MCFragment &F);
126 void relaxDwarfCallFrameFragment(MCFragment &F);
127 void relaxSFrameFragment(MCFragment &DF);
128
129public:
130 /// Construct a new assembler instance.
131 //
132 // FIXME: How are we going to parameterize this? Two obvious options are stay
133 // concrete and require clients to pass in a target like object. The other
134 // option is to make this abstract, and have targets provide concrete
135 // implementations as we do with AsmParser.
137 std::unique_ptr<MCAsmBackend> Backend,
138 std::unique_ptr<MCCodeEmitter> Emitter,
139 std::unique_ptr<MCObjectWriter> Writer);
140 MCAssembler(const MCAssembler &) = delete;
142
143 /// Compute the effective fragment size.
145
146 // Get the offset of the given fragment inside its containing section.
147 uint64_t getFragmentOffset(const MCFragment &F) const { return F.Offset; }
148
151
152 // Get the offset of the given symbol, as computed in the current
153 // layout.
154 // \return True on success.
155 LLVM_ABI bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const;
156
157 // Variant that reports a fatal error if the offset is not computable.
159
160 // If this symbol is equivalent to A + Constant, return A.
161 LLVM_ABI const MCSymbol *getBaseSymbol(const MCSymbol &Symbol) const;
162
163 /// Emit the section contents to \p OS.
165 const MCSection *Section) const;
166
167 /// Check whether a given symbol has been flagged with .thumb_func.
168 LLVM_ABI bool isThumbFunc(const MCSymbol *Func) const;
169
170 /// Flag a function symbol as the target of a .thumb_func directive.
171 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
172
173 /// Reuse an assembler instance
174 ///
175 LLVM_ABI void reset();
176
177 MCContext &getContext() const { return Context; }
178
179 MCAsmBackend *getBackendPtr() const { return Backend.get(); }
180
181 MCCodeEmitter *getEmitterPtr() const { return Emitter.get(); }
182
183 MCAsmBackend &getBackend() const { return *Backend; }
184
185 MCCodeEmitter &getEmitter() const { return *Emitter; }
186
187 MCObjectWriter &getWriter() const { return *Writer; }
188
190
191 /// Finish - Do final processing and write the object to the output stream.
192 /// \p Writer is used for custom object writer (as the MCJIT does),
193 /// if not specified it is automatically created from backend.
194 LLVM_ABI void Finish();
195
196 // Layout all section and prepare them for emission.
197 LLVM_ABI void layout();
198
199 bool hasLayout() const { return HasLayout; }
200 bool hasFinalLayout() const { return HasFinalLayout; }
201 bool getRelaxAll() const { return RelaxAll; }
202 void setRelaxAll(bool Value) { RelaxAll = Value; }
203 int64_t getStretch() const { return Stretch; }
204
205 bool isBundlingEnabled() const { return bool(BundleAlign); }
207 assert(BundleAlign && "bundling is not enabled");
208 return *BundleAlign;
209 }
210 void setBundleAlign(Align Value) { BundleAlign = Value; }
211
212 const_iterator begin() const { return Sections.begin(); }
213 const_iterator end() const { return Sections.end(); }
214
218 symbols() const {
219 return make_pointee_range(Symbols);
220 }
221
222 LLVM_ABI bool registerSection(MCSection &Section);
223 LLVM_ABI bool registerSymbol(const MCSymbol &Symbol);
224 LLVM_ABI void addRelocDirective(RelocDirective RD);
225
226 LLVM_ABI void reportError(SMLoc L, const Twine &Msg) const;
227 // Record pending errors during layout iteration, as they may go away once the
228 // layout is finalized.
229 LLVM_ABI void recordError(SMLoc L, const Twine &Msg) const;
230 LLVM_ABI void flushPendingErrors() const;
231
232 LLVM_ABI void dump() const;
233};
234
235} // end namespace llvm
236
237#endif // LLVM_MC_MCASSEMBLER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:215
dxil DXContainer Global Emitter
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
#define F(x, y, z)
Definition MD5.cpp:54
PowerPC TLS Dynamic Call Fixup
const char * Msg
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
Generic interface to target specific assembler backends.
const_iterator begin() const
MCContext & getContext() const
LLVM_ABI bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
LLVM_ABI uint64_t getSectionAddressSize(const MCSection &Sec) const
MCAssembler & operator=(const MCAssembler &)=delete
LLVM_ABI void Finish()
Finish - Do final processing and write the object to the output stream.
SmallVector< MCSection *, 0 > SectionListType
Definition MCAssembler.h:50
bool isBundlingEnabled() const
LLVM_ABI void reportError(SMLoc L, const Twine &Msg) const
void setIsThumbFunc(const MCSymbol *Func)
Flag a function symbol as the target of a .thumb_func directive.
LLVM_ABI void writeSectionData(raw_ostream &OS, const MCSection *Section) const
Emit the section contents to OS.
iterator_range< pointee_iterator< SmallVector< const MCSymbol *, 0 >::const_iterator > > symbols() const
int64_t getStretch() const
LLVM_ABI void dump() const
LLVM_ABI void layout()
void setBundleAlign(Align Value)
MCObjectWriter & getWriter() const
bool hasFinalLayout() const
Align getBundleAlign() const
MCCodeEmitter * getEmitterPtr() const
LLVM_ABI void addRelocDirective(RelocDirective RD)
SmallVectorImpl< const MCSymbol * > & getSymbols()
bool getRelaxAll() const
MCAssembler(const MCAssembler &)=delete
MCCodeEmitter & getEmitter() const
LLVM_ABI void recordError(SMLoc L, const Twine &Msg) const
LLVM_ABI MCAssembler(MCContext &Context, std::unique_ptr< MCAsmBackend > Backend, std::unique_ptr< MCCodeEmitter > Emitter, std::unique_ptr< MCObjectWriter > Writer)
Construct a new assembler instance.
LLVM_ABI bool isThumbFunc(const MCSymbol *Func) const
Check whether a given symbol has been flagged with .thumb_func.
const_iterator end() const
MCAsmBackend & getBackend() const
LLVM_ABI bool registerSection(MCSection &Section)
LLVM_ABI void flushPendingErrors() const
LLVM_ABI uint64_t computeFragmentSize(const MCFragment &F) const
Compute the effective fragment size.
LLVM_ABI const MCSymbol * getBaseSymbol(const MCSymbol &Symbol) const
pointee_iterator< SectionListType::const_iterator > const_iterator
Definition MCAssembler.h:51
MCAsmBackend * getBackendPtr() const
LLVM_ABI uint64_t getSectionFileSize(const MCSection &Sec) const
friend class MCObjectWriter
Definition MCAssembler.h:49
LLVM_ABI void reset()
Reuse an assembler instance.
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol)
bool hasLayout() const
uint64_t getFragmentOffset(const MCFragment &F) const
MCDwarfLineTableParams getDWARFLinetableParams() const
void setRelaxAll(bool Value)
Represents required padding such that a particular other set of fragments does not cross a particular...
Definition MCSection.h:539
Fragment representing the .cv_def_range directive.
Definition MCSection.h:510
Fragment representing the binary annotations produced by the .cv_inline_linetable directive.
Definition MCSection.h:482
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
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:61
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:580
Represent a reference to a symbol from inside an expression.
Definition MCExpr.h:190
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
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
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:53
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.
@ Offset
Definition DWP.cpp:578
iterator_range< pointee_iterator< WrappedIteratorT > > make_pointee_range(RangeT &&Range)
Definition iterator.h:341
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
An iterator type that allows iterating over the pointees via some other iterator.
Definition iterator.h:329