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;
29class MCObjectWriter;
30class MCOperand;
31class MCSubtargetInfo;
32class MCValue;
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;
63 bool AllowBundling = false;
64
65public:
66 MCAsmBackend(const MCAsmBackend &) = delete;
68 virtual ~MCAsmBackend();
69
71
73
74 MCContext &getContext() const;
75
76 /// Return true if this target might automatically pad instructions and thus
77 /// need to emit padding enable/disable directives around sensative code.
78 bool allowAutoPadding() const { return AllowAutoPadding; }
79 /// Return true if this target allows an unrelaxable instruction to be
80 /// emitted into RelaxableFragment and then we can increase its size in a
81 /// tricky way for optimization.
83 /// Return true if this target implements `.bundle_align_mode`. Other targets
84 /// reject the directive instead of emitting unbundled code.
85 bool allowBundling() const { return AllowBundling; }
86
87 /// lifetime management
88 virtual void reset() {}
89
90 /// Create a new MCObjectWriter instance for use by the assembler backend to
91 /// emit the final object file.
92 std::unique_ptr<MCObjectWriter>
93 createObjectWriter(raw_pwrite_stream &OS) const;
94
95 /// Create an MCObjectWriter that writes two object files: a .o file which is
96 /// linked into the final program and a .dwo file which is used by debuggers.
97 /// This function is only supported with ELF targets.
98 std::unique_ptr<MCObjectWriter>
99 createDwoObjectWriter(raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS) const;
100
101 virtual std::unique_ptr<MCObjectTargetWriter>
103
104 /// \name Target Fixup Interfaces
105 /// @{
106
107 /// Map a relocation name used in .reloc to a fixup kind.
108 virtual std::optional<MCFixupKind> getFixupKind(StringRef Name) const;
109
110 /// Get information on a fixup kind.
111 virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const;
112
113 // Evaluate a fixup, returning std::nullopt to use default handling for
114 // `Value` and `IsResolved`. Otherwise, returns `IsResolved` with the
115 // expectation that the hook updates `Value`.
116 virtual std::optional<bool> evaluateFixup(const MCFragment &, MCFixup &,
117 MCValue &, uint64_t &) {
118 return {};
119 }
120
121 void maybeAddReloc(const MCFragment &, const MCFixup &, const MCValue &,
122 uint64_t &Value, bool IsResolved);
123
124 // Determine if a relocation is required. In addition, apply `Value` to the
125 // `Data` fragment at the specified fixup offset if applicable. `Data` points
126 // to the first byte of the fixup offset, which may be at the content's end if
127 // the fixup is zero-sized.
128 virtual void applyFixup(const MCFragment &, const MCFixup &,
130 bool IsResolved) = 0;
131
132 /// @}
133
134 /// \name Target Relaxation Interfaces
135 /// @{
136
137 /// Check whether the given instruction (encoded as Opcode+Operands) may need
138 /// relaxation.
139 virtual bool mayNeedRelaxation(unsigned Opcode, ArrayRef<MCOperand> Operands,
140 const MCSubtargetInfo &STI) const {
141 return false;
142 }
143
144 /// Target specific predicate for whether a given fixup requires the
145 /// associated instruction to be relaxed.
146 virtual bool fixupNeedsRelaxationAdvanced(const MCFragment &, const MCFixup &,
147 const MCValue &, uint64_t,
148 bool Resolved) const;
149
150 /// Relax the instruction in the given fragment to the next wider instruction.
151 ///
152 /// \param [out] Inst The instruction to relax, which is also the relaxed
153 /// instruction.
154 /// \param STI the subtarget information for the associated instruction.
155 virtual void relaxInstruction(MCInst &Inst,
156 const MCSubtargetInfo &STI) const {
157 llvm_unreachable("Needed if fixupNeedsRelaxationAdvanced may return true");
158 }
159
160 // Defined by linker relaxation targets.
161
162 // Return false to use default handling. Otherwise, set `Size` to the number
163 // of padding bytes.
164 virtual bool relaxAlign(MCFragment &F, unsigned &Size) { return false; }
165 virtual bool relaxDwarfLineAddr(MCFragment &) const { return false; }
166 virtual bool relaxDwarfCFA(MCFragment &) const { return false; }
167 virtual bool relaxSFrameCFA(MCFragment &) const { return false; }
168
169 // Defined by linker relaxation targets to possibly emit LEB128 relocations
170 // and set Value at the relocated location.
171 virtual std::pair<bool, bool> relaxLEB128(MCFragment &,
172 int64_t &Value) const {
173 return std::make_pair(false, false);
174 }
175
176 /// @}
177
178 /// Returns the minimum size of a nop in bytes on this target. The assembler
179 /// will use this to emit excess padding in situations where the padding
180 /// required for simple alignment would be less than the minimum nop size.
181 ///
182 virtual unsigned getMinimumNopSize() const { return 1; }
183
184 /// Returns the maximum size of a nop in bytes on this target.
185 ///
186 virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const {
187 return 0;
188 }
189
190 /// Write an (optimal) nop sequence of Count bytes to the given output. If the
191 /// target cannot generate such a sequence, it should return an error.
192 ///
193 /// \return - True on success.
195 const MCSubtargetInfo *STI) const = 0;
196
197 // Return true if fragment offsets have been adjusted and an extra layout
198 // iteration is needed.
199 virtual bool finishLayout() const { return false; }
200
201 /// Generate the compact unwind encoding for the CFI instructions.
203 const MCContext *Ctxt) const {
204 return 0;
205 }
206
207 bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const;
208
209 // Return STI for fragments with hasInstructions() == true.
210 static const MCSubtargetInfo *getSubtargetInfo(const MCFragment &F);
211};
212
213} // end namespace llvm
214
215#endif // LLVM_MC_MCASMBACKEND_H
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition Compiler.h:215
#define F(x, y, z)
Definition MD5.cpp:54
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: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
Streaming object file generation interface.
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:40
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.