LLVM 22.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"
16#include "llvm/Support/Endian.h"
17#include <cstdint>
18
19namespace llvm {
20
21class MCFragment;
22class MCSymbol;
23class MCAssembler;
24class MCContext;
25struct MCDwarfFrameInfo;
26class MCInst;
27class MCObjectStreamer;
28class MCObjectTargetWriter;
29class MCObjectWriter;
30class MCOperand;
31class MCSubtargetInfo;
32class MCValue;
33class raw_pwrite_stream;
34class StringRef;
35class raw_ostream;
36
37/// Target independent information on a fixup kind.
39 /// A target specific name for the fixup kind. The names will be unique for
40 /// distinct kinds on any given target.
41 const char *Name;
42
43 /// The bit offset to write the relocation into.
45
46 /// The number of bits written by this fixup. The bits are assumed to be
47 /// contiguous.
49
50 /// Flags describing additional information on this fixup kind.
51 unsigned Flags;
52};
53
54/// Generic interface to target specific assembler backends.
56protected: // Can only create subclasses.
58
59 MCAssembler *Asm = nullptr;
60
61 bool AllowAutoPadding = false;
62 bool AllowEnhancedRelaxation = false;
63
64public:
65 MCAsmBackend(const MCAsmBackend &) = delete;
67 virtual ~MCAsmBackend();
68
70
71 void setAssembler(MCAssembler *A) { Asm = A; }
72
73 MCContext &getContext() const;
74
75 /// Return true if this target might automatically pad instructions and thus
76 /// need to emit padding enable/disable directives around sensative code.
77 bool allowAutoPadding() const { return AllowAutoPadding; }
78 /// Return true if this target allows an unrelaxable instruction to be
79 /// emitted into RelaxableFragment and then we can increase its size in a
80 /// tricky way for optimization.
81 bool allowEnhancedRelaxation() const { return AllowEnhancedRelaxation; }
82
83 /// lifetime management
84 virtual void reset() {}
85
86 /// Create a new MCObjectWriter instance for use by the assembler backend to
87 /// emit the final object file.
88 std::unique_ptr<MCObjectWriter>
89 createObjectWriter(raw_pwrite_stream &OS) const;
90
91 /// Create an MCObjectWriter that writes two object files: a .o file which is
92 /// linked into the final program and a .dwo file which is used by debuggers.
93 /// This function is only supported with ELF targets.
94 std::unique_ptr<MCObjectWriter>
95 createDwoObjectWriter(raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS) const;
96
97 virtual std::unique_ptr<MCObjectTargetWriter>
99
100 /// \name Target Fixup Interfaces
101 /// @{
102
103 /// Map a relocation name used in .reloc to a fixup kind.
104 virtual std::optional<MCFixupKind> getFixupKind(StringRef Name) const;
105
106 /// Get information on a fixup kind.
107 virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const;
108
109 // Evaluate a fixup, returning std::nullopt to use default handling for
110 // `Value` and `IsResolved`. Otherwise, returns `IsResolved` with the
111 // expectation that the hook updates `Value`.
112 virtual std::optional<bool> evaluateFixup(const MCFragment &, MCFixup &,
113 MCValue &, uint64_t &) {
114 return {};
115 }
116
117 void maybeAddReloc(const MCFragment &, const MCFixup &, const MCValue &,
118 uint64_t &Value, bool IsResolved);
119
120 // Determine if a relocation is required. In addition, apply `Value` to the
121 // `Data` fragment at the specified fixup offset if applicable. `Data` points
122 // to the first byte of the fixup offset, which may be at the content's end if
123 // the fixup is zero-sized.
124 virtual void applyFixup(const MCFragment &, const MCFixup &,
125 const MCValue &Target, uint8_t *Data, uint64_t Value,
126 bool IsResolved) = 0;
127
128 /// @}
129
130 /// \name Target Relaxation Interfaces
131 /// @{
132
133 /// Check whether the given instruction (encoded as Opcode+Operands) may need
134 /// relaxation.
135 virtual bool mayNeedRelaxation(unsigned Opcode, ArrayRef<MCOperand> Operands,
136 const MCSubtargetInfo &STI) const {
137 return false;
138 }
139
140 /// Target specific predicate for whether a given fixup requires the
141 /// associated instruction to be relaxed.
142 virtual bool fixupNeedsRelaxationAdvanced(const MCFragment &, const MCFixup &,
143 const MCValue &, uint64_t,
144 bool Resolved) const;
145
146 /// Simple predicate for targets where !Resolved implies requiring relaxation
147 virtual bool fixupNeedsRelaxation(const MCFixup &Fixup,
148 uint64_t Value) const {
149 llvm_unreachable("Needed if mayNeedRelaxation may return true");
150 }
151
152 /// Relax the instruction in the given fragment to the next wider instruction.
153 ///
154 /// \param [out] Inst The instruction to relax, which is also the relaxed
155 /// instruction.
156 /// \param STI the subtarget information for the associated instruction.
157 virtual void relaxInstruction(MCInst &Inst,
158 const MCSubtargetInfo &STI) const {
160 "Needed if fixupNeedsRelaxation/fixupNeedsRelaxationAdvanced may "
161 "return true");
162 }
163
164 // Defined by linker relaxation targets.
165
166 // Return false to use default handling. Otherwise, set `Size` to the number
167 // of padding bytes.
168 virtual bool relaxAlign(MCFragment &F, unsigned &Size) { return false; }
169 virtual bool relaxDwarfLineAddr(MCFragment &) const { return false; }
170 virtual bool relaxDwarfCFA(MCFragment &) const { return false; }
171
172 // Defined by linker relaxation targets to possibly emit LEB128 relocations
173 // and set Value at the relocated location.
174 virtual std::pair<bool, bool> relaxLEB128(MCFragment &,
175 int64_t &Value) const {
176 return std::make_pair(false, false);
177 }
178
179 /// @}
180
181 /// Returns the minimum size of a nop in bytes on this target. The assembler
182 /// will use this to emit excess padding in situations where the padding
183 /// required for simple alignment would be less than the minimum nop size.
184 ///
185 virtual unsigned getMinimumNopSize() const { return 1; }
186
187 /// Returns the maximum size of a nop in bytes on this target.
188 ///
189 virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const {
190 return 0;
191 }
192
193 /// Write an (optimal) nop sequence of Count bytes to the given output. If the
194 /// target cannot generate such a sequence, it should return an error.
195 ///
196 /// \return - True on success.
197 virtual bool writeNopData(raw_ostream &OS, uint64_t Count,
198 const MCSubtargetInfo *STI) const = 0;
199
200 // Return true if fragment offsets have been adjusted and an extra layout
201 // iteration is needed.
202 virtual bool finishLayout(const MCAssembler &Asm) const { return false; }
203
204 /// Generate the compact unwind encoding for the CFI instructions.
206 const MCContext *Ctxt) const {
207 return 0;
208 }
209
210 bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const;
211
212 // Return STI for fragments with hasInstructions() == true.
213 static const MCSubtargetInfo *getSubtargetInfo(const MCFragment &F);
214};
215
216} // end namespace llvm
217
218#endif // LLVM_MC_MCASMBACKEND_H
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition: Compiler.h:213
std::string Name
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define F(x, y, z)
Definition: MD5.cpp:55
mir Rename Register Operands
PowerPC TLS Dynamic Call Fixup
endianness Endian
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:55
virtual unsigned getMinimumNopSize() const
Returns the minimum size of a nop in bytes on this target.
Definition: MCAsmBackend.h:185
MCAsmBackend(const MCAsmBackend &)=delete
const llvm::endianness Endian
Definition: MCAsmBackend.h:69
bool allowEnhancedRelaxation() const
Return true if this target allows an unrelaxable instruction to be emitted into RelaxableFragment and...
Definition: MCAsmBackend.h:81
bool allowAutoPadding() const
Return true if this target might automatically pad instructions and thus need to emit padding enable/...
Definition: MCAsmBackend.h:77
virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const
Returns the maximum size of a nop in bytes on this target.
Definition: MCAsmBackend.h:189
virtual bool finishLayout(const MCAssembler &Asm) const
Definition: MCAsmBackend.h:202
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:157
MCAsmBackend(llvm::endianness Endian)
Definition: MCAsmBackend.h:57
virtual bool relaxAlign(MCFragment &F, unsigned &Size)
Definition: MCAsmBackend.h:168
virtual bool mayNeedRelaxation(unsigned Opcode, ArrayRef< MCOperand > Operands, const MCSubtargetInfo &STI) const
Check whether the given instruction (encoded as Opcode+Operands) may need relaxation.
Definition: MCAsmBackend.h:135
virtual std::pair< bool, bool > relaxLEB128(MCFragment &, int64_t &Value) const
Definition: MCAsmBackend.h:174
virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value) const
Simple predicate for targets where !Resolved implies requiring relaxation.
Definition: MCAsmBackend.h:147
MCAsmBackend & operator=(const MCAsmBackend &)=delete
virtual bool relaxDwarfCFA(MCFragment &) const
Definition: MCAsmBackend.h:170
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
virtual void reset()
lifetime management
Definition: MCAsmBackend.h:84
void setAssembler(MCAssembler *A)
Definition: MCAsmBackend.h:71
virtual void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target, uint8_t *Data, uint64_t Value, bool IsResolved)=0
virtual ~MCAsmBackend()
virtual std::optional< bool > evaluateFixup(const MCFragment &, MCFixup &, MCValue &, uint64_t &)
Definition: MCAsmBackend.h:112
virtual uint64_t generateCompactUnwindEncoding(const MCDwarfFrameInfo *FI, const MCContext *Ctxt) const
Generate the compact unwind encoding for the CFI instructions.
Definition: MCAsmBackend.h:205
virtual bool relaxDwarfLineAddr(MCFragment &) const
Definition: MCAsmBackend.h:169
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:61
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:188
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:42
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition: Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:435
#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
endianness
Definition: bit.h:71
Target independent information on a fixup kind.
Definition: MCAsmBackend.h:38
const char * Name
A target specific name for the fixup kind.
Definition: MCAsmBackend.h:41
uint8_t TargetOffset
The bit offset to write the relocation into.
Definition: MCAsmBackend.h:44
uint8_t TargetSize
The number of bits written by this fixup.
Definition: MCAsmBackend.h:48
unsigned Flags
Flags describing additional information on this fixup kind.
Definition: MCAsmBackend.h:51