1 //===- llvm/MC/MCAsmBackend.h - MC Asm Backend ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCASMBACKEND_H
11 #define LLVM_MC_MCASMBACKEND_H
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"
26 class MCCFIInstruction;
28 struct MCFixupKindInfo;
31 class MCObjectStreamer;
33 struct MCCodePaddingContext;
34 class MCRelaxableFragment;
35 class MCSubtargetInfo;
37 class raw_pwrite_stream;
39 /// Generic interface to target specific assembler backends.
41 std::unique_ptr<MCCodePadder> CodePadder;
43 protected: // Can only create subclasses.
45 MCAsmBackend(std::unique_ptr<MCCodePadder> TargetCodePadder);
48 MCAsmBackend(const MCAsmBackend &) = delete;
49 MCAsmBackend &operator=(const MCAsmBackend &) = delete;
50 virtual ~MCAsmBackend();
52 /// lifetime management
53 virtual void reset() {}
55 /// Create a new MCObjectWriter instance for use by the assembler backend to
56 /// emit the final object file.
57 virtual std::unique_ptr<MCObjectWriter>
58 createObjectWriter(raw_pwrite_stream &OS) const = 0;
60 /// \name Target Fixup Interfaces
63 /// Get the number of target specific fixup kinds.
64 virtual unsigned getNumFixupKinds() const = 0;
66 /// Map a relocation name used in .reloc to a fixup kind.
67 virtual Optional<MCFixupKind> getFixupKind(StringRef Name) const;
69 /// Get information on a fixup kind.
70 virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;
72 /// Hook to check if a relocation is needed for some target specific reason.
73 virtual bool shouldForceRelocation(const MCAssembler &Asm,
75 const MCValue &Target) {
79 /// Apply the \p Value for given \p Fixup into the provided data fragment, at
80 /// the offset specified by the fixup and following the fixup kind as
81 /// appropriate. Errors (such as an out of range fixup value) should be
82 /// reported via \p Ctx.
83 virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
84 const MCValue &Target, MutableArrayRef<char> Data,
85 uint64_t Value, bool IsResolved) const = 0;
89 /// \name Target Relaxation Interfaces
92 /// Check whether the given instruction may need relaxation.
94 /// \param Inst - The instruction to test.
95 virtual bool mayNeedRelaxation(const MCInst &Inst) const = 0;
97 /// Target specific predicate for whether a given fixup requires the
98 /// associated instruction to be relaxed.
99 virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
101 const MCRelaxableFragment *DF,
102 const MCAsmLayout &Layout) const;
104 /// Simple predicate for targets where !Resolved implies requiring relaxation
105 virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
106 const MCRelaxableFragment *DF,
107 const MCAsmLayout &Layout) const = 0;
109 /// Relax the instruction in the given fragment to the next wider instruction.
111 /// \param Inst The instruction to relax, which may be the same as the
113 /// \param STI the subtarget information for the associated instruction.
114 /// \param [out] Res On return, the relaxed instruction.
115 virtual void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
116 MCInst &Res) const = 0;
120 /// Returns the minimum size of a nop in bytes on this target. The assembler
121 /// will use this to emit excess padding in situations where the padding
122 /// required for simple alignment would be less than the minimum nop size.
124 virtual unsigned getMinimumNopSize() const { return 1; }
126 /// Write an (optimal) nop sequence of Count bytes to the given output. If the
127 /// target cannot generate such a sequence, it should return an error.
129 /// \return - True on success.
130 virtual bool writeNopData(uint64_t Count, MCObjectWriter *OW) const = 0;
132 /// Give backend an opportunity to finish layout after relaxation
133 virtual void finishLayout(MCAssembler const &Asm,
134 MCAsmLayout &Layout) const {}
136 /// Handle any target-specific assembler flags. By default, do nothing.
137 virtual void handleAssemblerFlag(MCAssemblerFlag Flag) {}
139 /// \brief Generate the compact unwind encoding for the CFI instructions.
141 generateCompactUnwindEncoding(ArrayRef<MCCFIInstruction>) const {
145 /// Handles all target related code padding when starting to write a new
146 /// basic block to an object file.
148 /// \param OS The streamer used for writing the padding data and function.
149 /// \param Context the context of the padding, Embeds the basic block's
151 void handleCodePaddingBasicBlockStart(MCObjectStreamer *OS,
152 const MCCodePaddingContext &Context);
153 /// Handles all target related code padding after writing a block to an object
156 /// \param Context the context of the padding, Embeds the basic block's
158 void handleCodePaddingBasicBlockEnd(const MCCodePaddingContext &Context);
159 /// Handles all target related code padding before writing a new instruction
160 /// to an object file.
162 /// \param Inst the instruction.
163 void handleCodePaddingInstructionBegin(const MCInst &Inst);
164 /// Handles all target related code padding after writing an instruction to an
167 /// \param Inst the instruction.
168 void handleCodePaddingInstructionEnd(const MCInst &Inst);
170 /// Relaxes a fragment (changes the size of the padding) according to target
171 /// requirements. The new size computation is done w.r.t a layout.
173 /// \param PF The fragment to relax.
174 /// \param Layout Code layout information.
176 /// \returns true iff any relaxation occured.
177 bool relaxFragment(MCPaddingFragment *PF, MCAsmLayout &Layout);
180 } // end namespace llvm
182 #endif // LLVM_MC_MCASMBACKEND_H