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/MC/MCDirectives.h"
16 #include "llvm/MC/MCFixup.h"
17 #include "llvm/Support/DataTypes.h"
18 #include "llvm/Support/ErrorHandling.h"
23 class MCCFIInstruction;
24 class MCELFObjectTargetWriter;
25 struct MCFixupKindInfo;
28 class MCRelaxableFragment;
31 class MCSubtargetInfo;
33 class raw_pwrite_stream;
35 /// Generic interface to target specific assembler backends.
37 MCAsmBackend(const MCAsmBackend &) = delete;
38 void operator=(const MCAsmBackend &) = delete;
40 protected: // Can only create subclasses.
44 virtual ~MCAsmBackend();
46 /// lifetime management
47 virtual void reset() {}
49 /// Create a new MCObjectWriter instance for use by the assembler backend to
50 /// emit the final object file.
51 virtual MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const = 0;
53 /// \name Target Fixup Interfaces
56 /// Get the number of target specific fixup kinds.
57 virtual unsigned getNumFixupKinds() const = 0;
59 /// Map a relocation name used in .reloc to a fixup kind.
60 virtual Optional<MCFixupKind> getFixupKind(StringRef Name) const;
62 /// Get information on a fixup kind.
63 virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;
65 /// Target hook to adjust the literal value of a fixup if necessary.
66 /// IsResolved signals whether the caller believes a relocation is needed; the
67 /// target can modify the value. The default does nothing.
68 virtual void processFixupValue(const MCAssembler &Asm,
69 const MCAsmLayout &Layout,
70 const MCFixup &Fixup, const MCFragment *DF,
71 const MCValue &Target, uint64_t &Value,
74 /// Apply the \p Value for given \p Fixup into the provided data fragment, at
75 /// the offset specified by the fixup and following the fixup kind as
77 virtual void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
78 uint64_t Value, bool IsPCRel) const = 0;
82 /// \name Target Relaxation Interfaces
85 /// Check whether the given instruction may need relaxation.
87 /// \param Inst - The instruction to test.
88 virtual bool mayNeedRelaxation(const MCInst &Inst) const = 0;
90 /// Target specific predicate for whether a given fixup requires the
91 /// associated instruction to be relaxed.
92 virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
94 const MCRelaxableFragment *DF,
95 const MCAsmLayout &Layout) const;
97 /// Simple predicate for targets where !Resolved implies requiring relaxation
98 virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
99 const MCRelaxableFragment *DF,
100 const MCAsmLayout &Layout) const = 0;
102 /// Relax the instruction in the given fragment to the next wider instruction.
104 /// \param Inst The instruction to relax, which may be the same as the
106 /// \param STI the subtarget information for the associated instruction.
107 /// \param [out] Res On return, the relaxed instruction.
108 virtual void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
109 MCInst &Res) const = 0;
113 /// Returns the minimum size of a nop in bytes on this target. The assembler
114 /// will use this to emit excess padding in situations where the padding
115 /// required for simple alignment would be less than the minimum nop size.
117 virtual unsigned getMinimumNopSize() const { return 1; }
119 /// Write an (optimal) nop sequence of Count bytes to the given output. If the
120 /// target cannot generate such a sequence, it should return an error.
122 /// \return - True on success.
123 virtual bool writeNopData(uint64_t Count, MCObjectWriter *OW) const = 0;
125 /// Give backend an opportunity to finish layout after relaxation
126 virtual void finishLayout(MCAssembler const &Asm,
127 MCAsmLayout &Layout) const {}
129 /// Handle any target-specific assembler flags. By default, do nothing.
130 virtual void handleAssemblerFlag(MCAssemblerFlag Flag) {}
132 /// \brief Generate the compact unwind encoding for the CFI instructions.
134 generateCompactUnwindEncoding(ArrayRef<MCCFIInstruction>) const {
139 } // End llvm namespace