blob: 030d3c05aa5ad56ba05c163f2628ce76b38e5d3b [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- 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 Scullcdfcccc2018-10-05 20:58:37 +010019#include "llvm/Support/Endian.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010020#include <cstdint>
21#include <memory>
22
23namespace llvm {
24
25class MCAsmLayout;
26class MCAssembler;
27class MCCFIInstruction;
28class MCCodePadder;
29struct MCFixupKindInfo;
30class MCFragment;
31class MCInst;
32class MCObjectStreamer;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010033class MCObjectTargetWriter;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010034class MCObjectWriter;
35struct MCCodePaddingContext;
36class MCRelaxableFragment;
37class MCSubtargetInfo;
38class MCValue;
39class raw_pwrite_stream;
40
41/// Generic interface to target specific assembler backends.
42class MCAsmBackend {
43 std::unique_ptr<MCCodePadder> CodePadder;
44
45protected: // Can only create subclasses.
Andrew Scullcdfcccc2018-10-05 20:58:37 +010046 MCAsmBackend(support::endianness Endian);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010047
48public:
49 MCAsmBackend(const MCAsmBackend &) = delete;
50 MCAsmBackend &operator=(const MCAsmBackend &) = delete;
51 virtual ~MCAsmBackend();
52
Andrew Scullcdfcccc2018-10-05 20:58:37 +010053 const support::endianness Endian;
54
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010055 /// 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 Scullcdfcccc2018-10-05 20:58:37 +010060 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 Scull5e1ddfa2018-08-14 10:06:54 +010071
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 Scullcdfcccc2018-10-05 20:58:37 +010095 /// The \p STI is present only for fragments of type MCRelaxableFragment and
96 /// MCDataFragment with hasInstructions() == true.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010097 virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
98 const MCValue &Target, MutableArrayRef<char> Data,
Andrew Scullcdfcccc2018-10-05 20:58:37 +010099 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 Scull5e1ddfa2018-08-14 10:06:54 +0100105
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 Scullcdfcccc2018-10-05 20:58:37 +0100114 /// \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 Scull5e1ddfa2018-08-14 10:06:54 +0100118
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 Scullcdfcccc2018-10-05 20:58:37 +0100124 const MCAsmLayout &Layout,
125 const bool WasForced) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100126
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 Scullcdfcccc2018-10-05 20:58:37 +0100153 virtual bool writeNopData(raw_ostream &OS, uint64_t Count) const = 0;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100154
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 Scullcdfcccc2018-10-05 20:58:37 +0100162 /// Generate the compact unwind encoding for the CFI instructions.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100163 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