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