LLVM  14.0.0git
MCAsmBackend.h
Go to the documentation of this file.
1 //===- llvm/MC/MCAsmBackend.h - MC Asm Backend ------------------*- C++ -*-===//
2 //
3 // 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
6 //
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/MC/MCDirectives.h"
15 #include "llvm/MC/MCFixup.h"
16 #include "llvm/MC/MCFragment.h"
17 #include "llvm/Support/Endian.h"
18 #include <cstdint>
19 
20 namespace llvm {
21 
22 class MCAsmLayout;
23 class MCAssembler;
24 class MCCFIInstruction;
25 struct MCFixupKindInfo;
26 class MCInst;
27 class MCObjectStreamer;
28 class MCObjectTargetWriter;
29 class MCObjectWriter;
30 class MCSubtargetInfo;
31 class MCValue;
32 class raw_pwrite_stream;
33 class StringRef;
34 
35 /// Generic interface to target specific assembler backends.
36 class MCAsmBackend {
37 protected: // Can only create subclasses.
39 
40 public:
41  MCAsmBackend(const MCAsmBackend &) = delete;
42  MCAsmBackend &operator=(const MCAsmBackend &) = delete;
43  virtual ~MCAsmBackend();
44 
46 
47  /// Return true if this target might automatically pad instructions and thus
48  /// need to emit padding enable/disable directives around sensative code.
49  virtual bool allowAutoPadding() const { return false; }
50  /// Return true if this target allows an unrelaxable instruction to be
51  /// emitted into RelaxableFragment and then we can increase its size in a
52  /// tricky way for optimization.
53  virtual bool allowEnhancedRelaxation() const { return false; }
54 
55  /// Give the target a chance to manipulate state related to instruction
56  /// alignment (e.g. padding for optimization), instruction relaxablility, etc.
57  /// before and after actually emitting the instruction.
58  virtual void emitInstructionBegin(MCObjectStreamer &OS, const MCInst &Inst,
59  const MCSubtargetInfo &STI) {}
60  virtual void emitInstructionEnd(MCObjectStreamer &OS, const MCInst &Inst) {}
61 
62  /// lifetime management
63  virtual void reset() {}
64 
65  /// Create a new MCObjectWriter instance for use by the assembler backend to
66  /// emit the final object file.
67  std::unique_ptr<MCObjectWriter>
69 
70  /// Create an MCObjectWriter that writes two object files: a .o file which is
71  /// linked into the final program and a .dwo file which is used by debuggers.
72  /// This function is only supported with ELF targets.
73  std::unique_ptr<MCObjectWriter>
75 
76  virtual std::unique_ptr<MCObjectTargetWriter>
77  createObjectTargetWriter() const = 0;
78 
79  /// \name Target Fixup Interfaces
80  /// @{
81 
82  /// Get the number of target specific fixup kinds.
83  virtual unsigned getNumFixupKinds() const = 0;
84 
85  /// Map a relocation name used in .reloc to a fixup kind.
87 
88  /// Get information on a fixup kind.
89  virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;
90 
91  /// Hook to check if a relocation is needed for some target specific reason.
92  virtual bool shouldForceRelocation(const MCAssembler &Asm,
93  const MCFixup &Fixup,
94  const MCValue &Target) {
95  return false;
96  }
97 
98  /// Hook to check if extra nop bytes must be inserted for alignment directive.
99  /// For some targets this may be necessary in order to support linker
100  /// relaxation. The number of bytes to insert are returned in Size.
102  unsigned &Size) {
103  return false;
104  }
105 
106  /// Hook which indicates if the target requires a fixup to be generated when
107  /// handling an align directive in an executable section
109  const MCAsmLayout &Layout,
110  MCAlignFragment &AF) {
111  return false;
112  }
113 
114  virtual bool evaluateTargetFixup(const MCAssembler &Asm,
115  const MCAsmLayout &Layout,
116  const MCFixup &Fixup, const MCFragment *DF,
117  const MCValue &Target, uint64_t &Value,
118  bool &WasForced) {
119  llvm_unreachable("Need to implement hook if target has custom fixups");
120  }
121 
122  /// Apply the \p Value for given \p Fixup into the provided data fragment, at
123  /// the offset specified by the fixup and following the fixup kind as
124  /// appropriate. Errors (such as an out of range fixup value) should be
125  /// reported via \p Ctx.
126  /// The \p STI is present only for fragments of type MCRelaxableFragment and
127  /// MCDataFragment with hasInstructions() == true.
128  virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
130  uint64_t Value, bool IsResolved,
131  const MCSubtargetInfo *STI) const = 0;
132 
133  /// @}
134 
135  /// \name Target Relaxation Interfaces
136  /// @{
137 
138  /// Check whether the given instruction may need relaxation.
139  ///
140  /// \param Inst - The instruction to test.
141  /// \param STI - The MCSubtargetInfo in effect when the instruction was
142  /// encoded.
143  virtual bool mayNeedRelaxation(const MCInst &Inst,
144  const MCSubtargetInfo &STI) const {
145  return false;
146  }
147 
148  /// Target specific predicate for whether a given fixup requires the
149  /// associated instruction to be relaxed.
150  virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
151  uint64_t Value,
152  const MCRelaxableFragment *DF,
153  const MCAsmLayout &Layout,
154  const bool WasForced) const;
155 
156  /// Simple predicate for targets where !Resolved implies requiring relaxation
157  virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
158  const MCRelaxableFragment *DF,
159  const MCAsmLayout &Layout) const = 0;
160 
161  /// Relax the instruction in the given fragment to the next wider instruction.
162  ///
163  /// \param [out] Inst The instruction to relax, which is also the relaxed
164  /// instruction.
165  /// \param STI the subtarget information for the associated instruction.
166  virtual void relaxInstruction(MCInst &Inst,
167  const MCSubtargetInfo &STI) const {};
168 
170  MCAsmLayout &Layout, bool &WasRelaxed) const {
171  return false;
172  }
173 
175  bool &WasRelaxed) const {
176  return false;
177  }
178 
179  /// @}
180 
181  /// Returns the minimum size of a nop in bytes on this target. The assembler
182  /// will use this to emit excess padding in situations where the padding
183  /// required for simple alignment would be less than the minimum nop size.
184  ///
185  virtual unsigned getMinimumNopSize() const { return 1; }
186 
187  /// Returns the maximum size of a nop in bytes on this target.
188  ///
189  virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const {
190  return 0;
191  }
192 
193  /// Write an (optimal) nop sequence of Count bytes to the given output. If the
194  /// target cannot generate such a sequence, it should return an error.
195  ///
196  /// \return - True on success.
197  virtual bool writeNopData(raw_ostream &OS, uint64_t Count,
198  const MCSubtargetInfo *STI) const = 0;
199 
200  /// Give backend an opportunity to finish layout after relaxation
201  virtual void finishLayout(MCAssembler const &Asm,
202  MCAsmLayout &Layout) const {}
203 
204  /// Handle any target-specific assembler flags. By default, do nothing.
206 
207  /// Generate the compact unwind encoding for the CFI instructions.
208  virtual uint32_t
210  return 0;
211  }
212 
213  /// Check whether a given symbol has been flagged with MICROMIPS flag.
214  virtual bool isMicroMips(const MCSymbol *Sym) const {
215  return false;
216  }
217 };
218 
219 } // end namespace llvm
220 
221 #endif // LLVM_MC_MCASMBACKEND_H
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::MCAsmBackend::isMicroMips
virtual bool isMicroMips(const MCSymbol *Sym) const
Check whether a given symbol has been flagged with MICROMIPS flag.
Definition: MCAsmBackend.h:214
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::MCRelaxableFragment
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:271
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Optional.h
llvm::MCAsmBackend::getFixupKindInfo
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
Definition: MCAsmBackend.cpp:74
llvm::MCAsmBackend::emitInstructionEnd
virtual void emitInstructionEnd(MCObjectStreamer &OS, const MCInst &Inst)
Definition: MCAsmBackend.h:60
llvm::MCDwarfCallFrameFragment
Definition: MCFragment.h:481
MCDirectives.h
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:137
llvm::MCAsmBackend::handleAssemblerFlag
virtual void handleAssemblerFlag(MCAssemblerFlag Flag)
Handle any target-specific assembler flags. By default, do nothing.
Definition: MCAsmBackend.h:205
MCFragment.h
llvm::MCAsmBackend::~MCAsmBackend
virtual ~MCAsmBackend()
llvm::MCAsmBackend::applyFixup
virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, MutableArrayRef< char > Data, uint64_t Value, bool IsResolved, const MCSubtargetInfo *STI) const =0
Apply the Value for given Fixup into the provided data fragment, at the offset specified by the fixup...
llvm::Optional
Definition: APInt.h:33
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::MCAssemblerFlag
MCAssemblerFlag
Definition: MCDirectives.h:50
llvm::MCAsmBackend::createDwoObjectWriter
std::unique_ptr< MCObjectWriter > createDwoObjectWriter(raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS) const
Create an MCObjectWriter that writes two object files: a .o file which is linked into the final progr...
Definition: MCAsmBackend.cpp:54
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::MCAsmBackend::Endian
const support::endianness Endian
Definition: MCAsmBackend.h:45
llvm::MCAsmBackend::finishLayout
virtual void finishLayout(MCAssembler const &Asm, MCAsmLayout &Layout) const
Give backend an opportunity to finish layout after relaxation.
Definition: MCAsmBackend.h:201
llvm::MCFragment
Definition: MCFragment.h:31
llvm::MCAsmBackend::getMinimumNopSize
virtual unsigned getMinimumNopSize() const
Returns the minimum size of a nop in bytes on this target.
Definition: MCAsmBackend.h:185
llvm::MCAsmBackend
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:36
llvm::MutableArrayRef< char >
llvm::MCAlignFragment
Definition: MCFragment.h:295
llvm::MCObjectStreamer
Streaming object file generation interface.
Definition: MCObjectStreamer.h:36
llvm::MCID::Flag
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:146
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::MCAsmBackend::reset
virtual void reset()
lifetime management
Definition: MCAsmBackend.h:63
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::MCAsmBackend::shouldForceRelocation
virtual bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target)
Hook to check if a relocation is needed for some target specific reason.
Definition: MCAsmBackend.h:92
DF
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
llvm::MCAsmBackend::relaxDwarfLineAddr
virtual bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF, MCAsmLayout &Layout, bool &WasRelaxed) const
Definition: MCAsmBackend.h:169
llvm::MCDwarfLineAddrFragment
Definition: MCFragment.h:457
llvm::MCAsmBackend::generateCompactUnwindEncoding
virtual uint32_t generateCompactUnwindEncoding(ArrayRef< MCCFIInstruction >) const
Generate the compact unwind encoding for the CFI instructions.
Definition: MCAsmBackend.h:209
llvm::MCAssembler
Definition: MCAssembler.h:60
uint64_t
llvm::raw_pwrite_stream
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:417
llvm::MCAsmBackend::shouldInsertExtraNopBytesForCodeAlign
virtual bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF, unsigned &Size)
Hook to check if extra nop bytes must be inserted for alignment directive.
Definition: MCAsmBackend.h:101
llvm::MCAsmBackend::allowEnhancedRelaxation
virtual bool allowEnhancedRelaxation() const
Return true if this target allows an unrelaxable instruction to be emitted into RelaxableFragment and...
Definition: MCAsmBackend.h:53
ArrayRef.h
llvm::MCAsmBackend::shouldInsertFixupForCodeAlign
virtual bool shouldInsertFixupForCodeAlign(MCAssembler &Asm, const MCAsmLayout &Layout, MCAlignFragment &AF)
Hook which indicates if the target requires a fixup to be generated when handling an align directive ...
Definition: MCAsmBackend.h:108
llvm::MCAsmBackend::getFixupKind
virtual Optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
Definition: MCAsmBackend.cpp:70
llvm::MCFixupKindInfo
Target independent information on a fixup kind.
Definition: MCFixupKindInfo.h:15
llvm::MCAsmBackend::fixupNeedsRelaxationAdvanced
virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout, const bool WasForced) const
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
Definition: MCAsmBackend.cpp:104
llvm::MCAsmBackend::writeNopData
virtual bool writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const =0
Write an (optimal) nop sequence of Count bytes to the given output.
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::MCAsmBackend::allowAutoPadding
virtual bool allowAutoPadding() const
Return true if this target might automatically pad instructions and thus need to emit padding enable/...
Definition: MCAsmBackend.h:49
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::MCAsmBackend::createObjectTargetWriter
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
uint32_t
MCFixup.h
llvm::MCAsmBackend::relaxInstruction
virtual void relaxInstruction(MCInst &Inst, const MCSubtargetInfo &STI) const
Relax the instruction in the given fragment to the next wider instruction.
Definition: MCAsmBackend.h:166
llvm::MCAsmBackend::getMaximumNopSize
virtual unsigned getMaximumNopSize(const MCSubtargetInfo &STI) const
Returns the maximum size of a nop in bytes on this target.
Definition: MCAsmBackend.h:189
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::MCAsmBackend::evaluateTargetFixup
virtual bool evaluateTargetFixup(const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFixup &Fixup, const MCFragment *DF, const MCValue &Target, uint64_t &Value, bool &WasForced)
Definition: MCAsmBackend.h:114
llvm::MCAsmLayout
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
llvm::MCAsmBackend::relaxDwarfCFA
virtual bool relaxDwarfCFA(MCDwarfCallFrameFragment &DF, MCAsmLayout &Layout, bool &WasRelaxed) const
Definition: MCAsmBackend.h:174
llvm::MCAsmBackend::fixupNeedsRelaxation
virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout) const =0
Simple predicate for targets where !Resolved implies requiring relaxation.
llvm::MCAsmBackend::emitInstructionBegin
virtual void emitInstructionBegin(MCObjectStreamer &OS, const MCInst &Inst, const MCSubtargetInfo &STI)
Give the target a chance to manipulate state related to instruction alignment (e.g.
Definition: MCAsmBackend.h:58
llvm::MCFixupKind
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
llvm::MCAsmBackend::createObjectWriter
std::unique_ptr< MCObjectWriter > createObjectWriter(raw_pwrite_stream &OS) const
Create a new MCObjectWriter instance for use by the assembler backend to emit the final object file.
Definition: MCAsmBackend.cpp:30
llvm::MCAsmBackend::MCAsmBackend
MCAsmBackend(support::endianness Endian)
Definition: MCAsmBackend.cpp:25
llvm::MCAsmBackend::operator=
MCAsmBackend & operator=(const MCAsmBackend &)=delete
llvm::support::endianness
endianness
Definition: Endian.h:27
llvm::HexStyle::Asm
@ Asm
0ffh
Definition: MCInstPrinter.h:34
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:37
llvm::MCAsmBackend::getNumFixupKinds
virtual unsigned getNumFixupKinds() const =0
Get the number of target specific fixup kinds.
Endian.h
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:75
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::MCAsmBackend::mayNeedRelaxation
virtual bool mayNeedRelaxation(const MCInst &Inst, const MCSubtargetInfo &STI) const
Check whether the given instruction may need relaxation.
Definition: MCAsmBackend.h:143