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