LLVM 23.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
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
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 &,
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 /// Relax the instruction in the given fragment to the next wider instruction.
147 ///
148 /// \param [out] Inst The instruction to relax, which is also the relaxed
149 /// instruction.
150 /// \param STI the subtarget information for the associated instruction.
151 virtual void relaxInstruction(MCInst &Inst,
152 const MCSubtargetInfo &STI) const {
153 llvm_unreachable("Needed if fixupNeedsRelaxationAdvanced may return true");
154 }
155
156 // Defined by linker relaxation targets.
157
158 // Return false to use default handling. Otherwise, set `Size` to the number
159 // of padding bytes.
160 virtual bool relaxAlign(MCFragment &F, unsigned &Size) { return false; }
161 virtual bool relaxDwarfLineAddr(MCFragment &) const { return false; }
162 virtual bool relaxDwarfCFA(MCFragment &) const { return false; }
163 virtual bool relaxSFrameCFA(MCFragment &) const { return false; }
164
165 // Defined by linker relaxation targets to possibly emit LEB128 relocations
166 // and set Value at the relocated location.
167 virtual std::pair<bool, bool> relaxLEB128(MCFragment &,
168 int64_t &Value) const {
169 return std::make_pair(false, false);
170 }
171
172 /// @}
173
174 /// Returns the minimum size of a nop in bytes on this target. The assembler
175 /// will use this to emit excess padding in situations where the padding
176 /// required for simple alignment would be less than the minimum nop size.
177 ///
178 virtual unsigned getMinimumNopSize() const { return 1; }
179
180 /// Returns the maximum size of a nop in bytes on this target.
181 ///
182 virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const {
183 return 0;
184 }
185
186 /// Write an (optimal) nop sequence of Count bytes to the given output. If the
187 /// target cannot generate such a sequence, it should return an error.
188 ///
189 /// \return - True on success.
191 const MCSubtargetInfo *STI) const = 0;
192
193 // Return true if fragment offsets have been adjusted and an extra layout
194 // iteration is needed.
195 virtual bool finishLayout() const { return false; }
196
197 /// Generate the compact unwind encoding for the CFI instructions.
199 const MCContext *Ctxt) const {
200 return 0;
201 }
202
203 bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const;
204
205 // Return STI for fragments with hasInstructions() == true.
206 static const MCSubtargetInfo *getSubtargetInfo(const MCFragment &F);
207};
208
209} // end namespace llvm
210
211#endif // LLVM_MC_MCASMBACKEND_H
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition Compiler.h:213
#define F(x, y, z)
Definition MD5.cpp:54
ArrayRef - 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
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
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.
#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 Types.h:26
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
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.