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"
24 class MCCFIInstruction;
25 struct MCFixupKindInfo;
29 class MCRelaxableFragment;
30 class MCSubtargetInfo;
32 class raw_pwrite_stream;
34 /// Generic interface to target specific assembler backends.
36 protected: // Can only create subclasses.
40 MCAsmBackend(const MCAsmBackend &) = delete;
41 MCAsmBackend &operator=(const MCAsmBackend &) = delete;
42 virtual ~MCAsmBackend();
44 /// lifetime management
45 virtual void reset() {}
47 /// Create a new MCObjectWriter instance for use by the assembler backend to
48 /// emit the final object file.
49 virtual MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const = 0;
51 /// \name Target Fixup Interfaces
54 /// Get the number of target specific fixup kinds.
55 virtual unsigned getNumFixupKinds() const = 0;
57 /// Map a relocation name used in .reloc to a fixup kind.
58 virtual Optional<MCFixupKind> getFixupKind(StringRef Name) const;
60 /// Get information on a fixup kind.
61 virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;
63 /// Hook to check if a relocation is needed for some target specific reason.
64 virtual bool shouldForceRelocation(const MCAssembler &Asm,
66 const MCValue &Target) {
70 /// Apply the \p Value for given \p Fixup into the provided data fragment, at
71 /// the offset specified by the fixup and following the fixup kind as
72 /// appropriate. Errors (such as an out of range fixup value) should be
73 /// reported via \p Ctx.
74 virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
75 const MCValue &Target, MutableArrayRef<char> Data,
76 uint64_t Value, bool IsResolved) const = 0;
80 /// \name Target Relaxation Interfaces
83 /// Check whether the given instruction may need relaxation.
85 /// \param Inst - The instruction to test.
86 virtual bool mayNeedRelaxation(const MCInst &Inst) const = 0;
88 /// Target specific predicate for whether a given fixup requires the
89 /// associated instruction to be relaxed.
90 virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
92 const MCRelaxableFragment *DF,
93 const MCAsmLayout &Layout) const;
95 /// Simple predicate for targets where !Resolved implies requiring relaxation
96 virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
97 const MCRelaxableFragment *DF,
98 const MCAsmLayout &Layout) const = 0;
100 /// Relax the instruction in the given fragment to the next wider instruction.
102 /// \param Inst The instruction to relax, which may be the same as the
104 /// \param STI the subtarget information for the associated instruction.
105 /// \param [out] Res On return, the relaxed instruction.
106 virtual void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
107 MCInst &Res) const = 0;
111 /// Returns the minimum size of a nop in bytes on this target. The assembler
112 /// will use this to emit excess padding in situations where the padding
113 /// required for simple alignment would be less than the minimum nop size.
115 virtual unsigned getMinimumNopSize() const { return 1; }
117 /// Write an (optimal) nop sequence of Count bytes to the given output. If the
118 /// target cannot generate such a sequence, it should return an error.
120 /// \return - True on success.
121 virtual bool writeNopData(uint64_t Count, MCObjectWriter *OW) const = 0;
123 /// Give backend an opportunity to finish layout after relaxation
124 virtual void finishLayout(MCAssembler const &Asm,
125 MCAsmLayout &Layout) const {}
127 /// Handle any target-specific assembler flags. By default, do nothing.
128 virtual void handleAssemblerFlag(MCAssemblerFlag Flag) {}
130 /// \brief Generate the compact unwind encoding for the CFI instructions.
132 generateCompactUnwindEncoding(ArrayRef<MCCFIInstruction>) const {
137 } // end namespace llvm
139 #endif // LLVM_MC_MCASMBACKEND_H