Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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/ADT/StringRef.h" |
| 16 | #include "llvm/MC/MCDirectives.h" |
| 17 | #include "llvm/MC/MCFixup.h" |
| 18 | #include "llvm/MC/MCFragment.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 19 | #include "llvm/Support/Endian.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 20 | #include <cstdint> |
| 21 | #include <memory> |
| 22 | |
| 23 | namespace llvm { |
| 24 | |
| 25 | class MCAsmLayout; |
| 26 | class MCAssembler; |
| 27 | class MCCFIInstruction; |
| 28 | class MCCodePadder; |
| 29 | struct MCFixupKindInfo; |
| 30 | class MCFragment; |
| 31 | class MCInst; |
| 32 | class MCObjectStreamer; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 33 | class MCObjectTargetWriter; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 34 | class MCObjectWriter; |
| 35 | struct MCCodePaddingContext; |
| 36 | class MCRelaxableFragment; |
| 37 | class MCSubtargetInfo; |
| 38 | class MCValue; |
| 39 | class raw_pwrite_stream; |
| 40 | |
| 41 | /// Generic interface to target specific assembler backends. |
| 42 | class MCAsmBackend { |
| 43 | std::unique_ptr<MCCodePadder> CodePadder; |
| 44 | |
| 45 | protected: // Can only create subclasses. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 46 | MCAsmBackend(support::endianness Endian); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 47 | |
| 48 | public: |
| 49 | MCAsmBackend(const MCAsmBackend &) = delete; |
| 50 | MCAsmBackend &operator=(const MCAsmBackend &) = delete; |
| 51 | virtual ~MCAsmBackend(); |
| 52 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 53 | const support::endianness Endian; |
| 54 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 55 | /// lifetime management |
| 56 | virtual void reset() {} |
| 57 | |
| 58 | /// Create a new MCObjectWriter instance for use by the assembler backend to |
| 59 | /// emit the final object file. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 60 | std::unique_ptr<MCObjectWriter> |
| 61 | createObjectWriter(raw_pwrite_stream &OS) const; |
| 62 | |
| 63 | /// Create an MCObjectWriter that writes two object files: a .o file which is |
| 64 | /// linked into the final program and a .dwo file which is used by debuggers. |
| 65 | /// This function is only supported with ELF targets. |
| 66 | std::unique_ptr<MCObjectWriter> |
| 67 | createDwoObjectWriter(raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS) const; |
| 68 | |
| 69 | virtual std::unique_ptr<MCObjectTargetWriter> |
| 70 | createObjectTargetWriter() const = 0; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 71 | |
| 72 | /// \name Target Fixup Interfaces |
| 73 | /// @{ |
| 74 | |
| 75 | /// Get the number of target specific fixup kinds. |
| 76 | virtual unsigned getNumFixupKinds() const = 0; |
| 77 | |
| 78 | /// Map a relocation name used in .reloc to a fixup kind. |
| 79 | virtual Optional<MCFixupKind> getFixupKind(StringRef Name) const; |
| 80 | |
| 81 | /// Get information on a fixup kind. |
| 82 | virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const; |
| 83 | |
| 84 | /// Hook to check if a relocation is needed for some target specific reason. |
| 85 | virtual bool shouldForceRelocation(const MCAssembler &Asm, |
| 86 | const MCFixup &Fixup, |
| 87 | const MCValue &Target) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | /// Apply the \p Value for given \p Fixup into the provided data fragment, at |
| 92 | /// the offset specified by the fixup and following the fixup kind as |
| 93 | /// appropriate. Errors (such as an out of range fixup value) should be |
| 94 | /// reported via \p Ctx. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 95 | /// The \p STI is present only for fragments of type MCRelaxableFragment and |
| 96 | /// MCDataFragment with hasInstructions() == true. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 97 | virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, |
| 98 | const MCValue &Target, MutableArrayRef<char> Data, |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 99 | uint64_t Value, bool IsResolved, |
| 100 | const MCSubtargetInfo *STI) const = 0; |
| 101 | |
| 102 | /// Check whether the given target requires emitting differences of two |
| 103 | /// symbols as a set of relocations. |
| 104 | virtual bool requiresDiffExpressionRelocations() const { return false; } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 105 | |
| 106 | /// @} |
| 107 | |
| 108 | /// \name Target Relaxation Interfaces |
| 109 | /// @{ |
| 110 | |
| 111 | /// Check whether the given instruction may need relaxation. |
| 112 | /// |
| 113 | /// \param Inst - The instruction to test. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 114 | /// \param STI - The MCSubtargetInfo in effect when the instruction was |
| 115 | /// encoded. |
| 116 | virtual bool mayNeedRelaxation(const MCInst &Inst, |
| 117 | const MCSubtargetInfo &STI) const = 0; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 118 | |
| 119 | /// Target specific predicate for whether a given fixup requires the |
| 120 | /// associated instruction to be relaxed. |
| 121 | virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved, |
| 122 | uint64_t Value, |
| 123 | const MCRelaxableFragment *DF, |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 124 | const MCAsmLayout &Layout, |
| 125 | const bool WasForced) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 126 | |
| 127 | /// Simple predicate for targets where !Resolved implies requiring relaxation |
| 128 | virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, |
| 129 | const MCRelaxableFragment *DF, |
| 130 | const MCAsmLayout &Layout) const = 0; |
| 131 | |
| 132 | /// Relax the instruction in the given fragment to the next wider instruction. |
| 133 | /// |
| 134 | /// \param Inst The instruction to relax, which may be the same as the |
| 135 | /// output. |
| 136 | /// \param STI the subtarget information for the associated instruction. |
| 137 | /// \param [out] Res On return, the relaxed instruction. |
| 138 | virtual void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI, |
| 139 | MCInst &Res) const = 0; |
| 140 | |
| 141 | /// @} |
| 142 | |
| 143 | /// Returns the minimum size of a nop in bytes on this target. The assembler |
| 144 | /// will use this to emit excess padding in situations where the padding |
| 145 | /// required for simple alignment would be less than the minimum nop size. |
| 146 | /// |
| 147 | virtual unsigned getMinimumNopSize() const { return 1; } |
| 148 | |
| 149 | /// Write an (optimal) nop sequence of Count bytes to the given output. If the |
| 150 | /// target cannot generate such a sequence, it should return an error. |
| 151 | /// |
| 152 | /// \return - True on success. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 153 | virtual bool writeNopData(raw_ostream &OS, uint64_t Count) const = 0; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 154 | |
| 155 | /// Give backend an opportunity to finish layout after relaxation |
| 156 | virtual void finishLayout(MCAssembler const &Asm, |
| 157 | MCAsmLayout &Layout) const {} |
| 158 | |
| 159 | /// Handle any target-specific assembler flags. By default, do nothing. |
| 160 | virtual void handleAssemblerFlag(MCAssemblerFlag Flag) {} |
| 161 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 162 | /// Generate the compact unwind encoding for the CFI instructions. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 163 | virtual uint32_t |
| 164 | generateCompactUnwindEncoding(ArrayRef<MCCFIInstruction>) const { |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | /// Handles all target related code padding when starting to write a new |
| 169 | /// basic block to an object file. |
| 170 | /// |
| 171 | /// \param OS The streamer used for writing the padding data and function. |
| 172 | /// \param Context the context of the padding, Embeds the basic block's |
| 173 | /// parameters. |
| 174 | void handleCodePaddingBasicBlockStart(MCObjectStreamer *OS, |
| 175 | const MCCodePaddingContext &Context); |
| 176 | /// Handles all target related code padding after writing a block to an object |
| 177 | /// file. |
| 178 | /// |
| 179 | /// \param Context the context of the padding, Embeds the basic block's |
| 180 | /// parameters. |
| 181 | void handleCodePaddingBasicBlockEnd(const MCCodePaddingContext &Context); |
| 182 | /// Handles all target related code padding before writing a new instruction |
| 183 | /// to an object file. |
| 184 | /// |
| 185 | /// \param Inst the instruction. |
| 186 | void handleCodePaddingInstructionBegin(const MCInst &Inst); |
| 187 | /// Handles all target related code padding after writing an instruction to an |
| 188 | /// object file. |
| 189 | /// |
| 190 | /// \param Inst the instruction. |
| 191 | void handleCodePaddingInstructionEnd(const MCInst &Inst); |
| 192 | |
| 193 | /// Relaxes a fragment (changes the size of the padding) according to target |
| 194 | /// requirements. The new size computation is done w.r.t a layout. |
| 195 | /// |
| 196 | /// \param PF The fragment to relax. |
| 197 | /// \param Layout Code layout information. |
| 198 | /// |
| 199 | /// \returns true iff any relaxation occurred. |
| 200 | bool relaxFragment(MCPaddingFragment *PF, MCAsmLayout &Layout); |
| 201 | }; |
| 202 | |
| 203 | } // end namespace llvm |
| 204 | |
| 205 | #endif // LLVM_MC_MCASMBACKEND_H |