Line data Source code
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"
19 : #include "llvm/Support/Endian.h"
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;
33 : class MCObjectTargetWriter;
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 34975 : class MCAsmBackend {
43 : std::unique_ptr<MCCodePadder> CodePadder;
44 :
45 : protected: // Can only create subclasses.
46 : MCAsmBackend(support::endianness Endian);
47 :
48 : public:
49 : MCAsmBackend(const MCAsmBackend &) = delete;
50 : MCAsmBackend &operator=(const MCAsmBackend &) = delete;
51 : virtual ~MCAsmBackend();
52 :
53 : const support::endianness Endian;
54 :
55 : /// lifetime management
56 8140 : virtual void reset() {}
57 :
58 : /// Create a new MCObjectWriter instance for use by the assembler backend to
59 : /// emit the final object file.
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;
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 3154519 : virtual bool shouldForceRelocation(const MCAssembler &Asm,
86 : const MCFixup &Fixup,
87 : const MCValue &Target) {
88 3154519 : 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.
95 : /// The \p STI is present only for fragments of type MCRelaxableFragment and
96 : /// MCDataFragment with hasInstructions() == true.
97 : virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
98 : const MCValue &Target, MutableArrayRef<char> Data,
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 10397765 : virtual bool requiresDiffExpressionRelocations() const { return false; }
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.
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;
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,
124 : const MCAsmLayout &Layout,
125 : const bool WasForced) const;
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 41720 : 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.
153 : virtual bool writeNopData(raw_ostream &OS, uint64_t Count) const = 0;
154 :
155 : /// Give backend an opportunity to finish layout after relaxation
156 12286 : virtual void finishLayout(MCAssembler const &Asm,
157 12286 : MCAsmLayout &Layout) const {}
158 :
159 : /// Handle any target-specific assembler flags. By default, do nothing.
160 326 : virtual void handleAssemblerFlag(MCAssemblerFlag Flag) {}
161 :
162 : /// Generate the compact unwind encoding for the CFI instructions.
163 : virtual uint32_t
164 0 : generateCompactUnwindEncoding(ArrayRef<MCCFIInstruction>) const {
165 0 : return 0;
166 : }
167 :
168 : /// Check whether a given symbol has been flagged with MICROMIPS flag.
169 5380490 : virtual bool isMicroMips(const MCSymbol *Sym) const {
170 5380490 : return false;
171 : }
172 :
173 : /// Handles all target related code padding when starting to write a new
174 : /// basic block to an object file.
175 : ///
176 : /// \param OS The streamer used for writing the padding data and function.
177 : /// \param Context the context of the padding, Embeds the basic block's
178 : /// parameters.
179 : void handleCodePaddingBasicBlockStart(MCObjectStreamer *OS,
180 : const MCCodePaddingContext &Context);
181 : /// Handles all target related code padding after writing a block to an object
182 : /// file.
183 : ///
184 : /// \param Context the context of the padding, Embeds the basic block's
185 : /// parameters.
186 : void handleCodePaddingBasicBlockEnd(const MCCodePaddingContext &Context);
187 : /// Handles all target related code padding before writing a new instruction
188 : /// to an object file.
189 : ///
190 : /// \param Inst the instruction.
191 : void handleCodePaddingInstructionBegin(const MCInst &Inst);
192 : /// Handles all target related code padding after writing an instruction to an
193 : /// object file.
194 : ///
195 : /// \param Inst the instruction.
196 : void handleCodePaddingInstructionEnd(const MCInst &Inst);
197 :
198 : /// Relaxes a fragment (changes the size of the padding) according to target
199 : /// requirements. The new size computation is done w.r.t a layout.
200 : ///
201 : /// \param PF The fragment to relax.
202 : /// \param Layout Code layout information.
203 : ///
204 : /// \returns true iff any relaxation occurred.
205 : bool relaxFragment(MCPaddingFragment *PF, MCAsmLayout &Layout);
206 : };
207 :
208 : } // end namespace llvm
209 :
210 : #endif // LLVM_MC_MCASMBACKEND_H
|