LLVM 20.0.0git
MCAsmBackend.h
Go to the documentation of this file.
1//===- llvm/MC/MCAsmBackend.h - MC Asm Backend ------------------*- 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_MCASMBACKEND_H
10#define LLVM_MC_MCASMBACKEND_H
11
12#include "llvm/ADT/ArrayRef.h"
14#include "llvm/MC/MCFixup.h"
15#include "llvm/Support/Endian.h"
16#include <cstdint>
17
18namespace llvm {
19
20class MCAlignFragment;
21class MCDwarfCallFrameFragment;
22class MCDwarfLineAddrFragment;
23class MCFragment;
24class MCLEBFragment;
25class MCRelaxableFragment;
26class MCSymbol;
27class MCAssembler;
28class MCContext;
29struct MCDwarfFrameInfo;
30struct MCFixupKindInfo;
31class MCInst;
32class MCObjectStreamer;
33class MCObjectTargetWriter;
34class MCObjectWriter;
35class MCSubtargetInfo;
36class MCValue;
37class raw_pwrite_stream;
38class StringRef;
39class raw_ostream;
40
41/// Generic interface to target specific assembler backends.
43protected: // Can only create subclasses.
45
46public:
47 MCAsmBackend(const MCAsmBackend &) = delete;
49 virtual ~MCAsmBackend();
50
52
53 /// Fixup kind used for linker relaxation. Currently only used by RISC-V
54 /// and LoongArch.
55 const unsigned RelaxFixupKind;
57
58 /// Return true if this target might automatically pad instructions and thus
59 /// need to emit padding enable/disable directives around sensative code.
60 virtual bool allowAutoPadding() const { return false; }
61 /// Return true if this target allows an unrelaxable instruction to be
62 /// emitted into RelaxableFragment and then we can increase its size in a
63 /// tricky way for optimization.
64 virtual bool allowEnhancedRelaxation() const { return false; }
65
66 /// lifetime management
67 virtual void reset() {}
68
69 /// Create a new MCObjectWriter instance for use by the assembler backend to
70 /// emit the final object file.
71 std::unique_ptr<MCObjectWriter>
73
74 /// Create an MCObjectWriter that writes two object files: a .o file which is
75 /// linked into the final program and a .dwo file which is used by debuggers.
76 /// This function is only supported with ELF targets.
77 std::unique_ptr<MCObjectWriter>
79
80 virtual std::unique_ptr<MCObjectTargetWriter>
82
83 /// \name Target Fixup Interfaces
84 /// @{
85
86 /// Get the number of target specific fixup kinds.
87 virtual unsigned getNumFixupKinds() const = 0;
88
89 /// Map a relocation name used in .reloc to a fixup kind.
90 virtual std::optional<MCFixupKind> getFixupKind(StringRef Name) const;
91
92 /// Get information on a fixup kind.
93 virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;
94
95 /// Hook to check if a relocation is needed for some target specific reason.
96 virtual bool shouldForceRelocation(const MCAssembler &Asm,
97 const MCFixup &Fixup,
98 const MCValue &Target,
99 const MCSubtargetInfo *STI) {
100 return false;
101 }
102
103 /// Hook to check if extra nop bytes must be inserted for alignment directive.
104 /// For some targets this may be necessary in order to support linker
105 /// relaxation. The number of bytes to insert are returned in Size.
107 unsigned &Size) {
108 return false;
109 }
110
111 /// Hook which indicates if the target requires a fixup to be generated when
112 /// handling an align directive in an executable section
114 MCAlignFragment &AF) {
115 return false;
116 }
117
118 virtual bool evaluateTargetFixup(const MCAssembler &Asm,
119 const MCFixup &Fixup, const MCFragment *DF,
120 const MCValue &Target,
121 const MCSubtargetInfo *STI, uint64_t &Value,
122 bool &WasForced) {
123 llvm_unreachable("Need to implement hook if target has custom fixups");
124 }
125
126 virtual bool handleAddSubRelocations(const MCAssembler &Asm,
127 const MCFragment &F,
128 const MCFixup &Fixup,
129 const MCValue &Target,
130 uint64_t &FixedValue) const {
131 return false;
132 }
133
134 /// Apply the \p Value for given \p Fixup into the provided data fragment, at
135 /// the offset specified by the fixup and following the fixup kind as
136 /// appropriate. Errors (such as an out of range fixup value) should be
137 /// reported via \p Ctx.
138 /// The \p STI is present only for fragments of type MCRelaxableFragment and
139 /// MCDataFragment with hasInstructions() == true.
140 virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
142 uint64_t Value, bool IsResolved,
143 const MCSubtargetInfo *STI) const = 0;
144
145 /// @}
146
147 /// \name Target Relaxation Interfaces
148 /// @{
149
150 /// Check whether the given instruction may need relaxation.
151 ///
152 /// \param Inst - The instruction to test.
153 /// \param STI - The MCSubtargetInfo in effect when the instruction was
154 /// encoded.
155 virtual bool mayNeedRelaxation(const MCInst &Inst,
156 const MCSubtargetInfo &STI) const {
157 return false;
158 }
159
160 /// Target specific predicate for whether a given fixup requires the
161 /// associated instruction to be relaxed.
162 virtual bool fixupNeedsRelaxationAdvanced(const MCAssembler &Asm,
163 const MCFixup &Fixup, bool Resolved,
165 const MCRelaxableFragment *DF,
166 const bool WasForced) const;
167
168 /// Simple predicate for targets where !Resolved implies requiring relaxation
169 virtual bool fixupNeedsRelaxation(const MCFixup &Fixup,
170 uint64_t Value) const {
171 llvm_unreachable("Needed if mayNeedRelaxation may return true");
172 }
173
174 /// Relax the instruction in the given fragment to the next wider instruction.
175 ///
176 /// \param [out] Inst The instruction to relax, which is also the relaxed
177 /// instruction.
178 /// \param STI the subtarget information for the associated instruction.
179 virtual void relaxInstruction(MCInst &Inst,
180 const MCSubtargetInfo &STI) const {};
181
182 virtual bool relaxDwarfLineAddr(const MCAssembler &Asm,
184 bool &WasRelaxed) const {
185 return false;
186 }
187
188 virtual bool relaxDwarfCFA(const MCAssembler &Asm,
190 bool &WasRelaxed) const {
191 return false;
192 }
193
194 // Defined by linker relaxation targets to possibly emit LEB128 relocations
195 // and set Value at the relocated location.
196 virtual std::pair<bool, bool>
197 relaxLEB128(const MCAssembler &Asm, MCLEBFragment &LF, int64_t &Value) const {
198 return std::make_pair(false, false);
199 }
200
201 /// @}
202
203 /// Returns the minimum size of a nop in bytes on this target. The assembler
204 /// will use this to emit excess padding in situations where the padding
205 /// required for simple alignment would be less than the minimum nop size.
206 ///
207 virtual unsigned getMinimumNopSize() const { return 1; }
208
209 /// Returns the maximum size of a nop in bytes on this target.
210 ///
211 virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const {
212 return 0;
213 }
214
215 /// Write an (optimal) nop sequence of Count bytes to the given output. If the
216 /// target cannot generate such a sequence, it should return an error.
217 ///
218 /// \return - True on success.
219 virtual bool writeNopData(raw_ostream &OS, uint64_t Count,
220 const MCSubtargetInfo *STI) const = 0;
221
222 /// Give backend an opportunity to finish layout after relaxation
223 virtual void finishLayout(MCAssembler const &Asm) const {}
224
225 /// Handle any target-specific assembler flags. By default, do nothing.
227
228 /// Generate the compact unwind encoding for the CFI instructions.
230 const MCContext *Ctxt) const {
231 return 0;
232 }
233
234 /// Check whether a given symbol has been flagged with MICROMIPS flag.
235 virtual bool isMicroMips(const MCSymbol *Sym) const {
236 return false;
237 }
238
239 bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const;
240};
241
242} // end namespace llvm
243
244#endif // LLVM_MC_MCASMBACKEND_H
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
std::string Name
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define F(x, y, z)
Definition: MD5.cpp:55
PowerPC TLS Dynamic Call Fixup
raw_pwrite_stream & OS
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:42
std::unique_ptr< MCObjectWriter > createObjectWriter(raw_pwrite_stream &OS) const
Create a new MCObjectWriter instance for use by the assembler backend to emit the final object file.
std::unique_ptr< MCObjectWriter > createDwoObjectWriter(raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS) const
Create an MCObjectWriter that writes two object files: a .o file which is linked into the final progr...
virtual unsigned getMinimumNopSize() const
Returns the minimum size of a nop in bytes on this target.
Definition: MCAsmBackend.h:207
MCAsmBackend(const MCAsmBackend &)=delete
const llvm::endianness Endian
Definition: MCAsmBackend.h:51
virtual bool allowEnhancedRelaxation() const
Return true if this target allows an unrelaxable instruction to be emitted into RelaxableFragment and...
Definition: MCAsmBackend.h:64
virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const
Returns the maximum size of a nop in bytes on this target.
Definition: MCAsmBackend.h:211
virtual void handleAssemblerFlag(MCAssemblerFlag Flag)
Handle any target-specific assembler flags. By default, do nothing.
Definition: MCAsmBackend.h:226
virtual bool writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const =0
Write an (optimal) nop sequence of Count bytes to the given output.
virtual void relaxInstruction(MCInst &Inst, const MCSubtargetInfo &STI) const
Relax the instruction in the given fragment to the next wider instruction.
Definition: MCAsmBackend.h:179
virtual bool mayNeedRelaxation(const MCInst &Inst, const MCSubtargetInfo &STI) const
Check whether the given instruction may need relaxation.
Definition: MCAsmBackend.h:155
virtual bool isMicroMips(const MCSymbol *Sym) const
Check whether a given symbol has been flagged with MICROMIPS flag.
Definition: MCAsmBackend.h:235
virtual bool shouldInsertFixupForCodeAlign(MCAssembler &Asm, MCAlignFragment &AF)
Hook which indicates if the target requires a fixup to be generated when handling an align directive ...
Definition: MCAsmBackend.h:113
virtual bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF, unsigned &Size)
Hook to check if extra nop bytes must be inserted for alignment directive.
Definition: MCAsmBackend.h:106
virtual bool fixupNeedsRelaxationAdvanced(const MCAssembler &Asm, const MCFixup &Fixup, bool Resolved, uint64_t Value, const MCRelaxableFragment *DF, const bool WasForced) const
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value) const
Simple predicate for targets where !Resolved implies requiring relaxation.
Definition: MCAsmBackend.h:169
virtual bool relaxDwarfCFA(const MCAssembler &Asm, MCDwarfCallFrameFragment &DF, bool &WasRelaxed) const
Definition: MCAsmBackend.h:188
MCAsmBackend & operator=(const MCAsmBackend &)=delete
bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const
virtual bool relaxDwarfLineAddr(const MCAssembler &Asm, MCDwarfLineAddrFragment &DF, bool &WasRelaxed) const
Definition: MCAsmBackend.h:182
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
virtual void finishLayout(MCAssembler const &Asm) const
Give backend an opportunity to finish layout after relaxation.
Definition: MCAsmBackend.h:223
virtual bool handleAddSubRelocations(const MCAssembler &Asm, const MCFragment &F, const MCFixup &Fixup, const MCValue &Target, uint64_t &FixedValue) const
Definition: MCAsmBackend.h:126
bool allowLinkerRelaxation() const
Definition: MCAsmBackend.h:56
virtual bool evaluateTargetFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCFragment *DF, const MCValue &Target, const MCSubtargetInfo *STI, uint64_t &Value, bool &WasForced)
Definition: MCAsmBackend.h:118
virtual void reset()
lifetime management
Definition: MCAsmBackend.h:67
virtual bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, const MCSubtargetInfo *STI)
Hook to check if a relocation is needed for some target specific reason.
Definition: MCAsmBackend.h:96
const unsigned RelaxFixupKind
Fixup kind used for linker relaxation.
Definition: MCAsmBackend.h:55
virtual unsigned getNumFixupKinds() const =0
Get the number of target specific fixup kinds.
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual std::optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
virtual std::pair< bool, bool > relaxLEB128(const MCAssembler &Asm, MCLEBFragment &LF, int64_t &Value) const
Definition: MCAsmBackend.h:197
virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, MutableArrayRef< char > Data, uint64_t Value, bool IsResolved, const MCSubtargetInfo *STI) const =0
Apply the Value for given Fixup into the provided data fragment, at the offset specified by the fixup...
virtual bool allowAutoPadding() const
Return true if this target might automatically pad instructions and thus need to emit padding enable/...
Definition: MCAsmBackend.h:60
virtual ~MCAsmBackend()
virtual uint64_t generateCompactUnwindEncoding(const MCDwarfFrameInfo *FI, const MCContext *Ctxt) const
Generate the compact unwind encoding for the CFI instructions.
Definition: MCAsmBackend.h:229
Context object for machine code objects.
Definition: MCContext.h:83
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:185
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:234
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
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:310
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:434
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
@ MaxFixupKind
Set limit to accommodate the highest reloc type in use for all Targets, currently R_AARCH64_IRELATIVE...
Definition: MCFixup.h:54
MCAssemblerFlag
Definition: MCDirectives.h:53
endianness
Definition: bit.h:70
Target independent information on a fixup kind.