LLVM 24.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;
28class MCObjectWriter;
29class MCOperand;
30class MCSubtargetInfo;
31class MCValue;
33class StringRef;
34class raw_ostream;
35
36/// Target independent information on a fixup kind.
38 /// A target specific name for the fixup kind. The names will be unique for
39 /// distinct kinds on any given target.
40 const char *Name;
41
42 /// The bit offset to write the relocation into.
44
45 /// The number of bits written by this fixup. The bits are assumed to be
46 /// contiguous.
48
49 /// Flags describing additional information on this fixup kind.
50 unsigned Flags;
51};
52
53/// Generic interface to target specific assembler backends.
55protected: // Can only create subclasses.
57
58 MCAssembler *Asm = nullptr;
59
60 bool AllowAutoPadding = false;
62 bool AllowBundling = false;
63
64public:
65 MCAsmBackend(const MCAsmBackend &) = delete;
67 virtual ~MCAsmBackend();
68
70
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.
82 /// Return true if this target implements `.bundle_align_mode`. Other targets
83 /// reject the directive instead of emitting unbundled code.
84 bool allowBundling() const { return AllowBundling; }
85
86 /// lifetime management
87 virtual void reset() {}
88
89 /// Create a new MCObjectWriter instance for use by the assembler backend to
90 /// emit the final object file.
91 std::unique_ptr<MCObjectWriter>
92 createObjectWriter(raw_pwrite_stream &OS) const;
93
94 /// Create an MCObjectWriter that writes two object files: a .o file which is
95 /// linked into the final program and a .dwo file which is used by debuggers.
96 /// This function is only supported with ELF targets.
97 std::unique_ptr<MCObjectWriter>
98 createDwoObjectWriter(raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS) const;
99
100 virtual std::unique_ptr<MCObjectTargetWriter>
102
103 /// \name Target Fixup Interfaces
104 /// @{
105
106 /// Map a relocation name used in .reloc to a fixup kind.
107 virtual std::optional<MCFixupKind> getFixupKind(StringRef Name) const;
108
109 /// Get information on a fixup kind.
110 virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const;
111
112 // Evaluate a fixup, returning std::nullopt to use default handling for
113 // `Value` and `IsResolved`. Otherwise, returns `IsResolved` with the
114 // expectation that the hook updates `Value`.
115 virtual std::optional<bool> evaluateFixup(const MCFragment &, MCFixup &,
116 MCValue &, uint64_t &) {
117 return {};
118 }
119
120 void maybeAddReloc(const MCFragment &, const MCFixup &, const MCValue &,
121 uint64_t &Value, bool IsResolved);
122
123 // Determine if a relocation is required. In addition, apply `Value` to the
124 // `Data` fragment at the specified fixup offset if applicable. `Data` points
125 // to the first byte of the fixup offset, which may be at the content's end if
126 // the fixup is zero-sized.
127 virtual void applyFixup(const MCFragment &, const MCFixup &,
129 bool IsResolved) = 0;
130
131 /// @}
132
133 /// \name Target Relaxation Interfaces
134 /// @{
135
136 /// Check whether the given instruction (encoded as Opcode+Operands) may need
137 /// relaxation.
138 virtual bool mayNeedRelaxation(unsigned Opcode, ArrayRef<MCOperand> Operands,
139 const MCSubtargetInfo &STI) const {
140 return false;
141 }
142
143 /// Target specific predicate for whether a given fixup requires the
144 /// associated instruction to be relaxed.
145 virtual bool fixupNeedsRelaxationAdvanced(const MCFragment &, const MCFixup &,
146 const MCValue &, uint64_t,
147 bool Resolved) const;
148
149 /// Relax the instruction in the given fragment to the next wider instruction.
150 ///
151 /// \param [out] Inst The instruction to relax, which is also the relaxed
152 /// instruction.
153 /// \param STI the subtarget information for the associated instruction.
154 virtual void relaxInstruction(MCInst &Inst,
155 const MCSubtargetInfo &STI) const {
156 llvm_unreachable("Needed if fixupNeedsRelaxationAdvanced may return true");
157 }
158
159 /// Relax the instruction in fragment F. Targets may override this to define
160 /// symbols relative to F.
161 virtual void relaxInstruction(MCFragment &F) const;
162
163 // Defined by linker relaxation targets.
164
165 // Return false to use default handling. Otherwise, set `Size` to the number
166 // of padding bytes.
167 virtual bool relaxAlign(MCFragment &F, unsigned &Size) { return false; }
168 virtual bool relaxDwarfLineAddr(MCFragment &) const { return false; }
169 virtual bool relaxDwarfCFA(MCFragment &) const { return false; }
170 virtual bool relaxSFrameCFA(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.
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 { 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
uint64_t uint64_t
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition Compiler.h:211
#define F(x, y, z)
Definition MD5.cpp:54
SI Fold Operands
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
virtual unsigned getMinimumNopSize() const
Returns the minimum size of a nop in bytes on this target.
MCAsmBackend(const MCAsmBackend &)=delete
const llvm::endianness Endian
bool allowEnhancedRelaxation() const
Return true if this target allows an unrelaxable instruction to be emitted into RelaxableFragment and...
bool allowAutoPadding() const
Return true if this target might automatically pad instructions and thus need to emit padding enable/...
virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const
Returns the maximum size of a nop in bytes on this target.
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.
MCAsmBackend(llvm::endianness Endian)
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual bool relaxAlign(MCFragment &F, unsigned &Size)
virtual bool mayNeedRelaxation(unsigned Opcode, ArrayRef< MCOperand > Operands, const MCSubtargetInfo &STI) const
Check whether the given instruction (encoded as Opcode+Operands) may need relaxation.
virtual std::pair< bool, bool > relaxLEB128(MCFragment &, int64_t &Value) const
virtual bool relaxSFrameCFA(MCFragment &) const
MCAsmBackend & operator=(const MCAsmBackend &)=delete
virtual bool relaxDwarfCFA(MCFragment &) const
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
virtual bool finishLayout() const
MCAssembler * Asm
bool allowBundling() const
Return true if this target implements .bundle_align_mode.
virtual void reset()
lifetime management
void setAssembler(MCAssembler *A)
virtual std::optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
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 &)
virtual uint64_t generateCompactUnwindEncoding(const MCDwarfFrameInfo *FI, const MCContext *Ctxt) const
Generate the compact unwind encoding for the CFI instructions.
virtual bool relaxDwarfLineAddr(MCFragment &) const
Context object for machine code objects.
Definition MCContext.h:84
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:65
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:187
Base class for classes that define behaviour that is specific to both the target and the object forma...
Defines the object file and target independent interfaces used by the assembler backend to write nati...
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:39
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
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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.
#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.
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
endianness
Definition bit.h:71
Target independent information on a fixup kind.
const char * Name
A target specific name for the fixup kind.
uint8_t TargetOffset
The bit offset to write the relocation into.
uint8_t TargetSize
The number of bits written by this fixup.
unsigned Flags
Flags describing additional information on this fixup kind.