blob: 1f3ad6c1e547885ed2c2f05855a43502c30284c5 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/MC/MCAsmBackend.h - MC Asm Backend ------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010018#include "llvm/Support/Endian.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010019#include <cstdint>
20#include <memory>
21
22namespace llvm {
23
24class MCAsmLayout;
25class MCAssembler;
26class MCCFIInstruction;
27class MCCodePadder;
28struct MCFixupKindInfo;
29class MCFragment;
30class MCInst;
31class MCObjectStreamer;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010032class MCObjectTargetWriter;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010033class MCObjectWriter;
34struct MCCodePaddingContext;
35class MCRelaxableFragment;
36class MCSubtargetInfo;
37class MCValue;
38class raw_pwrite_stream;
39
40/// Generic interface to target specific assembler backends.
41class MCAsmBackend {
42 std::unique_ptr<MCCodePadder> CodePadder;
43
44protected: // Can only create subclasses.
Andrew Scullcdfcccc2018-10-05 20:58:37 +010045 MCAsmBackend(support::endianness Endian);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046
47public:
48 MCAsmBackend(const MCAsmBackend &) = delete;
49 MCAsmBackend &operator=(const MCAsmBackend &) = delete;
50 virtual ~MCAsmBackend();
51
Andrew Scullcdfcccc2018-10-05 20:58:37 +010052 const support::endianness Endian;
53
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010054 /// 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.
Andrew Scullcdfcccc2018-10-05 20:58:37 +010059 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;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010070
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
Andrew Walbran16937d02019-10-22 13:54:20 +010090 /// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100106 /// 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.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100110 /// The \p STI is present only for fragments of type MCRelaxableFragment and
111 /// MCDataFragment with hasInstructions() == true.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100112 virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
113 const MCValue &Target, MutableArrayRef<char> Data,
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100114 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; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100120
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.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100129 /// \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;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100133
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,
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100139 const MCAsmLayout &Layout,
140 const bool WasForced) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100141
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.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100168 virtual bool writeNopData(raw_ostream &OS, uint64_t Count) const = 0;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100169
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
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100177 /// Generate the compact unwind encoding for the CFI instructions.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100178 virtual uint32_t
179 generateCompactUnwindEncoding(ArrayRef<MCCFIInstruction>) const {
180 return 0;
181 }
182
Andrew Walbran16937d02019-10-22 13:54:20 +0100183 /// Check whether a given symbol has been flagged with MICROMIPS flag.
184 virtual bool isMicroMips(const MCSymbol *Sym) const {
185 return false;
186 }
187
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100188 /// 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