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 uint64_t Value,
100 const MCSubtargetInfo *STI) {
101 return false;
102 }
103
104 /// Hook to check if extra nop bytes must be inserted for alignment directive.
105 /// For some targets this may be necessary in order to support linker
106 /// relaxation. The number of bytes to insert are returned in Size.
108 unsigned &Size) {
109 return false;
110 }
111
112 /// Hook which indicates if the target requires a fixup to be generated when
113 /// handling an align directive in an executable section
115 MCAlignFragment &AF) {
116 return false;
117 }
118
119 virtual bool evaluateTargetFixup(const MCAssembler &Asm,
120 const MCFixup &Fixup, const MCFragment *DF,
121 const MCValue &Target,
122 const MCSubtargetInfo *STI, uint64_t &Value,
123 bool &WasForced) {
124 llvm_unreachable("Need to implement hook if target has custom fixups");
125 }
126
127 virtual bool handleAddSubRelocations(const MCAssembler &Asm,
128 const MCFragment &F,
129 const MCFixup &Fixup,
130 const MCValue &Target,
131 uint64_t &FixedValue) const {
132 return false;
133 }
134
135 /// Apply the \p Value for given \p Fixup into the provided data fragment, at
136 /// the offset specified by the fixup and following the fixup kind as
137 /// appropriate. Errors (such as an out of range fixup value) should be
138 /// reported via \p Ctx.
139 /// The \p STI is present only for fragments of type MCRelaxableFragment and
140 /// MCDataFragment with hasInstructions() == true.
141 virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
143 uint64_t Value, bool IsResolved,
144 const MCSubtargetInfo *STI) const = 0;
145
146 /// @}
147
148 /// \name Target Relaxation Interfaces
149 /// @{
150
151 /// Check whether the given instruction may need relaxation.
152 ///
153 /// \param Inst - The instruction to test.
154 /// \param STI - The MCSubtargetInfo in effect when the instruction was
155 /// encoded.
156 virtual bool mayNeedRelaxation(const MCInst &Inst,
157 const MCSubtargetInfo &STI) const {
158 return false;
159 }
160
161 /// Target specific predicate for whether a given fixup requires the
162 /// associated instruction to be relaxed.
163 virtual bool fixupNeedsRelaxationAdvanced(const MCAssembler &Asm,
164 const MCFixup &Fixup, bool Resolved,
166 const MCRelaxableFragment *DF,
167 const bool WasForced) const;
168
169 /// Simple predicate for targets where !Resolved implies requiring relaxation
170 virtual bool fixupNeedsRelaxation(const MCFixup &Fixup,
171 uint64_t Value) const {
172 llvm_unreachable("Needed if mayNeedRelaxation may return true");
173 }
174
175 /// Relax the instruction in the given fragment to the next wider instruction.
176 ///
177 /// \param [out] Inst The instruction to relax, which is also the relaxed
178 /// instruction.
179 /// \param STI the subtarget information for the associated instruction.
180 virtual void relaxInstruction(MCInst &Inst,
181 const MCSubtargetInfo &STI) const {};
182
183 virtual bool relaxDwarfLineAddr(const MCAssembler &Asm,
185 bool &WasRelaxed) const {
186 return false;
187 }
188
189 virtual bool relaxDwarfCFA(const MCAssembler &Asm,
191 bool &WasRelaxed) const {
192 return false;
193 }
194
195 // Defined by linker relaxation targets to possibly emit LEB128 relocations
196 // and set Value at the relocated location.
197 virtual std::pair<bool, bool>
198 relaxLEB128(const MCAssembler &Asm, MCLEBFragment &LF, int64_t &Value) const {
199 return std::make_pair(false, false);
200 }
201
202 /// @}
203
204 /// Returns the minimum size of a nop in bytes on this target. The assembler
205 /// will use this to emit excess padding in situations where the padding
206 /// required for simple alignment would be less than the minimum nop size.
207 ///
208 virtual unsigned getMinimumNopSize() const { return 1; }
209
210 /// Returns the maximum size of a nop in bytes on this target.
211 ///
212 virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const {
213 return 0;
214 }
215
216 /// Write an (optimal) nop sequence of Count bytes to the given output. If the
217 /// target cannot generate such a sequence, it should return an error.
218 ///
219 /// \return - True on success.
220 virtual bool writeNopData(raw_ostream &OS, uint64_t Count,
221 const MCSubtargetInfo *STI) const = 0;
222
223 /// Give backend an opportunity to finish layout after relaxation
224 virtual void finishLayout(MCAssembler const &Asm) const {}
225
226 /// Handle any target-specific assembler flags. By default, do nothing.
228
229 /// Generate the compact unwind encoding for the CFI instructions.
231 const MCContext *Ctxt) const {
232 return 0;
233 }
234
235 /// Check whether a given symbol has been flagged with MICROMIPS flag.
236 virtual bool isMicroMips(const MCSymbol *Sym) const {
237 return false;
238 }
239
240 bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const;
241};
242
243} // end namespace llvm
244
245#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:208
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:212
virtual void handleAssemblerFlag(MCAssemblerFlag Flag)
Handle any target-specific assembler flags. By default, do nothing.
Definition: MCAsmBackend.h:227
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:180
virtual bool mayNeedRelaxation(const MCInst &Inst, const MCSubtargetInfo &STI) const
Check whether the given instruction may need relaxation.
Definition: MCAsmBackend.h:156
virtual bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, const uint64_t Value, const MCSubtargetInfo *STI)
Hook to check if a relocation is needed for some target specific reason.
Definition: MCAsmBackend.h:96
virtual bool isMicroMips(const MCSymbol *Sym) const
Check whether a given symbol has been flagged with MICROMIPS flag.
Definition: MCAsmBackend.h:236
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:114
virtual bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF, unsigned &Size)
Hook to check if extra nop bytes must be inserted for alignment directive.
Definition: MCAsmBackend.h:107
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:170
virtual bool relaxDwarfCFA(const MCAssembler &Asm, MCDwarfCallFrameFragment &DF, bool &WasRelaxed) const
Definition: MCAsmBackend.h:189
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:183
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:224
virtual bool handleAddSubRelocations(const MCAssembler &Asm, const MCFragment &F, const MCFixup &Fixup, const MCValue &Target, uint64_t &FixedValue) const
Definition: MCAsmBackend.h:127
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:119
virtual void reset()
lifetime management
Definition: MCAsmBackend.h:67
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:198
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:230
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:228
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.