LLVM 18.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 MCRelaxableFragment;
25class MCSymbol;
26class MCAsmLayout;
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 unsigned RelaxFixupKind = MaxFixupKind);
46
47public:
48 MCAsmBackend(const MCAsmBackend &) = delete;
50 virtual ~MCAsmBackend();
51
53
54 /// Fixup kind used for linker relaxation. Currently only used by RISC-V.
55 const unsigned RelaxFixupKind;
56
57 /// Return true if this target might automatically pad instructions and thus
58 /// need to emit padding enable/disable directives around sensative code.
59 virtual bool allowAutoPadding() const { return false; }
60 /// Return true if this target allows an unrelaxable instruction to be
61 /// emitted into RelaxableFragment and then we can increase its size in a
62 /// tricky way for optimization.
63 virtual bool allowEnhancedRelaxation() const { return false; }
64
65 /// Give the target a chance to manipulate state related to instruction
66 /// alignment (e.g. padding for optimization), instruction relaxablility, etc.
67 /// before and after actually emitting the instruction.
68 virtual void emitInstructionBegin(MCObjectStreamer &OS, const MCInst &Inst,
69 const MCSubtargetInfo &STI) {}
70 virtual void emitInstructionEnd(MCObjectStreamer &OS, const MCInst &Inst) {}
71
72 /// lifetime management
73 virtual void reset() {}
74
75 /// Create a new MCObjectWriter instance for use by the assembler backend to
76 /// emit the final object file.
77 std::unique_ptr<MCObjectWriter>
79
80 /// Create an MCObjectWriter that writes two object files: a .o file which is
81 /// linked into the final program and a .dwo file which is used by debuggers.
82 /// This function is only supported with ELF targets.
83 std::unique_ptr<MCObjectWriter>
85
86 virtual std::unique_ptr<MCObjectTargetWriter>
88
89 /// \name Target Fixup Interfaces
90 /// @{
91
92 /// Get the number of target specific fixup kinds.
93 virtual unsigned getNumFixupKinds() const = 0;
94
95 /// Map a relocation name used in .reloc to a fixup kind.
96 virtual std::optional<MCFixupKind> getFixupKind(StringRef Name) const;
97
98 /// Get information on a fixup kind.
99 virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;
100
101 /// Hook to check if a relocation is needed for some target specific reason.
102 virtual bool shouldForceRelocation(const MCAssembler &Asm,
103 const MCFixup &Fixup,
104 const MCValue &Target) {
105 return false;
106 }
107
108 /// Hook to check if extra nop bytes must be inserted for alignment directive.
109 /// For some targets this may be necessary in order to support linker
110 /// relaxation. The number of bytes to insert are returned in Size.
112 unsigned &Size) {
113 return false;
114 }
115
116 /// Hook which indicates if the target requires a fixup to be generated when
117 /// handling an align directive in an executable section
119 const MCAsmLayout &Layout,
120 MCAlignFragment &AF) {
121 return false;
122 }
123
124 virtual bool evaluateTargetFixup(const MCAssembler &Asm,
125 const MCAsmLayout &Layout,
126 const MCFixup &Fixup, const MCFragment *DF,
127 const MCValue &Target, uint64_t &Value,
128 bool &WasForced) {
129 llvm_unreachable("Need to implement hook if target has custom fixups");
130 }
131
132 virtual bool handleAddSubRelocations(const MCAsmLayout &Layout,
133 const MCFragment &F,
134 const MCFixup &Fixup,
135 const MCValue &Target,
136 uint64_t &FixedValue) const {
137 return false;
138 }
139
140 /// Apply the \p Value for given \p Fixup into the provided data fragment, at
141 /// the offset specified by the fixup and following the fixup kind as
142 /// appropriate. Errors (such as an out of range fixup value) should be
143 /// reported via \p Ctx.
144 /// The \p STI is present only for fragments of type MCRelaxableFragment and
145 /// MCDataFragment with hasInstructions() == true.
146 virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
148 uint64_t Value, bool IsResolved,
149 const MCSubtargetInfo *STI) const = 0;
150
151 /// @}
152
153 /// \name Target Relaxation Interfaces
154 /// @{
155
156 /// Check whether the given instruction may need relaxation.
157 ///
158 /// \param Inst - The instruction to test.
159 /// \param STI - The MCSubtargetInfo in effect when the instruction was
160 /// encoded.
161 virtual bool mayNeedRelaxation(const MCInst &Inst,
162 const MCSubtargetInfo &STI) const {
163 return false;
164 }
165
166 /// Target specific predicate for whether a given fixup requires the
167 /// associated instruction to be relaxed.
168 virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
170 const MCRelaxableFragment *DF,
171 const MCAsmLayout &Layout,
172 const bool WasForced) const;
173
174 /// Simple predicate for targets where !Resolved implies requiring relaxation
176 const MCRelaxableFragment *DF,
177 const MCAsmLayout &Layout) const = 0;
178
179 /// Relax the instruction in the given fragment to the next wider instruction.
180 ///
181 /// \param [out] Inst The instruction to relax, which is also the relaxed
182 /// instruction.
183 /// \param STI the subtarget information for the associated instruction.
184 virtual void relaxInstruction(MCInst &Inst,
185 const MCSubtargetInfo &STI) const {};
186
188 MCAsmLayout &Layout, bool &WasRelaxed) const {
189 return false;
190 }
191
193 bool &WasRelaxed) const {
194 return false;
195 }
196
197 /// @}
198
199 /// Returns the minimum size of a nop in bytes on this target. The assembler
200 /// will use this to emit excess padding in situations where the padding
201 /// required for simple alignment would be less than the minimum nop size.
202 ///
203 virtual unsigned getMinimumNopSize() const { return 1; }
204
205 /// Returns the maximum size of a nop in bytes on this target.
206 ///
207 virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const {
208 return 0;
209 }
210
211 /// Write an (optimal) nop sequence of Count bytes to the given output. If the
212 /// target cannot generate such a sequence, it should return an error.
213 ///
214 /// \return - True on success.
215 virtual bool writeNopData(raw_ostream &OS, uint64_t Count,
216 const MCSubtargetInfo *STI) const = 0;
217
218 /// Give backend an opportunity to finish layout after relaxation
219 virtual void finishLayout(MCAssembler const &Asm,
220 MCAsmLayout &Layout) const {}
221
222 /// Handle any target-specific assembler flags. By default, do nothing.
224
225 /// Generate the compact unwind encoding for the CFI instructions.
227 const MCContext *Ctxt) const {
228 return 0;
229 }
230
231 /// Check whether a given symbol has been flagged with MICROMIPS flag.
232 virtual bool isMicroMips(const MCSymbol *Sym) const {
233 return false;
234 }
235
236 bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const;
237};
238
239} // end namespace llvm
240
241#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:468
#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
virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout, const bool WasForced) const
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
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:203
MCAsmBackend(const MCAsmBackend &)=delete
virtual bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF, MCAsmLayout &Layout, bool &WasRelaxed) const
Definition: MCAsmBackend.h:187
virtual bool evaluateTargetFixup(const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFixup &Fixup, const MCFragment *DF, const MCValue &Target, uint64_t &Value, bool &WasForced)
Definition: MCAsmBackend.h:124
virtual bool handleAddSubRelocations(const MCAsmLayout &Layout, const MCFragment &F, const MCFixup &Fixup, const MCValue &Target, uint64_t &FixedValue) const
Definition: MCAsmBackend.h:132
virtual bool shouldInsertFixupForCodeAlign(MCAssembler &Asm, const MCAsmLayout &Layout, MCAlignFragment &AF)
Hook which indicates if the target requires a fixup to be generated when handling an align directive ...
Definition: MCAsmBackend.h:118
virtual bool allowEnhancedRelaxation() const
Return true if this target allows an unrelaxable instruction to be emitted into RelaxableFragment and...
Definition: MCAsmBackend.h:63
virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const
Returns the maximum size of a nop in bytes on this target.
Definition: MCAsmBackend.h:207
virtual void handleAssemblerFlag(MCAssemblerFlag Flag)
Handle any target-specific assembler flags. By default, do nothing.
Definition: MCAsmBackend.h:223
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:184
virtual bool mayNeedRelaxation(const MCInst &Inst, const MCSubtargetInfo &STI) const
Check whether the given instruction may need relaxation.
Definition: MCAsmBackend.h:161
virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout) const =0
Simple predicate for targets where !Resolved implies requiring relaxation.
virtual bool isMicroMips(const MCSymbol *Sym) const
Check whether a given symbol has been flagged with MICROMIPS flag.
Definition: MCAsmBackend.h:232
virtual void finishLayout(MCAssembler const &Asm, MCAsmLayout &Layout) const
Give backend an opportunity to finish layout after relaxation.
Definition: MCAsmBackend.h:219
virtual bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target)
Hook to check if a relocation is needed for some target specific reason.
Definition: MCAsmBackend.h:102
virtual bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF, unsigned &Size)
Hook to check if extra nop bytes must be inserted for alignment directive.
Definition: MCAsmBackend.h:111
MCAsmBackend & operator=(const MCAsmBackend &)=delete
virtual bool relaxDwarfCFA(MCDwarfCallFrameFragment &DF, MCAsmLayout &Layout, bool &WasRelaxed) const
Definition: MCAsmBackend.h:192
bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const
virtual void emitInstructionEnd(MCObjectStreamer &OS, const MCInst &Inst)
Definition: MCAsmBackend.h:70
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
virtual void emitInstructionBegin(MCObjectStreamer &OS, const MCInst &Inst, const MCSubtargetInfo &STI)
Give the target a chance to manipulate state related to instruction alignment (e.g.
Definition: MCAsmBackend.h:68
virtual void reset()
lifetime management
Definition: MCAsmBackend.h:73
const unsigned RelaxFixupKind
Fixup kind used for linker relaxation. Currently only used by RISC-V.
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.
const support::endianness Endian
Definition: MCAsmBackend.h:52
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:59
virtual ~MCAsmBackend()
virtual uint32_t generateCompactUnwindEncoding(const MCDwarfFrameInfo *FI, const MCContext *Ctxt) const
Generate the compact unwind encoding for the CFI instructions.
Definition: MCAsmBackend.h:226
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
Context object for machine code objects.
Definition: MCContext.h:76
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:70
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Streaming object file generation interface.
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:274
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:307
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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:428
#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:53
MCAssemblerFlag
Definition: MCDirectives.h:53
Target independent information on a fixup kind.