1 //===-- llvm/MC/MCAsmBackend.h - MC Asm Backend -----------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLVM_MC_MCASMBACKEND_H 11 #define LLVM_MC_MCASMBACKEND_H 12 13 #include "llvm/ADT/ArrayRef.h" 14 #include "llvm/ADT/Optional.h" 15 #include "llvm/MC/MCDirectives.h" 16 #include "llvm/MC/MCDwarf.h" 17 #include "llvm/MC/MCFixup.h" 18 #include "llvm/Support/DataTypes.h" 19 #include "llvm/Support/ErrorHandling.h" 20 21 namespace llvm { 22 class MCAsmLayout; 23 class MCAssembler; 24 class MCELFObjectTargetWriter; 25 struct MCFixupKindInfo; 26 class MCFragment; 27 class MCInst; 28 class MCRelaxableFragment; 29 class MCObjectWriter; 30 class MCSection; 31 class MCValue; 32 class raw_pwrite_stream; 33 34 /// Generic interface to target specific assembler backends. 35 class MCAsmBackend { 36 MCAsmBackend(const MCAsmBackend &) = delete; 37 void operator=(const MCAsmBackend &) = delete; 38 39 protected: // Can only create subclasses. 40 MCAsmBackend(); 41 42 public: 43 virtual ~MCAsmBackend(); 44 45 /// lifetime management reset()46 virtual void reset() {} 47 48 /// Create a new MCObjectWriter instance for use by the assembler backend to 49 /// emit the final object file. 50 virtual MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const = 0; 51 52 /// \name Target Fixup Interfaces 53 /// @{ 54 55 /// Get the number of target specific fixup kinds. 56 virtual unsigned getNumFixupKinds() const = 0; 57 58 /// Map a relocation name used in .reloc to a fixup kind. 59 virtual Optional<MCFixupKind> getFixupKind(StringRef Name) const; 60 61 /// Get information on a fixup kind. 62 virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const; 63 64 /// Target hook to adjust the literal value of a fixup if necessary. 65 /// IsResolved signals whether the caller believes a relocation is needed; the 66 /// target can modify the value. The default does nothing. processFixupValue(const MCAssembler & Asm,const MCAsmLayout & Layout,const MCFixup & Fixup,const MCFragment * DF,const MCValue & Target,uint64_t & Value,bool & IsResolved)67 virtual void processFixupValue(const MCAssembler &Asm, 68 const MCAsmLayout &Layout, 69 const MCFixup &Fixup, const MCFragment *DF, 70 const MCValue &Target, uint64_t &Value, 71 bool &IsResolved) {} 72 73 /// Apply the \p Value for given \p Fixup into the provided data fragment, at 74 /// the offset specified by the fixup and following the fixup kind as 75 /// appropriate. 76 virtual void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize, 77 uint64_t Value, bool IsPCRel) const = 0; 78 79 /// @} 80 81 /// \name Target Relaxation Interfaces 82 /// @{ 83 84 /// Check whether the given instruction may need relaxation. 85 /// 86 /// \param Inst - The instruction to test. 87 virtual bool mayNeedRelaxation(const MCInst &Inst) const = 0; 88 89 /// Target specific predicate for whether a given fixup requires the 90 /// associated instruction to be relaxed. 91 virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved, 92 uint64_t Value, 93 const MCRelaxableFragment *DF, 94 const MCAsmLayout &Layout) const; 95 96 /// Simple predicate for targets where !Resolved implies requiring relaxation 97 virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, 98 const MCRelaxableFragment *DF, 99 const MCAsmLayout &Layout) const = 0; 100 101 /// Relax the instruction in the given fragment to the next wider instruction. 102 /// 103 /// \param Inst The instruction to relax, which may be the same as the 104 /// output. 105 /// \param STI the subtarget information for the associated instruction. 106 /// \param [out] Res On return, the relaxed instruction. 107 virtual void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI, 108 MCInst &Res) const = 0; 109 110 /// @} 111 112 /// Returns the minimum size of a nop in bytes on this target. The assembler 113 /// will use this to emit excess padding in situations where the padding 114 /// required for simple alignment would be less than the minimum nop size. 115 /// getMinimumNopSize()116 virtual unsigned getMinimumNopSize() const { return 1; } 117 118 /// Write an (optimal) nop sequence of Count bytes to the given output. If the 119 /// target cannot generate such a sequence, it should return an error. 120 /// 121 /// \return - True on success. 122 virtual bool writeNopData(uint64_t Count, MCObjectWriter *OW) const = 0; 123 124 /// Give backend an opportunity to finish layout after relaxation finishLayout(MCAssembler const & Asm,MCAsmLayout & Layout)125 virtual void finishLayout(MCAssembler const &Asm, 126 MCAsmLayout &Layout) const {} 127 128 /// Handle any target-specific assembler flags. By default, do nothing. handleAssemblerFlag(MCAssemblerFlag Flag)129 virtual void handleAssemblerFlag(MCAssemblerFlag Flag) {} 130 131 /// \brief Generate the compact unwind encoding for the CFI instructions. 132 virtual uint32_t generateCompactUnwindEncoding(ArrayRef<MCCFIInstruction>)133 generateCompactUnwindEncoding(ArrayRef<MCCFIInstruction>) const { 134 return 0; 135 } 136 }; 137 138 } // End llvm namespace 139 140 #endif 141