File indexing completed on 2026-05-10 08:44:12
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_MC_MCASMBACKEND_H
0010 #define LLVM_MC_MCASMBACKEND_H
0011
0012 #include "llvm/ADT/ArrayRef.h"
0013 #include "llvm/MC/MCDirectives.h"
0014 #include "llvm/MC/MCFixup.h"
0015 #include "llvm/Support/Endian.h"
0016 #include <cstdint>
0017
0018 namespace llvm {
0019
0020 class MCAlignFragment;
0021 class MCDwarfCallFrameFragment;
0022 class MCDwarfLineAddrFragment;
0023 class MCFragment;
0024 class MCLEBFragment;
0025 class MCRelaxableFragment;
0026 class MCSymbol;
0027 class MCAssembler;
0028 class MCContext;
0029 struct MCDwarfFrameInfo;
0030 struct MCFixupKindInfo;
0031 class MCInst;
0032 class MCObjectStreamer;
0033 class MCObjectTargetWriter;
0034 class MCObjectWriter;
0035 class MCSubtargetInfo;
0036 class MCValue;
0037 class raw_pwrite_stream;
0038 class StringRef;
0039 class raw_ostream;
0040
0041
0042 class MCAsmBackend {
0043 protected:
0044 MCAsmBackend(llvm::endianness Endian, unsigned RelaxFixupKind = MaxFixupKind);
0045
0046 public:
0047 MCAsmBackend(const MCAsmBackend &) = delete;
0048 MCAsmBackend &operator=(const MCAsmBackend &) = delete;
0049 virtual ~MCAsmBackend();
0050
0051 const llvm::endianness Endian;
0052
0053
0054
0055 const unsigned RelaxFixupKind;
0056 bool allowLinkerRelaxation() const { return RelaxFixupKind != MaxFixupKind; }
0057
0058
0059
0060 virtual bool allowAutoPadding() const { return false; }
0061
0062
0063
0064 virtual bool allowEnhancedRelaxation() const { return false; }
0065
0066
0067 virtual void reset() {}
0068
0069
0070
0071 std::unique_ptr<MCObjectWriter>
0072 createObjectWriter(raw_pwrite_stream &OS) const;
0073
0074
0075
0076
0077 std::unique_ptr<MCObjectWriter>
0078 createDwoObjectWriter(raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS) const;
0079
0080 virtual std::unique_ptr<MCObjectTargetWriter>
0081 createObjectTargetWriter() const = 0;
0082
0083
0084
0085
0086
0087 virtual unsigned getNumFixupKinds() const = 0;
0088
0089
0090 virtual std::optional<MCFixupKind> getFixupKind(StringRef Name) const;
0091
0092
0093 virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;
0094
0095
0096 virtual bool shouldForceRelocation(const MCAssembler &Asm,
0097 const MCFixup &Fixup,
0098 const MCValue &Target,
0099 const uint64_t Value,
0100 const MCSubtargetInfo *STI) {
0101 return false;
0102 }
0103
0104
0105
0106
0107 virtual bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF,
0108 unsigned &Size) {
0109 return false;
0110 }
0111
0112
0113
0114 virtual bool shouldInsertFixupForCodeAlign(MCAssembler &Asm,
0115 MCAlignFragment &AF) {
0116 return false;
0117 }
0118
0119 virtual bool evaluateTargetFixup(const MCAssembler &Asm,
0120 const MCFixup &Fixup, const MCFragment *DF,
0121 const MCValue &Target,
0122 const MCSubtargetInfo *STI, uint64_t &Value,
0123 bool &WasForced) {
0124 llvm_unreachable("Need to implement hook if target has custom fixups");
0125 }
0126
0127 virtual bool handleAddSubRelocations(const MCAssembler &Asm,
0128 const MCFragment &F,
0129 const MCFixup &Fixup,
0130 const MCValue &Target,
0131 uint64_t &FixedValue) const {
0132 return false;
0133 }
0134
0135
0136
0137
0138
0139
0140
0141 virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
0142 const MCValue &Target, MutableArrayRef<char> Data,
0143 uint64_t Value, bool IsResolved,
0144 const MCSubtargetInfo *STI) const = 0;
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156 virtual bool mayNeedRelaxation(const MCInst &Inst,
0157 const MCSubtargetInfo &STI) const {
0158 return false;
0159 }
0160
0161
0162
0163 virtual bool fixupNeedsRelaxationAdvanced(const MCAssembler &Asm,
0164 const MCFixup &Fixup, bool Resolved,
0165 uint64_t Value,
0166 const MCRelaxableFragment *DF,
0167 const bool WasForced) const;
0168
0169
0170 virtual bool fixupNeedsRelaxation(const MCFixup &Fixup,
0171 uint64_t Value) const {
0172 llvm_unreachable("Needed if mayNeedRelaxation may return true");
0173 }
0174
0175
0176
0177
0178
0179
0180 virtual void relaxInstruction(MCInst &Inst,
0181 const MCSubtargetInfo &STI) const {};
0182
0183 virtual bool relaxDwarfLineAddr(const MCAssembler &Asm,
0184 MCDwarfLineAddrFragment &DF,
0185 bool &WasRelaxed) const {
0186 return false;
0187 }
0188
0189 virtual bool relaxDwarfCFA(const MCAssembler &Asm,
0190 MCDwarfCallFrameFragment &DF,
0191 bool &WasRelaxed) const {
0192 return false;
0193 }
0194
0195
0196
0197 virtual std::pair<bool, bool>
0198 relaxLEB128(const MCAssembler &Asm, MCLEBFragment &LF, int64_t &Value) const {
0199 return std::make_pair(false, false);
0200 }
0201
0202
0203
0204
0205
0206
0207
0208 virtual unsigned getMinimumNopSize() const { return 1; }
0209
0210
0211
0212 virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const {
0213 return 0;
0214 }
0215
0216
0217
0218
0219
0220 virtual bool writeNopData(raw_ostream &OS, uint64_t Count,
0221 const MCSubtargetInfo *STI) const = 0;
0222
0223
0224 virtual void finishLayout(MCAssembler const &Asm) const {}
0225
0226
0227 virtual void handleAssemblerFlag(MCAssemblerFlag Flag) {}
0228
0229
0230 virtual uint64_t generateCompactUnwindEncoding(const MCDwarfFrameInfo *FI,
0231 const MCContext *Ctxt) const {
0232 return 0;
0233 }
0234
0235
0236 virtual bool isMicroMips(const MCSymbol *Sym) const {
0237 return false;
0238 }
0239
0240 bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const;
0241 };
0242
0243 }
0244
0245 #endif