LLVM  9.0.0svn
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/ADT/StringRef.h"
15 #include "llvm/MC/MCDirectives.h"
16 #include "llvm/MC/MCFixup.h"
17 #include "llvm/MC/MCFragment.h"
18 #include "llvm/Support/Endian.h"
19 #include <cstdint>
20 #include <memory>
21 
22 namespace llvm {
23 
24 class MCAsmLayout;
25 class MCAssembler;
26 class MCCFIInstruction;
27 class MCCodePadder;
28 struct MCFixupKindInfo;
29 class MCFragment;
30 class MCInst;
31 class MCObjectStreamer;
32 class MCObjectTargetWriter;
33 class MCObjectWriter;
34 struct MCCodePaddingContext;
35 class MCRelaxableFragment;
36 class MCSubtargetInfo;
37 class MCValue;
38 class raw_pwrite_stream;
39 
40 /// Generic interface to target specific assembler backends.
41 class MCAsmBackend {
42  std::unique_ptr<MCCodePadder> CodePadder;
43 
44 protected: // Can only create subclasses.
46 
47 public:
48  MCAsmBackend(const MCAsmBackend &) = delete;
49  MCAsmBackend &operator=(const MCAsmBackend &) = delete;
50  virtual ~MCAsmBackend();
51 
53 
54  /// lifetime management
55  virtual void reset() {}
56 
57  /// Create a new MCObjectWriter instance for use by the assembler backend to
58  /// emit the final object file.
59  std::unique_ptr<MCObjectWriter>
61 
62  /// Create an MCObjectWriter that writes two object files: a .o file which is
63  /// linked into the final program and a .dwo file which is used by debuggers.
64  /// This function is only supported with ELF targets.
65  std::unique_ptr<MCObjectWriter>
67 
68  virtual std::unique_ptr<MCObjectTargetWriter>
69  createObjectTargetWriter() const = 0;
70 
71  /// \name Target Fixup Interfaces
72  /// @{
73 
74  /// Get the number of target specific fixup kinds.
75  virtual unsigned getNumFixupKinds() const = 0;
76 
77  /// Map a relocation name used in .reloc to a fixup kind.
79 
80  /// Get information on a fixup kind.
81  virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const;
82 
83  /// Hook to check if a relocation is needed for some target specific reason.
84  virtual bool shouldForceRelocation(const MCAssembler &Asm,
85  const MCFixup &Fixup,
86  const MCValue &Target) {
87  return false;
88  }
89 
90  /// Hook to check if extra nop bytes must be inserted for alignment directive.
91  /// For some targets this may be necessary in order to support linker
92  /// relaxation. The number of bytes to insert are returned in Size.
94  unsigned &Size) {
95  return false;
96  }
97 
98  /// Hook which indicates if the target requires a fixup to be generated when
99  /// handling an align directive in an executable section
101  const MCAsmLayout &Layout,
102  MCAlignFragment &AF) {
103  return false;
104  }
105 
106  /// Apply the \p Value for given \p Fixup into the provided data fragment, at
107  /// the offset specified by the fixup and following the fixup kind as
108  /// appropriate. Errors (such as an out of range fixup value) should be
109  /// reported via \p Ctx.
110  /// The \p STI is present only for fragments of type MCRelaxableFragment and
111  /// MCDataFragment with hasInstructions() == true.
112  virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
114  uint64_t Value, bool IsResolved,
115  const MCSubtargetInfo *STI) const = 0;
116 
117  /// Check whether the given target requires emitting differences of two
118  /// symbols as a set of relocations.
119  virtual bool requiresDiffExpressionRelocations() const { return false; }
120 
121  /// @}
122 
123  /// \name Target Relaxation Interfaces
124  /// @{
125 
126  /// Check whether the given instruction may need relaxation.
127  ///
128  /// \param Inst - The instruction to test.
129  /// \param STI - The MCSubtargetInfo in effect when the instruction was
130  /// encoded.
131  virtual bool mayNeedRelaxation(const MCInst &Inst,
132  const MCSubtargetInfo &STI) const = 0;
133 
134  /// Target specific predicate for whether a given fixup requires the
135  /// associated instruction to be relaxed.
136  virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
137  uint64_t Value,
138  const MCRelaxableFragment *DF,
139  const MCAsmLayout &Layout,
140  const bool WasForced) const;
141 
142  /// Simple predicate for targets where !Resolved implies requiring relaxation
143  virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
144  const MCRelaxableFragment *DF,
145  const MCAsmLayout &Layout) const = 0;
146 
147  /// Relax the instruction in the given fragment to the next wider instruction.
148  ///
149  /// \param Inst The instruction to relax, which may be the same as the
150  /// output.
151  /// \param STI the subtarget information for the associated instruction.
152  /// \param [out] Res On return, the relaxed instruction.
153  virtual void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
154  MCInst &Res) const = 0;
155 
156  /// @}
157 
158  /// Returns the minimum size of a nop in bytes on this target. The assembler
159  /// will use this to emit excess padding in situations where the padding
160  /// required for simple alignment would be less than the minimum nop size.
161  ///
162  virtual unsigned getMinimumNopSize() const { return 1; }
163 
164  /// Write an (optimal) nop sequence of Count bytes to the given output. If the
165  /// target cannot generate such a sequence, it should return an error.
166  ///
167  /// \return - True on success.
168  virtual bool writeNopData(raw_ostream &OS, uint64_t Count) const = 0;
169 
170  /// Give backend an opportunity to finish layout after relaxation
171  virtual void finishLayout(MCAssembler const &Asm,
172  MCAsmLayout &Layout) const {}
173 
174  /// Handle any target-specific assembler flags. By default, do nothing.
176 
177  /// Generate the compact unwind encoding for the CFI instructions.
178  virtual uint32_t
180  return 0;
181  }
182 
183  /// Check whether a given symbol has been flagged with MICROMIPS flag.
184  virtual bool isMicroMips(const MCSymbol *Sym) const {
185  return false;
186  }
187 
188  /// Handles all target related code padding when starting to write a new
189  /// basic block to an object file.
190  ///
191  /// \param OS The streamer used for writing the padding data and function.
192  /// \param Context the context of the padding, Embeds the basic block's
193  /// parameters.
196  /// Handles all target related code padding after writing a block to an object
197  /// file.
198  ///
199  /// \param Context the context of the padding, Embeds the basic block's
200  /// parameters.
202  /// Handles all target related code padding before writing a new instruction
203  /// to an object file.
204  ///
205  /// \param Inst the instruction.
206  void handleCodePaddingInstructionBegin(const MCInst &Inst);
207  /// Handles all target related code padding after writing an instruction to an
208  /// object file.
209  ///
210  /// \param Inst the instruction.
211  void handleCodePaddingInstructionEnd(const MCInst &Inst);
212 
213  /// Relaxes a fragment (changes the size of the padding) according to target
214  /// requirements. The new size computation is done w.r.t a layout.
215  ///
216  /// \param PF The fragment to relax.
217  /// \param Layout Code layout information.
218  ///
219  /// \returns true iff any relaxation occurred.
220  bool relaxFragment(MCPaddingFragment *PF, MCAsmLayout &Layout);
221 };
222 
223 } // end namespace llvm
224 
225 #endif // LLVM_MC_MCASMBACKEND_H
virtual bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF, unsigned &Size)
Hook to check if extra nop bytes must be inserted for alignment directive.
Definition: MCAsmBackend.h:93
Fragment for adding required padding.
Definition: MCFragment.h:340
void handleCodePaddingBasicBlockEnd(const MCCodePaddingContext &Context)
Handles all target related code padding after writing a block to an object file.
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
virtual bool mayNeedRelaxation(const MCInst &Inst, const MCSubtargetInfo &STI) const =0
Check whether the given instruction may need relaxation.
This represents an "assembler immediate".
Definition: MCValue.h:39
const support::endianness Endian
Definition: MCAsmBackend.h:52
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
virtual bool requiresDiffExpressionRelocations() const
Check whether the given target requires emitting differences of two symbols as a set of relocations...
Definition: MCAsmBackend.h:119
virtual void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI, MCInst &Res) const =0
Relax the instruction in the given fragment to the next wider instruction.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
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:100
virtual ~MCAsmBackend()
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
void handleCodePaddingInstructionEnd(const MCInst &Inst)
Handles all target related code padding after writing an instruction to an object file...
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.
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...
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:73
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
virtual uint32_t generateCompactUnwindEncoding(ArrayRef< MCCFIInstruction >) const
Generate the compact unwind encoding for the CFI instructions.
Definition: MCAsmBackend.h:179
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...
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:84
Streaming object file generation interface.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
virtual unsigned getMinimumNopSize() const
Returns the minimum size of a nop in bytes on this target.
Definition: MCAsmBackend.h:162
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:117
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:270
virtual bool writeNopData(raw_ostream &OS, uint64_t Count) const =0
Write an (optimal) nop sequence of Count bytes to the given output.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:290
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:22
void handleCodePaddingInstructionBegin(const MCInst &Inst)
Handles all target related code padding before writing a new instruction to an object file...
virtual unsigned getNumFixupKinds() const =0
Get the number of target specific fixup kinds.
PowerPC TLS Dynamic Call Fixup
void handleCodePaddingBasicBlockStart(MCObjectStreamer *OS, const MCCodePaddingContext &Context)
Handles all target related code padding when starting to write a new basic block to an object file...
virtual Optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
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...
Target - Wrapper for Target specific information.
virtual void handleAssemblerFlag(MCAssemblerFlag Flag)
Handle any target-specific assembler flags. By default, do nothing.
Definition: MCAsmBackend.h:175
MCAssemblerFlag
Definition: MCDirectives.h:48
MCAsmBackend & operator=(const MCAsmBackend &)=delete
Generic base class for all target subtargets.
uint32_t Size
Definition: Profile.cpp:46
Target independent information on a fixup kind.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:340
bool relaxFragment(MCPaddingFragment *PF, MCAsmLayout &Layout)
Relaxes a fragment (changes the size of the padding) according to target requirements.
LLVM Value Representation.
Definition: Value.h:72
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:41
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
virtual void reset()
lifetime management
Definition: MCAsmBackend.h:55
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...
virtual void finishLayout(MCAssembler const &Asm, MCAsmLayout &Layout) const
Give backend an opportunity to finish layout after relaxation.
Definition: MCAsmBackend.h:171
MCAsmBackend(support::endianness Endian)
virtual bool isMicroMips(const MCSymbol *Sym) const
Check whether a given symbol has been flagged with MICROMIPS flag.
Definition: MCAsmBackend.h:184