Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:12

0001 //===- llvm/MC/MCAsmBackend.h - MC Asm Backend ------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
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 /// Generic interface to target specific assembler backends.
0042 class MCAsmBackend {
0043 protected: // Can only create subclasses.
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   /// Fixup kind used for linker relaxation. Currently only used by RISC-V
0054   /// and LoongArch.
0055   const unsigned RelaxFixupKind;
0056   bool allowLinkerRelaxation() const { return RelaxFixupKind != MaxFixupKind; }
0057 
0058   /// Return true if this target might automatically pad instructions and thus
0059   /// need to emit padding enable/disable directives around sensative code.
0060   virtual bool allowAutoPadding() const { return false; }
0061   /// Return true if this target allows an unrelaxable instruction to be
0062   /// emitted into RelaxableFragment and then we can increase its size in a
0063   /// tricky way for optimization.
0064   virtual bool allowEnhancedRelaxation() const { return false; }
0065 
0066   /// lifetime management
0067   virtual void reset() {}
0068 
0069   /// Create a new MCObjectWriter instance for use by the assembler backend to
0070   /// emit the final object file.
0071   std::unique_ptr<MCObjectWriter>
0072   createObjectWriter(raw_pwrite_stream &OS) const;
0073 
0074   /// Create an MCObjectWriter that writes two object files: a .o file which is
0075   /// linked into the final program and a .dwo file which is used by debuggers.
0076   /// This function is only supported with ELF targets.
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   /// \name Target Fixup Interfaces
0084   /// @{
0085 
0086   /// Get the number of target specific fixup kinds.
0087   virtual unsigned getNumFixupKinds() const = 0;
0088 
0089   /// Map a relocation name used in .reloc to a fixup kind.
0090   virtual std::optional<MCFixupKind> getFixupKind(StringRef Name) const;
0091 
0092   /// Get information on a fixup kind.
0093   virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;
0094 
0095   /// Hook to check if a relocation is needed for some target specific reason.
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   /// Hook to check if extra nop bytes must be inserted for alignment directive.
0105   /// For some targets this may be necessary in order to support linker
0106   /// relaxation. The number of bytes to insert are returned in Size.
0107   virtual bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF,
0108                                                      unsigned &Size) {
0109     return false;
0110   }
0111 
0112   /// Hook which indicates if the target requires a fixup to be generated when
0113   /// handling an align directive in an executable section
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   /// Apply the \p Value for given \p Fixup into the provided data fragment, at
0136   /// the offset specified by the fixup and following the fixup kind as
0137   /// appropriate. Errors (such as an out of range fixup value) should be
0138   /// reported via \p Ctx.
0139   /// The  \p STI is present only for fragments of type MCRelaxableFragment and
0140   /// MCDataFragment with hasInstructions() == true.
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   /// \name Target Relaxation Interfaces
0149   /// @{
0150 
0151   /// Check whether the given instruction may need relaxation.
0152   ///
0153   /// \param Inst - The instruction to test.
0154   /// \param STI - The MCSubtargetInfo in effect when the instruction was
0155   /// encoded.
0156   virtual bool mayNeedRelaxation(const MCInst &Inst,
0157                                  const MCSubtargetInfo &STI) const {
0158     return false;
0159   }
0160 
0161   /// Target specific predicate for whether a given fixup requires the
0162   /// associated instruction to be relaxed.
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   /// Simple predicate for targets where !Resolved implies requiring relaxation
0170   virtual bool fixupNeedsRelaxation(const MCFixup &Fixup,
0171                                     uint64_t Value) const {
0172     llvm_unreachable("Needed if mayNeedRelaxation may return true");
0173   }
0174 
0175   /// Relax the instruction in the given fragment to the next wider instruction.
0176   ///
0177   /// \param [out] Inst The instruction to relax, which is also the relaxed
0178   /// instruction.
0179   /// \param STI the subtarget information for the associated instruction.
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   // Defined by linker relaxation targets to possibly emit LEB128 relocations
0196   // and set Value at the relocated location.
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   /// Returns the minimum size of a nop in bytes on this target. The assembler
0205   /// will use this to emit excess padding in situations where the padding
0206   /// required for simple alignment would be less than the minimum nop size.
0207   ///
0208   virtual unsigned getMinimumNopSize() const { return 1; }
0209 
0210   /// Returns the maximum size of a nop in bytes on this target.
0211   ///
0212   virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const {
0213     return 0;
0214   }
0215 
0216   /// Write an (optimal) nop sequence of Count bytes to the given output. If the
0217   /// target cannot generate such a sequence, it should return an error.
0218   ///
0219   /// \return - True on success.
0220   virtual bool writeNopData(raw_ostream &OS, uint64_t Count,
0221                             const MCSubtargetInfo *STI) const = 0;
0222 
0223   /// Give backend an opportunity to finish layout after relaxation
0224   virtual void finishLayout(MCAssembler const &Asm) const {}
0225 
0226   /// Handle any target-specific assembler flags. By default, do nothing.
0227   virtual void handleAssemblerFlag(MCAssemblerFlag Flag) {}
0228 
0229   /// Generate the compact unwind encoding for the CFI instructions.
0230   virtual uint64_t generateCompactUnwindEncoding(const MCDwarfFrameInfo *FI,
0231                                                  const MCContext *Ctxt) const {
0232     return 0;
0233   }
0234 
0235   /// Check whether a given symbol has been flagged with MICROMIPS flag.
0236   virtual bool isMicroMips(const MCSymbol *Sym) const {
0237     return false;
0238   }
0239 
0240   bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const;
0241 };
0242 
0243 } // end namespace llvm
0244 
0245 #endif // LLVM_MC_MCASMBACKEND_H