LLVM 17.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 MCCFIInstruction;
29struct MCFixupKindInfo;
30class MCInst;
31class MCObjectStreamer;
32class MCObjectTargetWriter;
33class MCObjectWriter;
34class MCSubtargetInfo;
35class MCValue;
36class raw_pwrite_stream;
37class StringRef;
38class raw_ostream;
39
40/// Generic interface to target specific assembler backends.
42protected: // Can only create subclasses.
44
45public:
46 MCAsmBackend(const MCAsmBackend &) = delete;
48 virtual ~MCAsmBackend();
49
51
52 /// Return true if this target might automatically pad instructions and thus
53 /// need to emit padding enable/disable directives around sensative code.
54 virtual bool allowAutoPadding() const { return false; }
55 /// Return true if this target allows an unrelaxable instruction to be
56 /// emitted into RelaxableFragment and then we can increase its size in a
57 /// tricky way for optimization.
58 virtual bool allowEnhancedRelaxation() const { return false; }
59
60 /// Give the target a chance to manipulate state related to instruction
61 /// alignment (e.g. padding for optimization), instruction relaxablility, etc.
62 /// before and after actually emitting the instruction.
63 virtual void emitInstructionBegin(MCObjectStreamer &OS, const MCInst &Inst,
64 const MCSubtargetInfo &STI) {}
65 virtual void emitInstructionEnd(MCObjectStreamer &OS, const MCInst &Inst) {}
66
67 /// lifetime management
68 virtual void reset() {}
69
70 /// Create a new MCObjectWriter instance for use by the assembler backend to
71 /// emit the final object file.
72 std::unique_ptr<MCObjectWriter>
74
75 /// Create an MCObjectWriter that writes two object files: a .o file which is
76 /// linked into the final program and a .dwo file which is used by debuggers.
77 /// This function is only supported with ELF targets.
78 std::unique_ptr<MCObjectWriter>
80
81 virtual std::unique_ptr<MCObjectTargetWriter>
83
84 /// \name Target Fixup Interfaces
85 /// @{
86
87 /// Get the number of target specific fixup kinds.
88 virtual unsigned getNumFixupKinds() const = 0;
89
90 /// Map a relocation name used in .reloc to a fixup kind.
91 virtual std::optional<MCFixupKind> getFixupKind(StringRef Name) const;
92
93 /// Get information on a fixup kind.
94 virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;
95
96 /// Hook to check if a relocation is needed for some target specific reason.
97 virtual bool shouldForceRelocation(const MCAssembler &Asm,
98 const MCFixup &Fixup,
99 const MCValue &Target) {
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 const MCAsmLayout &Layout,
115 MCAlignFragment &AF) {
116 return false;
117 }
118
119 virtual bool evaluateTargetFixup(const MCAssembler &Asm,
120 const MCAsmLayout &Layout,
121 const MCFixup &Fixup, const MCFragment *DF,
122 const MCValue &Target, uint64_t &Value,
123 bool &WasForced) {
124 llvm_unreachable("Need to implement hook if target has custom fixups");
125 }
126
127 /// Apply the \p Value for given \p Fixup into the provided data fragment, at
128 /// the offset specified by the fixup and following the fixup kind as
129 /// appropriate. Errors (such as an out of range fixup value) should be
130 /// reported via \p Ctx.
131 /// The \p STI is present only for fragments of type MCRelaxableFragment and
132 /// MCDataFragment with hasInstructions() == true.
133 virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
135 uint64_t Value, bool IsResolved,
136 const MCSubtargetInfo *STI) const = 0;
137
138 /// @}
139
140 /// \name Target Relaxation Interfaces
141 /// @{
142
143 /// Check whether the given instruction may need relaxation.
144 ///
145 /// \param Inst - The instruction to test.
146 /// \param STI - The MCSubtargetInfo in effect when the instruction was
147 /// encoded.
148 virtual bool mayNeedRelaxation(const MCInst &Inst,
149 const MCSubtargetInfo &STI) const {
150 return false;
151 }
152
153 /// Target specific predicate for whether a given fixup requires the
154 /// associated instruction to be relaxed.
155 virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
157 const MCRelaxableFragment *DF,
158 const MCAsmLayout &Layout,
159 const bool WasForced) const;
160
161 /// Simple predicate for targets where !Resolved implies requiring relaxation
163 const MCRelaxableFragment *DF,
164 const MCAsmLayout &Layout) const = 0;
165
166 /// Relax the instruction in the given fragment to the next wider instruction.
167 ///
168 /// \param [out] Inst The instruction to relax, which is also the relaxed
169 /// instruction.
170 /// \param STI the subtarget information for the associated instruction.
171 virtual void relaxInstruction(MCInst &Inst,
172 const MCSubtargetInfo &STI) const {};
173
175 MCAsmLayout &Layout, bool &WasRelaxed) const {
176 return false;
177 }
178
180 bool &WasRelaxed) const {
181 return false;
182 }
183
184 /// @}
185
186 /// Returns the minimum size of a nop in bytes on this target. The assembler
187 /// will use this to emit excess padding in situations where the padding
188 /// required for simple alignment would be less than the minimum nop size.
189 ///
190 virtual unsigned getMinimumNopSize() const { return 1; }
191
192 /// Returns the maximum size of a nop in bytes on this target.
193 ///
194 virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const {
195 return 0;
196 }
197
198 /// Write an (optimal) nop sequence of Count bytes to the given output. If the
199 /// target cannot generate such a sequence, it should return an error.
200 ///
201 /// \return - True on success.
202 virtual bool writeNopData(raw_ostream &OS, uint64_t Count,
203 const MCSubtargetInfo *STI) const = 0;
204
205 /// Give backend an opportunity to finish layout after relaxation
206 virtual void finishLayout(MCAssembler const &Asm,
207 MCAsmLayout &Layout) const {}
208
209 /// Handle any target-specific assembler flags. By default, do nothing.
211
212 /// Generate the compact unwind encoding for the CFI instructions.
213 virtual uint32_t
215 return 0;
216 }
217
218 /// Check whether a given symbol has been flagged with MICROMIPS flag.
219 virtual bool isMicroMips(const MCSymbol *Sym) const {
220 return false;
221 }
222};
223
224} // end namespace llvm
225
226#endif // LLVM_MC_MCASMBACKEND_H
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
std::string Name
uint64_t Size
PowerPC TLS Dynamic Call Fixup
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:41
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:190
MCAsmBackend(const MCAsmBackend &)=delete
virtual bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF, MCAsmLayout &Layout, bool &WasRelaxed) const
Definition: MCAsmBackend.h:174
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:119
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:113
virtual bool allowEnhancedRelaxation() const
Return true if this target allows an unrelaxable instruction to be emitted into RelaxableFragment and...
Definition: MCAsmBackend.h:58
virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const
Returns the maximum size of a nop in bytes on this target.
Definition: MCAsmBackend.h:194
virtual void handleAssemblerFlag(MCAssemblerFlag Flag)
Handle any target-specific assembler flags. By default, do nothing.
Definition: MCAsmBackend.h:210
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:171
virtual bool mayNeedRelaxation(const MCInst &Inst, const MCSubtargetInfo &STI) const
Check whether the given instruction may need relaxation.
Definition: MCAsmBackend.h:148
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:219
virtual void finishLayout(MCAssembler const &Asm, MCAsmLayout &Layout) const
Give backend an opportunity to finish layout after relaxation.
Definition: MCAsmBackend.h:206
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:97
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
MCAsmBackend & operator=(const MCAsmBackend &)=delete
virtual bool relaxDwarfCFA(MCDwarfCallFrameFragment &DF, MCAsmLayout &Layout, bool &WasRelaxed) const
Definition: MCAsmBackend.h:179
virtual void emitInstructionEnd(MCObjectStreamer &OS, const MCInst &Inst)
Definition: MCAsmBackend.h:65
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:63
virtual void reset()
lifetime management
Definition: MCAsmBackend.h:68
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:50
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 uint32_t generateCompactUnwindEncoding(ArrayRef< MCCFIInstruction >) const
Generate the compact unwind encoding for the CFI instructions.
Definition: MCAsmBackend.h:214
virtual bool allowAutoPadding() const
Return true if this target might automatically pad instructions and thus need to emit padding enable/...
Definition: MCAsmBackend.h:54
virtual ~MCAsmBackend()
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
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: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:270
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:305
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
MCAssemblerFlag
Definition: MCDirectives.h:52
Target independent information on a fixup kind.