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